@oaiz/app-kit 0.0.0-stage → 0.1.4128

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 (107) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +143 -2
  3. package/THIRD_PARTY_NOTICES.md +76 -0
  4. package/package.json +139 -4
  5. package/registry.json +1847 -0
  6. package/src/components/activity-timeline.tsx +227 -0
  7. package/src/components/app-shell.tsx +622 -0
  8. package/src/components/bar-list.tsx +47 -0
  9. package/src/components/board.tsx +700 -0
  10. package/src/components/calendar-view.tsx +1388 -0
  11. package/src/components/card-grid.tsx +462 -0
  12. package/src/components/charts.tsx +701 -0
  13. package/src/components/chat-markdown.tsx +230 -0
  14. package/src/components/chat-panel.tsx +760 -0
  15. package/src/components/code-editor.tsx +276 -0
  16. package/src/components/collection-controls.tsx +226 -0
  17. package/src/components/command-palette.tsx +403 -0
  18. package/src/components/confirm-action.tsx +123 -0
  19. package/src/components/data-table.tsx +821 -0
  20. package/src/components/date-input.tsx +429 -0
  21. package/src/components/empty-state.tsx +31 -0
  22. package/src/components/error-state.tsx +37 -0
  23. package/src/components/file-upload.tsx +595 -0
  24. package/src/components/filter-bar.tsx +415 -0
  25. package/src/components/grouped-picker.tsx +250 -0
  26. package/src/components/line-items.tsx +601 -0
  27. package/src/components/list-view.tsx +491 -0
  28. package/src/components/metrics.tsx +53 -0
  29. package/src/components/page-header.tsx +33 -0
  30. package/src/components/property-picker.tsx +546 -0
  31. package/src/components/record-detail.tsx +416 -0
  32. package/src/components/record-form.tsx +295 -0
  33. package/src/components/record-name.tsx +85 -0
  34. package/src/components/save-bar.tsx +142 -0
  35. package/src/components/sign-in.tsx +179 -0
  36. package/src/components/split-view.tsx +128 -0
  37. package/src/components/status-dot.tsx +56 -0
  38. package/src/components/stepper.tsx +153 -0
  39. package/src/components/text-diff-model.ts +111 -0
  40. package/src/components/text-diff.tsx +98 -0
  41. package/src/components/time-zone-picker.tsx +69 -0
  42. package/src/components/ui/accordion.tsx +138 -0
  43. package/src/components/ui/alert-dialog.tsx +67 -0
  44. package/src/components/ui/alert.tsx +65 -0
  45. package/src/components/ui/avatar-group.tsx +119 -0
  46. package/src/components/ui/avatar.tsx +55 -0
  47. package/src/components/ui/badge.tsx +59 -0
  48. package/src/components/ui/bottom-navigation.tsx +102 -0
  49. package/src/components/ui/breadcrumb.tsx +117 -0
  50. package/src/components/ui/button.tsx +71 -0
  51. package/src/components/ui/card.tsx +95 -0
  52. package/src/components/ui/checkbox.tsx +31 -0
  53. package/src/components/ui/collapsible.tsx +27 -0
  54. package/src/components/ui/context-menu.tsx +270 -0
  55. package/src/components/ui/count-up.tsx +136 -0
  56. package/src/components/ui/detail-disclosure-section.tsx +109 -0
  57. package/src/components/ui/dialog.tsx +157 -0
  58. package/src/components/ui/dropdown-menu.tsx +273 -0
  59. package/src/components/ui/form-field.tsx +155 -0
  60. package/src/components/ui/heading.tsx +126 -0
  61. package/src/components/ui/hover-card.tsx +45 -0
  62. package/src/components/ui/input.tsx +50 -0
  63. package/src/components/ui/label.tsx +30 -0
  64. package/src/components/ui/orb-spinner.tsx +238 -0
  65. package/src/components/ui/popover.tsx +59 -0
  66. package/src/components/ui/progress-ring.tsx +125 -0
  67. package/src/components/ui/progress.tsx +38 -0
  68. package/src/components/ui/radio-group.tsx +124 -0
  69. package/src/components/ui/resizable.tsx +48 -0
  70. package/src/components/ui/scroll-fade.tsx +98 -0
  71. package/src/components/ui/segmented-control.tsx +70 -0
  72. package/src/components/ui/select.tsx +230 -0
  73. package/src/components/ui/separator.tsx +29 -0
  74. package/src/components/ui/sheet.tsx +170 -0
  75. package/src/components/ui/shimmer-text.tsx +87 -0
  76. package/src/components/ui/shortcut.tsx +39 -0
  77. package/src/components/ui/skeleton.tsx +15 -0
  78. package/src/components/ui/slider.tsx +48 -0
  79. package/src/components/ui/switch.tsx +44 -0
  80. package/src/components/ui/table.tsx +119 -0
  81. package/src/components/ui/tabs.tsx +80 -0
  82. package/src/components/ui/text.tsx +93 -0
  83. package/src/components/ui/textarea.tsx +51 -0
  84. package/src/components/ui/toast.tsx +141 -0
  85. package/src/components/ui/toaster.tsx +40 -0
  86. package/src/components/ui/tooltip.tsx +38 -0
  87. package/src/components/ui/top-banner.tsx +108 -0
  88. package/src/components/ui/tree-view.tsx +215 -0
  89. package/src/components/ui/use-toast.tsx +184 -0
  90. package/src/components/ui/voice-orb.tsx +252 -0
  91. package/src/hooks/oaiz-chat.tsx +530 -0
  92. package/src/hooks/oaiz-data.tsx +617 -0
  93. package/src/hooks/oaiz-session.tsx +63 -0
  94. package/src/hooks/use-arrivals.ts +19 -0
  95. package/src/hooks/use-breakpoint.ts +100 -0
  96. package/src/hooks/use-media-query.ts +36 -0
  97. package/src/hooks/use-theme.ts +67 -0
  98. package/src/hooks/use-url-state.ts +77 -0
  99. package/src/lib/breakpoints.ts +81 -0
  100. package/src/lib/csp-nonce.ts +15 -0
  101. package/src/lib/format.ts +291 -0
  102. package/src/lib/speech-level.ts +118 -0
  103. package/src/lib/utils.ts +109 -0
  104. package/src/styles/oaiz/app.css +21 -0
  105. package/src/styles/oaiz/scrollbars.css +20 -0
  106. package/src/styles/oaiz/tokens.css +952 -0
  107. package/src/styles/oaiz/voice-orb.css +39 -0
@@ -0,0 +1,530 @@
1
+ import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
2
+ import type { OaizChat, OaizChatMessage, OaizError, OaizVoiceSnapshot, OaizVoiceTurn, OaizVoiceTurnSource } from '@oaiz/sdk';
3
+
4
+ /** One event of a turn's stream, as `client.chat.send` yields it. */
5
+ export type OaizChatEvent =
6
+ ReturnType<OaizChat['send']> extends AsyncIterable<infer Frame extends { data: unknown }>
7
+ ? NonNullable<Frame['data']>
8
+ : never;
9
+
10
+ type ChatMessageRow = NonNullable<OaizChatEvent['message']>;
11
+
12
+ /** `waiting`: the call has not run and waits for the member's decision. */
13
+ export type ChatToolState = 'running' | 'done' | 'failed' | 'stopped' | 'waiting';
14
+
15
+ /** A tool call the turn stopped on: it has not run, and runs only when the member approves it. */
16
+ export type PendingToolCall = {
17
+ toolCallId: string;
18
+ toolName: string;
19
+ /** The call's label shown to people. */
20
+ displayName?: string;
21
+ /** The arguments the call runs with when approved. */
22
+ args: Record<string, unknown>;
23
+ };
24
+
25
+ /** One row of a chat: a member's message, an agent's answer, its reasoning, or a tool it called. */
26
+ export type ChatItem =
27
+ | { kind: 'user'; id: string; content: string; createdAt?: string }
28
+ | { kind: 'answer'; id: string; content: string; streaming: boolean; createdAt?: string }
29
+ | { kind: 'reasoning'; id: string; content: string; streaming: boolean; durationMs?: number }
30
+ | {
31
+ kind: 'tool';
32
+ id: string;
33
+ name: string;
34
+ state: ChatToolState;
35
+ durationMs?: number;
36
+ input?: Record<string, unknown>;
37
+ output?: Record<string, unknown>;
38
+ error?: string;
39
+ };
40
+
41
+ /** `waiting`: the agent works and writes nothing yet. `streaming`: the answer is arriving. */
42
+ export type OaizChatStatus = 'idle' | 'waiting' | 'streaming';
43
+
44
+ export type OaizChatState = {
45
+ items: ChatItem[];
46
+ status: OaizChatStatus;
47
+ /** The chat to continue; `null` until `session.created` names a new chat. */
48
+ sessionId: string | null;
49
+ /** The saved messages of `sessionId` are loading. */
50
+ loading: boolean;
51
+ /** Why the last message or the last load failed. */
52
+ error: OaizError | null;
53
+ /** The call the turn stopped on, until the member approves or declines it. A message waits for it. */
54
+ pending: PendingToolCall | null;
55
+ };
56
+
57
+ const NEW_CHAT: OaizChatState = {
58
+ items: [],
59
+ status: 'idle',
60
+ sessionId: null,
61
+ loading: false,
62
+ error: null,
63
+ pending: null,
64
+ };
65
+
66
+ function toolLabel(toolName: string | undefined): string {
67
+ if (!toolName) return 'Tool';
68
+ const words = toolName.replace(/[_-]+/g, ' ').trim();
69
+ return words.charAt(0).toUpperCase() + words.slice(1);
70
+ }
71
+
72
+ /** A tool's result without the fields OAIZ adds to every result. */
73
+ function toolOutput(result: Record<string, unknown> | undefined): Record<string, unknown> | undefined {
74
+ if (!result) return undefined;
75
+ const { durationMs: _durationMs, cost: _cost, ...output } = result;
76
+ return output;
77
+ }
78
+
79
+ function toItem(message: ChatMessageRow): ChatItem | null {
80
+ const { id, content, createdAt, latencyMs } = message;
81
+ if (message.role === 'user') {
82
+ return message.messageType === undefined || message.messageType === 'user_input'
83
+ ? { kind: 'user', id, content, createdAt }
84
+ : null;
85
+ }
86
+ switch (message.messageType) {
87
+ case 'ai_response':
88
+ return { kind: 'answer', id, content, streaming: false, createdAt };
89
+ case 'thinking':
90
+ return { kind: 'reasoning', id, content, streaming: false, durationMs: latencyMs };
91
+ case 'tool_started':
92
+ case 'tool_finished': {
93
+ const call = message.artifacts?.find((artifact) => artifact.toolCallResult)?.toolCallResult;
94
+ const name = call?.displayName || toolLabel(call?.toolName);
95
+ const finished = message.messageType === 'tool_finished';
96
+ const durationMs = call?.result?.durationMs;
97
+ return {
98
+ kind: 'tool',
99
+ id,
100
+ name,
101
+ state: !finished ? 'running' : call?.success === false ? 'failed' : 'done',
102
+ durationMs: typeof durationMs === 'number' ? durationMs : undefined,
103
+ input: call?.args,
104
+ output: finished ? toolOutput(call?.result) : undefined,
105
+ error: finished ? call?.error : undefined,
106
+ };
107
+ }
108
+ default:
109
+ return null;
110
+ }
111
+ }
112
+
113
+ function upsert(items: ChatItem[], item: ChatItem, clientMessageId?: string): ChatItem[] {
114
+ const index = items.findIndex(
115
+ (each) => each.id === item.id || each.id === clientMessageId,
116
+ );
117
+ if (index < 0) return [...items, item];
118
+ return items.map((each, at) => (at === index ? item : each));
119
+ }
120
+
121
+ /**
122
+ * Ends what a turn left open: unfinished text stops streaming, and a running tool has stopped. When
123
+ * the last turn stopped on a call for the member's decision, that turn's unfinished calls wait.
124
+ */
125
+ function settled(items: ChatItem[], awaitingDecision = false): ChatItem[] {
126
+ const lastTurn = awaitingDecision ? items.map((item) => item.kind).lastIndexOf('user') + 1 : items.length;
127
+ return items.map((item, at) => {
128
+ if ((item.kind === 'answer' || item.kind === 'reasoning') && item.streaming) {
129
+ return { ...item, streaming: false };
130
+ }
131
+ if (item.kind === 'tool' && item.state === 'running') {
132
+ return { ...item, state: at >= lastTurn ? 'waiting' : 'stopped' };
133
+ }
134
+ return item;
135
+ });
136
+ }
137
+
138
+ function toPendingToolCall(call: {
139
+ toolCallId?: string | null;
140
+ toolName?: string | null;
141
+ displayName?: string | null;
142
+ args?: Record<string, unknown> | null;
143
+ }): PendingToolCall | null {
144
+ if (!call.toolCallId || !call.toolName) return null;
145
+ return {
146
+ toolCallId: call.toolCallId,
147
+ toolName: call.toolName,
148
+ displayName: call.displayName || undefined,
149
+ args: call.args ?? {},
150
+ };
151
+ }
152
+
153
+ function applyDelta(
154
+ state: OaizChatState,
155
+ delta: NonNullable<OaizChatEvent['delta']>,
156
+ messageId: string,
157
+ ): OaizChatState {
158
+ const known = state.items.find((item) => item.id === messageId);
159
+ const before = known?.kind === 'answer' || known?.kind === 'reasoning' ? known.content : '';
160
+ const content = before + (delta.content ?? '');
161
+ const streaming = !delta.isFinal;
162
+ if (delta.targetType === 'thinking') {
163
+ return {
164
+ ...state,
165
+ items: upsert(state.items, {
166
+ kind: 'reasoning',
167
+ id: messageId,
168
+ content,
169
+ streaming,
170
+ durationMs: delta.latencyMs ?? undefined,
171
+ }),
172
+ };
173
+ }
174
+ return {
175
+ ...state,
176
+ items: upsert(state.items, {
177
+ kind: 'answer',
178
+ id: messageId,
179
+ content,
180
+ streaming,
181
+ createdAt: known?.kind === 'answer' ? known.createdAt : new Date().toISOString(),
182
+ }),
183
+ status: 'streaming',
184
+ };
185
+ }
186
+
187
+ /** The chat after one event of a turn's stream. Events a chat does not display change nothing. */
188
+ export function applyChatEvent(state: OaizChatState, event: OaizChatEvent): OaizChatState {
189
+ switch (event.type) {
190
+ case 'session.created':
191
+ return event.sessionId ? { ...state, sessionId: event.sessionId } : state;
192
+ case 'message.created':
193
+ case 'message.updated': {
194
+ const item = event.message ? toItem(event.message) : null;
195
+ if (item === null) return state;
196
+ return {
197
+ ...state,
198
+ items: upsert(state.items, item, event.message?.clientMessageId),
199
+ status: item.kind === 'tool' ? 'waiting' : state.status,
200
+ };
201
+ }
202
+ case 'generation.started':
203
+ // A decision's turn starts only once OAIZ has taken the decision.
204
+ return state.pending ? { ...state, pending: null } : state;
205
+ case 'tool.approvalRequired':
206
+ return event.toolApprovalInfo
207
+ ? { ...state, pending: toPendingToolCall(event.toolApprovalInfo) }
208
+ : state;
209
+ case 'message.delta':
210
+ return event.delta?.messageId
211
+ ? applyDelta(state, event.delta, event.delta.messageId)
212
+ : state;
213
+ case 'error':
214
+ return {
215
+ ...state,
216
+ error: {
217
+ message: event.error?.message ?? 'The agent could not answer.',
218
+ code: event.error?.code ?? '',
219
+ hints: [],
220
+ },
221
+ };
222
+ default:
223
+ return state;
224
+ }
225
+ }
226
+
227
+ /** How many recent turns the voice model is told about when a call starts. */
228
+ const RECENT_TURNS = 6;
229
+ /** Longest line handed over per turn; the whole text has to fit one provider append of at most 500 tokens. */
230
+ const MAX_LINE_CHARS = 240;
231
+
232
+ /** The chat's recent messages as plain text, for the voice model when a call starts on them. */
233
+ export function renderRecentChatForVoice(items: ChatItem[]): string {
234
+ const lines = items.flatMap((item) => {
235
+ if (item.kind !== 'user' && item.kind !== 'answer') return [];
236
+ const content = item.content.replace(/\s+/g, ' ').trim();
237
+ if (!content) return [];
238
+ const clipped = content.length > MAX_LINE_CHARS ? `${content.slice(0, MAX_LINE_CHARS)}…` : content;
239
+ return [`${item.kind === 'user' ? 'User' : 'Assistant'}: ${clipped}`];
240
+ });
241
+ return lines.slice(-RECENT_TURNS).join('\n');
242
+ }
243
+
244
+ /** Set once, at the hook's first render, to offer voice. */
245
+ export type OaizChatVoiceOptions = {
246
+ /** What the app is, for the voice model; at most 2,000 characters. */
247
+ instructions?: string;
248
+ /**
249
+ * The native tools a spoken turn may call. The chat's tools do not carry over: the microphone
250
+ * hears the room, and a named write tool runs at once on what it heard. Name read tools. A
251
+ * message typed during a call uses the chat's tools.
252
+ */
253
+ nativeTools?: OaizChatMessage['nativeTools'];
254
+ /** The integration actions a spoken turn may call, named on the same terms as `nativeTools`. */
255
+ integrationTools?: OaizChatMessage['integrationTools'];
256
+ };
257
+
258
+ export type OaizChatVoiceControls = Pick<
259
+ OaizVoiceSnapshot,
260
+ 'state' | 'assistantSpeech' | 'outputAnalyser' | 'muted' | 'error'
261
+ > & {
262
+ /** Opens the call on this chat. A refusal arrives as `error`. */
263
+ start: () => Promise<void>;
264
+ /** Ends the call. A turn in progress finishes into the chat. */
265
+ stop: () => void;
266
+ toggleMute: () => void;
267
+ };
268
+
269
+ const NO_VOICE: OaizVoiceSnapshot = {
270
+ state: 'idle',
271
+ assistantSpeech: '',
272
+ outputAnalyser: null,
273
+ muted: false,
274
+ error: null,
275
+ sessionId: null,
276
+ turnRunning: false,
277
+ };
278
+ const noVoice = () => NO_VOICE;
279
+ const subscribeToNothing = () => () => undefined;
280
+
281
+ export type UseOaizChatOptions = Pick<
282
+ OaizChatMessage,
283
+ 'systemPrompt' | 'nativeTools' | 'integrationTools' | 'model' | 'providerKeyUuid'
284
+ > & {
285
+ /** `client.chat` of the app's `@oaiz/sdk` client, created once at module scope. */
286
+ chat: Pick<OaizChat, 'send' | 'approve' | 'decline' | 'sessions' | 'voice'>;
287
+ /** A saved chat to open first; omit it to start a new one. */
288
+ sessionId?: string;
289
+ /** Offers voice, with the tools a spoken turn may call. */
290
+ voice?: OaizChatVoiceOptions;
291
+ };
292
+
293
+ export type OaizChatControls = OaizChatState & {
294
+ /** Sends one member message. It does nothing while a turn runs or a call waits for a decision. */
295
+ send: (content: string) => Promise<void>;
296
+ /** Runs the pending call with its saved arguments and streams the rest of the turn. */
297
+ approve: () => Promise<void>;
298
+ /** Tells the agent the member declined: the pending call does not run, and the turn goes on. */
299
+ decline: () => Promise<void>;
300
+ /** Closes the stream, which stops the turn. What the turn saved stays in the chat. */
301
+ stop: () => void;
302
+ /**
303
+ * Sends the failed message again, or loads the chat again when loading it or a decision failed.
304
+ */
305
+ retry: () => Promise<void>;
306
+ /** Opens a saved chat, such as one from `chat.sessions.list()`. */
307
+ openSession: (sessionId: string) => Promise<void>;
308
+ /** Starts an empty chat. */
309
+ newChat: () => void;
310
+ /** The voice call on this chat; null unless the `voice` option is set. */
311
+ voice: OaizChatVoiceControls | null;
312
+ };
313
+
314
+ /** One chat with the app's agent over `client.chat`, as rows the chat panel displays. */
315
+ export function useOaizChat({
316
+ chat,
317
+ sessionId: initialSession,
318
+ voice: voiceOptions,
319
+ ...turn
320
+ }: UseOaizChatOptions): OaizChatControls {
321
+ const [state, setState] = useState<OaizChatState>({
322
+ ...NEW_CHAT,
323
+ sessionId: initialSession ?? null,
324
+ loading: initialSession !== undefined,
325
+ });
326
+ const running = useRef<AbortController | null>(null);
327
+ const latestOpen = useRef(0);
328
+ // A failed decision is not sent again: OAIZ may have taken it, so retry reloads the chat.
329
+ const lastWasDecision = useRef(false);
330
+
331
+ // The chat's own tools stay out of a spoken turn: only the voice option names them. A message
332
+ // typed during a call is the member's own, so it keeps the chat's tools.
333
+ const voiceTurns = useRef<Record<OaizVoiceTurnSource, OaizVoiceTurn>>({
334
+ spoken: { systemPrompt: turn.systemPrompt },
335
+ typed: turn,
336
+ });
337
+ useEffect(() => {
338
+ voiceTurns.current = {
339
+ spoken: {
340
+ systemPrompt: turn.systemPrompt,
341
+ model: turn.model,
342
+ providerKeyUuid: turn.providerKeyUuid,
343
+ nativeTools: voiceOptions?.nativeTools,
344
+ integrationTools: voiceOptions?.integrationTools,
345
+ },
346
+ typed: turn,
347
+ };
348
+ });
349
+ const [voiceClient] = useState(() =>
350
+ voiceOptions === undefined
351
+ ? null
352
+ : chat.voice({
353
+ instructions: voiceOptions.instructions,
354
+ turn: (source) => voiceTurns.current[source],
355
+ onEvent: (event) => setState((current) => applyChatEvent(current, event)),
356
+ }),
357
+ );
358
+ const voice = useSyncExternalStore(
359
+ voiceClient?.subscribe ?? subscribeToNothing,
360
+ voiceClient?.getSnapshot ?? noVoice,
361
+ );
362
+
363
+ // A voice turn has no `run` around it, so its start and end come from the voice client.
364
+ const voiceTurnRan = useRef(false);
365
+ useEffect(() => {
366
+ if (voice.turnRunning === voiceTurnRan.current) return;
367
+ voiceTurnRan.current = voice.turnRunning;
368
+ if (voice.turnRunning) {
369
+ setState((current) => ({ ...current, error: null, status: 'waiting' }));
370
+ } else if (running.current === null) {
371
+ setState((current) => ({ ...current, status: 'idle', items: settled(current.items, current.pending !== null) }));
372
+ }
373
+ }, [voice.turnRunning]);
374
+
375
+ /** Ends the call and its turns, because the chat they belong to is leaving the view. */
376
+ function leaveVoice() {
377
+ voiceClient?.stopAllTurns();
378
+ void voiceClient?.stop();
379
+ }
380
+
381
+ /** Leaves the running turn behind, so that it changes nothing more in the chat. */
382
+ function abandonTurn() {
383
+ running.current?.abort();
384
+ running.current = null;
385
+ leaveVoice();
386
+ }
387
+
388
+ async function openSession(sessionId: string) {
389
+ abandonTurn();
390
+ const open = ++latestOpen.current;
391
+ setState({ ...NEW_CHAT, sessionId, loading: true });
392
+ const answer = await chat.sessions.get(sessionId);
393
+ if (open !== latestOpen.current) return;
394
+ if (answer.error) {
395
+ setState({ ...NEW_CHAT, sessionId, error: answer.error });
396
+ return;
397
+ }
398
+ const items = (answer.data.messages ?? []).flatMap((message) => toItem(message) ?? []);
399
+ const pending = answer.data.pendingApproval ? toPendingToolCall(answer.data.pendingApproval) : null;
400
+ setState({ ...NEW_CHAT, sessionId, items: settled(items, pending !== null), pending });
401
+ }
402
+
403
+ useEffect(() => {
404
+ if (initialSession !== undefined) void openSession(initialSession);
405
+ // Later changes go through `openSession`.
406
+ // eslint-disable-next-line react-hooks/exhaustive-deps
407
+ }, []);
408
+
409
+ useEffect(
410
+ () => () => {
411
+ running.current?.abort();
412
+ leaveVoice();
413
+ },
414
+ // `voiceClient` is set once; `running` is a ref.
415
+ // eslint-disable-next-line react-hooks/exhaustive-deps
416
+ [],
417
+ );
418
+
419
+ async function run(content: string, base: ChatItem[]) {
420
+ if (running.current !== null || state.pending !== null) return;
421
+ const controller = new AbortController();
422
+ running.current = controller;
423
+ lastWasDecision.current = false;
424
+ // Each attempt has its own id, because OAIZ runs a client message id once.
425
+ const clientMessageId = crypto.randomUUID();
426
+ setState((current) => ({
427
+ ...current,
428
+ error: null,
429
+ status: 'waiting',
430
+ items: [
431
+ ...base,
432
+ { kind: 'user', id: clientMessageId, content, createdAt: new Date().toISOString() },
433
+ ],
434
+ }));
435
+ await follow(
436
+ chat.send(
437
+ { ...turn, content, clientMessageId, sessionId: state.sessionId ?? undefined },
438
+ { signal: controller.signal },
439
+ ),
440
+ controller,
441
+ );
442
+ }
443
+
444
+ /**
445
+ * Continues the turn that waits on the pending call, with the chat's tools as the message had them.
446
+ * The call stays pending until the continued turn starts, so a refused decision can be made again.
447
+ */
448
+ async function decide(approved: boolean) {
449
+ const { pending, sessionId } = state;
450
+ if (running.current !== null || pending === null || sessionId === null) return;
451
+ const controller = new AbortController();
452
+ running.current = controller;
453
+ lastWasDecision.current = true;
454
+ setState((current) => ({ ...current, error: null, status: 'waiting' }));
455
+ const decision = { ...turn, sessionId, toolCallId: pending.toolCallId };
456
+ const options = { signal: controller.signal };
457
+ await follow(approved ? chat.approve(decision, options) : chat.decline(decision, options), controller);
458
+ }
459
+
460
+ /** Applies a turn's events to the chat until its stream ends. */
461
+ async function follow(frames: ReturnType<OaizChat['send']>, controller: AbortController) {
462
+ for await (const frame of frames) {
463
+ // Closing the stream answers with a failure that the member asked for.
464
+ if (controller.signal.aborted) break;
465
+ const { data: event, error } = frame;
466
+ setState((current) =>
467
+ error ? { ...current, error } : applyChatEvent(current, event),
468
+ );
469
+ }
470
+ if (running.current !== controller) return;
471
+ running.current = null;
472
+ setState((current) => ({
473
+ ...current,
474
+ status: 'idle',
475
+ items: settled(current.items, current.pending !== null),
476
+ }));
477
+ }
478
+
479
+ return {
480
+ ...state,
481
+ send: async (content) => {
482
+ if (state.pending !== null) return;
483
+ // A message typed during a call is answered in speech too.
484
+ if (voiceClient?.getSnapshot().state === 'live') voiceClient.send(content);
485
+ else await run(content, state.items);
486
+ },
487
+ approve: () => decide(true),
488
+ decline: () => decide(false),
489
+ stop: () => {
490
+ running.current?.abort();
491
+ voiceClient?.stopTurn();
492
+ },
493
+ retry: async () => {
494
+ if (lastWasDecision.current && state.sessionId) {
495
+ await openSession(state.sessionId);
496
+ return;
497
+ }
498
+ const users = state.items.filter((item) => item.kind === 'user');
499
+ const failed = users[users.length - 1];
500
+ if (failed) await run(failed.content, state.items.slice(0, state.items.indexOf(failed)));
501
+ else if (state.sessionId) await openSession(state.sessionId);
502
+ },
503
+ openSession,
504
+ newChat: () => {
505
+ abandonTurn();
506
+ latestOpen.current += 1;
507
+ setState(NEW_CHAT);
508
+ },
509
+ voice: voiceClient && {
510
+ state: voice.state,
511
+ assistantSpeech: voice.assistantSpeech,
512
+ outputAnalyser: voice.outputAnalyser,
513
+ muted: voice.muted,
514
+ error: voice.error,
515
+ start: async () => {
516
+ const answer = await voiceClient.start({
517
+ sessionId: state.sessionId ?? undefined,
518
+ recentContext: renderRecentChatForVoice(state.items),
519
+ });
520
+ const sessionId = answer.data?.sessionId;
521
+ // A stopped start can answer during a later call, so only the running call names the chat.
522
+ if (sessionId && voiceClient.getSnapshot().sessionId === sessionId) {
523
+ setState((current) => ({ ...current, sessionId }));
524
+ }
525
+ },
526
+ stop: () => void voiceClient.stop(),
527
+ toggleMute: voiceClient.toggleMute,
528
+ },
529
+ };
530
+ }