@pollen-robotics/reachy-mini-sdk 1.10.0-dev.0.main.05ce680 → 1.10.0-dev.0.main.4dc28bb

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 (125) hide show
  1. package/APP_CREATION_GUIDE.md +98 -0
  2. package/dist/lib/central-listener.d.ts +71 -0
  3. package/dist/lib/central-listener.d.ts.map +1 -0
  4. package/dist/lib/central-listener.js +277 -0
  5. package/dist/lib/central-listener.js.map +1 -0
  6. package/dist/lib/central-listener.test.d.ts +2 -0
  7. package/dist/lib/central-listener.test.d.ts.map +1 -0
  8. package/dist/lib/central-listener.test.js +378 -0
  9. package/dist/lib/central-listener.test.js.map +1 -0
  10. package/dist/lib/dc-watchdog.test.d.ts +2 -0
  11. package/dist/lib/dc-watchdog.test.d.ts.map +1 -0
  12. package/dist/lib/dc-watchdog.test.js +154 -0
  13. package/dist/lib/dc-watchdog.test.js.map +1 -0
  14. package/dist/lib/ensure-awake.test.d.ts +2 -0
  15. package/dist/lib/ensure-awake.test.d.ts.map +1 -0
  16. package/dist/lib/ensure-awake.test.js +117 -0
  17. package/dist/lib/ensure-awake.test.js.map +1 -0
  18. package/dist/lib/ice-grace.test.d.ts +2 -0
  19. package/dist/lib/ice-grace.test.d.ts.map +1 -0
  20. package/dist/lib/ice-grace.test.js +211 -0
  21. package/dist/lib/ice-grace.test.js.map +1 -0
  22. package/dist/lib/imu.test.d.ts +2 -0
  23. package/dist/lib/imu.test.d.ts.map +1 -0
  24. package/dist/lib/imu.test.js +78 -0
  25. package/dist/lib/imu.test.js.map +1 -0
  26. package/dist/lib/logger.d.ts +38 -0
  27. package/dist/lib/logger.d.ts.map +1 -0
  28. package/dist/lib/logger.js +81 -0
  29. package/dist/lib/logger.js.map +1 -0
  30. package/dist/lib/pending-replies.d.ts +127 -0
  31. package/dist/lib/pending-replies.d.ts.map +1 -0
  32. package/dist/lib/pending-replies.js +255 -0
  33. package/dist/lib/pending-replies.js.map +1 -0
  34. package/dist/lib/pending-replies.test.d.ts +2 -0
  35. package/dist/lib/pending-replies.test.d.ts.map +1 -0
  36. package/dist/lib/pending-replies.test.js +217 -0
  37. package/dist/lib/pending-replies.test.js.map +1 -0
  38. package/dist/lib/reachy-mini.d.ts +185 -91
  39. package/dist/lib/reachy-mini.d.ts.map +1 -1
  40. package/dist/lib/reachy-mini.js +604 -648
  41. package/dist/lib/reachy-mini.js.map +1 -1
  42. package/dist/lib/redial.test.d.ts +2 -0
  43. package/dist/lib/redial.test.d.ts.map +1 -0
  44. package/dist/lib/redial.test.js +206 -0
  45. package/dist/lib/redial.test.js.map +1 -0
  46. package/dist/lib/request.test.d.ts +2 -0
  47. package/dist/lib/request.test.d.ts.map +1 -0
  48. package/dist/lib/request.test.js +90 -0
  49. package/dist/lib/request.test.js.map +1 -0
  50. package/dist/lib/session-supervisor.d.ts +201 -0
  51. package/dist/lib/session-supervisor.d.ts.map +1 -0
  52. package/dist/lib/session-supervisor.js +520 -0
  53. package/dist/lib/session-supervisor.js.map +1 -0
  54. package/dist/lib/token-store.d.ts +50 -0
  55. package/dist/lib/token-store.d.ts.map +1 -0
  56. package/dist/lib/token-store.js +120 -0
  57. package/dist/lib/token-store.js.map +1 -0
  58. package/dist/lib/token-store.test.d.ts +2 -0
  59. package/dist/lib/token-store.test.d.ts.map +1 -0
  60. package/dist/lib/token-store.test.js +82 -0
  61. package/dist/lib/token-store.test.js.map +1 -0
  62. package/dist/lib/types.d.ts +280 -6
  63. package/dist/lib/types.d.ts.map +1 -1
  64. package/dist/lib/url-helpers.d.ts.map +1 -1
  65. package/dist/lib/url-helpers.js +3 -1
  66. package/dist/lib/url-helpers.js.map +1 -1
  67. package/dist/lib/version.d.ts +2 -0
  68. package/dist/lib/version.d.ts.map +1 -0
  69. package/dist/lib/version.js +5 -0
  70. package/dist/lib/version.js.map +1 -0
  71. package/dist/reachy-mini-sdk.d.ts +15 -1
  72. package/dist/reachy-mini-sdk.d.ts.map +1 -1
  73. package/dist/reachy-mini-sdk.js +12 -0
  74. package/dist/reachy-mini-sdk.js.map +1 -1
  75. package/host/dist/ReachyHost.d.ts.map +1 -1
  76. package/host/dist/chunks/centralRest-uXNMidlL.js +2315 -0
  77. package/host/dist/chunks/index-Beu4GNzi.js +821 -0
  78. package/host/dist/chunks/{mountHost-DztGlWwu.js → mountHost-BTTF4Uu9.js} +26739 -29305
  79. package/host/dist/components/DaemonUpdateGate.d.ts +65 -0
  80. package/host/dist/components/DaemonUpdateGate.d.ts.map +1 -0
  81. package/host/dist/components/ErrorView.d.ts.map +1 -1
  82. package/host/dist/components/HostErrorBoundary.d.ts +30 -0
  83. package/host/dist/components/HostErrorBoundary.d.ts.map +1 -0
  84. package/host/dist/components/PickerView.d.ts +16 -14
  85. package/host/dist/components/PickerView.d.ts.map +1 -1
  86. package/host/dist/components/PostOAuthSplash.d.ts +14 -9
  87. package/host/dist/components/PostOAuthSplash.d.ts.map +1 -1
  88. package/host/dist/components/ReachyHostShell.d.ts.map +1 -1
  89. package/host/dist/components/SignInView.d.ts.map +1 -1
  90. package/host/dist/components/TopBar.d.ts +5 -6
  91. package/host/dist/components/TopBar.d.ts.map +1 -1
  92. package/host/dist/embed/index.d.ts +68 -5
  93. package/host/dist/embed/index.d.ts.map +1 -1
  94. package/host/dist/entry/auto.js +2 -2
  95. package/host/dist/entry/embed.d.ts +5 -4
  96. package/host/dist/entry/embed.d.ts.map +1 -1
  97. package/host/dist/entry/embed.js +5 -4
  98. package/host/dist/hooks/useHostBridge.d.ts +35 -1
  99. package/host/dist/hooks/useHostBridge.d.ts.map +1 -1
  100. package/host/dist/hooks/useLatestDaemonVersion.d.ts +7 -0
  101. package/host/dist/hooks/useLatestDaemonVersion.d.ts.map +1 -0
  102. package/host/dist/hooks/useOAuth.d.ts +18 -0
  103. package/host/dist/hooks/useOAuth.d.ts.map +1 -1
  104. package/host/dist/hooks/useRobots.d.ts +0 -2
  105. package/host/dist/hooks/useRobots.d.ts.map +1 -1
  106. package/host/dist/index.d.ts +2 -2
  107. package/host/dist/index.d.ts.map +1 -1
  108. package/host/dist/index.js +11 -10
  109. package/host/dist/lib/daemonRelease.d.ts +84 -0
  110. package/host/dist/lib/daemonRelease.d.ts.map +1 -0
  111. package/host/dist/lib/protocol.d.ts +141 -7
  112. package/host/dist/lib/protocol.d.ts.map +1 -1
  113. package/host/dist/lib/rebootWatch.d.ts +51 -0
  114. package/host/dist/lib/rebootWatch.d.ts.map +1 -0
  115. package/host/dist/lib/sdk-types.d.ts +1 -1
  116. package/host/dist/lib/sdk-types.d.ts.map +1 -1
  117. package/host/dist/lib/sdkStaleness.d.ts +78 -0
  118. package/host/dist/lib/sdkStaleness.d.ts.map +1 -0
  119. package/host/dist/lib/settings.d.ts +16 -5
  120. package/host/dist/lib/settings.d.ts.map +1 -1
  121. package/package.json +9 -2
  122. package/host/dist/chunks/index-lMs2sCXG.js +0 -439
  123. package/host/dist/chunks/reachy-mini-DJD3mwi2.js +0 -1595
  124. package/host/dist/lib/centralListener.d.ts +0 -73
  125. package/host/dist/lib/centralListener.d.ts.map +0 -1
@@ -1,1595 +0,0 @@
1
- var O = Object.defineProperty, x = (r, e, t) => e in r ? O(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, C = (r, e, t) => (x(r, typeof e != "symbol" ? e + "" : e, t), t), $ = "https://huggingface.co";
2
- async function k(r, e) {
3
- var t, s;
4
- const i = new F(r.url, r.status, (t = r.headers.get("X-Request-Id")) != null ? t : void 0);
5
- i.message = `Api error with status ${i.statusCode}`;
6
- const o = [`URL: ${i.url}`, i.requestId ? `Request ID: ${i.requestId}` : void 0].filter(Boolean).join(". ");
7
- if ((s = r.headers.get("Content-Type")) != null && s.startsWith("application/json")) {
8
- const n = await r.json();
9
- i.message = n.error || n.message || i.message, i.data = n;
10
- } else
11
- i.data = { message: await r.text() };
12
- throw i.message += `. ${o}`, i;
13
- }
14
- var F = class extends Error {
15
- constructor(r, e, t, s) {
16
- super(s), C(this, "statusCode"), C(this, "url"), C(this, "requestId"), C(this, "data"), this.statusCode = e, this.requestId = t, this.url = r;
17
- }
18
- };
19
- new Promise((r) => {
20
- });
21
- function N(r) {
22
- if (globalThis.Buffer)
23
- return globalThis.Buffer.from(r).toString("base64");
24
- {
25
- const e = [];
26
- return r.forEach((t) => {
27
- e.push(String.fromCharCode(t));
28
- }), globalThis.btoa(e.join(""));
29
- }
30
- }
31
- async function M(r) {
32
- var e, t;
33
- if (typeof window > "u")
34
- throw new Error("oauthHandleRedirect is only available in the browser");
35
- const s = new URLSearchParams(window.location.search), [i, o] = [s.get("error"), s.get("error_description")];
36
- if (i)
37
- throw new Error(`${i}: ${o}`);
38
- const n = s.get("code"), a = localStorage.getItem("huggingface.co:oauth:nonce");
39
- if (!n)
40
- throw new Error("Missing oauth code from query parameters in redirected URL");
41
- if (!a)
42
- throw new Error("Missing oauth nonce from localStorage");
43
- const c = localStorage.getItem("huggingface.co:oauth:code_verifier");
44
- if (!c)
45
- throw new Error("Missing oauth code_verifier from localStorage");
46
- const l = s.get("state");
47
- if (!l)
48
- throw new Error("Missing oauth state from query parameters in redirected URL");
49
- let h;
50
- try {
51
- h = JSON.parse(l);
52
- } catch {
53
- throw new Error("Invalid oauth state in redirected URL, unable to parse JSON: " + l);
54
- }
55
- if (h.nonce !== a)
56
- throw new Error("Invalid oauth state in redirected URL");
57
- const d = $, m = `${new URL(d).origin}/.well-known/openid-configuration`, f = await fetch(m, {
58
- headers: {
59
- Accept: "application/json"
60
- }
61
- });
62
- if (!f.ok)
63
- throw await k(f);
64
- const p = await f.json(), v = await fetch(p.token_endpoint, {
65
- method: "POST",
66
- headers: {
67
- "Content-Type": "application/x-www-form-urlencoded"
68
- },
69
- body: new URLSearchParams({
70
- grant_type: "authorization_code",
71
- code: n,
72
- redirect_uri: h.redirectUri,
73
- code_verifier: c
74
- }).toString()
75
- });
76
- if (localStorage.removeItem("huggingface.co:oauth:code_verifier"), localStorage.removeItem("huggingface.co:oauth:nonce"), !v.ok)
77
- throw await k(v);
78
- const g = await v.json(), w = new Date(Date.now() + g.expires_in * 1e3), S = await fetch(p.userinfo_endpoint, {
79
- headers: {
80
- Authorization: `Bearer ${g.access_token}`
81
- }
82
- });
83
- if (!S.ok)
84
- throw await k(S);
85
- const u = await S.json();
86
- return {
87
- accessToken: g.access_token,
88
- accessTokenExpiresAt: w,
89
- userInfo: {
90
- id: u.sub,
91
- name: u.name,
92
- fullname: u.preferred_username,
93
- email: u.email,
94
- emailVerified: u.email_verified,
95
- avatarUrl: u.picture,
96
- websiteUrl: u.website,
97
- isPro: u.isPro,
98
- orgs: (t = (e = u.orgs) == null ? void 0 : e.map((_) => ({
99
- id: _.sub,
100
- name: _.name,
101
- fullname: _.name,
102
- isEnterprise: _.isEnterprise,
103
- canPay: _.canPay,
104
- avatarUrl: _.picture,
105
- roleInOrg: _.roleInOrg
106
- }))) != null ? t : []
107
- },
108
- state: h.state,
109
- scope: g.scope
110
- };
111
- }
112
- async function H(r) {
113
- if (typeof window > "u")
114
- throw new Error("oauthHandleRedirect is only available in the browser");
115
- const e = new URLSearchParams(window.location.search);
116
- return e.has("error") ? M() : e.has("code") ? localStorage.getItem("huggingface.co:oauth:nonce") ? M() : (console.warn(
117
- "Missing oauth nonce from localStorage. This can happen when the user refreshes the page after logging in, without changing the URL."
118
- ), !1) : !1;
119
- }
120
- async function V(r) {
121
- var e, t;
122
- if (typeof window > "u")
123
- throw new Error("oauthLogin is only available in the browser");
124
- const s = r?.hubUrl || $, i = `${new URL(s).origin}/.well-known/openid-configuration`, o = await fetch(i, {
125
- headers: {
126
- Accept: "application/json"
127
- }
128
- });
129
- if (!o.ok)
130
- throw await k(o);
131
- const n = await o.json(), a = globalThis.crypto.randomUUID(), c = globalThis.crypto.randomUUID() + globalThis.crypto.randomUUID();
132
- localStorage.setItem("huggingface.co:oauth:nonce", a), localStorage.setItem("huggingface.co:oauth:code_verifier", c);
133
- const l = r?.redirectUrl || window.location.href, h = JSON.stringify({
134
- nonce: a,
135
- redirectUri: l,
136
- state: r?.state
137
- }), d = (t = (e = window?.huggingface) == null ? void 0 : e.variables) != null ? t : null, m = r?.clientId || d?.OAUTH_CLIENT_ID;
138
- if (!m)
139
- throw d ? new Error("Missing clientId, please add hf_oauth: true to the README.md's metadata in your static Space") : new Error("Missing clientId");
140
- const f = N(
141
- new Uint8Array(await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(c)))
142
- ).replace(/[+]/g, "-").replace(/[/]/g, "_").replace(/=/g, "");
143
- return `${n.authorization_endpoint}?${new URLSearchParams({
144
- client_id: m,
145
- scope: r?.scopes || d?.OAUTH_SCOPES || "openid profile",
146
- response_type: "code",
147
- redirect_uri: l,
148
- state: h,
149
- code_challenge: f,
150
- code_challenge_method: "S256"
151
- }).toString()}`;
152
- }
153
- function R(r) {
154
- return r * Math.PI / 180;
155
- }
156
- function B(r, e, t) {
157
- const s = R(r), i = R(e), o = R(t), n = Math.cos(o), a = Math.sin(o), c = Math.cos(i), l = Math.sin(i), h = Math.cos(s), d = Math.sin(s);
158
- return [
159
- [n * c, n * l * d - a * h, n * l * h + a * d, 0],
160
- [a * c, a * l * d + n * h, a * l * h - n * d, 0],
161
- [-l, c * d, c * h, 0],
162
- [0, 0, 0, 1]
163
- ];
164
- }
165
- function G() {
166
- if (typeof window > "u" || !window.location.hash) return;
167
- const r = window.location.hash.startsWith("#") ? window.location.hash.slice(1) : window.location.hash;
168
- let e;
169
- try {
170
- e = new URLSearchParams(r);
171
- } catch {
172
- return;
173
- }
174
- const t = e.get("hf_token");
175
- if (!t) return;
176
- const s = e.get("hf_username") || "user", i = e.get("hf_token_expires"), o = i && !Number.isNaN(new Date(i).getTime()) ? i : new Date(Date.now() + 365 * 24 * 60 * 60 * 1e3).toISOString();
177
- try {
178
- sessionStorage.setItem("hf_token", t), sessionStorage.setItem("hf_username", s), sessionStorage.setItem("hf_token_expires", o);
179
- } catch (c) {
180
- console.warn("[reachy-mini] could not persist pre-seeded HF credentials:", c);
181
- }
182
- e.delete("hf_token"), e.delete("hf_username"), e.delete("hf_token_expires");
183
- const n = e.toString(), a = window.location.pathname + window.location.search + (n ? "#" + n : "");
184
- try {
185
- window.history.replaceState(null, "", a);
186
- } catch {
187
- }
188
- }
189
- function q() {
190
- if (typeof window > "u") return null;
191
- if (window.location.hash) {
192
- const r = window.location.hash.startsWith("#") ? window.location.hash.slice(1) : window.location.hash;
193
- try {
194
- const t = new URLSearchParams(r).get("robot_peer_id");
195
- if (t) return t;
196
- } catch {
197
- }
198
- }
199
- if (window.location.search)
200
- try {
201
- const e = new URLSearchParams(window.location.search).get("robot_peer_id");
202
- if (e) return e;
203
- } catch {
204
- }
205
- return null;
206
- }
207
- function W(r) {
208
- const e = r.split(`\r
209
- `);
210
- let t = !1;
211
- for (const s of e)
212
- if (s.startsWith("m=audio") ? t = !0 : s.startsWith("m=") && (t = !1), t && s === "a=sendrecv") return !0;
213
- return !1;
214
- }
215
- const y = 12 * 1024, E = 1 * 1024 * 1024, z = 512 * 1024;
216
- function J() {
217
- return typeof CompressionStream < "u";
218
- }
219
- function U() {
220
- return "u" + Math.random().toString(36).slice(2, 11) + Date.now().toString(36);
221
- }
222
- function A(r) {
223
- let e = "";
224
- for (let s = 0; s < r.length; s += 32768)
225
- e += String.fromCharCode.apply(
226
- null,
227
- Array.from(r.subarray(s, s + 32768))
228
- );
229
- return btoa(e);
230
- }
231
- async function X(r) {
232
- const e = new TextEncoder().encode(r), t = await new Response(
233
- new Blob([e]).stream().pipeThrough(new CompressionStream("gzip"))
234
- ).arrayBuffer();
235
- return A(new Uint8Array(t));
236
- }
237
- function L(r) {
238
- const e = Math.round(Number(r) || 0);
239
- return Math.max(0, Math.min(100, e));
240
- }
241
- function P(r) {
242
- switch ((r.type || "").toLowerCase().split(";")[0]?.trim() ?? "") {
243
- case "audio/ogg":
244
- case "application/ogg":
245
- return "ogg-base64";
246
- case "audio/opus":
247
- return "opus-base64";
248
- case "audio/mpeg":
249
- case "audio/mp3":
250
- return "mp3-base64";
251
- case "audio/flac":
252
- case "audio/x-flac":
253
- return "flac-base64";
254
- case "audio/mp4":
255
- case "audio/m4a":
256
- case "audio/x-m4a":
257
- return "m4a-base64";
258
- case "audio/aac":
259
- return "aac-base64";
260
- default:
261
- return "wav-base64";
262
- }
263
- }
264
- const j = 3e3, D = 1e3, Y = 6e4, K = 4e3;
265
- class Q extends EventTarget {
266
- // ─── Config ──────────────────────────────────────────────────────────
267
- _signalingUrl;
268
- _clientId;
269
- _appName;
270
- _videoJitterBufferTargetMs;
271
- _autoStartFromUrl;
272
- _autoStartAttempted;
273
- // ─── Public-ish state mirrors ────────────────────────────────────────
274
- _state = "disconnected";
275
- _robots = [];
276
- _robotState = {};
277
- _preselectedRobotId;
278
- // ─── Auth ────────────────────────────────────────────────────────────
279
- _token = null;
280
- _username = null;
281
- _tokenExpires = null;
282
- // ─── Signaling ───────────────────────────────────────────────────────
283
- _sseAbortController = null;
284
- // ─── WebRTC ──────────────────────────────────────────────────────────
285
- _pc = null;
286
- _dc = null;
287
- _sessionId = null;
288
- _selectedRobotId = null;
289
- _pendingRemoteIce = [];
290
- // ─── Audio ───────────────────────────────────────────────────────────
291
- _micStream = null;
292
- _micMuted = !0;
293
- _audioMuted = !0;
294
- _micSupported = !1;
295
- // ─── Timers ──────────────────────────────────────────────────────────
296
- _latencyMonitorId = null;
297
- _stateRefreshInterval = null;
298
- // ─── Single-slot promise resolvers ───────────────────────────────────
299
- _versionResolve = null;
300
- _hardwareIdResolve = null;
301
- _volumeResolve = null;
302
- _micVolumeResolve = null;
303
- _trackedFaceResolve = null;
304
- // applyAudioConfig() / readAudioParameter() share the same single-slot
305
- // pattern as the volume helpers. Separate slots so the two can be
306
- // in-flight concurrently without collision.
307
- _applyAudioConfigResolve = null;
308
- _readAudioParameterResolve = null;
309
- // ─── Log subscribers ─────────────────────────────────────────────────
310
- _logSubscribers = /* @__PURE__ */ new Set();
311
- _updateProgressSubscribers = /* @__PURE__ */ new Set();
312
- // ─── JSON-RPC app control (over the same DataChannel) ────────────────
313
- // rpcCall() sends {jsonrpc,id,method,params} and awaits the matching
314
- // response; onNotification() subscribes to one-way events (no id) the
315
- // robot/app pushes (conversation.phase/turn/transcript, ...).
316
- _rpcCounter = 0;
317
- _pendingRpc = /* @__PURE__ */ new Map();
318
- _rpcListeners = /* @__PURE__ */ new Map();
319
- // ─── Broadcast waiters (playMove / playUploadedAudio) ────────────────
320
- _broadcastWaiters = [];
321
- // ─── Active upload ids for no-arg cancels ────────────────────────────
322
- _activeMoveUploadId = null;
323
- _activeAudioUploadId = null;
324
- // ─── Session promise plumbing ────────────────────────────────────────
325
- _sessionResolve = null;
326
- _sessionReject = null;
327
- _iceConnected = !1;
328
- _dcOpen = !1;
329
- // ─── Resilience: ICE-blip debounce + network awareness ──────────────
330
- // Backs `_scheduleIceGrace` / `_armIceGraceOnVisibility` and the
331
- // `networkOnline` / `networkOffline` / `networkChange` forwarders.
332
- // All three handler slots are scoped to the lifetime of a live
333
- // session (installed in `startSession`, cleared in
334
- // `stopSession` / `disconnect` / `_handleEndSession` /
335
- // `_failSessionRejected`).
336
- _iceGraceTimer = null;
337
- _iceGraceReason = null;
338
- _pendingVisibilityHandler = null;
339
- _onlineHandler = null;
340
- _offlineHandler = null;
341
- _connectionChangeHandler = null;
342
- // ─── Motion completion plumbing (wake_up / goto_sleep) ───────────────
343
- _pendingMotionCompletions = {
344
- wake_up: [],
345
- goto_sleep: []
346
- };
347
- // ─── Video element ───────────────────────────────────────────────────
348
- _videoElement = null;
349
- constructor(e = {}) {
350
- super(), this._signalingUrl = e.signalingUrl || "https://pollen-robotics-reachy-mini-central.hf.space", this._clientId = e.clientId || null, this._appName = e.appName || "unknown", this._videoJitterBufferTargetMs = e.videoJitterBufferTargetMs ?? 0, this._autoStartFromUrl = e.autoStartFromUrl === !0, this._autoStartAttempted = !1, this._preselectedRobotId = q();
351
- }
352
- // ─── Read-only properties ────────────────────────────────────────────
353
- get state() {
354
- return this._state;
355
- }
356
- get robots() {
357
- return this._robots;
358
- }
359
- get robotState() {
360
- return this._robotState;
361
- }
362
- get username() {
363
- return this._username;
364
- }
365
- get isAuthenticated() {
366
- return !!this._token;
367
- }
368
- get micSupported() {
369
- return this._micSupported;
370
- }
371
- get micMuted() {
372
- return this._micMuted;
373
- }
374
- get audioMuted() {
375
- return this._audioMuted;
376
- }
377
- get preselectedRobotId() {
378
- return this._preselectedRobotId;
379
- }
380
- get isEmbedded() {
381
- return this._preselectedRobotId !== null;
382
- }
383
- /**
384
- * Internal: try to honour the `autoStartFromUrl` constructor
385
- * option. Called from the signaling-message handler after every
386
- * `robotsChanged` emit, so a robot that comes online after the
387
- * SDK is already `connected` still triggers the auto-start.
388
- */
389
- _maybeAutoStart() {
390
- if (!this._autoStartFromUrl || this._autoStartAttempted || !this._preselectedRobotId || this._state !== "connected" || !this._robots.find((s) => s.id === this._preselectedRobotId)) return;
391
- this._autoStartAttempted = !0;
392
- const t = this._preselectedRobotId;
393
- setTimeout(() => {
394
- this._state === "connected" && this.startSession(t).catch((s) => {
395
- console.warn("[reachy-mini] autoStartFromUrl: startSession rejected:", s);
396
- });
397
- }, 0);
398
- }
399
- // ─── Auth ────────────────────────────────────────────────────────────
400
- async authenticate() {
401
- try {
402
- G();
403
- const e = await H();
404
- if (e)
405
- return this._username = e.userInfo.preferred_username || e.userInfo.name || null, this._token = e.accessToken, this._tokenExpires = e.accessTokenExpiresAt, sessionStorage.setItem("hf_token", this._token), sessionStorage.setItem("hf_username", this._username ?? ""), sessionStorage.setItem(
406
- "hf_token_expires",
407
- typeof this._tokenExpires == "string" ? this._tokenExpires : this._tokenExpires.toISOString()
408
- ), !0;
409
- const t = sessionStorage.getItem("hf_token"), s = sessionStorage.getItem("hf_username"), i = sessionStorage.getItem("hf_token_expires");
410
- return t && s && i && new Date(i) > /* @__PURE__ */ new Date() ? (this._token = t, this._username = s, this._tokenExpires = i, !0) : !1;
411
- } catch (e) {
412
- return console.error("Auth error:", e), !1;
413
- }
414
- }
415
- async login() {
416
- const e = {};
417
- this._clientId && (e.clientId = this._clientId), window.location.href = await V(e);
418
- }
419
- logout() {
420
- sessionStorage.removeItem("hf_token"), sessionStorage.removeItem("hf_username"), sessionStorage.removeItem("hf_token_expires"), this._username = null, this._tokenExpires = null, this.disconnect();
421
- }
422
- // ─── Lifecycle ───────────────────────────────────────────────────────
423
- async connect(e) {
424
- if (this._state !== "disconnected") throw new Error("Already connected");
425
- if (e && (this._token = e), !this._token) throw new Error("No token — call authenticate() first or pass a token");
426
- this._sseAbortController = new AbortController();
427
- let t;
428
- try {
429
- t = await fetch(
430
- `${this._signalingUrl}/events`,
431
- {
432
- signal: this._sseAbortController.signal,
433
- headers: { Authorization: `Bearer ${this._token}` }
434
- }
435
- );
436
- } catch (s) {
437
- throw this._sseAbortController = null, s;
438
- }
439
- if (!t.ok)
440
- throw this._sseAbortController = null, new Error(`HTTP ${t.status}`);
441
- return new Promise((s, i) => {
442
- let o = !1;
443
- const n = t.body.getReader(), a = new TextDecoder();
444
- let c = "";
445
- (async () => {
446
- try {
447
- for (; ; ) {
448
- const { done: h, value: d } = await n.read();
449
- if (h) break;
450
- c += a.decode(d, { stream: !0 });
451
- const m = c.split(`
452
- `);
453
- c = m.pop() ?? "";
454
- for (const f of m)
455
- if (f.startsWith("data:"))
456
- try {
457
- const p = JSON.parse(f.slice(5).trim());
458
- !o && p.type === "welcome" && p.peerId && (o = !0, this._state = "connected", await this._sendToServer({
459
- type: "setPeerStatus",
460
- roles: ["listener"],
461
- meta: { name: this._appName }
462
- }), this._emit("connected", { peerId: p.peerId }), s()), this._handleSignalingMessage(p);
463
- } catch {
464
- }
465
- }
466
- } catch (h) {
467
- if (h.name !== "AbortError" && this._emit("error", { source: "signaling", error: h }), !o) {
468
- i(h);
469
- return;
470
- }
471
- }
472
- this._state !== "disconnected" && (this._state = "disconnected", this._emit("disconnected", { reason: "SSE closed" })), o || i(new Error("Connection closed before welcome"));
473
- })();
474
- });
475
- }
476
- async autoConnect(e = {}) {
477
- const {
478
- token: t,
479
- pickRobot: s,
480
- autoPickIfSingle: i = !0,
481
- filterBusy: o = !0,
482
- wakeOnConnect: n = !0
483
- } = e;
484
- if (this._state === "streaming") {
485
- const c = this._robots?.find((l) => l.id === this._selectedRobotId);
486
- return {
487
- robotId: this._selectedRobotId,
488
- robotName: c?.meta?.name ?? null,
489
- isEmbedded: this.isEmbedded,
490
- alreadyStreaming: !0
491
- };
492
- }
493
- const a = this._autoStartFromUrl;
494
- this._autoStartFromUrl = !1;
495
- try {
496
- if (t)
497
- this._token = t;
498
- else if (!this._token && !await this.authenticate())
499
- throw new Error("Not authenticated — call login() or pass a token");
500
- this._state === "disconnected" && await this.connect();
501
- let c, l = null;
502
- if (this.isEmbedded) {
503
- c = this._preselectedRobotId;
504
- try {
505
- await this._waitForRobotInList(c, 5e3);
506
- } catch {
507
- }
508
- l = this._robots?.find((d) => d.id === c)?.meta?.name ?? null;
509
- } else {
510
- const h = await this._fetchOwnedRobots({ filterBusy: o });
511
- if (h.length === 0)
512
- throw new Error("No reachable robots");
513
- if (i && h.length === 1 && !h[0].busy)
514
- c = h[0].id, l = h[0].name;
515
- else if (s) {
516
- const d = await s(h);
517
- if (!d) throw new Error("Robot selection cancelled");
518
- c = d, l = h.find((m) => m.id === d)?.name ?? null;
519
- } else
520
- throw new Error(
521
- "Multiple robots available — pass a pickRobot callback to autoConnect()"
522
- );
523
- }
524
- if (await this.startSession(c), n && typeof this.ensureAwake == "function")
525
- try {
526
- await this.ensureAwake();
527
- } catch (h) {
528
- console.warn("[reachy-mini] autoConnect: ensureAwake failed:", h);
529
- }
530
- return { robotId: c, robotName: l, isEmbedded: this.isEmbedded };
531
- } finally {
532
- this._autoStartFromUrl = a;
533
- }
534
- }
535
- async _fetchOwnedRobots({ filterBusy: e = !0 } = {}) {
536
- try {
537
- const t = await fetch(`${this._signalingUrl}/api/robot-status`, {
538
- headers: { Authorization: `Bearer ${this._token}` }
539
- });
540
- if (!t.ok) throw new Error(`HTTP ${t.status}`);
541
- const s = await t.json(), i = /* @__PURE__ */ new Map();
542
- for (const o of s.robots || []) {
543
- if (e && o.busy) continue;
544
- const n = o.meta?.install_id ?? o.meta?.hardware_id ?? o.peerId;
545
- i.set(n, {
546
- id: o.peerId,
547
- name: o.robotName ?? o.meta?.name ?? null,
548
- busy: !!o.busy,
549
- activeApp: o.activeApp ?? null,
550
- meta: o.meta ?? {},
551
- lastSeenAgeSeconds: o.last_seen_age_seconds ?? null
552
- });
553
- }
554
- return Array.from(i.values()).sort(
555
- (o, n) => (o.lastSeenAgeSeconds ?? 1 / 0) - (n.lastSeenAgeSeconds ?? 1 / 0)
556
- );
557
- } catch (t) {
558
- return console.warn("[reachy-mini] /api/robot-status unavailable, using SSE list:", t), (this._robots || []).map((s) => ({
559
- id: s.id,
560
- name: s.meta?.name ?? null,
561
- busy: !1,
562
- activeApp: null,
563
- meta: s.meta ?? {},
564
- lastSeenAgeSeconds: null
565
- }));
566
- }
567
- }
568
- _waitForRobotInList(e, t) {
569
- return this._robots?.find((s) => s.id === e) ? Promise.resolve() : new Promise((s, i) => {
570
- const o = () => {
571
- this._robots?.find((a) => a.id === e) && (this.removeEventListener("robotsChanged", o), clearTimeout(n), s());
572
- }, n = setTimeout(() => {
573
- this.removeEventListener("robotsChanged", o), i(new Error(`Timeout waiting for robot ${e} in list`));
574
- }, t);
575
- this.addEventListener("robotsChanged", o);
576
- });
577
- }
578
- async startSession(e) {
579
- if (this._state !== "connected") throw new Error("Not connected");
580
- this._selectedRobotId = e, this._iceConnected = !1, this._dcOpen = !1, this._micSupported = !1, this._pendingRemoteIce = [];
581
- try {
582
- const t = window, s = t.AudioContext ?? t.webkitAudioContext;
583
- if (!s) throw new Error("AudioContext not supported");
584
- const i = new s(), o = i.createMediaStreamDestination(), n = i.createOscillator(), a = i.createGain();
585
- a.gain.value = 0, n.connect(a).connect(o), n.start();
586
- const c = o.stream;
587
- c.getAudioTracks().forEach((l) => {
588
- l.enabled = !1;
589
- }), this._micStream = c, this._micMuted = !0;
590
- } catch (t) {
591
- console.warn("Audio sender placeholder setup failed:", t), this._micStream = null;
592
- }
593
- return this._pc = new RTCPeerConnection({
594
- iceServers: [{ urls: "stun:stun.l.google.com:19302" }]
595
- }), this._installNetworkListeners(), new Promise((t, s) => {
596
- this._sessionResolve = t, this._sessionReject = s, this._pc.ontrack = (i) => {
597
- if (i.track.kind === "video") {
598
- const o = this._videoJitterBufferTargetMs;
599
- try {
600
- i.receiver.jitterBufferTarget = o;
601
- } catch {
602
- }
603
- try {
604
- i.receiver.playoutDelayHint = o / 1e3;
605
- } catch {
606
- }
607
- this._emit("videoTrack", { track: i.track, stream: i.streams[0] });
608
- }
609
- }, this._pc.onicecandidate = async (i) => {
610
- i.candidate && this._sessionId && await this._sendToServer({
611
- type: "peer",
612
- sessionId: this._sessionId,
613
- ice: {
614
- candidate: i.candidate.candidate,
615
- sdpMLineIndex: i.candidate.sdpMLineIndex,
616
- sdpMid: i.candidate.sdpMid
617
- }
618
- });
619
- }, this._pc.oniceconnectionstatechange = () => {
620
- const i = this._pc?.iceConnectionState;
621
- if (i) {
622
- if (this._emit("iceStateChange", { state: i }), i === "connected" || i === "completed") {
623
- this._clearIceGrace(), this._iceConnected = !0, this._checkSessionReady();
624
- return;
625
- }
626
- if (i === "disconnected") {
627
- typeof document < "u" && document.hidden ? this._armIceGraceOnVisibility() : this._scheduleIceGrace(j, "disconnected");
628
- return;
629
- }
630
- if (i === "failed") {
631
- this._scheduleIceGrace(D, "failed");
632
- return;
633
- }
634
- }
635
- }, this._pc.ondatachannel = (i) => {
636
- this._dc = i.channel, this._dc.onopen = () => {
637
- this._dcOpen = !0, this._checkSessionReady();
638
- }, this._dc.onmessage = (o) => this._handleRobotMessage(JSON.parse(o.data));
639
- }, this._sendToServer({ type: "startSession", peerId: e }).then((i) => {
640
- if (i?.type === "sessionRejected") {
641
- this._failSessionRejected(i);
642
- return;
643
- }
644
- i?.sessionId && (this._sessionId = i.sessionId);
645
- });
646
- });
647
- }
648
- _failSessionRejected(e) {
649
- const t = new Error(
650
- e.reason === "robot_busy" ? `Robot is busy: "${e.activeApp || "another app"}" is already connected` : `Session rejected: ${e.reason || "unknown reason"}`
651
- );
652
- if (t.reason = e.reason ?? null, t.activeApp = e.activeApp ?? null, this._clearIceGrace(), this._uninstallNetworkListeners(), this._pc && (this._pc.close(), this._pc = null), this._micStream && (this._micStream.getTracks().forEach((s) => s.stop()), this._micStream = null), this._iceConnected = !1, this._dcOpen = !1, this._micMuted = !0, this._micSupported = !1, this._emit("sessionRejected", { reason: e.reason, activeApp: e.activeApp }), this._sessionReject) {
653
- const s = this._sessionReject;
654
- this._sessionResolve = null, this._sessionReject = null, s(t);
655
- }
656
- }
657
- async stopSession() {
658
- this._versionResolve && (this._versionResolve(null), this._versionResolve = null), this._hardwareIdResolve && (this._hardwareIdResolve(null), this._hardwareIdResolve = null), this._volumeResolve && (this._volumeResolve(null), this._volumeResolve = null), this._micVolumeResolve && (this._micVolumeResolve(null), this._micVolumeResolve = null), this._trackedFaceResolve && (this._trackedFaceResolve(null), this._trackedFaceResolve = null), this._applyAudioConfigResolve && (this._applyAudioConfigResolve(!1), this._applyAudioConfigResolve = null), this._readAudioParameterResolve && (this._readAudioParameterResolve(null), this._readAudioParameterResolve = null), this._logSubscribers.clear(), this._updateProgressSubscribers.clear(), this._rejectPendingMotionCompletions(new Error("Session stopped")), this._rejectPendingRpc(new Error("Session stopped")), this._clearIceGrace(), this._uninstallNetworkListeners(), this._sessionReject && (this._sessionReject(new Error("Session stopped")), this._sessionResolve = null, this._sessionReject = null), this._stateRefreshInterval && (clearInterval(this._stateRefreshInterval), this._stateRefreshInterval = null), this._latencyMonitorId && (clearInterval(this._latencyMonitorId), this._latencyMonitorId = null), this._sessionId && await this._sendToServer({ type: "endSession", sessionId: this._sessionId }), this._micStream && (this._micStream.getTracks().forEach((t) => t.stop()), this._micStream = null), this._micMuted = !0, this._micSupported = !1, this._pc && (this._pc.close(), this._pc = null), this._dc && (this._dc.close(), this._dc = null), this._sessionId = null, this._iceConnected = !1, this._dcOpen = !1, this._state === "streaming" && (this._state = "connected", this._emit("sessionStopped", { reason: "user" }));
659
- }
660
- disconnect() {
661
- this._sseAbortController && (this._sseAbortController.abort(), this._sseAbortController = null), this._versionResolve && (this._versionResolve(null), this._versionResolve = null), this._hardwareIdResolve && (this._hardwareIdResolve(null), this._hardwareIdResolve = null), this._volumeResolve && (this._volumeResolve(null), this._volumeResolve = null), this._micVolumeResolve && (this._micVolumeResolve(null), this._micVolumeResolve = null), this._trackedFaceResolve && (this._trackedFaceResolve(null), this._trackedFaceResolve = null), this._applyAudioConfigResolve && (this._applyAudioConfigResolve(!1), this._applyAudioConfigResolve = null), this._readAudioParameterResolve && (this._readAudioParameterResolve(null), this._readAudioParameterResolve = null), this._logSubscribers.clear(), this._updateProgressSubscribers.clear(), this._rejectPendingMotionCompletions(new Error("Disconnected")), this._clearIceGrace(), this._uninstallNetworkListeners(), this._sessionReject && (this._sessionReject(new Error("Disconnected")), this._sessionResolve = null, this._sessionReject = null), this._stateRefreshInterval && (clearInterval(this._stateRefreshInterval), this._stateRefreshInterval = null), this._latencyMonitorId && (clearInterval(this._latencyMonitorId), this._latencyMonitorId = null), this._sessionId && this._token && this._sendToServer({ type: "endSession", sessionId: this._sessionId }), this._micStream && (this._micStream.getTracks().forEach((e) => e.stop()), this._micStream = null), this._pc && (this._pc.close(), this._pc = null), this._dc && (this._dc.close(), this._dc = null), this._sessionId = null, this._micMuted = !0, this._micSupported = !1, this._iceConnected = !1, this._dcOpen = !1, this._robots = [], this._state = "disconnected", this._emit("disconnected", { reason: "user" });
662
- }
663
- // ─── Resilience: ICE-blip debounce + network awareness ───────────────
664
- //
665
- // Both halves below are intentionally generic (they don't know about
666
- // motion, audio, or the FSM): they just smooth out browser-level
667
- // events so the consumer's own state machine doesn't get torn down
668
- // by routine WiFi/4G/screen-off noise.
669
- /**
670
- * Cancel any pending ICE grace timer and visibility handler. Called
671
- * on a healed `connected`/`completed` transition AND from the
672
- * lifecycle teardown paths so a callback can't fire after `_pc`
673
- * is closed.
674
- */
675
- _clearIceGrace() {
676
- this._iceGraceTimer !== null && (clearTimeout(this._iceGraceTimer), this._iceGraceTimer = null), this._iceGraceReason = null, this._pendingVisibilityHandler && typeof document < "u" && document.removeEventListener("visibilitychange", this._pendingVisibilityHandler), this._pendingVisibilityHandler = null;
677
- }
678
- /**
679
- * Start a grace window. After `ms`, re-check the live ICE state:
680
- * - If we healed back to `connected`/`completed`, the timer was
681
- * already cancelled in `oniceconnectionstatechange`, so we
682
- * never get here.
683
- * - If we're still in the originally-observed bad state (or
684
- * worse), surface the error and reject any pending session
685
- * promise. The original code path is preserved verbatim so
686
- * downstream consumers see the same `error` payload shape.
687
- */
688
- _scheduleIceGrace(e, t) {
689
- if (this._iceGraceTimer !== null) {
690
- if (this._iceGraceReason === t) return;
691
- clearTimeout(this._iceGraceTimer);
692
- }
693
- this._iceGraceReason = t, this._iceGraceTimer = setTimeout(() => {
694
- this._iceGraceTimer = null;
695
- const s = this._iceGraceReason;
696
- this._iceGraceReason = null;
697
- const i = this._pc?.iceConnectionState;
698
- if (!(i === "connected" || i === "completed")) {
699
- if (s === "disconnected" && i === "disconnected") {
700
- this._emit("error", {
701
- source: "webrtc",
702
- error: new Error(`ICE stuck in 'disconnected' for > ${e}ms`)
703
- });
704
- return;
705
- }
706
- if (s === "failed" || i === "failed") {
707
- const o = new Error("ICE connection failed");
708
- this._sessionReject && (this._sessionReject(o), this._sessionResolve = null, this._sessionReject = null), this._emit("error", { source: "webrtc", error: o });
709
- }
710
- }
711
- }, e);
712
- }
713
- /**
714
- * `disconnected` while the tab is hidden. JS timers are throttled
715
- * in background tabs (Chrome clamps to ~1 Hz, Safari can pause
716
- * altogether), so a foreground grace timer would either miss the
717
- * window or fire long after the connection healed. Wait for the
718
- * tab to come back, then re-evaluate.
719
- */
720
- _armIceGraceOnVisibility() {
721
- if (this._pendingVisibilityHandler) return;
722
- const e = Date.now(), t = () => {
723
- if (typeof document < "u" && document.hidden || (document.removeEventListener("visibilitychange", t), this._pendingVisibilityHandler = null, !this._pc)) return;
724
- const s = this._pc.iceConnectionState;
725
- if (!(s === "connected" || s === "completed")) {
726
- if (Date.now() - e > Y) {
727
- const i = new Error(
728
- "Session expired while tab was backgrounded"
729
- );
730
- this._sessionReject && (this._sessionReject(i), this._sessionResolve = null, this._sessionReject = null), this._emit("error", { source: "webrtc", error: i });
731
- return;
732
- }
733
- if (s === "failed") {
734
- this._scheduleIceGrace(D, "failed");
735
- return;
736
- }
737
- this._scheduleIceGrace(j, "disconnected");
738
- }
739
- };
740
- document.addEventListener("visibilitychange", t), this._pendingVisibilityHandler = t;
741
- }
742
- /**
743
- * Install browser-level network listeners and forward them as
744
- * public `networkOnline` / `networkOffline` / `networkChange`
745
- * events on this instance. Idempotent: called from
746
- * `startSession()`, removed by `_uninstallNetworkListeners` on
747
- * teardown. Reachable only when there's a live `window`
748
- * (defensive guard for SSR / test environments).
749
- *
750
- * `online` / `offline` are semantically about CONNECTIVITY:
751
- * "does the OS think we can reach the internet". They flip
752
- * symmetrically.
753
- *
754
- * `connection.change` (NetworkInformation API, Chrome / Android
755
- * WebView only) is semantically about the TRANSPORT: it fires
756
- * on Wi-Fi → 4G swaps, AP roams, etc. without necessarily going
757
- * through `offline`. We forward it as its own `networkChange`
758
- * event rather than aliasing it onto `networkOnline`, so
759
- * consumers don't have to guess whether they're seeing a real
760
- * connectivity recovery or a silent transport swap.
761
- */
762
- _installNetworkListeners() {
763
- if (this._onlineHandler || typeof window > "u") return;
764
- const e = () => this._emit("networkOnline", {}), t = () => this._emit("networkOffline", {});
765
- window.addEventListener("online", e), window.addEventListener("offline", t), this._onlineHandler = e, this._offlineHandler = t;
766
- const s = navigator.connection;
767
- if (s && typeof s.addEventListener == "function") {
768
- const i = () => this._emit("networkChange", {
769
- effectiveType: s.effectiveType,
770
- downlink: s.downlink,
771
- rtt: s.rtt,
772
- saveData: s.saveData
773
- });
774
- s.addEventListener("change", i), this._connectionChangeHandler = i;
775
- }
776
- }
777
- /** Counterpart to `_installNetworkListeners`. */
778
- _uninstallNetworkListeners() {
779
- typeof window < "u" && (this._onlineHandler && window.removeEventListener("online", this._onlineHandler), this._offlineHandler && window.removeEventListener("offline", this._offlineHandler));
780
- const e = navigator.connection;
781
- e && this._connectionChangeHandler && typeof e.removeEventListener == "function" && e.removeEventListener("change", this._connectionChangeHandler), this._onlineHandler = null, this._offlineHandler = null, this._connectionChangeHandler = null;
782
- }
783
- // ─── Commands ────────────────────────────────────────────────────────
784
- /**
785
- * Atomic raw-units pose update over the data channel. Channels you
786
- * omit are held at their last commanded value (per-axis, independent).
787
- *
788
- * **Head pose is in the WORLD frame.** The daemon's IK splits the
789
- * requested head world-yaw between body rotation and the stewart
790
- * platform, subject to the mechanical limit
791
- * `|head_yaw_world − body_yaw| ≤ 65°`.
792
- *
793
- * **If you want the head to FOLLOW the body** (tank-style rotation):
794
- * a `setTarget({ body_yaw })` on its own does NOT rotate the head —
795
- * the head's commanded world yaw is unchanged, so its gaze stays
796
- * pinned in world frame while the body turns under it. To make the
797
- * head turn with the body, include a `head` matrix in the SAME call
798
- * with the body-yaw delta added to the head RPY's yaw:
799
- *
800
- * ```ts
801
- * // Body-yaw drag handler: tank-couple the head so it follows.
802
- * const delta = newBodyDeg - lastCommandedBodyDeg;
803
- * const nextHeadYaw = lastCommandedHeadYawDeg + delta;
804
- * robot.setTarget({
805
- * head: rpyToMatrix(headRoll, headPitch, nextHeadYaw).flat(),
806
- * body_yaw: degToRad(newBodyDeg),
807
- * });
808
- * lastCommandedHeadYawDeg = nextHeadYaw;
809
- * lastCommandedBodyDeg = newBodyDeg;
810
- * ```
811
- *
812
- * **Baseline must be the last COMMANDED value, not telemetry.** For
813
- * continuous-input controllers (slider drag, joystick), do not use
814
- * `state.head` from the `state` event as the baseline for incremental
815
- * commands — telemetry lags one WebRTC round-trip, so cumulative
816
- * deltas computed against it stall (every iteration in a rapid drag
817
- * adds the same `delta` to the same stale baseline → the head fails
818
- * to keep up). Track the last-commanded RPY in your own buffer.
819
- *
820
- * @param head Flat row-major 4×4 matrix (16 finite numbers) in
821
- * the world frame. Omit to hold the previous head target.
822
- * @param antennas `[rightRad, leftRad]` (radians). Omit to hold.
823
- * @param body_yaw Signed radians. Omit to hold.
824
- * @returns `true` if the command was queued on the data channel,
825
- * `false` if the channel is not open.
826
- */
827
- setTarget({ head: e, antennas: t, body_yaw: s } = {}) {
828
- const i = { type: "set_full_target" };
829
- if (e !== void 0) {
830
- if (!Array.isArray(e) || e.length !== 16 || !e.every((o) => Number.isFinite(o)))
831
- throw new TypeError(
832
- `setTarget: head must be a 16-element flat row-major 4×4 matrix of finite numbers; got ${Array.isArray(e) ? `Array(${e.length})` : typeof e}`
833
- );
834
- i.head = e;
835
- }
836
- if (t !== void 0) {
837
- if (!Array.isArray(t) || t.length !== 2 || !t.every((o) => Number.isFinite(o)))
838
- throw new TypeError(
839
- `setTarget: antennas must be [rightRad, leftRad] (2 finite numbers); got ${Array.isArray(t) ? `Array(${t.length})` : typeof t}`
840
- );
841
- i.antennas = t;
842
- }
843
- if (s !== void 0) {
844
- if (!Number.isFinite(s))
845
- throw new TypeError(
846
- `setTarget: body_yaw must be a finite number (radians); got ${s}`
847
- );
848
- i.body_yaw = s;
849
- }
850
- return this._sendCommand(i);
851
- }
852
- gotoTarget({ head: e, antennas: t, body_yaw: s, duration: i }) {
853
- const o = { type: "goto_target" };
854
- if (e !== void 0) {
855
- if (!Array.isArray(e) || e.length !== 16 || !e.every((n) => Number.isFinite(n)))
856
- throw new TypeError(
857
- `gotoTarget: head must be a 16-element flat row-major 4×4 matrix of finite numbers; got ${Array.isArray(e) ? `Array(${e.length})` : typeof e}`
858
- );
859
- o.head = e;
860
- }
861
- if (t !== void 0) {
862
- if (!Array.isArray(t) || t.length !== 2 || !t.every((n) => Number.isFinite(n)))
863
- throw new TypeError(
864
- `gotoTarget: antennas must be [rightRad, leftRad] (2 finite numbers); got ${Array.isArray(t) ? `Array(${t.length})` : typeof t}`
865
- );
866
- o.antennas = t;
867
- }
868
- if (s !== void 0) {
869
- if (!Number.isFinite(s))
870
- throw new TypeError(
871
- `gotoTarget: body_yaw must be a finite number (radians); got ${s}`
872
- );
873
- o.body_yaw = s;
874
- }
875
- if (!Number.isFinite(i) || i <= 0)
876
- throw new TypeError(
877
- `gotoTarget: duration must be a positive finite number (seconds); got ${i}`
878
- );
879
- return o.duration = i, this._sendCommand(o);
880
- }
881
- setHeadRpyDeg(e, t, s) {
882
- return this.setTarget({ head: B(e, t, s).flat() });
883
- }
884
- setAntennasDeg(e, t) {
885
- return this.setTarget({ antennas: [R(e), R(t)] });
886
- }
887
- setBodyYawDeg(e) {
888
- return this.setTarget({ body_yaw: R(e) });
889
- }
890
- playSound(e) {
891
- return this._sendCommand({ type: "play_sound", file: e });
892
- }
893
- clearIncomingAudio() {
894
- return this._sendCommand({ type: "clear_incoming_audio" });
895
- }
896
- startHeadTracking(e = 1) {
897
- if (!Number.isFinite(e))
898
- throw new TypeError(`startHeadTracking: weight must be a finite number; got ${e}`);
899
- const t = Math.min(Math.max(e, 0), 1);
900
- return this._sendCommand({
901
- type: "set_head_tracking",
902
- enabled: !0,
903
- weight: t
904
- });
905
- }
906
- stopHeadTracking() {
907
- return this._sendCommand({ type: "set_head_tracking", enabled: !1 });
908
- }
909
- getTrackedFace() {
910
- return this._slotRoundtrip(
911
- () => this._trackedFaceResolve,
912
- (e) => {
913
- this._trackedFaceResolve = e;
914
- },
915
- { type: "get_tracked_face" }
916
- );
917
- }
918
- /**
919
- * Trigger a PyPI update of the daemon over the data channel. Remote
920
- * counterpart of `POST /update/start`. The daemon acks then restarts
921
- * itself once the install finishes, which tears this session down -
922
- * the caller is expected to reconnect afterwards.
923
- *
924
- * Pass `onProgress` to receive `update_progress` events (one per log
925
- * line of the update job). A *successful* update restarts the daemon
926
- * before a `done` event can arrive, so treat the session teardown +
927
- * a successful reconnect as the success signal; `onProgress` will fire
928
- * with `status: 'failed'` if the install errors before the restart.
929
- *
930
- * Returns `false` if the data channel isn't open.
931
- */
932
- startDaemonUpdate({ preRelease: e = !1, onProgress: t } = {}) {
933
- return t && this._updateProgressSubscribers.add(t), this._sendCommand({ type: "start_update", pre_release: e });
934
- }
935
- setMotorMode(e) {
936
- return this._sendCommand({ type: "set_motor_mode", mode: e });
937
- }
938
- setMotorTorque(e, t = null) {
939
- return this._sendCommand({ type: "set_torque", on: e, ids: t });
940
- }
941
- wakeUp({ timeoutMs: e = 8e3 } = {}) {
942
- return this._sendCommand({ type: "set_motor_mode", mode: "enabled" }), this._sendCommandAwaitCompletion("wake_up", e);
943
- }
944
- gotoSleep({ timeoutMs: e = 8e3 } = {}) {
945
- return this._sendCommandAwaitCompletion("goto_sleep", e);
946
- }
947
- _sendCommandAwaitCompletion(e, t) {
948
- return this._sendCommand({ type: e }) ? new Promise((s, i) => {
949
- const o = {
950
- resolve: s,
951
- reject: i,
952
- timer: setTimeout(() => {
953
- const n = this._pendingMotionCompletions[e], a = n.indexOf(o);
954
- a !== -1 && n.splice(a, 1), i(new Error(`${e} timed out after ${t}ms`));
955
- }, t)
956
- };
957
- this._pendingMotionCompletions[e].push(o);
958
- }) : Promise.reject(new Error(`${e}: data channel not open`));
959
- }
960
- _rejectPendingMotionCompletions(e) {
961
- for (const t of Object.keys(this._pendingMotionCompletions)) {
962
- const s = this._pendingMotionCompletions[t];
963
- for (; s.length; ) {
964
- const i = s.shift();
965
- clearTimeout(i.timer), i.reject(e);
966
- }
967
- }
968
- }
969
- isAwake() {
970
- const e = this._robotState?.motor_mode;
971
- return e === "enabled" || e === "gravity_compensation";
972
- }
973
- async ensureAwake(e = 1e3) {
974
- return this._robotState?.motor_mode === void 0 && await new Promise((t) => {
975
- const s = () => {
976
- this.removeEventListener("state", s), clearTimeout(i), t();
977
- }, i = setTimeout(s, e);
978
- this.addEventListener("state", s), this.requestState();
979
- }), this.isAwake() || this.wakeUp().catch(() => {
980
- }), !0;
981
- }
982
- getVersion() {
983
- return new Promise((e, t) => {
984
- if (!this._dc || this._dc.readyState !== "open") {
985
- t(new Error("Data channel not open"));
986
- return;
987
- }
988
- this._versionResolve && this._versionResolve(null), this._versionResolve = e, this._sendCommand({ type: "get_version" });
989
- });
990
- }
991
- getHardwareId() {
992
- return new Promise((e, t) => {
993
- if (!this._dc || this._dc.readyState !== "open") {
994
- t(new Error("Data channel not open"));
995
- return;
996
- }
997
- this._hardwareIdResolve && this._hardwareIdResolve(null), this._hardwareIdResolve = e, this._sendCommand({ type: "get_hardware_id" });
998
- });
999
- }
1000
- getVolume() {
1001
- return this._slotRoundtrip(
1002
- () => this._volumeResolve,
1003
- (e) => {
1004
- this._volumeResolve = e;
1005
- },
1006
- { type: "get_volume" }
1007
- );
1008
- }
1009
- setVolume(e) {
1010
- return this._slotRoundtrip(
1011
- () => this._volumeResolve,
1012
- (t) => {
1013
- this._volumeResolve = t;
1014
- },
1015
- { type: "set_volume", volume: L(e) }
1016
- );
1017
- }
1018
- getMicrophoneVolume() {
1019
- return this._slotRoundtrip(
1020
- () => this._micVolumeResolve,
1021
- (e) => {
1022
- this._micVolumeResolve = e;
1023
- },
1024
- { type: "get_microphone_volume" }
1025
- );
1026
- }
1027
- setMicrophoneVolume(e) {
1028
- return this._slotRoundtrip(
1029
- () => this._micVolumeResolve,
1030
- (t) => {
1031
- this._micVolumeResolve = t;
1032
- },
1033
- { type: "set_microphone_volume", volume: L(e) }
1034
- );
1035
- }
1036
- applyAudioConfig(e, { verify: t = !0 } = {}) {
1037
- return this._slotRoundtrip(
1038
- () => this._applyAudioConfigResolve,
1039
- (s) => {
1040
- this._applyAudioConfigResolve = s;
1041
- },
1042
- { type: "apply_audio_config", config: e, verify: t }
1043
- ).then((s) => s === !0);
1044
- }
1045
- readAudioParameter(e) {
1046
- return this._slotRoundtrip(
1047
- () => this._readAudioParameterResolve,
1048
- (t) => {
1049
- this._readAudioParameterResolve = t;
1050
- },
1051
- { type: "read_audio_parameter", name: e }
1052
- );
1053
- }
1054
- /**
1055
- * Internal: send a command and await the matching daemon response in a
1056
- * named single-resolver slot. Used by the volume helpers and the
1057
- * XVF3800 audio-config helpers — every one of them has a strict
1058
- * request/response shape where a single in-flight call per slot is
1059
- * sufficient. If a previous request on the same slot is still
1060
- * pending when a new one comes in, the older promise is resolved to
1061
- * `null` so its caller doesn't hang forever.
1062
- *
1063
- * Slot access is passed in as getter/setter closures rather than a
1064
- * key into `this`: that keeps the helper fully generic-checked (T is
1065
- * inferred from the slot's resolver type at each call site) with no
1066
- * indexed-property casts.
1067
- */
1068
- _slotRoundtrip(e, t, s) {
1069
- return new Promise((i, o) => {
1070
- if (!this._dc || this._dc.readyState !== "open") {
1071
- o(new Error("Data channel not open"));
1072
- return;
1073
- }
1074
- const n = e();
1075
- n && n(null);
1076
- let a;
1077
- const c = (l) => {
1078
- a !== void 0 && (clearTimeout(a), a = void 0), e() === c && t(null), i(l);
1079
- };
1080
- t(c), a = setTimeout(() => c(null), K), this._sendCommand(s);
1081
- });
1082
- }
1083
- sendRaw(e) {
1084
- return this._sendCommand(e);
1085
- }
1086
- subscribeLogs({ onLine: e, onError: t }) {
1087
- if (typeof e != "function")
1088
- throw new TypeError("subscribeLogs: onLine callback is required");
1089
- const s = { onLine: e, onError: t }, i = this._logSubscribers.size === 0;
1090
- this._logSubscribers.add(s), i && this._sendCommand({ type: "subscribe_logs" });
1091
- let o = !1;
1092
- return () => {
1093
- o || (o = !0, this._logSubscribers.delete(s), this._logSubscribers.size === 0 && this._sendCommand({ type: "unsubscribe_logs" }));
1094
- };
1095
- }
1096
- requestState() {
1097
- return this._sendCommand({ type: "get_state" });
1098
- }
1099
- // ─── Audio ───────────────────────────────────────────────────────────
1100
- setAudioMuted(e) {
1101
- this._audioMuted = e, this._videoElement && (this._videoElement.muted = e);
1102
- }
1103
- setMicMuted(e) {
1104
- this._micMuted = e, this._micStream && this._micStream.getAudioTracks().forEach((t) => {
1105
- t.enabled = !e;
1106
- });
1107
- }
1108
- // ─── Video helper ────────────────────────────────────────────────────
1109
- attachVideo(e) {
1110
- this._videoElement = e, e.muted = this._audioMuted;
1111
- const t = (i) => {
1112
- const o = i;
1113
- e.srcObject = o.detail.stream, e.playsInline = !0, "requestVideoFrameCallback" in e && this._startLatencyMonitor(e);
1114
- }, s = () => {
1115
- e.srcObject = null;
1116
- };
1117
- return this.addEventListener("videoTrack", t), this.addEventListener("sessionStopped", s), () => {
1118
- this.removeEventListener("videoTrack", t), this.removeEventListener("sessionStopped", s), this._latencyMonitorId && (clearInterval(this._latencyMonitorId), this._latencyMonitorId = null), e.srcObject = null, this._videoElement = null;
1119
- };
1120
- }
1121
- // ─── Daemon-side recorded-move playback ──────────────────────────────
1122
- async playMove(e, {
1123
- audioBlob: t = null,
1124
- audioLeadMs: s = -100,
1125
- description: i = "move",
1126
- encoding: o = "gzip+base64",
1127
- playFrequency: n = 100,
1128
- initialGotoDuration: a = 0,
1129
- startTimeoutMs: c = 8e3,
1130
- onProgress: l = () => {
1131
- },
1132
- onStarted: h = () => {
1133
- }
1134
- } = {}) {
1135
- if (!this._dc || this._dc.readyState !== "open")
1136
- throw new Error("data channel not open");
1137
- if (!e?.time?.length || !e?.set_target_data?.length)
1138
- throw new Error("playMove: motion must have time + set_target_data");
1139
- const d = U();
1140
- this._activeMoveUploadId = d;
1141
- const m = {
1142
- description: i,
1143
- time: e.time,
1144
- set_target_data: e.set_target_data
1145
- }, f = JSON.stringify(m);
1146
- let p, v;
1147
- o === "gzip+base64" && J() ? (p = await X(f), v = "gzip+base64") : (p = f, v = "json");
1148
- const g = Math.ceil(p.length / y) || 1;
1149
- l({
1150
- phase: "starting",
1151
- sent: 0,
1152
- total: g,
1153
- bytes: p.length,
1154
- encoding: v
1155
- }), this._sendCommand({
1156
- type: "upload_move_start",
1157
- upload_id: d,
1158
- total_chunks: g,
1159
- description: i,
1160
- encoding: v
1161
- });
1162
- for (let u = 0; u < g; u++) {
1163
- this._dc.bufferedAmount > E && await this._awaitDataChannelDrain();
1164
- const _ = u * y;
1165
- this._sendCommand({
1166
- type: "upload_move_chunk",
1167
- upload_id: d,
1168
- chunk_index: u,
1169
- chunk: p.slice(_, _ + y)
1170
- }), l({ phase: "upload", sent: u + 1, total: g });
1171
- }
1172
- if (this._sendCommand({ type: "upload_move_finish", upload_id: d }), l({ phase: "uploaded", sent: g, total: g }), t) {
1173
- const u = new Uint8Array(await t.arrayBuffer()), _ = A(u), b = Math.ceil(_.length / y) || 1;
1174
- l({
1175
- phase: "audio-starting",
1176
- sent: 0,
1177
- total: b,
1178
- bytes: _.length
1179
- }), this._sendCommand({
1180
- type: "upload_audio_start",
1181
- upload_id: d,
1182
- total_chunks: b,
1183
- encoding: P(t),
1184
- description: i
1185
- });
1186
- for (let I = 0; I < b; I++) {
1187
- this._dc.bufferedAmount > E && await this._awaitDataChannelDrain();
1188
- const T = I * y;
1189
- this._sendCommand({
1190
- type: "upload_audio_chunk",
1191
- upload_id: d,
1192
- chunk_index: I,
1193
- chunk: _.slice(T, T + y)
1194
- }), l({ phase: "audio-upload", sent: I + 1, total: b });
1195
- }
1196
- this._sendCommand({ type: "upload_audio_finish", upload_id: d }), l({ phase: "audio-uploaded", sent: b, total: b });
1197
- }
1198
- this._sendCommand({
1199
- type: "play_uploaded_move",
1200
- upload_id: d,
1201
- play_frequency: n,
1202
- initial_goto_duration: a,
1203
- audio_lead_ms: s
1204
- });
1205
- let w;
1206
- try {
1207
- w = await this._waitForBroadcast(
1208
- (u) => u?.type === "play_uploaded_move" && u?.upload_id === d && (u.started === !0 || typeof u.error == "string"),
1209
- { timeoutMs: c, debugLabel: "play_uploaded_move started" }
1210
- );
1211
- } catch (u) {
1212
- throw new Error(
1213
- `Daemon did not respond to play_uploaded_move (requires the reachy_mini daemon with feature/daemon-side-move-upload). Underlying: ${u.message}`
1214
- );
1215
- }
1216
- if (typeof w.error == "string")
1217
- throw new Error(`play_uploaded_move: ${w.error}`);
1218
- try {
1219
- h({
1220
- duration_s: w.duration_s,
1221
- has_audio: w.has_audio === !0
1222
- });
1223
- } catch (u) {
1224
- console.warn("playMove.onStarted threw:", u);
1225
- }
1226
- l({ phase: "playing", duration_s: w.duration_s });
1227
- const S = await this._waitForBroadcast(
1228
- (u) => u?.type === "play_uploaded_move" && u?.upload_id === d && (u.finished === !0 || u.cancelled === !0 || typeof u.error == "string"),
1229
- {
1230
- timeoutMs: (w.duration_s + 30) * 1e3,
1231
- debugLabel: "play_uploaded_move final"
1232
- }
1233
- );
1234
- return this._activeMoveUploadId === d && (this._activeMoveUploadId = null), S;
1235
- }
1236
- cancelMove(e = null) {
1237
- const t = e ?? this._activeMoveUploadId;
1238
- return t ? this._sendCommand({ type: "cancel_move", upload_id: t }) : !1;
1239
- }
1240
- async uploadAudio(e, { description: t = "audio", onProgress: s = () => {
1241
- } } = {}) {
1242
- if (!this._dc || this._dc.readyState !== "open")
1243
- throw new Error("data channel not open");
1244
- if (!(e instanceof Blob))
1245
- throw new TypeError("uploadAudio: expected a Blob");
1246
- const i = U(), o = new Uint8Array(await e.arrayBuffer()), n = A(o), a = Math.ceil(n.length / y) || 1;
1247
- s({ phase: "audio-starting", sent: 0, total: a, bytes: n.length }), this._sendCommand({
1248
- type: "upload_audio_start",
1249
- upload_id: i,
1250
- total_chunks: a,
1251
- encoding: P(e),
1252
- description: t
1253
- });
1254
- for (let c = 0; c < a; c++) {
1255
- this._dc.bufferedAmount > E && await this._awaitDataChannelDrain();
1256
- const l = c * y;
1257
- this._sendCommand({
1258
- type: "upload_audio_chunk",
1259
- upload_id: i,
1260
- chunk_index: c,
1261
- chunk: n.slice(l, l + y)
1262
- }), s({ phase: "audio-upload", sent: c + 1, total: a });
1263
- }
1264
- return this._sendCommand({ type: "upload_audio_finish", upload_id: i }), s({ phase: "audio-uploaded", sent: a, total: a }), i;
1265
- }
1266
- async playUploadedAudio(e, { timeoutMs: t = 8e3 } = {}) {
1267
- if (!this._dc || this._dc.readyState !== "open")
1268
- throw new Error("data channel not open");
1269
- const s = this._waitForBroadcast(
1270
- (o) => o?.type === "play_uploaded_audio" && o?.upload_id === e && (o.started === !0 || typeof o.error == "string"),
1271
- { timeoutMs: t, debugLabel: "play_uploaded_audio started" }
1272
- );
1273
- this._sendCommand({ type: "play_uploaded_audio", upload_id: e });
1274
- const i = await s;
1275
- if (typeof i.error == "string") throw new Error(i.error);
1276
- return this._activeAudioUploadId = e, i;
1277
- }
1278
- cancelAudio(e = null) {
1279
- const t = e ?? this._activeAudioUploadId;
1280
- return t ? (this._activeAudioUploadId === t && (this._activeAudioUploadId = null), this._sendCommand({ type: "cancel_audio", upload_id: t })) : !1;
1281
- }
1282
- // ─── Private ─────────────────────────────────────────────────────────
1283
- _emit(e, t) {
1284
- this.dispatchEvent(new CustomEvent(e, { detail: t }));
1285
- }
1286
- _waitForBroadcast(e, { timeoutMs: t = 5e3, debugLabel: s = "" } = {}) {
1287
- return new Promise((i, o) => {
1288
- const n = {
1289
- predicate: e,
1290
- resolve: i,
1291
- timer: setTimeout(() => {
1292
- const a = this._broadcastWaiters.indexOf(n);
1293
- a !== -1 && this._broadcastWaiters.splice(a, 1), o(new Error(`broadcast timeout (${t} ms): ${s}`));
1294
- }, t)
1295
- };
1296
- this._broadcastWaiters.push(n);
1297
- });
1298
- }
1299
- async _awaitDataChannelDrain() {
1300
- for (; this._dc && this._dc.bufferedAmount > z; )
1301
- if (await new Promise((e) => setTimeout(e, 30)), !this._dc || this._dc.readyState !== "open")
1302
- throw new Error("data channel closed mid-upload");
1303
- }
1304
- async _sendToServer(e) {
1305
- if (!this._token) throw new Error("No token — authenticate() first");
1306
- try {
1307
- const t = await fetch(`${this._signalingUrl}/send`, {
1308
- method: "POST",
1309
- headers: {
1310
- "Content-Type": "application/json",
1311
- Authorization: `Bearer ${this._token}`
1312
- },
1313
- body: JSON.stringify(e)
1314
- });
1315
- if (!t.ok) {
1316
- let s = "";
1317
- try {
1318
- s = await t.text();
1319
- } catch {
1320
- }
1321
- return console.warn(
1322
- `[reachy-mini] /send rejected (${t.status}) for type=${e?.type}; body=${s || "<empty>"}`
1323
- ), null;
1324
- }
1325
- return await t.json();
1326
- } catch (t) {
1327
- return console.error("Send error:", t), null;
1328
- }
1329
- }
1330
- _sendCommand(e) {
1331
- return !this._dc || this._dc.readyState !== "open" ? !1 : (this._dc.send(JSON.stringify(e)), !0);
1332
- }
1333
- /**
1334
- * Call a JSON-RPC method on the robot/app over the DataChannel and await
1335
- * its result. This is the one way to drive an on-robot app (start/stop it
1336
- * via `apps.*`, or drive a running app via its own namespace, e.g.
1337
- * `conversation.say`). Rejects on the JSON-RPC error, a closed channel, or
1338
- * timeout.
1339
- */
1340
- rpcCall(e, t = {}, s = {}) {
1341
- const i = s.timeoutMs ?? 2e4, o = `rpc-${++this._rpcCounter}`;
1342
- return this._sendCommand({ jsonrpc: "2.0", id: o, method: e, params: t }) ? new Promise((n, a) => {
1343
- const c = setTimeout(() => {
1344
- this._pendingRpc.delete(o), a(new Error(`rpcCall(${e}) timed out after ${i}ms`));
1345
- }, i);
1346
- this._pendingRpc.set(o, {
1347
- resolve: (l) => n(l),
1348
- reject: a,
1349
- timer: c
1350
- });
1351
- }) : Promise.reject(new Error(`rpcCall(${e}): data channel not open`));
1352
- }
1353
- /**
1354
- * Subscribe to a JSON-RPC notification (one-way event) pushed by the
1355
- * robot/app, e.g. `conversation.turn`. Returns an unsubscribe function.
1356
- */
1357
- onNotification(e, t) {
1358
- let s = this._rpcListeners.get(e);
1359
- return s || (s = /* @__PURE__ */ new Set(), this._rpcListeners.set(e, s)), s.add(t), () => {
1360
- this._rpcListeners.get(e)?.delete(t);
1361
- };
1362
- }
1363
- _rejectPendingRpc(e) {
1364
- for (const t of this._pendingRpc.values())
1365
- clearTimeout(t.timer), t.reject(e);
1366
- this._pendingRpc.clear();
1367
- }
1368
- _handleRpcMessage(e) {
1369
- if ("id" in e && e.id != null && ("result" in e || "error" in e)) {
1370
- const t = this._pendingRpc.get(e.id);
1371
- if (!t) return;
1372
- if (this._pendingRpc.delete(e.id), clearTimeout(t.timer), "error" in e && e.error) {
1373
- const s = e.error, i = new Error(s.message ?? "rpc error");
1374
- i.reason = s.data?.reason, t.reject(i);
1375
- } else
1376
- t.resolve(e.result);
1377
- return;
1378
- }
1379
- if (typeof e.method == "string") {
1380
- const t = e.params ?? {};
1381
- for (const s of this._rpcListeners.get(e.method) ?? [])
1382
- try {
1383
- s(t);
1384
- } catch (i) {
1385
- console.error(`onNotification(${e.method}) threw:`, i);
1386
- }
1387
- }
1388
- }
1389
- _checkSessionReady() {
1390
- this._iceConnected && this._dcOpen && this._sessionResolve && (this._state = "streaming", this.requestState(), this._stateRefreshInterval = setInterval(() => this.requestState(), 500), this._emit("streaming", { sessionId: this._sessionId, robotId: this._selectedRobotId }), this._sessionResolve(), this._sessionResolve = null, this._sessionReject = null);
1391
- }
1392
- async _handleSignalingMessage(e) {
1393
- switch (e.type) {
1394
- case "welcome":
1395
- break;
1396
- case "list":
1397
- this._robots = e.producers || [], this._emit("robotsChanged", { robots: this._robots }), this._maybeAutoStart();
1398
- break;
1399
- case "peerStatusChanged": {
1400
- const t = await this._sendToServer({ type: "list" });
1401
- t?.producers && (this._robots = t.producers, this._emit("robotsChanged", { robots: this._robots }), this._maybeAutoStart());
1402
- break;
1403
- }
1404
- case "sessionStarted":
1405
- this._sessionId = e.sessionId ?? null;
1406
- break;
1407
- case "sessionRejected":
1408
- this._failSessionRejected(e);
1409
- break;
1410
- case "endSession":
1411
- this._handleEndSession(e);
1412
- break;
1413
- case "peer":
1414
- this._handlePeerMessage(e);
1415
- break;
1416
- }
1417
- }
1418
- _handleEndSession(e) {
1419
- const t = e.reason, s = t === "robot_busy_local_app" ? "Robot is busy: a local Python app is running" : t === "local_app_started" ? "Disconnected: a local Python app started on the robot" : t === "robot_busy_local" ? "Robot is busy: another session is already active" : null;
1420
- if (this._sessionReject) {
1421
- const i = new Error(
1422
- s || `Session ended before it could start: ${t || "unknown reason"}`
1423
- );
1424
- i.reason = t ?? null, this._emit("sessionRejected", { reason: t, activeApp: null }), this._clearIceGrace(), this._uninstallNetworkListeners(), this._pc && (this._pc.close(), this._pc = null), this._micStream && (this._micStream.getTracks().forEach((n) => n.stop()), this._micStream = null), this._iceConnected = !1, this._dcOpen = !1, this._micMuted = !0, this._micSupported = !1;
1425
- const o = this._sessionReject;
1426
- this._sessionResolve = null, this._sessionReject = null, o(i);
1427
- return;
1428
- }
1429
- this._state === "streaming" && (this._emit("sessionStopped", {
1430
- reason: t || "remote_end",
1431
- message: s
1432
- }), this.stopSession().catch(() => {
1433
- }));
1434
- }
1435
- async _handlePeerMessage(e) {
1436
- if (this._pc)
1437
- try {
1438
- if (e.sdp) {
1439
- const t = e.sdp;
1440
- if (t.type === "offer") {
1441
- const i = W(t.sdp);
1442
- if (this._micSupported = i, this._emit("micSupported", { supported: i }), i && this._micStream)
1443
- for (const n of this._micStream.getAudioTracks())
1444
- this._pc.addTrack(n, this._micStream);
1445
- await this._pc.setRemoteDescription(new RTCSessionDescription(t));
1446
- const o = await this._pc.createAnswer();
1447
- await this._pc.setLocalDescription(o), await this._sendToServer({
1448
- type: "peer",
1449
- sessionId: this._sessionId,
1450
- sdp: { type: "answer", sdp: o.sdp }
1451
- });
1452
- } else
1453
- await this._pc.setRemoteDescription(new RTCSessionDescription(t));
1454
- const s = this._pendingRemoteIce;
1455
- if (s && s.length) {
1456
- this._pendingRemoteIce = [];
1457
- for (const i of s)
1458
- try {
1459
- await this._pc.addIceCandidate(new RTCIceCandidate(i));
1460
- } catch (o) {
1461
- console.warn("[reachy-mini] buffered ICE candidate rejected:", o);
1462
- }
1463
- }
1464
- }
1465
- if (e.ice) {
1466
- if (!e.ice.candidate) return;
1467
- this._pc.remoteDescription ? await this._pc.addIceCandidate(new RTCIceCandidate(e.ice)) : (this._pendingRemoteIce || (this._pendingRemoteIce = []), this._pendingRemoteIce.push(e.ice));
1468
- }
1469
- } catch (t) {
1470
- console.error("WebRTC error:", t), this._emit("error", { source: "webrtc", error: t });
1471
- }
1472
- }
1473
- _handleRobotMessage(e) {
1474
- if (e.jsonrpc === "2.0") {
1475
- this._handleRpcMessage(e);
1476
- return;
1477
- }
1478
- if ("version" in e && this._versionResolve) {
1479
- this._versionResolve(e.version), this._versionResolve = null;
1480
- return;
1481
- }
1482
- if ("hardware_id" in e && this._hardwareIdResolve) {
1483
- this._hardwareIdResolve(e.hardware_id), this._hardwareIdResolve = null;
1484
- return;
1485
- }
1486
- if (e.command === "get_volume" || e.command === "set_volume") {
1487
- this._volumeResolve && (this._volumeResolve(e.status === "error" ? null : e.volume), this._volumeResolve = null);
1488
- return;
1489
- }
1490
- if (e.command === "get_microphone_volume" || e.command === "set_microphone_volume") {
1491
- this._micVolumeResolve && (this._micVolumeResolve(e.status === "error" ? null : e.volume), this._micVolumeResolve = null);
1492
- return;
1493
- }
1494
- if (e.command === "apply_audio_config") {
1495
- this._applyAudioConfigResolve && (this._applyAudioConfigResolve(e.error ? !1 : !!e.applied), this._applyAudioConfigResolve = null);
1496
- return;
1497
- }
1498
- if (e.command === "read_audio_parameter") {
1499
- this._readAudioParameterResolve && (this._readAudioParameterResolve(
1500
- e.error ? null : e.values ?? null
1501
- ), this._readAudioParameterResolve = null);
1502
- return;
1503
- }
1504
- if (e.command === "get_tracked_face") {
1505
- this._trackedFaceResolve && (this._trackedFaceResolve(
1506
- e.face_target ?? null
1507
- ), this._trackedFaceResolve = null);
1508
- return;
1509
- }
1510
- if ((e.command === "wake_up" || e.command === "goto_sleep") && this._pendingMotionCompletions && this._pendingMotionCompletions[e.command]) {
1511
- const t = this._pendingMotionCompletions[e.command];
1512
- if (e.completed === !0 && t.length > 0) {
1513
- const s = t.shift();
1514
- clearTimeout(s.timer), s.resolve();
1515
- return;
1516
- }
1517
- if (e.error && t.length > 0) {
1518
- const s = t.shift();
1519
- clearTimeout(s.timer), s.reject(new Error(`${e.command}: ${e.error}`));
1520
- return;
1521
- }
1522
- }
1523
- if (e.type === "log_line") {
1524
- for (const t of this._logSubscribers)
1525
- try {
1526
- t.onLine({ timestamp: e.timestamp, line: e.line });
1527
- } catch (s) {
1528
- console.error("subscribeLogs onLine threw:", s);
1529
- }
1530
- return;
1531
- }
1532
- if (e.type === "log_stream_error") {
1533
- for (const t of this._logSubscribers)
1534
- if (typeof t.onError == "function")
1535
- try {
1536
- t.onError(e.error);
1537
- } catch (s) {
1538
- console.error("subscribeLogs onError threw:", s);
1539
- }
1540
- return;
1541
- }
1542
- if (e.command === "start_update") {
1543
- if (typeof e.error == "string") {
1544
- const t = { status: "failed", error: e.error };
1545
- for (const s of this._updateProgressSubscribers)
1546
- try {
1547
- s(t);
1548
- } catch (i) {
1549
- console.error("startDaemonUpdate onProgress threw:", i);
1550
- }
1551
- }
1552
- return;
1553
- }
1554
- if (e.type === "update_progress") {
1555
- const t = {
1556
- status: e.status,
1557
- line: typeof e.line == "string" ? e.line : void 0,
1558
- error: typeof e.error == "string" ? e.error : void 0
1559
- };
1560
- for (const s of this._updateProgressSubscribers)
1561
- try {
1562
- s(t);
1563
- } catch (i) {
1564
- console.error("startDaemonUpdate onProgress threw:", i);
1565
- }
1566
- return;
1567
- }
1568
- if (e.state) {
1569
- const t = e.state;
1570
- t.head_pose && (this._robotState.head = t.head_pose.flat()), t.antennas && (this._robotState.antennas = [t.antennas[0], t.antennas[1]]), typeof t.body_yaw == "number" && (this._robotState.body_yaw = t.body_yaw), t.motor_mode && (this._robotState.motor_mode = t.motor_mode), typeof t.is_move_running == "boolean" && (this._robotState.is_move_running = t.is_move_running), t.face_target && (this._robotState.face_target = t.face_target), this._emit("state", { ...this._robotState });
1571
- }
1572
- if (e.error && this._emit("error", { source: "robot", error: e.error }), this._broadcastWaiters.length > 0)
1573
- for (let t = this._broadcastWaiters.length - 1; t >= 0; t--) {
1574
- const s = this._broadcastWaiters[t];
1575
- if (s.predicate(e)) {
1576
- this._broadcastWaiters.splice(t, 1), clearTimeout(s.timer), s.resolve(e);
1577
- return;
1578
- }
1579
- }
1580
- }
1581
- /** Snap video playback to live edge if buffered lag exceeds 0.5 s. */
1582
- _startLatencyMonitor(e) {
1583
- this._latencyMonitorId && clearInterval(this._latencyMonitorId), this._latencyMonitorId = setInterval(() => {
1584
- if (!e.srcObject || e.paused) return;
1585
- const t = e.buffered;
1586
- if (t.length > 0) {
1587
- const s = t.end(t.length - 1), i = s - e.currentTime;
1588
- i > 0.5 && (console.log(`Latency correction: was ${i.toFixed(2)}s behind`), e.currentTime = s - 0.1);
1589
- }
1590
- }, 2e3);
1591
- }
1592
- }
1593
- export {
1594
- Q as R
1595
- };