@pollen-robotics/reachy-mini-sdk 1.9.0 → 1.10.0-dev.0.main.50ba239

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 (126) 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 +177 -88
  39. package/dist/lib/reachy-mini.d.ts.map +1 -1
  40. package/dist/lib/reachy-mini.js +603 -566
  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 +237 -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-CCkHVzTj.js +2209 -0
  77. package/host/dist/chunks/index-CS_PVKrS.js +703 -0
  78. package/host/dist/chunks/{mountHost-DjE5zE9R.js → mountHost-L2vGsxTL.js} +24995 -27491
  79. package/host/dist/components/ConnectingView.d.ts.map +1 -1
  80. package/host/dist/components/DaemonUpdateGate.d.ts +65 -0
  81. package/host/dist/components/DaemonUpdateGate.d.ts.map +1 -0
  82. package/host/dist/components/ErrorView.d.ts.map +1 -1
  83. package/host/dist/components/HostErrorBoundary.d.ts +30 -0
  84. package/host/dist/components/HostErrorBoundary.d.ts.map +1 -0
  85. package/host/dist/components/LeavingView.d.ts.map +1 -1
  86. package/host/dist/components/PickerView.d.ts +16 -14
  87. package/host/dist/components/PickerView.d.ts.map +1 -1
  88. package/host/dist/components/PostOAuthSplash.d.ts +14 -9
  89. package/host/dist/components/PostOAuthSplash.d.ts.map +1 -1
  90. package/host/dist/components/ReachyHostShell.d.ts.map +1 -1
  91. package/host/dist/components/SignInView.d.ts.map +1 -1
  92. package/host/dist/components/TopBar.d.ts +5 -6
  93. package/host/dist/components/TopBar.d.ts.map +1 -1
  94. package/host/dist/components/WelcomeBackOverlay.d.ts.map +1 -1
  95. package/host/dist/embed/index.d.ts +63 -2
  96. package/host/dist/embed/index.d.ts.map +1 -1
  97. package/host/dist/entry/auto.js +2 -2
  98. package/host/dist/entry/embed.d.ts +5 -4
  99. package/host/dist/entry/embed.d.ts.map +1 -1
  100. package/host/dist/entry/embed.js +5 -4
  101. package/host/dist/hooks/useHostBridge.d.ts +35 -1
  102. package/host/dist/hooks/useHostBridge.d.ts.map +1 -1
  103. package/host/dist/hooks/useLatestDaemonVersion.d.ts +7 -0
  104. package/host/dist/hooks/useLatestDaemonVersion.d.ts.map +1 -0
  105. package/host/dist/hooks/useOAuth.d.ts +18 -0
  106. package/host/dist/hooks/useOAuth.d.ts.map +1 -1
  107. package/host/dist/hooks/useRobots.d.ts +0 -2
  108. package/host/dist/hooks/useRobots.d.ts.map +1 -1
  109. package/host/dist/index.d.ts +2 -2
  110. package/host/dist/index.d.ts.map +1 -1
  111. package/host/dist/index.js +11 -10
  112. package/host/dist/lib/daemonRelease.d.ts +84 -0
  113. package/host/dist/lib/daemonRelease.d.ts.map +1 -0
  114. package/host/dist/lib/protocol.d.ts +141 -7
  115. package/host/dist/lib/protocol.d.ts.map +1 -1
  116. package/host/dist/lib/rebootWatch.d.ts +51 -0
  117. package/host/dist/lib/rebootWatch.d.ts.map +1 -0
  118. package/host/dist/lib/sdk-types.d.ts +1 -1
  119. package/host/dist/lib/sdk-types.d.ts.map +1 -1
  120. package/host/dist/lib/settings.d.ts +16 -5
  121. package/host/dist/lib/settings.d.ts.map +1 -1
  122. package/package.json +11 -4
  123. package/host/dist/chunks/index-lMs2sCXG.js +0 -439
  124. package/host/dist/chunks/reachy-mini-CdsdQLIU.js +0 -1523
  125. package/host/dist/lib/centralListener.d.ts +0 -73
  126. package/host/dist/lib/centralListener.d.ts.map +0 -1
@@ -0,0 +1,703 @@
1
+ import { decodeCredsFromHash as D, PROTOCOL_VERSION as y, PROTOCOL_SOURCE as b, isProtocolMessage as R } from "../lib/protocol.js";
2
+ import { f as $, c as x } from "./centralRest-CCkHVzTj.js";
3
+ const C = 8e3, U = 2e3, B = 900 * 1e3, J = 2500, K = 6e3, G = 6500, z = 1e3;
4
+ async function Y(e) {
5
+ await e.ensureAwake();
6
+ }
7
+ async function Z(e) {
8
+ e.robotState.motor_mode !== "disabled" && await new Promise((r) => {
9
+ let n = !1, s = 0, o = 0;
10
+ const t = () => {
11
+ n || (n = !0, e.removeEventListener("state", i), window.clearInterval(s), window.clearTimeout(o), r());
12
+ };
13
+ function i() {
14
+ e.robotState.motor_mode === "disabled" && t();
15
+ }
16
+ e.addEventListener("state", i), o = window.setTimeout(t, z), s = window.setInterval(() => e.requestState(), 120), e.requestState();
17
+ });
18
+ }
19
+ let O = null, N = null, I = "*";
20
+ function V() {
21
+ try {
22
+ if (typeof document < "u" && document.referrer)
23
+ return new URL(document.referrer).origin;
24
+ } catch {
25
+ }
26
+ return "*";
27
+ }
28
+ async function me(e = {}) {
29
+ return O || (O = Q(e)), await O;
30
+ }
31
+ async function we(e = {}) {
32
+ return N || (N = X(e)), await N;
33
+ }
34
+ async function Q(e) {
35
+ I = V();
36
+ const r = e.expectedOrigin ?? I, n = D(window.location.hash);
37
+ if (j(), !n)
38
+ throw new Error(
39
+ "[reachy-mini-sdk/host/embed] no creds bundle found in URL hash. Was the embed mounted directly without ?embedded=1#creds=...?"
40
+ );
41
+ if (!await ne(C))
42
+ throw new Error(
43
+ `[reachy-mini-sdk/host/embed] window.ReachyMini did not become available within ${C}ms - check the SDK CDN tag.`
44
+ );
45
+ const o = n.userName ?? n.username ?? null;
46
+ n.hfToken && o && te(n.hfToken, o);
47
+ const t = new window.ReachyMini({
48
+ appName: n.appName,
49
+ signalingUrl: n.signalingUrl,
50
+ ...e.sdkOptions
51
+ });
52
+ q = typeof t.sdkVersion == "string" ? t.sdkVersion : null;
53
+ const i = F(r, t);
54
+ S({
55
+ source: b,
56
+ type: "embed:ready",
57
+ version: y
58
+ }), i.start();
59
+ const a = await i.awaitHostInit(U, n);
60
+ v("connecting", "link"), h("boot:link:start", { robotPeerId: a.robotPeerId }), await t.authenticate(), h("boot:authenticate:ok", { state: t.state }), await t.connect(), h("boot:connect:ok", {
61
+ state: t.state,
62
+ robots: (t.robots ?? []).length
63
+ });
64
+ const d = await ee(a, n);
65
+ v("connecting", "session"), h("boot:session:start", {
66
+ robotPeerId: d,
67
+ handedPeerId: a.robotPeerId,
68
+ reresolved: d !== a.robotPeerId
69
+ }), fe(t);
70
+ try {
71
+ await t.startSession(d), h("boot:session:ok");
72
+ } catch (c) {
73
+ throw h("boot:session:error", {
74
+ message: c?.message ?? String(c)
75
+ }), c;
76
+ }
77
+ return P = await oe(t), h("boot:daemon-version", { daemonVersion: P }), v("connecting", "wake"), h("boot:wake:start"), await Y(t), h("boot:wake:ok", { awake: t.isAwake() }), i.attachPageHide(t), v("live", null), ue(t), ce(t), i.buildHandle(t, a);
78
+ }
79
+ async function X(e) {
80
+ I = V();
81
+ const r = e.expectedOrigin ?? I, n = D(window.location.hash);
82
+ if (j(), !n)
83
+ throw new Error(
84
+ "[reachy-mini-sdk/host/embed] no creds bundle found in URL hash. Was the embed mounted directly without ?embedded=1#creds=...?"
85
+ );
86
+ const s = F(r, null);
87
+ S({
88
+ source: b,
89
+ type: "embed:ready",
90
+ version: y
91
+ }), s.start();
92
+ const o = await s.awaitHostInit(U, n);
93
+ return s.attachPageHideCredsOnly(), v("live", null), s.buildCredsOnlyHandle(o, n);
94
+ }
95
+ function A(e) {
96
+ return {
97
+ theme: e.theme,
98
+ config: e.config,
99
+ appName: e.appName,
100
+ hostName: e.hostName,
101
+ userName: e.userName ?? null,
102
+ robotPeerId: e.robotPeerId,
103
+ robotHardwareId: e.robotHardwareId ?? null
104
+ };
105
+ }
106
+ function H(e) {
107
+ return {
108
+ theme: e.theme,
109
+ config: e.config,
110
+ appName: e.appName,
111
+ hostName: e.hostName,
112
+ userName: e.userName ?? null,
113
+ robotPeerId: e.robotPeerId,
114
+ robotHardwareId: e.robotHardwareId ?? null
115
+ };
116
+ }
117
+ async function ee(e, r) {
118
+ const n = e.robotHardwareId, s = r.hfToken;
119
+ if (!n || !s) return e.robotPeerId;
120
+ try {
121
+ const o = await $({
122
+ signalingUrl: r.signalingUrl,
123
+ hfToken: s
124
+ });
125
+ return o.ok ? o.robots.find((i) => i.hardwareId === n)?.id ?? e.robotPeerId : e.robotPeerId;
126
+ } catch {
127
+ return e.robotPeerId;
128
+ }
129
+ }
130
+ function F(e, r) {
131
+ const n = /* @__PURE__ */ new Set(), s = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set();
132
+ let t = null, i = !1, a = !1, d = !1, c = null;
133
+ function m(l) {
134
+ switch (l.type) {
135
+ case "host:init": {
136
+ t = H(l), s.forEach((f) => f(t.theme)), o.forEach((f) => f(t.config));
137
+ break;
138
+ }
139
+ case "host:theme-changed": {
140
+ t && (t.theme = l.theme), s.forEach((f) => f(l.theme));
141
+ break;
142
+ }
143
+ case "host:config-changed": {
144
+ t && (t.config = l.config), o.forEach((f) => f(l.config));
145
+ break;
146
+ }
147
+ case "host:leaving": {
148
+ T();
149
+ break;
150
+ }
151
+ case "host:start-update": {
152
+ r && re(r, l.preRelease === !0);
153
+ break;
154
+ }
155
+ case "host:cancel-update": {
156
+ E?.();
157
+ break;
158
+ }
159
+ }
160
+ }
161
+ function p() {
162
+ i || (i = !0, n.forEach((l) => {
163
+ try {
164
+ l();
165
+ } catch (f) {
166
+ L.warn("onLeave threw", f);
167
+ }
168
+ }));
169
+ }
170
+ async function T() {
171
+ if (!a) {
172
+ if (a = !0, h("leave:start"), p(), r) {
173
+ const l = Date.now();
174
+ let f = "ok";
175
+ try {
176
+ f = await Promise.race([
177
+ r.gotoSleep({ timeoutMs: K }).then(() => "ok"),
178
+ new Promise(
179
+ (g) => window.setTimeout(() => g("hard-timeout"), G)
180
+ )
181
+ ]);
182
+ } catch {
183
+ f = "error";
184
+ }
185
+ h("leave:sleep", { outcome: f, ms: Date.now() - l });
186
+ const w = Date.now();
187
+ let u = !1;
188
+ try {
189
+ r.setMotorMode("disabled"), await Z(r), u = !0;
190
+ } catch {
191
+ }
192
+ h("leave:motors-disabled", {
193
+ confirmed: u,
194
+ ms: Date.now() - w
195
+ });
196
+ }
197
+ h("leave:ack"), S({
198
+ source: b,
199
+ type: "embed:left",
200
+ version: y
201
+ });
202
+ }
203
+ }
204
+ return {
205
+ start() {
206
+ d || (d = !0, c = (l) => {
207
+ e !== "*" && l.origin !== e || R(l.data) && m(l.data);
208
+ }, window.addEventListener("message", c));
209
+ },
210
+ async awaitHostInit(l, f) {
211
+ return window.parent !== window ? t || new Promise((u) => {
212
+ const g = (k) => {
213
+ if (e !== "*" && k.origin !== e || !R(k.data)) return;
214
+ const M = k.data;
215
+ M.type === "host:init" && (window.removeEventListener("message", g), window.clearTimeout(_), t = H(M), u(t));
216
+ }, _ = window.setTimeout(() => {
217
+ window.removeEventListener("message", g), t || (t = A(f)), u(t);
218
+ }, l);
219
+ window.addEventListener("message", g);
220
+ }) : (t = A(f), t);
221
+ },
222
+ attachPageHide(l) {
223
+ const f = () => {
224
+ p();
225
+ try {
226
+ l.stopSession();
227
+ } catch {
228
+ }
229
+ };
230
+ window.addEventListener("pagehide", f, { once: !0 });
231
+ },
232
+ /** Credentials-only variant — no SDK to stop; just fire onLeave. */
233
+ attachPageHideCredsOnly() {
234
+ window.addEventListener("pagehide", p, { once: !0 });
235
+ },
236
+ /**
237
+ * Build the credentials-only handle. Same lifecycle accessors as
238
+ * `buildHandle` but exposes `hfToken` / `robotPeerId` /
239
+ * `signalingUrl` (from the creds bundle) instead of a `reachy`
240
+ * SDK instance, so the embed app can forward those to its
241
+ * backend.
242
+ */
243
+ buildCredsOnlyHandle(l, f) {
244
+ if (t = l, !f.hfToken)
245
+ throw new Error(
246
+ "[reachy-mini-sdk/host/embed] creds-only handle requires hfToken; the host shell did not provide one (visitor logged out?)."
247
+ );
248
+ return {
249
+ hfToken: f.hfToken,
250
+ robotPeerId: l.robotPeerId,
251
+ signalingUrl: f.signalingUrl,
252
+ get theme() {
253
+ return t.theme;
254
+ },
255
+ get config() {
256
+ return t.config;
257
+ },
258
+ get appName() {
259
+ return t.appName;
260
+ },
261
+ get hostName() {
262
+ return t.hostName;
263
+ },
264
+ get userName() {
265
+ return t.userName;
266
+ },
267
+ onLeave(w) {
268
+ return n.add(w), () => n.delete(w);
269
+ },
270
+ onThemeChange(w) {
271
+ return s.add(w), () => s.delete(w);
272
+ },
273
+ onConfigChange(w) {
274
+ const u = (g) => w(g);
275
+ return o.add(u), () => o.delete(u);
276
+ },
277
+ setAppState(w) {
278
+ v(
279
+ w.phase,
280
+ w.connectingStep ?? null,
281
+ w.message ?? null
282
+ );
283
+ },
284
+ requestLeave() {
285
+ S({
286
+ source: b,
287
+ type: "embed:request-leave",
288
+ version: y
289
+ });
290
+ },
291
+ reportError(w, u) {
292
+ S({
293
+ source: b,
294
+ type: "embed:error",
295
+ version: y,
296
+ message: w,
297
+ fatal: u?.fatal === !0,
298
+ detail: u?.detail
299
+ });
300
+ }
301
+ };
302
+ },
303
+ buildHandle(l, f) {
304
+ t = f;
305
+ const w = le(l);
306
+ return {
307
+ reachy: l,
308
+ media: w,
309
+ get theme() {
310
+ return t.theme;
311
+ },
312
+ get config() {
313
+ return t.config;
314
+ },
315
+ get appName() {
316
+ return t.appName;
317
+ },
318
+ get hostName() {
319
+ return t.hostName;
320
+ },
321
+ get userName() {
322
+ return t.userName;
323
+ },
324
+ onLeave(u) {
325
+ return n.add(u), () => n.delete(u);
326
+ },
327
+ onThemeChange(u) {
328
+ return s.add(u), () => s.delete(u);
329
+ },
330
+ onConfigChange(u) {
331
+ const g = (_) => u(_);
332
+ return o.add(g), () => o.delete(g);
333
+ },
334
+ setAppState(u) {
335
+ v(
336
+ u.phase,
337
+ u.connectingStep ?? null,
338
+ u.message ?? null
339
+ );
340
+ },
341
+ requestLeave() {
342
+ S({
343
+ source: b,
344
+ type: "embed:request-leave",
345
+ version: y
346
+ });
347
+ },
348
+ reportError(u, g) {
349
+ S({
350
+ source: b,
351
+ type: "embed:error",
352
+ version: y,
353
+ message: u,
354
+ fatal: g?.fatal === !0,
355
+ detail: g?.detail
356
+ });
357
+ }
358
+ };
359
+ }
360
+ };
361
+ }
362
+ function j() {
363
+ try {
364
+ const e = window.location.pathname + window.location.search;
365
+ history.replaceState(history.state, document.title, e);
366
+ } catch {
367
+ }
368
+ }
369
+ function te(e, r) {
370
+ try {
371
+ sessionStorage.setItem("hf_token", e), sessionStorage.setItem("hf_username", r), sessionStorage.setItem(
372
+ "hf_token_expires",
373
+ new Date(Date.now() + B).toISOString()
374
+ );
375
+ } catch {
376
+ }
377
+ }
378
+ function ne(e) {
379
+ return new Promise((r) => {
380
+ if (typeof window > "u") {
381
+ r(!1);
382
+ return;
383
+ }
384
+ if (window.ReachyMini) {
385
+ r(!0);
386
+ return;
387
+ }
388
+ let n = !1;
389
+ const s = () => {
390
+ n || (n = !0, window.removeEventListener("reachymini:ready", s), window.clearTimeout(o), r(!!window.ReachyMini));
391
+ }, o = window.setTimeout(() => {
392
+ n || (n = !0, window.removeEventListener("reachymini:ready", s), r(!1));
393
+ }, e);
394
+ window.addEventListener("reachymini:ready", s);
395
+ });
396
+ }
397
+ function S(e) {
398
+ if (!(typeof window > "u"))
399
+ try {
400
+ window.parent.postMessage(e, I);
401
+ } catch (r) {
402
+ L.warn("postMessage to host failed", r);
403
+ }
404
+ }
405
+ let E = null;
406
+ function re(e, r) {
407
+ const n = (a) => {
408
+ S({
409
+ source: b,
410
+ type: "embed:update-progress",
411
+ version: y,
412
+ ...a
413
+ });
414
+ }, s = (a) => {
415
+ const d = e.setAutoReconnect;
416
+ if (typeof d == "function")
417
+ try {
418
+ d.call(e, a);
419
+ } catch {
420
+ }
421
+ };
422
+ s(!1);
423
+ const o = () => {
424
+ E = null, e.removeEventListener("sessionStopped", o), n({ status: "rebooting" });
425
+ }, t = () => {
426
+ E = null, e.removeEventListener("sessionStopped", o), s(!0);
427
+ };
428
+ let i;
429
+ try {
430
+ i = e.startDaemonUpdate({
431
+ preRelease: r,
432
+ onProgress: (a) => {
433
+ a.status === "failed" && t(), n({
434
+ status: a.status,
435
+ line: a.line ?? null,
436
+ error: a.error ?? null
437
+ });
438
+ }
439
+ });
440
+ } catch (a) {
441
+ t(), n({ status: "failed", error: a?.message ?? String(a) });
442
+ return;
443
+ }
444
+ if (!i) {
445
+ t(), n({ status: "failed", error: "Data channel not open" });
446
+ return;
447
+ }
448
+ e.addEventListener("sessionStopped", o), E = t;
449
+ }
450
+ let P = null, q = null;
451
+ async function oe(e) {
452
+ try {
453
+ const r = await Promise.race([
454
+ e.getVersion(),
455
+ new Promise(
456
+ (n) => setTimeout(() => n(null), J)
457
+ )
458
+ ]);
459
+ return typeof r == "string" && r.length > 0 ? r : null;
460
+ } catch {
461
+ return null;
462
+ }
463
+ }
464
+ function v(e, r, n = null, s = null) {
465
+ S({
466
+ source: b,
467
+ type: "embed:app-state",
468
+ version: y,
469
+ phase: e,
470
+ connectingStep: r,
471
+ message: n,
472
+ rttMs: s,
473
+ daemonVersion: P,
474
+ sdkVersion: q
475
+ });
476
+ }
477
+ const se = 1500, ae = 6;
478
+ async function ie(e) {
479
+ try {
480
+ const r = await e.getStats();
481
+ let n = null;
482
+ return r.forEach((s) => {
483
+ const o = s;
484
+ o.type !== "candidate-pair" || !(o.selected === !0 || o.nominated === !0 && o.state === "succeeded") || typeof o.currentRoundTripTime == "number" && (n = o.currentRoundTripTime * 1e3);
485
+ }), n;
486
+ } catch {
487
+ return null;
488
+ }
489
+ }
490
+ function ce(e) {
491
+ if (typeof window > "u") return;
492
+ const r = () => W(e);
493
+ if (!r()) return;
494
+ const n = [];
495
+ let s = !1;
496
+ const o = async () => {
497
+ if (s) return;
498
+ const a = r();
499
+ if (!a || a.connectionState === "closed") return;
500
+ const d = await ie(a);
501
+ d !== null && (n.push(d), n.length > ae && n.shift(), v("live", null, null, Math.min(...n)));
502
+ }, t = window.setInterval(() => {
503
+ o();
504
+ }, se);
505
+ window.setTimeout(() => {
506
+ o();
507
+ }, 600);
508
+ const i = () => {
509
+ s || (s = !0, window.clearInterval(t));
510
+ };
511
+ e.addEventListener("sessionStopped", i), window.addEventListener("pagehide", i, { once: !0 });
512
+ }
513
+ function W(e) {
514
+ const r = e;
515
+ return r.peerConnection ?? r._pc ?? null;
516
+ }
517
+ const L = x("embed");
518
+ function de(e) {
519
+ return e.startsWith("boot:") || e.startsWith("leave:");
520
+ }
521
+ function h(e, r = {}) {
522
+ if (!(typeof window > "u")) {
523
+ try {
524
+ window.parent.postMessage(
525
+ {
526
+ source: b,
527
+ type: "embed:debug",
528
+ version: y,
529
+ tag: e,
530
+ payload: r
531
+ },
532
+ I
533
+ );
534
+ } catch {
535
+ }
536
+ try {
537
+ let n = "";
538
+ try {
539
+ n = JSON.stringify(r);
540
+ } catch {
541
+ n = "<unserializable>";
542
+ }
543
+ const s = `${e} ${n}`;
544
+ de(e) ? L.info(s) : L.debug(s);
545
+ } catch {
546
+ }
547
+ }
548
+ }
549
+ function le(e) {
550
+ let r = null;
551
+ const n = e, s = () => {
552
+ const i = W(e);
553
+ if (!i) return null;
554
+ const a = i.getReceivers().map((d) => d.track).filter(
555
+ (d) => d !== null && d.kind !== "" && d.readyState === "live"
556
+ );
557
+ return a.length === 0 ? null : new MediaStream(a);
558
+ }, o = () => r || (r = s(), r && h("media:cache:init", {
559
+ videoTracks: r.getVideoTracks().length,
560
+ audioTracks: r.getAudioTracks().length
561
+ }), r), t = () => {
562
+ r && (r = null, h("media:cache:clear"));
563
+ };
564
+ return e.addEventListener("sessionStopped", t), e.addEventListener("sessionReconnecting", t), {
565
+ attachVideo(i) {
566
+ const a = e.attachVideo(i), d = o();
567
+ if (d && i.srcObject !== d)
568
+ try {
569
+ i.srcObject = d, i.play().catch(() => {
570
+ }), h("media:attach:replay", {
571
+ videoTracks: d.getVideoTracks().length,
572
+ audioTracks: d.getAudioTracks().length
573
+ });
574
+ } catch (c) {
575
+ h("media:attach:replay:error", {
576
+ message: c?.message ?? String(c)
577
+ });
578
+ }
579
+ return a;
580
+ },
581
+ get robotStream() {
582
+ return o();
583
+ },
584
+ get micStream() {
585
+ return n._micStream;
586
+ }
587
+ };
588
+ }
589
+ function ue(e) {
590
+ e.addEventListener("sessionReconnecting", (r) => {
591
+ const n = r.detail, s = Number(n?.attempt ?? 1), o = Number(n?.maxAttempts ?? 1);
592
+ v(
593
+ "connecting",
594
+ "session",
595
+ s > 1 ? `Reconnecting to the robot (attempt ${s}/${o})` : "Connection lost — reconnecting to the robot"
596
+ );
597
+ }), e.addEventListener("sessionReconnected", () => {
598
+ v("live", null);
599
+ }), e.addEventListener("sessionStopped", (r) => {
600
+ const n = r.detail;
601
+ n?.reason === "reconnect_failed" && S({
602
+ source: b,
603
+ type: "embed:error",
604
+ version: y,
605
+ message: "Lost the connection to the robot and could not reconnect.",
606
+ fatal: !0,
607
+ detail: typeof n?.message == "string" ? n.message : void 0
608
+ });
609
+ });
610
+ }
611
+ function fe(e) {
612
+ const r = [
613
+ "connected",
614
+ "disconnected",
615
+ "streaming",
616
+ "sessionStopped",
617
+ "sessionRejected",
618
+ "sessionReconnecting",
619
+ "sessionReconnected",
620
+ "robotsChanged",
621
+ "error",
622
+ "state",
623
+ "log",
624
+ "message"
625
+ ];
626
+ for (const o of r)
627
+ try {
628
+ e.addEventListener(o, (t) => {
629
+ let i = {};
630
+ const a = t;
631
+ if (a && typeof a == "object" && "detail" in a)
632
+ try {
633
+ i = JSON.parse(JSON.stringify(a.detail ?? null));
634
+ } catch {
635
+ i = { _unserializable: !0 };
636
+ }
637
+ h(`sdk:${o}`, i);
638
+ });
639
+ } catch {
640
+ }
641
+ try {
642
+ const o = e, t = o._handleSignalingMessage;
643
+ if (typeof t == "function") {
644
+ const i = o._sendToServer;
645
+ typeof i == "function" && (o._sendToServer = async function(d) {
646
+ const c = d, m = { type: c?.type ?? "?" };
647
+ if (c && "peerId" in c && (m.peerId = String(c.peerId)), c && "sessionId" in c && (m.sessionId = String(c.sessionId)), c && "sdp" in c) {
648
+ const p = c.sdp;
649
+ m.sdpType = p?.type ?? "?", m.sdpLen = p?.sdp?.length ?? 0;
650
+ }
651
+ if (c && "ice" in c) {
652
+ const p = c.ice;
653
+ m.iceCand = (p?.candidate ?? "").slice(0, 60) || "<end-of-candidates>";
654
+ }
655
+ h("sdk:send", m);
656
+ try {
657
+ const p = await i.call(this, d), T = p;
658
+ return h("sdk:send:res", {
659
+ inFor: m.type,
660
+ resType: T?.type ?? null,
661
+ keys: T ? Object.keys(T) : []
662
+ }), p;
663
+ } catch (p) {
664
+ throw h("sdk:send:err", {
665
+ inFor: m.type,
666
+ msg: p?.message ?? String(p)
667
+ }), p;
668
+ }
669
+ }), o._handleSignalingMessage = function(d) {
670
+ const c = d, m = { type: c?.type ?? "?" };
671
+ if ("sessionId" in c && (m.sessionId = String(c.sessionId)), "peerId" in c && (m.peerId = String(c.peerId)), "sdp" in c) {
672
+ const p = c.sdp;
673
+ m.sdpType = p?.type ?? "?", m.sdpLen = p?.sdp?.length ?? 0;
674
+ }
675
+ if ("ice" in c) {
676
+ const p = c.ice;
677
+ m.iceCand = (p?.candidate ?? "").slice(0, 60) || "<end-of-candidates>";
678
+ }
679
+ return "reason" in c && (m.reason = String(c.reason)), h("sdk:sse", m), t.call(this, d);
680
+ };
681
+ }
682
+ } catch {
683
+ }
684
+ const n = Date.now(), s = window.setInterval(() => {
685
+ const o = e, t = o._pc, i = o._dc;
686
+ h("sdk:probe", {
687
+ elapsedMs: Date.now() - n,
688
+ myPeerId: o._peerId ?? null,
689
+ state: o._state ?? null,
690
+ sseAborted: o._sseAbortController?.signal?.aborted ?? null,
691
+ pcState: t?.connectionState ?? null,
692
+ iceState: t?.iceConnectionState ?? null,
693
+ iceGather: t?.iceGatheringState ?? null,
694
+ signalingState: t?.signalingState ?? null,
695
+ dcState: i?.readyState ?? null,
696
+ sessionId: o._sessionId ?? null
697
+ }), Date.now() - n > 3e4 && window.clearInterval(s);
698
+ }, 1500);
699
+ }
700
+ export {
701
+ we as a,
702
+ me as c
703
+ };