@pollen-robotics/reachy-mini-sdk 1.10.0-dev.0.main.b50d97e → 1.10.0-dev.0.main.e7f2273

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.
@@ -23,6 +23,9 @@ export declare class ReachyMini extends EventTarget implements ReachyMiniInstanc
23
23
  private _state;
24
24
  private _robots;
25
25
  private _robotState;
26
+ private _lastPoseSeq;
27
+ private _lastPoseFrameAt;
28
+ private _poseSubRefs;
26
29
  private readonly _preselectedRobotId;
27
30
  private _token;
28
31
  private _username;
@@ -44,10 +47,15 @@ export declare class ReachyMini extends EventTarget implements ReachyMiniInstanc
44
47
  private _volumeResolve;
45
48
  private _micVolumeResolve;
46
49
  private _trackedFaceResolve;
50
+ private _robotNameResolve;
51
+ private _deleteHfTokenResolve;
47
52
  private _applyAudioConfigResolve;
48
53
  private _readAudioParameterResolve;
49
54
  private readonly _logSubscribers;
50
55
  private readonly _updateProgressSubscribers;
56
+ private _rpcCounter;
57
+ private readonly _pendingRpc;
58
+ private readonly _rpcListeners;
51
59
  private _broadcastWaiters;
52
60
  private _activeMoveUploadId;
53
61
  private _activeAudioUploadId;
@@ -235,6 +243,37 @@ export declare class ReachyMini extends EventTarget implements ReachyMiniInstanc
235
243
  setVolume(volume: number): Promise<number | null>;
236
244
  getMicrophoneVolume(): Promise<number | null>;
237
245
  setMicrophoneVolume(volume: number): Promise<number | null>;
246
+ /**
247
+ * Query the persisted robot display name. Resolves the stored name,
248
+ * `null` when none is set / the channel isn't open / the daemon predates
249
+ * the `get_robot_name` command.
250
+ */
251
+ getRobotName(): Promise<string | null>;
252
+ /**
253
+ * Set and persist the robot display name on the robot. Resolves with the
254
+ * stored (trimmed) name, or `null` on error / channel-closed. Applied live
255
+ * by the daemon (status + central relay + mDNS), so it takes effect right
256
+ * away without a restart; the persisted name also overrides --robot-name
257
+ * on the next start.
258
+ */
259
+ setRobotName(name: string): Promise<string | null>;
260
+ /**
261
+ * Sign this robot out of Hugging Face: asks the daemon to delete its
262
+ * stored HF token, which de-registers the robot from the central
263
+ * signaling relay (it disappears from its owner's robot list until it
264
+ * is set up again). Works over the WebRTC data channel, so it reaches
265
+ * the robot remotely (no LAN HTTP path required).
266
+ *
267
+ * Resolves `true` when the daemon acked success, `false` on a daemon
268
+ * error, or `null` when no ack arrives before the timeout (e.g. a
269
+ * daemon that predates the `delete_hf_token` command silently drops
270
+ * it). Rejects if the data channel isn't open. Note the sign-out
271
+ * drops the central relay, so the session may tear down right after
272
+ * the ack - callers should treat a post-call session drop as expected,
273
+ * and a successful sign-out may surface as `null` if teardown races
274
+ * ahead of the ack.
275
+ */
276
+ signOut(): Promise<boolean | null>;
238
277
  applyAudioConfig(config: AudioConfigEntry[], { verify }?: ApplyAudioConfigOptions): Promise<boolean>;
239
278
  readAudioParameter(name: string): Promise<number[] | null>;
240
279
  /**
@@ -255,6 +294,21 @@ export declare class ReachyMini extends EventTarget implements ReachyMiniInstanc
255
294
  sendRaw(data: unknown): boolean;
256
295
  subscribeLogs({ onLine, onError }: SubscribeLogsOptions): () => void;
257
296
  requestState(): boolean;
297
+ /**
298
+ * Ask the daemon to *push* the robot state (~30 Hz) over the dedicated
299
+ * unreliable/unordered `pose` data channel instead of polling get_state.
300
+ * Fires `state` events as frames arrive. No-op against an older daemon (no
301
+ * pose channel) - fall back to `requestState()` polling there.
302
+ *
303
+ * Refcounted: pair every `subscribePose()` with exactly one
304
+ * `unsubscribePose()`. Multiple consumers share a single daemon-side
305
+ * subscription; the daemon only stops pushing once the last one releases.
306
+ * If the channel isn't open yet (or the session later reconnects), the
307
+ * subscription is (re-)asserted from `_checkSessionReady`.
308
+ */
309
+ subscribePose(): boolean;
310
+ /** Release one pose-stream consumer; sends `unsubscribe_pose` on the last. */
311
+ unsubscribePose(): boolean;
258
312
  setAudioMuted(muted: boolean): void;
259
313
  setMicMuted(muted: boolean): void;
260
314
  attachVideo(videoElement: HTMLVideoElement): () => void;
@@ -270,10 +324,32 @@ export declare class ReachyMini extends EventTarget implements ReachyMiniInstanc
270
324
  private _awaitDataChannelDrain;
271
325
  private _sendToServer;
272
326
  private _sendCommand;
327
+ /**
328
+ * Call a JSON-RPC method on the robot/app over the DataChannel and await
329
+ * its result. This is the one way to drive an on-robot app (start/stop it
330
+ * via `apps.*`, or drive a running app via its own namespace, e.g.
331
+ * `conversation.say`). Rejects on the JSON-RPC error, a closed channel, or
332
+ * timeout.
333
+ */
334
+ rpcCall<T = unknown>(method: string, params?: Record<string, unknown>, opts?: {
335
+ timeoutMs?: number;
336
+ }): Promise<T>;
337
+ /**
338
+ * Subscribe to a JSON-RPC notification (one-way event) pushed by the
339
+ * robot/app, e.g. `conversation.turn`. Returns an unsubscribe function.
340
+ */
341
+ onNotification(method: string, cb: (params: Record<string, unknown>) => void): () => void;
342
+ private _rejectPendingRpc;
343
+ private _handleRpcMessage;
273
344
  private _checkSessionReady;
274
345
  private _handleSignalingMessage;
275
346
  private _handleEndSession;
276
347
  private _handlePeerMessage;
348
+ /**
349
+ * @param fromPoseStream Frame came from the pushed `pose` channel rather
350
+ * than the reliable control channel. Only those may refresh the state
351
+ * mirror while the stream is live (see the `data.state` branch).
352
+ */
277
353
  private _handleRobotMessage;
278
354
  /** Snap video playback to live edge if buffered lag exceeds 0.5 s. */
279
355
  private _startLatencyMonitor;
@@ -1 +1 @@
1
- {"version":3,"file":"reachy-mini.d.ts","sourceRoot":"","sources":["../../lib/reachy-mini.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAwBH,OAAO,KAAK,EACR,uBAAuB,EACvB,gBAAgB,EAChB,kBAAkB,EAClB,iBAAiB,EAEjB,UAAU,EACV,kBAAkB,EAClB,QAAQ,EACR,eAAe,EACf,cAAc,EACd,wBAAwB,EAExB,kBAAkB,EAClB,iBAAiB,EACjB,SAAS,EACT,UAAU,EAEV,oBAAoB,EACpB,kBAAkB,EACrB,MAAM,YAAY,CAAC;AAmBpB,kEAAkE;AAClE,MAAM,WAAW,mBAAmB;IACh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sE;IACtE,OAAO,CAAC,oBAAoB;CAe/B"}
@@ -40,6 +40,35 @@ const ICE_FAILED_GRACE_MS = 1000;
40
40
  * to be honest with the user.
41
41
  */
42
42
  const MAX_VISIBILITY_DEFER_MS = 60_000;
43
+ /**
44
+ * How long a pushed pose frame keeps the periodic `get_state` poll on hold -
45
+ * both on the way out (no request is sent) and on the way back (a reply that
46
+ * arrives anyway doesn't touch the state mirror).
47
+ *
48
+ * Poll replies carry no `seq`, so they slip past the stale-frame guard: a
49
+ * reply that crosses a fresher pushed frame would rewind the very mirror the
50
+ * stream exists to smooth. While frames flow at ~30 Hz there is nothing left
51
+ * for the poll to add, so it stands down. Keying that on frame arrival rather
52
+ * than on `_poseSubRefs` keeps the poll running against a daemon that doesn't
53
+ * know `subscribe_pose`, and brings it back on its own if the stream stalls.
54
+ * A little over one 500 ms poll period, so a couple of dropped frames don't
55
+ * flip it back and forth.
56
+ */
57
+ const POSE_STREAM_FRESH_MS = 750;
58
+ /**
59
+ * Fail-open ceiling for a single `_slotRoundtrip` request/response.
60
+ * Every slot command has a strict "one reply per request" contract, so
61
+ * a missing reply means the daemon either never got it or - crucially -
62
+ * predates that command entirely: an older daemon silently drops an
63
+ * unknown `type` and sends nothing back, which would otherwise leave the
64
+ * caller's promise pending forever (e.g. a newer SDK calling a command a
65
+ * 1.8.x daemon doesn't implement). Resolving `null` on timeout maps
66
+ * cleanly onto the "unsupported / failed" value every slot caller already
67
+ * handles. 4 s is comfortably above a WebRTC data-channel round trip on a
68
+ * congested phone link while still failing fast enough that a gated UI
69
+ * doesn't feel hung.
70
+ */
71
+ const SLOT_ROUNDTRIP_TIMEOUT_MS = 4000;
43
72
  export class ReachyMini extends EventTarget {
44
73
  // ─── Config ──────────────────────────────────────────────────────────
45
74
  _signalingUrl;
@@ -52,6 +81,19 @@ export class ReachyMini extends EventTarget {
52
81
  _state = 'disconnected';
53
82
  _robots = [];
54
83
  _robotState = {};
84
+ // Highest `seq` seen on the unordered `pose` channel. Frames that arrive
85
+ // out of order (older seq) are dropped so a late packet can't rewind the
86
+ // live mirror. `null` until the first pose frame.
87
+ _lastPoseSeq = null;
88
+ // When the last pushed pose frame was applied, used to park the periodic
89
+ // `get_state` poll while the stream is live (see POSE_STREAM_FRESH_MS).
90
+ _lastPoseFrameAt = 0;
91
+ // Local refcount of pose-stream consumers (the 3D mirror, the wizard's
92
+ // move-end watcher, ...). The daemon's subscription is a per-peer boolean
93
+ // (not refcounted), so we only send `unsubscribe_pose` once the LAST local
94
+ // consumer releases - otherwise one consumer's cleanup would kill the
95
+ // stream for the others.
96
+ _poseSubRefs = 0;
55
97
  _preselectedRobotId;
56
98
  // ─── Auth ────────────────────────────────────────────────────────────
57
99
  _token = null;
@@ -79,6 +121,8 @@ export class ReachyMini extends EventTarget {
79
121
  _volumeResolve = null;
80
122
  _micVolumeResolve = null;
81
123
  _trackedFaceResolve = null;
124
+ _robotNameResolve = null;
125
+ _deleteHfTokenResolve = null;
82
126
  // applyAudioConfig() / readAudioParameter() share the same single-slot
83
127
  // pattern as the volume helpers. Separate slots so the two can be
84
128
  // in-flight concurrently without collision.
@@ -87,6 +131,13 @@ export class ReachyMini extends EventTarget {
87
131
  // ─── Log subscribers ─────────────────────────────────────────────────
88
132
  _logSubscribers = new Set();
89
133
  _updateProgressSubscribers = new Set();
134
+ // ─── JSON-RPC app control (over the same DataChannel) ────────────────
135
+ // rpcCall() sends {jsonrpc,id,method,params} and awaits the matching
136
+ // response; onNotification() subscribes to one-way events (no id) the
137
+ // robot/app pushes (conversation.phase/turn/transcript, ...).
138
+ _rpcCounter = 0;
139
+ _pendingRpc = new Map();
140
+ _rpcListeners = new Map();
90
141
  // ─── Broadcast waiters (playMove / playUploadedAudio) ────────────────
91
142
  _broadcastWaiters = [];
92
143
  // ─── Active upload ids for no-arg cancels ────────────────────────────
@@ -540,7 +591,35 @@ export class ReachyMini extends EventTarget {
540
591
  }
541
592
  };
542
593
  this._pc.ondatachannel = (e) => {
543
- this._dc = e.channel;
594
+ const ch = e.channel;
595
+ // On `subscribe_pose` the daemon opens a second,
596
+ // unreliable/unordered channel labelled "pose" that *pushes*
597
+ // the robot state at ~30 Hz (see media_server
598
+ // `_setup_pose_channel`), so this can fire mid-session. It
599
+ // carries the same `{state:{...}}` envelope as a get_state
600
+ // reply, so we route it through the same handler - but it
601
+ // must NOT gate session readiness (that's the reliable
602
+ // control channel's job) nor become `_dc` (commands must
603
+ // never ride the lossy channel).
604
+ if (ch.label === 'pose') {
605
+ // Fresh channel (new session or daemon restart): the
606
+ // daemon's seq counter may have reset, so forget the old
607
+ // high-water mark or we'd drop every new frame.
608
+ this._lastPoseSeq = null;
609
+ ch.onmessage = (ev) => {
610
+ const msg = JSON.parse(ev.data);
611
+ // Drop stale/reordered frames (unordered channel).
612
+ if (typeof msg.seq === 'number') {
613
+ if (this._lastPoseSeq !== null && msg.seq <= this._lastPoseSeq)
614
+ return;
615
+ this._lastPoseSeq = msg.seq;
616
+ }
617
+ this._lastPoseFrameAt = Date.now();
618
+ this._handleRobotMessage(msg, true);
619
+ };
620
+ return;
621
+ }
622
+ this._dc = ch;
544
623
  this._dc.onopen = () => {
545
624
  this._dcOpen = true;
546
625
  this._checkSessionReady();
@@ -616,9 +695,18 @@ export class ReachyMini extends EventTarget {
616
695
  this._readAudioParameterResolve(null);
617
696
  this._readAudioParameterResolve = null;
618
697
  }
698
+ if (this._robotNameResolve) {
699
+ this._robotNameResolve(null);
700
+ this._robotNameResolve = null;
701
+ }
702
+ if (this._deleteHfTokenResolve) {
703
+ this._deleteHfTokenResolve(null);
704
+ this._deleteHfTokenResolve = null;
705
+ }
619
706
  this._logSubscribers.clear();
620
707
  this._updateProgressSubscribers.clear();
621
708
  this._rejectPendingMotionCompletions(new Error('Session stopped'));
709
+ this._rejectPendingRpc(new Error('Session stopped'));
622
710
  // Tear down resilience plumbing BEFORE closing `_pc` so a
623
711
  // queued grace callback can't dereference a dead handle.
624
712
  this._clearIceGrace();
@@ -695,6 +783,14 @@ export class ReachyMini extends EventTarget {
695
783
  this._readAudioParameterResolve(null);
696
784
  this._readAudioParameterResolve = null;
697
785
  }
786
+ if (this._robotNameResolve) {
787
+ this._robotNameResolve(null);
788
+ this._robotNameResolve = null;
789
+ }
790
+ if (this._deleteHfTokenResolve) {
791
+ this._deleteHfTokenResolve(null);
792
+ this._deleteHfTokenResolve = null;
793
+ }
698
794
  this._logSubscribers.clear();
699
795
  this._updateProgressSubscribers.clear();
700
796
  this._rejectPendingMotionCompletions(new Error('Disconnected'));
@@ -1170,6 +1266,43 @@ export class ReachyMini extends EventTarget {
1170
1266
  setMicrophoneVolume(volume) {
1171
1267
  return this._slotRoundtrip(() => this._micVolumeResolve, (next) => { this._micVolumeResolve = next; }, { type: 'set_microphone_volume', volume: clampVolume(volume) });
1172
1268
  }
1269
+ /**
1270
+ * Query the persisted robot display name. Resolves the stored name,
1271
+ * `null` when none is set / the channel isn't open / the daemon predates
1272
+ * the `get_robot_name` command.
1273
+ */
1274
+ getRobotName() {
1275
+ return this._slotRoundtrip(() => this._robotNameResolve, (next) => { this._robotNameResolve = next; }, { type: 'get_robot_name' });
1276
+ }
1277
+ /**
1278
+ * Set and persist the robot display name on the robot. Resolves with the
1279
+ * stored (trimmed) name, or `null` on error / channel-closed. Applied live
1280
+ * by the daemon (status + central relay + mDNS), so it takes effect right
1281
+ * away without a restart; the persisted name also overrides --robot-name
1282
+ * on the next start.
1283
+ */
1284
+ setRobotName(name) {
1285
+ return this._slotRoundtrip(() => this._robotNameResolve, (next) => { this._robotNameResolve = next; }, { type: 'set_robot_name', name });
1286
+ }
1287
+ /**
1288
+ * Sign this robot out of Hugging Face: asks the daemon to delete its
1289
+ * stored HF token, which de-registers the robot from the central
1290
+ * signaling relay (it disappears from its owner's robot list until it
1291
+ * is set up again). Works over the WebRTC data channel, so it reaches
1292
+ * the robot remotely (no LAN HTTP path required).
1293
+ *
1294
+ * Resolves `true` when the daemon acked success, `false` on a daemon
1295
+ * error, or `null` when no ack arrives before the timeout (e.g. a
1296
+ * daemon that predates the `delete_hf_token` command silently drops
1297
+ * it). Rejects if the data channel isn't open. Note the sign-out
1298
+ * drops the central relay, so the session may tear down right after
1299
+ * the ack - callers should treat a post-call session drop as expected,
1300
+ * and a successful sign-out may surface as `null` if teardown races
1301
+ * ahead of the ack.
1302
+ */
1303
+ signOut() {
1304
+ return this._slotRoundtrip(() => this._deleteHfTokenResolve, (next) => { this._deleteHfTokenResolve = next; }, { type: 'delete_hf_token' });
1305
+ }
1173
1306
  applyAudioConfig(config, { verify = true } = {}) {
1174
1307
  return this._slotRoundtrip(() => this._applyAudioConfigResolve, (next) => { this._applyAudioConfigResolve = next; }, { type: 'apply_audio_config', config, verify }).then((v) => v === true);
1175
1308
  }
@@ -1199,7 +1332,29 @@ export class ReachyMini extends EventTarget {
1199
1332
  const prev = getSlot();
1200
1333
  if (prev)
1201
1334
  prev(null);
1202
- setSlot(resolve);
1335
+ let timer;
1336
+ // Single settle path shared by the daemon response, supersession
1337
+ // by a newer call, and the fail-open timeout below. It clears the
1338
+ // timer once and detaches itself from the slot only if it's still
1339
+ // the current occupant, so a stale settle (timed-out or superseded)
1340
+ // can't clear a newer call's slot registration. The message handler
1341
+ // stores `settle` (not `resolve`), so every response route funnels
1342
+ // through here.
1343
+ // Note: slots are keyed by command type, not request id, so this
1344
+ // does not prevent a genuinely late daemon reply from being routed
1345
+ // to a newer same-command caller — that cross-talk is inherent to
1346
+ // the single-flight slot design and unchanged here.
1347
+ const settle = (v) => {
1348
+ if (timer !== undefined) {
1349
+ clearTimeout(timer);
1350
+ timer = undefined;
1351
+ }
1352
+ if (getSlot() === settle)
1353
+ setSlot(null);
1354
+ resolve(v);
1355
+ };
1356
+ setSlot(settle);
1357
+ timer = setTimeout(() => settle(null), SLOT_ROUNDTRIP_TIMEOUT_MS);
1203
1358
  this._sendCommand(command);
1204
1359
  });
1205
1360
  }
@@ -1229,6 +1384,30 @@ export class ReachyMini extends EventTarget {
1229
1384
  requestState() {
1230
1385
  return this._sendCommand({ type: 'get_state' });
1231
1386
  }
1387
+ /**
1388
+ * Ask the daemon to *push* the robot state (~30 Hz) over the dedicated
1389
+ * unreliable/unordered `pose` data channel instead of polling get_state.
1390
+ * Fires `state` events as frames arrive. No-op against an older daemon (no
1391
+ * pose channel) - fall back to `requestState()` polling there.
1392
+ *
1393
+ * Refcounted: pair every `subscribePose()` with exactly one
1394
+ * `unsubscribePose()`. Multiple consumers share a single daemon-side
1395
+ * subscription; the daemon only stops pushing once the last one releases.
1396
+ * If the channel isn't open yet (or the session later reconnects), the
1397
+ * subscription is (re-)asserted from `_checkSessionReady`.
1398
+ */
1399
+ subscribePose() {
1400
+ this._poseSubRefs++;
1401
+ return this._sendCommand({ type: 'subscribe_pose' });
1402
+ }
1403
+ /** Release one pose-stream consumer; sends `unsubscribe_pose` on the last. */
1404
+ unsubscribePose() {
1405
+ if (this._poseSubRefs > 0)
1406
+ this._poseSubRefs--;
1407
+ if (this._poseSubRefs > 0)
1408
+ return true; // still wanted by another consumer
1409
+ return this._sendCommand({ type: 'unsubscribe_pose' });
1410
+ }
1232
1411
  // ─── Audio ───────────────────────────────────────────────────────────
1233
1412
  setAudioMuted(muted) {
1234
1413
  this._audioMuted = muted;
@@ -1526,11 +1705,105 @@ export class ReachyMini extends EventTarget {
1526
1705
  this._dc.send(JSON.stringify(cmd));
1527
1706
  return true;
1528
1707
  }
1708
+ /**
1709
+ * Call a JSON-RPC method on the robot/app over the DataChannel and await
1710
+ * its result. This is the one way to drive an on-robot app (start/stop it
1711
+ * via `apps.*`, or drive a running app via its own namespace, e.g.
1712
+ * `conversation.say`). Rejects on the JSON-RPC error, a closed channel, or
1713
+ * timeout.
1714
+ */
1715
+ rpcCall(method, params = {}, opts = {}) {
1716
+ const timeoutMs = opts.timeoutMs ?? 20000;
1717
+ const id = `rpc-${++this._rpcCounter}`;
1718
+ if (!this._sendCommand({ jsonrpc: '2.0', id, method, params })) {
1719
+ return Promise.reject(new Error(`rpcCall(${method}): data channel not open`));
1720
+ }
1721
+ return new Promise((resolve, reject) => {
1722
+ const timer = setTimeout(() => {
1723
+ this._pendingRpc.delete(id);
1724
+ reject(new Error(`rpcCall(${method}) timed out after ${timeoutMs}ms`));
1725
+ }, timeoutMs);
1726
+ this._pendingRpc.set(id, {
1727
+ resolve: (v) => resolve(v),
1728
+ reject,
1729
+ timer,
1730
+ });
1731
+ });
1732
+ }
1733
+ /**
1734
+ * Subscribe to a JSON-RPC notification (one-way event) pushed by the
1735
+ * robot/app, e.g. `conversation.turn`. Returns an unsubscribe function.
1736
+ */
1737
+ onNotification(method, cb) {
1738
+ let set = this._rpcListeners.get(method);
1739
+ if (!set) {
1740
+ set = new Set();
1741
+ this._rpcListeners.set(method, set);
1742
+ }
1743
+ set.add(cb);
1744
+ return () => {
1745
+ this._rpcListeners.get(method)?.delete(cb);
1746
+ };
1747
+ }
1748
+ _rejectPendingRpc(err) {
1749
+ for (const pending of this._pendingRpc.values()) {
1750
+ clearTimeout(pending.timer);
1751
+ pending.reject(err);
1752
+ }
1753
+ this._pendingRpc.clear();
1754
+ }
1755
+ _handleRpcMessage(data) {
1756
+ // Response to an rpcCall (correlated by id)...
1757
+ if ('id' in data && data.id != null && ('result' in data || 'error' in data)) {
1758
+ const pending = this._pendingRpc.get(data.id);
1759
+ if (!pending)
1760
+ return;
1761
+ this._pendingRpc.delete(data.id);
1762
+ clearTimeout(pending.timer);
1763
+ if ('error' in data && data.error) {
1764
+ const err = data.error;
1765
+ const e = new Error(err.message ?? 'rpc error');
1766
+ e.reason = err.data?.reason;
1767
+ pending.reject(e);
1768
+ }
1769
+ else {
1770
+ pending.resolve(data.result);
1771
+ }
1772
+ return;
1773
+ }
1774
+ // ...or a one-way notification (event): dispatch to listeners.
1775
+ if (typeof data.method === 'string') {
1776
+ const params = data.params ?? {};
1777
+ for (const cb of this._rpcListeners.get(data.method) ?? []) {
1778
+ try {
1779
+ cb(params);
1780
+ }
1781
+ catch (e) {
1782
+ console.error(`onNotification(${data.method}) threw:`, e);
1783
+ }
1784
+ }
1785
+ }
1786
+ }
1529
1787
  _checkSessionReady() {
1530
1788
  if (this._iceConnected && this._dcOpen && this._sessionResolve) {
1531
1789
  this._state = 'streaming';
1532
1790
  this.requestState();
1533
- this._stateRefreshInterval = setInterval(() => this.requestState(), 500);
1791
+ // Re-assert a pose subscription that was requested before the data
1792
+ // channel was open, or lost on reconnect (a fresh peer starts
1793
+ // unsubscribed on the daemon). Sent raw so it doesn't touch the
1794
+ // local refcount, which already reflects the live consumer count.
1795
+ if (this._poseSubRefs > 0)
1796
+ this._sendCommand({ type: 'subscribe_pose' });
1797
+ // A fresh session has received no pose frame yet, so a timestamp
1798
+ // left over from the previous one must not hold off the poll.
1799
+ this._lastPoseFrameAt = 0;
1800
+ this._stateRefreshInterval = setInterval(() => {
1801
+ // Skip while the pose stream is already feeding state; see
1802
+ // POSE_STREAM_FRESH_MS.
1803
+ if (Date.now() - this._lastPoseFrameAt < POSE_STREAM_FRESH_MS)
1804
+ return;
1805
+ this.requestState();
1806
+ }, 500);
1534
1807
  this._emit('streaming', { sessionId: this._sessionId, robotId: this._selectedRobotId });
1535
1808
  this._sessionResolve();
1536
1809
  this._sessionResolve = null;
@@ -1674,7 +1947,18 @@ export class ReachyMini extends EventTarget {
1674
1947
  this._emit('error', { source: 'webrtc', error: e });
1675
1948
  }
1676
1949
  }
1677
- _handleRobotMessage(data) {
1950
+ /**
1951
+ * @param fromPoseStream Frame came from the pushed `pose` channel rather
1952
+ * than the reliable control channel. Only those may refresh the state
1953
+ * mirror while the stream is live (see the `data.state` branch).
1954
+ */
1955
+ _handleRobotMessage(data, fromPoseStream = false) {
1956
+ // JSON-RPC frames (app control surface) are handled separately from
1957
+ // the legacy {command|type} robot messages that share this channel.
1958
+ if (data.jsonrpc === '2.0') {
1959
+ this._handleRpcMessage(data);
1960
+ return;
1961
+ }
1678
1962
  if ('version' in data && this._versionResolve) {
1679
1963
  this._versionResolve(data.version);
1680
1964
  this._versionResolve = null;
@@ -1699,6 +1983,20 @@ export class ReachyMini extends EventTarget {
1699
1983
  }
1700
1984
  return;
1701
1985
  }
1986
+ if (data.command === 'get_robot_name' || data.command === 'set_robot_name') {
1987
+ if (this._robotNameResolve) {
1988
+ this._robotNameResolve(data.status === 'error' ? null : (data.name ?? null));
1989
+ this._robotNameResolve = null;
1990
+ }
1991
+ return;
1992
+ }
1993
+ if (data.command === 'delete_hf_token') {
1994
+ if (this._deleteHfTokenResolve) {
1995
+ this._deleteHfTokenResolve(data.status === 'error' ? false : true);
1996
+ this._deleteHfTokenResolve = null;
1997
+ }
1998
+ return;
1999
+ }
1702
2000
  if (data.command === 'apply_audio_config') {
1703
2001
  if (this._applyAudioConfigResolve) {
1704
2002
  this._applyAudioConfigResolve(data.error ? false : !!data.applied);
@@ -1795,7 +2093,18 @@ export class ReachyMini extends EventTarget {
1795
2093
  }
1796
2094
  return;
1797
2095
  }
1798
- if (data.state) {
2096
+ // Only the stream may write the mirror while the stream is live. The
2097
+ // poll stands down in that case (see POSE_STREAM_FRESH_MS), but it
2098
+ // can't unsend a request already in flight: `get_state` rides the
2099
+ // reliable channel, so its reply queues behind whatever else is on it
2100
+ // - a `upload_move_*` burst, typically - and can land hundreds of ms
2101
+ // after the snapshot it carries. Having no `seq`, it slips past the
2102
+ // stale-frame guard and rewinds every consumer to a pose from before
2103
+ // the upload, until the next pushed frame puts them back. That reads
2104
+ // as a one-frame flick to the pre-move pose right as an animation
2105
+ // starts. Nothing is lost by dropping it: pushed frames carry the same
2106
+ // fields (daemon-side `build_state_dict` feeds both).
2107
+ if (data.state && (fromPoseStream || Date.now() - this._lastPoseFrameAt >= POSE_STREAM_FRESH_MS)) {
1799
2108
  const s = data.state;
1800
2109
  if (s.head_pose)
1801
2110
  this._robotState.head = s.head_pose.flat();