@tanstack/ai-react 0.18.1 → 0.19.1
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 +15 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +2 -23
- package/dist/esm/mcp-app-resource.js +45 -45
- package/dist/esm/mcp-app-resource.js.map +1 -1
- package/dist/esm/mcp-apps.js +1 -4
- package/dist/esm/types.d.ts +29 -3
- package/dist/esm/use-audio-recorder.js +41 -46
- package/dist/esm/use-audio-recorder.js.map +1 -1
- package/dist/esm/use-chat.js +330 -275
- package/dist/esm/use-chat.js.map +1 -1
- package/dist/esm/use-generate-audio.d.ts +50 -4
- package/dist/esm/use-generate-audio.js +47 -23
- package/dist/esm/use-generate-audio.js.map +1 -1
- package/dist/esm/use-generate-image.d.ts +50 -4
- package/dist/esm/use-generate-image.js +49 -23
- package/dist/esm/use-generate-image.js.map +1 -1
- package/dist/esm/use-generate-speech.d.ts +50 -4
- package/dist/esm/use-generate-speech.js +43 -23
- package/dist/esm/use-generate-speech.js.map +1 -1
- package/dist/esm/use-generate-video.d.ts +50 -4
- package/dist/esm/use-generate-video.js +141 -103
- package/dist/esm/use-generate-video.js.map +1 -1
- package/dist/esm/use-generation.d.ts +59 -6
- package/dist/esm/use-generation.js +113 -90
- package/dist/esm/use-generation.js.map +1 -1
- package/dist/esm/use-mcp-app-bridge.js +46 -23
- package/dist/esm/use-mcp-app-bridge.js.map +1 -1
- package/dist/esm/use-realtime-chat.js +185 -185
- package/dist/esm/use-realtime-chat.js.map +1 -1
- package/dist/esm/use-summarize.d.ts +50 -4
- package/dist/esm/use-summarize.js +46 -23
- package/dist/esm/use-summarize.js.map +1 -1
- package/dist/esm/use-transcription.d.ts +50 -4
- package/dist/esm/use-transcription.js +51 -20
- package/dist/esm/use-transcription.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +10 -0
- package/src/types.ts +44 -0
- package/src/use-chat.ts +221 -35
- package/src/use-generate-audio.ts +64 -17
- package/src/use-generate-image.ts +64 -17
- package/src/use-generate-speech.ts +64 -17
- package/src/use-generate-video.ts +99 -18
- package/src/use-generation.ts +112 -18
- package/src/use-summarize.ts +64 -17
- package/src/use-transcription.ts +60 -18
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/mcp-apps.js.map +0 -1
package/dist/esm/use-chat.js
CHANGED
|
@@ -1,279 +1,334 @@
|
|
|
1
1
|
import { ChatClient } from "@tanstack/ai-client";
|
|
2
2
|
import { createChatDevtoolsBridge } from "@tanstack/ai-client/devtools";
|
|
3
|
-
import {
|
|
3
|
+
import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
4
|
+
//#region src/use-chat.ts
|
|
5
|
+
var EMPTY_INTERRUPTS = Object.freeze([]);
|
|
6
|
+
var EMPTY_INTERRUPT_ERRORS = Object.freeze([]);
|
|
4
7
|
function useChat(options) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
8
|
+
const hookId = useId();
|
|
9
|
+
const clientId = options.threadId ?? hookId;
|
|
10
|
+
const [messages, setMessages] = useState(options.initialMessages || []);
|
|
11
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
12
|
+
const [error, setError] = useState(void 0);
|
|
13
|
+
const [status, setStatus] = useState("ready");
|
|
14
|
+
const [isSubscribed, setIsSubscribed] = useState(false);
|
|
15
|
+
const [connectionStatus, setConnectionStatus] = useState("disconnected");
|
|
16
|
+
const [sessionGenerating, setSessionGenerating] = useState(false);
|
|
17
|
+
const [queue, setQueue] = useState([]);
|
|
18
|
+
const [runId, setRunId] = useState(null);
|
|
19
|
+
const [interruptState, setInterruptState] = useState(() => ({
|
|
20
|
+
interrupts: EMPTY_INTERRUPTS,
|
|
21
|
+
pendingInterrupts: EMPTY_INTERRUPTS,
|
|
22
|
+
interruptErrors: EMPTY_INTERRUPT_ERRORS,
|
|
23
|
+
resuming: false
|
|
24
|
+
}));
|
|
25
|
+
const messagesRef = useRef(options.initialMessages || []);
|
|
26
|
+
const isFirstMountRef = useRef(true);
|
|
27
|
+
const subscribedRef = useRef(false);
|
|
28
|
+
const activeClientRef = useRef(null);
|
|
29
|
+
const cleanupInvalidationRef = useRef(null);
|
|
30
|
+
const cleanupDisposalRef = useRef(null);
|
|
31
|
+
messagesRef.current = messages;
|
|
32
|
+
const optionsRef = useRef(options);
|
|
33
|
+
optionsRef.current = options;
|
|
34
|
+
const syncResumeState = useCallback((target) => {
|
|
35
|
+
if (!target) return;
|
|
36
|
+
setRunId(target.getCurrentRunId());
|
|
37
|
+
setInterruptState(target.getInterruptState());
|
|
38
|
+
}, []);
|
|
39
|
+
const client = useMemo(() => {
|
|
40
|
+
const messagesToUse = options.initialMessages || [];
|
|
41
|
+
isFirstMountRef.current = false;
|
|
42
|
+
const initialOptions = optionsRef.current;
|
|
43
|
+
const transport = initialOptions.connection ? { connection: initialOptions.connection } : { fetcher: initialOptions.fetcher };
|
|
44
|
+
const instanceHolder = { current: void 0 };
|
|
45
|
+
const getActiveInstance = () => {
|
|
46
|
+
const currentInstance = instanceHolder.current;
|
|
47
|
+
if (!currentInstance || activeClientRef.current !== currentInstance) return;
|
|
48
|
+
return currentInstance;
|
|
49
|
+
};
|
|
50
|
+
const pendingInitializationErrors = [];
|
|
51
|
+
const instance = new ChatClient({
|
|
52
|
+
devtoolsBridgeFactory: createChatDevtoolsBridge,
|
|
53
|
+
...transport,
|
|
54
|
+
initialMessages: messagesToUse,
|
|
55
|
+
...initialOptions.body !== void 0 && { body: initialOptions.body },
|
|
56
|
+
...initialOptions.threadId !== void 0 && { threadId: initialOptions.threadId },
|
|
57
|
+
...initialOptions.forwardedProps !== void 0 && { forwardedProps: initialOptions.forwardedProps },
|
|
58
|
+
...initialOptions.persistence !== void 0 && { persistence: initialOptions.persistence },
|
|
59
|
+
...initialOptions.initialResumeSnapshot !== void 0 && { initialResumeSnapshot: initialOptions.initialResumeSnapshot },
|
|
60
|
+
...initialOptions.context !== void 0 && { context: initialOptions.context },
|
|
61
|
+
devtools: {
|
|
62
|
+
...initialOptions.devtools,
|
|
63
|
+
framework: "react",
|
|
64
|
+
hookName: "useChat",
|
|
65
|
+
outputKind: initialOptions.outputSchema ? "structured" : "chat"
|
|
66
|
+
},
|
|
67
|
+
onResponse: (response) => {
|
|
68
|
+
if (!getActiveInstance()) return;
|
|
69
|
+
optionsRef.current.onResponse?.(response);
|
|
70
|
+
},
|
|
71
|
+
onChunk: (chunk) => {
|
|
72
|
+
if (!getActiveInstance()) return;
|
|
73
|
+
optionsRef.current.onChunk?.(chunk);
|
|
74
|
+
},
|
|
75
|
+
onFinish: (message) => {
|
|
76
|
+
if (!getActiveInstance()) return;
|
|
77
|
+
optionsRef.current.onFinish?.(message);
|
|
78
|
+
},
|
|
79
|
+
onError: (error) => {
|
|
80
|
+
const currentInstance = instanceHolder.current;
|
|
81
|
+
if (!currentInstance) {
|
|
82
|
+
pendingInitializationErrors.push(error);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (activeClientRef.current !== currentInstance) return;
|
|
86
|
+
optionsRef.current.onError?.(error);
|
|
87
|
+
},
|
|
88
|
+
...initialOptions.tools !== void 0 && { tools: initialOptions.tools },
|
|
89
|
+
onCustomEvent: (eventType, data, context) => {
|
|
90
|
+
if (!getActiveInstance()) return;
|
|
91
|
+
optionsRef.current.onCustomEvent?.(eventType, data, context);
|
|
92
|
+
},
|
|
93
|
+
...options.streamProcessor !== void 0 && { streamProcessor: options.streamProcessor },
|
|
94
|
+
onMessagesChange: (newMessages) => {
|
|
95
|
+
if (!getActiveInstance()) return;
|
|
96
|
+
setMessages(newMessages);
|
|
97
|
+
},
|
|
98
|
+
onLoadingChange: (newIsLoading) => {
|
|
99
|
+
const currentInstance = getActiveInstance();
|
|
100
|
+
if (!currentInstance) return;
|
|
101
|
+
setIsLoading(newIsLoading);
|
|
102
|
+
syncResumeState(currentInstance);
|
|
103
|
+
},
|
|
104
|
+
onErrorChange: (newError) => {
|
|
105
|
+
if (!getActiveInstance()) return;
|
|
106
|
+
setError(newError);
|
|
107
|
+
},
|
|
108
|
+
onStatusChange: (status) => {
|
|
109
|
+
if (!getActiveInstance()) return;
|
|
110
|
+
setStatus(status);
|
|
111
|
+
},
|
|
112
|
+
onSubscriptionChange: (nextIsSubscribed) => {
|
|
113
|
+
if (!getActiveInstance()) return;
|
|
114
|
+
setIsSubscribed(nextIsSubscribed);
|
|
115
|
+
},
|
|
116
|
+
onConnectionStatusChange: (nextStatus) => {
|
|
117
|
+
if (!getActiveInstance()) return;
|
|
118
|
+
setConnectionStatus(nextStatus);
|
|
119
|
+
},
|
|
120
|
+
onSessionGeneratingChange: (isGenerating) => {
|
|
121
|
+
if (!getActiveInstance()) return;
|
|
122
|
+
setSessionGenerating(isGenerating);
|
|
123
|
+
},
|
|
124
|
+
...optionsRef.current.queue !== void 0 && { queue: optionsRef.current.queue },
|
|
125
|
+
onQueueChange: (nextQueue) => {
|
|
126
|
+
if (activeClientRef.current !== instance) return;
|
|
127
|
+
setQueue(nextQueue);
|
|
128
|
+
},
|
|
129
|
+
onRunIdChange: (nextRunId) => {
|
|
130
|
+
if (!getActiveInstance()) return;
|
|
131
|
+
setRunId(nextRunId);
|
|
132
|
+
},
|
|
133
|
+
onResumeStateChange: (_nextResumeState, nextPendingInterrupts) => {
|
|
134
|
+
if (!getActiveInstance()) return;
|
|
135
|
+
setInterruptState((current) => ({
|
|
136
|
+
...current,
|
|
137
|
+
interrupts: nextPendingInterrupts,
|
|
138
|
+
pendingInterrupts: nextPendingInterrupts
|
|
139
|
+
}));
|
|
140
|
+
},
|
|
141
|
+
onInterruptStateChange: (nextInterruptState) => {
|
|
142
|
+
if (!getActiveInstance()) return;
|
|
143
|
+
setInterruptState(nextInterruptState);
|
|
144
|
+
optionsRef.current.onInterruptStateChange?.(nextInterruptState);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
instanceHolder.current = instance;
|
|
148
|
+
activeClientRef.current = instance;
|
|
149
|
+
for (const error of pendingInitializationErrors) {
|
|
150
|
+
if (activeClientRef.current !== instance) break;
|
|
151
|
+
optionsRef.current.onError?.(error);
|
|
152
|
+
}
|
|
153
|
+
return instance;
|
|
154
|
+
}, [clientId, syncResumeState]);
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
const clientMessages = client.getMessages();
|
|
157
|
+
if (clientMessages !== messagesRef.current) setMessages(clientMessages);
|
|
158
|
+
}, [client]);
|
|
159
|
+
useEffect(() => {
|
|
160
|
+
client.updateOptions({ body: options.body });
|
|
161
|
+
}, [client, options.body]);
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
if (options.forwardedProps !== void 0) client.updateOptions({ forwardedProps: options.forwardedProps });
|
|
164
|
+
}, [client, options.forwardedProps]);
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
if (options.tools !== void 0) client.updateOptions({ tools: options.tools });
|
|
167
|
+
}, [client, options.tools]);
|
|
168
|
+
useEffect(() => {
|
|
169
|
+
client.updateOptions({ context: options.context });
|
|
170
|
+
}, [client, options.context]);
|
|
171
|
+
useEffect(() => {
|
|
172
|
+
if (options.queue !== void 0) client.updateOptions({ queue: options.queue });
|
|
173
|
+
}, [client, options.queue]);
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
if (options.live) {
|
|
176
|
+
client.subscribe();
|
|
177
|
+
subscribedRef.current = true;
|
|
178
|
+
} else if (subscribedRef.current) {
|
|
179
|
+
client.unsubscribe();
|
|
180
|
+
subscribedRef.current = false;
|
|
181
|
+
}
|
|
182
|
+
}, [client, options.live]);
|
|
183
|
+
useEffect(() => {
|
|
184
|
+
client.attach();
|
|
185
|
+
return () => {
|
|
186
|
+
client.detach();
|
|
187
|
+
};
|
|
188
|
+
}, [client]);
|
|
189
|
+
useEffect(() => {
|
|
190
|
+
if (cleanupDisposalRef.current?.client === client) {
|
|
191
|
+
clearTimeout(cleanupDisposalRef.current.timeout);
|
|
192
|
+
cleanupDisposalRef.current = null;
|
|
193
|
+
}
|
|
194
|
+
if (cleanupInvalidationRef.current) {
|
|
195
|
+
clearTimeout(cleanupInvalidationRef.current);
|
|
196
|
+
cleanupInvalidationRef.current = null;
|
|
197
|
+
}
|
|
198
|
+
activeClientRef.current = client;
|
|
199
|
+
client.mountDevtools();
|
|
200
|
+
syncResumeState(client);
|
|
201
|
+
return () => {
|
|
202
|
+
cleanupInvalidationRef.current = setTimeout(() => {
|
|
203
|
+
if (activeClientRef.current === client) activeClientRef.current = null;
|
|
204
|
+
cleanupInvalidationRef.current = null;
|
|
205
|
+
}, 0);
|
|
206
|
+
const disposal = {
|
|
207
|
+
client,
|
|
208
|
+
timeout: setTimeout(() => {
|
|
209
|
+
if (optionsRef.current.live) client.unsubscribe();
|
|
210
|
+
else client.stop();
|
|
211
|
+
client.dispose();
|
|
212
|
+
if (cleanupDisposalRef.current === disposal) cleanupDisposalRef.current = null;
|
|
213
|
+
}, 0)
|
|
214
|
+
};
|
|
215
|
+
cleanupDisposalRef.current = disposal;
|
|
216
|
+
};
|
|
217
|
+
}, [client, syncResumeState]);
|
|
218
|
+
const sendMessage = useCallback(async (content, sendOptions) => {
|
|
219
|
+
try {
|
|
220
|
+
await client.sendMessage(content, void 0, sendOptions);
|
|
221
|
+
} finally {
|
|
222
|
+
syncResumeState(client);
|
|
223
|
+
}
|
|
224
|
+
}, [client, syncResumeState]);
|
|
225
|
+
const cancelQueued = useCallback((id) => {
|
|
226
|
+
client.cancelQueued(id);
|
|
227
|
+
}, [client, syncResumeState]);
|
|
228
|
+
const append = useCallback(async (message) => {
|
|
229
|
+
try {
|
|
230
|
+
await client.append(message);
|
|
231
|
+
} finally {
|
|
232
|
+
syncResumeState(client);
|
|
233
|
+
}
|
|
234
|
+
}, [client, syncResumeState]);
|
|
235
|
+
const reload = useCallback(async () => {
|
|
236
|
+
try {
|
|
237
|
+
await client.reload();
|
|
238
|
+
} finally {
|
|
239
|
+
syncResumeState(client);
|
|
240
|
+
}
|
|
241
|
+
}, [client, syncResumeState]);
|
|
242
|
+
const stop = useCallback(() => {
|
|
243
|
+
client.stop();
|
|
244
|
+
}, [client]);
|
|
245
|
+
const clear = useCallback(() => {
|
|
246
|
+
client.clear();
|
|
247
|
+
syncResumeState(client);
|
|
248
|
+
}, [client, syncResumeState]);
|
|
249
|
+
const setMessagesManually = useCallback((newMessages) => {
|
|
250
|
+
client.setMessagesManually(newMessages);
|
|
251
|
+
}, [client]);
|
|
252
|
+
const addToolResult = useCallback(async (result) => {
|
|
253
|
+
await client.addToolResult(result);
|
|
254
|
+
}, [client]);
|
|
255
|
+
const addToolApprovalResponse = useCallback(async (response) => {
|
|
256
|
+
await client.addToolApprovalResponse(response);
|
|
257
|
+
syncResumeState(client);
|
|
258
|
+
}, [client, syncResumeState]);
|
|
259
|
+
const resumeInterrupts = useCallback(async (resumeItems, state) => {
|
|
260
|
+
const result = await client.resumeInterrupts(resumeItems, state ?? void 0);
|
|
261
|
+
syncResumeState(client);
|
|
262
|
+
return result;
|
|
263
|
+
}, [client, syncResumeState]);
|
|
264
|
+
const resolveInterrupts = useCallback((resolution) => {
|
|
265
|
+
if (typeof resolution === "boolean") client.resolveInterrupts(resolution);
|
|
266
|
+
else client.resolveInterrupts(resolution);
|
|
267
|
+
}, [client]);
|
|
268
|
+
const cancelInterrupts = useCallback(() => {
|
|
269
|
+
client.cancelInterrupts();
|
|
270
|
+
}, [client]);
|
|
271
|
+
const retryInterrupts = useCallback(() => {
|
|
272
|
+
client.retryInterrupts();
|
|
273
|
+
}, [client]);
|
|
274
|
+
const resumeInterruptsUnsafe = useCallback((resumeItems, state) => client.resumeInterruptsUnsafe(resumeItems, state), [client]);
|
|
275
|
+
const renderedMessages = client.getMessages();
|
|
276
|
+
const activeStructuredPart = useMemo(() => {
|
|
277
|
+
let lastUserIndex = -1;
|
|
278
|
+
for (let i = renderedMessages.length - 1; i >= 0; i--) if (renderedMessages[i]?.role === "user") {
|
|
279
|
+
lastUserIndex = i;
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
if (lastUserIndex === -1) return null;
|
|
283
|
+
for (let i = renderedMessages.length - 1; i > lastUserIndex; i--) {
|
|
284
|
+
const m = renderedMessages[i];
|
|
285
|
+
if (m?.role !== "assistant") continue;
|
|
286
|
+
const part = m.parts.find((p) => p.type === "structured-output");
|
|
287
|
+
if (part) return part;
|
|
288
|
+
}
|
|
289
|
+
return null;
|
|
290
|
+
}, [renderedMessages]);
|
|
291
|
+
const partial = useMemo(() => {
|
|
292
|
+
if (!activeStructuredPart) return {};
|
|
293
|
+
return activeStructuredPart.partial ?? activeStructuredPart.data ?? {};
|
|
294
|
+
}, [activeStructuredPart]);
|
|
295
|
+
const final = useMemo(() => {
|
|
296
|
+
if (!activeStructuredPart || activeStructuredPart.status !== "complete") return null;
|
|
297
|
+
return activeStructuredPart.data;
|
|
298
|
+
}, [activeStructuredPart]);
|
|
299
|
+
return {
|
|
300
|
+
messages: renderedMessages,
|
|
301
|
+
sendMessage,
|
|
302
|
+
append,
|
|
303
|
+
reload,
|
|
304
|
+
stop,
|
|
305
|
+
isLoading,
|
|
306
|
+
error,
|
|
307
|
+
status,
|
|
308
|
+
isSubscribed,
|
|
309
|
+
connectionStatus,
|
|
310
|
+
sessionGenerating,
|
|
311
|
+
setMessages: setMessagesManually,
|
|
312
|
+
clear,
|
|
313
|
+
addToolResult,
|
|
314
|
+
addToolApprovalResponse,
|
|
315
|
+
queue,
|
|
316
|
+
cancelQueued,
|
|
317
|
+
runId,
|
|
318
|
+
interrupts: interruptState.interrupts,
|
|
319
|
+
pendingInterrupts: interruptState.pendingInterrupts,
|
|
320
|
+
interruptErrors: interruptState.interruptErrors,
|
|
321
|
+
resuming: interruptState.resuming,
|
|
322
|
+
resolveInterrupts,
|
|
323
|
+
cancelInterrupts,
|
|
324
|
+
retryInterrupts,
|
|
325
|
+
resumeInterruptsUnsafe,
|
|
326
|
+
resumeInterrupts,
|
|
327
|
+
partial,
|
|
328
|
+
final
|
|
329
|
+
};
|
|
275
330
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
//# sourceMappingURL=use-chat.js.map
|
|
331
|
+
//#endregion
|
|
332
|
+
export { useChat };
|
|
333
|
+
|
|
334
|
+
//# sourceMappingURL=use-chat.js.map
|