@ai-sdk/react 0.0.0-9477ebb9-20250403064906
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 +1029 -0
- package/LICENSE +13 -0
- package/README.md +7 -0
- package/dist/index.d.mts +238 -0
- package/dist/index.d.ts +238 -0
- package/dist/index.js +666 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +646 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +81 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
experimental_useObject: () => experimental_useObject,
|
|
34
|
+
useChat: () => useChat,
|
|
35
|
+
useCompletion: () => useCompletion
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(src_exports);
|
|
38
|
+
|
|
39
|
+
// src/use-chat.ts
|
|
40
|
+
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
|
41
|
+
var import_react2 = require("react");
|
|
42
|
+
var import_swr = __toESM(require("swr"));
|
|
43
|
+
|
|
44
|
+
// src/throttle.ts
|
|
45
|
+
var import_throttleit = __toESM(require("throttleit"));
|
|
46
|
+
function throttle(fn, waitMs) {
|
|
47
|
+
return waitMs != null ? (0, import_throttleit.default)(fn, waitMs) : fn;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/util/use-stable-value.ts
|
|
51
|
+
var import_ui_utils = require("@ai-sdk/ui-utils");
|
|
52
|
+
var import_react = require("react");
|
|
53
|
+
function useStableValue(latestValue) {
|
|
54
|
+
const [value, setValue] = (0, import_react.useState)(latestValue);
|
|
55
|
+
(0, import_react.useEffect)(() => {
|
|
56
|
+
if (!(0, import_ui_utils.isDeepEqualData)(latestValue, value)) {
|
|
57
|
+
setValue(latestValue);
|
|
58
|
+
}
|
|
59
|
+
}, [latestValue, value]);
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/use-chat.ts
|
|
64
|
+
function useChat({
|
|
65
|
+
api = "/api/chat",
|
|
66
|
+
id,
|
|
67
|
+
initialMessages,
|
|
68
|
+
initialInput = "",
|
|
69
|
+
sendExtraMessageFields,
|
|
70
|
+
onToolCall,
|
|
71
|
+
experimental_prepareRequestBody,
|
|
72
|
+
maxSteps = 1,
|
|
73
|
+
streamProtocol = "data",
|
|
74
|
+
onResponse,
|
|
75
|
+
onFinish,
|
|
76
|
+
onError,
|
|
77
|
+
credentials,
|
|
78
|
+
headers,
|
|
79
|
+
body,
|
|
80
|
+
generateId = import_ui_utils2.generateId,
|
|
81
|
+
fetch: fetch2,
|
|
82
|
+
keepLastMessageOnError = true,
|
|
83
|
+
experimental_throttle: throttleWaitMs
|
|
84
|
+
} = {}) {
|
|
85
|
+
const [hookId] = (0, import_react2.useState)(generateId);
|
|
86
|
+
const chatId = id != null ? id : hookId;
|
|
87
|
+
const chatKey = typeof api === "string" ? [api, chatId] : chatId;
|
|
88
|
+
const stableInitialMessages = useStableValue(initialMessages != null ? initialMessages : []);
|
|
89
|
+
const processedInitialMessages = (0, import_react2.useMemo)(
|
|
90
|
+
() => (0, import_ui_utils2.fillMessageParts)(stableInitialMessages),
|
|
91
|
+
[stableInitialMessages]
|
|
92
|
+
);
|
|
93
|
+
const { data: messages, mutate } = (0, import_swr.default)(
|
|
94
|
+
[chatKey, "messages"],
|
|
95
|
+
null,
|
|
96
|
+
{ fallbackData: processedInitialMessages }
|
|
97
|
+
);
|
|
98
|
+
const messagesRef = (0, import_react2.useRef)(messages || []);
|
|
99
|
+
(0, import_react2.useEffect)(() => {
|
|
100
|
+
messagesRef.current = messages || [];
|
|
101
|
+
}, [messages]);
|
|
102
|
+
const { data: streamData, mutate: mutateStreamData } = (0, import_swr.default)([chatKey, "streamData"], null);
|
|
103
|
+
const streamDataRef = (0, import_react2.useRef)(streamData);
|
|
104
|
+
(0, import_react2.useEffect)(() => {
|
|
105
|
+
streamDataRef.current = streamData;
|
|
106
|
+
}, [streamData]);
|
|
107
|
+
const { data: status = "ready", mutate: mutateStatus } = (0, import_swr.default)([chatKey, "status"], null);
|
|
108
|
+
const { data: error = void 0, mutate: setError } = (0, import_swr.default)([chatKey, "error"], null);
|
|
109
|
+
const abortControllerRef = (0, import_react2.useRef)(null);
|
|
110
|
+
const extraMetadataRef = (0, import_react2.useRef)({
|
|
111
|
+
credentials,
|
|
112
|
+
headers,
|
|
113
|
+
body
|
|
114
|
+
});
|
|
115
|
+
(0, import_react2.useEffect)(() => {
|
|
116
|
+
extraMetadataRef.current = {
|
|
117
|
+
credentials,
|
|
118
|
+
headers,
|
|
119
|
+
body
|
|
120
|
+
};
|
|
121
|
+
}, [credentials, headers, body]);
|
|
122
|
+
const triggerRequest = (0, import_react2.useCallback)(
|
|
123
|
+
async (chatRequest) => {
|
|
124
|
+
var _a, _b;
|
|
125
|
+
mutateStatus("submitted");
|
|
126
|
+
setError(void 0);
|
|
127
|
+
const chatMessages = (0, import_ui_utils2.fillMessageParts)(chatRequest.messages);
|
|
128
|
+
const messageCount = chatMessages.length;
|
|
129
|
+
const maxStep = (0, import_ui_utils2.extractMaxToolInvocationStep)(
|
|
130
|
+
(_a = chatMessages[chatMessages.length - 1]) == null ? void 0 : _a.toolInvocations
|
|
131
|
+
);
|
|
132
|
+
try {
|
|
133
|
+
const abortController = new AbortController();
|
|
134
|
+
abortControllerRef.current = abortController;
|
|
135
|
+
const throttledMutate = throttle(mutate, throttleWaitMs);
|
|
136
|
+
const throttledMutateStreamData = throttle(
|
|
137
|
+
mutateStreamData,
|
|
138
|
+
throttleWaitMs
|
|
139
|
+
);
|
|
140
|
+
const previousMessages = messagesRef.current;
|
|
141
|
+
throttledMutate(chatMessages, false);
|
|
142
|
+
const constructedMessagesPayload = sendExtraMessageFields ? chatMessages : chatMessages.map(
|
|
143
|
+
({
|
|
144
|
+
role,
|
|
145
|
+
content,
|
|
146
|
+
experimental_attachments,
|
|
147
|
+
data,
|
|
148
|
+
annotations,
|
|
149
|
+
toolInvocations,
|
|
150
|
+
parts
|
|
151
|
+
}) => ({
|
|
152
|
+
role,
|
|
153
|
+
content,
|
|
154
|
+
...experimental_attachments !== void 0 && {
|
|
155
|
+
experimental_attachments
|
|
156
|
+
},
|
|
157
|
+
...data !== void 0 && { data },
|
|
158
|
+
...annotations !== void 0 && { annotations },
|
|
159
|
+
...toolInvocations !== void 0 && { toolInvocations },
|
|
160
|
+
...parts !== void 0 && { parts }
|
|
161
|
+
})
|
|
162
|
+
);
|
|
163
|
+
const existingData = streamDataRef.current;
|
|
164
|
+
await (0, import_ui_utils2.callChatApi)({
|
|
165
|
+
api,
|
|
166
|
+
body: (_b = experimental_prepareRequestBody == null ? void 0 : experimental_prepareRequestBody({
|
|
167
|
+
id: chatId,
|
|
168
|
+
messages: chatMessages,
|
|
169
|
+
requestData: chatRequest.data,
|
|
170
|
+
requestBody: chatRequest.body
|
|
171
|
+
})) != null ? _b : {
|
|
172
|
+
id: chatId,
|
|
173
|
+
messages: constructedMessagesPayload,
|
|
174
|
+
data: chatRequest.data,
|
|
175
|
+
...extraMetadataRef.current.body,
|
|
176
|
+
...chatRequest.body
|
|
177
|
+
},
|
|
178
|
+
streamProtocol,
|
|
179
|
+
credentials: extraMetadataRef.current.credentials,
|
|
180
|
+
headers: {
|
|
181
|
+
...extraMetadataRef.current.headers,
|
|
182
|
+
...chatRequest.headers
|
|
183
|
+
},
|
|
184
|
+
abortController: () => abortControllerRef.current,
|
|
185
|
+
restoreMessagesOnFailure() {
|
|
186
|
+
if (!keepLastMessageOnError) {
|
|
187
|
+
throttledMutate(previousMessages, false);
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
onResponse,
|
|
191
|
+
onUpdate({ message, data, replaceLastMessage }) {
|
|
192
|
+
mutateStatus("streaming");
|
|
193
|
+
throttledMutate(
|
|
194
|
+
[
|
|
195
|
+
...replaceLastMessage ? chatMessages.slice(0, chatMessages.length - 1) : chatMessages,
|
|
196
|
+
message
|
|
197
|
+
],
|
|
198
|
+
false
|
|
199
|
+
);
|
|
200
|
+
if (data == null ? void 0 : data.length) {
|
|
201
|
+
throttledMutateStreamData(
|
|
202
|
+
[...existingData != null ? existingData : [], ...data],
|
|
203
|
+
false
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
onToolCall,
|
|
208
|
+
onFinish,
|
|
209
|
+
generateId,
|
|
210
|
+
fetch: fetch2,
|
|
211
|
+
lastMessage: chatMessages[chatMessages.length - 1]
|
|
212
|
+
});
|
|
213
|
+
abortControllerRef.current = null;
|
|
214
|
+
mutateStatus("ready");
|
|
215
|
+
} catch (err) {
|
|
216
|
+
if (err.name === "AbortError") {
|
|
217
|
+
abortControllerRef.current = null;
|
|
218
|
+
mutateStatus("ready");
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
if (onError && err instanceof Error) {
|
|
222
|
+
onError(err);
|
|
223
|
+
}
|
|
224
|
+
setError(err);
|
|
225
|
+
mutateStatus("error");
|
|
226
|
+
}
|
|
227
|
+
const messages2 = messagesRef.current;
|
|
228
|
+
if ((0, import_ui_utils2.shouldResubmitMessages)({
|
|
229
|
+
originalMaxToolInvocationStep: maxStep,
|
|
230
|
+
originalMessageCount: messageCount,
|
|
231
|
+
maxSteps,
|
|
232
|
+
messages: messages2
|
|
233
|
+
})) {
|
|
234
|
+
await triggerRequest({ messages: messages2 });
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
[
|
|
238
|
+
mutate,
|
|
239
|
+
mutateStatus,
|
|
240
|
+
api,
|
|
241
|
+
extraMetadataRef,
|
|
242
|
+
onResponse,
|
|
243
|
+
onFinish,
|
|
244
|
+
onError,
|
|
245
|
+
setError,
|
|
246
|
+
mutateStreamData,
|
|
247
|
+
streamDataRef,
|
|
248
|
+
streamProtocol,
|
|
249
|
+
sendExtraMessageFields,
|
|
250
|
+
experimental_prepareRequestBody,
|
|
251
|
+
onToolCall,
|
|
252
|
+
maxSteps,
|
|
253
|
+
messagesRef,
|
|
254
|
+
abortControllerRef,
|
|
255
|
+
generateId,
|
|
256
|
+
fetch2,
|
|
257
|
+
keepLastMessageOnError,
|
|
258
|
+
throttleWaitMs,
|
|
259
|
+
chatId
|
|
260
|
+
]
|
|
261
|
+
);
|
|
262
|
+
const append = (0, import_react2.useCallback)(
|
|
263
|
+
async (message, {
|
|
264
|
+
data,
|
|
265
|
+
headers: headers2,
|
|
266
|
+
body: body2,
|
|
267
|
+
experimental_attachments
|
|
268
|
+
} = {}) => {
|
|
269
|
+
var _a, _b;
|
|
270
|
+
const attachmentsForRequest = await (0, import_ui_utils2.prepareAttachmentsForRequest)(
|
|
271
|
+
experimental_attachments
|
|
272
|
+
);
|
|
273
|
+
const messages2 = messagesRef.current.concat({
|
|
274
|
+
...message,
|
|
275
|
+
id: (_a = message.id) != null ? _a : generateId(),
|
|
276
|
+
createdAt: (_b = message.createdAt) != null ? _b : /* @__PURE__ */ new Date(),
|
|
277
|
+
experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : void 0,
|
|
278
|
+
parts: (0, import_ui_utils2.getMessageParts)(message)
|
|
279
|
+
});
|
|
280
|
+
return triggerRequest({ messages: messages2, headers: headers2, body: body2, data });
|
|
281
|
+
},
|
|
282
|
+
[triggerRequest, generateId]
|
|
283
|
+
);
|
|
284
|
+
const reload = (0, import_react2.useCallback)(
|
|
285
|
+
async ({ data, headers: headers2, body: body2 } = {}) => {
|
|
286
|
+
const messages2 = messagesRef.current;
|
|
287
|
+
if (messages2.length === 0) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
const lastMessage = messages2[messages2.length - 1];
|
|
291
|
+
return triggerRequest({
|
|
292
|
+
messages: lastMessage.role === "assistant" ? messages2.slice(0, -1) : messages2,
|
|
293
|
+
headers: headers2,
|
|
294
|
+
body: body2,
|
|
295
|
+
data
|
|
296
|
+
});
|
|
297
|
+
},
|
|
298
|
+
[triggerRequest]
|
|
299
|
+
);
|
|
300
|
+
const stop = (0, import_react2.useCallback)(() => {
|
|
301
|
+
if (abortControllerRef.current) {
|
|
302
|
+
abortControllerRef.current.abort();
|
|
303
|
+
abortControllerRef.current = null;
|
|
304
|
+
}
|
|
305
|
+
}, []);
|
|
306
|
+
const setMessages = (0, import_react2.useCallback)(
|
|
307
|
+
(messages2) => {
|
|
308
|
+
if (typeof messages2 === "function") {
|
|
309
|
+
messages2 = messages2(messagesRef.current);
|
|
310
|
+
}
|
|
311
|
+
const messagesWithParts = (0, import_ui_utils2.fillMessageParts)(messages2);
|
|
312
|
+
mutate(messagesWithParts, false);
|
|
313
|
+
messagesRef.current = messagesWithParts;
|
|
314
|
+
},
|
|
315
|
+
[mutate]
|
|
316
|
+
);
|
|
317
|
+
const setData = (0, import_react2.useCallback)(
|
|
318
|
+
(data) => {
|
|
319
|
+
if (typeof data === "function") {
|
|
320
|
+
data = data(streamDataRef.current);
|
|
321
|
+
}
|
|
322
|
+
mutateStreamData(data, false);
|
|
323
|
+
streamDataRef.current = data;
|
|
324
|
+
},
|
|
325
|
+
[mutateStreamData]
|
|
326
|
+
);
|
|
327
|
+
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
|
328
|
+
const handleSubmit = (0, import_react2.useCallback)(
|
|
329
|
+
async (event, options = {}, metadata) => {
|
|
330
|
+
var _a;
|
|
331
|
+
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
332
|
+
if (!input && !options.allowEmptySubmit)
|
|
333
|
+
return;
|
|
334
|
+
if (metadata) {
|
|
335
|
+
extraMetadataRef.current = {
|
|
336
|
+
...extraMetadataRef.current,
|
|
337
|
+
...metadata
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
const attachmentsForRequest = await (0, import_ui_utils2.prepareAttachmentsForRequest)(
|
|
341
|
+
options.experimental_attachments
|
|
342
|
+
);
|
|
343
|
+
const messages2 = messagesRef.current.concat({
|
|
344
|
+
id: generateId(),
|
|
345
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
346
|
+
role: "user",
|
|
347
|
+
content: input,
|
|
348
|
+
experimental_attachments: attachmentsForRequest.length > 0 ? attachmentsForRequest : void 0,
|
|
349
|
+
parts: [{ type: "text", text: input }]
|
|
350
|
+
});
|
|
351
|
+
const chatRequest = {
|
|
352
|
+
messages: messages2,
|
|
353
|
+
headers: options.headers,
|
|
354
|
+
body: options.body,
|
|
355
|
+
data: options.data
|
|
356
|
+
};
|
|
357
|
+
triggerRequest(chatRequest);
|
|
358
|
+
setInput("");
|
|
359
|
+
},
|
|
360
|
+
[input, generateId, triggerRequest]
|
|
361
|
+
);
|
|
362
|
+
const handleInputChange = (e) => {
|
|
363
|
+
setInput(e.target.value);
|
|
364
|
+
};
|
|
365
|
+
const addToolResult = (0, import_react2.useCallback)(
|
|
366
|
+
({ toolCallId, result }) => {
|
|
367
|
+
const currentMessages = messagesRef.current;
|
|
368
|
+
(0, import_ui_utils2.updateToolCallResult)({
|
|
369
|
+
messages: currentMessages,
|
|
370
|
+
toolCallId,
|
|
371
|
+
toolResult: result
|
|
372
|
+
});
|
|
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
|
+
}
|
|
383
|
+
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
384
|
+
if ((0, import_ui_utils2.isAssistantMessageWithCompletedToolCalls)(lastMessage)) {
|
|
385
|
+
triggerRequest({ messages: currentMessages });
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
[mutate, status, triggerRequest]
|
|
389
|
+
);
|
|
390
|
+
return {
|
|
391
|
+
messages: messages != null ? messages : [],
|
|
392
|
+
id: chatId,
|
|
393
|
+
setMessages,
|
|
394
|
+
data: streamData,
|
|
395
|
+
setData,
|
|
396
|
+
error,
|
|
397
|
+
append,
|
|
398
|
+
reload,
|
|
399
|
+
stop,
|
|
400
|
+
input,
|
|
401
|
+
setInput,
|
|
402
|
+
handleInputChange,
|
|
403
|
+
handleSubmit,
|
|
404
|
+
isLoading: status === "submitted" || status === "streaming",
|
|
405
|
+
status,
|
|
406
|
+
addToolResult
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// src/use-completion.ts
|
|
411
|
+
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
|
412
|
+
var import_react3 = require("react");
|
|
413
|
+
var import_swr2 = __toESM(require("swr"));
|
|
414
|
+
function useCompletion({
|
|
415
|
+
api = "/api/completion",
|
|
416
|
+
id,
|
|
417
|
+
initialCompletion = "",
|
|
418
|
+
initialInput = "",
|
|
419
|
+
credentials,
|
|
420
|
+
headers,
|
|
421
|
+
body,
|
|
422
|
+
streamProtocol = "data",
|
|
423
|
+
fetch: fetch2,
|
|
424
|
+
onResponse,
|
|
425
|
+
onFinish,
|
|
426
|
+
onError,
|
|
427
|
+
experimental_throttle: throttleWaitMs
|
|
428
|
+
} = {}) {
|
|
429
|
+
const hookId = (0, import_react3.useId)();
|
|
430
|
+
const completionId = id || hookId;
|
|
431
|
+
const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {
|
|
432
|
+
fallbackData: initialCompletion
|
|
433
|
+
});
|
|
434
|
+
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr2.default)(
|
|
435
|
+
[completionId, "loading"],
|
|
436
|
+
null
|
|
437
|
+
);
|
|
438
|
+
const { data: streamData, mutate: mutateStreamData } = (0, import_swr2.default)([completionId, "streamData"], null);
|
|
439
|
+
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
440
|
+
const completion = data;
|
|
441
|
+
const [abortController, setAbortController] = (0, import_react3.useState)(null);
|
|
442
|
+
const extraMetadataRef = (0, import_react3.useRef)({
|
|
443
|
+
credentials,
|
|
444
|
+
headers,
|
|
445
|
+
body
|
|
446
|
+
});
|
|
447
|
+
(0, import_react3.useEffect)(() => {
|
|
448
|
+
extraMetadataRef.current = {
|
|
449
|
+
credentials,
|
|
450
|
+
headers,
|
|
451
|
+
body
|
|
452
|
+
};
|
|
453
|
+
}, [credentials, headers, body]);
|
|
454
|
+
const triggerRequest = (0, import_react3.useCallback)(
|
|
455
|
+
async (prompt, options) => (0, import_ui_utils3.callCompletionApi)({
|
|
456
|
+
api,
|
|
457
|
+
prompt,
|
|
458
|
+
credentials: extraMetadataRef.current.credentials,
|
|
459
|
+
headers: { ...extraMetadataRef.current.headers, ...options == null ? void 0 : options.headers },
|
|
460
|
+
body: {
|
|
461
|
+
...extraMetadataRef.current.body,
|
|
462
|
+
...options == null ? void 0 : options.body
|
|
463
|
+
},
|
|
464
|
+
streamProtocol,
|
|
465
|
+
fetch: fetch2,
|
|
466
|
+
// throttle streamed ui updates:
|
|
467
|
+
setCompletion: throttle(
|
|
468
|
+
(completion2) => mutate(completion2, false),
|
|
469
|
+
throttleWaitMs
|
|
470
|
+
),
|
|
471
|
+
onData: throttle(
|
|
472
|
+
(data2) => mutateStreamData([...streamData != null ? streamData : [], ...data2 != null ? data2 : []], false),
|
|
473
|
+
throttleWaitMs
|
|
474
|
+
),
|
|
475
|
+
setLoading: mutateLoading,
|
|
476
|
+
setError,
|
|
477
|
+
setAbortController,
|
|
478
|
+
onResponse,
|
|
479
|
+
onFinish,
|
|
480
|
+
onError
|
|
481
|
+
}),
|
|
482
|
+
[
|
|
483
|
+
mutate,
|
|
484
|
+
mutateLoading,
|
|
485
|
+
api,
|
|
486
|
+
extraMetadataRef,
|
|
487
|
+
setAbortController,
|
|
488
|
+
onResponse,
|
|
489
|
+
onFinish,
|
|
490
|
+
onError,
|
|
491
|
+
setError,
|
|
492
|
+
streamData,
|
|
493
|
+
streamProtocol,
|
|
494
|
+
fetch2,
|
|
495
|
+
mutateStreamData,
|
|
496
|
+
throttleWaitMs
|
|
497
|
+
]
|
|
498
|
+
);
|
|
499
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
500
|
+
if (abortController) {
|
|
501
|
+
abortController.abort();
|
|
502
|
+
setAbortController(null);
|
|
503
|
+
}
|
|
504
|
+
}, [abortController]);
|
|
505
|
+
const setCompletion = (0, import_react3.useCallback)(
|
|
506
|
+
(completion2) => {
|
|
507
|
+
mutate(completion2, false);
|
|
508
|
+
},
|
|
509
|
+
[mutate]
|
|
510
|
+
);
|
|
511
|
+
const complete = (0, import_react3.useCallback)(
|
|
512
|
+
async (prompt, options) => {
|
|
513
|
+
return triggerRequest(prompt, options);
|
|
514
|
+
},
|
|
515
|
+
[triggerRequest]
|
|
516
|
+
);
|
|
517
|
+
const [input, setInput] = (0, import_react3.useState)(initialInput);
|
|
518
|
+
const handleSubmit = (0, import_react3.useCallback)(
|
|
519
|
+
(event) => {
|
|
520
|
+
var _a;
|
|
521
|
+
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
522
|
+
return input ? complete(input) : void 0;
|
|
523
|
+
},
|
|
524
|
+
[input, complete]
|
|
525
|
+
);
|
|
526
|
+
const handleInputChange = (0, import_react3.useCallback)(
|
|
527
|
+
(e) => {
|
|
528
|
+
setInput(e.target.value);
|
|
529
|
+
},
|
|
530
|
+
[setInput]
|
|
531
|
+
);
|
|
532
|
+
return {
|
|
533
|
+
completion,
|
|
534
|
+
complete,
|
|
535
|
+
error,
|
|
536
|
+
setCompletion,
|
|
537
|
+
stop,
|
|
538
|
+
input,
|
|
539
|
+
setInput,
|
|
540
|
+
handleInputChange,
|
|
541
|
+
handleSubmit,
|
|
542
|
+
isLoading,
|
|
543
|
+
data: streamData
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// src/use-object.ts
|
|
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");
|
|
551
|
+
var import_swr3 = __toESM(require("swr"));
|
|
552
|
+
var getOriginalFetch = () => fetch;
|
|
553
|
+
function useObject({
|
|
554
|
+
api,
|
|
555
|
+
id,
|
|
556
|
+
schema,
|
|
557
|
+
// required, in the future we will use it for validation
|
|
558
|
+
initialValue,
|
|
559
|
+
fetch: fetch2,
|
|
560
|
+
onError,
|
|
561
|
+
onFinish,
|
|
562
|
+
headers,
|
|
563
|
+
credentials
|
|
564
|
+
}) {
|
|
565
|
+
const hookId = (0, import_react4.useId)();
|
|
566
|
+
const completionId = id != null ? id : hookId;
|
|
567
|
+
const { data, mutate } = (0, import_swr3.default)(
|
|
568
|
+
[api, completionId],
|
|
569
|
+
null,
|
|
570
|
+
{ fallbackData: initialValue }
|
|
571
|
+
);
|
|
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)(() => {
|
|
576
|
+
var _a;
|
|
577
|
+
try {
|
|
578
|
+
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
579
|
+
} catch (ignored) {
|
|
580
|
+
} finally {
|
|
581
|
+
setIsLoading(false);
|
|
582
|
+
abortControllerRef.current = null;
|
|
583
|
+
}
|
|
584
|
+
}, []);
|
|
585
|
+
const submit = async (input) => {
|
|
586
|
+
var _a;
|
|
587
|
+
try {
|
|
588
|
+
mutate(void 0);
|
|
589
|
+
setIsLoading(true);
|
|
590
|
+
setError(void 0);
|
|
591
|
+
const abortController = new AbortController();
|
|
592
|
+
abortControllerRef.current = abortController;
|
|
593
|
+
const actualFetch = fetch2 != null ? fetch2 : getOriginalFetch();
|
|
594
|
+
const response = await actualFetch(api, {
|
|
595
|
+
method: "POST",
|
|
596
|
+
headers: {
|
|
597
|
+
"Content-Type": "application/json",
|
|
598
|
+
...headers
|
|
599
|
+
},
|
|
600
|
+
credentials,
|
|
601
|
+
signal: abortController.signal,
|
|
602
|
+
body: JSON.stringify(input)
|
|
603
|
+
});
|
|
604
|
+
if (!response.ok) {
|
|
605
|
+
throw new Error(
|
|
606
|
+
(_a = await response.text()) != null ? _a : "Failed to fetch the response."
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
if (response.body == null) {
|
|
610
|
+
throw new Error("The response body is empty.");
|
|
611
|
+
}
|
|
612
|
+
let accumulatedText = "";
|
|
613
|
+
let latestObject = void 0;
|
|
614
|
+
await response.body.pipeThrough(new TextDecoderStream()).pipeTo(
|
|
615
|
+
new WritableStream({
|
|
616
|
+
write(chunk) {
|
|
617
|
+
accumulatedText += chunk;
|
|
618
|
+
const { value } = (0, import_ui_utils4.parsePartialJson)(accumulatedText);
|
|
619
|
+
const currentObject = value;
|
|
620
|
+
if (!(0, import_ui_utils4.isDeepEqualData)(latestObject, currentObject)) {
|
|
621
|
+
latestObject = currentObject;
|
|
622
|
+
mutate(currentObject);
|
|
623
|
+
}
|
|
624
|
+
},
|
|
625
|
+
close() {
|
|
626
|
+
setIsLoading(false);
|
|
627
|
+
abortControllerRef.current = null;
|
|
628
|
+
if (onFinish != null) {
|
|
629
|
+
const validationResult = (0, import_provider_utils.safeValidateTypes)({
|
|
630
|
+
value: latestObject,
|
|
631
|
+
schema: (0, import_ui_utils4.asSchema)(schema)
|
|
632
|
+
});
|
|
633
|
+
onFinish(
|
|
634
|
+
validationResult.success ? { object: validationResult.value, error: void 0 } : { object: void 0, error: validationResult.error }
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
})
|
|
639
|
+
);
|
|
640
|
+
} catch (error2) {
|
|
641
|
+
if ((0, import_provider_utils.isAbortError)(error2)) {
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
if (onError && error2 instanceof Error) {
|
|
645
|
+
onError(error2);
|
|
646
|
+
}
|
|
647
|
+
setIsLoading(false);
|
|
648
|
+
setError(error2 instanceof Error ? error2 : new Error(String(error2)));
|
|
649
|
+
}
|
|
650
|
+
};
|
|
651
|
+
return {
|
|
652
|
+
submit,
|
|
653
|
+
object: data,
|
|
654
|
+
error,
|
|
655
|
+
isLoading,
|
|
656
|
+
stop
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
var experimental_useObject = useObject;
|
|
660
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
661
|
+
0 && (module.exports = {
|
|
662
|
+
experimental_useObject,
|
|
663
|
+
useChat,
|
|
664
|
+
useCompletion
|
|
665
|
+
});
|
|
666
|
+
//# sourceMappingURL=index.js.map
|