@ai-sdk/react 1.2.12 → 2.0.0-alpha.2
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/CHANGELOG.md +268 -26
- package/README.md +1 -1
- package/dist/index.d.mts +29 -122
- package/dist/index.d.ts +29 -122
- package/dist/index.js +164 -550
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +164 -558
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -10
package/dist/index.js
CHANGED
|
@@ -31,541 +31,160 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
experimental_useObject: () => experimental_useObject,
|
|
34
|
-
useAssistant: () => useAssistant,
|
|
35
34
|
useChat: () => useChat,
|
|
36
35
|
useCompletion: () => useCompletion
|
|
37
36
|
});
|
|
38
37
|
module.exports = __toCommonJS(src_exports);
|
|
39
38
|
|
|
40
|
-
// src/use-assistant.ts
|
|
41
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
42
|
-
var import_ui_utils = require("@ai-sdk/ui-utils");
|
|
43
|
-
var import_react = require("react");
|
|
44
|
-
var getOriginalFetch = () => fetch;
|
|
45
|
-
function useAssistant({
|
|
46
|
-
api,
|
|
47
|
-
threadId: threadIdParam,
|
|
48
|
-
credentials,
|
|
49
|
-
headers,
|
|
50
|
-
body,
|
|
51
|
-
onError,
|
|
52
|
-
fetch: fetch2
|
|
53
|
-
}) {
|
|
54
|
-
const [messages, setMessages] = (0, import_react.useState)([]);
|
|
55
|
-
const [input, setInput] = (0, import_react.useState)("");
|
|
56
|
-
const [currentThreadId, setCurrentThreadId] = (0, import_react.useState)(
|
|
57
|
-
void 0
|
|
58
|
-
);
|
|
59
|
-
const [status, setStatus] = (0, import_react.useState)("awaiting_message");
|
|
60
|
-
const [error, setError] = (0, import_react.useState)(void 0);
|
|
61
|
-
const handleInputChange = (event) => {
|
|
62
|
-
setInput(event.target.value);
|
|
63
|
-
};
|
|
64
|
-
const abortControllerRef = (0, import_react.useRef)(null);
|
|
65
|
-
const stop = (0, import_react.useCallback)(() => {
|
|
66
|
-
if (abortControllerRef.current) {
|
|
67
|
-
abortControllerRef.current.abort();
|
|
68
|
-
abortControllerRef.current = null;
|
|
69
|
-
}
|
|
70
|
-
}, []);
|
|
71
|
-
const append = async (message, requestOptions) => {
|
|
72
|
-
var _a, _b;
|
|
73
|
-
setStatus("in_progress");
|
|
74
|
-
setMessages((messages2) => {
|
|
75
|
-
var _a2;
|
|
76
|
-
return [
|
|
77
|
-
...messages2,
|
|
78
|
-
{
|
|
79
|
-
...message,
|
|
80
|
-
id: (_a2 = message.id) != null ? _a2 : (0, import_ui_utils.generateId)()
|
|
81
|
-
}
|
|
82
|
-
];
|
|
83
|
-
});
|
|
84
|
-
setInput("");
|
|
85
|
-
const abortController = new AbortController();
|
|
86
|
-
try {
|
|
87
|
-
abortControllerRef.current = abortController;
|
|
88
|
-
const actualFetch = fetch2 != null ? fetch2 : getOriginalFetch();
|
|
89
|
-
const response = await actualFetch(api, {
|
|
90
|
-
method: "POST",
|
|
91
|
-
credentials,
|
|
92
|
-
signal: abortController.signal,
|
|
93
|
-
headers: { "Content-Type": "application/json", ...headers },
|
|
94
|
-
body: JSON.stringify({
|
|
95
|
-
...body,
|
|
96
|
-
// always use user-provided threadId when available:
|
|
97
|
-
threadId: (_a = threadIdParam != null ? threadIdParam : currentThreadId) != null ? _a : null,
|
|
98
|
-
message: message.content,
|
|
99
|
-
// optional request data:
|
|
100
|
-
data: requestOptions == null ? void 0 : requestOptions.data
|
|
101
|
-
})
|
|
102
|
-
});
|
|
103
|
-
if (!response.ok) {
|
|
104
|
-
throw new Error(
|
|
105
|
-
(_b = await response.text()) != null ? _b : "Failed to fetch the assistant response."
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
if (response.body == null) {
|
|
109
|
-
throw new Error("The response body is empty.");
|
|
110
|
-
}
|
|
111
|
-
await (0, import_ui_utils.processAssistantStream)({
|
|
112
|
-
stream: response.body,
|
|
113
|
-
onAssistantMessagePart(value) {
|
|
114
|
-
setMessages((messages2) => [
|
|
115
|
-
...messages2,
|
|
116
|
-
{
|
|
117
|
-
id: value.id,
|
|
118
|
-
role: value.role,
|
|
119
|
-
content: value.content[0].text.value,
|
|
120
|
-
parts: []
|
|
121
|
-
}
|
|
122
|
-
]);
|
|
123
|
-
},
|
|
124
|
-
onTextPart(value) {
|
|
125
|
-
setMessages((messages2) => {
|
|
126
|
-
const lastMessage = messages2[messages2.length - 1];
|
|
127
|
-
return [
|
|
128
|
-
...messages2.slice(0, messages2.length - 1),
|
|
129
|
-
{
|
|
130
|
-
id: lastMessage.id,
|
|
131
|
-
role: lastMessage.role,
|
|
132
|
-
content: lastMessage.content + value,
|
|
133
|
-
parts: lastMessage.parts
|
|
134
|
-
}
|
|
135
|
-
];
|
|
136
|
-
});
|
|
137
|
-
},
|
|
138
|
-
onAssistantControlDataPart(value) {
|
|
139
|
-
setCurrentThreadId(value.threadId);
|
|
140
|
-
setMessages((messages2) => {
|
|
141
|
-
const lastMessage = messages2[messages2.length - 1];
|
|
142
|
-
lastMessage.id = value.messageId;
|
|
143
|
-
return [...messages2.slice(0, messages2.length - 1), lastMessage];
|
|
144
|
-
});
|
|
145
|
-
},
|
|
146
|
-
onDataMessagePart(value) {
|
|
147
|
-
setMessages((messages2) => {
|
|
148
|
-
var _a2;
|
|
149
|
-
return [
|
|
150
|
-
...messages2,
|
|
151
|
-
{
|
|
152
|
-
id: (_a2 = value.id) != null ? _a2 : (0, import_ui_utils.generateId)(),
|
|
153
|
-
role: "data",
|
|
154
|
-
content: "",
|
|
155
|
-
data: value.data,
|
|
156
|
-
parts: []
|
|
157
|
-
}
|
|
158
|
-
];
|
|
159
|
-
});
|
|
160
|
-
},
|
|
161
|
-
onErrorPart(value) {
|
|
162
|
-
setError(new Error(value));
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
} catch (error2) {
|
|
166
|
-
if ((0, import_provider_utils.isAbortError)(error2) && abortController.signal.aborted) {
|
|
167
|
-
abortControllerRef.current = null;
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
if (onError && error2 instanceof Error) {
|
|
171
|
-
onError(error2);
|
|
172
|
-
}
|
|
173
|
-
setError(error2);
|
|
174
|
-
} finally {
|
|
175
|
-
abortControllerRef.current = null;
|
|
176
|
-
setStatus("awaiting_message");
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
const submitMessage = async (event, requestOptions) => {
|
|
180
|
-
var _a;
|
|
181
|
-
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
182
|
-
if (input === "") {
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
append({ role: "user", content: input, parts: [] }, requestOptions);
|
|
186
|
-
};
|
|
187
|
-
const setThreadId = (threadId) => {
|
|
188
|
-
setCurrentThreadId(threadId);
|
|
189
|
-
setMessages([]);
|
|
190
|
-
};
|
|
191
|
-
return {
|
|
192
|
-
append,
|
|
193
|
-
messages,
|
|
194
|
-
setMessages,
|
|
195
|
-
threadId: currentThreadId,
|
|
196
|
-
setThreadId,
|
|
197
|
-
input,
|
|
198
|
-
setInput,
|
|
199
|
-
handleInputChange,
|
|
200
|
-
submitMessage,
|
|
201
|
-
status,
|
|
202
|
-
error,
|
|
203
|
-
stop
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// src/use-chat.ts
|
|
208
|
-
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
|
209
|
-
var import_react3 = require("react");
|
|
210
|
-
var import_swr = __toESM(require("swr"));
|
|
211
|
-
|
|
212
|
-
// src/throttle.ts
|
|
213
|
-
var import_throttleit = __toESM(require("throttleit"));
|
|
214
|
-
function throttle(fn, waitMs) {
|
|
215
|
-
return waitMs != null ? (0, import_throttleit.default)(fn, waitMs) : fn;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// src/util/use-stable-value.ts
|
|
219
|
-
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
|
220
|
-
var import_react2 = require("react");
|
|
221
|
-
function useStableValue(latestValue) {
|
|
222
|
-
const [value, setValue] = (0, import_react2.useState)(latestValue);
|
|
223
|
-
(0, import_react2.useEffect)(() => {
|
|
224
|
-
if (!(0, import_ui_utils2.isDeepEqualData)(latestValue, value)) {
|
|
225
|
-
setValue(latestValue);
|
|
226
|
-
}
|
|
227
|
-
}, [latestValue, value]);
|
|
228
|
-
return value;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
39
|
// src/use-chat.ts
|
|
40
|
+
var import_ai = require("ai");
|
|
41
|
+
var import_react = require("react");
|
|
232
42
|
function useChat({
|
|
233
|
-
|
|
234
|
-
id,
|
|
235
|
-
initialMessages,
|
|
43
|
+
chatId,
|
|
236
44
|
initialInput = "",
|
|
237
|
-
sendExtraMessageFields,
|
|
238
45
|
onToolCall,
|
|
239
|
-
experimental_prepareRequestBody,
|
|
240
|
-
maxSteps = 1,
|
|
241
|
-
streamProtocol = "data",
|
|
242
|
-
onResponse,
|
|
243
46
|
onFinish,
|
|
244
47
|
onError,
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
generateId: generateId2 = import_ui_utils3.generateId,
|
|
249
|
-
fetch: fetch2,
|
|
250
|
-
keepLastMessageOnError = true,
|
|
251
|
-
experimental_throttle: throttleWaitMs
|
|
48
|
+
generateId = import_ai.generateId,
|
|
49
|
+
experimental_throttle: throttleWaitMs,
|
|
50
|
+
chatStore: chatStoreArg
|
|
252
51
|
} = {}) {
|
|
253
|
-
const [hookId] = (0,
|
|
254
|
-
const
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
52
|
+
const [hookId] = (0, import_react.useState)(generateId);
|
|
53
|
+
const stableChatId = chatId != null ? chatId : hookId;
|
|
54
|
+
const chatStore = (0, import_react.useRef)(
|
|
55
|
+
chatStoreArg != null ? chatStoreArg : (0, import_ai.defaultChatStore)({
|
|
56
|
+
api: "/api/chat",
|
|
57
|
+
generateId
|
|
58
|
+
})
|
|
260
59
|
);
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const { data: status = "ready", mutate: mutateStatus } = (0, import_swr.default)([chatKey, "status"], null);
|
|
276
|
-
const { data: error = void 0, mutate: setError } = (0, import_swr.default)([chatKey, "error"], null);
|
|
277
|
-
const abortControllerRef = (0, import_react3.useRef)(null);
|
|
278
|
-
const extraMetadataRef = (0, import_react3.useRef)({
|
|
279
|
-
credentials,
|
|
280
|
-
headers,
|
|
281
|
-
body
|
|
282
|
-
});
|
|
283
|
-
(0, import_react3.useEffect)(() => {
|
|
284
|
-
extraMetadataRef.current = {
|
|
285
|
-
credentials,
|
|
286
|
-
headers,
|
|
287
|
-
body
|
|
288
|
-
};
|
|
289
|
-
}, [credentials, headers, body]);
|
|
290
|
-
const triggerRequest = (0, import_react3.useCallback)(
|
|
291
|
-
async (chatRequest, requestType = "generate") => {
|
|
292
|
-
var _a, _b;
|
|
293
|
-
mutateStatus("submitted");
|
|
294
|
-
setError(void 0);
|
|
295
|
-
const chatMessages = (0, import_ui_utils3.fillMessageParts)(chatRequest.messages);
|
|
296
|
-
const messageCount = chatMessages.length;
|
|
297
|
-
const maxStep = (0, import_ui_utils3.extractMaxToolInvocationStep)(
|
|
298
|
-
(_a = chatMessages[chatMessages.length - 1]) == null ? void 0 : _a.toolInvocations
|
|
299
|
-
);
|
|
300
|
-
try {
|
|
301
|
-
const abortController = new AbortController();
|
|
302
|
-
abortControllerRef.current = abortController;
|
|
303
|
-
const throttledMutate = throttle(mutate, throttleWaitMs);
|
|
304
|
-
const throttledMutateStreamData = throttle(
|
|
305
|
-
mutateStreamData,
|
|
306
|
-
throttleWaitMs
|
|
307
|
-
);
|
|
308
|
-
const previousMessages = messagesRef.current;
|
|
309
|
-
throttledMutate(chatMessages, false);
|
|
310
|
-
const constructedMessagesPayload = sendExtraMessageFields ? chatMessages : chatMessages.map(
|
|
311
|
-
({
|
|
312
|
-
role,
|
|
313
|
-
content,
|
|
314
|
-
experimental_attachments,
|
|
315
|
-
data,
|
|
316
|
-
annotations,
|
|
317
|
-
toolInvocations,
|
|
318
|
-
parts
|
|
319
|
-
}) => ({
|
|
320
|
-
role,
|
|
321
|
-
content,
|
|
322
|
-
...experimental_attachments !== void 0 && {
|
|
323
|
-
experimental_attachments
|
|
324
|
-
},
|
|
325
|
-
...data !== void 0 && { data },
|
|
326
|
-
...annotations !== void 0 && { annotations },
|
|
327
|
-
...toolInvocations !== void 0 && { toolInvocations },
|
|
328
|
-
...parts !== void 0 && { parts }
|
|
329
|
-
})
|
|
330
|
-
);
|
|
331
|
-
const existingData = streamDataRef.current;
|
|
332
|
-
await (0, import_ui_utils3.callChatApi)({
|
|
333
|
-
api,
|
|
334
|
-
body: (_b = experimental_prepareRequestBody == null ? void 0 : experimental_prepareRequestBody({
|
|
335
|
-
id: chatId,
|
|
336
|
-
messages: chatMessages,
|
|
337
|
-
requestData: chatRequest.data,
|
|
338
|
-
requestBody: chatRequest.body
|
|
339
|
-
})) != null ? _b : {
|
|
340
|
-
id: chatId,
|
|
341
|
-
messages: constructedMessagesPayload,
|
|
342
|
-
data: chatRequest.data,
|
|
343
|
-
...extraMetadataRef.current.body,
|
|
344
|
-
...chatRequest.body
|
|
345
|
-
},
|
|
346
|
-
streamProtocol,
|
|
347
|
-
credentials: extraMetadataRef.current.credentials,
|
|
348
|
-
headers: {
|
|
349
|
-
...extraMetadataRef.current.headers,
|
|
350
|
-
...chatRequest.headers
|
|
351
|
-
},
|
|
352
|
-
abortController: () => abortControllerRef.current,
|
|
353
|
-
restoreMessagesOnFailure() {
|
|
354
|
-
if (!keepLastMessageOnError) {
|
|
355
|
-
throttledMutate(previousMessages, false);
|
|
356
|
-
}
|
|
357
|
-
},
|
|
358
|
-
onResponse,
|
|
359
|
-
onUpdate({ message, data, replaceLastMessage }) {
|
|
360
|
-
mutateStatus("streaming");
|
|
361
|
-
throttledMutate(
|
|
362
|
-
[
|
|
363
|
-
...replaceLastMessage ? chatMessages.slice(0, chatMessages.length - 1) : chatMessages,
|
|
364
|
-
message
|
|
365
|
-
],
|
|
366
|
-
false
|
|
367
|
-
);
|
|
368
|
-
if (data == null ? void 0 : data.length) {
|
|
369
|
-
throttledMutateStreamData(
|
|
370
|
-
[...existingData != null ? existingData : [], ...data],
|
|
371
|
-
false
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
},
|
|
375
|
-
onToolCall,
|
|
376
|
-
onFinish,
|
|
377
|
-
generateId: generateId2,
|
|
378
|
-
fetch: fetch2,
|
|
379
|
-
lastMessage: chatMessages[chatMessages.length - 1],
|
|
380
|
-
requestType
|
|
381
|
-
});
|
|
382
|
-
abortControllerRef.current = null;
|
|
383
|
-
mutateStatus("ready");
|
|
384
|
-
} catch (err) {
|
|
385
|
-
if (err.name === "AbortError") {
|
|
386
|
-
abortControllerRef.current = null;
|
|
387
|
-
mutateStatus("ready");
|
|
388
|
-
return null;
|
|
389
|
-
}
|
|
390
|
-
if (onError && err instanceof Error) {
|
|
391
|
-
onError(err);
|
|
60
|
+
if (!chatStore.current.hasChat(stableChatId)) {
|
|
61
|
+
chatStore.current.addChat(stableChatId, []);
|
|
62
|
+
}
|
|
63
|
+
const subscribe = (0, import_react.useCallback)(
|
|
64
|
+
({
|
|
65
|
+
onStoreChange,
|
|
66
|
+
eventType
|
|
67
|
+
}) => {
|
|
68
|
+
return chatStore.current.subscribe({
|
|
69
|
+
onChatChanged: (event) => {
|
|
70
|
+
if (event.chatId !== stableChatId || event.type !== eventType) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
onStoreChange();
|
|
392
74
|
}
|
|
393
|
-
setError(err);
|
|
394
|
-
mutateStatus("error");
|
|
395
|
-
}
|
|
396
|
-
const messages2 = messagesRef.current;
|
|
397
|
-
if ((0, import_ui_utils3.shouldResubmitMessages)({
|
|
398
|
-
originalMaxToolInvocationStep: maxStep,
|
|
399
|
-
originalMessageCount: messageCount,
|
|
400
|
-
maxSteps,
|
|
401
|
-
messages: messages2
|
|
402
|
-
})) {
|
|
403
|
-
await triggerRequest({ messages: messages2 });
|
|
404
|
-
}
|
|
405
|
-
},
|
|
406
|
-
[
|
|
407
|
-
mutate,
|
|
408
|
-
mutateStatus,
|
|
409
|
-
api,
|
|
410
|
-
extraMetadataRef,
|
|
411
|
-
onResponse,
|
|
412
|
-
onFinish,
|
|
413
|
-
onError,
|
|
414
|
-
setError,
|
|
415
|
-
mutateStreamData,
|
|
416
|
-
streamDataRef,
|
|
417
|
-
streamProtocol,
|
|
418
|
-
sendExtraMessageFields,
|
|
419
|
-
experimental_prepareRequestBody,
|
|
420
|
-
onToolCall,
|
|
421
|
-
maxSteps,
|
|
422
|
-
messagesRef,
|
|
423
|
-
abortControllerRef,
|
|
424
|
-
generateId2,
|
|
425
|
-
fetch2,
|
|
426
|
-
keepLastMessageOnError,
|
|
427
|
-
throttleWaitMs,
|
|
428
|
-
chatId
|
|
429
|
-
]
|
|
430
|
-
);
|
|
431
|
-
const append = (0, import_react3.useCallback)(
|
|
432
|
-
async (message, {
|
|
433
|
-
data,
|
|
434
|
-
headers: headers2,
|
|
435
|
-
body: body2,
|
|
436
|
-
experimental_attachments = message.experimental_attachments
|
|
437
|
-
} = {}) => {
|
|
438
|
-
var _a, _b;
|
|
439
|
-
const attachmentsForRequest = await (0, import_ui_utils3.prepareAttachmentsForRequest)(
|
|
440
|
-
experimental_attachments
|
|
441
|
-
);
|
|
442
|
-
const messages2 = messagesRef.current.concat({
|
|
443
|
-
...message,
|
|
444
|
-
id: (_a = message.id) != null ? _a : generateId2(),
|
|
445
|
-
createdAt: (_b = message.createdAt) != null ? _b : /* @__PURE__ */ new Date(),
|
|
446
|
-
experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : void 0,
|
|
447
|
-
parts: (0, import_ui_utils3.getMessageParts)(message)
|
|
448
75
|
});
|
|
449
|
-
return triggerRequest({ messages: messages2, headers: headers2, body: body2, data });
|
|
450
76
|
},
|
|
451
|
-
[
|
|
77
|
+
[chatStore, stableChatId]
|
|
452
78
|
);
|
|
453
|
-
const
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
79
|
+
const addToolResult = (0, import_react.useCallback)(
|
|
80
|
+
(options) => chatStore.current.addToolResult({ chatId: stableChatId, ...options }),
|
|
81
|
+
[chatStore, stableChatId]
|
|
82
|
+
);
|
|
83
|
+
const stopStream = (0, import_react.useCallback)(() => {
|
|
84
|
+
chatStore.current.stopStream({ chatId: stableChatId });
|
|
85
|
+
}, [chatStore, stableChatId]);
|
|
86
|
+
const error = (0, import_react.useSyncExternalStore)(
|
|
87
|
+
(callback) => subscribe({
|
|
88
|
+
onStoreChange: callback,
|
|
89
|
+
eventType: "chat-status-changed"
|
|
90
|
+
}),
|
|
91
|
+
() => chatStore.current.getError(stableChatId),
|
|
92
|
+
() => chatStore.current.getError(stableChatId)
|
|
93
|
+
);
|
|
94
|
+
const status = (0, import_react.useSyncExternalStore)(
|
|
95
|
+
(callback) => subscribe({
|
|
96
|
+
onStoreChange: callback,
|
|
97
|
+
eventType: "chat-status-changed"
|
|
98
|
+
}),
|
|
99
|
+
() => chatStore.current.getStatus(stableChatId),
|
|
100
|
+
() => chatStore.current.getStatus(stableChatId)
|
|
101
|
+
);
|
|
102
|
+
const messages = (0, import_react.useSyncExternalStore)(
|
|
103
|
+
(callback) => {
|
|
104
|
+
return subscribe({
|
|
105
|
+
onStoreChange: callback,
|
|
106
|
+
eventType: "chat-messages-changed"
|
|
465
107
|
});
|
|
466
108
|
},
|
|
467
|
-
|
|
109
|
+
() => chatStore.current.getMessages(stableChatId),
|
|
110
|
+
() => chatStore.current.getMessages(stableChatId)
|
|
468
111
|
);
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
(messages2) => {
|
|
481
|
-
if (typeof messages2 === "function") {
|
|
482
|
-
messages2 = messages2(messagesRef.current);
|
|
483
|
-
}
|
|
484
|
-
const messagesWithParts = (0, import_ui_utils3.fillMessageParts)(messages2);
|
|
485
|
-
mutate(messagesWithParts, false);
|
|
486
|
-
messagesRef.current = messagesWithParts;
|
|
487
|
-
},
|
|
488
|
-
[mutate]
|
|
112
|
+
const append = (0, import_react.useCallback)(
|
|
113
|
+
(message, { headers, body } = {}) => chatStore.current.submitMessage({
|
|
114
|
+
chatId: stableChatId,
|
|
115
|
+
message,
|
|
116
|
+
headers,
|
|
117
|
+
body,
|
|
118
|
+
onError,
|
|
119
|
+
onToolCall,
|
|
120
|
+
onFinish
|
|
121
|
+
}),
|
|
122
|
+
[chatStore, stableChatId, onError, onToolCall, onFinish]
|
|
489
123
|
);
|
|
490
|
-
const
|
|
491
|
-
(
|
|
492
|
-
|
|
493
|
-
|
|
124
|
+
const reload = (0, import_react.useCallback)(
|
|
125
|
+
async ({ headers, body } = {}) => chatStore.current.resubmitLastUserMessage({
|
|
126
|
+
chatId: stableChatId,
|
|
127
|
+
headers,
|
|
128
|
+
body,
|
|
129
|
+
onError,
|
|
130
|
+
onToolCall,
|
|
131
|
+
onFinish
|
|
132
|
+
}),
|
|
133
|
+
[chatStore, stableChatId, onError, onToolCall, onFinish]
|
|
134
|
+
);
|
|
135
|
+
const stop = (0, import_react.useCallback)(() => stopStream(), [stopStream]);
|
|
136
|
+
const experimental_resume = (0, import_react.useCallback)(
|
|
137
|
+
async () => chatStore.current.resumeStream({
|
|
138
|
+
chatId: stableChatId,
|
|
139
|
+
onError,
|
|
140
|
+
onToolCall,
|
|
141
|
+
onFinish
|
|
142
|
+
}),
|
|
143
|
+
[chatStore, stableChatId, onError, onToolCall, onFinish]
|
|
144
|
+
);
|
|
145
|
+
const setMessages = (0, import_react.useCallback)(
|
|
146
|
+
(messagesParam) => {
|
|
147
|
+
if (typeof messagesParam === "function") {
|
|
148
|
+
messagesParam = messagesParam(messages);
|
|
494
149
|
}
|
|
495
|
-
|
|
496
|
-
|
|
150
|
+
chatStore.current.setMessages({
|
|
151
|
+
id: stableChatId,
|
|
152
|
+
messages: messagesParam
|
|
153
|
+
});
|
|
497
154
|
},
|
|
498
|
-
[
|
|
155
|
+
[stableChatId, messages]
|
|
499
156
|
);
|
|
500
|
-
const [input, setInput] = (0,
|
|
501
|
-
const handleSubmit = (0,
|
|
502
|
-
async (event, options = {}
|
|
157
|
+
const [input, setInput] = (0, import_react.useState)(initialInput);
|
|
158
|
+
const handleSubmit = (0, import_react.useCallback)(
|
|
159
|
+
async (event, options = {}) => {
|
|
503
160
|
var _a;
|
|
504
161
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
505
|
-
|
|
162
|
+
const fileParts = Array.isArray(options == null ? void 0 : options.files) ? options.files : await (0, import_ai.convertFileListToFileUIParts)(options == null ? void 0 : options.files);
|
|
163
|
+
if (!input && fileParts.length === 0)
|
|
506
164
|
return;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
165
|
+
append(
|
|
166
|
+
{
|
|
167
|
+
id: generateId(),
|
|
168
|
+
role: "user",
|
|
169
|
+
metadata: void 0,
|
|
170
|
+
parts: [...fileParts, { type: "text", text: input }]
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
headers: options.headers,
|
|
174
|
+
body: options.body
|
|
175
|
+
}
|
|
515
176
|
);
|
|
516
|
-
const messages2 = messagesRef.current.concat({
|
|
517
|
-
id: generateId2(),
|
|
518
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
519
|
-
role: "user",
|
|
520
|
-
content: input,
|
|
521
|
-
experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : void 0,
|
|
522
|
-
parts: [{ type: "text", text: input }]
|
|
523
|
-
});
|
|
524
|
-
const chatRequest = {
|
|
525
|
-
messages: messages2,
|
|
526
|
-
headers: options.headers,
|
|
527
|
-
body: options.body,
|
|
528
|
-
data: options.data
|
|
529
|
-
};
|
|
530
|
-
triggerRequest(chatRequest);
|
|
531
177
|
setInput("");
|
|
532
178
|
},
|
|
533
|
-
[input,
|
|
179
|
+
[input, generateId, append]
|
|
534
180
|
);
|
|
535
181
|
const handleInputChange = (e) => {
|
|
536
182
|
setInput(e.target.value);
|
|
537
183
|
};
|
|
538
|
-
const addToolResult = (0, import_react3.useCallback)(
|
|
539
|
-
({ toolCallId, result }) => {
|
|
540
|
-
const currentMessages = messagesRef.current;
|
|
541
|
-
(0, import_ui_utils3.updateToolCallResult)({
|
|
542
|
-
messages: currentMessages,
|
|
543
|
-
toolCallId,
|
|
544
|
-
toolResult: result
|
|
545
|
-
});
|
|
546
|
-
mutate(
|
|
547
|
-
[
|
|
548
|
-
...currentMessages.slice(0, currentMessages.length - 1),
|
|
549
|
-
{ ...currentMessages[currentMessages.length - 1] }
|
|
550
|
-
],
|
|
551
|
-
false
|
|
552
|
-
);
|
|
553
|
-
if (status === "submitted" || status === "streaming") {
|
|
554
|
-
return;
|
|
555
|
-
}
|
|
556
|
-
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
557
|
-
if ((0, import_ui_utils3.isAssistantMessageWithCompletedToolCalls)(lastMessage)) {
|
|
558
|
-
triggerRequest({ messages: currentMessages });
|
|
559
|
-
}
|
|
560
|
-
},
|
|
561
|
-
[mutate, status, triggerRequest]
|
|
562
|
-
);
|
|
563
184
|
return {
|
|
564
|
-
messages
|
|
565
|
-
|
|
185
|
+
messages,
|
|
186
|
+
chatId: stableChatId,
|
|
566
187
|
setMessages,
|
|
567
|
-
data: streamData,
|
|
568
|
-
setData,
|
|
569
188
|
error,
|
|
570
189
|
append,
|
|
571
190
|
reload,
|
|
@@ -575,16 +194,23 @@ function useChat({
|
|
|
575
194
|
setInput,
|
|
576
195
|
handleInputChange,
|
|
577
196
|
handleSubmit,
|
|
578
|
-
isLoading: status === "submitted" || status === "streaming",
|
|
579
197
|
status,
|
|
580
198
|
addToolResult
|
|
581
199
|
};
|
|
582
200
|
}
|
|
583
201
|
|
|
584
202
|
// src/use-completion.ts
|
|
585
|
-
var
|
|
586
|
-
var
|
|
587
|
-
var
|
|
203
|
+
var import_ai2 = require("ai");
|
|
204
|
+
var import_react2 = require("react");
|
|
205
|
+
var import_swr = __toESM(require("swr"));
|
|
206
|
+
|
|
207
|
+
// src/throttle.ts
|
|
208
|
+
var import_throttleit = __toESM(require("throttleit"));
|
|
209
|
+
function throttle(fn, waitMs) {
|
|
210
|
+
return waitMs != null ? (0, import_throttleit.default)(fn, waitMs) : fn;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/use-completion.ts
|
|
588
214
|
function useCompletion({
|
|
589
215
|
api = "/api/completion",
|
|
590
216
|
id,
|
|
@@ -595,38 +221,36 @@ function useCompletion({
|
|
|
595
221
|
body,
|
|
596
222
|
streamProtocol = "data",
|
|
597
223
|
fetch: fetch2,
|
|
598
|
-
onResponse,
|
|
599
224
|
onFinish,
|
|
600
225
|
onError,
|
|
601
226
|
experimental_throttle: throttleWaitMs
|
|
602
227
|
} = {}) {
|
|
603
|
-
const hookId = (0,
|
|
228
|
+
const hookId = (0, import_react2.useId)();
|
|
604
229
|
const completionId = id || hookId;
|
|
605
|
-
const { data, mutate } = (0,
|
|
230
|
+
const { data, mutate } = (0, import_swr.default)([api, completionId], null, {
|
|
606
231
|
fallbackData: initialCompletion
|
|
607
232
|
});
|
|
608
|
-
const { data: isLoading = false, mutate: mutateLoading } = (0,
|
|
233
|
+
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr.default)(
|
|
609
234
|
[completionId, "loading"],
|
|
610
235
|
null
|
|
611
236
|
);
|
|
612
|
-
const
|
|
613
|
-
const [error, setError] = (0, import_react4.useState)(void 0);
|
|
237
|
+
const [error, setError] = (0, import_react2.useState)(void 0);
|
|
614
238
|
const completion = data;
|
|
615
|
-
const [abortController, setAbortController] = (0,
|
|
616
|
-
const extraMetadataRef = (0,
|
|
239
|
+
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
|
240
|
+
const extraMetadataRef = (0, import_react2.useRef)({
|
|
617
241
|
credentials,
|
|
618
242
|
headers,
|
|
619
243
|
body
|
|
620
244
|
});
|
|
621
|
-
(0,
|
|
245
|
+
(0, import_react2.useEffect)(() => {
|
|
622
246
|
extraMetadataRef.current = {
|
|
623
247
|
credentials,
|
|
624
248
|
headers,
|
|
625
249
|
body
|
|
626
250
|
};
|
|
627
251
|
}, [credentials, headers, body]);
|
|
628
|
-
const triggerRequest = (0,
|
|
629
|
-
async (prompt, options) => (0,
|
|
252
|
+
const triggerRequest = (0, import_react2.useCallback)(
|
|
253
|
+
async (prompt, options) => (0, import_ai2.callCompletionApi)({
|
|
630
254
|
api,
|
|
631
255
|
prompt,
|
|
632
256
|
credentials: extraMetadataRef.current.credentials,
|
|
@@ -642,14 +266,9 @@ function useCompletion({
|
|
|
642
266
|
(completion2) => mutate(completion2, false),
|
|
643
267
|
throttleWaitMs
|
|
644
268
|
),
|
|
645
|
-
onData: throttle(
|
|
646
|
-
(data2) => mutateStreamData([...streamData != null ? streamData : [], ...data2 != null ? data2 : []], false),
|
|
647
|
-
throttleWaitMs
|
|
648
|
-
),
|
|
649
269
|
setLoading: mutateLoading,
|
|
650
270
|
setError,
|
|
651
271
|
setAbortController,
|
|
652
|
-
onResponse,
|
|
653
272
|
onFinish,
|
|
654
273
|
onError
|
|
655
274
|
}),
|
|
@@ -659,37 +278,34 @@ function useCompletion({
|
|
|
659
278
|
api,
|
|
660
279
|
extraMetadataRef,
|
|
661
280
|
setAbortController,
|
|
662
|
-
onResponse,
|
|
663
281
|
onFinish,
|
|
664
282
|
onError,
|
|
665
283
|
setError,
|
|
666
|
-
streamData,
|
|
667
284
|
streamProtocol,
|
|
668
285
|
fetch2,
|
|
669
|
-
mutateStreamData,
|
|
670
286
|
throttleWaitMs
|
|
671
287
|
]
|
|
672
288
|
);
|
|
673
|
-
const stop = (0,
|
|
289
|
+
const stop = (0, import_react2.useCallback)(() => {
|
|
674
290
|
if (abortController) {
|
|
675
291
|
abortController.abort();
|
|
676
292
|
setAbortController(null);
|
|
677
293
|
}
|
|
678
294
|
}, [abortController]);
|
|
679
|
-
const setCompletion = (0,
|
|
295
|
+
const setCompletion = (0, import_react2.useCallback)(
|
|
680
296
|
(completion2) => {
|
|
681
297
|
mutate(completion2, false);
|
|
682
298
|
},
|
|
683
299
|
[mutate]
|
|
684
300
|
);
|
|
685
|
-
const complete = (0,
|
|
301
|
+
const complete = (0, import_react2.useCallback)(
|
|
686
302
|
async (prompt, options) => {
|
|
687
303
|
return triggerRequest(prompt, options);
|
|
688
304
|
},
|
|
689
305
|
[triggerRequest]
|
|
690
306
|
);
|
|
691
|
-
const [input, setInput] = (0,
|
|
692
|
-
const handleSubmit = (0,
|
|
307
|
+
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
|
308
|
+
const handleSubmit = (0, import_react2.useCallback)(
|
|
693
309
|
(event) => {
|
|
694
310
|
var _a;
|
|
695
311
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
@@ -697,7 +313,7 @@ function useCompletion({
|
|
|
697
313
|
},
|
|
698
314
|
[input, complete]
|
|
699
315
|
);
|
|
700
|
-
const handleInputChange = (0,
|
|
316
|
+
const handleInputChange = (0, import_react2.useCallback)(
|
|
701
317
|
(e) => {
|
|
702
318
|
setInput(e.target.value);
|
|
703
319
|
},
|
|
@@ -713,17 +329,16 @@ function useCompletion({
|
|
|
713
329
|
setInput,
|
|
714
330
|
handleInputChange,
|
|
715
331
|
handleSubmit,
|
|
716
|
-
isLoading
|
|
717
|
-
data: streamData
|
|
332
|
+
isLoading
|
|
718
333
|
};
|
|
719
334
|
}
|
|
720
335
|
|
|
721
336
|
// src/use-object.ts
|
|
722
|
-
var
|
|
723
|
-
var
|
|
724
|
-
var
|
|
725
|
-
var
|
|
726
|
-
var
|
|
337
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
338
|
+
var import_ai3 = require("ai");
|
|
339
|
+
var import_react3 = require("react");
|
|
340
|
+
var import_swr2 = __toESM(require("swr"));
|
|
341
|
+
var getOriginalFetch = () => fetch;
|
|
727
342
|
function useObject({
|
|
728
343
|
api,
|
|
729
344
|
id,
|
|
@@ -736,17 +351,17 @@ function useObject({
|
|
|
736
351
|
headers,
|
|
737
352
|
credentials
|
|
738
353
|
}) {
|
|
739
|
-
const hookId = (0,
|
|
354
|
+
const hookId = (0, import_react3.useId)();
|
|
740
355
|
const completionId = id != null ? id : hookId;
|
|
741
|
-
const { data, mutate } = (0,
|
|
356
|
+
const { data, mutate } = (0, import_swr2.default)(
|
|
742
357
|
[api, completionId],
|
|
743
358
|
null,
|
|
744
359
|
{ fallbackData: initialValue }
|
|
745
360
|
);
|
|
746
|
-
const [error, setError] = (0,
|
|
747
|
-
const [isLoading, setIsLoading] = (0,
|
|
748
|
-
const abortControllerRef = (0,
|
|
749
|
-
const stop = (0,
|
|
361
|
+
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
362
|
+
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
363
|
+
const abortControllerRef = (0, import_react3.useRef)(null);
|
|
364
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
750
365
|
var _a;
|
|
751
366
|
try {
|
|
752
367
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -764,7 +379,7 @@ function useObject({
|
|
|
764
379
|
setError(void 0);
|
|
765
380
|
const abortController = new AbortController();
|
|
766
381
|
abortControllerRef.current = abortController;
|
|
767
|
-
const actualFetch = fetch2 != null ? fetch2 :
|
|
382
|
+
const actualFetch = fetch2 != null ? fetch2 : getOriginalFetch();
|
|
768
383
|
const response = await actualFetch(api, {
|
|
769
384
|
method: "POST",
|
|
770
385
|
headers: {
|
|
@@ -787,22 +402,22 @@ function useObject({
|
|
|
787
402
|
let latestObject = void 0;
|
|
788
403
|
await response.body.pipeThrough(new TextDecoderStream()).pipeTo(
|
|
789
404
|
new WritableStream({
|
|
790
|
-
write(chunk) {
|
|
405
|
+
async write(chunk) {
|
|
791
406
|
accumulatedText += chunk;
|
|
792
|
-
const { value } = (0,
|
|
407
|
+
const { value } = await (0, import_ai3.parsePartialJson)(accumulatedText);
|
|
793
408
|
const currentObject = value;
|
|
794
|
-
if (!(0,
|
|
409
|
+
if (!(0, import_ai3.isDeepEqualData)(latestObject, currentObject)) {
|
|
795
410
|
latestObject = currentObject;
|
|
796
411
|
mutate(currentObject);
|
|
797
412
|
}
|
|
798
413
|
},
|
|
799
|
-
close() {
|
|
414
|
+
async close() {
|
|
800
415
|
setIsLoading(false);
|
|
801
416
|
abortControllerRef.current = null;
|
|
802
417
|
if (onFinish != null) {
|
|
803
|
-
const validationResult = (0,
|
|
418
|
+
const validationResult = await (0, import_provider_utils.safeValidateTypes)({
|
|
804
419
|
value: latestObject,
|
|
805
|
-
schema: (0,
|
|
420
|
+
schema: (0, import_ai3.asSchema)(schema)
|
|
806
421
|
});
|
|
807
422
|
onFinish(
|
|
808
423
|
validationResult.success ? { object: validationResult.value, error: void 0 } : { object: void 0, error: validationResult.error }
|
|
@@ -812,7 +427,7 @@ function useObject({
|
|
|
812
427
|
})
|
|
813
428
|
);
|
|
814
429
|
} catch (error2) {
|
|
815
|
-
if ((0,
|
|
430
|
+
if ((0, import_provider_utils.isAbortError)(error2)) {
|
|
816
431
|
return;
|
|
817
432
|
}
|
|
818
433
|
if (onError && error2 instanceof Error) {
|
|
@@ -834,7 +449,6 @@ var experimental_useObject = useObject;
|
|
|
834
449
|
// Annotate the CommonJS export names for ESM import in node:
|
|
835
450
|
0 && (module.exports = {
|
|
836
451
|
experimental_useObject,
|
|
837
|
-
useAssistant,
|
|
838
452
|
useChat,
|
|
839
453
|
useCompletion
|
|
840
454
|
});
|