@ai-sdk/react 1.2.6 → 2.0.0-canary.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/CHANGELOG.md +20 -4
- package/README.md +1 -1
- package/dist/index.d.mts +2 -61
- package/dist/index.d.ts +2 -61
- package/dist/index.js +73 -242
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +44 -215
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -31,182 +31,14 @@ 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
39
|
// src/use-chat.ts
|
|
208
|
-
var
|
|
209
|
-
var
|
|
40
|
+
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
|
41
|
+
var import_react2 = require("react");
|
|
210
42
|
var import_swr = __toESM(require("swr"));
|
|
211
43
|
|
|
212
44
|
// src/throttle.ts
|
|
@@ -216,12 +48,12 @@ function throttle(fn, waitMs) {
|
|
|
216
48
|
}
|
|
217
49
|
|
|
218
50
|
// src/util/use-stable-value.ts
|
|
219
|
-
var
|
|
220
|
-
var
|
|
51
|
+
var import_ui_utils = require("@ai-sdk/ui-utils");
|
|
52
|
+
var import_react = require("react");
|
|
221
53
|
function useStableValue(latestValue) {
|
|
222
|
-
const [value, setValue] = (0,
|
|
223
|
-
(0,
|
|
224
|
-
if (!(0,
|
|
54
|
+
const [value, setValue] = (0, import_react.useState)(latestValue);
|
|
55
|
+
(0, import_react.useEffect)(() => {
|
|
56
|
+
if (!(0, import_ui_utils.isDeepEqualData)(latestValue, value)) {
|
|
225
57
|
setValue(latestValue);
|
|
226
58
|
}
|
|
227
59
|
}, [latestValue, value]);
|
|
@@ -245,17 +77,17 @@ function useChat({
|
|
|
245
77
|
credentials,
|
|
246
78
|
headers,
|
|
247
79
|
body,
|
|
248
|
-
generateId
|
|
80
|
+
generateId = import_ui_utils2.generateId,
|
|
249
81
|
fetch: fetch2,
|
|
250
82
|
keepLastMessageOnError = true,
|
|
251
83
|
experimental_throttle: throttleWaitMs
|
|
252
84
|
} = {}) {
|
|
253
|
-
const [hookId] = (0,
|
|
85
|
+
const [hookId] = (0, import_react2.useState)(generateId);
|
|
254
86
|
const chatId = id != null ? id : hookId;
|
|
255
87
|
const chatKey = typeof api === "string" ? [api, chatId] : chatId;
|
|
256
88
|
const stableInitialMessages = useStableValue(initialMessages != null ? initialMessages : []);
|
|
257
|
-
const processedInitialMessages = (0,
|
|
258
|
-
() => (0,
|
|
89
|
+
const processedInitialMessages = (0, import_react2.useMemo)(
|
|
90
|
+
() => (0, import_ui_utils2.fillMessageParts)(stableInitialMessages),
|
|
259
91
|
[stableInitialMessages]
|
|
260
92
|
);
|
|
261
93
|
const { data: messages, mutate } = (0, import_swr.default)(
|
|
@@ -263,38 +95,38 @@ function useChat({
|
|
|
263
95
|
null,
|
|
264
96
|
{ fallbackData: processedInitialMessages }
|
|
265
97
|
);
|
|
266
|
-
const messagesRef = (0,
|
|
267
|
-
(0,
|
|
98
|
+
const messagesRef = (0, import_react2.useRef)(messages || []);
|
|
99
|
+
(0, import_react2.useEffect)(() => {
|
|
268
100
|
messagesRef.current = messages || [];
|
|
269
101
|
}, [messages]);
|
|
270
102
|
const { data: streamData, mutate: mutateStreamData } = (0, import_swr.default)([chatKey, "streamData"], null);
|
|
271
|
-
const streamDataRef = (0,
|
|
272
|
-
(0,
|
|
103
|
+
const streamDataRef = (0, import_react2.useRef)(streamData);
|
|
104
|
+
(0, import_react2.useEffect)(() => {
|
|
273
105
|
streamDataRef.current = streamData;
|
|
274
106
|
}, [streamData]);
|
|
275
107
|
const { data: status = "ready", mutate: mutateStatus } = (0, import_swr.default)([chatKey, "status"], null);
|
|
276
108
|
const { data: error = void 0, mutate: setError } = (0, import_swr.default)([chatKey, "error"], null);
|
|
277
|
-
const abortControllerRef = (0,
|
|
278
|
-
const extraMetadataRef = (0,
|
|
109
|
+
const abortControllerRef = (0, import_react2.useRef)(null);
|
|
110
|
+
const extraMetadataRef = (0, import_react2.useRef)({
|
|
279
111
|
credentials,
|
|
280
112
|
headers,
|
|
281
113
|
body
|
|
282
114
|
});
|
|
283
|
-
(0,
|
|
115
|
+
(0, import_react2.useEffect)(() => {
|
|
284
116
|
extraMetadataRef.current = {
|
|
285
117
|
credentials,
|
|
286
118
|
headers,
|
|
287
119
|
body
|
|
288
120
|
};
|
|
289
121
|
}, [credentials, headers, body]);
|
|
290
|
-
const triggerRequest = (0,
|
|
122
|
+
const triggerRequest = (0, import_react2.useCallback)(
|
|
291
123
|
async (chatRequest) => {
|
|
292
124
|
var _a, _b;
|
|
293
125
|
mutateStatus("submitted");
|
|
294
126
|
setError(void 0);
|
|
295
|
-
const chatMessages = (0,
|
|
127
|
+
const chatMessages = (0, import_ui_utils2.fillMessageParts)(chatRequest.messages);
|
|
296
128
|
const messageCount = chatMessages.length;
|
|
297
|
-
const maxStep = (0,
|
|
129
|
+
const maxStep = (0, import_ui_utils2.extractMaxToolInvocationStep)(
|
|
298
130
|
(_a = chatMessages[chatMessages.length - 1]) == null ? void 0 : _a.toolInvocations
|
|
299
131
|
);
|
|
300
132
|
try {
|
|
@@ -329,7 +161,7 @@ function useChat({
|
|
|
329
161
|
})
|
|
330
162
|
);
|
|
331
163
|
const existingData = streamDataRef.current;
|
|
332
|
-
await (0,
|
|
164
|
+
await (0, import_ui_utils2.callChatApi)({
|
|
333
165
|
api,
|
|
334
166
|
body: (_b = experimental_prepareRequestBody == null ? void 0 : experimental_prepareRequestBody({
|
|
335
167
|
id: chatId,
|
|
@@ -374,7 +206,7 @@ function useChat({
|
|
|
374
206
|
},
|
|
375
207
|
onToolCall,
|
|
376
208
|
onFinish,
|
|
377
|
-
generateId
|
|
209
|
+
generateId,
|
|
378
210
|
fetch: fetch2,
|
|
379
211
|
lastMessage: chatMessages[chatMessages.length - 1]
|
|
380
212
|
});
|
|
@@ -393,7 +225,7 @@ function useChat({
|
|
|
393
225
|
mutateStatus("error");
|
|
394
226
|
}
|
|
395
227
|
const messages2 = messagesRef.current;
|
|
396
|
-
if ((0,
|
|
228
|
+
if ((0, import_ui_utils2.shouldResubmitMessages)({
|
|
397
229
|
originalMaxToolInvocationStep: maxStep,
|
|
398
230
|
originalMessageCount: messageCount,
|
|
399
231
|
maxSteps,
|
|
@@ -420,14 +252,14 @@ function useChat({
|
|
|
420
252
|
maxSteps,
|
|
421
253
|
messagesRef,
|
|
422
254
|
abortControllerRef,
|
|
423
|
-
|
|
255
|
+
generateId,
|
|
424
256
|
fetch2,
|
|
425
257
|
keepLastMessageOnError,
|
|
426
258
|
throttleWaitMs,
|
|
427
259
|
chatId
|
|
428
260
|
]
|
|
429
261
|
);
|
|
430
|
-
const append = (0,
|
|
262
|
+
const append = (0, import_react2.useCallback)(
|
|
431
263
|
async (message, {
|
|
432
264
|
data,
|
|
433
265
|
headers: headers2,
|
|
@@ -435,21 +267,21 @@ function useChat({
|
|
|
435
267
|
experimental_attachments
|
|
436
268
|
} = {}) => {
|
|
437
269
|
var _a, _b;
|
|
438
|
-
const attachmentsForRequest = await (0,
|
|
270
|
+
const attachmentsForRequest = await (0, import_ui_utils2.prepareAttachmentsForRequest)(
|
|
439
271
|
experimental_attachments
|
|
440
272
|
);
|
|
441
273
|
const messages2 = messagesRef.current.concat({
|
|
442
274
|
...message,
|
|
443
|
-
id: (_a = message.id) != null ? _a :
|
|
275
|
+
id: (_a = message.id) != null ? _a : generateId(),
|
|
444
276
|
createdAt: (_b = message.createdAt) != null ? _b : /* @__PURE__ */ new Date(),
|
|
445
277
|
experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : void 0,
|
|
446
|
-
parts: (0,
|
|
278
|
+
parts: (0, import_ui_utils2.getMessageParts)(message)
|
|
447
279
|
});
|
|
448
280
|
return triggerRequest({ messages: messages2, headers: headers2, body: body2, data });
|
|
449
281
|
},
|
|
450
|
-
[triggerRequest,
|
|
282
|
+
[triggerRequest, generateId]
|
|
451
283
|
);
|
|
452
|
-
const reload = (0,
|
|
284
|
+
const reload = (0, import_react2.useCallback)(
|
|
453
285
|
async ({ data, headers: headers2, body: body2 } = {}) => {
|
|
454
286
|
const messages2 = messagesRef.current;
|
|
455
287
|
if (messages2.length === 0) {
|
|
@@ -465,24 +297,24 @@ function useChat({
|
|
|
465
297
|
},
|
|
466
298
|
[triggerRequest]
|
|
467
299
|
);
|
|
468
|
-
const stop = (0,
|
|
300
|
+
const stop = (0, import_react2.useCallback)(() => {
|
|
469
301
|
if (abortControllerRef.current) {
|
|
470
302
|
abortControllerRef.current.abort();
|
|
471
303
|
abortControllerRef.current = null;
|
|
472
304
|
}
|
|
473
305
|
}, []);
|
|
474
|
-
const setMessages = (0,
|
|
306
|
+
const setMessages = (0, import_react2.useCallback)(
|
|
475
307
|
(messages2) => {
|
|
476
308
|
if (typeof messages2 === "function") {
|
|
477
309
|
messages2 = messages2(messagesRef.current);
|
|
478
310
|
}
|
|
479
|
-
const messagesWithParts = (0,
|
|
311
|
+
const messagesWithParts = (0, import_ui_utils2.fillMessageParts)(messages2);
|
|
480
312
|
mutate(messagesWithParts, false);
|
|
481
313
|
messagesRef.current = messagesWithParts;
|
|
482
314
|
},
|
|
483
315
|
[mutate]
|
|
484
316
|
);
|
|
485
|
-
const setData = (0,
|
|
317
|
+
const setData = (0, import_react2.useCallback)(
|
|
486
318
|
(data) => {
|
|
487
319
|
if (typeof data === "function") {
|
|
488
320
|
data = data(streamDataRef.current);
|
|
@@ -492,8 +324,8 @@ function useChat({
|
|
|
492
324
|
},
|
|
493
325
|
[mutateStreamData]
|
|
494
326
|
);
|
|
495
|
-
const [input, setInput] = (0,
|
|
496
|
-
const handleSubmit = (0,
|
|
327
|
+
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
|
328
|
+
const handleSubmit = (0, import_react2.useCallback)(
|
|
497
329
|
async (event, options = {}, metadata) => {
|
|
498
330
|
var _a;
|
|
499
331
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
@@ -505,11 +337,11 @@ function useChat({
|
|
|
505
337
|
...metadata
|
|
506
338
|
};
|
|
507
339
|
}
|
|
508
|
-
const attachmentsForRequest = await (0,
|
|
340
|
+
const attachmentsForRequest = await (0, import_ui_utils2.prepareAttachmentsForRequest)(
|
|
509
341
|
options.experimental_attachments
|
|
510
342
|
);
|
|
511
343
|
const messages2 = messagesRef.current.concat({
|
|
512
|
-
id:
|
|
344
|
+
id: generateId(),
|
|
513
345
|
createdAt: /* @__PURE__ */ new Date(),
|
|
514
346
|
role: "user",
|
|
515
347
|
content: input,
|
|
@@ -525,15 +357,15 @@ function useChat({
|
|
|
525
357
|
triggerRequest(chatRequest);
|
|
526
358
|
setInput("");
|
|
527
359
|
},
|
|
528
|
-
[input,
|
|
360
|
+
[input, generateId, triggerRequest]
|
|
529
361
|
);
|
|
530
362
|
const handleInputChange = (e) => {
|
|
531
363
|
setInput(e.target.value);
|
|
532
364
|
};
|
|
533
|
-
const addToolResult = (0,
|
|
365
|
+
const addToolResult = (0, import_react2.useCallback)(
|
|
534
366
|
({ toolCallId, result }) => {
|
|
535
367
|
const currentMessages = messagesRef.current;
|
|
536
|
-
(0,
|
|
368
|
+
(0, import_ui_utils2.updateToolCallResult)({
|
|
537
369
|
messages: currentMessages,
|
|
538
370
|
toolCallId,
|
|
539
371
|
toolResult: result
|
|
@@ -549,7 +381,7 @@ function useChat({
|
|
|
549
381
|
return;
|
|
550
382
|
}
|
|
551
383
|
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
552
|
-
if ((0,
|
|
384
|
+
if ((0, import_ui_utils2.isAssistantMessageWithCompletedToolCalls)(lastMessage)) {
|
|
553
385
|
triggerRequest({ messages: currentMessages });
|
|
554
386
|
}
|
|
555
387
|
},
|
|
@@ -576,8 +408,8 @@ function useChat({
|
|
|
576
408
|
}
|
|
577
409
|
|
|
578
410
|
// src/use-completion.ts
|
|
579
|
-
var
|
|
580
|
-
var
|
|
411
|
+
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
|
412
|
+
var import_react3 = require("react");
|
|
581
413
|
var import_swr2 = __toESM(require("swr"));
|
|
582
414
|
function useCompletion({
|
|
583
415
|
api = "/api/completion",
|
|
@@ -594,7 +426,7 @@ function useCompletion({
|
|
|
594
426
|
onError,
|
|
595
427
|
experimental_throttle: throttleWaitMs
|
|
596
428
|
} = {}) {
|
|
597
|
-
const hookId = (0,
|
|
429
|
+
const hookId = (0, import_react3.useId)();
|
|
598
430
|
const completionId = id || hookId;
|
|
599
431
|
const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {
|
|
600
432
|
fallbackData: initialCompletion
|
|
@@ -604,23 +436,23 @@ function useCompletion({
|
|
|
604
436
|
null
|
|
605
437
|
);
|
|
606
438
|
const { data: streamData, mutate: mutateStreamData } = (0, import_swr2.default)([completionId, "streamData"], null);
|
|
607
|
-
const [error, setError] = (0,
|
|
439
|
+
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
608
440
|
const completion = data;
|
|
609
|
-
const [abortController, setAbortController] = (0,
|
|
610
|
-
const extraMetadataRef = (0,
|
|
441
|
+
const [abortController, setAbortController] = (0, import_react3.useState)(null);
|
|
442
|
+
const extraMetadataRef = (0, import_react3.useRef)({
|
|
611
443
|
credentials,
|
|
612
444
|
headers,
|
|
613
445
|
body
|
|
614
446
|
});
|
|
615
|
-
(0,
|
|
447
|
+
(0, import_react3.useEffect)(() => {
|
|
616
448
|
extraMetadataRef.current = {
|
|
617
449
|
credentials,
|
|
618
450
|
headers,
|
|
619
451
|
body
|
|
620
452
|
};
|
|
621
453
|
}, [credentials, headers, body]);
|
|
622
|
-
const triggerRequest = (0,
|
|
623
|
-
async (prompt, options) => (0,
|
|
454
|
+
const triggerRequest = (0, import_react3.useCallback)(
|
|
455
|
+
async (prompt, options) => (0, import_ui_utils3.callCompletionApi)({
|
|
624
456
|
api,
|
|
625
457
|
prompt,
|
|
626
458
|
credentials: extraMetadataRef.current.credentials,
|
|
@@ -664,26 +496,26 @@ function useCompletion({
|
|
|
664
496
|
throttleWaitMs
|
|
665
497
|
]
|
|
666
498
|
);
|
|
667
|
-
const stop = (0,
|
|
499
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
668
500
|
if (abortController) {
|
|
669
501
|
abortController.abort();
|
|
670
502
|
setAbortController(null);
|
|
671
503
|
}
|
|
672
504
|
}, [abortController]);
|
|
673
|
-
const setCompletion = (0,
|
|
505
|
+
const setCompletion = (0, import_react3.useCallback)(
|
|
674
506
|
(completion2) => {
|
|
675
507
|
mutate(completion2, false);
|
|
676
508
|
},
|
|
677
509
|
[mutate]
|
|
678
510
|
);
|
|
679
|
-
const complete = (0,
|
|
511
|
+
const complete = (0, import_react3.useCallback)(
|
|
680
512
|
async (prompt, options) => {
|
|
681
513
|
return triggerRequest(prompt, options);
|
|
682
514
|
},
|
|
683
515
|
[triggerRequest]
|
|
684
516
|
);
|
|
685
|
-
const [input, setInput] = (0,
|
|
686
|
-
const handleSubmit = (0,
|
|
517
|
+
const [input, setInput] = (0, import_react3.useState)(initialInput);
|
|
518
|
+
const handleSubmit = (0, import_react3.useCallback)(
|
|
687
519
|
(event) => {
|
|
688
520
|
var _a;
|
|
689
521
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
@@ -691,7 +523,7 @@ function useCompletion({
|
|
|
691
523
|
},
|
|
692
524
|
[input, complete]
|
|
693
525
|
);
|
|
694
|
-
const handleInputChange = (0,
|
|
526
|
+
const handleInputChange = (0, import_react3.useCallback)(
|
|
695
527
|
(e) => {
|
|
696
528
|
setInput(e.target.value);
|
|
697
529
|
},
|
|
@@ -713,11 +545,11 @@ function useCompletion({
|
|
|
713
545
|
}
|
|
714
546
|
|
|
715
547
|
// src/use-object.ts
|
|
716
|
-
var
|
|
717
|
-
var
|
|
718
|
-
var
|
|
548
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
549
|
+
var import_ui_utils4 = require("@ai-sdk/ui-utils");
|
|
550
|
+
var import_react4 = require("react");
|
|
719
551
|
var import_swr3 = __toESM(require("swr"));
|
|
720
|
-
var
|
|
552
|
+
var getOriginalFetch = () => fetch;
|
|
721
553
|
function useObject({
|
|
722
554
|
api,
|
|
723
555
|
id,
|
|
@@ -730,17 +562,17 @@ function useObject({
|
|
|
730
562
|
headers,
|
|
731
563
|
credentials
|
|
732
564
|
}) {
|
|
733
|
-
const hookId = (0,
|
|
565
|
+
const hookId = (0, import_react4.useId)();
|
|
734
566
|
const completionId = id != null ? id : hookId;
|
|
735
567
|
const { data, mutate } = (0, import_swr3.default)(
|
|
736
568
|
[api, completionId],
|
|
737
569
|
null,
|
|
738
570
|
{ fallbackData: initialValue }
|
|
739
571
|
);
|
|
740
|
-
const [error, setError] = (0,
|
|
741
|
-
const [isLoading, setIsLoading] = (0,
|
|
742
|
-
const abortControllerRef = (0,
|
|
743
|
-
const stop = (0,
|
|
572
|
+
const [error, setError] = (0, import_react4.useState)(void 0);
|
|
573
|
+
const [isLoading, setIsLoading] = (0, import_react4.useState)(false);
|
|
574
|
+
const abortControllerRef = (0, import_react4.useRef)(null);
|
|
575
|
+
const stop = (0, import_react4.useCallback)(() => {
|
|
744
576
|
var _a;
|
|
745
577
|
try {
|
|
746
578
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -758,7 +590,7 @@ function useObject({
|
|
|
758
590
|
setError(void 0);
|
|
759
591
|
const abortController = new AbortController();
|
|
760
592
|
abortControllerRef.current = abortController;
|
|
761
|
-
const actualFetch = fetch2 != null ? fetch2 :
|
|
593
|
+
const actualFetch = fetch2 != null ? fetch2 : getOriginalFetch();
|
|
762
594
|
const response = await actualFetch(api, {
|
|
763
595
|
method: "POST",
|
|
764
596
|
headers: {
|
|
@@ -783,9 +615,9 @@ function useObject({
|
|
|
783
615
|
new WritableStream({
|
|
784
616
|
write(chunk) {
|
|
785
617
|
accumulatedText += chunk;
|
|
786
|
-
const { value } = (0,
|
|
618
|
+
const { value } = (0, import_ui_utils4.parsePartialJson)(accumulatedText);
|
|
787
619
|
const currentObject = value;
|
|
788
|
-
if (!(0,
|
|
620
|
+
if (!(0, import_ui_utils4.isDeepEqualData)(latestObject, currentObject)) {
|
|
789
621
|
latestObject = currentObject;
|
|
790
622
|
mutate(currentObject);
|
|
791
623
|
}
|
|
@@ -794,9 +626,9 @@ function useObject({
|
|
|
794
626
|
setIsLoading(false);
|
|
795
627
|
abortControllerRef.current = null;
|
|
796
628
|
if (onFinish != null) {
|
|
797
|
-
const validationResult = (0,
|
|
629
|
+
const validationResult = (0, import_provider_utils.safeValidateTypes)({
|
|
798
630
|
value: latestObject,
|
|
799
|
-
schema: (0,
|
|
631
|
+
schema: (0, import_ui_utils4.asSchema)(schema)
|
|
800
632
|
});
|
|
801
633
|
onFinish(
|
|
802
634
|
validationResult.success ? { object: validationResult.value, error: void 0 } : { object: void 0, error: validationResult.error }
|
|
@@ -806,7 +638,7 @@ function useObject({
|
|
|
806
638
|
})
|
|
807
639
|
);
|
|
808
640
|
} catch (error2) {
|
|
809
|
-
if ((0,
|
|
641
|
+
if ((0, import_provider_utils.isAbortError)(error2)) {
|
|
810
642
|
return;
|
|
811
643
|
}
|
|
812
644
|
if (onError && error2 instanceof Error) {
|
|
@@ -828,7 +660,6 @@ var experimental_useObject = useObject;
|
|
|
828
660
|
// Annotate the CommonJS export names for ESM import in node:
|
|
829
661
|
0 && (module.exports = {
|
|
830
662
|
experimental_useObject,
|
|
831
|
-
useAssistant,
|
|
832
663
|
useChat,
|
|
833
664
|
useCompletion
|
|
834
665
|
});
|