@theoremjs/react 0.3.5-next.1036.g513f336 → 0.3.5-next.1037.g9282f21

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.
package/README.md CHANGED
@@ -501,6 +501,10 @@ This code opens `wss://<your host>/api/live/relay?profile=<id>`. To send your ow
501
501
 
502
502
  To get the messages your relay sends with `liveHostEnvelope`, give `useLiveCall` an `onHostMessage`. See [Send the page your own messages](https://github.com/masudl-hub/theoremai/blob/main/web/README.md#send-the-page-your-own-messages).
503
503
 
504
+ `startCall`, and the start buttons of `LiveRunner`, open the call's audio inside the press, before `connection` is called. When your own press waits before it calls `startCall`, call `call.prepareAudio()` first in that press: the call then uses that audio and does not open another. See [Open the audio in the press](https://github.com/masudl-hub/theoremai/blob/main/web/README.md#open-the-audio-in-the-press).
505
+
506
+ To know when a call ends, and why, give `useLiveCall` an `onCallEnded`. It is called once for each call the person starts, with a `LiveCallEnd`: `cancelled` when the person ends the call before it connects, `user` after it connects, `agent` when the other side closes it, and `failed` with the error. A call whose component unmounts ends as `user`, or `cancelled` before it connects. See [Know when the call ended](https://github.com/masudl-hub/theoremai/blob/main/web/README.md#know-when-the-call-ended).
507
+
504
508
  `LiveRunner` also accepts `theme`, `mode`, `labels`, `media`, `resolveMedia`, `mediaViewer`, and `trace`. Its captions show the media a tool returns as the chat does: see [Tool media](#tool-media). Tool gates in a call appear in a dialog over the call.
505
509
 
506
510
  ### A call across pages
@@ -571,6 +575,7 @@ The relay receives the absence duration. If the profile sets `live.resumed`, the
571
575
  The kernel validates native state before resumption. A protocol, provider, model, connection, key slot, or history mismatch rebuilds from portable history.
572
576
  It emits `provider_warning`. The client then discards the incompatible checkpoint and retains the history.
573
577
  A rebuilt session with history does not repeat the initial greeting.
578
+ When your relay knows that the call is new, for example a new call for a person who has history, set `opens: 'new'` on the `runSession` request, and the agent greets. `opens: 'continuing'` gives the resumed prompt after `afterMs`, as a reconnect does.
574
579
 
575
580
  A failed call does not reconnect. Without `live.sessionResumption`, a provider-ended call is complete.
576
581
 
@@ -60,6 +60,7 @@ export declare function useLiveCall({ iface, connection, video, ...options }: Li
60
60
  gatePrompt: import("@theoremjs/web/client").LiveToolGatePrompt | null;
61
61
  setTextDraft: import("react").Dispatch<import("react").SetStateAction<string>>;
62
62
  handleEnd: () => void;
63
+ prepareAudio: () => void;
63
64
  startCall: (options: {
64
65
  video: boolean;
65
66
  }) => void;
@@ -84,6 +84,9 @@ export function useLiveCall({ iface, connection, video, ...options }) {
84
84
  isMuted: ui.isMuted,
85
85
  voiceEnabled: voiceAvailable,
86
86
  }), [ui.activeTool, ui.connectPhase, ui.isMuted, ui.status, voiceAvailable]);
87
+ const onCallEndedRef = useRef(options.onCallEnded);
88
+ onCallEndedRef.current = options.onCallEnded;
89
+ const callEnded = useCallback((end) => onCallEndedRef.current?.(end), []);
87
90
  // why: One feed per call: every session it opens adds its records.
88
91
  const traces = useMemo(createTraceFeed, []);
89
92
  const { clientRef, ensureClient, clearClient } = useLiveSessionClient({
@@ -100,6 +103,7 @@ export function useLiveCall({ iface, connection, video, ...options }) {
100
103
  onTurnEvent: options.onTurnEvent,
101
104
  onSessionClosing: options.onSessionClosing,
102
105
  onSessionReady: options.onSessionReady,
106
+ onCallEnded: options.onCallEnded,
103
107
  onHostMessage: options.onHostMessage,
104
108
  voiceAvailable,
105
109
  traces,
@@ -134,6 +138,7 @@ export function useLiveCall({ iface, connection, video, ...options }) {
134
138
  sessionPermissionsRef: ui.sessionPermissionsRef,
135
139
  ensureClient,
136
140
  clearClient,
141
+ callEnded,
137
142
  cancelGateDecision: gate.cancelGateDecision,
138
143
  stopVideo: ui.stopVideo,
139
144
  resetCaptions: ui.resetCaptions,
@@ -225,6 +230,7 @@ export function useLiveCall({ iface, connection, video, ...options }) {
225
230
  gatePrompt: gate.gatePrompt,
226
231
  setTextDraft: ui.setTextDraft,
227
232
  handleEnd,
233
+ prepareAudio: controls.prepareAudio,
228
234
  startCall,
229
235
  handleRestart,
230
236
  handleSendText: controls.handleSendText,
@@ -1,4 +1,4 @@
1
- import type { LiveConnection, LiveConnectPhase, LiveSessionClient, LiveSessionStatus } from '@theoremjs/web/client';
1
+ import type { LiveCallEnd, LiveConnection, LiveConnectPhase, LiveSessionClient, LiveSessionStatus } from '@theoremjs/web/client';
2
2
  import { type Dispatch, type MutableRefObject, type SetStateAction } from 'react';
3
3
  import { type LiveCaptionState } from '../../client/live/live-captions.js';
4
4
  import { type LiveFacingMode, type LiveVideoCapture, type LiveVideoOptions } from '../../client/live/live-video.js';
@@ -14,6 +14,8 @@ export declare function useLiveRunnerControls(args: {
14
14
  sessionPermissionsRef: MutableRefObject<string[]>;
15
15
  ensureClient: (connection: LiveConnection) => LiveSessionClient;
16
16
  clearClient: () => void;
17
+ /** The host's `onCallEnded`, for a call that ends before its client exists. */
18
+ callEnded: (end: LiveCallEnd) => void;
17
19
  cancelGateDecision: (reason?: string) => void;
18
20
  stopVideo: () => void;
19
21
  resetCaptions: () => void;
@@ -38,6 +40,7 @@ export declare function useLiveRunnerControls(args: {
38
40
  setVideoFacingMode: Dispatch<SetStateAction<LiveFacingMode>>;
39
41
  setIsVideoOn: Dispatch<SetStateAction<boolean>>;
40
42
  }): {
43
+ prepareAudio: () => void;
41
44
  teardownSession: () => void;
42
45
  startSession: () => Promise<void>;
43
46
  handleSendText: (text?: string) => void;
@@ -1,9 +1,31 @@
1
- import { useCallback } from 'react';
1
+ import { prepareLiveAudio } from '@theoremjs/web/client';
2
+ import { useCallback, useEffect, useRef, } from 'react';
2
3
  import { applyLiveTranscript } from "../../client/live/live-captions.js";
3
4
  import { startLiveVideoCapture, } from "../../client/live/live-video.js";
4
5
  /** Media + session lifecycle handlers for LiveRunner. */
5
6
  export function useLiveRunnerControls(args) {
7
+ // invariant: A start still waiting on its connection is the open call: ending it ends that call.
8
+ const startingRef = useRef(null);
9
+ /** The audio output `prepareAudio` opened, until a start takes it. */
10
+ const preparedAudioRef = useRef(null);
11
+ const prepareAudio = useCallback(() => {
12
+ const held = preparedAudioRef.current;
13
+ if (held && held.state !== 'closed')
14
+ return;
15
+ preparedAudioRef.current = prepareLiveAudio();
16
+ }, []);
17
+ useEffect(() => () => {
18
+ releaseAudio(preparedAudioRef.current);
19
+ preparedAudioRef.current = null;
20
+ }, []);
6
21
  const teardownSession = useCallback(() => {
22
+ const starting = startingRef.current;
23
+ startingRef.current = null;
24
+ if (starting) {
25
+ starting.ended = true;
26
+ releaseAudio(starting.audio);
27
+ args.callEnded({ cause: 'cancelled' });
28
+ }
7
29
  args.cancelGateDecision();
8
30
  args.clearClient();
9
31
  args.stopVideo();
@@ -19,15 +41,53 @@ export function useLiveRunnerControls(args) {
19
41
  args.setOutputLevel(0);
20
42
  }, [args]);
21
43
  const startSession = useCallback(async () => {
44
+ // invariant: The audio opens before the first await, inside the press that started the call.
45
+ const held = preparedAudioRef.current;
46
+ preparedAudioRef.current = null;
22
47
  args.clearFailure();
23
48
  args.resetCaptions();
49
+ const failStart = (err) => {
50
+ args.reportFailure(err);
51
+ args.callEnded({
52
+ cause: 'failed',
53
+ error: err instanceof Error ? err : new Error(String(err)),
54
+ });
55
+ };
56
+ let audio;
57
+ try {
58
+ audio = held && held.state !== 'closed' ? held : prepareLiveAudio();
59
+ }
60
+ catch (err) {
61
+ failStart(err);
62
+ return;
63
+ }
64
+ const starting = { ended: false, audio };
65
+ startingRef.current = starting;
66
+ let connection;
67
+ try {
68
+ connection = await args.connectionRef.current();
69
+ }
70
+ catch (err) {
71
+ if (starting.ended)
72
+ return;
73
+ startingRef.current = null;
74
+ releaseAudio(audio);
75
+ failStart(err);
76
+ return;
77
+ }
78
+ if (starting.ended)
79
+ return;
80
+ startingRef.current = null;
24
81
  try {
25
- const liveClient = args.ensureClient(await args.connectionRef.current());
82
+ const liveClient = args.ensureClient(connection);
26
83
  if (args.statusRef.current === 'disconnected' || args.statusRef.current === 'error') {
27
- await liveClient.connect();
84
+ await liveClient.connect({ audio });
28
85
  }
86
+ else
87
+ releaseAudio(audio);
29
88
  }
30
89
  catch (err) {
90
+ releaseAudio(audio);
31
91
  args.reportFailure(err);
32
92
  }
33
93
  }, [args]);
@@ -86,6 +146,7 @@ export function useLiveRunnerControls(args) {
86
146
  await startSession();
87
147
  }, [startSession, teardownSession]);
88
148
  return {
149
+ prepareAudio,
89
150
  teardownSession,
90
151
  startSession,
91
152
  handleSendText,
@@ -95,3 +156,10 @@ export function useLiveRunnerControls(args) {
95
156
  handleRestart,
96
157
  };
97
158
  }
159
+ function releaseAudio(audio) {
160
+ if (!audio || audio.state === 'closed')
161
+ return;
162
+ audio.close().catch(() => {
163
+ // why: a context that closed meanwhile has nothing left to release.
164
+ });
165
+ }
@@ -68,6 +68,9 @@ export function useLiveSessionClient(bindings) {
68
68
  onSessionReady: (info) => {
69
69
  bindingsRef.current.onSessionReady?.(info);
70
70
  },
71
+ onCallEnded: (end) => {
72
+ bindingsRef.current.onCallEnded?.(end);
73
+ },
71
74
  ...(b.onHostMessage
72
75
  ? {
73
76
  onHostMessage: (message) => {
package/live.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- export type { LiveAudioOptions, LiveCallOptions, LiveClientTuning, LiveConnection, PageTool, PageToolAnswer, PageTools, } from '@theoremjs/web/client';
6
+ export type { LiveAudioOptions, LiveCallEnd, LiveCallOptions, LiveClientTuning, LiveConnection, PageTool, PageToolAnswer, PageTools, } from '@theoremjs/web/client';
7
7
  export type { LiveFacingMode, LiveVideoFrame, LiveVideoOptions, } from './client/live/live-video.js';
8
8
  export { type LiveVideoFit, type LiveVideoOverlay, type LiveVideoView, liveFramePointInPreview, } from './client/live/live-video-view.js';
9
9
  export { type LiveCall, type LiveCallInput, useLiveCall, } from './components/live/use-live-call.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/react",
3
- "version": "0.3.5-next.1036.g513f336",
3
+ "version": "0.3.5-next.1037.g9282f21",
4
4
  "description": "React bindings for Theorem: headless hooks + transport, an Astryx chat UI, and a Web-standard server handler.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -60,8 +60,8 @@
60
60
  "@stylexjs/stylex": "0.19.0",
61
61
  "react": "^19.0.0",
62
62
  "react-dom": "^19.0.0",
63
- "@theoremjs/agents": "^0.3.5-next.1036.g513f336",
64
- "@theoremjs/web": "^0.3.5-next.1036.g513f336"
63
+ "@theoremjs/agents": "^0.3.5-next.1037.g9282f21",
64
+ "@theoremjs/web": "^0.3.5-next.1037.g9282f21"
65
65
  },
66
66
  "peerDependenciesMeta": {
67
67
  "@astryxdesign/core": {