File size: 5,001 Bytes
63ed3a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bun
/**
 * Basic Usage Example for LeRobot Arena Inference Server TypeScript Client
 * 
 * This example demonstrates how to:
 * 1. Create a client instance
 * 2. Check server health
 * 3. Create an inference session
 * 4. Start inference
 * 5. Monitor session status
 * 6. Clean up resources
 */

import { 
  LeRobotInferenceServerClient
} from '../src/index';

import type { 
  CreateSessionRequest,
  SessionStatusResponse 
} from '../src/generated';

async function main() {
  // Create client instance
  const client = new LeRobotInferenceServerClient('http://localhost:8001');

  try {
    console.log('πŸ” Checking server health...');
    const isHealthy = await client.isHealthy();
    if (!isHealthy) {
      console.error('❌ Server is not healthy. Make sure the inference server is running.');
      process.exit(1);
    }
    console.log('βœ… Server is healthy!');

    // Get detailed health info
    const healthInfo = await client.getHealth();
    console.log('πŸ“Š Server status:', healthInfo);

    // Create a session (using generated types)
    const sessionRequest: CreateSessionRequest = {
      session_id: 'example-session-' + Date.now(),
      policy_path: './checkpoints/act_so101_beyond', // Update with your model path
      camera_names: ['front', 'wrist'], // Update with your camera names
      arena_server_url: 'http://localhost:8000', // Update with your arena server URL
      workspace_id: null // Let the server generate a workspace ID
    };

    console.log('πŸš€ Creating inference session...');
    const session = await client.createSession(sessionRequest);
    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);

    // Start inference
    console.log('▢️ Starting inference...');
    await client.startInference(sessionRequest.session_id);
    console.log('βœ… Inference started!');

    // Wait for the session to be running
    console.log('⏳ Waiting for session to be running...');
    const runningStatus = await client.waitForSessionStatus(
      sessionRequest.session_id, 
      'running', 
      30000 // 30 second timeout
    );
    console.log('πŸƒ Session is now running!');

    // Monitor the session for a few seconds
    console.log('πŸ“Š Monitoring session status...');
    for (let i = 0; i < 5; i++) {
      const status: SessionStatusResponse = await client.getSessionStatus(sessionRequest.session_id);
      console.log(`πŸ“ˆ Status: ${status.status}, Stats:`, status.stats);
      
      // Wait 2 seconds before next check
      await new Promise(resolve => setTimeout(resolve, 2000));
    }

    // Get system info for debugging
    console.log('πŸ”§ Getting system information...');
    const systemInfo = await client.getSystemInfo();
    console.log('πŸ’» System info:', systemInfo);

    // Get session queue info
    console.log('πŸ“‹ Getting session queue info...');
    const queueInfo = await client.getSessionQueueInfo(sessionRequest.session_id);
    console.log('πŸ“ Queue info:', queueInfo);

    // Stop inference
    console.log('⏹️ Stopping inference...');
    await client.stopInference(sessionRequest.session_id);
    console.log('βœ… Inference stopped!');

    // Clean up - delete the session
    console.log('🧹 Cleaning up session...');
    await client.deleteSession(sessionRequest.session_id);
    console.log('βœ… Session deleted!');

    console.log('πŸŽ‰ Example completed successfully!');

  } catch (error) {
    console.error('❌ Error:', error);
    process.exit(1);
  }
}

// Alternative: Using the convenience function
async function quickExample() {
  const client = new LeRobotInferenceServerClient('http://localhost:8001');

  try {
    // This creates a session and starts inference in one call
    const result = await client.createAndStartSession({
      session_id: 'quick-example-' + Date.now(),
      policy_path: './checkpoints/act_so101_beyond',
      camera_names: ['front'],
      arena_server_url: 'http://localhost:8000'
    });

    console.log('πŸš€ Quick session created and started!');
    console.log('Session:', result.session);
    console.log('Status:', result.status);

    // Clean up
    await client.deleteSession(result.status.session_id);
    console.log('βœ… Quick example completed!');

  } catch (error) {
    console.error('❌ Quick example error:', error);
  }
}

// Run the main example
if (import.meta.main) {
  console.log('=== LeRobot Arena Inference Server Client Example ===\n');
  
  // Choose which example to run based on command line argument
  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();
  }
}