@ai-sdk/react 1.2.4 → 2.0.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/README.md +1 -1
- package/dist/index.d.mts +2 -61
- package/dist/index.d.ts +2 -61
- package/dist/index.js +84 -244
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +55 -217
- 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,26 +357,35 @@ 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
|
|
540
372
|
});
|
|
541
|
-
mutate(
|
|
373
|
+
mutate(
|
|
374
|
+
[
|
|
375
|
+
...currentMessages.slice(0, currentMessages.length - 1),
|
|
376
|
+
{ ...currentMessages[currentMessages.length - 1] }
|
|
377
|
+
],
|
|
378
|
+
false
|
|
379
|
+
);
|
|
380
|
+
if (status === "submitted" || status === "streaming") {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
542
383
|
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
543
|
-
if ((0,
|
|
384
|
+
if ((0, import_ui_utils2.isAssistantMessageWithCompletedToolCalls)(lastMessage)) {
|
|
544
385
|
triggerRequest({ messages: currentMessages });
|
|
545
386
|
}
|
|
546
387
|
},
|
|
547
|
-
[mutate, triggerRequest]
|
|
388
|
+
[mutate, status, triggerRequest]
|
|
548
389
|
);
|
|
549
390
|
return {
|
|
550
391
|
messages: messages != null ? messages : [],
|
|
@@ -567,8 +408,8 @@ function useChat({
|
|
|
567
408
|
}
|
|
568
409
|
|
|
569
410
|
// src/use-completion.ts
|
|
570
|
-
var
|
|
571
|
-
var
|
|
411
|
+
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
|
412
|
+
var import_react3 = require("react");
|
|
572
413
|
var import_swr2 = __toESM(require("swr"));
|
|
573
414
|
function useCompletion({
|
|
574
415
|
api = "/api/completion",
|
|
@@ -585,7 +426,7 @@ function useCompletion({
|
|
|
585
426
|
onError,
|
|
586
427
|
experimental_throttle: throttleWaitMs
|
|
587
428
|
} = {}) {
|
|
588
|
-
const hookId = (0,
|
|
429
|
+
const hookId = (0, import_react3.useId)();
|
|
589
430
|
const completionId = id || hookId;
|
|
590
431
|
const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {
|
|
591
432
|
fallbackData: initialCompletion
|
|
@@ -595,23 +436,23 @@ function useCompletion({
|
|
|
595
436
|
null
|
|
596
437
|
);
|
|
597
438
|
const { data: streamData, mutate: mutateStreamData } = (0, import_swr2.default)([completionId, "streamData"], null);
|
|
598
|
-
const [error, setError] = (0,
|
|
439
|
+
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
599
440
|
const completion = data;
|
|
600
|
-
const [abortController, setAbortController] = (0,
|
|
601
|
-
const extraMetadataRef = (0,
|
|
441
|
+
const [abortController, setAbortController] = (0, import_react3.useState)(null);
|
|
442
|
+
const extraMetadataRef = (0, import_react3.useRef)({
|
|
602
443
|
credentials,
|
|
603
444
|
headers,
|
|
604
445
|
body
|
|
605
446
|
});
|
|
606
|
-
(0,
|
|
447
|
+
(0, import_react3.useEffect)(() => {
|
|
607
448
|
extraMetadataRef.current = {
|
|
608
449
|
credentials,
|
|
609
450
|
headers,
|
|
610
451
|
body
|
|
611
452
|
};
|
|
612
453
|
}, [credentials, headers, body]);
|
|
613
|
-
const triggerRequest = (0,
|
|
614
|
-
async (prompt, options) => (0,
|
|
454
|
+
const triggerRequest = (0, import_react3.useCallback)(
|
|
455
|
+
async (prompt, options) => (0, import_ui_utils3.callCompletionApi)({
|
|
615
456
|
api,
|
|
616
457
|
prompt,
|
|
617
458
|
credentials: extraMetadataRef.current.credentials,
|
|
@@ -655,26 +496,26 @@ function useCompletion({
|
|
|
655
496
|
throttleWaitMs
|
|
656
497
|
]
|
|
657
498
|
);
|
|
658
|
-
const stop = (0,
|
|
499
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
659
500
|
if (abortController) {
|
|
660
501
|
abortController.abort();
|
|
661
502
|
setAbortController(null);
|
|
662
503
|
}
|
|
663
504
|
}, [abortController]);
|
|
664
|
-
const setCompletion = (0,
|
|
505
|
+
const setCompletion = (0, import_react3.useCallback)(
|
|
665
506
|
(completion2) => {
|
|
666
507
|
mutate(completion2, false);
|
|
667
508
|
},
|
|
668
509
|
[mutate]
|
|
669
510
|
);
|
|
670
|
-
const complete = (0,
|
|
511
|
+
const complete = (0, import_react3.useCallback)(
|
|
671
512
|
async (prompt, options) => {
|
|
672
513
|
return triggerRequest(prompt, options);
|
|
673
514
|
},
|
|
674
515
|
[triggerRequest]
|
|
675
516
|
);
|
|
676
|
-
const [input, setInput] = (0,
|
|
677
|
-
const handleSubmit = (0,
|
|
517
|
+
const [input, setInput] = (0, import_react3.useState)(initialInput);
|
|
518
|
+
const handleSubmit = (0, import_react3.useCallback)(
|
|
678
519
|
(event) => {
|
|
679
520
|
var _a;
|
|
680
521
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
@@ -682,7 +523,7 @@ function useCompletion({
|
|
|
682
523
|
},
|
|
683
524
|
[input, complete]
|
|
684
525
|
);
|
|
685
|
-
const handleInputChange = (0,
|
|
526
|
+
const handleInputChange = (0, import_react3.useCallback)(
|
|
686
527
|
(e) => {
|
|
687
528
|
setInput(e.target.value);
|
|
688
529
|
},
|
|
@@ -704,11 +545,11 @@ function useCompletion({
|
|
|
704
545
|
}
|
|
705
546
|
|
|
706
547
|
// src/use-object.ts
|
|
707
|
-
var
|
|
708
|
-
var
|
|
709
|
-
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");
|
|
710
551
|
var import_swr3 = __toESM(require("swr"));
|
|
711
|
-
var
|
|
552
|
+
var getOriginalFetch = () => fetch;
|
|
712
553
|
function useObject({
|
|
713
554
|
api,
|
|
714
555
|
id,
|
|
@@ -721,17 +562,17 @@ function useObject({
|
|
|
721
562
|
headers,
|
|
722
563
|
credentials
|
|
723
564
|
}) {
|
|
724
|
-
const hookId = (0,
|
|
565
|
+
const hookId = (0, import_react4.useId)();
|
|
725
566
|
const completionId = id != null ? id : hookId;
|
|
726
567
|
const { data, mutate } = (0, import_swr3.default)(
|
|
727
568
|
[api, completionId],
|
|
728
569
|
null,
|
|
729
570
|
{ fallbackData: initialValue }
|
|
730
571
|
);
|
|
731
|
-
const [error, setError] = (0,
|
|
732
|
-
const [isLoading, setIsLoading] = (0,
|
|
733
|
-
const abortControllerRef = (0,
|
|
734
|
-
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)(() => {
|
|
735
576
|
var _a;
|
|
736
577
|
try {
|
|
737
578
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -749,7 +590,7 @@ function useObject({
|
|
|
749
590
|
setError(void 0);
|
|
750
591
|
const abortController = new AbortController();
|
|
751
592
|
abortControllerRef.current = abortController;
|
|
752
|
-
const actualFetch = fetch2 != null ? fetch2 :
|
|
593
|
+
const actualFetch = fetch2 != null ? fetch2 : getOriginalFetch();
|
|
753
594
|
const response = await actualFetch(api, {
|
|
754
595
|
method: "POST",
|
|
755
596
|
headers: {
|
|
@@ -774,9 +615,9 @@ function useObject({
|
|
|
774
615
|
new WritableStream({
|
|
775
616
|
write(chunk) {
|
|
776
617
|
accumulatedText += chunk;
|
|
777
|
-
const { value } = (0,
|
|
618
|
+
const { value } = (0, import_ui_utils4.parsePartialJson)(accumulatedText);
|
|
778
619
|
const currentObject = value;
|
|
779
|
-
if (!(0,
|
|
620
|
+
if (!(0, import_ui_utils4.isDeepEqualData)(latestObject, currentObject)) {
|
|
780
621
|
latestObject = currentObject;
|
|
781
622
|
mutate(currentObject);
|
|
782
623
|
}
|
|
@@ -785,9 +626,9 @@ function useObject({
|
|
|
785
626
|
setIsLoading(false);
|
|
786
627
|
abortControllerRef.current = null;
|
|
787
628
|
if (onFinish != null) {
|
|
788
|
-
const validationResult = (0,
|
|
629
|
+
const validationResult = (0, import_provider_utils.safeValidateTypes)({
|
|
789
630
|
value: latestObject,
|
|
790
|
-
schema: (0,
|
|
631
|
+
schema: (0, import_ui_utils4.asSchema)(schema)
|
|
791
632
|
});
|
|
792
633
|
onFinish(
|
|
793
634
|
validationResult.success ? { object: validationResult.value, error: void 0 } : { object: void 0, error: validationResult.error }
|
|
@@ -797,7 +638,7 @@ function useObject({
|
|
|
797
638
|
})
|
|
798
639
|
);
|
|
799
640
|
} catch (error2) {
|
|
800
|
-
if ((0,
|
|
641
|
+
if ((0, import_provider_utils.isAbortError)(error2)) {
|
|
801
642
|
return;
|
|
802
643
|
}
|
|
803
644
|
if (onError && error2 instanceof Error) {
|
|
@@ -819,7 +660,6 @@ var experimental_useObject = useObject;
|
|
|
819
660
|
// Annotate the CommonJS export names for ESM import in node:
|
|
820
661
|
0 && (module.exports = {
|
|
821
662
|
experimental_useObject,
|
|
822
|
-
useAssistant,
|
|
823
663
|
useChat,
|
|
824
664
|
useCompletion
|
|
825
665
|
});
|