@pollen-robotics/reachy-mini-sdk 1.7.3-main.5cd69ad

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