@ai-sdk/react 0.0.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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +21 -0
- package/.turbo/turbo-clean.log +4 -0
- package/CHANGELOG.md +9 -0
- package/LICENSE +13 -0
- package/README.md +7 -0
- package/dist/index.d.mts +189 -0
- package/dist/index.d.ts +189 -0
- package/dist/index.js +684 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +653 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +74 -0
- package/src/index.ts +3 -0
- package/src/use-assistant.ts +279 -0
- package/src/use-assistant.ui.test.tsx +132 -0
- package/src/use-chat.ts +572 -0
- package/src/use-chat.ui.test.tsx +209 -0
- package/src/use-completion.ts +203 -0
- package/src/use-completion.ui.test.tsx +142 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +13 -0
- package/turbo.json +8 -0
- package/vitest.config.js +12 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,684 @@
|
|
|
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_useAssistant: () => experimental_useAssistant,
|
|
34
|
+
useAssistant: () => useAssistant,
|
|
35
|
+
useChat: () => useChat,
|
|
36
|
+
useCompletion: () => useCompletion
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(src_exports);
|
|
39
|
+
|
|
40
|
+
// src/use-chat.ts
|
|
41
|
+
var import_ui_utils = require("@ai-sdk/ui-utils");
|
|
42
|
+
var import_react = require("react");
|
|
43
|
+
var import_swr = __toESM(require("swr"));
|
|
44
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamMode, onFinish, onResponse, onToolCall, sendExtraMessageFields) => {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
const previousMessages = messagesRef.current;
|
|
47
|
+
mutate(chatRequest.messages, false);
|
|
48
|
+
const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
|
|
49
|
+
({
|
|
50
|
+
role,
|
|
51
|
+
content,
|
|
52
|
+
name,
|
|
53
|
+
data,
|
|
54
|
+
annotations,
|
|
55
|
+
toolInvocations,
|
|
56
|
+
function_call,
|
|
57
|
+
tool_calls,
|
|
58
|
+
tool_call_id
|
|
59
|
+
}) => ({
|
|
60
|
+
role,
|
|
61
|
+
content,
|
|
62
|
+
...name !== void 0 && { name },
|
|
63
|
+
...data !== void 0 && { data },
|
|
64
|
+
...annotations !== void 0 && { annotations },
|
|
65
|
+
...toolInvocations !== void 0 && { toolInvocations },
|
|
66
|
+
// outdated function/tool call handling (TODO deprecate):
|
|
67
|
+
tool_call_id,
|
|
68
|
+
...function_call !== void 0 && { function_call },
|
|
69
|
+
...tool_calls !== void 0 && { tool_calls }
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
return await (0, import_ui_utils.callChatApi)({
|
|
73
|
+
api,
|
|
74
|
+
messages: constructedMessagesPayload,
|
|
75
|
+
body: {
|
|
76
|
+
data: chatRequest.data,
|
|
77
|
+
...extraMetadataRef.current.body,
|
|
78
|
+
...(_a = chatRequest.options) == null ? void 0 : _a.body,
|
|
79
|
+
...chatRequest.functions !== void 0 && {
|
|
80
|
+
functions: chatRequest.functions
|
|
81
|
+
},
|
|
82
|
+
...chatRequest.function_call !== void 0 && {
|
|
83
|
+
function_call: chatRequest.function_call
|
|
84
|
+
},
|
|
85
|
+
...chatRequest.tools !== void 0 && {
|
|
86
|
+
tools: chatRequest.tools
|
|
87
|
+
},
|
|
88
|
+
...chatRequest.tool_choice !== void 0 && {
|
|
89
|
+
tool_choice: chatRequest.tool_choice
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
streamMode,
|
|
93
|
+
credentials: extraMetadataRef.current.credentials,
|
|
94
|
+
headers: {
|
|
95
|
+
...extraMetadataRef.current.headers,
|
|
96
|
+
...(_b = chatRequest.options) == null ? void 0 : _b.headers
|
|
97
|
+
},
|
|
98
|
+
abortController: () => abortControllerRef.current,
|
|
99
|
+
restoreMessagesOnFailure() {
|
|
100
|
+
mutate(previousMessages, false);
|
|
101
|
+
},
|
|
102
|
+
onResponse,
|
|
103
|
+
onUpdate(merged, data) {
|
|
104
|
+
mutate([...chatRequest.messages, ...merged], false);
|
|
105
|
+
mutateStreamData([...existingData || [], ...data || []], false);
|
|
106
|
+
},
|
|
107
|
+
onToolCall,
|
|
108
|
+
onFinish,
|
|
109
|
+
generateId: generateId2
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
function useChat({
|
|
113
|
+
api = "/api/chat",
|
|
114
|
+
id,
|
|
115
|
+
initialMessages,
|
|
116
|
+
initialInput = "",
|
|
117
|
+
sendExtraMessageFields,
|
|
118
|
+
experimental_onFunctionCall,
|
|
119
|
+
experimental_onToolCall,
|
|
120
|
+
onToolCall,
|
|
121
|
+
experimental_maxAutomaticRoundtrips = 0,
|
|
122
|
+
maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,
|
|
123
|
+
maxToolRoundtrips = maxAutomaticRoundtrips,
|
|
124
|
+
streamMode,
|
|
125
|
+
onResponse,
|
|
126
|
+
onFinish,
|
|
127
|
+
onError,
|
|
128
|
+
credentials,
|
|
129
|
+
headers,
|
|
130
|
+
body,
|
|
131
|
+
generateId: generateId2 = import_ui_utils.generateId
|
|
132
|
+
} = {}) {
|
|
133
|
+
const hookId = (0, import_react.useId)();
|
|
134
|
+
const idKey = id != null ? id : hookId;
|
|
135
|
+
const chatKey = typeof api === "string" ? [api, idKey] : idKey;
|
|
136
|
+
const [initialMessagesFallback] = (0, import_react.useState)([]);
|
|
137
|
+
const { data: messages, mutate } = (0, import_swr.default)(
|
|
138
|
+
[chatKey, "messages"],
|
|
139
|
+
null,
|
|
140
|
+
{ fallbackData: initialMessages != null ? initialMessages : initialMessagesFallback }
|
|
141
|
+
);
|
|
142
|
+
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr.default)(
|
|
143
|
+
[chatKey, "loading"],
|
|
144
|
+
null
|
|
145
|
+
);
|
|
146
|
+
const { data: streamData, mutate: mutateStreamData } = (0, import_swr.default)([chatKey, "streamData"], null);
|
|
147
|
+
const { data: error = void 0, mutate: setError } = (0, import_swr.default)([chatKey, "error"], null);
|
|
148
|
+
const messagesRef = (0, import_react.useRef)(messages || []);
|
|
149
|
+
(0, import_react.useEffect)(() => {
|
|
150
|
+
messagesRef.current = messages || [];
|
|
151
|
+
}, [messages]);
|
|
152
|
+
const abortControllerRef = (0, import_react.useRef)(null);
|
|
153
|
+
const extraMetadataRef = (0, import_react.useRef)({
|
|
154
|
+
credentials,
|
|
155
|
+
headers,
|
|
156
|
+
body
|
|
157
|
+
});
|
|
158
|
+
(0, import_react.useEffect)(() => {
|
|
159
|
+
extraMetadataRef.current = {
|
|
160
|
+
credentials,
|
|
161
|
+
headers,
|
|
162
|
+
body
|
|
163
|
+
};
|
|
164
|
+
}, [credentials, headers, body]);
|
|
165
|
+
const triggerRequest = (0, import_react.useCallback)(
|
|
166
|
+
async (chatRequest) => {
|
|
167
|
+
try {
|
|
168
|
+
mutateLoading(true);
|
|
169
|
+
setError(void 0);
|
|
170
|
+
const abortController = new AbortController();
|
|
171
|
+
abortControllerRef.current = abortController;
|
|
172
|
+
await (0, import_ui_utils.processChatStream)({
|
|
173
|
+
getStreamedResponse: () => getStreamedResponse(
|
|
174
|
+
api,
|
|
175
|
+
chatRequest,
|
|
176
|
+
mutate,
|
|
177
|
+
mutateStreamData,
|
|
178
|
+
streamData,
|
|
179
|
+
extraMetadataRef,
|
|
180
|
+
messagesRef,
|
|
181
|
+
abortControllerRef,
|
|
182
|
+
generateId2,
|
|
183
|
+
streamMode,
|
|
184
|
+
onFinish,
|
|
185
|
+
onResponse,
|
|
186
|
+
onToolCall,
|
|
187
|
+
sendExtraMessageFields
|
|
188
|
+
),
|
|
189
|
+
experimental_onFunctionCall,
|
|
190
|
+
experimental_onToolCall,
|
|
191
|
+
updateChatRequest: (chatRequestParam) => {
|
|
192
|
+
chatRequest = chatRequestParam;
|
|
193
|
+
},
|
|
194
|
+
getCurrentMessages: () => messagesRef.current
|
|
195
|
+
});
|
|
196
|
+
abortControllerRef.current = null;
|
|
197
|
+
} catch (err) {
|
|
198
|
+
if (err.name === "AbortError") {
|
|
199
|
+
abortControllerRef.current = null;
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
if (onError && err instanceof Error) {
|
|
203
|
+
onError(err);
|
|
204
|
+
}
|
|
205
|
+
setError(err);
|
|
206
|
+
} finally {
|
|
207
|
+
mutateLoading(false);
|
|
208
|
+
}
|
|
209
|
+
const messages2 = messagesRef.current;
|
|
210
|
+
const lastMessage = messages2[messages2.length - 1];
|
|
211
|
+
if (
|
|
212
|
+
// ensure there is a last message:
|
|
213
|
+
lastMessage != null && // check if the feature is enabled:
|
|
214
|
+
maxToolRoundtrips > 0 && // check that roundtrip is possible:
|
|
215
|
+
isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic roundtrips:
|
|
216
|
+
countTrailingAssistantMessages(messages2) <= maxToolRoundtrips
|
|
217
|
+
) {
|
|
218
|
+
await triggerRequest({ messages: messages2 });
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
[
|
|
222
|
+
mutate,
|
|
223
|
+
mutateLoading,
|
|
224
|
+
api,
|
|
225
|
+
extraMetadataRef,
|
|
226
|
+
onResponse,
|
|
227
|
+
onFinish,
|
|
228
|
+
onError,
|
|
229
|
+
setError,
|
|
230
|
+
mutateStreamData,
|
|
231
|
+
streamData,
|
|
232
|
+
streamMode,
|
|
233
|
+
sendExtraMessageFields,
|
|
234
|
+
experimental_onFunctionCall,
|
|
235
|
+
experimental_onToolCall,
|
|
236
|
+
onToolCall,
|
|
237
|
+
maxToolRoundtrips,
|
|
238
|
+
messagesRef,
|
|
239
|
+
abortControllerRef,
|
|
240
|
+
generateId2
|
|
241
|
+
]
|
|
242
|
+
);
|
|
243
|
+
const append = (0, import_react.useCallback)(
|
|
244
|
+
async (message, {
|
|
245
|
+
options,
|
|
246
|
+
functions,
|
|
247
|
+
function_call,
|
|
248
|
+
tools,
|
|
249
|
+
tool_choice,
|
|
250
|
+
data
|
|
251
|
+
} = {}) => {
|
|
252
|
+
if (!message.id) {
|
|
253
|
+
message.id = generateId2();
|
|
254
|
+
}
|
|
255
|
+
const chatRequest = {
|
|
256
|
+
messages: messagesRef.current.concat(message),
|
|
257
|
+
options,
|
|
258
|
+
data,
|
|
259
|
+
...functions !== void 0 && { functions },
|
|
260
|
+
...function_call !== void 0 && { function_call },
|
|
261
|
+
...tools !== void 0 && { tools },
|
|
262
|
+
...tool_choice !== void 0 && { tool_choice }
|
|
263
|
+
};
|
|
264
|
+
return triggerRequest(chatRequest);
|
|
265
|
+
},
|
|
266
|
+
[triggerRequest, generateId2]
|
|
267
|
+
);
|
|
268
|
+
const reload = (0, import_react.useCallback)(
|
|
269
|
+
async ({
|
|
270
|
+
options,
|
|
271
|
+
functions,
|
|
272
|
+
function_call,
|
|
273
|
+
tools,
|
|
274
|
+
tool_choice
|
|
275
|
+
} = {}) => {
|
|
276
|
+
if (messagesRef.current.length === 0)
|
|
277
|
+
return null;
|
|
278
|
+
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
|
279
|
+
if (lastMessage.role === "assistant") {
|
|
280
|
+
const chatRequest2 = {
|
|
281
|
+
messages: messagesRef.current.slice(0, -1),
|
|
282
|
+
options,
|
|
283
|
+
...functions !== void 0 && { functions },
|
|
284
|
+
...function_call !== void 0 && { function_call },
|
|
285
|
+
...tools !== void 0 && { tools },
|
|
286
|
+
...tool_choice !== void 0 && { tool_choice }
|
|
287
|
+
};
|
|
288
|
+
return triggerRequest(chatRequest2);
|
|
289
|
+
}
|
|
290
|
+
const chatRequest = {
|
|
291
|
+
messages: messagesRef.current,
|
|
292
|
+
options,
|
|
293
|
+
...functions !== void 0 && { functions },
|
|
294
|
+
...function_call !== void 0 && { function_call },
|
|
295
|
+
...tools !== void 0 && { tools },
|
|
296
|
+
...tool_choice !== void 0 && { tool_choice }
|
|
297
|
+
};
|
|
298
|
+
return triggerRequest(chatRequest);
|
|
299
|
+
},
|
|
300
|
+
[triggerRequest]
|
|
301
|
+
);
|
|
302
|
+
const stop = (0, import_react.useCallback)(() => {
|
|
303
|
+
if (abortControllerRef.current) {
|
|
304
|
+
abortControllerRef.current.abort();
|
|
305
|
+
abortControllerRef.current = null;
|
|
306
|
+
}
|
|
307
|
+
}, []);
|
|
308
|
+
const setMessages = (0, import_react.useCallback)(
|
|
309
|
+
(messages2) => {
|
|
310
|
+
mutate(messages2, false);
|
|
311
|
+
messagesRef.current = messages2;
|
|
312
|
+
},
|
|
313
|
+
[mutate]
|
|
314
|
+
);
|
|
315
|
+
const [input, setInput] = (0, import_react.useState)(initialInput);
|
|
316
|
+
const handleSubmit = (0, import_react.useCallback)(
|
|
317
|
+
(e, options = {}, metadata) => {
|
|
318
|
+
if (metadata) {
|
|
319
|
+
extraMetadataRef.current = {
|
|
320
|
+
...extraMetadataRef.current,
|
|
321
|
+
...metadata
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
e.preventDefault();
|
|
325
|
+
if (!input)
|
|
326
|
+
return;
|
|
327
|
+
append(
|
|
328
|
+
{
|
|
329
|
+
content: input,
|
|
330
|
+
role: "user",
|
|
331
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
332
|
+
},
|
|
333
|
+
options
|
|
334
|
+
);
|
|
335
|
+
setInput("");
|
|
336
|
+
},
|
|
337
|
+
[input, append]
|
|
338
|
+
);
|
|
339
|
+
const handleInputChange = (e) => {
|
|
340
|
+
setInput(e.target.value);
|
|
341
|
+
};
|
|
342
|
+
const addToolResult = ({
|
|
343
|
+
toolCallId,
|
|
344
|
+
result
|
|
345
|
+
}) => {
|
|
346
|
+
const updatedMessages = messagesRef.current.map(
|
|
347
|
+
(message, index, arr) => (
|
|
348
|
+
// update the tool calls in the last assistant message:
|
|
349
|
+
index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? {
|
|
350
|
+
...message,
|
|
351
|
+
toolInvocations: message.toolInvocations.map(
|
|
352
|
+
(toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation
|
|
353
|
+
)
|
|
354
|
+
} : message
|
|
355
|
+
)
|
|
356
|
+
);
|
|
357
|
+
mutate(updatedMessages, false);
|
|
358
|
+
const lastMessage = updatedMessages[updatedMessages.length - 1];
|
|
359
|
+
if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
|
|
360
|
+
triggerRequest({ messages: updatedMessages });
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
return {
|
|
364
|
+
messages: messages || [],
|
|
365
|
+
error,
|
|
366
|
+
append,
|
|
367
|
+
reload,
|
|
368
|
+
stop,
|
|
369
|
+
setMessages,
|
|
370
|
+
input,
|
|
371
|
+
setInput,
|
|
372
|
+
handleInputChange,
|
|
373
|
+
handleSubmit,
|
|
374
|
+
isLoading,
|
|
375
|
+
data: streamData,
|
|
376
|
+
addToolResult,
|
|
377
|
+
experimental_addToolResult: addToolResult
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function isAssistantMessageWithCompletedToolCalls(message) {
|
|
381
|
+
return message.role === "assistant" && message.toolInvocations && message.toolInvocations.length > 0 && message.toolInvocations.every((toolInvocation) => "result" in toolInvocation);
|
|
382
|
+
}
|
|
383
|
+
function countTrailingAssistantMessages(messages) {
|
|
384
|
+
let count = 0;
|
|
385
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
386
|
+
if (messages[i].role === "assistant") {
|
|
387
|
+
count++;
|
|
388
|
+
} else {
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return count;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// src/use-completion.ts
|
|
396
|
+
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
|
397
|
+
var import_react2 = require("react");
|
|
398
|
+
var import_swr2 = __toESM(require("swr"));
|
|
399
|
+
function useCompletion({
|
|
400
|
+
api = "/api/completion",
|
|
401
|
+
id,
|
|
402
|
+
initialCompletion = "",
|
|
403
|
+
initialInput = "",
|
|
404
|
+
credentials,
|
|
405
|
+
headers,
|
|
406
|
+
body,
|
|
407
|
+
streamMode,
|
|
408
|
+
onResponse,
|
|
409
|
+
onFinish,
|
|
410
|
+
onError
|
|
411
|
+
} = {}) {
|
|
412
|
+
const hookId = (0, import_react2.useId)();
|
|
413
|
+
const completionId = id || hookId;
|
|
414
|
+
const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {
|
|
415
|
+
fallbackData: initialCompletion
|
|
416
|
+
});
|
|
417
|
+
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr2.default)(
|
|
418
|
+
[completionId, "loading"],
|
|
419
|
+
null
|
|
420
|
+
);
|
|
421
|
+
const { data: streamData, mutate: mutateStreamData } = (0, import_swr2.default)([completionId, "streamData"], null);
|
|
422
|
+
const [error, setError] = (0, import_react2.useState)(void 0);
|
|
423
|
+
const completion = data;
|
|
424
|
+
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
|
425
|
+
const extraMetadataRef = (0, import_react2.useRef)({
|
|
426
|
+
credentials,
|
|
427
|
+
headers,
|
|
428
|
+
body
|
|
429
|
+
});
|
|
430
|
+
(0, import_react2.useEffect)(() => {
|
|
431
|
+
extraMetadataRef.current = {
|
|
432
|
+
credentials,
|
|
433
|
+
headers,
|
|
434
|
+
body
|
|
435
|
+
};
|
|
436
|
+
}, [credentials, headers, body]);
|
|
437
|
+
const triggerRequest = (0, import_react2.useCallback)(
|
|
438
|
+
async (prompt, options) => (0, import_ui_utils2.callCompletionApi)({
|
|
439
|
+
api,
|
|
440
|
+
prompt,
|
|
441
|
+
credentials: extraMetadataRef.current.credentials,
|
|
442
|
+
headers: { ...extraMetadataRef.current.headers, ...options == null ? void 0 : options.headers },
|
|
443
|
+
body: {
|
|
444
|
+
...extraMetadataRef.current.body,
|
|
445
|
+
...options == null ? void 0 : options.body
|
|
446
|
+
},
|
|
447
|
+
streamMode,
|
|
448
|
+
setCompletion: (completion2) => mutate(completion2, false),
|
|
449
|
+
setLoading: mutateLoading,
|
|
450
|
+
setError,
|
|
451
|
+
setAbortController,
|
|
452
|
+
onResponse,
|
|
453
|
+
onFinish,
|
|
454
|
+
onError,
|
|
455
|
+
onData: (data2) => {
|
|
456
|
+
mutateStreamData([...streamData || [], ...data2 || []], false);
|
|
457
|
+
}
|
|
458
|
+
}),
|
|
459
|
+
[
|
|
460
|
+
mutate,
|
|
461
|
+
mutateLoading,
|
|
462
|
+
api,
|
|
463
|
+
extraMetadataRef,
|
|
464
|
+
setAbortController,
|
|
465
|
+
onResponse,
|
|
466
|
+
onFinish,
|
|
467
|
+
onError,
|
|
468
|
+
setError,
|
|
469
|
+
streamData,
|
|
470
|
+
streamMode,
|
|
471
|
+
mutateStreamData
|
|
472
|
+
]
|
|
473
|
+
);
|
|
474
|
+
const stop = (0, import_react2.useCallback)(() => {
|
|
475
|
+
if (abortController) {
|
|
476
|
+
abortController.abort();
|
|
477
|
+
setAbortController(null);
|
|
478
|
+
}
|
|
479
|
+
}, [abortController]);
|
|
480
|
+
const setCompletion = (0, import_react2.useCallback)(
|
|
481
|
+
(completion2) => {
|
|
482
|
+
mutate(completion2, false);
|
|
483
|
+
},
|
|
484
|
+
[mutate]
|
|
485
|
+
);
|
|
486
|
+
const complete = (0, import_react2.useCallback)(
|
|
487
|
+
async (prompt, options) => {
|
|
488
|
+
return triggerRequest(prompt, options);
|
|
489
|
+
},
|
|
490
|
+
[triggerRequest]
|
|
491
|
+
);
|
|
492
|
+
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
|
493
|
+
const handleSubmit = (0, import_react2.useCallback)(
|
|
494
|
+
(e) => {
|
|
495
|
+
e.preventDefault();
|
|
496
|
+
if (!input)
|
|
497
|
+
return;
|
|
498
|
+
return complete(input);
|
|
499
|
+
},
|
|
500
|
+
[input, complete]
|
|
501
|
+
);
|
|
502
|
+
const handleInputChange = (e) => {
|
|
503
|
+
setInput(e.target.value);
|
|
504
|
+
};
|
|
505
|
+
return {
|
|
506
|
+
completion,
|
|
507
|
+
complete,
|
|
508
|
+
error,
|
|
509
|
+
setCompletion,
|
|
510
|
+
stop,
|
|
511
|
+
input,
|
|
512
|
+
setInput,
|
|
513
|
+
handleInputChange,
|
|
514
|
+
handleSubmit,
|
|
515
|
+
isLoading,
|
|
516
|
+
data: streamData
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// src/use-assistant.ts
|
|
521
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
522
|
+
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
|
523
|
+
var import_react3 = require("react");
|
|
524
|
+
function useAssistant({
|
|
525
|
+
api,
|
|
526
|
+
threadId: threadIdParam,
|
|
527
|
+
credentials,
|
|
528
|
+
headers,
|
|
529
|
+
body,
|
|
530
|
+
onError
|
|
531
|
+
}) {
|
|
532
|
+
const [messages, setMessages] = (0, import_react3.useState)([]);
|
|
533
|
+
const [input, setInput] = (0, import_react3.useState)("");
|
|
534
|
+
const [threadId, setThreadId] = (0, import_react3.useState)(void 0);
|
|
535
|
+
const [status, setStatus] = (0, import_react3.useState)("awaiting_message");
|
|
536
|
+
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
537
|
+
const handleInputChange = (event) => {
|
|
538
|
+
setInput(event.target.value);
|
|
539
|
+
};
|
|
540
|
+
const abortControllerRef = (0, import_react3.useRef)(null);
|
|
541
|
+
const stop = (0, import_react3.useCallback)(() => {
|
|
542
|
+
if (abortControllerRef.current) {
|
|
543
|
+
abortControllerRef.current.abort();
|
|
544
|
+
abortControllerRef.current = null;
|
|
545
|
+
}
|
|
546
|
+
}, []);
|
|
547
|
+
const append = async (message, requestOptions) => {
|
|
548
|
+
var _a;
|
|
549
|
+
setStatus("in_progress");
|
|
550
|
+
setMessages((messages2) => {
|
|
551
|
+
var _a2;
|
|
552
|
+
return [
|
|
553
|
+
...messages2,
|
|
554
|
+
{
|
|
555
|
+
...message,
|
|
556
|
+
id: (_a2 = message.id) != null ? _a2 : (0, import_ui_utils3.generateId)()
|
|
557
|
+
}
|
|
558
|
+
];
|
|
559
|
+
});
|
|
560
|
+
setInput("");
|
|
561
|
+
const abortController = new AbortController();
|
|
562
|
+
try {
|
|
563
|
+
abortControllerRef.current = abortController;
|
|
564
|
+
const result = await fetch(api, {
|
|
565
|
+
method: "POST",
|
|
566
|
+
credentials,
|
|
567
|
+
signal: abortController.signal,
|
|
568
|
+
headers: { "Content-Type": "application/json", ...headers },
|
|
569
|
+
body: JSON.stringify({
|
|
570
|
+
...body,
|
|
571
|
+
// always use user-provided threadId when available:
|
|
572
|
+
threadId: (_a = threadIdParam != null ? threadIdParam : threadId) != null ? _a : null,
|
|
573
|
+
message: message.content,
|
|
574
|
+
// optional request data:
|
|
575
|
+
data: requestOptions == null ? void 0 : requestOptions.data
|
|
576
|
+
})
|
|
577
|
+
});
|
|
578
|
+
if (result.body == null) {
|
|
579
|
+
throw new Error("The response body is empty.");
|
|
580
|
+
}
|
|
581
|
+
for await (const { type, value } of (0, import_ui_utils3.readDataStream)(
|
|
582
|
+
result.body.getReader()
|
|
583
|
+
)) {
|
|
584
|
+
switch (type) {
|
|
585
|
+
case "assistant_message": {
|
|
586
|
+
setMessages((messages2) => [
|
|
587
|
+
...messages2,
|
|
588
|
+
{
|
|
589
|
+
id: value.id,
|
|
590
|
+
role: value.role,
|
|
591
|
+
content: value.content[0].text.value
|
|
592
|
+
}
|
|
593
|
+
]);
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
case "text": {
|
|
597
|
+
setMessages((messages2) => {
|
|
598
|
+
const lastMessage = messages2[messages2.length - 1];
|
|
599
|
+
return [
|
|
600
|
+
...messages2.slice(0, messages2.length - 1),
|
|
601
|
+
{
|
|
602
|
+
id: lastMessage.id,
|
|
603
|
+
role: lastMessage.role,
|
|
604
|
+
content: lastMessage.content + value
|
|
605
|
+
}
|
|
606
|
+
];
|
|
607
|
+
});
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
case "data_message": {
|
|
611
|
+
setMessages((messages2) => {
|
|
612
|
+
var _a2;
|
|
613
|
+
return [
|
|
614
|
+
...messages2,
|
|
615
|
+
{
|
|
616
|
+
id: (_a2 = value.id) != null ? _a2 : (0, import_ui_utils3.generateId)(),
|
|
617
|
+
role: "data",
|
|
618
|
+
content: "",
|
|
619
|
+
data: value.data
|
|
620
|
+
}
|
|
621
|
+
];
|
|
622
|
+
});
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
case "assistant_control_data": {
|
|
626
|
+
setThreadId(value.threadId);
|
|
627
|
+
setMessages((messages2) => {
|
|
628
|
+
const lastMessage = messages2[messages2.length - 1];
|
|
629
|
+
lastMessage.id = value.messageId;
|
|
630
|
+
return [...messages2.slice(0, messages2.length - 1), lastMessage];
|
|
631
|
+
});
|
|
632
|
+
break;
|
|
633
|
+
}
|
|
634
|
+
case "error": {
|
|
635
|
+
setError(new Error(value));
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
} catch (error2) {
|
|
641
|
+
if ((0, import_provider_utils.isAbortError)(error2) && abortController.signal.aborted) {
|
|
642
|
+
abortControllerRef.current = null;
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
if (onError && error2 instanceof Error) {
|
|
646
|
+
onError(error2);
|
|
647
|
+
}
|
|
648
|
+
setError(error2);
|
|
649
|
+
} finally {
|
|
650
|
+
abortControllerRef.current = null;
|
|
651
|
+
setStatus("awaiting_message");
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
const submitMessage = async (event, requestOptions) => {
|
|
655
|
+
var _a;
|
|
656
|
+
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
|
657
|
+
if (input === "") {
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
append({ role: "user", content: input }, requestOptions);
|
|
661
|
+
};
|
|
662
|
+
return {
|
|
663
|
+
append,
|
|
664
|
+
messages,
|
|
665
|
+
setMessages,
|
|
666
|
+
threadId,
|
|
667
|
+
input,
|
|
668
|
+
setInput,
|
|
669
|
+
handleInputChange,
|
|
670
|
+
submitMessage,
|
|
671
|
+
status,
|
|
672
|
+
error,
|
|
673
|
+
stop
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
var experimental_useAssistant = useAssistant;
|
|
677
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
678
|
+
0 && (module.exports = {
|
|
679
|
+
experimental_useAssistant,
|
|
680
|
+
useAssistant,
|
|
681
|
+
useChat,
|
|
682
|
+
useCompletion
|
|
683
|
+
});
|
|
684
|
+
//# sourceMappingURL=index.js.map
|