@pollen-robotics/reachy-mini-sdk 1.7.3-main.b44388c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/CHANGELOG.md +188 -0
  2. package/LICENSE +201 -0
  3. package/README.md +129 -0
  4. package/host/APP_AUTHOR_GUIDE.md +646 -0
  5. package/host/README.md +155 -0
  6. package/host/SPEC.md +618 -0
  7. package/host/dist/ReachyHost.d.ts +39 -0
  8. package/host/dist/ReachyHost.d.ts.map +1 -0
  9. package/host/dist/assets/index.d.ts +16 -0
  10. package/host/dist/assets/index.d.ts.map +1 -0
  11. package/host/dist/chunks/index-C3B3FuBC.js +402 -0
  12. package/host/dist/chunks/index-C3B3FuBC.js.map +1 -0
  13. package/host/dist/chunks/mountHost-8f-laxwI.js +48280 -0
  14. package/host/dist/chunks/mountHost-8f-laxwI.js.map +1 -0
  15. package/host/dist/chunks/reachy-mini-sdk-CM-WMoGJ.js +1425 -0
  16. package/host/dist/chunks/reachy-mini-sdk-CM-WMoGJ.js.map +1 -0
  17. package/host/dist/components/ConnectingView.d.ts +13 -0
  18. package/host/dist/components/ConnectingView.d.ts.map +1 -0
  19. package/host/dist/components/EmbedFrame.d.ts +16 -0
  20. package/host/dist/components/EmbedFrame.d.ts.map +1 -0
  21. package/host/dist/components/ErrorView.d.ts +21 -0
  22. package/host/dist/components/ErrorView.d.ts.map +1 -0
  23. package/host/dist/components/LeavingView.d.ts +33 -0
  24. package/host/dist/components/LeavingView.d.ts.map +1 -0
  25. package/host/dist/components/PickerView.d.ts +51 -0
  26. package/host/dist/components/PickerView.d.ts.map +1 -0
  27. package/host/dist/components/ReachyHostShell.d.ts +21 -0
  28. package/host/dist/components/ReachyHostShell.d.ts.map +1 -0
  29. package/host/dist/components/SignInView.d.ts +32 -0
  30. package/host/dist/components/SignInView.d.ts.map +1 -0
  31. package/host/dist/components/StepsProgressIndicator.d.ts +41 -0
  32. package/host/dist/components/StepsProgressIndicator.d.ts.map +1 -0
  33. package/host/dist/components/TopBar.d.ts +52 -0
  34. package/host/dist/components/TopBar.d.ts.map +1 -0
  35. package/host/dist/components/WelcomeBackOverlay.d.ts +28 -0
  36. package/host/dist/components/WelcomeBackOverlay.d.ts.map +1 -0
  37. package/host/dist/embed/index.d.ts +150 -0
  38. package/host/dist/embed/index.d.ts.map +1 -0
  39. package/host/dist/entry/auto.d.ts +12 -0
  40. package/host/dist/entry/auto.d.ts.map +1 -0
  41. package/host/dist/entry/auto.js +7 -0
  42. package/host/dist/entry/auto.js.map +1 -0
  43. package/host/dist/entry/embed.d.ts +12 -0
  44. package/host/dist/entry/embed.d.ts.map +1 -0
  45. package/host/dist/entry/embed.js +7 -0
  46. package/host/dist/entry/embed.js.map +1 -0
  47. package/host/dist/hooks/useHfProfile.d.ts +7 -0
  48. package/host/dist/hooks/useHfProfile.d.ts.map +1 -0
  49. package/host/dist/hooks/useHostBridge.d.ts +37 -0
  50. package/host/dist/hooks/useHostBridge.d.ts.map +1 -0
  51. package/host/dist/hooks/useOAuth.d.ts +16 -0
  52. package/host/dist/hooks/useOAuth.d.ts.map +1 -0
  53. package/host/dist/hooks/useRobots.d.ts +17 -0
  54. package/host/dist/hooks/useRobots.d.ts.map +1 -0
  55. package/host/dist/hooks/useSdk.d.ts +16 -0
  56. package/host/dist/hooks/useSdk.d.ts.map +1 -0
  57. package/host/dist/index.d.ts +25 -0
  58. package/host/dist/index.d.ts.map +1 -0
  59. package/host/dist/index.js +15 -0
  60. package/host/dist/index.js.map +1 -0
  61. package/host/dist/lib/centralListener.d.ts +73 -0
  62. package/host/dist/lib/centralListener.d.ts.map +1 -0
  63. package/host/dist/lib/centralRest.d.ts +35 -0
  64. package/host/dist/lib/centralRest.d.ts.map +1 -0
  65. package/host/dist/lib/protocol.d.ts +230 -0
  66. package/host/dist/lib/protocol.d.ts.map +1 -0
  67. package/host/dist/lib/protocol.js +48 -0
  68. package/host/dist/lib/protocol.js.map +1 -0
  69. package/host/dist/lib/sdk-types.d.ts +46 -0
  70. package/host/dist/lib/sdk-types.d.ts.map +1 -0
  71. package/host/dist/lib/settings.d.ts +69 -0
  72. package/host/dist/lib/settings.d.ts.map +1 -0
  73. package/host/dist/lib/signalingUrl.d.ts +28 -0
  74. package/host/dist/lib/signalingUrl.d.ts.map +1 -0
  75. package/host/dist/lib/theme.d.ts +4 -0
  76. package/host/dist/lib/theme.d.ts.map +1 -0
  77. package/host/dist/lib/themeMode.d.ts +9 -0
  78. package/host/dist/lib/themeMode.d.ts.map +1 -0
  79. package/host/dist/lib/tokens.d.ts +64 -0
  80. package/host/dist/lib/tokens.d.ts.map +1 -0
  81. package/host/dist/mountHost.d.ts +36 -0
  82. package/host/dist/mountHost.d.ts.map +1 -0
  83. package/package.json +109 -0
  84. package/reachy-mini-sdk.d.ts +285 -0
  85. package/reachy-mini-sdk.js +2164 -0
@@ -0,0 +1,2164 @@
1
+ /**
2
+ * reachy-mini.js — Browser SDK for controlling a Reachy Mini robot over WebRTC.
3
+ * https://github.com/pollen-robotics/reachy-mini
4
+ *
5
+ * QUICK START
6
+ * ───────────
7
+ * import { ReachyMini } from "./reachy-mini.js";
8
+ * const robot = new ReachyMini();
9
+ *
10
+ * // 1. Auth (HuggingFace OAuth — required for the signaling server)
11
+ * if (!await robot.authenticate()) { robot.login(); return; }
12
+ *
13
+ * // 2. Connect to signaling server (SSE)
14
+ * await robot.connect();
15
+ *
16
+ * // 3. Pick a robot once the list arrives
17
+ * robot.addEventListener("robotsChanged", (e) => {
18
+ * const robots = e.detail.robots; // [{ id, meta: { name } }, ...]
19
+ * });
20
+ *
21
+ * // 4. Start a WebRTC session (resolves when video + data channel ready)
22
+ * const detach = robot.attachVideo(document.querySelector("video"));
23
+ * await robot.startSession(robotId);
24
+ *
25
+ * // 5. Send commands — degree-friendly helpers, all built on setTarget()
26
+ * robot.setHeadRpyDeg(0, 10, -5); // roll, pitch, yaw in degrees
27
+ * robot.setAntennasDeg(30, -30); // right, left in degrees
28
+ * robot.setBodyYawDeg(15); // body yaw in degrees
29
+ *
30
+ * // …or compose an atomic update in raw wire units (full SE(3); no XYZ loss):
31
+ * robot.setTarget({
32
+ * head: rpyToMatrix(0, 10, -5).flat(), // number[16] flat row-major 4×4
33
+ * antennas: [degToRad(30), degToRad(-30)],
34
+ * body_yaw: degToRad(15),
35
+ * });
36
+ * robot.playSound("wake_up.wav"); // filename on robot
37
+ * const ver = await robot.getVersion(); // e.g. "1.5.1"
38
+ *
39
+ * // 6. Receive live state (emitted every ~500 ms while streaming; call
40
+ * // robot.requestState() yourself for higher rates — see its JSDoc).
41
+ * // State payload is the daemon's raw wire shape — use the math
42
+ * // utilities exported from this module for degree conversions.
43
+ * robot.addEventListener("state", (e) => {
44
+ * const { head, antennas, body_yaw, motor_mode, is_move_running } = e.detail;
45
+ * // head: number[16] — flat row-major 4×4 (full SE(3))
46
+ * // antennas: [rightRad, leftRad]
47
+ * // body_yaw: number — radians
48
+ * // motor_mode: "enabled" | "disabled" | "gravity_compensation"
49
+ * // is_move_running: boolean
50
+ * // For human-friendly head RPY:
51
+ * // const rpy = matrixToRpy(head); // { roll, pitch, yaw } in degrees
52
+ * });
53
+ *
54
+ * // 7. Audio controls
55
+ * robot.setAudioMuted(false); // unmute robot speaker (muted by default)
56
+ * robot.setMicMuted(false); // unmute your mic → robot speaker (if supported)
57
+ *
58
+ * // 8. Cleanup
59
+ * detach(); // remove video binding
60
+ * await robot.stopSession(); // back to 'connected'
61
+ * robot.disconnect(); // back to 'disconnected' (keeps auth)
62
+ * robot.logout(); // clear HF credentials too
63
+ *
64
+ *
65
+ * STATE MACHINE
66
+ * ─────────────
67
+ * 'disconnected' ──connect()──▸ 'connected' ──startSession()──▸ 'streaming'
68
+ * ▴ disconnect() ▴ stopSession()
69
+ * └─────────────────────────────┘
70
+ *
71
+ *
72
+ * CONSTRUCTOR OPTIONS
73
+ * ───────────────────
74
+ * new ReachyMini({
75
+ * signalingUrl: string, // default: "https://pollen-robotics-reachy-mini-central.hf.space"
76
+ * enableMicrophone: boolean, // default: true — acquire mic for bidirectional audio
77
+ * videoJitterBufferTargetMs: number, // default: 0 — receiver-side jitter buffer hint, ms
78
+ * // 0 = "render ASAP" (teleop). Spec range [0, 4000].
79
+ * // Raise (100–400) on flaky links to trade latency for resilience.
80
+ * autoStartFromUrl: boolean, // default: false — when true AND the URL carries a `robot_peer_id` hint,
81
+ * // auto-call `startSession(preselectedRobotId)` after
82
+ * // `connect()` resolves and that robot appears online.
83
+ * // One-shot per page load; suits iframe-embedded apps
84
+ * // that want zero-tap entry from the host shell.
85
+ * })
86
+ *
87
+ *
88
+ * READ-ONLY PROPERTIES
89
+ * ────────────────────
90
+ * .state "disconnected" | "connected" | "streaming"
91
+ * .robots Array<{ id: string, meta: { name: string } }>
92
+ * .robotState Mirror of the latest "state" event detail —
93
+ * { head: number[16], antennas: [rightRad, leftRad],
94
+ * body_yaw, motor_mode, is_move_running }
95
+ * (fields only present once the daemon sends them;
96
+ * see EVENTS below)
97
+ * .username string | null — HF username after authenticate()
98
+ * .isAuthenticated boolean — true if a valid HF token is available
99
+ * .micSupported boolean — true if robot offers bidirectional audio
100
+ * .micMuted boolean — your microphone mute state
101
+ * .audioMuted boolean — robot speaker mute state (local)
102
+ * .preselectedRobotId string | null — peer id from `?robot_peer_id=` /
103
+ * `#robot_peer_id=`; null if absent.
104
+ * Use it to skip your robot picker
105
+ * when a host iframe (e.g. the
106
+ * Reachy Mini mobile shell) embeds
107
+ * this app.
108
+ * .isEmbedded boolean — true iff `preselectedRobotId !==
109
+ * null`. Branch your UX on this:
110
+ * when true, hide the robot picker
111
+ * and your sign-in screen (the
112
+ * host has already handled both).
113
+ *
114
+ *
115
+ * EVENTS (EventTarget — use addEventListener)
116
+ * ──────────────────────────────────────────────
117
+ * "connected" { peerId: string }
118
+ * "disconnected" { reason: string }
119
+ * "robotsChanged" { robots: Array<{ id, meta }> }
120
+ * "streaming" { sessionId: string, robotId: string }
121
+ * "sessionStopped" { reason: string }
122
+ * "state" { head: number[16], // flat row-major 4×4, when daemon sends head_pose
123
+ * antennas: [rightRad, leftRad], // when daemon sends antennas
124
+ * body_yaw: number, // radians, when daemon sends body_yaw
125
+ * motor_mode: string, // when daemon sends motor_mode
126
+ * is_move_running: boolean } // when daemon sends is_move_running
127
+ * "videoTrack" { track: MediaStreamTrack, stream: MediaStream }
128
+ * "micSupported" { supported: boolean }
129
+ * "error" { source: "signaling"|"webrtc"|"robot", error: Error|string }
130
+ *
131
+ *
132
+ * EXPORTS
133
+ * ───────
134
+ * export default ReachyMini;
135
+ * export { ReachyMini, rpyToMatrix, matrixToRpy, degToRad, radToDeg };
136
+ */
137
+
138
+ import {
139
+ oauthHandleRedirectIfPresent,
140
+ oauthLoginUrl,
141
+ } from "@huggingface/hub";
142
+
143
+ // ─── Math utilities ──────────────────────────────────────────────────────────
144
+
145
+ /** @param {number} deg @returns {number} */
146
+ export function degToRad(deg) { return deg * Math.PI / 180; }
147
+
148
+ /** @param {number} rad @returns {number} */
149
+ export function radToDeg(rad) { return rad * 180 / Math.PI; }
150
+
151
+ /**
152
+ * Roll/pitch/yaw (degrees) → 4×4 rotation matrix (ZYX convention).
153
+ * This is the wire format for the robot's `set_target` command.
154
+ * @param {number} rollDeg @param {number} pitchDeg @param {number} yawDeg
155
+ * @returns {number[][]} 4×4 matrix
156
+ */
157
+ export function rpyToMatrix(rollDeg, pitchDeg, yawDeg) {
158
+ const r = degToRad(rollDeg), p = degToRad(pitchDeg), y = degToRad(yawDeg);
159
+ const cy = Math.cos(y), sy = Math.sin(y);
160
+ const cp = Math.cos(p), sp = Math.sin(p);
161
+ const cr = Math.cos(r), sr = Math.sin(r);
162
+ return [
163
+ [cy * cp, cy * sp * sr - sy * cr, cy * sp * cr + sy * sr, 0],
164
+ [sy * cp, sy * sp * sr + cy * cr, sy * sp * cr - cy * sr, 0],
165
+ [-sp, cp * sr, cp * cr, 0],
166
+ [0, 0, 0, 1],
167
+ ];
168
+ }
169
+
170
+ /**
171
+ * Rotation matrix (3×3 or 4×4) → { roll, pitch, yaw } in degrees.
172
+ * @param {number[][]} m @returns {{ roll: number, pitch: number, yaw: number }}
173
+ */
174
+ export function matrixToRpy(m) {
175
+ return {
176
+ roll: radToDeg(Math.atan2(m[2][1], m[2][2])),
177
+ pitch: radToDeg(Math.asin(-m[2][0])),
178
+ yaw: radToDeg(Math.atan2(m[1][0], m[0][0])),
179
+ };
180
+ }
181
+
182
+ // ─── Internal helpers ────────────────────────────────────────────────────────
183
+
184
+ /** Clamp a volume to [0, 100] and round to integer — mirrors the server-side
185
+ * Field(..., ge=0, le=100) validator so calling setVolume(150) doesn't 400. */
186
+ function clampVolume(v) {
187
+ const n = Math.round(Number(v) || 0);
188
+ return Math.max(0, Math.min(100, n));
189
+ }
190
+
191
+ /**
192
+ * Pick up HuggingFace credentials passed via the URL fragment and move them
193
+ * into `sessionStorage`, where `authenticate()` looks them up.
194
+ *
195
+ * This is the bridge that lets a host page (e.g. the Reachy Mini mobile
196
+ * app, or the vibe-coder preview iframe) embed a Space hosting a SDK
197
+ * consumer despite `X-Frame-Options: SAMEORIGIN` on `huggingface.co/login`:
198
+ * the host already holds a valid token (through its own OAuth flow) and
199
+ * appends it to the iframe URL as
200
+ *
201
+ * #hf_token=<jwt>&hf_username=<handle>&hf_token_expires=<iso>
202
+ *
203
+ * Fragments are NOT sent over HTTP, so the credentials never leak to
204
+ * the HF Space backend or to intermediate proxies.
205
+ *
206
+ * Why all three keys: `authenticate()`'s cache check requires the token,
207
+ * the username AND a future expiry to ALL be present in `sessionStorage`,
208
+ * otherwise it returns `false` and the app falls through to a full OAuth
209
+ * round-trip — which can't complete inside an iframe.
210
+ *
211
+ * Called once from the top of `authenticate()` so SDK consumers don't
212
+ * need any boilerplate of their own. We clear the fragment right after
213
+ * reading it so a page reload does not keep the credentials visible in
214
+ * the address bar.
215
+ *
216
+ * No-op when:
217
+ * - there is no `window` (SSR / Worker contexts),
218
+ * - the URL has no fragment,
219
+ * - the fragment carries no `hf_token` (other apps may use the
220
+ * fragment for theme / route / etc.; we leave those alone).
221
+ */
222
+ function consumeFragmentCredentials() {
223
+ if (typeof window === 'undefined' || !window.location.hash) return;
224
+ const raw = window.location.hash.startsWith('#')
225
+ ? window.location.hash.slice(1)
226
+ : window.location.hash;
227
+ let params;
228
+ try { params = new URLSearchParams(raw); } catch (_e) { return; }
229
+ const token = params.get('hf_token');
230
+ if (!token) return;
231
+ // `hf_username` is required by the cache check. Hosts that haven't
232
+ // resolved the user's HF handle yet may pass a literal "user"
233
+ // placeholder; the SDK only uses the value for display and never
234
+ // round-trips it server-side, so the placeholder is harmless.
235
+ const username = params.get('hf_username') || 'user';
236
+ // `hf_token_expires` is a far-future ISO date for personal access
237
+ // tokens (no real expiry). Hosts typically synthesise ~1 year out;
238
+ // we accept whatever was sent and fall back to "1 year from now"
239
+ // if the parameter is missing or unparseable, so a partial fragment
240
+ // still gets the user logged in.
241
+ const expiresParam = params.get('hf_token_expires');
242
+ const expires =
243
+ expiresParam && !Number.isNaN(new Date(expiresParam).getTime())
244
+ ? expiresParam
245
+ : new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString();
246
+ try {
247
+ sessionStorage.setItem('hf_token', token);
248
+ sessionStorage.setItem('hf_username', username);
249
+ sessionStorage.setItem('hf_token_expires', expires);
250
+ } catch (err) {
251
+ console.warn('[reachy-mini] could not persist pre-seeded HF credentials:', err);
252
+ }
253
+ // Strip the auth keys from the address bar but keep any other hash
254
+ // params the app or SDK might care about (theme, embedded, …).
255
+ params.delete('hf_token');
256
+ params.delete('hf_username');
257
+ params.delete('hf_token_expires');
258
+ const remaining = params.toString();
259
+ const cleanUrl =
260
+ window.location.pathname +
261
+ window.location.search +
262
+ (remaining ? '#' + remaining : '');
263
+ try { window.history.replaceState(null, '', cleanUrl); } catch (_e) {}
264
+ }
265
+
266
+ /**
267
+ * Pick up a preselected robot peer id from the URL.
268
+ *
269
+ * Looked up in this order:
270
+ * 1. URL fragment (`#robot_peer_id=<peerId>`)
271
+ * 2. URL query (`?robot_peer_id=<peerId>`)
272
+ *
273
+ * Both spellings are accepted because:
274
+ * - the Reachy Mini mobile shell sends it in the query today,
275
+ * - the vibe-coder preview / future hosts may prefer the fragment for
276
+ * symmetry with `consumeFragmentCredentials`,
277
+ * - the value is NOT a secret (peer ids are public on the central
278
+ * signaling server's robot listing) so query is fine.
279
+ *
280
+ * Returns `null` when no peer id is found in either location, when there
281
+ * is no `window` (SSR / Worker context), or on parse error. Unlike
282
+ * credentials, we do NOT strip the param from the URL: the value is
283
+ * harmless to keep visible and removing it would break tools that read
284
+ * the URL for context.
285
+ *
286
+ * @returns {string|null}
287
+ */
288
+ function readPreselectedRobotIdFromUrl() {
289
+ if (typeof window === 'undefined') return null;
290
+ // 1. Fragment (`#robot_peer_id=…`).
291
+ if (window.location.hash) {
292
+ const raw = window.location.hash.startsWith('#')
293
+ ? window.location.hash.slice(1)
294
+ : window.location.hash;
295
+ try {
296
+ const params = new URLSearchParams(raw);
297
+ const fromHash = params.get('robot_peer_id');
298
+ if (fromHash) return fromHash;
299
+ } catch (_e) { /* malformed fragment — fall through */ }
300
+ }
301
+ // 2. Query (`?robot_peer_id=…`).
302
+ if (window.location.search) {
303
+ try {
304
+ const params = new URLSearchParams(window.location.search);
305
+ const fromQuery = params.get('robot_peer_id');
306
+ if (fromQuery) return fromQuery;
307
+ } catch (_e) { /* malformed query — fall through */ }
308
+ }
309
+ return null;
310
+ }
311
+
312
+ /** Check if the audio m= section of an SDP has a=sendrecv (bidirectional audio). */
313
+ function sdpHasAudioSendRecv(sdp) {
314
+ const lines = sdp.split('\r\n');
315
+ let inAudio = false;
316
+ for (const line of lines) {
317
+ if (line.startsWith('m=audio')) inAudio = true;
318
+ else if (line.startsWith('m=')) inAudio = false;
319
+ if (inAudio && line === 'a=sendrecv') return true;
320
+ }
321
+ return false;
322
+ }
323
+
324
+ // ─── ReachyMini class ────────────────────────────────────────────────────────
325
+
326
+ export class ReachyMini extends EventTarget {
327
+
328
+ /** @param {{ signalingUrl?: string, enableMicrophone?: boolean, clientId?: string, appName?: string, videoJitterBufferTargetMs?: number, autoStartFromUrl?: boolean }} [options] */
329
+ constructor(options = {}) {
330
+ super();
331
+ this._signalingUrl = options.signalingUrl || 'https://pollen-robotics-reachy-mini-central.hf.space';
332
+ this._enableMicrophone = options.enableMicrophone !== false;
333
+ this._clientId = options.clientId || null;
334
+ this._appName = options.appName || 'unknown';
335
+ // Hint to the receiver's WebRTC jitter buffer (ms). 0 = "render ASAP",
336
+ // appropriate for teleop. Spec range [0, 4000]. Browsers that don't
337
+ // implement RTCRtpReceiver.jitterBufferTarget fall back to default
338
+ // buffering (~150-200 ms).
339
+ this._videoJitterBufferTargetMs = options.videoJitterBufferTargetMs ?? 0;
340
+ // When true AND the URL carried a `robot_peer_id` hint at
341
+ // construction (so `preselectedRobotId !== null`), the SDK
342
+ // auto-calls `startSession(preselectedRobotId)` as soon as
343
+ // that robot appears in the central's robot list after the
344
+ // app's own `connect()` resolves. Lets host-iframe-embedded
345
+ // consumers (mobile shell, vibe-coder preview) skip their
346
+ // robot picker AND skip the manual `startSession` call —
347
+ // they just `await robot.connect()` and receive a `streaming`
348
+ // event when the SDK has dialed in. One-shot: a manual
349
+ // `stopSession()` followed by another `startSession()` is
350
+ // not auto-replayed. Default `false` keeps the standalone
351
+ // Space behavior unchanged.
352
+ this._autoStartFromUrl = options.autoStartFromUrl === true;
353
+ this._autoStartAttempted = false;
354
+
355
+ this._state = 'disconnected'; // 'disconnected' | 'connected' | 'streaming'
356
+ this._robots = []; // latest robot list from signaling
357
+ this._robotState = {}; // populated from daemon state events (wire shape)
358
+
359
+ // Preselected robot peer id read from the URL at construction
360
+ // time. When a host iframe (typically the Reachy Mini mobile
361
+ // shell) embeds an SDK consumer, it appends the peer id of the
362
+ // robot it's already connected to via
363
+ // `?robot_peer_id=…` (or `#robot_peer_id=…`). Apps can read
364
+ // `robot.preselectedRobotId` and call `startSession(id)`
365
+ // directly to skip their robot picker. Captured ONCE at
366
+ // construction so subsequent URL changes (history navigation,
367
+ // hash mutations from `consumeFragmentCredentials`) don't move
368
+ // the target out from under the consumer.
369
+ this._preselectedRobotId = readPreselectedRobotIdFromUrl();
370
+
371
+ // Auth
372
+ this._token = null;
373
+ this._username = null;
374
+ this._tokenExpires = null;
375
+
376
+ // Signaling
377
+ this._peerId = null;
378
+ this._sseAbortController = null;
379
+
380
+ // WebRTC
381
+ this._pc = null; // RTCPeerConnection
382
+ this._dc = null; // RTCDataChannel (robot commands)
383
+ this._sessionId = null;
384
+ this._selectedRobotId = null;
385
+
386
+ // Audio
387
+ this._micStream = null; // MediaStream from getUserMedia
388
+ this._micMuted = true;
389
+ this._audioMuted = true;
390
+ this._micSupported = false; // set after SDP negotiation
391
+
392
+ // Timers
393
+ this._latencyMonitorId = null;
394
+ this._stateRefreshInterval = null;
395
+
396
+ // getVersion() / getHardwareId() promise plumbing
397
+ this._versionResolve = null;
398
+ this._hardwareIdResolve = null;
399
+
400
+ // Volume getter/setter promise plumbing (get_volume / set_volume).
401
+ // Speaker and microphone are tracked separately so two in-flight
402
+ // requests can't collide on the same slot.
403
+ this._volumeResolve = null;
404
+ this._micVolumeResolve = null;
405
+
406
+ // subscribeLogs(): a Set of {onLine, onError} subscribers. The
407
+ // first add sends `subscribe_logs`; removing the last sends
408
+ // `unsubscribe_logs`. We keep a single daemon-side stream and
409
+ // fan out to local subscribers in `_handleRobotMessage`.
410
+ this._logSubscribers = new Set();
411
+
412
+ // startSession() promise plumbing
413
+ this._sessionResolve = null;
414
+ this._sessionReject = null;
415
+ this._iceConnected = false;
416
+ this._dcOpen = false;
417
+
418
+ // Motion-completion plumbing for wakeUp() / gotoSleep().
419
+ //
420
+ // The daemon's data-channel handler dispatches `wake_up` and
421
+ // `goto_sleep` as async tasks and replies with
422
+ // `{status: "ok", command, completed: true}` when the trajectory
423
+ // ACTUALLY finishes (or with `{error, command}` on failure). We
424
+ // surface that as a Promise so callers can `await robot.gotoSleep()`
425
+ // and chain `setMotorMode('disabled')` without racing the
426
+ // trajectory player.
427
+ //
428
+ // Queues, not single slots: the data-channel protocol has no
429
+ // request IDs, but FIFO ordering is guaranteed by the daemon's
430
+ // serialised dispatcher, so the N-th response matches the N-th
431
+ // request. A queue makes back-to-back calls (e.g. teardown firing
432
+ // on top of an in-flight wake_up) safe; a single slot would
433
+ // silently drop the earlier awaiter.
434
+ this._pendingMotionCompletions = {
435
+ wake_up: [],
436
+ goto_sleep: [],
437
+ };
438
+
439
+ // Set by attachVideo()
440
+ this._videoElement = null;
441
+ }
442
+
443
+ // ─── Read-only properties ────────────────────────────────────────────
444
+
445
+ /** @returns {"disconnected"|"connected"|"streaming"} */
446
+ get state() { return this._state; }
447
+
448
+ /** @returns {Array<{id: string, meta: {name: string}}>} */
449
+ get robots() { return this._robots; }
450
+
451
+ /**
452
+ * Latest robot state (same shape as the "state" event detail).
453
+ * Mirrors the daemon's wire format — fields appear only once the
454
+ * daemon has sent the corresponding source field. Use the exported
455
+ * math utilities (``matrixToRpy``, ``radToDeg``) for human units.
456
+ * @returns {{
457
+ * head?: number[],
458
+ * antennas?: number[],
459
+ * body_yaw?: number,
460
+ * motor_mode?: "enabled"|"disabled"|"gravity_compensation",
461
+ * is_move_running?: boolean,
462
+ * }}
463
+ */
464
+ get robotState() { return this._robotState; }
465
+
466
+ /** @returns {string|null} HuggingFace username, set after authenticate(). */
467
+ get username() { return this._username; }
468
+
469
+ /** @returns {boolean} True if a valid HF token is available. */
470
+ get isAuthenticated() { return !!this._token; }
471
+
472
+ /** @returns {boolean} True if the robot's SDP offered bidirectional audio. */
473
+ get micSupported() { return this._micSupported; }
474
+
475
+ /** @returns {boolean} */
476
+ get micMuted() { return this._micMuted; }
477
+
478
+ /** @returns {boolean} */
479
+ get audioMuted() { return this._audioMuted; }
480
+
481
+ /**
482
+ * Peer id of the robot the embedding host wants this session to
483
+ * target, captured from the URL at construction time. Apps that
484
+ * want to support iframe-embedding without forcing the user to
485
+ * re-pick a robot read this and pass it straight to
486
+ * `startSession()` once `connect()` resolves:
487
+ *
488
+ * await robot.connect();
489
+ * await robot.startSession(robot.preselectedRobotId ?? pickedId);
490
+ *
491
+ * Returns `null` when the URL carries no `robot_peer_id` (typical
492
+ * standalone Space load). The value is also exposed on the
493
+ * "robotsChanged" payload via the `meta` sidecar for
494
+ * convenience, but the most direct read is right here.
495
+ *
496
+ * @returns {string|null}
497
+ */
498
+ get preselectedRobotId() { return this._preselectedRobotId; }
499
+
500
+ /**
501
+ * Convenience flag for apps that want to branch their UX on
502
+ * "am I embedded in a host shell?". True iff the URL carried a
503
+ * `robot_peer_id` hint at construction time (which only happens
504
+ * when a host iframe — mobile shell, vibe-coder preview, etc. —
505
+ * is the parent). Apps typically use it to skip their robot
506
+ * picker and their sign-in screen, since both are duplicated
507
+ * work the host has already done.
508
+ *
509
+ * @returns {boolean}
510
+ */
511
+ get isEmbedded() { return this._preselectedRobotId !== null; }
512
+
513
+ /**
514
+ * Internal: try to honour the `autoStartFromUrl` constructor
515
+ * option. Called from the signaling-message handler after every
516
+ * `robotsChanged` emit, so a robot that comes online after the
517
+ * SDK is already `connected` still triggers the auto-start.
518
+ * No-op unless `autoStartFromUrl` is set, the URL carries a
519
+ * preselect, the SDK is `connected`, the preselected robot is
520
+ * in the latest list, and we haven't already attempted in this
521
+ * page load. Errors are swallowed to a `console.warn` — the
522
+ * normal `startSession` rejection / `sessionRejected` event
523
+ * still fires for app-level handling.
524
+ *
525
+ * Defers the actual `startSession()` call by one macrotask
526
+ * (`setTimeout(..., 0)`) so it runs OUTSIDE the
527
+ * `_handleSignalingMessage` callstack that just processed the
528
+ * `'list'` message. Reproduced on Android WebView: firing
529
+ * `startSession` synchronously inside the SSE handler races the
530
+ * daemon's setup, leading to a connected-but-no-keyframe state
531
+ * where the receiver eternally NACKs and the iframe shows a
532
+ * black <video>. The macrotask-deferral is the minimum nudge
533
+ * that consistently resolves the race in our reproduction; if
534
+ * it ever proves insufficient on slower hardware, bump to
535
+ * a small explicit delay (e.g. 250 ms).
536
+ */
537
+ _maybeAutoStart() {
538
+ if (!this._autoStartFromUrl) return;
539
+ if (this._autoStartAttempted) return;
540
+ if (!this._preselectedRobotId) return;
541
+ if (this._state !== 'connected') return;
542
+ const match = this._robots.find((r) => r.id === this._preselectedRobotId);
543
+ if (!match) return;
544
+ this._autoStartAttempted = true;
545
+ const peerId = this._preselectedRobotId;
546
+ setTimeout(() => {
547
+ // Re-check state in case a manual stopSession / disconnect
548
+ // landed between the schedule and the fire.
549
+ if (this._state !== 'connected') return;
550
+ this.startSession(peerId).catch((err) => {
551
+ console.warn('[reachy-mini] autoStartFromUrl: startSession rejected:', err);
552
+ });
553
+ }, 0);
554
+ }
555
+
556
+ // ─── Auth ────────────────────────────────────────────────────────────
557
+
558
+ /**
559
+ * Check for a valid HuggingFace token.
560
+ *
561
+ * Resolution order:
562
+ * 1. URL fragment hand-off (`#hf_token=…&hf_username=…&hf_token_expires=…`).
563
+ * A host iframe — typically the Reachy Mini mobile app or a
564
+ * vibe-coder preview — can pass credentials through the URL
565
+ * fragment to bypass HF's `X-Frame-Options: SAMEORIGIN` block
566
+ * on `huggingface.co/login`. Seeded into `sessionStorage` and
567
+ * then stripped from the address bar so a page reload does not
568
+ * keep the credentials visible.
569
+ * 2. OAuth redirect callback (standalone Space, first sign-in).
570
+ * 3. `sessionStorage` cache (subsequent loads in any context).
571
+ *
572
+ * @returns {Promise<boolean>} true → token ready, false → call login()
573
+ */
574
+ async authenticate() {
575
+ try {
576
+ // 1. Iframe hand-off. No-op when the URL has no fragment or
577
+ // the fragment carries no `hf_token`, so this is free on
578
+ // standalone Space loads.
579
+ consumeFragmentCredentials();
580
+
581
+ // 2. OAuth redirect callback.
582
+ const result = await oauthHandleRedirectIfPresent();
583
+ if (result) {
584
+ this._username = result.userInfo.preferred_username || result.userInfo.name;
585
+ this._token = result.accessToken;
586
+ this._tokenExpires = result.accessTokenExpiresAt;
587
+ sessionStorage.setItem('hf_token', this._token);
588
+ sessionStorage.setItem('hf_username', this._username);
589
+ sessionStorage.setItem('hf_token_expires', this._tokenExpires);
590
+ return true;
591
+ }
592
+
593
+ // 3. sessionStorage cache. Both paths above also write here,
594
+ // so this is the canonical lookup for any subsequent call.
595
+ const t = sessionStorage.getItem('hf_token');
596
+ const u = sessionStorage.getItem('hf_username');
597
+ const e = sessionStorage.getItem('hf_token_expires');
598
+ if (t && u && e && new Date(e) > new Date()) {
599
+ this._token = t;
600
+ this._username = u;
601
+ this._tokenExpires = e;
602
+ return true;
603
+ }
604
+ return false;
605
+ } catch (e) {
606
+ console.error('Auth error:', e);
607
+ return false;
608
+ }
609
+ }
610
+
611
+ /** Redirect the browser to the HuggingFace OAuth login page. */
612
+ async login() {
613
+ const opts = {};
614
+ if (this._clientId) opts.clientId = this._clientId;
615
+ window.location.href = await oauthLoginUrl(opts);
616
+ }
617
+
618
+ /** Clear stored HF credentials and disconnect everything. */
619
+ logout() {
620
+ sessionStorage.removeItem('hf_token');
621
+ sessionStorage.removeItem('hf_username');
622
+ sessionStorage.removeItem('hf_token_expires');
623
+ this._username = null;
624
+ this._tokenExpires = null;
625
+ this.disconnect();
626
+ }
627
+
628
+ // ─── Lifecycle ───────────────────────────────────────────────────────
629
+
630
+ /**
631
+ * Open SSE signaling connection. Resolves once the server sends `welcome`.
632
+ * Emits "robotsChanged" as robots come and go.
633
+ * @param {string} [token] — HF access token. Omit to use the one from authenticate().
634
+ * @returns {Promise<void>}
635
+ */
636
+ async connect(token) {
637
+ if (this._state !== 'disconnected') throw new Error('Already connected');
638
+ if (token) this._token = token;
639
+ if (!this._token) throw new Error('No token — call authenticate() first or pass a token');
640
+ this._sseAbortController = new AbortController();
641
+
642
+ let res;
643
+ try {
644
+ // Token goes in the Authorization header, not the URL —
645
+ // keeps it out of DevTools Network tab, browser history,
646
+ // Referer, and any server/proxy access log. We use fetch
647
+ // + manual stream reader (below) rather than EventSource
648
+ // specifically to allow custom headers.
649
+ res = await fetch(
650
+ `${this._signalingUrl}/events`,
651
+ {
652
+ signal: this._sseAbortController.signal,
653
+ headers: { 'Authorization': `Bearer ${this._token}` },
654
+ },
655
+ );
656
+ } catch (e) {
657
+ this._sseAbortController = null;
658
+ throw e;
659
+ }
660
+ if (!res.ok) {
661
+ this._sseAbortController = null;
662
+ throw new Error(`HTTP ${res.status}`);
663
+ }
664
+
665
+ return new Promise((resolve, reject) => {
666
+ let welcomed = false;
667
+ const reader = res.body.getReader();
668
+ const decoder = new TextDecoder();
669
+ let buffer = '';
670
+
671
+ const readLoop = async () => {
672
+ try {
673
+ while (true) {
674
+ const { done, value } = await reader.read();
675
+ if (done) break;
676
+ buffer += decoder.decode(value, { stream: true });
677
+ const lines = buffer.split('\n');
678
+ buffer = lines.pop();
679
+ for (const line of lines) {
680
+ if (!line.startsWith('data:')) continue;
681
+ try {
682
+ const msg = JSON.parse(line.slice(5).trim());
683
+ if (!welcomed && msg.type === 'welcome') {
684
+ welcomed = true;
685
+ this._peerId = msg.peerId;
686
+ this._state = 'connected';
687
+ await this._sendToServer({
688
+ type: 'setPeerStatus',
689
+ roles: ['listener'],
690
+ meta: { name: this._appName },
691
+ });
692
+ this._emit('connected', { peerId: msg.peerId });
693
+ resolve();
694
+ }
695
+ this._handleSignalingMessage(msg);
696
+ } catch (_) { /* malformed JSON — skip */ }
697
+ }
698
+ }
699
+ } catch (e) {
700
+ if (e.name !== 'AbortError') {
701
+ this._emit('error', { source: 'signaling', error: e });
702
+ }
703
+ if (!welcomed) { reject(e); return; }
704
+ }
705
+ // SSE stream ended (server closed or network drop)
706
+ if (this._state !== 'disconnected') {
707
+ this._state = 'disconnected';
708
+ this._emit('disconnected', { reason: 'SSE closed' });
709
+ }
710
+ if (!welcomed) reject(new Error('Connection closed before welcome'));
711
+ };
712
+
713
+ readLoop();
714
+ });
715
+ }
716
+
717
+ /**
718
+ * One-shot bring-up: auth → SSE connect → robot selection → session →
719
+ * wake up. The all-in-one entry point that captures the common
720
+ * "embed *or* standalone, just get me streaming" flow so each
721
+ * consumer does not have to re-implement it.
722
+ *
723
+ * What it does, in order:
724
+ * 1. **Auth.** If `this._token` is not set, calls `authenticate()`
725
+ * (which honours the iframe URL-fragment hand-off, the OAuth
726
+ * redirect callback, and the `sessionStorage` cache). Throws if
727
+ * none yield a token — the consumer should call `login()` and
728
+ * retry after the redirect. Pass an explicit `token` to skip
729
+ * `authenticate()` entirely.
730
+ * 2. **Connect.** If `state === 'disconnected'`, opens the SSE
731
+ * signaling channel.
732
+ * 3. **Pick a robot.**
733
+ * - **Embed mode** (`this.isEmbedded`): uses
734
+ * `this._preselectedRobotId` from the URL. No picker callback
735
+ * invoked; we briefly wait for that robot to appear in the
736
+ * SSE list, then proceed.
737
+ * - **Standalone**: GETs `/api/robot-status` for the owner's
738
+ * robots with busy state, dedupes by `install_id`, sorts by
739
+ * freshness. If `autoPickIfSingle` and exactly one free, picks
740
+ * it. Else calls the consumer-supplied `pickRobot(robots)`
741
+ * callback. Throws if neither yields an id.
742
+ * 4. **Start session.** Awaits `startSession(robotId)` (ICE + DC).
743
+ * 5. **Wake up.** Awaits `ensureAwake()` so sliders don't silently
744
+ * no-op against a torque-off robot.
745
+ *
746
+ * @param {{
747
+ * token?: string, // skip authenticate(); use this raw HF token
748
+ * pickRobot?: (robots: Array<{
749
+ * id: string,
750
+ * name: string|null,
751
+ * busy: boolean,
752
+ * activeApp: string|null,
753
+ * meta: object,
754
+ * lastSeenAgeSeconds: number|null,
755
+ * }>) => Promise<string|null>, // called only in standalone, multi-robot case
756
+ * autoPickIfSingle?: boolean, // default true — skip the callback when 1 free robot
757
+ * filterBusy?: boolean, // default true — hide busy robots from the picker
758
+ * wakeOnConnect?: boolean, // default true — call ensureAwake() after startSession
759
+ * }} [options]
760
+ * @returns {Promise<{
761
+ * robotId: string,
762
+ * robotName: string|null,
763
+ * isEmbedded: boolean,
764
+ * alreadyStreaming?: boolean,
765
+ * }>}
766
+ */
767
+ async autoConnect(options = {}) {
768
+ const {
769
+ token = null,
770
+ pickRobot = null,
771
+ autoPickIfSingle = true,
772
+ filterBusy = true,
773
+ wakeOnConnect = true,
774
+ } = options;
775
+
776
+ // Idempotent fast-path: caller invoked autoConnect() on an
777
+ // already-streaming session (e.g. on a route change inside an
778
+ // SPA). Return the current selection rather than tearing down.
779
+ if (this._state === 'streaming') {
780
+ const cur = this._robots?.find((r) => r.id === this._selectedRobotId);
781
+ return {
782
+ robotId: this._selectedRobotId,
783
+ robotName: cur?.meta?.name ?? null,
784
+ isEmbedded: this.isEmbedded,
785
+ alreadyStreaming: true,
786
+ };
787
+ }
788
+
789
+ // autoConnect takes over the bring-up — disable the SDK's
790
+ // own `autoStartFromUrl` so the two paths don't race and
791
+ // both call `startSession()` against the same preselected
792
+ // robot. The race used to manifest as central rejecting the
793
+ // second attempt with "Robot is busy: <appName>" — the
794
+ // appName being our own first attempt. Restored on the way
795
+ // out so a later `stopSession()` followed by a fresh
796
+ // listener attach still benefits from auto-start.
797
+ const _prevAutoStartFromUrl = this._autoStartFromUrl;
798
+ this._autoStartFromUrl = false;
799
+
800
+ try {
801
+ // 1. Auth.
802
+ if (token) {
803
+ this._token = token;
804
+ } else if (!this._token) {
805
+ const ok = await this.authenticate();
806
+ if (!ok) {
807
+ // login() does a full page redirect; we don't trigger
808
+ // it here so the consumer can decide (a desktop tray
809
+ // wants different recovery than a standalone Space).
810
+ throw new Error('Not authenticated — call login() or pass a token');
811
+ }
812
+ }
813
+
814
+ // 2. SSE connect.
815
+ if (this._state === 'disconnected') {
816
+ await this.connect();
817
+ }
818
+
819
+ // 3. Resolve the target robot.
820
+ let robotId;
821
+ let robotName = null;
822
+ if (this.isEmbedded) {
823
+ robotId = this._preselectedRobotId;
824
+ // Wait briefly for the preselected robot to surface in the
825
+ // SSE list. Best-effort: if it never shows we still try
826
+ // startSession() — central may know about a robot the SSE
827
+ // list pushes only a moment later.
828
+ try {
829
+ await this._waitForRobotInList(robotId, 5000);
830
+ } catch (_) { /* fall through */ }
831
+ const found = this._robots?.find((r) => r.id === robotId);
832
+ robotName = found?.meta?.name ?? null;
833
+ } else {
834
+ const robots = await this._fetchOwnedRobots({ filterBusy });
835
+ if (robots.length === 0) {
836
+ throw new Error('No reachable robots');
837
+ }
838
+ if (autoPickIfSingle && robots.length === 1 && !robots[0].busy) {
839
+ robotId = robots[0].id;
840
+ robotName = robots[0].name;
841
+ } else if (pickRobot) {
842
+ const picked = await pickRobot(robots);
843
+ if (!picked) throw new Error('Robot selection cancelled');
844
+ robotId = picked;
845
+ robotName = robots.find((r) => r.id === picked)?.name ?? null;
846
+ } else {
847
+ throw new Error(
848
+ 'Multiple robots available — pass a pickRobot callback to autoConnect()',
849
+ );
850
+ }
851
+ }
852
+
853
+ // 4. Session.
854
+ await this.startSession(robotId);
855
+
856
+ // 5. Wake.
857
+ if (wakeOnConnect && typeof this.ensureAwake === 'function') {
858
+ try { await this.ensureAwake(); }
859
+ catch (e) { console.warn('[reachy-mini] autoConnect: ensureAwake failed:', e); }
860
+ }
861
+
862
+ return { robotId, robotName, isEmbedded: this.isEmbedded };
863
+ } finally {
864
+ this._autoStartFromUrl = _prevAutoStartFromUrl;
865
+ }
866
+ }
867
+
868
+ /**
869
+ * Fetch the caller's robots with busy state, deduped + sorted.
870
+ * One-shot snapshot — no live subscription. Falls back to the SSE
871
+ * `_robots` cache if `/api/robot-status` is unavailable (older
872
+ * central deployments don't expose it).
873
+ *
874
+ * Dedup: same physical robot can appear twice transiently after a
875
+ * daemon reinstall (new peerId, same install_id). Last-writer-wins
876
+ * on `install_id`, then `hardware_id`, then `peerId` (= no dedup).
877
+ *
878
+ * @returns {Promise<Array<{
879
+ * id: string,
880
+ * name: string|null,
881
+ * busy: boolean,
882
+ * activeApp: string|null,
883
+ * meta: object,
884
+ * lastSeenAgeSeconds: number|null,
885
+ * }>>}
886
+ */
887
+ async _fetchOwnedRobots({ filterBusy = true } = {}) {
888
+ try {
889
+ const res = await fetch(`${this._signalingUrl}/api/robot-status`, {
890
+ headers: { 'Authorization': `Bearer ${this._token}` },
891
+ });
892
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
893
+ const json = await res.json();
894
+ const seen = new Map(); // dedup key → projected robot
895
+ for (const r of (json.robots || [])) {
896
+ if (filterBusy && r.busy) continue;
897
+ const key = r.meta?.install_id ?? r.meta?.hardware_id ?? r.peerId;
898
+ seen.set(key, {
899
+ id: r.peerId,
900
+ name: r.robotName ?? r.meta?.name ?? null,
901
+ busy: !!r.busy,
902
+ activeApp: r.activeApp ?? null,
903
+ meta: r.meta ?? {},
904
+ lastSeenAgeSeconds: r.last_seen_age_seconds ?? null,
905
+ });
906
+ }
907
+ return Array.from(seen.values()).sort(
908
+ (a, b) => (a.lastSeenAgeSeconds ?? Infinity) - (b.lastSeenAgeSeconds ?? Infinity),
909
+ );
910
+ } catch (e) {
911
+ console.warn('[reachy-mini] /api/robot-status unavailable, using SSE list:', e);
912
+ return (this._robots || []).map((r) => ({
913
+ id: r.id,
914
+ name: r.meta?.name ?? null,
915
+ busy: false, // unknown — SSE list does not carry busy state
916
+ activeApp: null,
917
+ meta: r.meta ?? {},
918
+ lastSeenAgeSeconds: null,
919
+ }));
920
+ }
921
+ }
922
+
923
+ /**
924
+ * Resolve once `robotId` appears in `_robots`, or reject after
925
+ * `timeoutMs`. Used by `autoConnect()`'s embed branch so the preselected
926
+ * robot has a chance to surface from the first SSE `list` push before
927
+ * `startSession()` is fired.
928
+ */
929
+ _waitForRobotInList(robotId, timeoutMs) {
930
+ if (this._robots?.find((r) => r.id === robotId)) return Promise.resolve();
931
+ return new Promise((resolve, reject) => {
932
+ const onChange = () => {
933
+ if (this._robots?.find((r) => r.id === robotId)) {
934
+ this.removeEventListener('robotsChanged', onChange);
935
+ clearTimeout(timeoutId);
936
+ resolve();
937
+ }
938
+ };
939
+ const timeoutId = setTimeout(() => {
940
+ this.removeEventListener('robotsChanged', onChange);
941
+ reject(new Error(`Timeout waiting for robot ${robotId} in list`));
942
+ }, timeoutMs);
943
+ this.addEventListener('robotsChanged', onChange);
944
+ });
945
+ }
946
+
947
+ /**
948
+ * Start a WebRTC session with the given robot.
949
+ * Acquires the microphone (if enabled), negotiates SDP, and waits for
950
+ * both ICE connection and data channel to be ready before resolving.
951
+ * Emits "videoTrack" when the robot's camera stream arrives.
952
+ * Emits "micSupported" once SDP negotiation reveals whether the robot
953
+ * accepts bidirectional audio.
954
+ * @param {string} robotId — one of the ids from the robots list
955
+ * @returns {Promise<void>}
956
+ */
957
+ async startSession(robotId) {
958
+ if (this._state !== 'connected') throw new Error('Not connected');
959
+ this._selectedRobotId = robotId;
960
+ this._iceConnected = false;
961
+ this._dcOpen = false;
962
+ this._micSupported = false;
963
+ // Buffer for ICE candidates that arrive before the SDP
964
+ // exchange completes (see _handlePeerMessage). Reset on every
965
+ // fresh session so stale candidates from a previous attempt
966
+ // don't get applied to a new RTCPeerConnection.
967
+ this._pendingRemoteIce = [];
968
+
969
+ // Acquire mic eagerly so the browser permission prompt appears now,
970
+ // but tracks stay disabled (muted) until the user explicitly unmutes.
971
+ if (this._enableMicrophone) {
972
+ try {
973
+ this._micStream = await navigator.mediaDevices.getUserMedia({ audio: true });
974
+ this._micStream.getAudioTracks().forEach(t => { t.enabled = false; });
975
+ this._micMuted = true;
976
+ } catch (e) {
977
+ console.warn('Microphone not available:', e);
978
+ // Fall back to a silent placeholder track. We MUST add an
979
+ // audio track before createAnswer or the answer SDP comes
980
+ // back as recvonly for audio - which negotiates the audio
981
+ // SENDER side off the wire entirely. A host that wants to
982
+ // later inject a different audio source (e.g. a synthesised
983
+ // AI voice via replaceTrack on the sender) needs a live
984
+ // sendrecv slot, even if the initial track is silent.
985
+ try {
986
+ const ctx = new (window.AudioContext || window.webkitAudioContext)();
987
+ const dst = ctx.createMediaStreamDestination();
988
+ // A muted oscillator keeps the track "alive" without
989
+ // emitting any audible signal.
990
+ const osc = ctx.createOscillator();
991
+ const gain = ctx.createGain();
992
+ gain.gain.value = 0;
993
+ osc.connect(gain).connect(dst);
994
+ osc.start();
995
+ this._micStream = dst.stream;
996
+ this._micStream.getAudioTracks().forEach(t => { t.enabled = false; });
997
+ this._micMuted = true;
998
+ this._silentMicFallback = { ctx, osc };
999
+ } catch (fallbackErr) {
1000
+ console.warn('Silent mic fallback failed:', fallbackErr);
1001
+ this._micStream = null;
1002
+ }
1003
+ }
1004
+ }
1005
+
1006
+ this._pc = new RTCPeerConnection({
1007
+ iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
1008
+ });
1009
+
1010
+ return new Promise((resolve, reject) => {
1011
+ this._sessionResolve = resolve;
1012
+ this._sessionReject = reject;
1013
+
1014
+ this._pc.ontrack = (e) => {
1015
+ if (e.track.kind === 'video') {
1016
+ // Tell the receiver's jitter buffer to minimise its hold
1017
+ // time. Both properties target the same internal buffer;
1018
+ // browsers ignore whichever they don't implement.
1019
+ const ms = this._videoJitterBufferTargetMs;
1020
+ try { e.receiver.jitterBufferTarget = ms; } catch (_) {}
1021
+ try { e.receiver.playoutDelayHint = ms / 1000; } catch (_) {}
1022
+ this._emit('videoTrack', { track: e.track, stream: e.streams[0] });
1023
+ }
1024
+ };
1025
+
1026
+ this._pc.onicecandidate = async (e) => {
1027
+ if (e.candidate && this._sessionId) {
1028
+ await this._sendToServer({
1029
+ type: 'peer',
1030
+ sessionId: this._sessionId,
1031
+ ice: {
1032
+ candidate: e.candidate.candidate,
1033
+ sdpMLineIndex: e.candidate.sdpMLineIndex,
1034
+ sdpMid: e.candidate.sdpMid,
1035
+ },
1036
+ });
1037
+ }
1038
+ };
1039
+
1040
+ this._pc.oniceconnectionstatechange = () => {
1041
+ const s = this._pc?.iceConnectionState;
1042
+ if (!s) return;
1043
+ if (s === 'connected' || s === 'completed') {
1044
+ this._iceConnected = true;
1045
+ this._checkSessionReady();
1046
+ } else if (s === 'failed') {
1047
+ const err = new Error('ICE connection failed');
1048
+ if (this._sessionReject) {
1049
+ this._sessionReject(err);
1050
+ this._sessionResolve = null;
1051
+ this._sessionReject = null;
1052
+ }
1053
+ this._emit('error', { source: 'webrtc', error: err });
1054
+ } else if (s === 'disconnected') {
1055
+ this._emit('error', { source: 'webrtc', error: new Error('ICE disconnected') });
1056
+ }
1057
+ };
1058
+
1059
+ this._pc.ondatachannel = (e) => {
1060
+ this._dc = e.channel;
1061
+ this._dc.onopen = () => {
1062
+ this._dcOpen = true;
1063
+ this._checkSessionReady();
1064
+ };
1065
+ this._dc.onmessage = (ev) => this._handleRobotMessage(JSON.parse(ev.data));
1066
+ };
1067
+
1068
+ this._sendToServer({ type: 'startSession', peerId: robotId }).then((r) => {
1069
+ if (r?.type === 'sessionRejected') {
1070
+ this._failSessionRejected(r);
1071
+ return;
1072
+ }
1073
+ if (r?.sessionId) this._sessionId = r.sessionId;
1074
+ });
1075
+ });
1076
+ }
1077
+
1078
+ /**
1079
+ * Internal: handle a sessionRejected response from central.
1080
+ * Releases resources allocated by startSession() (RTCPeerConnection,
1081
+ * microphone stream) and rejects the pending startSession() promise
1082
+ * with an Error carrying `.reason` and `.activeApp`.
1083
+ *
1084
+ * Called from both the POST-response path (primary) and the SSE
1085
+ * handler (defensive, in case the server changes).
1086
+ */
1087
+ _failSessionRejected(msg) {
1088
+ const err = new Error(
1089
+ msg.reason === 'robot_busy'
1090
+ ? `Robot is busy: "${msg.activeApp || 'another app'}" is already connected`
1091
+ : `Session rejected: ${msg.reason || 'unknown reason'}`
1092
+ );
1093
+ err.reason = msg.reason;
1094
+ err.activeApp = msg.activeApp;
1095
+
1096
+ // Release resources allocated optimistically in startSession()
1097
+ // before we knew the server would refuse.
1098
+ if (this._pc) { this._pc.close(); this._pc = null; }
1099
+ if (this._micStream) { this._micStream.getTracks().forEach(t => t.stop()); this._micStream = null; }
1100
+ this._iceConnected = false;
1101
+ this._dcOpen = false;
1102
+ this._micMuted = true;
1103
+ this._micSupported = false;
1104
+
1105
+ this._emit('sessionRejected', { reason: msg.reason, activeApp: msg.activeApp });
1106
+
1107
+ if (this._sessionReject) {
1108
+ const reject = this._sessionReject;
1109
+ this._sessionResolve = null;
1110
+ this._sessionReject = null;
1111
+ reject(err);
1112
+ }
1113
+ }
1114
+
1115
+ /**
1116
+ * End the WebRTC session. Returns to "connected" state so you can
1117
+ * startSession() again with the same or a different robot.
1118
+ * @returns {Promise<void>}
1119
+ */
1120
+ async stopSession() {
1121
+ if (this._versionResolve) { this._versionResolve(null); this._versionResolve = null; }
1122
+ if (this._hardwareIdResolve) { this._hardwareIdResolve(null); this._hardwareIdResolve = null; }
1123
+ if (this._volumeResolve) { this._volumeResolve(null); this._volumeResolve = null; }
1124
+ if (this._micVolumeResolve) { this._micVolumeResolve(null); this._micVolumeResolve = null; }
1125
+ // Drop any active log subscribers — the daemon-side subprocess
1126
+ // is torn down on peer-disconnect, so resubscribing across a
1127
+ // reconnect requires a fresh subscribeLogs() call from the
1128
+ // consumer.
1129
+ this._logSubscribers.clear();
1130
+ // Drain any in-flight wakeUp() / gotoSleep() awaiters before
1131
+ // the data channel is killed below, so callers don't sit on a
1132
+ // promise that can never resolve.
1133
+ this._rejectPendingMotionCompletions(new Error('Session stopped'));
1134
+ if (this._sessionReject) {
1135
+ this._sessionReject(new Error('Session stopped'));
1136
+ this._sessionResolve = null;
1137
+ this._sessionReject = null;
1138
+ }
1139
+
1140
+ if (this._stateRefreshInterval) { clearInterval(this._stateRefreshInterval); this._stateRefreshInterval = null; }
1141
+ if (this._latencyMonitorId) { clearInterval(this._latencyMonitorId); this._latencyMonitorId = null; }
1142
+
1143
+ if (this._sessionId) {
1144
+ await this._sendToServer({ type: 'endSession', sessionId: this._sessionId });
1145
+ }
1146
+
1147
+ if (this._micStream) { this._micStream.getTracks().forEach(t => t.stop()); this._micStream = null; }
1148
+ this._micMuted = true;
1149
+ this._micSupported = false;
1150
+
1151
+ if (this._pc) { this._pc.close(); this._pc = null; }
1152
+ if (this._dc) { this._dc.close(); this._dc = null; }
1153
+
1154
+ this._sessionId = null;
1155
+ this._iceConnected = false;
1156
+ this._dcOpen = false;
1157
+
1158
+ const wasStreaming = this._state === 'streaming';
1159
+ if (wasStreaming) {
1160
+ this._state = 'connected';
1161
+ this._emit('sessionStopped', { reason: 'user' });
1162
+ }
1163
+ }
1164
+
1165
+ /**
1166
+ * Full teardown — abort SSE, close WebRTC.
1167
+ * Auth state is preserved (call logout() to also clear credentials).
1168
+ */
1169
+ disconnect() {
1170
+ if (this._sseAbortController) { this._sseAbortController.abort(); this._sseAbortController = null; }
1171
+
1172
+ if (this._versionResolve) { this._versionResolve(null); this._versionResolve = null; }
1173
+ if (this._hardwareIdResolve) { this._hardwareIdResolve(null); this._hardwareIdResolve = null; }
1174
+ if (this._volumeResolve) { this._volumeResolve(null); this._volumeResolve = null; }
1175
+ if (this._micVolumeResolve) { this._micVolumeResolve(null); this._micVolumeResolve = null; }
1176
+ this._logSubscribers.clear();
1177
+ // Same rationale as in stopSession(): drain pending motion
1178
+ // awaiters before tearing down the data channel.
1179
+ this._rejectPendingMotionCompletions(new Error('Disconnected'));
1180
+ if (this._sessionReject) {
1181
+ this._sessionReject(new Error('Disconnected'));
1182
+ this._sessionResolve = null;
1183
+ this._sessionReject = null;
1184
+ }
1185
+
1186
+ if (this._stateRefreshInterval) { clearInterval(this._stateRefreshInterval); this._stateRefreshInterval = null; }
1187
+ if (this._latencyMonitorId) { clearInterval(this._latencyMonitorId); this._latencyMonitorId = null; }
1188
+
1189
+ if (this._sessionId && this._token) {
1190
+ this._sendToServer({ type: 'endSession', sessionId: this._sessionId }); // fire-and-forget
1191
+ }
1192
+
1193
+ if (this._micStream) { this._micStream.getTracks().forEach(t => t.stop()); this._micStream = null; }
1194
+ if (this._pc) { this._pc.close(); this._pc = null; }
1195
+ if (this._dc) { this._dc.close(); this._dc = null; }
1196
+
1197
+ this._sessionId = null;
1198
+ this._micMuted = true;
1199
+ this._micSupported = false;
1200
+ this._iceConnected = false;
1201
+ this._dcOpen = false;
1202
+ this._peerId = null;
1203
+ this._robots = [];
1204
+ this._state = 'disconnected';
1205
+ this._emit('disconnected', { reason: 'user' });
1206
+ }
1207
+
1208
+ // ─── Commands ────────────────────────────────────────────────────────
1209
+ // All return false if the data channel is not open, true if sent.
1210
+
1211
+ /**
1212
+ * Send a target pose to the robot. Wire-shape, raw units only —
1213
+ * single source of truth for motion commands. Every field is
1214
+ * optional; omitted fields leave the daemon's previous target
1215
+ * unchanged, so partial updates compose naturally.
1216
+ *
1217
+ * For human units (degrees), use the ``setHeadRpyDeg`` /
1218
+ * ``setAntennasDeg`` / ``setBodyYawDeg`` thin wrappers below.
1219
+ *
1220
+ * @param {object} [target]
1221
+ * @param {number[]} [target.head] 16-element flat row-major 4×4
1222
+ * matrix (full SE(3); preserves translation, no XYZ loss).
1223
+ * @param {number[]} [target.antennas] ``[rightRad, leftRad]``.
1224
+ * @param {number} [target.body_yaw] Body yaw in radians.
1225
+ * @returns {boolean} false if the data channel is not open.
1226
+ * @throws {TypeError} if any provided field has the wrong shape or
1227
+ * contains a non-finite value (NaN, Infinity). Validation runs at
1228
+ * the JS boundary so caller mistakes surface with a stack trace
1229
+ * pointing to the call site, not as a confusing daemon-side error.
1230
+ */
1231
+ setTarget({ head, antennas, body_yaw } = {}) {
1232
+ const cmd = { type: "set_full_target" };
1233
+ if (head !== undefined) {
1234
+ if (!Array.isArray(head) || head.length !== 16
1235
+ || !head.every((n) => Number.isFinite(n))) {
1236
+ throw new TypeError(
1237
+ 'setTarget: head must be a 16-element flat row-major 4×4 matrix '
1238
+ + `of finite numbers; got ${Array.isArray(head) ? `Array(${head.length})` : typeof head}`
1239
+ );
1240
+ }
1241
+ cmd.head = head;
1242
+ }
1243
+ if (antennas !== undefined) {
1244
+ if (!Array.isArray(antennas) || antennas.length !== 2
1245
+ || !antennas.every((n) => Number.isFinite(n))) {
1246
+ throw new TypeError(
1247
+ 'setTarget: antennas must be [rightRad, leftRad] (2 finite numbers); '
1248
+ + `got ${Array.isArray(antennas) ? `Array(${antennas.length})` : typeof antennas}`
1249
+ );
1250
+ }
1251
+ cmd.antennas = antennas;
1252
+ }
1253
+ if (body_yaw !== undefined) {
1254
+ if (!Number.isFinite(body_yaw)) {
1255
+ throw new TypeError(
1256
+ `setTarget: body_yaw must be a finite number (radians); got ${body_yaw}`
1257
+ );
1258
+ }
1259
+ cmd.body_yaw = body_yaw;
1260
+ }
1261
+ return this._sendCommand(cmd);
1262
+ }
1263
+
1264
+ /**
1265
+ * Smooth daemon-side interpolation to a target pose over
1266
+ * ``duration`` seconds. Mirrors ``setTarget``'s wire shape (head
1267
+ * is a 16-element flat row-major 4×4, antennas are
1268
+ * ``[rightRad, leftRad]``, body_yaw is radians) and adds a
1269
+ * required ``duration`` field. The daemon dispatches the command
1270
+ * to its lerp planner instead of jumping to the target.
1271
+ *
1272
+ * Use this for one-shot smooth approaches to an arbitrary pose
1273
+ * (e.g. soft-return-to-base after recording, or pre-positioning
1274
+ * before a streamed playback). For continuous streamed motion,
1275
+ * use ``setTarget`` and lerp client-side.
1276
+ *
1277
+ * @param {{head?: number[], antennas?: number[], body_yaw?: number, duration: number}} args
1278
+ * @returns {boolean} false if the data channel is not open.
1279
+ * @throws {TypeError} if any provided field has the wrong shape
1280
+ * or contains a non-finite value (NaN, Infinity), or if
1281
+ * ``duration`` is missing or non-positive.
1282
+ */
1283
+ gotoTarget({ head, antennas, body_yaw, duration } = {}) {
1284
+ const cmd = { type: "goto_target" };
1285
+ if (head !== undefined) {
1286
+ if (!Array.isArray(head) || head.length !== 16
1287
+ || !head.every((n) => Number.isFinite(n))) {
1288
+ throw new TypeError(
1289
+ 'gotoTarget: head must be a 16-element flat row-major 4×4 matrix '
1290
+ + `of finite numbers; got ${Array.isArray(head) ? `Array(${head.length})` : typeof head}`
1291
+ );
1292
+ }
1293
+ cmd.head = head;
1294
+ }
1295
+ if (antennas !== undefined) {
1296
+ if (!Array.isArray(antennas) || antennas.length !== 2
1297
+ || !antennas.every((n) => Number.isFinite(n))) {
1298
+ throw new TypeError(
1299
+ 'gotoTarget: antennas must be [rightRad, leftRad] (2 finite numbers); '
1300
+ + `got ${Array.isArray(antennas) ? `Array(${antennas.length})` : typeof antennas}`
1301
+ );
1302
+ }
1303
+ cmd.antennas = antennas;
1304
+ }
1305
+ if (body_yaw !== undefined) {
1306
+ if (!Number.isFinite(body_yaw)) {
1307
+ throw new TypeError(
1308
+ `gotoTarget: body_yaw must be a finite number (radians); got ${body_yaw}`
1309
+ );
1310
+ }
1311
+ cmd.body_yaw = body_yaw;
1312
+ }
1313
+ if (!Number.isFinite(duration) || duration <= 0) {
1314
+ throw new TypeError(
1315
+ `gotoTarget: duration must be a positive finite number (seconds); got ${duration}`
1316
+ );
1317
+ }
1318
+ cmd.duration = duration;
1319
+ return this._sendCommand(cmd);
1320
+ }
1321
+
1322
+ /**
1323
+ * Set head orientation from roll/pitch/yaw in degrees.
1324
+ * Convenience wrapper over ``setTarget``.
1325
+ * @param {number} rollDeg @param {number} pitchDeg @param {number} yawDeg
1326
+ * @returns {boolean}
1327
+ */
1328
+ setHeadRpyDeg(rollDeg, pitchDeg, yawDeg) {
1329
+ return this.setTarget({ head: rpyToMatrix(rollDeg, pitchDeg, yawDeg).flat() });
1330
+ }
1331
+
1332
+ /**
1333
+ * Set antenna positions from degrees.
1334
+ * Convenience wrapper over ``setTarget``.
1335
+ * @param {number} rightDeg @param {number} leftDeg
1336
+ * @returns {boolean}
1337
+ */
1338
+ setAntennasDeg(rightDeg, leftDeg) {
1339
+ return this.setTarget({ antennas: [degToRad(rightDeg), degToRad(leftDeg)] });
1340
+ }
1341
+
1342
+ /**
1343
+ * Set body yaw from degrees.
1344
+ * Convenience wrapper over ``setTarget``.
1345
+ * @param {number} yawDeg
1346
+ * @returns {boolean}
1347
+ */
1348
+ setBodyYawDeg(yawDeg) {
1349
+ return this.setTarget({ body_yaw: degToRad(yawDeg) });
1350
+ }
1351
+
1352
+ /**
1353
+ * Play a sound file on the robot.
1354
+ * @param {string} file — filename available on the robot (e.g. "wake_up.wav")
1355
+ * @returns {boolean}
1356
+ */
1357
+ playSound(file) {
1358
+ return this._sendCommand({ type: "play_sound", file });
1359
+ }
1360
+
1361
+ /**
1362
+ * Set the motor control mode.
1363
+ *
1364
+ * @param {"enabled"|"disabled"|"gravity_compensation"} mode
1365
+ * - "enabled" torque on, position-controlled.
1366
+ * - "disabled" torque off; the robot is backdrivable
1367
+ * and will not hold any pose.
1368
+ * - "gravity_compensation" torque on in current-control mode;
1369
+ * motors actively cancel gravity so the
1370
+ * robot is easy to move by hand.
1371
+ * @returns {boolean} false if the data channel is not open.
1372
+ */
1373
+ setMotorMode(mode) {
1374
+ return this._sendCommand({ type: "set_motor_mode", mode });
1375
+ }
1376
+
1377
+ /**
1378
+ * Toggle torque on/off, optionally per-motor.
1379
+ *
1380
+ * @param {boolean} on
1381
+ * @param {string[]} [ids] motor names (e.g. ["left_antenna"]). When
1382
+ * omitted, applies globally — equivalent to setMotorMode("enabled"
1383
+ * | "disabled").
1384
+ * @returns {boolean} false if the data channel is not open.
1385
+ */
1386
+ setMotorTorque(on, ids = null) {
1387
+ return this._sendCommand({ type: "set_torque", on, ids });
1388
+ }
1389
+
1390
+ /**
1391
+ * Play the wake-up animation (full head/antennas trajectory on the
1392
+ * robot, ~1-3 s depending on the starting head pose) and resolve
1393
+ * when the daemon reports the trajectory player has actually
1394
+ * finished.
1395
+ *
1396
+ * This helper sends a ``set_motor_mode: "enabled"`` command *before*
1397
+ * the ``wake_up`` command so the animation actually moves the motors.
1398
+ * The robot's ``wake_up`` handler does not touch motor mode itself;
1399
+ * if torque is off when the trajectory runs, the commanded positions
1400
+ * are silently ignored and the robot stays limp. Both commands
1401
+ * travel over the same data channel so ordering at the backend is
1402
+ * preserved.
1403
+ *
1404
+ * The returned promise resolves on the daemon's
1405
+ * ``{command: "wake_up", completed: true}`` response (sent after
1406
+ * the trajectory player is fully done, not just when the command
1407
+ * is enqueued). Lets a UI overlay (e.g. the host's "Wake-up" step)
1408
+ * stay up for exactly the right duration, and lets callers chain
1409
+ * setup that depends on the head being in the awake pose without
1410
+ * racing the trajectory.
1411
+ *
1412
+ * Semantics match the REST endpoint ``POST /api/move/play/wake_up``
1413
+ * plus the LAN convention of enabling motors before playing motion
1414
+ * trajectories.
1415
+ *
1416
+ * @param {object} [options]
1417
+ * @param {number} [options.timeoutMs=8000] hard upper bound; the
1418
+ * promise rejects with a TimeoutError-shaped Error if the daemon
1419
+ * stops responding (e.g. data channel went down mid-animation
1420
+ * without firing close events).
1421
+ * @returns {Promise<void>}
1422
+ */
1423
+ wakeUp({ timeoutMs = 8000 } = {}) {
1424
+ this._sendCommand({ type: "set_motor_mode", mode: "enabled" });
1425
+ return this._sendCommandAwaitCompletion("wake_up", timeoutMs);
1426
+ }
1427
+
1428
+ /**
1429
+ * Play the goto-sleep animation and resolve when the daemon reports
1430
+ * the trajectory player has finished. See ``wakeUp`` for the
1431
+ * completion-signal rationale.
1432
+ *
1433
+ * Does NOT touch motor mode: the daemon's ``goto_sleep`` handler
1434
+ * manages the transition out of torque on its own (motors must stay
1435
+ * powered during the trajectory to move into the sleep pose, then
1436
+ * are typically disabled by the daemon once the pose is reached).
1437
+ *
1438
+ * The awaitable form lets callers chain ``setMotorMode('disabled')``
1439
+ * AFTER the trajectory lands instead of racing it, which previously
1440
+ * caused the head to drop mid-animation when consumers tore down
1441
+ * too eagerly.
1442
+ *
1443
+ * Semantics match ``POST /api/move/play/goto_sleep`` and the
1444
+ * ``"goto_sleep"`` WebRTC command.
1445
+ *
1446
+ * @param {object} [options]
1447
+ * @param {number} [options.timeoutMs=8000]
1448
+ * @returns {Promise<void>}
1449
+ */
1450
+ gotoSleep({ timeoutMs = 8000 } = {}) {
1451
+ return this._sendCommandAwaitCompletion("goto_sleep", timeoutMs);
1452
+ }
1453
+
1454
+ /**
1455
+ * Internal: send a motion command and resolve when the daemon's
1456
+ * matching ``{command, completed: true}`` response lands.
1457
+ *
1458
+ * Pushes one entry onto ``_pendingMotionCompletions[command]``; the
1459
+ * data-channel reader (``_handleRobotMessage``) shifts the oldest
1460
+ * entry off the queue when a response arrives, which preserves the
1461
+ * FIFO matching that the daemon's serialised dispatcher relies on.
1462
+ *
1463
+ * Rejects immediately if the data channel is not open; the underlying
1464
+ * ``_sendCommand`` returns false in that case and we never enqueue an
1465
+ * awaiter that the daemon could never reach.
1466
+ *
1467
+ * @param {"wake_up"|"goto_sleep"} command
1468
+ * @param {number} timeoutMs
1469
+ * @returns {Promise<void>}
1470
+ */
1471
+ _sendCommandAwaitCompletion(command, timeoutMs) {
1472
+ if (!this._sendCommand({ type: command })) {
1473
+ return Promise.reject(new Error(`${command}: data channel not open`));
1474
+ }
1475
+ return new Promise((resolve, reject) => {
1476
+ const entry = {
1477
+ resolve,
1478
+ reject,
1479
+ timer: setTimeout(() => {
1480
+ const queue = this._pendingMotionCompletions[command];
1481
+ const idx = queue.indexOf(entry);
1482
+ if (idx !== -1) queue.splice(idx, 1);
1483
+ reject(new Error(`${command} timed out after ${timeoutMs}ms`));
1484
+ }, timeoutMs),
1485
+ };
1486
+ this._pendingMotionCompletions[command].push(entry);
1487
+ });
1488
+ }
1489
+
1490
+ /**
1491
+ * Internal: drain every pending motion-completion resolver with the
1492
+ * given error. Called by ``stopSession()`` and ``disconnect()`` so a
1493
+ * teardown that interrupts an in-flight ``gotoSleep`` does not leave
1494
+ * the caller awaiting forever.
1495
+ */
1496
+ _rejectPendingMotionCompletions(error) {
1497
+ for (const command of Object.keys(this._pendingMotionCompletions)) {
1498
+ const queue = this._pendingMotionCompletions[command];
1499
+ while (queue.length) {
1500
+ const entry = queue.shift();
1501
+ clearTimeout(entry.timer);
1502
+ entry.reject(error);
1503
+ }
1504
+ }
1505
+ }
1506
+
1507
+ /**
1508
+ * Whether the robot's motors are currently powered (the "awake" state).
1509
+ *
1510
+ * Reads ``motor_mode`` from the last state event. Both ``"enabled"``
1511
+ * and ``"gravity_compensation"`` count as awake: in gravity-comp the
1512
+ * motors are actively holding the arm against gravity, so the robot
1513
+ * is *not* limp and playing wake_up on top would fight the user.
1514
+ * Only ``"disabled"`` (true sleep) is considered not-awake.
1515
+ *
1516
+ * Returns ``false`` before the first state event arrives (typical
1517
+ * right after ``startSession()``). Use ``ensureAwake()`` if you want
1518
+ * to wait for the first state before deciding.
1519
+ *
1520
+ * @returns {boolean}
1521
+ */
1522
+ isAwake() {
1523
+ const mode = this._robotState?.motor_mode;
1524
+ return mode === "enabled" || mode === "gravity_compensation";
1525
+ }
1526
+
1527
+ /**
1528
+ * Wake the robot up if it is currently asleep, otherwise no-op.
1529
+ *
1530
+ * Intended as the first line of any app after ``startSession()``
1531
+ * resolves — robots are often left in the sleep pose (torque off,
1532
+ * head resting on the base) and commanded positions are silently
1533
+ * ignored in that state.
1534
+ *
1535
+ * If no state event has arrived yet, waits up to ``timeoutMs`` for
1536
+ * one before deciding. If still no state, falls back to sending
1537
+ * ``wakeUp()`` (safe: the daemon's wake_up handler is idempotent
1538
+ * at the motion level — it moves to the awake pose from wherever
1539
+ * the head currently is).
1540
+ *
1541
+ * @param {number} [timeoutMs=1000] how long to wait for the first
1542
+ * state event before falling through to wakeUp().
1543
+ * @returns {Promise<boolean>} true if the robot is awake afterwards.
1544
+ */
1545
+ async ensureAwake(timeoutMs = 1000) {
1546
+ if (this._robotState?.motor_mode === undefined) {
1547
+ await new Promise((resolve) => {
1548
+ const done = () => {
1549
+ this.removeEventListener('state', done);
1550
+ clearTimeout(timer);
1551
+ resolve();
1552
+ };
1553
+ const timer = setTimeout(done, timeoutMs);
1554
+ this.addEventListener('state', done);
1555
+ this.requestState();
1556
+ });
1557
+ }
1558
+ if (this.isAwake()) return true;
1559
+ // wakeUp() now returns a Promise. Fire-and-forget here — we
1560
+ // intentionally do not await the trajectory completion, the
1561
+ // caller of ensureAwake() decides whether to block on the
1562
+ // animation. Catch the rejection so a teardown that interrupts
1563
+ // the wake doesn't surface an unhandledrejection event from
1564
+ // this internal helper.
1565
+ this.wakeUp().catch(() => { /* swallow: caller may have torn down */ });
1566
+ return true;
1567
+ }
1568
+
1569
+ /**
1570
+ * Request the daemon version.
1571
+ * Resolves with the version string (or null if unavailable).
1572
+ * @returns {Promise<string|null>}
1573
+ */
1574
+ getVersion() {
1575
+ return new Promise((resolve, reject) => {
1576
+ if (!this._dc || this._dc.readyState !== 'open') {
1577
+ reject(new Error('Data channel not open'));
1578
+ return;
1579
+ }
1580
+ if (this._versionResolve) {
1581
+ this._versionResolve(null);
1582
+ }
1583
+ this._versionResolve = resolve;
1584
+ this._sendCommand({ type: "get_version" });
1585
+ });
1586
+ }
1587
+
1588
+ /**
1589
+ * Request the robot's unique hardware ID — the Pollen audio device's
1590
+ * USB serial. Same value across Lite and Wireless variants, stable
1591
+ * across reboots and OS reinstalls. Useful for fleet management,
1592
+ * per-robot calibration cache keys, or identifying which physical
1593
+ * robot a session is bound to.
1594
+ * Resolves with the hardware ID string (or null if no robot is
1595
+ * attached, e.g. the daemon is running on a developer machine).
1596
+ * @returns {Promise<string|null>}
1597
+ */
1598
+ getHardwareId() {
1599
+ return new Promise((resolve, reject) => {
1600
+ if (!this._dc || this._dc.readyState !== 'open') {
1601
+ reject(new Error('Data channel not open'));
1602
+ return;
1603
+ }
1604
+ if (this._hardwareIdResolve) {
1605
+ this._hardwareIdResolve(null);
1606
+ }
1607
+ this._hardwareIdResolve = resolve;
1608
+ this._sendCommand({ type: "get_hardware_id" });
1609
+ });
1610
+ }
1611
+
1612
+ /**
1613
+ * Query the current speaker volume (0-100).
1614
+ * Resolves with null if volume control is unavailable (platform unsupported
1615
+ * or audio stack down).
1616
+ * @returns {Promise<number|null>}
1617
+ */
1618
+ getVolume() {
1619
+ return this._volumeRoundtrip({ type: "get_volume" }, "_volumeResolve");
1620
+ }
1621
+
1622
+ /**
1623
+ * Set the speaker volume (0-100). Persists for the next connection
1624
+ * (same semantics as the REST /api/volume/set endpoint).
1625
+ * Resolves with the applied volume, or null on failure.
1626
+ * @param {number} volume 0-100
1627
+ * @returns {Promise<number|null>}
1628
+ */
1629
+ setVolume(volume) {
1630
+ return this._volumeRoundtrip(
1631
+ { type: "set_volume", volume: clampVolume(volume) },
1632
+ "_volumeResolve",
1633
+ );
1634
+ }
1635
+
1636
+ /**
1637
+ * Query the current microphone input volume (0-100).
1638
+ * @returns {Promise<number|null>}
1639
+ */
1640
+ getMicrophoneVolume() {
1641
+ return this._volumeRoundtrip(
1642
+ { type: "get_microphone_volume" },
1643
+ "_micVolumeResolve",
1644
+ );
1645
+ }
1646
+
1647
+ /**
1648
+ * Set the microphone input volume (0-100). Persists across sessions.
1649
+ * @param {number} volume 0-100
1650
+ * @returns {Promise<number|null>}
1651
+ */
1652
+ setMicrophoneVolume(volume) {
1653
+ return this._volumeRoundtrip(
1654
+ { type: "set_microphone_volume", volume: clampVolume(volume) },
1655
+ "_micVolumeResolve",
1656
+ );
1657
+ }
1658
+
1659
+ /**
1660
+ * Internal: send a volume command and await the single-slot response.
1661
+ * The slot name selects which resolver (speaker vs mic) owns the
1662
+ * pending request so the two can be in-flight concurrently without
1663
+ * collision.
1664
+ */
1665
+ _volumeRoundtrip(command, slot) {
1666
+ return new Promise((resolve, reject) => {
1667
+ if (!this._dc || this._dc.readyState !== 'open') {
1668
+ reject(new Error('Data channel not open'));
1669
+ return;
1670
+ }
1671
+ // If a previous request on the same slot is still pending,
1672
+ // resolve it to null so its caller doesn't hang forever.
1673
+ if (this[slot]) this[slot](null);
1674
+ this[slot] = resolve;
1675
+ this._sendCommand(command);
1676
+ });
1677
+ }
1678
+
1679
+ /**
1680
+ * Send an arbitrary JSON command over the data channel.
1681
+ * @param {object} data @returns {boolean}
1682
+ */
1683
+ sendRaw(data) {
1684
+ return this._sendCommand(data);
1685
+ }
1686
+
1687
+ /**
1688
+ * Subscribe to the daemon's `journalctl -u reachy-mini-daemon`
1689
+ * stream over the WebRTC data channel.
1690
+ *
1691
+ * One daemon-side subprocess is shared across all local subscribers:
1692
+ * the first call sends `subscribe_logs`, removing the last subscriber
1693
+ * sends `unsubscribe_logs`. Calling the returned `unsubscribe()`
1694
+ * twice is a no-op.
1695
+ *
1696
+ * @param {{
1697
+ * onLine: (entry: { timestamp: string, line: string }) => void,
1698
+ * onError?: (error: string) => void,
1699
+ * }} options
1700
+ * @returns {() => void} unsubscribe
1701
+ */
1702
+ subscribeLogs({ onLine, onError } = {}) {
1703
+ if (typeof onLine !== 'function') {
1704
+ throw new TypeError('subscribeLogs: onLine callback is required');
1705
+ }
1706
+ const sub = { onLine, onError };
1707
+ const wasEmpty = this._logSubscribers.size === 0;
1708
+ this._logSubscribers.add(sub);
1709
+ if (wasEmpty) this._sendCommand({ type: 'subscribe_logs' });
1710
+
1711
+ let detached = false;
1712
+ return () => {
1713
+ if (detached) return;
1714
+ detached = true;
1715
+ this._logSubscribers.delete(sub);
1716
+ if (this._logSubscribers.size === 0) {
1717
+ this._sendCommand({ type: 'unsubscribe_logs' });
1718
+ }
1719
+ };
1720
+ }
1721
+
1722
+ /**
1723
+ * Request a state snapshot. The response arrives as a "state" event.
1724
+ * Called automatically every 500 ms while streaming.
1725
+ *
1726
+ * Safe to call at a higher rate if you need faster telemetry: e.g.
1727
+ * ``setInterval(() => robot.requestState(), 20)`` for ~50 Hz, or drive
1728
+ * it from a ``requestAnimationFrame`` loop for display-rate updates.
1729
+ * On LAN the daemon can sustain ~90-100 Hz round-trips over the
1730
+ * datachannel; over the internet expect the WebRTC path's RTT to
1731
+ * dominate. The built-in 500 ms poll keeps running in parallel — it
1732
+ * is harmless, as state responses are idempotent.
1733
+ *
1734
+ * @returns {boolean}
1735
+ */
1736
+ requestState() {
1737
+ return this._sendCommand({ type: "get_state" });
1738
+ }
1739
+
1740
+ // ─── Audio ───────────────────────────────────────────────────────────
1741
+
1742
+ /**
1743
+ * Mute/unmute the robot's audio playback (speaker) locally.
1744
+ * Audio is muted by default — browsers require a user gesture to unmute.
1745
+ * @param {boolean} muted
1746
+ */
1747
+ setAudioMuted(muted) {
1748
+ this._audioMuted = muted;
1749
+ if (this._videoElement) this._videoElement.muted = muted;
1750
+ }
1751
+
1752
+ /**
1753
+ * Mute/unmute your microphone. Only works if micSupported is true.
1754
+ * Mic is muted by default even after acquisition.
1755
+ * @param {boolean} muted
1756
+ */
1757
+ setMicMuted(muted) {
1758
+ this._micMuted = muted;
1759
+ if (this._micStream) {
1760
+ this._micStream.getAudioTracks().forEach(t => { t.enabled = !muted; });
1761
+ }
1762
+ }
1763
+
1764
+ // ─── Video helper ────────────────────────────────────────────────────
1765
+
1766
+ /**
1767
+ * Bind a `<video>` element to this robot's stream.
1768
+ * Call before startSession(). Sets srcObject when the video track arrives,
1769
+ * applies audio mute state, and runs a latency monitor that snaps to the
1770
+ * live edge if the buffer grows > 0.5 s.
1771
+ *
1772
+ * @param {HTMLVideoElement} videoElement
1773
+ * @returns {() => void} cleanup function — call to detach video and stop monitoring
1774
+ */
1775
+ attachVideo(videoElement) {
1776
+ this._videoElement = videoElement;
1777
+ videoElement.muted = this._audioMuted;
1778
+
1779
+ const onVideoTrack = (e) => {
1780
+ videoElement.srcObject = e.detail.stream;
1781
+ videoElement.playsInline = true;
1782
+ if ('requestVideoFrameCallback' in videoElement) {
1783
+ this._startLatencyMonitor(videoElement);
1784
+ }
1785
+ };
1786
+
1787
+ const onSessionStopped = () => { videoElement.srcObject = null; };
1788
+
1789
+ this.addEventListener('videoTrack', onVideoTrack);
1790
+ this.addEventListener('sessionStopped', onSessionStopped);
1791
+
1792
+ return () => {
1793
+ this.removeEventListener('videoTrack', onVideoTrack);
1794
+ this.removeEventListener('sessionStopped', onSessionStopped);
1795
+ if (this._latencyMonitorId) { clearInterval(this._latencyMonitorId); this._latencyMonitorId = null; }
1796
+ videoElement.srcObject = null;
1797
+ this._videoElement = null;
1798
+ };
1799
+ }
1800
+
1801
+ // ─── Private ─────────────────────────────────────────────────────────
1802
+
1803
+ _emit(name, detail) {
1804
+ this.dispatchEvent(new CustomEvent(name, { detail }));
1805
+ }
1806
+
1807
+ async _sendToServer(message) {
1808
+ // Mirrors connect()'s guard — a missing token between connect and
1809
+ // send (e.g. logout mid-session) would otherwise produce
1810
+ // "Authorization: Bearer undefined", which central correctly 401s
1811
+ // but silently returns null to the caller, hiding the real cause.
1812
+ if (!this._token) throw new Error('No token — authenticate() first');
1813
+ try {
1814
+ // Token in Authorization header, not URL — same reasoning as
1815
+ // connect()'s SSE fetch: never put secrets in URLs.
1816
+ const res = await fetch(`${this._signalingUrl}/send`, {
1817
+ method: 'POST',
1818
+ headers: {
1819
+ 'Content-Type': 'application/json',
1820
+ 'Authorization': `Bearer ${this._token}`,
1821
+ },
1822
+ body: JSON.stringify(message),
1823
+ });
1824
+ if (!res.ok) {
1825
+ // Surface 4xx/5xx with the rejected message type. The
1826
+ // browser already logs "Failed to load resource: <status>"
1827
+ // but never says which call produced it, which makes
1828
+ // tardy `peer`/`endSession`/`setPeerStatus` races
1829
+ // (typical after a session has been torn down) hard to
1830
+ // diagnose. Returning null preserves the historical
1831
+ // contract for callers that only care about the success
1832
+ // path.
1833
+ let body = '';
1834
+ try { body = await res.text(); } catch { /* ignore */ }
1835
+ console.warn(
1836
+ `[reachy-mini] /send rejected (${res.status}) for type=${message?.type}; body=${body || '<empty>'}`,
1837
+ );
1838
+ return null;
1839
+ }
1840
+ return await res.json();
1841
+ } catch (e) {
1842
+ console.error('Send error:', e);
1843
+ return null;
1844
+ }
1845
+ }
1846
+
1847
+ _sendCommand(cmd) {
1848
+ if (!this._dc || this._dc.readyState !== 'open') return false;
1849
+ this._dc.send(JSON.stringify(cmd));
1850
+ return true;
1851
+ }
1852
+
1853
+ /** Resolves the startSession() promise once both ICE and datachannel are ready. */
1854
+ _checkSessionReady() {
1855
+ if (this._iceConnected && this._dcOpen && this._sessionResolve) {
1856
+ this._state = 'streaming';
1857
+ this.requestState();
1858
+ this._stateRefreshInterval = setInterval(() => this.requestState(), 500);
1859
+ this._emit('streaming', { sessionId: this._sessionId, robotId: this._selectedRobotId });
1860
+ this._sessionResolve();
1861
+ this._sessionResolve = null;
1862
+ this._sessionReject = null;
1863
+ }
1864
+ }
1865
+
1866
+ async _handleSignalingMessage(msg) {
1867
+ switch (msg.type) {
1868
+ case 'welcome':
1869
+ break; // handled in connect()
1870
+ case 'list':
1871
+ this._robots = msg.producers || [];
1872
+ this._emit('robotsChanged', { robots: this._robots });
1873
+ this._maybeAutoStart();
1874
+ break;
1875
+ case 'peerStatusChanged': {
1876
+ const list = await this._sendToServer({ type: 'list' });
1877
+ if (list?.producers) {
1878
+ this._robots = list.producers;
1879
+ this._emit('robotsChanged', { robots: this._robots });
1880
+ this._maybeAutoStart();
1881
+ }
1882
+ break;
1883
+ }
1884
+ case 'sessionStarted':
1885
+ this._sessionId = msg.sessionId;
1886
+ break;
1887
+ case 'sessionRejected':
1888
+ // Defensive: the current central server returns sessionRejected
1889
+ // as the direct POST response (handled at the startSession() call
1890
+ // site). This branch is a safety net in case the server ever
1891
+ // pushes the rejection over SSE instead.
1892
+ this._failSessionRejected(msg);
1893
+ break;
1894
+ case 'endSession':
1895
+ this._handleEndSession(msg);
1896
+ break;
1897
+ case 'peer':
1898
+ this._handlePeerMessage(msg);
1899
+ break;
1900
+ }
1901
+ }
1902
+
1903
+ /**
1904
+ * Internal: handle an endSession pushed from central.
1905
+ *
1906
+ * Two user-visible scenarios converge here:
1907
+ *
1908
+ * 1. Pending startSession — central accepted our request but the
1909
+ * robot-side relay then refused (e.g. a local Python app holds
1910
+ * the daemon's robot lock). Without this handler, the client's
1911
+ * startSession() promise would hang forever because ICE/datachannel
1912
+ * never come up.
1913
+ * 2. Streaming was evicted — a local Python app started on the robot
1914
+ * while we were connected, so the relay tore down our session.
1915
+ *
1916
+ * The ``reason`` is forwarded verbatim from the relay through central:
1917
+ * - "robot_busy_local_app": daemon lock held by a local Python app
1918
+ * (refused before any media negotiation).
1919
+ * - "local_app_started": a local Python app started mid-session and
1920
+ * evicted us.
1921
+ * - "robot_busy_local": relay's safety-net for stale/concurrent
1922
+ * sessions (should be rare).
1923
+ */
1924
+ _handleEndSession(msg) {
1925
+ const reason = msg.reason;
1926
+ const friendly = reason === 'robot_busy_local_app'
1927
+ ? 'Robot is busy: a local Python app is running'
1928
+ : reason === 'local_app_started'
1929
+ ? 'Disconnected: a local Python app started on the robot'
1930
+ : reason === 'robot_busy_local'
1931
+ ? 'Robot is busy: another session is already active'
1932
+ : null;
1933
+
1934
+ // Case 1: a startSession() is still pending — reject its promise
1935
+ // with the same error shape as sessionRejected so app code can
1936
+ // treat both paths identically.
1937
+ if (this._sessionReject) {
1938
+ const err = new Error(
1939
+ friendly || `Session ended before it could start: ${reason || 'unknown reason'}`
1940
+ );
1941
+ err.reason = reason;
1942
+ this._emit('sessionRejected', { reason, activeApp: null });
1943
+ // Release resources allocated optimistically by startSession().
1944
+ if (this._pc) { this._pc.close(); this._pc = null; }
1945
+ if (this._micStream) { this._micStream.getTracks().forEach(t => t.stop()); this._micStream = null; }
1946
+ this._iceConnected = false;
1947
+ this._dcOpen = false;
1948
+ this._micMuted = true;
1949
+ this._micSupported = false;
1950
+ const reject = this._sessionReject;
1951
+ this._sessionResolve = null;
1952
+ this._sessionReject = null;
1953
+ reject(err);
1954
+ return;
1955
+ }
1956
+
1957
+ // Case 2: we were streaming and got kicked. Leave cleanup of _pc,
1958
+ // _dc, mic and timers to stopSession() so the event listener path
1959
+ // matches the user-initiated stop path exactly.
1960
+ if (this._state === 'streaming') {
1961
+ this._emit('sessionStopped', {
1962
+ reason: reason || 'remote_end',
1963
+ message: friendly,
1964
+ });
1965
+ // Fire-and-forget: we just react to what the server already
1966
+ // decided. stopSession() sends its own endSession back but
1967
+ // central has already dropped the session, so the echo is
1968
+ // harmless.
1969
+ this.stopSession().catch(() => { });
1970
+ }
1971
+ }
1972
+
1973
+ async _handlePeerMessage(msg) {
1974
+ if (!this._pc) return;
1975
+ try {
1976
+ if (msg.sdp) {
1977
+ const sdp = msg.sdp;
1978
+ if (sdp.type === 'offer') {
1979
+ const supportsMic = sdpHasAudioSendRecv(sdp.sdp);
1980
+ this._micSupported = supportsMic;
1981
+ this._emit('micSupported', { supported: supportsMic });
1982
+
1983
+ // Mic track must be added BEFORE setRemoteDescription so the
1984
+ // generated answer naturally includes sendrecv for audio.
1985
+ if (supportsMic && this._micStream) {
1986
+ for (const track of this._micStream.getAudioTracks()) {
1987
+ this._pc.addTrack(track, this._micStream);
1988
+ }
1989
+ }
1990
+
1991
+ await this._pc.setRemoteDescription(new RTCSessionDescription(sdp));
1992
+ const answer = await this._pc.createAnswer();
1993
+ await this._pc.setLocalDescription(answer);
1994
+ await this._sendToServer({
1995
+ type: 'peer',
1996
+ sessionId: this._sessionId,
1997
+ sdp: { type: 'answer', sdp: answer.sdp },
1998
+ });
1999
+ } else {
2000
+ await this._pc.setRemoteDescription(new RTCSessionDescription(sdp));
2001
+ }
2002
+ // Replay any ICE candidates that arrived before the
2003
+ // SDP exchange completed (see the buffering branch
2004
+ // below for context).
2005
+ const pending = this._pendingRemoteIce;
2006
+ if (pending && pending.length) {
2007
+ this._pendingRemoteIce = [];
2008
+ for (const ice of pending) {
2009
+ try {
2010
+ await this._pc.addIceCandidate(new RTCIceCandidate(ice));
2011
+ } catch (err) {
2012
+ console.warn('[reachy-mini] buffered ICE candidate rejected:', err);
2013
+ }
2014
+ }
2015
+ }
2016
+ }
2017
+ if (msg.ice) {
2018
+ // Safari (and the iOS WKWebView Tauri ships on) rejects
2019
+ // empty candidate strings with `OperationError: Expect
2020
+ // line: candidate:<candidate-str>`. The signaling
2021
+ // server uses an empty string as the end-of-candidates
2022
+ // marker (legal per the WebRTC spec but optional).
2023
+ // Chrome / Firefox swallow it silently; we mirror that
2024
+ // here so the iOS WebView stops surfacing the noise as
2025
+ // a robot-side WebRTC error event.
2026
+ if (!msg.ice.candidate) return;
2027
+ if (this._pc.remoteDescription) {
2028
+ await this._pc.addIceCandidate(new RTCIceCandidate(msg.ice));
2029
+ } else {
2030
+ // The signaling transport (SSE through central) is
2031
+ // not strictly ordered across the offer / ICE
2032
+ // streams when the SDK runs inside a cross-origin
2033
+ // iframe or in Safari / iOS WKWebView: the first
2034
+ // ICE candidates can land before the offer SDP
2035
+ // does. Calling addIceCandidate before
2036
+ // setRemoteDescription throws
2037
+ // `InvalidStateError: The remote description was
2038
+ // null` and the candidate is silently lost,
2039
+ // sometimes wedging ICE altogether. Buffer here
2040
+ // and replay above as soon as the offer has been
2041
+ // applied.
2042
+ if (!this._pendingRemoteIce) this._pendingRemoteIce = [];
2043
+ this._pendingRemoteIce.push(msg.ice);
2044
+ }
2045
+ }
2046
+ } catch (e) {
2047
+ console.error('WebRTC error:', e);
2048
+ this._emit('error', { source: 'webrtc', error: e });
2049
+ }
2050
+ }
2051
+
2052
+ /** Parse robot messages and dispatch. */
2053
+ _handleRobotMessage(data) {
2054
+ if ('version' in data && this._versionResolve) {
2055
+ this._versionResolve(data.version);
2056
+ this._versionResolve = null;
2057
+ return;
2058
+ }
2059
+ if ('hardware_id' in data && this._hardwareIdResolve) {
2060
+ this._hardwareIdResolve(data.hardware_id);
2061
+ this._hardwareIdResolve = null;
2062
+ return;
2063
+ }
2064
+ // Volume responses. Backend tags each response with `command` so we
2065
+ // know which pending resolver (speaker vs mic) to fulfil. If a
2066
+ // response arrives with no matching pending request (e.g. stale
2067
+ // after reconnect), just ignore it — the data channel protocol
2068
+ // has no multiplexing, so unmatched replies are expected.
2069
+ if (data.command === 'get_volume' || data.command === 'set_volume') {
2070
+ if (this._volumeResolve) {
2071
+ this._volumeResolve(data.status === 'error' ? null : data.volume);
2072
+ this._volumeResolve = null;
2073
+ }
2074
+ return;
2075
+ }
2076
+ if (data.command === 'get_microphone_volume' || data.command === 'set_microphone_volume') {
2077
+ if (this._micVolumeResolve) {
2078
+ this._micVolumeResolve(data.status === 'error' ? null : data.volume);
2079
+ this._micVolumeResolve = null;
2080
+ }
2081
+ return;
2082
+ }
2083
+ // Motion completion responses. The daemon emits
2084
+ // `{status: "ok", command: "wake_up"|"goto_sleep", completed: true}`
2085
+ // after the trajectory player is fully done, or `{error, command}`
2086
+ // on failure. Route them to the FIFO queue of pending awaiters
2087
+ // populated by `_sendCommandAwaitCompletion`; the N-th response
2088
+ // matches the N-th request thanks to the daemon's serialised
2089
+ // dispatcher.
2090
+ if (
2091
+ (data.command === 'wake_up' || data.command === 'goto_sleep') &&
2092
+ this._pendingMotionCompletions &&
2093
+ this._pendingMotionCompletions[data.command]
2094
+ ) {
2095
+ const queue = this._pendingMotionCompletions[data.command];
2096
+ if (data.completed === true && queue.length > 0) {
2097
+ const entry = queue.shift();
2098
+ clearTimeout(entry.timer);
2099
+ entry.resolve();
2100
+ return;
2101
+ }
2102
+ if (data.error && queue.length > 0) {
2103
+ const entry = queue.shift();
2104
+ clearTimeout(entry.timer);
2105
+ entry.reject(new Error(`${data.command}: ${data.error}`));
2106
+ return;
2107
+ }
2108
+ }
2109
+ if (data.type === 'log_line') {
2110
+ for (const sub of this._logSubscribers) {
2111
+ try {
2112
+ sub.onLine({ timestamp: data.timestamp, line: data.line });
2113
+ } catch (e) {
2114
+ console.error('subscribeLogs onLine threw:', e);
2115
+ }
2116
+ }
2117
+ return;
2118
+ }
2119
+ if (data.type === 'log_stream_error') {
2120
+ for (const sub of this._logSubscribers) {
2121
+ if (typeof sub.onError === 'function') {
2122
+ try { sub.onError(data.error); }
2123
+ catch (e) { console.error('subscribeLogs onError threw:', e); }
2124
+ }
2125
+ }
2126
+ return;
2127
+ }
2128
+ if (data.state) {
2129
+ const s = data.state;
2130
+ // Wire-shape pass-through. The daemon ships the head pose as a
2131
+ // nested 4×4 (numpy tolist()); we flatten to 16 numbers so
2132
+ // consumers can hand it straight to WebGL / Three.js / trajectory
2133
+ // logs. Everything else is forwarded as-is.
2134
+ if (s.head_pose) this._robotState.head = s.head_pose.flat();
2135
+ if (s.antennas) this._robotState.antennas = [s.antennas[0], s.antennas[1]];
2136
+ if (typeof s.body_yaw === 'number') this._robotState.body_yaw = s.body_yaw;
2137
+ if (s.motor_mode) this._robotState.motor_mode = s.motor_mode;
2138
+ if (typeof s.is_move_running === 'boolean') this._robotState.is_move_running = s.is_move_running;
2139
+ this._emit('state', { ...this._robotState });
2140
+ }
2141
+ if (data.error) {
2142
+ this._emit('error', { source: 'robot', error: data.error });
2143
+ }
2144
+ }
2145
+
2146
+ /** Snap video playback to live edge if buffered lag exceeds 0.5 s. */
2147
+ _startLatencyMonitor(video) {
2148
+ if (this._latencyMonitorId) clearInterval(this._latencyMonitorId);
2149
+ this._latencyMonitorId = setInterval(() => {
2150
+ if (!video.srcObject || video.paused) return;
2151
+ const buf = video.buffered;
2152
+ if (buf.length > 0) {
2153
+ const end = buf.end(buf.length - 1);
2154
+ const lag = end - video.currentTime;
2155
+ if (lag > 0.5) {
2156
+ console.log(`Latency correction: was ${lag.toFixed(2)}s behind`);
2157
+ video.currentTime = end - 0.1;
2158
+ }
2159
+ }
2160
+ }, 2000);
2161
+ }
2162
+ }
2163
+
2164
+ export default ReachyMini;