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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# User Story 005: Building Block API - Clean, Composable, Unified

## 🎯 Goal

Create a clean building block API across Python lerobot, Node.js lerobot.js, and Web lerobot.js where users manage state and workflow while we abstract away robotics complexity. Users learn core concepts once and apply them everywhere.

## 🚨 Problem Statement

Currently, the APIs have fundamentally different approaches:

### Current Inconsistencies

1. **State Management**:
   - Python/Node.js: Functions handle connection lifecycle internally
   - Web: User must manually manage controllers and connections
2. **Complexity Exposure**:
   - Python/Node.js: `calibrate(config)` - single call does everything
   - Web: Multi-step `controller.performHomingStep()`, `controller.moveToPosition()`, etc.
3. **Resource Management**:
   - Python/Node.js: Each function connects/disconnects automatically
   - Web: User manages port lifecycle manually
4. **Device Identification**:
   - Node.js: Port discovery finds system ports by name (COM4, /dev/ttyUSB0)
   - Web: No device identification - WebSerial can't distinguish between identical devices

### User Pain Points

- **Different Mental Models**: Same operations require different approaches per platform
- **Web Complexity Exposure**: Users must understand servo protocols and multi-step procedures
- **Inconsistent Resource Management**: Some platforms auto-manage, others don't
- **Device Identification Issues**: Web can't identify which physical robot is connected
- **Hidden State Problems**: Users can't control when connections happen or track device status

## 🎯 Success Criteria

### Primary Goal: Building Block Consistency

- [ ] **Same Operation Patterns**: All platforms use identical patterns for core operations
- [ ] **Clean Responsibility Split**: Users manage state/UI, library handles robotics
- [ ] **Self-Contained Operations**: Each function manages its own connection lifecycle
- [ ] **Unified Control Model**: Long-running operations use same session-based control

### Secondary Goal: Platform-Appropriate Implementation

- [ ] **Smart Device Pairing**: Web uses WebUSB + WebSerial for device identification + communication
- [ ] **Hidden Robotics Complexity**: Baud rates, protocols, servo commands abstracted away
- [ ] **Graceful Resource Management**: Automatic connection cleanup with user control
- [ ] **Consistent Error Handling**: Platform-specific errors mapped to common patterns

## πŸ“‹ Ground Truth Decisions

### 1. Building Block Philosophy

**DECIDED: User manages state, we provide clean tools**

βœ… **User Responsibilities:**

- Device pairing and storage (when to pair, how to persist)
- UI workflow (when to show dialogs, how to display progress)
- State management (which devices they have, tracking connections)
- Error handling UI (how to display errors to end users)

βœ… **Library Responsibilities:**

- WebUSB + WebSerial pairing dialogs (native browser interactions)
- Baud rate configuration (1000000 for SO-100)
- STS3215 servo protocol implementation
- Calibration and teleoperation procedures
- Resource conflict prevention with clear error messages

### 2. Device Pairing vs Connection Pattern

**DECIDED: Separate pairing (once) from connection (per operation)**

```javascript
// PAIRING: One-time device identification (stores device reference)
const deviceInfo = await lerobot.pairDevice();
// User stores this: localStorage, state management, etc.

// OPERATIONS: Each function connects internally using device reference
const session = await lerobot.calibrate({ robot: { device: deviceInfo } });
await session.stop(); // Graceful disconnect

const session2 = await lerobot.teleoperate({ robot: { device: deviceInfo } });
await session2.stop(); // Independent connection lifecycle
```

**Web Implementation Details:**

- `pairDevice()` shows WebUSB dialog β†’ gets device info β†’ shows WebSerial dialog β†’ extracts serial ID
- Each operation connects to paired device internally (no singleton state)
- Users never see servo protocols, baud rates, or connection management

### 3. Connection Lifecycle Pattern

**DECIDED: Each operation is self-contained (like Python/Node.js)**

```javascript
// PYTHON/NODE.JS CURRENT PATTERN (keep this):
await calibrate(config); // Internally: connect β†’ calibrate β†’ disconnect
await teleoperate(config); // Internally: connect β†’ teleoperate β†’ disconnect

// WEB NEW PATTERN (match Python/Node.js):
const cal = await calibrate({ robot: { device: pairedDevice } }); // connect β†’ start calibration
await cal.stop(); // disconnect

const tel = await teleoperate({ robot: { device: pairedDevice } }); // connect β†’ start teleoperation
await tel.stop(); // disconnect
```

**Key Principle:** No shared connections, no singletons, each operation manages its own lifecycle.

### 4. Long-Running Operation Control

**DECIDED: Both calibrate and teleoperate are long-running until user stops**

```javascript
// UNIFIED SESSION PATTERN for both operations:
interface RobotSession {
  stop(): Promise<void>; // Graceful shutdown + disconnect
  getState(): SessionState; // Current operation state
}

// Both operations return controllable sessions:
const calibration = await lerobot.calibrate({
  robot: { device: myDevice },
  onStateChange: (state) => updateUI(state),
});

const teleoperation = await lerobot.teleoperate({
  robot: { device: myDevice },
  onStateChange: (state) => updateUI(state),
});

// Same control pattern for both:
await calibration.stop();
await teleoperation.stop();
```

**Calibrate Reality Check:**

- Starts calibration procedure
- Waits for user to move robot through ranges
- Records positions until user decides to stop
- Can run indefinitely like teleoperate

### 5. Web Device Identification Solution

**DECIDED: WebUSB + WebSerial both required**

```javascript
// Why both APIs are needed:
// 1. WebUSB: Get device serial number β†’ "This is robot arm #ABC123"
// 2. WebSerial: Actual communication β†’ Send servo commands

async function pairDevice(): Promise<DeviceInfo> {
  // Step 1: WebUSB for device identification
  const usbDevice = await navigator.usb.requestDevice({...});
  const serialId = extractSerialNumber(usbDevice);

  // Step 2: WebSerial for communication
  const serialPort = await navigator.serial.requestPort();
  await serialPort.open({ baudRate: 1000000 }); // We handle baud rate

  return {
    serialId,
    usbDevice,
    serialPort,
    type: detectRobotType(usbDevice) // We detect SO-100 vs other robots
  };
}
```

## πŸ› οΈ Implementation Architecture

### Core Building Blocks

```javascript
// DEVICE PAIRING (Web-specific, one-time)
export async function pairDevice(): Promise<DeviceInfo>

// UNIFIED OPERATIONS (Same across all platforms)
export async function calibrate(config: CalibrationConfig): Promise<CalibrationSession>
export async function teleoperate(config: TeleoperationConfig): Promise<TeleoperationSession>

// UNIFIED SESSION CONTROL
interface RobotSession {
  stop(): Promise<void>
  getState(): SessionState
}
```

### Platform-Specific Implementation

**Node.js (No changes needed - already correct pattern):**

```javascript
// Each function connects/disconnects internally - keep as-is
export async function calibrate(config) {
  const device = createDevice(config.robot);
  await device.connect();
  // ... calibration work
  await device.disconnect();
}
```

**Web (New unified wrapper):**

```javascript
// Hide controller complexity behind unified interface
export async function calibrate(config) {
  let connection = null;
  try {
    connection = await connectToDevice(config.robot.device); // We handle WebSerial setup
    const session = new CalibrationSession(connection, config);
    return session; // User controls session lifecycle
  } catch (error) {
    if (connection) await connection.disconnect();
    throw error;
  }
}
```

## πŸ“‹ Detailed API Specification

### 1. Device Pairing API (Web Only)

```javascript
// Web-specific device pairing
interface DeviceInfo {
  serialId: string;        // From WebUSB device descriptor
  robotType: 'so100_follower' | 'so100_leader';
  usbVendorId: number;
  usbProductId: number;
  // Internal connection details (user doesn't need to know)
}

export async function pairDevice(): Promise<DeviceInfo>
```

**User Workflow:**

1. Call `pairDevice()` β†’ Native browser dialogs appear
2. Store `DeviceInfo` in their state management
3. Use stored device for all subsequent operations

### 2. Unified Calibration API

```javascript
interface CalibrationConfig {
  robot: {
    device: DeviceInfo;     // Web: paired device, Node.js: { type, port }
  };
  onStateChange?: (state: 'connecting' | 'connected' | 'active' | 'stopping' | 'stopped') => void;
  onProgress?: (progress: CalibrationProgress) => void;
}

interface CalibrationSession extends RobotSession {
  stop(): Promise<CalibrationResults>;  // Returns calibration data
}

// SAME across all platforms:
export async function calibrate(config: CalibrationConfig): Promise<CalibrationSession>
```

### 3. Unified Teleoperation API

```javascript
interface TeleoperationConfig {
  robot: {
    device: DeviceInfo;     // Web: paired device, Node.js: { type, port }
  };
  controls?: {
    [key: string]: ControlMapping;
  };
  onStateChange?: (state: SessionState) => void;
}

interface TeleoperationSession extends RobotSession {
  updateKeyState(key: string, pressed: boolean): void; // For keyboard control
  getCurrentPositions(): Promise<Record<string, number>>;
}

// SAME across all platforms:
export async function teleoperate(config: TeleoperationConfig): Promise<TeleoperationSession>
```

### 4. Error Handling

```javascript
// Standard error types across platforms
class DeviceNotPairedError extends Error {}
class DeviceConnectionError extends Error {}
class DeviceAlreadyConnectedError extends Error {}
class CalibrationError extends Error {}
class TeleoperationError extends Error {}

// Platform-specific errors mapped to standard types
// Web: WebSerial/WebUSB errors β†’ DeviceConnectionError
// Node.js: SerialPort errors β†’ DeviceConnectionError
```

## πŸ”„ User Experience Flows

### First-Time Web User

```javascript
// 1. Pair device once (shows native browser dialogs)
const myRobot = await lerobot.pairDevice();
localStorage.setItem("myRobot", JSON.stringify(myRobot));

// 2. Use paired device for operations
const calibration = await lerobot.calibrate({
  robot: { device: myRobot },
  onStateChange: (state) => updateStatus(state),
});

// 3. User controls session
await calibration.stop();
```

### Returning Web User

```javascript
// 1. Load stored device (no browser dialogs)
const myRobot = JSON.parse(localStorage.getItem("myRobot"));

// 2. Operations work immediately (connect internally)
const teleoperation = await lerobot.teleoperate({
  robot: { device: myRobot },
  onStateChange: (state) => updateStatus(state),
});
```

### Node.js User (Unchanged)

```javascript
// Same as current - no changes needed
const calibration = await lerobot.calibrate({
  robot: { type: "so100_follower", port: "COM4" },
});
await calibration.stop();
```

## πŸ§ͺ Implementation Phases

### Phase 1: Core Building Blocks

- [ ] Define unified interfaces (`RobotSession`, `DeviceInfo`)
- [ ] Implement Web `pairDevice()` with WebUSB + WebSerial
- [ ] Create session-based wrappers for Web calibration/teleoperation
- [ ] Standardize error types and mapping

### Phase 2: API Unification

- [ ] Ensure Node.js returns session objects (minimal changes)
- [ ] Web operations return sessions that hide controller complexity
- [ ] Unified state change callbacks and progress reporting
- [ ] Cross-platform testing with real hardware

### Phase 3: Developer Experience

- [ ] Update examples to use building block pattern
- [ ] Create migration guide from current APIs
- [ ] Unified documentation with platform-specific notes
- [ ] Performance optimization and bundle size analysis

## πŸ“Š Success Metrics

### Developer Experience

- [ ] **Single Learning Curve**: Core concepts (pairing, sessions, operations) work everywhere
- [ ] **User Control**: Developers control state, UI, and workflow completely
- [ ] **Platform Abstraction**: Robotics complexity hidden, platform differences minimal

### Technical Quality

- [ ] **Resource Management**: No connection leaks, graceful cleanup
- [ ] **Error Handling**: Clear, actionable errors with consistent types
- [ ] **Performance**: No regressions, efficient resource usage

### API Consistency

- [ ] **Same Patterns**: Operations, sessions, and control work identically
- [ ] **Platform Appropriate**: Each platform uses native capabilities optimally
- [ ] **Migration Path**: Existing code can adopt new patterns incrementally

## πŸ” Edge Cases & Considerations

### Multiple Device Management

```javascript
// User manages multiple devices
const robot1 = await lerobot.pairDevice(); // First robot
const robot2 = await lerobot.pairDevice(); // Second robot

// Independent sessions
const cal1 = await lerobot.calibrate({ robot: { device: robot1 } });
const tel2 = await lerobot.teleoperate({ robot: { device: robot2 } });
```

### Resource Conflicts

```javascript
// Library prevents conflicts with clear errors
const session1 = await lerobot.calibrate({ robot: { device: myRobot } });

try {
  const session2 = await lerobot.teleoperate({ robot: { device: myRobot } });
} catch (error) {
  if (error instanceof DeviceAlreadyConnectedError) {
    // User gets clear message: "Device already in use by calibration. Stop calibration first."
    await session1.stop();
    const session2 = await lerobot.teleoperate({ robot: { device: myRobot } });
  }
}
```

### Browser Tab Management

```javascript
// Sessions automatically cleanup on page unload
window.addEventListener("beforeunload", async () => {
  if (currentSession) {
    await currentSession.stop(); // Graceful disconnect
  }
});
```

## πŸ“ Definition of Done

- [ ] **Web Pairing**: `pairDevice()` uses WebUSB + WebSerial to identify and store device info
- [ ] **Session Control**: Both `calibrate()` and `teleoperate()` return controllable sessions
- [ ] **Self-Contained Operations**: Each operation connects/disconnects internally, no shared state
- [ ] **Unified Error Handling**: Platform-specific errors mapped to consistent error types
- [ ] **Resource Management**: Clear error messages for conflicts, automatic cleanup
- [ ] **Documentation**: Single API reference with platform-specific implementation notes
- [ ] **Migration Path**: Existing users can adopt new patterns without breaking changes
- [ ] **Hardware Validation**: Tested with real SO-100 hardware on all platforms

---

**Priority**: πŸ”΄ Critical - Blocks adoption due to API inconsistency
**Effort**: πŸ”₯πŸ”₯πŸ”₯ High - Significant Web refactoring, but Node.js mostly unchanged
**Impact**: πŸš€πŸš€πŸš€ Transformative - Enables true cross-platform development

**Impact**: πŸš€πŸš€πŸš€ Very High - Transforms user experience and enables widespread adoption