| #!/usr/bin/env bun |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { |
| rootGet, |
| healthCheckHealthGet, |
| listSessionsSessionsGet, |
| createSessionSessionsPost, |
| startInferenceSessionsSessionIdStartPost, |
| stopInferenceSessionsSessionIdStopPost, |
| deleteSessionSessionsSessionIdDelete |
| } from '../src'; |
|
|
| import type { |
| CreateSessionRequest, |
| SessionStatusResponse |
| } from '../src'; |
|
|
| |
| const BASE_URL = 'http://localhost:8001/api'; |
|
|
| |
| async function waitForSessionStatus( |
| sessionId: string, |
| targetStatus: string, |
| timeoutMs: number = 30000 |
| ): Promise<SessionStatusResponse> { |
| const startTime = Date.now(); |
| |
| while (Date.now() - startTime < timeoutMs) { |
| const sessions = await listSessionsSessionsGet({ baseUrl: BASE_URL }); |
| const session = sessions.data?.find(s => s.session_id === sessionId); |
| |
| if (session && session.status === targetStatus) { |
| return session; |
| } |
| |
| if (session && session.status === 'error') { |
| throw new Error(`Session failed: ${session.error_message}`); |
| } |
| |
| |
| await new Promise(resolve => setTimeout(resolve, 1000)); |
| } |
| |
| throw new Error(`Timeout waiting for session ${sessionId} to reach status ${targetStatus}`); |
| } |
|
|
| |
| async function getSessionStatus(sessionId: string): Promise<SessionStatusResponse> { |
| const sessions = await listSessionsSessionsGet({ baseUrl: BASE_URL }); |
| const session = sessions.data?.find(s => s.session_id === sessionId); |
| |
| if (!session) { |
| throw new Error(`Session ${sessionId} not found`); |
| } |
| |
| return session; |
| } |
|
|
| async function main() { |
| try { |
| console.log('π Checking server health...'); |
| const healthResponse = await rootGet({ baseUrl: BASE_URL }); |
| if (!healthResponse.data) { |
| console.error('β Server is not healthy. Make sure the inference server is running.'); |
| process.exit(1); |
| } |
| console.log('β
Server is healthy!'); |
|
|
| |
| console.log('π Getting detailed server status...'); |
| const detailedHealth = await healthCheckHealthGet({ baseUrl: BASE_URL }); |
| console.log('π Server status:', detailedHealth.data); |
|
|
| |
| const sessionRequest: CreateSessionRequest = { |
| session_id: 'example-session-' + Date.now(), |
| policy_path: 'LaetusH/act_so101_beyond', |
| camera_names: ['front', 'wrist'], |
| transport_server_url: 'http://localhost:8000', |
| workspace_id: null, |
| policy_type: 'act', |
| language_instruction: null |
| }; |
|
|
| console.log('π Creating inference session...'); |
| const sessionResponse = await createSessionSessionsPost({ |
| body: sessionRequest, |
| baseUrl: BASE_URL |
| }); |
| |
| if (!sessionResponse.data) { |
| throw new Error('Failed to create session'); |
| } |
| |
| const session = sessionResponse.data; |
| console.log('β
Session created!'); |
| console.log('π Workspace ID:', session.workspace_id); |
| console.log('π· Camera rooms:', session.camera_room_ids); |
| console.log('π Joint input room:', session.joint_input_room_id); |
| console.log('π― Joint output room:', session.joint_output_room_id); |
|
|
| |
| console.log('βΆοΈ Starting inference...'); |
| await startInferenceSessionsSessionIdStartPost({ |
| path: { session_id: sessionRequest.session_id }, |
| baseUrl: BASE_URL |
| }); |
| console.log('β
Inference started!'); |
|
|
| |
| console.log('β³ Waiting for session to be running...'); |
| const runningStatus = await waitForSessionStatus( |
| sessionRequest.session_id, |
| 'running', |
| 30000 |
| ); |
| console.log('π Session is now running!'); |
|
|
| |
| console.log('π Monitoring session status...'); |
| for (let i = 0; i < 5; i++) { |
| const status: SessionStatusResponse = await getSessionStatus(sessionRequest.session_id); |
| console.log(`π Status: ${status.status}, Stats:`, status.stats); |
| |
| |
| await new Promise(resolve => setTimeout(resolve, 2000)); |
| } |
|
|
| |
| console.log('π Getting all sessions...'); |
| const allSessions = await listSessionsSessionsGet({ baseUrl: BASE_URL }); |
| console.log('π All sessions:', allSessions.data?.map(s => ({ |
| id: s.session_id, |
| status: s.status, |
| policy_path: s.policy_path |
| }))); |
|
|
| |
| console.log('βΉοΈ Stopping inference...'); |
| await stopInferenceSessionsSessionIdStopPost({ |
| path: { session_id: sessionRequest.session_id }, |
| baseUrl: BASE_URL |
| }); |
| console.log('β
Inference stopped!'); |
|
|
| |
| console.log('π§Ή Cleaning up session...'); |
| await deleteSessionSessionsSessionIdDelete({ |
| path: { session_id: sessionRequest.session_id }, |
| baseUrl: BASE_URL |
| }); |
| console.log('β
Session deleted!'); |
|
|
| console.log('π Example completed successfully!'); |
|
|
| } catch (error) { |
| console.error('β Error:', error); |
| process.exit(1); |
| } |
| } |
|
|
| |
| async function quickExample() { |
| try { |
| |
| console.log('π Testing server health...'); |
| const healthResponse = await rootGet({ baseUrl: BASE_URL }); |
| if (!healthResponse.data) { |
| throw new Error('Server health check failed'); |
| } |
| console.log('β
Server is healthy!'); |
| |
| |
| const sessionId = 'quick-example-' + Date.now(); |
| console.log('π Creating session...'); |
| |
| const sessionResponse = await createSessionSessionsPost({ |
| body: { |
| session_id: sessionId, |
| policy_path: 'LaetusH/act_so101_beyond', |
| camera_names: ['front'], |
| transport_server_url: 'http://localhost:8000' |
| }, |
| baseUrl: BASE_URL |
| }); |
|
|
| if (!sessionResponse.data) { |
| throw new Error(`Failed to create session: ${sessionResponse.error?.detail || 'Unknown error'}`); |
| } |
|
|
| console.log('β
Session created!'); |
| console.log('π Workspace ID:', sessionResponse.data.workspace_id); |
| console.log('π· Camera rooms:', sessionResponse.data.camera_room_ids); |
|
|
| |
| console.log('βΆοΈ Starting inference...'); |
| await startInferenceSessionsSessionIdStartPost({ |
| path: { session_id: sessionId }, |
| baseUrl: BASE_URL |
| }); |
| console.log('β
Inference started!'); |
|
|
| |
| console.log('π Checking status...'); |
| await new Promise(resolve => setTimeout(resolve, 2000)); |
| const status = await getSessionStatus(sessionId); |
| console.log(`π Status: ${status.status}`); |
| console.log('π Stats:', status.stats); |
|
|
| |
| console.log('π§Ή Cleaning up...'); |
| await deleteSessionSessionsSessionIdDelete({ |
| path: { session_id: sessionId }, |
| baseUrl: BASE_URL |
| }); |
| console.log('β
Quick example completed!'); |
|
|
| } catch (error) { |
| console.error('β Quick example error:', error); |
| } |
| } |
|
|
| |
| if (import.meta.main) { |
| console.log('=== RobotHub Inference Server Client Example ===\n'); |
| |
| |
| const runQuick = process.argv.includes('--quick'); |
| |
| if (runQuick) { |
| console.log('Running quick example...\n'); |
| await quickExample(); |
| } else { |
| console.log('Running full example...\n'); |
| await main(); |
| } |
| } |