@arbidocs/react 0.5.184 → 0.5.185

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.
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkKQY2LP76_cjs = require('../../chunk-KQY2LP76.cjs');
3
+ var chunkTKPBJCWQ_cjs = require('../../chunk-TKPBJCWQ.cjs');
4
4
  require('../../chunk-EEIYXVM4.cjs');
5
5
  require('../../chunk-BEOHXA2Z.cjs');
6
6
  var chunkSJZRVX7T_cjs = require('../../chunk-SJZRVX7T.cjs');
@@ -124,8 +124,8 @@ function useCallTranscriber({
124
124
  const arbi = chunkSJZRVX7T_cjs.useArbi();
125
125
  const queryClient = reactQuery.useQueryClient();
126
126
  const profile = arbi.isLoggedIn ? arbi.user.profile() : null;
127
- const myProfileName = profile && chunkKQY2LP76_cjs.toProfileView(profile).fullName || null;
128
- const deploymentAvailable = chunkKQY2LP76_cjs.useCallTranscriptionAvailable();
127
+ const myProfileName = profile && chunkTKPBJCWQ_cjs.toProfileView(profile).fullName || null;
128
+ const deploymentAvailable = chunkTKPBJCWQ_cjs.useCallTranscriptionAvailable();
129
129
  const available = deploymentAvailable && !!callKey;
130
130
  const [state, setState] = react.useState(null);
131
131
  const [creatorExtId, setCreatorExtId] = react.useState(null);
@@ -168,7 +168,7 @@ function useCallTranscriber({
168
168
  };
169
169
  }, [available, callExtId]);
170
170
  react.useEffect(
171
- () => chunkKQY2LP76_cjs.onCallEvent("call.transcription", (message) => {
171
+ () => chunkTKPBJCWQ_cjs.onCallEvent("call.transcription", (message) => {
172
172
  if (message.call_ext_id === callExtId) applyState(message);
173
173
  }),
174
174
  [callExtId, applyState]
@@ -184,7 +184,7 @@ function useCallTranscriber({
184
184
  const persist = react.useCallback(
185
185
  (segment, speakerNeedsProxy) => {
186
186
  if (!callKey || persisted.current.has(segment.segmentId)) return;
187
- const role = chunkKQY2LP76_cjs.transcriptPersistRole({
187
+ const role = chunkTKPBJCWQ_cjs.transcriptPersistRole({
188
188
  myIdentity,
189
189
  canPost: canPost && (segment.speakerIdentity !== myIdentity || selfPosts.current),
190
190
  hasCallKey: true,
@@ -193,7 +193,7 @@ function useCallTranscriber({
193
193
  });
194
194
  if (!role) return;
195
195
  persisted.current.add(segment.segmentId);
196
- chunkKQY2LP76_cjs.persistTranscriptSegment(arbi, queryClient, callExtId, callKey, segment).catch(
196
+ chunkTKPBJCWQ_cjs.persistTranscriptSegment(arbi, queryClient, callExtId, callKey, segment).catch(
197
197
  (err) => {
198
198
  persisted.current.delete(segment.segmentId);
199
199
  if (role === "own" && err?.status === 403) {
@@ -213,7 +213,7 @@ function useCallTranscriber({
213
213
  );
214
214
  react.useEffect(() => {
215
215
  const onData = (payload, participant, _kind, topic, _encryption, participantIdentity) => {
216
- if (topic === chunkKQY2LP76_cjs.CALL_TRANSCRIPTION_TOPIC) {
216
+ if (topic === chunkTKPBJCWQ_cjs.CALL_TRANSCRIPTION_TOPIC) {
217
217
  if (participant || participantIdentity) return;
218
218
  try {
219
219
  const next = browser.calls.transcriptionStateFrom(JSON.parse(new TextDecoder().decode(payload)));
@@ -222,10 +222,10 @@ function useCallTranscriber({
222
222
  }
223
223
  return;
224
224
  }
225
- if (topic !== chunkKQY2LP76_cjs.CALL_CAPTION_TOPIC || !callKey) return;
225
+ if (topic !== chunkTKPBJCWQ_cjs.CALL_CAPTION_TOPIC || !callKey) return;
226
226
  const identity = participant?.identity ?? participantIdentity;
227
227
  if (!identity || identity === myIdentity) return;
228
- void chunkKQY2LP76_cjs.decodeCaptionPacket(payload, callKey).then((packet) => {
228
+ void chunkTKPBJCWQ_cjs.decodeCaptionPacket(payload, callKey).then((packet) => {
229
229
  if (!packet || packet.identity !== identity) return;
230
230
  const name = participant?.name || packet.name || identity;
231
231
  const t = Math.min(packet.t, Date.now());
@@ -265,7 +265,7 @@ function useCallTranscriber({
265
265
  })) {
266
266
  return;
267
267
  }
268
- const segmentId = chunkKQY2LP76_cjs.newSegmentId();
268
+ const segmentId = chunkTKPBJCWQ_cjs.newSegmentId();
269
269
  const startedAt = new Date(segment.startedAt).toISOString();
270
270
  let text;
271
271
  try {
@@ -299,7 +299,7 @@ function useCallTranscriber({
299
299
  });
300
300
  try {
301
301
  await localParticipant.publishData(
302
- await chunkKQY2LP76_cjs.encodeCaptionPacket(
302
+ await chunkTKPBJCWQ_cjs.encodeCaptionPacket(
303
303
  {
304
304
  v: 1,
305
305
  segment_id: segmentId,
@@ -311,7 +311,7 @@ function useCallTranscriber({
311
311
  },
312
312
  callKey
313
313
  ),
314
- { reliable: true, topic: chunkKQY2LP76_cjs.CALL_CAPTION_TOPIC }
314
+ { reliable: true, topic: chunkTKPBJCWQ_cjs.CALL_CAPTION_TOPIC }
315
315
  );
316
316
  } catch (err) {
317
317
  console.warn("[calls] could not broadcast a caption", err);
@@ -426,14 +426,14 @@ function useCallRecorder({
426
426
  };
427
427
  }, [callExtId]);
428
428
  react.useEffect(
429
- () => chunkKQY2LP76_cjs.onCallEvent("call.recording", (message) => {
429
+ () => chunkTKPBJCWQ_cjs.onCallEvent("call.recording", (message) => {
430
430
  if (message.call_ext_id === callExtId) applyState(message);
431
431
  }),
432
432
  [callExtId, applyState]
433
433
  );
434
434
  react.useEffect(() => {
435
435
  const onData = (payload, participant, _kind, topic, _encryption, participantIdentity) => {
436
- if (topic !== chunkKQY2LP76_cjs.CALL_RECORDING_TOPIC) return;
436
+ if (topic !== chunkTKPBJCWQ_cjs.CALL_RECORDING_TOPIC) return;
437
437
  if (participant || participantIdentity) return;
438
438
  try {
439
439
  const next = browser.calls.recordingStateFrom(JSON.parse(new TextDecoder().decode(payload)));
@@ -454,7 +454,7 @@ function useCallRecorder({
454
454
  try {
455
455
  const row = call ?? await arbi.calls.get(callExtId);
456
456
  if (!row.workspace_locked && !row.chat_ext_id && callKey) {
457
- const ref = await chunkKQY2LP76_cjs.ensureMeetingChat(arbi, queryClient, callExtId, callKey);
457
+ const ref = await chunkTKPBJCWQ_cjs.ensureMeetingChat(arbi, queryClient, callExtId, callKey);
458
458
  if (ref.kind === "chat") setCall({ ...row, chat_ext_id: ref.chatExtId });
459
459
  }
460
460
  const answer = await arbi.calls.recording.start(callExtId, mode);
@@ -556,7 +556,7 @@ function useTrackUploader(recording, callKey, kind, mimeType) {
556
556
  const queryClient = reactQuery.useQueryClient();
557
557
  return react.useMemo(() => {
558
558
  if (!shouldCapture || !recordingExtId || !call) return null;
559
- return chunkKQY2LP76_cjs.trackUploaderFor({
559
+ return chunkTKPBJCWQ_cjs.trackUploaderFor({
560
560
  arbi,
561
561
  callExtId: call.external_id,
562
562
  recordingExtId,
@@ -564,7 +564,7 @@ function useTrackUploader(recording, callKey, kind, mimeType) {
564
564
  mimeType,
565
565
  recordingStartedAt: startedAt,
566
566
  linkVersion,
567
- conversationKey: () => chunkKQY2LP76_cjs.conversationKeyForCall(arbi, queryClient, call, { callKey }),
567
+ conversationKey: () => chunkTKPBJCWQ_cjs.conversationKeyForCall(arbi, queryClient, call, { callKey }),
568
568
  // The server stopped the recording at the quota (its state event says so too).
569
569
  onStopped: () => markStopped()
570
570
  });
@@ -613,10 +613,10 @@ function useInCallChat({
613
613
  react.useEffect(() => {
614
614
  if (!callKey) return;
615
615
  const onData = (payload, participant, _kind, topic, _encryption, participantIdentity) => {
616
- if (topic !== chunkKQY2LP76_cjs.CALL_CHAT_TOPIC) return;
616
+ if (topic !== chunkTKPBJCWQ_cjs.CALL_CHAT_TOPIC) return;
617
617
  const identity = participant?.identity ?? participantIdentity;
618
618
  if (!identity || identity === myIdentity) return;
619
- void chunkKQY2LP76_cjs.decodeCallChatPacket(payload, callKey).then((packet) => {
619
+ void chunkTKPBJCWQ_cjs.decodeCallChatPacket(payload, callKey).then((packet) => {
620
620
  if (!packet) return;
621
621
  const key = `${identity}:${packet.id}`;
622
622
  if (seen.current.has(key)) return;
@@ -646,7 +646,7 @@ function useInCallChat({
646
646
  if (!open || !canRead || historyRequested.current) return;
647
647
  historyRequested.current = true;
648
648
  setHistoryLoading(true);
649
- chunkKQY2LP76_cjs.loadMeetingHistory(arbi, queryClient, callExtId).then((result) => {
649
+ chunkTKPBJCWQ_cjs.loadMeetingHistory(arbi, queryClient, callExtId).then((result) => {
650
650
  if (!result) return;
651
651
  setMeetingChat(result.ref);
652
652
  setHistory(result.messages);
@@ -657,13 +657,13 @@ function useInCallChat({
657
657
  }, [open, canRead, callExtId]);
658
658
  const send = react.useCallback(
659
659
  async (raw) => {
660
- const text = raw.trim().slice(0, chunkKQY2LP76_cjs.CALL_CHAT_MAX_CHARS);
660
+ const text = raw.trim().slice(0, chunkTKPBJCWQ_cjs.CALL_CHAT_MAX_CHARS);
661
661
  if (!text || !callKey || readOnly) return;
662
- const id = chunkKQY2LP76_cjs.newPacketId();
662
+ const id = chunkTKPBJCWQ_cjs.newPacketId();
663
663
  const at = Date.now();
664
664
  await localParticipant.publishData(
665
- await chunkKQY2LP76_cjs.encodeCallChatPacket({ v: 1, id, text, at }, callKey),
666
- { reliable: true, topic: chunkKQY2LP76_cjs.CALL_CHAT_TOPIC }
665
+ await chunkTKPBJCWQ_cjs.encodeCallChatPacket({ v: 1, id, text, at }, callKey),
666
+ { reliable: true, topic: chunkTKPBJCWQ_cjs.CALL_CHAT_TOPIC }
667
667
  );
668
668
  setLive((prev) => [
669
669
  ...prev,
@@ -682,7 +682,7 @@ function useInCallChat({
682
682
  (prev) => prev.map((l) => l.id === id && l.senderIdentity === myIdentity ? { ...l, status } : l)
683
683
  );
684
684
  try {
685
- setMeetingChat(await chunkKQY2LP76_cjs.persistMeetingMessage(arbi, queryClient, callExtId, callKey, text));
685
+ setMeetingChat(await chunkTKPBJCWQ_cjs.persistMeetingMessage(arbi, queryClient, callExtId, callKey, text));
686
686
  mark("saved");
687
687
  } catch (err) {
688
688
  console.warn("[calls] could not keep the message in the meeting chat", err);
@@ -694,7 +694,7 @@ function useInCallChat({
694
694
  const openMeeting = react.useCallback(async () => {
695
695
  if (!callKey) return;
696
696
  try {
697
- const ref = meetingChat ?? await chunkKQY2LP76_cjs.ensureMeetingChat(arbi, queryClient, callExtId, callKey);
697
+ const ref = meetingChat ?? await chunkTKPBJCWQ_cjs.ensureMeetingChat(arbi, queryClient, callExtId, callKey);
698
698
  setMeetingChat(ref);
699
699
  openMeetingChat?.(ref);
700
700
  } catch (err) {
@@ -702,7 +702,7 @@ function useInCallChat({
702
702
  }
703
703
  }, [callExtId, callKey, meetingChat]);
704
704
  const lines = react.useMemo(
705
- () => chunkKQY2LP76_cjs.mergeChatLines(history, live, {
705
+ () => chunkTKPBJCWQ_cjs.mergeChatLines(history, live, {
706
706
  myIdentity,
707
707
  nameOf: (identity) => identity === myIdentity ? localParticipant.name || "You" : room.remoteParticipants.get(identity)?.name || null
708
708
  }),
@@ -761,7 +761,7 @@ function useWebinar({
761
761
  const n = ++questionsRead.current;
762
762
  try {
763
763
  const rows = await webinarApi().questions.list(callExtId, linkVersion);
764
- const opened = await Promise.all(rows.map((q) => chunkKQY2LP76_cjs.openQuestion(q, callKey)));
764
+ const opened = await Promise.all(rows.map((q) => chunkTKPBJCWQ_cjs.openQuestion(q, callKey)));
765
765
  if (n === questionsRead.current) setQuestions(opened);
766
766
  } catch (err) {
767
767
  console.warn("[calls] could not load the questions", err);
@@ -773,7 +773,7 @@ function useWebinar({
773
773
  }, [refresh, refreshQuestions]);
774
774
  react.useEffect(() => {
775
775
  if (!enabled) return;
776
- return chunkKQY2LP76_cjs.onCallEvent("call.webinar", (m) => {
776
+ return chunkTKPBJCWQ_cjs.onCallEvent("call.webinar", (m) => {
777
777
  if (m.call_ext_id !== callExtId) return;
778
778
  setState((s) => s ? { ...s, live: m.live, chat: m.chat } : s);
779
779
  });
@@ -782,8 +782,8 @@ function useWebinar({
782
782
  react.useEffect(() => {
783
783
  if (!enabled) return;
784
784
  const onData = (payload, participant, _kind, topic, _encryption, participantIdentity) => {
785
- if (topic !== chunkKQY2LP76_cjs.CALL_WEBINAR_TOPIC) return;
786
- const packet = chunkKQY2LP76_cjs.parseWebinarPacket(payload, !!(participant || participantIdentity));
785
+ if (topic !== chunkTKPBJCWQ_cjs.CALL_WEBINAR_TOPIC) return;
786
+ const packet = chunkTKPBJCWQ_cjs.parseWebinarPacket(payload, !!(participant || participantIdentity));
787
787
  if (!packet) return;
788
788
  switch (packet.type) {
789
789
  case "state":
@@ -804,7 +804,7 @@ function useWebinar({
804
804
  return;
805
805
  case "demoted":
806
806
  setStageCheck(false);
807
- chunkKQY2LP76_cjs.markRejoining(callExtId);
807
+ chunkTKPBJCWQ_cjs.markRejoining(callExtId);
808
808
  if (fallback.current) clearTimeout(fallback.current);
809
809
  fallback.current = setTimeout(() => void room.disconnect(), DEMOTE_REJOIN_FALLBACK_MS);
810
810
  return;
@@ -831,12 +831,12 @@ function useWebinar({
831
831
  [callExtId, linkVersion]
832
832
  );
833
833
  const stepDown = react.useCallback(async () => {
834
- chunkKQY2LP76_cjs.markRejoining(callExtId);
834
+ chunkTKPBJCWQ_cjs.markRejoining(callExtId);
835
835
  setStageCheck(false);
836
836
  try {
837
837
  await webinarApi().demote(callExtId, myIdentity, linkVersion);
838
838
  } catch (err) {
839
- chunkKQY2LP76_cjs.markRejoining(callExtId, false);
839
+ chunkTKPBJCWQ_cjs.markRejoining(callExtId, false);
840
840
  failed("leave the stage")(err);
841
841
  }
842
842
  }, [callExtId, myIdentity, linkVersion]);
@@ -896,7 +896,7 @@ function useWebinar({
896
896
  if (!callKey) throw new Error("This call has no key.");
897
897
  await webinarApi().questions.ask(
898
898
  callExtId,
899
- await chunkKQY2LP76_cjs.sealQuestionText(text, callKey),
899
+ await chunkTKPBJCWQ_cjs.sealQuestionText(text, callKey),
900
900
  linkVersion
901
901
  );
902
902
  }, "ask the question"),
@@ -910,7 +910,7 @@ function useWebinar({
910
910
  await webinarApi().questions.update(
911
911
  callExtId,
912
912
  id,
913
- { answer: await chunkKQY2LP76_cjs.sealQuestionText(text, callKey) },
913
+ { answer: await chunkTKPBJCWQ_cjs.sealQuestionText(text, callKey) },
914
914
  linkVersion
915
915
  );
916
916
  }, "answer the question"),
@@ -940,15 +940,15 @@ function useWebinar({
940
940
  function useHostMuteNotice(callExtId) {
941
941
  const room = componentsReact.useRoomContext();
942
942
  react.useEffect(() => {
943
- const notifyMuted = chunkKQY2LP76_cjs.createMutedNotifier((text) => chunk5ECKSF6H_cjs.notify.info(text));
943
+ const notifyMuted = chunkTKPBJCWQ_cjs.createMutedNotifier((text) => chunk5ECKSF6H_cjs.notify.info(text));
944
944
  const onData = (payload, participant, _kind, topic, _encryption, participantIdentity) => {
945
- if (topic !== chunkKQY2LP76_cjs.CALL_MODERATION_TOPIC) return;
945
+ if (topic !== chunkTKPBJCWQ_cjs.CALL_MODERATION_TOPIC) return;
946
946
  if (participant || participantIdentity) return;
947
- const notice = chunkKQY2LP76_cjs.mutedNoticeFromPacket(payload);
947
+ const notice = chunkTKPBJCWQ_cjs.mutedNoticeFromPacket(payload);
948
948
  if (notice) notifyMuted(notice);
949
949
  };
950
950
  room.on(livekitClient.RoomEvent.DataReceived, onData);
951
- const offWs = chunkKQY2LP76_cjs.onCallEvent("call.muted", (message) => {
951
+ const offWs = chunkTKPBJCWQ_cjs.onCallEvent("call.muted", (message) => {
952
952
  if (message.callExtId === callExtId) notifyMuted(message);
953
953
  });
954
954
  return () => {
@@ -960,9 +960,11 @@ function useHostMuteNotice(callExtId) {
960
960
  function checkedCallMedia(value, participant) {
961
961
  if (!value || typeof value !== "object") throw new Error("Invalid call media");
962
962
  const v = value;
963
- if (v.protocol !== "arbi.call.media.v1" || v.participant !== participant || typeof v.utterance_id !== "string" || v.utterance_id.length > 64 || !Number.isSafeInteger(v.sequence) || Number(v.sequence) < 0 || !["start", "media", "end", "stop", "fallback"].includes(String(v.type)))
963
+ if (v.protocol !== "arbi.call.media.v1" || v.participant !== participant || typeof v.utterance_id !== "string" || v.utterance_id.length > 64 || !Number.isSafeInteger(v.sequence) || Number(v.sequence) < 0 || !["start", "media", "audio", "animation", "video-stop", "end", "stop", "fallback"].includes(
964
+ String(v.type)
965
+ ))
964
966
  throw new Error("Invalid call media identity or sequence");
965
- if (v.type === "start" && (v.sample_rate !== 24e3 || v.fps !== 60 || typeof v.avatar !== "string" || !Array.isArray(v.names) || v.names.length !== 52 || new Set(v.names).size !== 52 || !v.names.every((name) => typeof name === "string" && name.length < 80)))
967
+ if (v.type === "start" && (v.sample_rate !== 24e3 || v.fps !== 60 || typeof v.avatar !== "string" || !Array.isArray(v.names) || v.names.length !== 52 && !(v.independent === true && v.names.length === 0) || new Set(v.names).size !== v.names.length || !v.names.every((name) => typeof name === "string" && name.length < 80)))
966
968
  throw new Error("Invalid call media configuration");
967
969
  if (v.type === "media" && (!Number.isSafeInteger(v.start_sample) || Number(v.start_sample) < 0 || typeof v.pcm !== "string" || v.pcm.length > 6400 || !Array.isArray(v.frames) || !v.frames.length || v.frames.length > 6 || !v.frames.every(
968
970
  (frame) => Array.isArray(frame) && frame.length === 52 && frame.every(
@@ -970,6 +972,16 @@ function checkedCallMedia(value, participant) {
970
972
  )
971
973
  )))
972
974
  throw new Error("Invalid call media samples");
975
+ if ((v.type === "audio" || v.type === "animation") && (!Number.isSafeInteger(v.start_sample) || Number(v.start_sample) < 0))
976
+ throw new Error("Invalid independent media clock");
977
+ if (v.type === "audio" && (typeof v.pcm !== "string" || !v.pcm.length || v.pcm.length > 14e3))
978
+ throw new Error("Invalid independent PCM");
979
+ if (v.type === "animation" && (!Array.isArray(v.names) || v.names.length !== 52 || new Set(v.names).size !== 52 || !v.names.every((name) => typeof name === "string" && name.length < 80) || !Array.isArray(v.frames) || !v.frames.length || v.frames.length > 6 || !v.frames.every(
980
+ (frame) => Array.isArray(frame) && frame.length === 52 && frame.every(
981
+ (x) => typeof x === "number" && Number.isFinite(x) && x >= -2 && x <= 2
982
+ )
983
+ )))
984
+ throw new Error("Invalid independent animation");
973
985
  return v;
974
986
  }
975
987
  var CallMediaPlayer = class {
@@ -983,11 +995,16 @@ var CallMediaPlayer = class {
983
995
  changed;
984
996
  measured;
985
997
  streaming = false;
998
+ independent = false;
999
+ motionRequested = true;
1000
+ motionCurrent = true;
1001
+ motionLag = 0;
1002
+ motion = [];
986
1003
  audible = false;
987
1004
  get active() {
988
1005
  return this.audible;
989
1006
  }
990
- /** Retain transport ownership between utterances; reattaching RTC can replay its buffered tail. */
1007
+ /** Retain ownership between utterances; only explicit fallback may unmute RTC. */
991
1008
  suppressesTrack = false;
992
1009
  avatar = "";
993
1010
  names = [];
@@ -1017,15 +1034,24 @@ var CallMediaPlayer = class {
1017
1034
  }
1018
1035
  if (message.type === "start") {
1019
1036
  if (message.sequence !== 0 || this.seen.has(message.utterance_id)) return;
1020
- this.reset();
1037
+ const preserveAudio = this.independent && message.independent === true;
1038
+ const previousEnd = preserveAudio ? this.nextTime : 0;
1039
+ this.reset(false, preserveAudio);
1021
1040
  this.seen.add(message.utterance_id);
1022
1041
  if (this.seen.size > 64) this.seen.delete(this.seen.values().next().value);
1023
1042
  this.utterance = message.utterance_id;
1043
+ this.independent = message.independent === true;
1044
+ this.motionCurrent = this.motionRequested;
1045
+ this.motionLag = 0;
1046
+ this.motion = [];
1024
1047
  this.avatar = message.avatar;
1025
1048
  this.names = message.names;
1026
1049
  this.sequence = 0;
1027
1050
  this.nextSample = 0;
1028
- this.nextTime = this.context.currentTime + 0.15;
1051
+ this.nextTime = Math.max(
1052
+ previousEnd,
1053
+ this.context.currentTime + (this.independent ? 0.04 : 0.15)
1054
+ );
1029
1055
  this.streaming = true;
1030
1056
  this.renderedMouth = false;
1031
1057
  this.mouthRest = void 0;
@@ -1039,11 +1065,32 @@ var CallMediaPlayer = class {
1039
1065
  if (message.utterance_id !== this.utterance || message.sequence <= this.sequence) return;
1040
1066
  if (message.sequence !== this.sequence + 1) throw new Error("Call media sequence gap");
1041
1067
  this.sequence = message.sequence;
1042
- if (message.type === "media") {
1068
+ if (["audio", "animation", "video-stop"].includes(message.type) && !this.independent)
1069
+ throw new Error("Independent media was not negotiated");
1070
+ if (message.type === "video-stop") {
1071
+ this.motionCurrent = false;
1072
+ this.motion = [];
1073
+ return;
1074
+ }
1075
+ if (message.type === "animation") {
1076
+ if (!this.independent || !this.motionCurrent) return;
1077
+ if (!this.motion.length) {
1078
+ const current = this.segments.find(
1079
+ (s) => this.context.currentTime >= s.at && this.context.currentTime < s.at + s.duration
1080
+ );
1081
+ const sample = current ? current.startSample + (this.context.currentTime - current.at) * 24e3 : 0;
1082
+ this.motionLag = Math.min(2, Math.max(0, (sample - message.start_sample) / 24e3) + 0.1);
1083
+ }
1084
+ this.names = message.names;
1085
+ this.motion.push({ startSample: message.start_sample, frames: message.frames });
1086
+ if (this.motion.length > 128) this.motion.shift();
1087
+ return;
1088
+ }
1089
+ if (message.type === "media" || message.type === "audio") {
1043
1090
  if (!this.streaming || message.start_sample !== this.nextSample)
1044
1091
  throw new Error("Call media sample gap");
1045
1092
  const pcm = client.base64ToBytes(message.pcm);
1046
- if (pcm.length % 2 || pcm.length > message.frames.length * 800)
1093
+ if (pcm.length % 2 || message.type === "media" && pcm.length > message.frames.length * 800)
1047
1094
  throw new Error("Invalid PCM length");
1048
1095
  if (this.nextTime - this.context.currentTime > 2) throw new Error("Call media queue exceeded");
1049
1096
  const buffer = this.context.createBuffer(1, pcm.length / 2, 24e3);
@@ -1099,10 +1146,10 @@ var CallMediaPlayer = class {
1099
1146
  at,
1100
1147
  startSample: message.start_sample,
1101
1148
  duration: buffer.duration,
1102
- frames: message.frames
1149
+ frames: message.frames ?? []
1103
1150
  });
1104
1151
  this.segments = this.segments.filter(
1105
- (s) => s.at + s.duration > this.context.currentTime - 0.1
1152
+ (s) => s.at + s.duration > this.context.currentTime - (this.independent ? 2.1 : 0.1)
1106
1153
  );
1107
1154
  this.nextTime = at + buffer.duration;
1108
1155
  this.nextSample += values.length;
@@ -1115,25 +1162,38 @@ var CallMediaPlayer = class {
1115
1162
  this.streaming = false;
1116
1163
  this.finishTimer = setTimeout(
1117
1164
  () => this.reset(),
1118
- Math.max(0, this.nextTime - this.context.currentTime) * 1e3 + 30
1165
+ Math.max(
1166
+ 0,
1167
+ this.nextTime - this.context.currentTime + (this.independent && this.motionCurrent ? this.motionLag + 0.1 : 0)
1168
+ ) * 1e3 + 30
1119
1169
  );
1120
1170
  } else if (message.type === "fallback" || message.type === "stop") {
1121
1171
  this.reset(message.type === "fallback");
1122
1172
  }
1123
1173
  }
1124
1174
  sample() {
1125
- const now = this.context.currentTime;
1175
+ const now = this.context.currentTime - (this.independent ? this.motionLag : 0);
1126
1176
  const segment = this.segments.find((s) => now >= s.at && now < s.at + s.duration);
1127
- if (!segment) return null;
1128
- const frame = (now - segment.at) * 60;
1129
- const i = Math.min(Math.floor(frame), segment.frames.length - 1);
1130
- const a = segment.frames[i], b = segment.frames[Math.min(i + 1, segment.frames.length - 1)];
1177
+ if (!segment || this.independent && !this.motionCurrent) return null;
1178
+ let frames = segment.frames;
1179
+ let frame = (now - segment.at) * 60;
1180
+ if (this.independent) {
1181
+ const sourceSample = segment.startSample + (now - segment.at) * 24e3;
1182
+ const packet = this.motion.find(
1183
+ (p) => sourceSample >= p.startSample && sourceSample < p.startSample + p.frames.length * 400
1184
+ );
1185
+ if (!packet) return null;
1186
+ frames = packet.frames;
1187
+ frame = (sourceSample - packet.startSample) / 400;
1188
+ }
1189
+ const i = Math.min(Math.floor(frame), frames.length - 1);
1190
+ const a = frames[i], b = frames[Math.min(i + 1, frames.length - 1)];
1131
1191
  return a.map((value, j) => value + (b[j] - value) * (frame - i));
1132
1192
  }
1133
1193
  /** Call after the real mesh render, only with mouth/jaw controls actually applied to it. */
1134
1194
  reportMouthRenderSubmitted(controls) {
1135
1195
  if (this.renderedMouth) return;
1136
- const now = this.context.currentTime;
1196
+ const now = this.context.currentTime - (this.independent ? this.motionLag : 0);
1137
1197
  const segment = this.segments.find((s) => now >= s.at && now < s.at + s.duration);
1138
1198
  if (!segment) return;
1139
1199
  const mouth = Object.fromEntries(
@@ -1185,16 +1245,24 @@ var CallMediaPlayer = class {
1185
1245
  this.audible = value;
1186
1246
  this.changed();
1187
1247
  }
1188
- reset(releaseTrack = false) {
1248
+ setMotionEnabled(enabled) {
1249
+ this.motionRequested = enabled;
1250
+ if (!enabled) {
1251
+ this.motionCurrent = false;
1252
+ this.motion = [];
1253
+ }
1254
+ }
1255
+ reset(releaseTrack = false, preserveAudio = false) {
1189
1256
  clearTimeout(this.finishTimer);
1190
- for (const source of this.sources) {
1257
+ for (const source of preserveAudio ? [] : this.sources) {
1191
1258
  source.stop();
1192
1259
  source.disconnect();
1193
1260
  }
1194
- this.sources.clear();
1261
+ if (!preserveAudio) this.sources.clear();
1195
1262
  this.segments = [];
1263
+ this.motion = [];
1196
1264
  this.streaming = false;
1197
- this.setAudible(false);
1265
+ if (!preserveAudio) this.setAudible(false);
1198
1266
  if (releaseTrack) this.suppressesTrack = false;
1199
1267
  this.changed();
1200
1268
  }
@@ -1210,7 +1278,19 @@ var EVENTS = /* @__PURE__ */ new Set([
1210
1278
  "first_answer_text",
1211
1279
  "first_tts_request",
1212
1280
  "first_pcm",
1281
+ "native_speech_started",
1282
+ "mic_last_voiced_frame_forwarded",
1283
+ "native_speech_stopped",
1284
+ "native_response_created",
1285
+ "native_function_arguments_ready",
1286
+ "native_first_pcm_received",
1213
1287
  "first_pose",
1288
+ "tool_result_sent",
1289
+ "first_pcm_after_tool",
1290
+ "first_voiced_pcm_after_tool",
1291
+ "first_voiced_answer_pcm",
1292
+ "post_tool_audio_ambiguous",
1293
+ "first_pcm_after_answer_text",
1214
1294
  "animation_connect_started",
1215
1295
  "animation_ready"
1216
1296
  ]);
@@ -1246,34 +1326,59 @@ function checkedCallTimings(value, participant) {
1246
1326
  }
1247
1327
 
1248
1328
  // src/calls/livekit/useCallMedia.ts
1249
- function useCallMedia(enabled, key) {
1329
+ function useCallMedia(enabled, key, animationEnabled = true) {
1250
1330
  const room = componentsReact.useRoomContext();
1331
+ const animation = react.useRef(animationEnabled);
1332
+ animation.current = animationEnabled;
1333
+ const nativeAudio = react.useRef(null);
1334
+ const advertiseRef = react.useRef(() => {
1335
+ });
1251
1336
  const [players, setPlayers] = react.useState(/* @__PURE__ */ new Map());
1252
1337
  react.useEffect(() => {
1253
1338
  if (!enabled || !key || typeof AudioContext === "undefined") return;
1339
+ nativeAudio.current = null;
1254
1340
  const context = new AudioContext();
1255
1341
  const live = /* @__PURE__ */ new Map();
1256
1342
  const chains = /* @__PURE__ */ new Map();
1343
+ const failed2 = /* @__PURE__ */ new Set();
1257
1344
  let disposed = false;
1345
+ let motionFailed = false;
1258
1346
  const advertise = () => {
1259
1347
  if (!disposed && context.state === "running" && room.state === livekitClient.ConnectionState.Connected) {
1260
- void room.localParticipant.setAttributes({ "arbi.call_media": client.CALL_MEDIA_PROTOCOL }).catch(() => void 0);
1348
+ nativeAudio.current ??= animation.current;
1349
+ void room.localParticipant.setAttributes({
1350
+ "arbi.call_media": animation.current && !motionFailed && !failed2.size ? client.CALL_MEDIA_PROTOCOL : "",
1351
+ "arbi.call_media_native": nativeAudio.current && !failed2.size ? "arbi.call.media.independent.v1" : "rtc",
1352
+ "arbi.call_avatar": animation.current && nativeAudio.current && !motionFailed && !failed2.size ? "true" : ""
1353
+ }).catch(() => void 0);
1261
1354
  }
1262
1355
  };
1356
+ advertiseRef.current = advertise;
1263
1357
  const changed = () => {
1264
1358
  if (!disposed) setPlayers(new Map(live));
1265
1359
  };
1360
+ const fallback = (participant) => {
1361
+ if (participant.attributes["arbi.call_assistant"] !== "true" || participant.attributes["arbi.call_media_fallback"] !== "rtc")
1362
+ return;
1363
+ failed2.add(participant.identity);
1364
+ live.get(participant.identity)?.reset(true);
1365
+ advertise();
1366
+ };
1367
+ const attributesChanged = (_changed, participant) => fallback(participant);
1266
1368
  const received = (bytes, from, _kind, topic) => {
1267
1369
  if (topic !== client.CALL_MEDIA_PROTOCOL && topic !== CALL_TIMING_PROTOCOL || !from || from.attributes["arbi.call_assistant"] !== "true")
1268
1370
  return;
1371
+ fallback(from);
1372
+ if (failed2.has(from.identity)) return;
1269
1373
  const receivedAt = performance.now();
1270
1374
  const identity = from.identity;
1271
1375
  const chain = chains.get(identity) ?? Promise.resolve();
1272
1376
  chains.set(
1273
1377
  identity,
1274
1378
  chain.then(async () => {
1275
- if (disposed) return;
1379
+ if (disposed || failed2.has(identity)) return;
1276
1380
  const plaintext = await client.decryptCallMedia(bytes, key);
1381
+ if (disposed || failed2.has(identity)) return;
1277
1382
  let player = live.get(identity);
1278
1383
  if (!player) {
1279
1384
  player = new CallMediaPlayer(context, changed, (measurement) => {
@@ -1290,14 +1395,32 @@ function useCallMedia(enabled, key) {
1290
1395
  for (const event of events) player.timing(event, receivedAt, events.length);
1291
1396
  return;
1292
1397
  }
1293
- const message = checkedCallMedia(plaintext, identity);
1398
+ let message;
1399
+ try {
1400
+ message = checkedCallMedia(plaintext, identity);
1401
+ } catch (error) {
1402
+ if (!plaintext || typeof plaintext !== "object" || plaintext.type !== "animation")
1403
+ throw error;
1404
+ message = checkedCallMedia({ ...plaintext, type: "video-stop" }, identity);
1405
+ motionFailed = true;
1406
+ advertise();
1407
+ }
1408
+ if (message.type === "start" && (message.independent ? !nativeAudio.current : !animation.current))
1409
+ return;
1294
1410
  if (context.state !== "running") await context.resume();
1295
1411
  if (context.state !== "running") throw new Error("Call audio is unavailable");
1412
+ if (failed2.has(identity)) return;
1413
+ player.setMotionEnabled(animation.current && !motionFailed);
1296
1414
  player.push(message, receivedAt);
1297
1415
  }).catch(() => {
1298
1416
  if (topic === CALL_TIMING_PROTOCOL) return;
1417
+ failed2.add(identity);
1299
1418
  live.get(identity)?.reset(true);
1300
- void room.localParticipant.setAttributes({ "arbi.call_media": "" }).catch(() => void 0);
1419
+ void room.localParticipant.setAttributes({
1420
+ "arbi.call_media": "",
1421
+ "arbi.call_media_native": "",
1422
+ "arbi.call_avatar": ""
1423
+ }).catch(() => void 0);
1301
1424
  })
1302
1425
  );
1303
1426
  };
@@ -1307,6 +1430,7 @@ function useCallMedia(enabled, key) {
1307
1430
  changed();
1308
1431
  };
1309
1432
  room.on(livekitClient.RoomEvent.DataReceived, received);
1433
+ room.on(livekitClient.RoomEvent.ParticipantAttributesChanged, attributesChanged);
1310
1434
  room.on(livekitClient.RoomEvent.ParticipantDisconnected, removed);
1311
1435
  room.on(livekitClient.RoomEvent.Connected, advertise);
1312
1436
  room.on(livekitClient.RoomEvent.Reconnected, advertise);
@@ -1315,18 +1439,29 @@ function useCallMedia(enabled, key) {
1315
1439
  }).catch(() => void 0);
1316
1440
  return () => {
1317
1441
  disposed = true;
1442
+ advertiseRef.current = () => {
1443
+ };
1318
1444
  room.off(livekitClient.RoomEvent.DataReceived, received);
1445
+ room.off(livekitClient.RoomEvent.ParticipantAttributesChanged, attributesChanged);
1319
1446
  room.off(livekitClient.RoomEvent.ParticipantDisconnected, removed);
1320
1447
  room.off(livekitClient.RoomEvent.Connected, advertise);
1321
1448
  room.off(livekitClient.RoomEvent.Reconnected, advertise);
1322
1449
  if (room.state === livekitClient.ConnectionState.Connected) {
1323
- void room.localParticipant.setAttributes({ "arbi.call_media": "" }).catch(() => void 0);
1450
+ void room.localParticipant.setAttributes({
1451
+ "arbi.call_media": "",
1452
+ "arbi.call_media_native": "",
1453
+ "arbi.call_avatar": ""
1454
+ }).catch(() => void 0);
1324
1455
  }
1325
1456
  for (const player of live.values()) player.reset();
1326
1457
  void context.close();
1327
1458
  setPlayers(/* @__PURE__ */ new Map());
1328
1459
  };
1329
1460
  }, [enabled, key, room]);
1461
+ react.useEffect(() => {
1462
+ for (const player of players.values()) player.setMotionEnabled(animationEnabled);
1463
+ advertiseRef.current();
1464
+ }, [animationEnabled, players]);
1330
1465
  return players;
1331
1466
  }
1332
1467