@lumavox/react 0.6.0 → 0.9.0

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
@@ -55,6 +55,7 @@ redial.
55
55
  | `session` | the session it opened, with its last messages |
56
56
  | `error` | `{ kind, message }`: the session, an `error` frame, the close, the microphone |
57
57
  | `speaking` `recording` | the adapter is playing; the recorder is open |
58
+ | `interrupted` | the device has the audio — a call, Siri, another app |
58
59
  | `send` `startAudio` `sendAudio` `endAudio` `cancel` `mute` `reconnect` `close` | the conversation's |
59
60
  | `startRecording` `stopRecording({ transcribeOnly, discard })` | through the `audio` adapter |
60
61
  | `newSession()` | a new conversation with the same agent |
@@ -67,9 +68,13 @@ running the latest render's handler. **`audio`**, an `AgentAudio` — a player a
67
68
  recorder — makes replies audible and `startRecording` work: replies play
68
69
  as they arrive and stop on `cancel`, `mute` or a new message; recording
69
70
  opens the microphone first and the recording at the rate it was granted.
70
- Without an adapter or an `onAudio`, no speech is asked for. The SDK ships
71
- no player or recorder in this release: write one on Web Audio, or on your
72
- React Native audio library.
71
+ Without an adapter or an `onAudio`, no speech is asked for. In a browser,
72
+ `createWebAudio()` from `@lumavox/client/web-audio` is one — call its
73
+ `prime()` in the click that starts the conversation; on React Native,
74
+ `@lumavox/react-native` is one install with these hooks and `createAudio()`.
75
+ An adapter that tells an interruption — a call, Siri, the app leaving the
76
+ screen — has the recording in progress thrown away, `interrupted` set, and
77
+ `onAudioInterrupted` and `onAudioResumed` called.
73
78
 
74
79
  ## Reading
75
80
 
@@ -12,6 +12,11 @@
12
12
  * *then* the recording, at the rate the recorder was granted, so a
13
13
  * refused microphone never opens a window on the socket. The adapter is
14
14
  * the app's: the hook never disposes it.
15
+ * - **An interruption throws the recording away**: when the adapter says
16
+ * the device took the audio (`onInterruption`) — a call, Siri, the app
17
+ * leaving the screen — a recording open is discarded as
18
+ * `stopRecording({ discard: true })` would, and `interrupted` is true
19
+ * until the adapter gives the audio back.
15
20
  * - **A late answer is dropped quietly**: a session opened after unmount
16
21
  * is never connected, and a press released while the microphone was
17
22
  * still opening gives it back.
@@ -34,6 +39,10 @@ export interface UseAgentOptions extends ConversationHandlers, ConnectOptions {
34
39
  client?: Client;
35
40
  /** A player and a recorder: replies are played, and `startRecording` records. Yours to dispose. */
36
41
  audio?: AgentAudio;
42
+ /** The device took the audio away — a call, Siri, another app: playback paused, a recording thrown away. */
43
+ onAudioInterrupted?: () => void;
44
+ /** The device gave the audio back: playback goes on where it stopped. */
45
+ onAudioResumed?: () => void;
37
46
  }
38
47
  /** What went wrong last; cleared by the next turn. */
39
48
  export type AgentError =
@@ -81,6 +90,8 @@ export interface AgentControls extends Snapshot {
81
90
  speaking: boolean;
82
91
  /** The recorder is open. */
83
92
  recording: boolean;
93
+ /** The device has the audio — a call, Siri, another app: playback is paused until it gives it back. */
94
+ interrupted: boolean;
84
95
  /** The conversation itself, once it is made. */
85
96
  conversation: Conversation | null;
86
97
  send(text: string, options?: TurnOptions): Promise<TurnOutcome>;
package/dist/use-agent.js CHANGED
@@ -12,6 +12,11 @@
12
12
  * *then* the recording, at the rate the recorder was granted, so a
13
13
  * refused microphone never opens a window on the socket. The adapter is
14
14
  * the app's: the hook never disposes it.
15
+ * - **An interruption throws the recording away**: when the adapter says
16
+ * the device took the audio (`onInterruption`) — a call, Siri, the app
17
+ * leaving the screen — a recording open is discarded as
18
+ * `stopRecording({ discard: true })` would, and `interrupted` is true
19
+ * until the adapter gives the audio back.
15
20
  * - **A late answer is dropped quietly**: a session opened after unmount
16
21
  * is never connected, and a press released while the microphone was
17
22
  * still opening gives it back.
@@ -74,6 +79,7 @@ export function useAgent(target, options = {}) {
74
79
  const [error, setError] = useState(null);
75
80
  const [speaking, setSpeaking] = useState(false);
76
81
  const [recording, setRecording] = useState(false);
82
+ const [interrupted, setInterrupted] = useState(false);
77
83
  const [failed, setFailed] = useState(false);
78
84
  const current = useRef(null);
79
85
  const waiting = useRef([]);
@@ -348,6 +354,21 @@ export function useAgent(target, options = {}) {
348
354
  current.current?.close();
349
355
  },
350
356
  }), [connected, hush]);
357
+ // The device took the audio, or gave it back: an open recording is
358
+ // thrown away, its speaker cut off mid-sentence.
359
+ const { stopRecording } = controls;
360
+ useEffect(() => {
361
+ setInterrupted(false);
362
+ return audio?.onInterruption?.((now) => {
363
+ setInterrupted(now);
364
+ if (now) {
365
+ void stopRecording({ discard: true });
366
+ latest.current.onAudioInterrupted?.();
367
+ }
368
+ else
369
+ latest.current.onAudioResumed?.();
370
+ });
371
+ }, [audio, stopRecording]);
351
372
  const readyAgent = snapshot.ready?.agent_id ?? null;
352
373
  const newSession = useCallback(() => {
353
374
  const agent = agentId ?? readyAgent;
@@ -365,6 +386,7 @@ export function useAgent(target, options = {}) {
365
386
  error,
366
387
  speaking,
367
388
  recording,
389
+ interrupted,
368
390
  conversation,
369
391
  ...controls,
370
392
  newSession,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumavox/react",
3
- "version": "0.6.0",
3
+ "version": "0.9.0",
4
4
  "description": "React and React Native hooks for Lumavox agents: a conversation that keeps itself connected, sessions, memory and relations, over @lumavox/client.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -29,7 +29,7 @@
29
29
  "typecheck": "tsc --noEmit && tsc --noEmit -p test"
30
30
  },
31
31
  "dependencies": {
32
- "@lumavox/client": "0.6.0"
32
+ "@lumavox/client": "0.9.0"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "react": ">=18"