@ai-sdk/react 1.2.12 → 2.0.0-alpha.10
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 +358 -26
- package/README.md +1 -1
- package/dist/index.d.mts +25 -160
- package/dist/index.d.ts +25 -160
- package/dist/index.js +147 -566
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +145 -574
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -11
package/dist/index.js
CHANGED
|
@@ -26,188 +26,78 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
mod
|
|
27
27
|
));
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __accessCheck = (obj, member, msg) => {
|
|
30
|
+
if (!member.has(obj))
|
|
31
|
+
throw TypeError("Cannot " + msg);
|
|
32
|
+
};
|
|
33
|
+
var __privateGet = (obj, member, getter) => {
|
|
34
|
+
__accessCheck(obj, member, "read from private field");
|
|
35
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
36
|
+
};
|
|
37
|
+
var __privateAdd = (obj, member, value) => {
|
|
38
|
+
if (member.has(obj))
|
|
39
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
40
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
41
|
+
};
|
|
42
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
43
|
+
__accessCheck(obj, member, "write to private field");
|
|
44
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
45
|
+
return value;
|
|
46
|
+
};
|
|
29
47
|
|
|
30
48
|
// src/index.ts
|
|
31
49
|
var src_exports = {};
|
|
32
50
|
__export(src_exports, {
|
|
51
|
+
Chat: () => Chat,
|
|
33
52
|
experimental_useObject: () => experimental_useObject,
|
|
34
|
-
useAssistant: () => useAssistant,
|
|
35
53
|
useChat: () => useChat,
|
|
36
54
|
useCompletion: () => useCompletion
|
|
37
55
|
});
|
|
38
56
|
module.exports = __toCommonJS(src_exports);
|
|
39
57
|
|
|
40
|
-
// src/use-
|
|
41
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
42
|
-
var import_ui_utils = require("@ai-sdk/ui-utils");
|
|
58
|
+
// src/use-chat.ts
|
|
43
59
|
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
60
|
|
|
207
|
-
// src/
|
|
208
|
-
var
|
|
209
|
-
var
|
|
210
|
-
var
|
|
61
|
+
// src/chat.react.ts
|
|
62
|
+
var import_ai = require("ai");
|
|
63
|
+
var _messages;
|
|
64
|
+
var ReactChatState = class {
|
|
65
|
+
constructor(messages = []) {
|
|
66
|
+
__privateAdd(this, _messages, void 0);
|
|
67
|
+
this.status = "ready";
|
|
68
|
+
this.error = void 0;
|
|
69
|
+
this.pushMessage = (message) => {
|
|
70
|
+
__privateSet(this, _messages, this.messages.concat(message));
|
|
71
|
+
};
|
|
72
|
+
this.popMessage = () => {
|
|
73
|
+
__privateSet(this, _messages, this.messages.slice(0, -1));
|
|
74
|
+
};
|
|
75
|
+
this.replaceMessage = (index, message) => {
|
|
76
|
+
__privateSet(this, _messages, [
|
|
77
|
+
...__privateGet(this, _messages).slice(0, index),
|
|
78
|
+
message,
|
|
79
|
+
...__privateGet(this, _messages).slice(index + 1)
|
|
80
|
+
]);
|
|
81
|
+
};
|
|
82
|
+
this.snapshot = (value) => structuredClone(value);
|
|
83
|
+
__privateSet(this, _messages, messages);
|
|
84
|
+
}
|
|
85
|
+
get messages() {
|
|
86
|
+
return __privateGet(this, _messages);
|
|
87
|
+
}
|
|
88
|
+
set messages(messages) {
|
|
89
|
+
__privateSet(this, _messages, [...messages]);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
_messages = new WeakMap();
|
|
93
|
+
var Chat = class extends import_ai.AbstractChat {
|
|
94
|
+
constructor({
|
|
95
|
+
messages,
|
|
96
|
+
...init
|
|
97
|
+
}) {
|
|
98
|
+
super({ ...init, state: new ReactChatState(messages) });
|
|
99
|
+
}
|
|
100
|
+
};
|
|
211
101
|
|
|
212
102
|
// src/throttle.ts
|
|
213
103
|
var import_throttleit = __toESM(require("throttleit"));
|
|
@@ -215,376 +105,78 @@ function throttle(fn, waitMs) {
|
|
|
215
105
|
return waitMs != null ? (0, import_throttleit.default)(fn, waitMs) : fn;
|
|
216
106
|
}
|
|
217
107
|
|
|
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
108
|
// src/use-chat.ts
|
|
232
109
|
function useChat({
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
initialMessages,
|
|
236
|
-
initialInput = "",
|
|
237
|
-
sendExtraMessageFields,
|
|
238
|
-
onToolCall,
|
|
239
|
-
experimental_prepareRequestBody,
|
|
240
|
-
maxSteps = 1,
|
|
241
|
-
streamProtocol = "data",
|
|
242
|
-
onResponse,
|
|
243
|
-
onFinish,
|
|
244
|
-
onError,
|
|
245
|
-
credentials,
|
|
246
|
-
headers,
|
|
247
|
-
body,
|
|
248
|
-
generateId: generateId2 = import_ui_utils3.generateId,
|
|
249
|
-
fetch: fetch2,
|
|
250
|
-
keepLastMessageOnError = true,
|
|
251
|
-
experimental_throttle: throttleWaitMs
|
|
110
|
+
experimental_throttle: throttleWaitMs,
|
|
111
|
+
...options
|
|
252
112
|
} = {}) {
|
|
253
|
-
const
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
null,
|
|
264
|
-
{ fallbackData: processedInitialMessages }
|
|
265
|
-
);
|
|
266
|
-
const messagesRef = (0, import_react3.useRef)(messages || []);
|
|
267
|
-
(0, import_react3.useEffect)(() => {
|
|
268
|
-
messagesRef.current = messages || [];
|
|
269
|
-
}, [messages]);
|
|
270
|
-
const { data: streamData, mutate: mutateStreamData } = (0, import_swr.default)([chatKey, "streamData"], null);
|
|
271
|
-
const streamDataRef = (0, import_react3.useRef)(streamData);
|
|
272
|
-
(0, import_react3.useEffect)(() => {
|
|
273
|
-
streamDataRef.current = streamData;
|
|
274
|
-
}, [streamData]);
|
|
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);
|
|
392
|
-
}
|
|
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
|
-
});
|
|
449
|
-
return triggerRequest({ messages: messages2, headers: headers2, body: body2, data });
|
|
450
|
-
},
|
|
451
|
-
[triggerRequest, generateId2]
|
|
452
|
-
);
|
|
453
|
-
const reload = (0, import_react3.useCallback)(
|
|
454
|
-
async ({ data, headers: headers2, body: body2 } = {}) => {
|
|
455
|
-
const messages2 = messagesRef.current;
|
|
456
|
-
if (messages2.length === 0) {
|
|
457
|
-
return null;
|
|
113
|
+
const chatRef = (0, import_react.useRef)("chat" in options ? options.chat : new Chat(options));
|
|
114
|
+
const subscribe = (0, import_react.useCallback)(
|
|
115
|
+
({
|
|
116
|
+
onStoreChange,
|
|
117
|
+
eventType
|
|
118
|
+
}) => chatRef.current.subscribe({
|
|
119
|
+
onChange: (event) => {
|
|
120
|
+
if (event.type !== eventType)
|
|
121
|
+
return;
|
|
122
|
+
onStoreChange();
|
|
458
123
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
messages: lastMessage.role === "assistant" ? messages2.slice(0, -1) : messages2,
|
|
462
|
-
headers: headers2,
|
|
463
|
-
body: body2,
|
|
464
|
-
data
|
|
465
|
-
});
|
|
466
|
-
},
|
|
467
|
-
[triggerRequest]
|
|
124
|
+
}),
|
|
125
|
+
[chatRef]
|
|
468
126
|
);
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
abortControllerRef.current = null;
|
|
473
|
-
}
|
|
474
|
-
}, []);
|
|
475
|
-
const experimental_resume = (0, import_react3.useCallback)(async () => {
|
|
476
|
-
const messages2 = messagesRef.current;
|
|
477
|
-
triggerRequest({ messages: messages2 }, "resume");
|
|
478
|
-
}, [triggerRequest]);
|
|
479
|
-
const setMessages = (0, import_react3.useCallback)(
|
|
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]
|
|
127
|
+
const addToolResult = (0, import_react.useCallback)(
|
|
128
|
+
(options2) => chatRef.current.addToolResult(options2),
|
|
129
|
+
[chatRef]
|
|
489
130
|
);
|
|
490
|
-
const
|
|
491
|
-
(
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
},
|
|
498
|
-
[mutateStreamData]
|
|
131
|
+
const status = (0, import_react.useSyncExternalStore)(
|
|
132
|
+
(callback) => subscribe({
|
|
133
|
+
onStoreChange: callback,
|
|
134
|
+
eventType: "status-changed"
|
|
135
|
+
}),
|
|
136
|
+
() => chatRef.current.status,
|
|
137
|
+
() => chatRef.current.status
|
|
499
138
|
);
|
|
500
|
-
const
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
if (!input && !options.allowEmptySubmit)
|
|
506
|
-
return;
|
|
507
|
-
if (metadata) {
|
|
508
|
-
extraMetadataRef.current = {
|
|
509
|
-
...extraMetadataRef.current,
|
|
510
|
-
...metadata
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
const attachmentsForRequest = await (0, import_ui_utils3.prepareAttachmentsForRequest)(
|
|
514
|
-
options.experimental_attachments
|
|
515
|
-
);
|
|
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 }]
|
|
139
|
+
const subscribeToChatStoreForMessages = (0, import_react.useCallback)(
|
|
140
|
+
(callback) => {
|
|
141
|
+
return subscribe({
|
|
142
|
+
onStoreChange: throttleWaitMs ? throttle(callback, throttleWaitMs) : callback,
|
|
143
|
+
eventType: "messages-changed"
|
|
523
144
|
});
|
|
524
|
-
const chatRequest = {
|
|
525
|
-
messages: messages2,
|
|
526
|
-
headers: options.headers,
|
|
527
|
-
body: options.body,
|
|
528
|
-
data: options.data
|
|
529
|
-
};
|
|
530
|
-
triggerRequest(chatRequest);
|
|
531
|
-
setInput("");
|
|
532
145
|
},
|
|
533
|
-
[
|
|
146
|
+
[subscribe, throttleWaitMs]
|
|
534
147
|
);
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
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 });
|
|
148
|
+
const messages = (0, import_react.useSyncExternalStore)(
|
|
149
|
+
(callback) => subscribeToChatStoreForMessages(callback),
|
|
150
|
+
() => chatRef.current.messages,
|
|
151
|
+
() => chatRef.current.messages
|
|
152
|
+
);
|
|
153
|
+
const setMessages = (0, import_react.useCallback)(
|
|
154
|
+
(messagesParam) => {
|
|
155
|
+
if (typeof messagesParam === "function") {
|
|
156
|
+
messagesParam = messagesParam(messages);
|
|
559
157
|
}
|
|
158
|
+
chatRef.current.messages = messagesParam;
|
|
560
159
|
},
|
|
561
|
-
[
|
|
160
|
+
[chatRef, messages]
|
|
562
161
|
);
|
|
563
162
|
return {
|
|
564
|
-
|
|
565
|
-
|
|
163
|
+
id: chatRef.current.id,
|
|
164
|
+
messages,
|
|
566
165
|
setMessages,
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
stop,
|
|
573
|
-
experimental_resume,
|
|
574
|
-
input,
|
|
575
|
-
setInput,
|
|
576
|
-
handleInputChange,
|
|
577
|
-
handleSubmit,
|
|
578
|
-
isLoading: status === "submitted" || status === "streaming",
|
|
166
|
+
sendMessage: chatRef.current.sendMessage,
|
|
167
|
+
reload: chatRef.current.reload,
|
|
168
|
+
stop: chatRef.current.stop,
|
|
169
|
+
error: chatRef.current.error,
|
|
170
|
+
experimental_resume: chatRef.current.experimental_resume,
|
|
579
171
|
status,
|
|
580
172
|
addToolResult
|
|
581
173
|
};
|
|
582
174
|
}
|
|
583
175
|
|
|
584
176
|
// src/use-completion.ts
|
|
585
|
-
var
|
|
586
|
-
var
|
|
587
|
-
var
|
|
177
|
+
var import_ai2 = require("ai");
|
|
178
|
+
var import_react2 = require("react");
|
|
179
|
+
var import_swr = __toESM(require("swr"));
|
|
588
180
|
function useCompletion({
|
|
589
181
|
api = "/api/completion",
|
|
590
182
|
id,
|
|
@@ -595,38 +187,36 @@ function useCompletion({
|
|
|
595
187
|
body,
|
|
596
188
|
streamProtocol = "data",
|
|
597
189
|
fetch: fetch2,
|
|
598
|
-
onResponse,
|
|
599
190
|
onFinish,
|
|
600
191
|
onError,
|
|
601
192
|
experimental_throttle: throttleWaitMs
|
|
602
193
|
} = {}) {
|
|
603
|
-
const hookId = (0,
|
|
194
|
+
const hookId = (0, import_react2.useId)();
|
|
604
195
|
const completionId = id || hookId;
|
|
605
|
-
const { data, mutate } = (0,
|
|
196
|
+
const { data, mutate } = (0, import_swr.default)([api, completionId], null, {
|
|
606
197
|
fallbackData: initialCompletion
|
|
607
198
|
});
|
|
608
|
-
const { data: isLoading = false, mutate: mutateLoading } = (0,
|
|
199
|
+
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr.default)(
|
|
609
200
|
[completionId, "loading"],
|
|
610
201
|
null
|
|
611
202
|
);
|
|
612
|
-
const
|
|
613
|
-
const [error, setError] = (0, import_react4.useState)(void 0);
|
|
203
|
+
const [error, setError] = (0, import_react2.useState)(void 0);
|
|
614
204
|
const completion = data;
|
|
615
|
-
const [abortController, setAbortController] = (0,
|
|
616
|
-
const extraMetadataRef = (0,
|
|
205
|
+
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
|
206
|
+
const extraMetadataRef = (0, import_react2.useRef)({
|
|
617
207
|
credentials,
|
|
618
208
|
headers,
|
|
619
209
|
body
|
|
620
210
|
});
|
|
621
|
-
(0,
|
|
211
|
+
(0, import_react2.useEffect)(() => {
|
|
622
212
|
extraMetadataRef.current = {
|
|
623
213
|
credentials,
|
|
624
214
|
headers,
|
|
625
215
|
body
|
|
626
216
|
};
|
|
627
217
|
}, [credentials, headers, body]);
|
|
628
|
-
const triggerRequest = (0,
|
|
629
|
-
async (prompt, options) => (0,
|
|
218
|
+
const triggerRequest = (0, import_react2.useCallback)(
|
|
219
|
+
async (prompt, options) => (0, import_ai2.callCompletionApi)({
|
|
630
220
|
api,
|
|
631
221
|
prompt,
|
|
632
222
|
credentials: extraMetadataRef.current.credentials,
|
|
@@ -642,14 +232,9 @@ function useCompletion({
|
|
|
642
232
|
(completion2) => mutate(completion2, false),
|
|
643
233
|
throttleWaitMs
|
|
644
234
|
),
|
|
645
|
-
onData: throttle(
|
|
646
|
-
(data2) => mutateStreamData([...streamData != null ? streamData : [], ...data2 != null ? data2 : []], false),
|
|
647
|
-
throttleWaitMs
|
|
648
|
-
),
|
|
649
235
|
setLoading: mutateLoading,
|
|
650
236
|
setError,
|
|
651
237
|
setAbortController,
|
|
652
|
-
onResponse,
|
|
653
238
|
onFinish,
|
|
654
239
|
onError
|
|
655
240
|
}),
|
|
@@ -659,37 +244,34 @@ function useCompletion({
|
|
|
659
244
|
api,
|
|
660
245
|
extraMetadataRef,
|
|
661
246
|
setAbortController,
|
|
662
|
-
onResponse,
|
|
663
247
|
onFinish,
|
|
664
248
|
onError,
|
|
665
249
|
setError,
|
|
666
|
-
streamData,
|
|
667
250
|
streamProtocol,
|
|
668
251
|
fetch2,
|
|
669
|
-
mutateStreamData,
|
|
670
252
|
throttleWaitMs
|
|
671
253
|
]
|
|
672
254
|
);
|
|
673
|
-
const stop = (0,
|
|
255
|
+
const stop = (0, import_react2.useCallback)(() => {
|
|
674
256
|
if (abortController) {
|
|
675
257
|
abortController.abort();
|
|
676
258
|
setAbortController(null);
|
|
677
259
|
}
|
|
678
260
|
}, [abortController]);
|
|
679
|
-
const setCompletion = (0,
|
|
261
|
+
const setCompletion = (0, import_react2.useCallback)(
|
|
680
262
|
(completion2) => {
|
|
681
263
|
mutate(completion2, false);
|
|
682
264
|
},
|
|
683
265
|
[mutate]
|
|
684
266
|
);
|
|
685
|
-
const complete = (0,
|
|
267
|
+
const complete = (0, import_react2.useCallback)(
|
|
686
268
|
async (prompt, options) => {
|
|
687
269
|
return triggerRequest(prompt, options);
|
|
688
270
|
},
|
|
689
271
|
[triggerRequest]
|
|
690
272
|
);
|
|
691
|
-
const [input, setInput] = (0,
|
|
692
|
-
const handleSubmit = (0,
|
|
273
|
+
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
|
274
|
+
const handleSubmit = (0, import_react2.useCallback)(
|
|
693
275
|
(event) => {
|
|
694
276
|
var _a;
|
|
695
277
|
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
@@ -697,7 +279,7 @@ function useCompletion({
|
|
|
697
279
|
},
|
|
698
280
|
[input, complete]
|
|
699
281
|
);
|
|
700
|
-
const handleInputChange = (0,
|
|
282
|
+
const handleInputChange = (0, import_react2.useCallback)(
|
|
701
283
|
(e) => {
|
|
702
284
|
setInput(e.target.value);
|
|
703
285
|
},
|
|
@@ -713,17 +295,16 @@ function useCompletion({
|
|
|
713
295
|
setInput,
|
|
714
296
|
handleInputChange,
|
|
715
297
|
handleSubmit,
|
|
716
|
-
isLoading
|
|
717
|
-
data: streamData
|
|
298
|
+
isLoading
|
|
718
299
|
};
|
|
719
300
|
}
|
|
720
301
|
|
|
721
302
|
// src/use-object.ts
|
|
722
|
-
var
|
|
723
|
-
var
|
|
724
|
-
var
|
|
725
|
-
var
|
|
726
|
-
var
|
|
303
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
304
|
+
var import_ai3 = require("ai");
|
|
305
|
+
var import_react3 = require("react");
|
|
306
|
+
var import_swr2 = __toESM(require("swr"));
|
|
307
|
+
var getOriginalFetch = () => fetch;
|
|
727
308
|
function useObject({
|
|
728
309
|
api,
|
|
729
310
|
id,
|
|
@@ -736,17 +317,17 @@ function useObject({
|
|
|
736
317
|
headers,
|
|
737
318
|
credentials
|
|
738
319
|
}) {
|
|
739
|
-
const hookId = (0,
|
|
320
|
+
const hookId = (0, import_react3.useId)();
|
|
740
321
|
const completionId = id != null ? id : hookId;
|
|
741
|
-
const { data, mutate } = (0,
|
|
322
|
+
const { data, mutate } = (0, import_swr2.default)(
|
|
742
323
|
[api, completionId],
|
|
743
324
|
null,
|
|
744
325
|
{ fallbackData: initialValue }
|
|
745
326
|
);
|
|
746
|
-
const [error, setError] = (0,
|
|
747
|
-
const [isLoading, setIsLoading] = (0,
|
|
748
|
-
const abortControllerRef = (0,
|
|
749
|
-
const stop = (0,
|
|
327
|
+
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
328
|
+
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
329
|
+
const abortControllerRef = (0, import_react3.useRef)(null);
|
|
330
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
750
331
|
var _a;
|
|
751
332
|
try {
|
|
752
333
|
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
@@ -764,7 +345,7 @@ function useObject({
|
|
|
764
345
|
setError(void 0);
|
|
765
346
|
const abortController = new AbortController();
|
|
766
347
|
abortControllerRef.current = abortController;
|
|
767
|
-
const actualFetch = fetch2 != null ? fetch2 :
|
|
348
|
+
const actualFetch = fetch2 != null ? fetch2 : getOriginalFetch();
|
|
768
349
|
const response = await actualFetch(api, {
|
|
769
350
|
method: "POST",
|
|
770
351
|
headers: {
|
|
@@ -787,22 +368,22 @@ function useObject({
|
|
|
787
368
|
let latestObject = void 0;
|
|
788
369
|
await response.body.pipeThrough(new TextDecoderStream()).pipeTo(
|
|
789
370
|
new WritableStream({
|
|
790
|
-
write(chunk) {
|
|
371
|
+
async write(chunk) {
|
|
791
372
|
accumulatedText += chunk;
|
|
792
|
-
const { value } = (0,
|
|
373
|
+
const { value } = await (0, import_ai3.parsePartialJson)(accumulatedText);
|
|
793
374
|
const currentObject = value;
|
|
794
|
-
if (!(0,
|
|
375
|
+
if (!(0, import_ai3.isDeepEqualData)(latestObject, currentObject)) {
|
|
795
376
|
latestObject = currentObject;
|
|
796
377
|
mutate(currentObject);
|
|
797
378
|
}
|
|
798
379
|
},
|
|
799
|
-
close() {
|
|
380
|
+
async close() {
|
|
800
381
|
setIsLoading(false);
|
|
801
382
|
abortControllerRef.current = null;
|
|
802
383
|
if (onFinish != null) {
|
|
803
|
-
const validationResult = (0,
|
|
384
|
+
const validationResult = await (0, import_provider_utils.safeValidateTypes)({
|
|
804
385
|
value: latestObject,
|
|
805
|
-
schema: (0,
|
|
386
|
+
schema: (0, import_ai3.asSchema)(schema)
|
|
806
387
|
});
|
|
807
388
|
onFinish(
|
|
808
389
|
validationResult.success ? { object: validationResult.value, error: void 0 } : { object: void 0, error: validationResult.error }
|
|
@@ -812,7 +393,7 @@ function useObject({
|
|
|
812
393
|
})
|
|
813
394
|
);
|
|
814
395
|
} catch (error2) {
|
|
815
|
-
if ((0,
|
|
396
|
+
if ((0, import_provider_utils.isAbortError)(error2)) {
|
|
816
397
|
return;
|
|
817
398
|
}
|
|
818
399
|
if (onError && error2 instanceof Error) {
|
|
@@ -833,8 +414,8 @@ function useObject({
|
|
|
833
414
|
var experimental_useObject = useObject;
|
|
834
415
|
// Annotate the CommonJS export names for ESM import in node:
|
|
835
416
|
0 && (module.exports = {
|
|
417
|
+
Chat,
|
|
836
418
|
experimental_useObject,
|
|
837
|
-
useAssistant,
|
|
838
419
|
useChat,
|
|
839
420
|
useCompletion
|
|
840
421
|
});
|