ai 3.1.29 → 3.1.31
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/dist/index.d.mts +4 -563
- package/dist/index.d.ts +4 -563
- package/dist/index.js +38 -509
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +40 -497
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -33
- package/prompts/dist/index.d.mts +2 -129
- package/prompts/dist/index.d.ts +2 -129
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +9 -648
- package/react/dist/index.d.ts +9 -648
- package/react/dist/index.js +4 -1441
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +9 -1429
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/rsc-server.mjs +15 -236
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +7 -465
- package/solid/dist/index.d.ts +7 -465
- package/solid/dist/index.js +3 -1057
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +7 -1056
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +12 -422
- package/svelte/dist/index.d.ts +12 -422
- package/svelte/dist/index.js +12 -768
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +10 -762
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +7 -459
- package/vue/dist/index.d.ts +7 -459
- package/vue/dist/index.js +3 -1057
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +7 -1046
- package/vue/dist/index.mjs.map +1 -1
package/react/dist/index.js
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
'use client'
|
2
2
|
"use strict";
|
3
|
-
var __create = Object.create;
|
4
3
|
var __defProp = Object.defineProperty;
|
5
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
6
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
8
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
9
7
|
var __export = (target, all) => {
|
10
8
|
for (var name in all)
|
@@ -18,1457 +16,22 @@ var __copyProps = (to, from, except, desc) => {
|
|
18
16
|
}
|
19
17
|
return to;
|
20
18
|
};
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
22
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
23
|
-
// file that has been converted to a CommonJS file using a Babel-
|
24
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
25
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
27
|
-
mod
|
28
|
-
));
|
29
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
30
20
|
|
31
21
|
// react/index.ts
|
32
22
|
var react_exports = {};
|
33
23
|
__export(react_exports, {
|
34
|
-
experimental_useAssistant: () => experimental_useAssistant,
|
35
24
|
useAssistant: () => useAssistant,
|
36
25
|
useChat: () => useChat,
|
37
26
|
useCompletion: () => useCompletion
|
38
27
|
});
|
39
28
|
module.exports = __toCommonJS(react_exports);
|
40
|
-
|
41
|
-
|
42
|
-
var
|
43
|
-
var
|
44
|
-
|
45
|
-
// shared/generate-id.ts
|
46
|
-
var import_non_secure = require("nanoid/non-secure");
|
47
|
-
var generateId = (0, import_non_secure.customAlphabet)(
|
48
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
49
|
-
7
|
50
|
-
);
|
51
|
-
|
52
|
-
// shared/stream-parts.ts
|
53
|
-
var textStreamPart = {
|
54
|
-
code: "0",
|
55
|
-
name: "text",
|
56
|
-
parse: (value) => {
|
57
|
-
if (typeof value !== "string") {
|
58
|
-
throw new Error('"text" parts expect a string value.');
|
59
|
-
}
|
60
|
-
return { type: "text", value };
|
61
|
-
}
|
62
|
-
};
|
63
|
-
var functionCallStreamPart = {
|
64
|
-
code: "1",
|
65
|
-
name: "function_call",
|
66
|
-
parse: (value) => {
|
67
|
-
if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
|
68
|
-
throw new Error(
|
69
|
-
'"function_call" parts expect an object with a "function_call" property.'
|
70
|
-
);
|
71
|
-
}
|
72
|
-
return {
|
73
|
-
type: "function_call",
|
74
|
-
value
|
75
|
-
};
|
76
|
-
}
|
77
|
-
};
|
78
|
-
var dataStreamPart = {
|
79
|
-
code: "2",
|
80
|
-
name: "data",
|
81
|
-
parse: (value) => {
|
82
|
-
if (!Array.isArray(value)) {
|
83
|
-
throw new Error('"data" parts expect an array value.');
|
84
|
-
}
|
85
|
-
return { type: "data", value };
|
86
|
-
}
|
87
|
-
};
|
88
|
-
var errorStreamPart = {
|
89
|
-
code: "3",
|
90
|
-
name: "error",
|
91
|
-
parse: (value) => {
|
92
|
-
if (typeof value !== "string") {
|
93
|
-
throw new Error('"error" parts expect a string value.');
|
94
|
-
}
|
95
|
-
return { type: "error", value };
|
96
|
-
}
|
97
|
-
};
|
98
|
-
var assistantMessageStreamPart = {
|
99
|
-
code: "4",
|
100
|
-
name: "assistant_message",
|
101
|
-
parse: (value) => {
|
102
|
-
if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
|
103
|
-
(item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
|
104
|
-
)) {
|
105
|
-
throw new Error(
|
106
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
107
|
-
);
|
108
|
-
}
|
109
|
-
return {
|
110
|
-
type: "assistant_message",
|
111
|
-
value
|
112
|
-
};
|
113
|
-
}
|
114
|
-
};
|
115
|
-
var assistantControlDataStreamPart = {
|
116
|
-
code: "5",
|
117
|
-
name: "assistant_control_data",
|
118
|
-
parse: (value) => {
|
119
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
120
|
-
throw new Error(
|
121
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
122
|
-
);
|
123
|
-
}
|
124
|
-
return {
|
125
|
-
type: "assistant_control_data",
|
126
|
-
value: {
|
127
|
-
threadId: value.threadId,
|
128
|
-
messageId: value.messageId
|
129
|
-
}
|
130
|
-
};
|
131
|
-
}
|
132
|
-
};
|
133
|
-
var dataMessageStreamPart = {
|
134
|
-
code: "6",
|
135
|
-
name: "data_message",
|
136
|
-
parse: (value) => {
|
137
|
-
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
138
|
-
throw new Error(
|
139
|
-
'"data_message" parts expect an object with a "role" and "data" property.'
|
140
|
-
);
|
141
|
-
}
|
142
|
-
return {
|
143
|
-
type: "data_message",
|
144
|
-
value
|
145
|
-
};
|
146
|
-
}
|
147
|
-
};
|
148
|
-
var toolCallsStreamPart = {
|
149
|
-
code: "7",
|
150
|
-
name: "tool_calls",
|
151
|
-
parse: (value) => {
|
152
|
-
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some(
|
153
|
-
(tc) => tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string"
|
154
|
-
)) {
|
155
|
-
throw new Error(
|
156
|
-
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
157
|
-
);
|
158
|
-
}
|
159
|
-
return {
|
160
|
-
type: "tool_calls",
|
161
|
-
value
|
162
|
-
};
|
163
|
-
}
|
164
|
-
};
|
165
|
-
var messageAnnotationsStreamPart = {
|
166
|
-
code: "8",
|
167
|
-
name: "message_annotations",
|
168
|
-
parse: (value) => {
|
169
|
-
if (!Array.isArray(value)) {
|
170
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
171
|
-
}
|
172
|
-
return { type: "message_annotations", value };
|
173
|
-
}
|
174
|
-
};
|
175
|
-
var toolCallStreamPart = {
|
176
|
-
code: "9",
|
177
|
-
name: "tool_call",
|
178
|
-
parse: (value) => {
|
179
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
|
180
|
-
throw new Error(
|
181
|
-
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
182
|
-
);
|
183
|
-
}
|
184
|
-
return {
|
185
|
-
type: "tool_call",
|
186
|
-
value
|
187
|
-
};
|
188
|
-
}
|
189
|
-
};
|
190
|
-
var toolResultStreamPart = {
|
191
|
-
code: "a",
|
192
|
-
name: "tool_result",
|
193
|
-
parse: (value) => {
|
194
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
|
195
|
-
throw new Error(
|
196
|
-
'"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
|
197
|
-
);
|
198
|
-
}
|
199
|
-
return {
|
200
|
-
type: "tool_result",
|
201
|
-
value
|
202
|
-
};
|
203
|
-
}
|
204
|
-
};
|
205
|
-
var streamParts = [
|
206
|
-
textStreamPart,
|
207
|
-
functionCallStreamPart,
|
208
|
-
dataStreamPart,
|
209
|
-
errorStreamPart,
|
210
|
-
assistantMessageStreamPart,
|
211
|
-
assistantControlDataStreamPart,
|
212
|
-
dataMessageStreamPart,
|
213
|
-
toolCallsStreamPart,
|
214
|
-
messageAnnotationsStreamPart,
|
215
|
-
toolCallStreamPart,
|
216
|
-
toolResultStreamPart
|
217
|
-
];
|
218
|
-
var streamPartsByCode = {
|
219
|
-
[textStreamPart.code]: textStreamPart,
|
220
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
221
|
-
[dataStreamPart.code]: dataStreamPart,
|
222
|
-
[errorStreamPart.code]: errorStreamPart,
|
223
|
-
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
224
|
-
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
225
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
226
|
-
[toolCallsStreamPart.code]: toolCallsStreamPart,
|
227
|
-
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
|
228
|
-
[toolCallStreamPart.code]: toolCallStreamPart,
|
229
|
-
[toolResultStreamPart.code]: toolResultStreamPart
|
230
|
-
};
|
231
|
-
var StreamStringPrefixes = {
|
232
|
-
[textStreamPart.name]: textStreamPart.code,
|
233
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
234
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
235
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
236
|
-
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
237
|
-
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
238
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
239
|
-
[toolCallsStreamPart.name]: toolCallsStreamPart.code,
|
240
|
-
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
|
241
|
-
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
242
|
-
[toolResultStreamPart.name]: toolResultStreamPart.code
|
243
|
-
};
|
244
|
-
var validCodes = streamParts.map((part) => part.code);
|
245
|
-
var parseStreamPart = (line) => {
|
246
|
-
const firstSeparatorIndex = line.indexOf(":");
|
247
|
-
if (firstSeparatorIndex === -1) {
|
248
|
-
throw new Error("Failed to parse stream string. No separator found.");
|
249
|
-
}
|
250
|
-
const prefix = line.slice(0, firstSeparatorIndex);
|
251
|
-
if (!validCodes.includes(prefix)) {
|
252
|
-
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
253
|
-
}
|
254
|
-
const code = prefix;
|
255
|
-
const textValue = line.slice(firstSeparatorIndex + 1);
|
256
|
-
const jsonValue = JSON.parse(textValue);
|
257
|
-
return streamPartsByCode[code].parse(jsonValue);
|
258
|
-
};
|
259
|
-
|
260
|
-
// shared/read-data-stream.ts
|
261
|
-
var NEWLINE = "\n".charCodeAt(0);
|
262
|
-
function concatChunks(chunks, totalLength) {
|
263
|
-
const concatenatedChunks = new Uint8Array(totalLength);
|
264
|
-
let offset = 0;
|
265
|
-
for (const chunk of chunks) {
|
266
|
-
concatenatedChunks.set(chunk, offset);
|
267
|
-
offset += chunk.length;
|
268
|
-
}
|
269
|
-
chunks.length = 0;
|
270
|
-
return concatenatedChunks;
|
271
|
-
}
|
272
|
-
async function* readDataStream(reader, {
|
273
|
-
isAborted
|
274
|
-
} = {}) {
|
275
|
-
const decoder = new TextDecoder();
|
276
|
-
const chunks = [];
|
277
|
-
let totalLength = 0;
|
278
|
-
while (true) {
|
279
|
-
const { value } = await reader.read();
|
280
|
-
if (value) {
|
281
|
-
chunks.push(value);
|
282
|
-
totalLength += value.length;
|
283
|
-
if (value[value.length - 1] !== NEWLINE) {
|
284
|
-
continue;
|
285
|
-
}
|
286
|
-
}
|
287
|
-
if (chunks.length === 0) {
|
288
|
-
break;
|
289
|
-
}
|
290
|
-
const concatenatedChunks = concatChunks(chunks, totalLength);
|
291
|
-
totalLength = 0;
|
292
|
-
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
293
|
-
for (const streamPart of streamParts2) {
|
294
|
-
yield streamPart;
|
295
|
-
}
|
296
|
-
if (isAborted == null ? void 0 : isAborted()) {
|
297
|
-
reader.cancel();
|
298
|
-
break;
|
299
|
-
}
|
300
|
-
}
|
301
|
-
}
|
302
|
-
|
303
|
-
// shared/parse-complex-response.ts
|
304
|
-
function assignAnnotationsToMessage(message, annotations) {
|
305
|
-
if (!message || !annotations || !annotations.length)
|
306
|
-
return message;
|
307
|
-
return { ...message, annotations: [...annotations] };
|
308
|
-
}
|
309
|
-
async function parseComplexResponse({
|
310
|
-
reader,
|
311
|
-
abortControllerRef,
|
312
|
-
update,
|
313
|
-
onToolCall,
|
314
|
-
onFinish,
|
315
|
-
generateId: generateId2 = generateId,
|
316
|
-
getCurrentDate = () => /* @__PURE__ */ new Date()
|
317
|
-
}) {
|
318
|
-
const createdAt = getCurrentDate();
|
319
|
-
const prefixMap = {
|
320
|
-
data: []
|
321
|
-
};
|
322
|
-
let message_annotations = void 0;
|
323
|
-
for await (const { type, value } of readDataStream(reader, {
|
324
|
-
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
325
|
-
})) {
|
326
|
-
if (type === "text") {
|
327
|
-
if (prefixMap["text"]) {
|
328
|
-
prefixMap["text"] = {
|
329
|
-
...prefixMap["text"],
|
330
|
-
content: (prefixMap["text"].content || "") + value
|
331
|
-
};
|
332
|
-
} else {
|
333
|
-
prefixMap["text"] = {
|
334
|
-
id: generateId2(),
|
335
|
-
role: "assistant",
|
336
|
-
content: value,
|
337
|
-
createdAt
|
338
|
-
};
|
339
|
-
}
|
340
|
-
}
|
341
|
-
if (type === "tool_call") {
|
342
|
-
if (prefixMap.text == null) {
|
343
|
-
prefixMap.text = {
|
344
|
-
id: generateId2(),
|
345
|
-
role: "assistant",
|
346
|
-
content: "",
|
347
|
-
createdAt
|
348
|
-
};
|
349
|
-
}
|
350
|
-
if (prefixMap.text.toolInvocations == null) {
|
351
|
-
prefixMap.text.toolInvocations = [];
|
352
|
-
}
|
353
|
-
prefixMap.text.toolInvocations.push(value);
|
354
|
-
if (onToolCall) {
|
355
|
-
const result = await onToolCall({ toolCall: value });
|
356
|
-
if (result != null) {
|
357
|
-
prefixMap.text.toolInvocations[prefixMap.text.toolInvocations.length - 1] = { ...value, result };
|
358
|
-
}
|
359
|
-
}
|
360
|
-
} else if (type === "tool_result") {
|
361
|
-
if (prefixMap.text == null) {
|
362
|
-
prefixMap.text = {
|
363
|
-
id: generateId2(),
|
364
|
-
role: "assistant",
|
365
|
-
content: "",
|
366
|
-
createdAt
|
367
|
-
};
|
368
|
-
}
|
369
|
-
if (prefixMap.text.toolInvocations == null) {
|
370
|
-
prefixMap.text.toolInvocations = [];
|
371
|
-
}
|
372
|
-
const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
|
373
|
-
(invocation) => invocation.toolCallId === value.toolCallId
|
374
|
-
);
|
375
|
-
if (toolInvocationIndex !== -1) {
|
376
|
-
prefixMap.text.toolInvocations[toolInvocationIndex] = value;
|
377
|
-
} else {
|
378
|
-
prefixMap.text.toolInvocations.push(value);
|
379
|
-
}
|
380
|
-
}
|
381
|
-
let functionCallMessage = null;
|
382
|
-
if (type === "function_call") {
|
383
|
-
prefixMap["function_call"] = {
|
384
|
-
id: generateId2(),
|
385
|
-
role: "assistant",
|
386
|
-
content: "",
|
387
|
-
function_call: value.function_call,
|
388
|
-
name: value.function_call.name,
|
389
|
-
createdAt
|
390
|
-
};
|
391
|
-
functionCallMessage = prefixMap["function_call"];
|
392
|
-
}
|
393
|
-
let toolCallMessage = null;
|
394
|
-
if (type === "tool_calls") {
|
395
|
-
prefixMap["tool_calls"] = {
|
396
|
-
id: generateId2(),
|
397
|
-
role: "assistant",
|
398
|
-
content: "",
|
399
|
-
tool_calls: value.tool_calls,
|
400
|
-
createdAt
|
401
|
-
};
|
402
|
-
toolCallMessage = prefixMap["tool_calls"];
|
403
|
-
}
|
404
|
-
if (type === "data") {
|
405
|
-
prefixMap["data"].push(...value);
|
406
|
-
}
|
407
|
-
let responseMessage = prefixMap["text"];
|
408
|
-
if (type === "message_annotations") {
|
409
|
-
if (!message_annotations) {
|
410
|
-
message_annotations = [...value];
|
411
|
-
} else {
|
412
|
-
message_annotations.push(...value);
|
413
|
-
}
|
414
|
-
functionCallMessage = assignAnnotationsToMessage(
|
415
|
-
prefixMap["function_call"],
|
416
|
-
message_annotations
|
417
|
-
);
|
418
|
-
toolCallMessage = assignAnnotationsToMessage(
|
419
|
-
prefixMap["tool_calls"],
|
420
|
-
message_annotations
|
421
|
-
);
|
422
|
-
responseMessage = assignAnnotationsToMessage(
|
423
|
-
prefixMap["text"],
|
424
|
-
message_annotations
|
425
|
-
);
|
426
|
-
}
|
427
|
-
if (message_annotations == null ? void 0 : message_annotations.length) {
|
428
|
-
const messagePrefixKeys = [
|
429
|
-
"text",
|
430
|
-
"function_call",
|
431
|
-
"tool_calls"
|
432
|
-
];
|
433
|
-
messagePrefixKeys.forEach((key) => {
|
434
|
-
if (prefixMap[key]) {
|
435
|
-
prefixMap[key].annotations = [...message_annotations];
|
436
|
-
}
|
437
|
-
});
|
438
|
-
}
|
439
|
-
const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
|
440
|
-
...assignAnnotationsToMessage(message, message_annotations)
|
441
|
-
}));
|
442
|
-
update(merged, [...prefixMap["data"]]);
|
443
|
-
}
|
444
|
-
onFinish == null ? void 0 : onFinish(prefixMap);
|
445
|
-
return {
|
446
|
-
messages: [
|
447
|
-
prefixMap.text,
|
448
|
-
prefixMap.function_call,
|
449
|
-
prefixMap.tool_calls
|
450
|
-
].filter(Boolean),
|
451
|
-
data: prefixMap.data
|
452
|
-
};
|
453
|
-
}
|
454
|
-
|
455
|
-
// shared/utils.ts
|
456
|
-
function createChunkDecoder(complex) {
|
457
|
-
const decoder = new TextDecoder();
|
458
|
-
if (!complex) {
|
459
|
-
return function(chunk) {
|
460
|
-
if (!chunk)
|
461
|
-
return "";
|
462
|
-
return decoder.decode(chunk, { stream: true });
|
463
|
-
};
|
464
|
-
}
|
465
|
-
return function(chunk) {
|
466
|
-
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
467
|
-
return decoded.map(parseStreamPart).filter(Boolean);
|
468
|
-
};
|
469
|
-
}
|
470
|
-
|
471
|
-
// shared/call-chat-api.ts
|
472
|
-
async function callChatApi({
|
473
|
-
api,
|
474
|
-
messages,
|
475
|
-
body,
|
476
|
-
streamMode = "stream-data",
|
477
|
-
credentials,
|
478
|
-
headers,
|
479
|
-
abortController,
|
480
|
-
restoreMessagesOnFailure,
|
481
|
-
onResponse,
|
482
|
-
onUpdate,
|
483
|
-
onFinish,
|
484
|
-
onToolCall,
|
485
|
-
generateId: generateId2
|
486
|
-
}) {
|
487
|
-
var _a;
|
488
|
-
const response = await fetch(api, {
|
489
|
-
method: "POST",
|
490
|
-
body: JSON.stringify({
|
491
|
-
messages,
|
492
|
-
...body
|
493
|
-
}),
|
494
|
-
headers: {
|
495
|
-
"Content-Type": "application/json",
|
496
|
-
...headers
|
497
|
-
},
|
498
|
-
signal: (_a = abortController == null ? void 0 : abortController()) == null ? void 0 : _a.signal,
|
499
|
-
credentials
|
500
|
-
}).catch((err) => {
|
501
|
-
restoreMessagesOnFailure();
|
502
|
-
throw err;
|
503
|
-
});
|
504
|
-
if (onResponse) {
|
505
|
-
try {
|
506
|
-
await onResponse(response);
|
507
|
-
} catch (err) {
|
508
|
-
throw err;
|
509
|
-
}
|
510
|
-
}
|
511
|
-
if (!response.ok) {
|
512
|
-
restoreMessagesOnFailure();
|
513
|
-
throw new Error(
|
514
|
-
await response.text() || "Failed to fetch the chat response."
|
515
|
-
);
|
516
|
-
}
|
517
|
-
if (!response.body) {
|
518
|
-
throw new Error("The response body is empty.");
|
519
|
-
}
|
520
|
-
const reader = response.body.getReader();
|
521
|
-
switch (streamMode) {
|
522
|
-
case "text": {
|
523
|
-
const decoder = createChunkDecoder();
|
524
|
-
const resultMessage = {
|
525
|
-
id: generateId2(),
|
526
|
-
createdAt: /* @__PURE__ */ new Date(),
|
527
|
-
role: "assistant",
|
528
|
-
content: ""
|
529
|
-
};
|
530
|
-
while (true) {
|
531
|
-
const { done, value } = await reader.read();
|
532
|
-
if (done) {
|
533
|
-
break;
|
534
|
-
}
|
535
|
-
resultMessage.content += decoder(value);
|
536
|
-
resultMessage.id = generateId2();
|
537
|
-
onUpdate([{ ...resultMessage }], []);
|
538
|
-
if ((abortController == null ? void 0 : abortController()) === null) {
|
539
|
-
reader.cancel();
|
540
|
-
break;
|
541
|
-
}
|
542
|
-
}
|
543
|
-
onFinish == null ? void 0 : onFinish(resultMessage);
|
544
|
-
return {
|
545
|
-
messages: [resultMessage],
|
546
|
-
data: []
|
547
|
-
};
|
548
|
-
}
|
549
|
-
case "stream-data": {
|
550
|
-
return await parseComplexResponse({
|
551
|
-
reader,
|
552
|
-
abortControllerRef: abortController != null ? { current: abortController() } : void 0,
|
553
|
-
update: onUpdate,
|
554
|
-
onToolCall,
|
555
|
-
onFinish(prefixMap) {
|
556
|
-
if (onFinish && prefixMap.text != null) {
|
557
|
-
onFinish(prefixMap.text);
|
558
|
-
}
|
559
|
-
},
|
560
|
-
generateId: generateId2
|
561
|
-
});
|
562
|
-
}
|
563
|
-
default: {
|
564
|
-
const exhaustiveCheck = streamMode;
|
565
|
-
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
566
|
-
}
|
567
|
-
}
|
568
|
-
}
|
569
|
-
|
570
|
-
// shared/process-chat-stream.ts
|
571
|
-
async function processChatStream({
|
572
|
-
getStreamedResponse: getStreamedResponse2,
|
573
|
-
experimental_onFunctionCall,
|
574
|
-
experimental_onToolCall,
|
575
|
-
updateChatRequest,
|
576
|
-
getCurrentMessages
|
577
|
-
}) {
|
578
|
-
while (true) {
|
579
|
-
const messagesAndDataOrJustMessage = await getStreamedResponse2();
|
580
|
-
if ("messages" in messagesAndDataOrJustMessage) {
|
581
|
-
let hasFollowingResponse = false;
|
582
|
-
for (const message of messagesAndDataOrJustMessage.messages) {
|
583
|
-
if ((message.function_call === void 0 || typeof message.function_call === "string") && (message.tool_calls === void 0 || typeof message.tool_calls === "string")) {
|
584
|
-
continue;
|
585
|
-
}
|
586
|
-
hasFollowingResponse = true;
|
587
|
-
if (experimental_onFunctionCall) {
|
588
|
-
const functionCall = message.function_call;
|
589
|
-
if (typeof functionCall !== "object") {
|
590
|
-
console.warn(
|
591
|
-
"experimental_onFunctionCall should not be defined when using tools"
|
592
|
-
);
|
593
|
-
continue;
|
594
|
-
}
|
595
|
-
const functionCallResponse = await experimental_onFunctionCall(
|
596
|
-
getCurrentMessages(),
|
597
|
-
functionCall
|
598
|
-
);
|
599
|
-
if (functionCallResponse === void 0) {
|
600
|
-
hasFollowingResponse = false;
|
601
|
-
break;
|
602
|
-
}
|
603
|
-
updateChatRequest(functionCallResponse);
|
604
|
-
}
|
605
|
-
if (experimental_onToolCall) {
|
606
|
-
const toolCalls = message.tool_calls;
|
607
|
-
if (!Array.isArray(toolCalls) || toolCalls.some((toolCall) => typeof toolCall !== "object")) {
|
608
|
-
console.warn(
|
609
|
-
"experimental_onToolCall should not be defined when using tools"
|
610
|
-
);
|
611
|
-
continue;
|
612
|
-
}
|
613
|
-
const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
|
614
|
-
if (toolCallResponse === void 0) {
|
615
|
-
hasFollowingResponse = false;
|
616
|
-
break;
|
617
|
-
}
|
618
|
-
updateChatRequest(toolCallResponse);
|
619
|
-
}
|
620
|
-
}
|
621
|
-
if (!hasFollowingResponse) {
|
622
|
-
break;
|
623
|
-
}
|
624
|
-
} else {
|
625
|
-
let fixFunctionCallArguments2 = function(response) {
|
626
|
-
for (const message of response.messages) {
|
627
|
-
if (message.tool_calls !== void 0) {
|
628
|
-
for (const toolCall of message.tool_calls) {
|
629
|
-
if (typeof toolCall === "object") {
|
630
|
-
if (toolCall.function.arguments && typeof toolCall.function.arguments !== "string") {
|
631
|
-
toolCall.function.arguments = JSON.stringify(
|
632
|
-
toolCall.function.arguments
|
633
|
-
);
|
634
|
-
}
|
635
|
-
}
|
636
|
-
}
|
637
|
-
}
|
638
|
-
if (message.function_call !== void 0) {
|
639
|
-
if (typeof message.function_call === "object") {
|
640
|
-
if (message.function_call.arguments && typeof message.function_call.arguments !== "string") {
|
641
|
-
message.function_call.arguments = JSON.stringify(
|
642
|
-
message.function_call.arguments
|
643
|
-
);
|
644
|
-
}
|
645
|
-
}
|
646
|
-
}
|
647
|
-
}
|
648
|
-
};
|
649
|
-
var fixFunctionCallArguments = fixFunctionCallArguments2;
|
650
|
-
const streamedResponseMessage = messagesAndDataOrJustMessage;
|
651
|
-
if ((streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") && (streamedResponseMessage.tool_calls === void 0 || typeof streamedResponseMessage.tool_calls === "string")) {
|
652
|
-
break;
|
653
|
-
}
|
654
|
-
if (experimental_onFunctionCall) {
|
655
|
-
const functionCall = streamedResponseMessage.function_call;
|
656
|
-
if (!(typeof functionCall === "object")) {
|
657
|
-
console.warn(
|
658
|
-
"experimental_onFunctionCall should not be defined when using tools"
|
659
|
-
);
|
660
|
-
continue;
|
661
|
-
}
|
662
|
-
const functionCallResponse = await experimental_onFunctionCall(getCurrentMessages(), functionCall);
|
663
|
-
if (functionCallResponse === void 0)
|
664
|
-
break;
|
665
|
-
fixFunctionCallArguments2(functionCallResponse);
|
666
|
-
updateChatRequest(functionCallResponse);
|
667
|
-
}
|
668
|
-
if (experimental_onToolCall) {
|
669
|
-
const toolCalls = streamedResponseMessage.tool_calls;
|
670
|
-
if (!(typeof toolCalls === "object")) {
|
671
|
-
console.warn(
|
672
|
-
"experimental_onToolCall should not be defined when using functions"
|
673
|
-
);
|
674
|
-
continue;
|
675
|
-
}
|
676
|
-
const toolCallResponse = await experimental_onToolCall(getCurrentMessages(), toolCalls);
|
677
|
-
if (toolCallResponse === void 0)
|
678
|
-
break;
|
679
|
-
fixFunctionCallArguments2(toolCallResponse);
|
680
|
-
updateChatRequest(toolCallResponse);
|
681
|
-
}
|
682
|
-
}
|
683
|
-
}
|
684
|
-
}
|
685
|
-
|
686
|
-
// react/use-chat.ts
|
687
|
-
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamMode, onFinish, onResponse, onToolCall, sendExtraMessageFields) => {
|
688
|
-
var _a, _b;
|
689
|
-
const previousMessages = messagesRef.current;
|
690
|
-
mutate(chatRequest.messages, false);
|
691
|
-
const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
|
692
|
-
({
|
693
|
-
role,
|
694
|
-
content,
|
695
|
-
name,
|
696
|
-
data,
|
697
|
-
annotations,
|
698
|
-
toolInvocations,
|
699
|
-
function_call,
|
700
|
-
tool_calls,
|
701
|
-
tool_call_id
|
702
|
-
}) => ({
|
703
|
-
role,
|
704
|
-
content,
|
705
|
-
...name !== void 0 && { name },
|
706
|
-
...data !== void 0 && { data },
|
707
|
-
...annotations !== void 0 && { annotations },
|
708
|
-
...toolInvocations !== void 0 && { toolInvocations },
|
709
|
-
// outdated function/tool call handling (TODO deprecate):
|
710
|
-
tool_call_id,
|
711
|
-
...function_call !== void 0 && { function_call },
|
712
|
-
...tool_calls !== void 0 && { tool_calls }
|
713
|
-
})
|
714
|
-
);
|
715
|
-
if (typeof api !== "string") {
|
716
|
-
const replyId = generateId2();
|
717
|
-
const createdAt = /* @__PURE__ */ new Date();
|
718
|
-
let responseMessage = {
|
719
|
-
id: replyId,
|
720
|
-
createdAt,
|
721
|
-
content: "",
|
722
|
-
role: "assistant"
|
723
|
-
};
|
724
|
-
async function readRow(promise) {
|
725
|
-
const { content, ui, next } = await promise;
|
726
|
-
responseMessage["content"] = content;
|
727
|
-
responseMessage["ui"] = await ui;
|
728
|
-
mutate([...chatRequest.messages, { ...responseMessage }], false);
|
729
|
-
if (next) {
|
730
|
-
await readRow(next);
|
731
|
-
}
|
732
|
-
}
|
733
|
-
try {
|
734
|
-
const promise = api({
|
735
|
-
messages: constructedMessagesPayload,
|
736
|
-
data: chatRequest.data
|
737
|
-
});
|
738
|
-
await readRow(promise);
|
739
|
-
} catch (e) {
|
740
|
-
mutate(previousMessages, false);
|
741
|
-
throw e;
|
742
|
-
}
|
743
|
-
if (onFinish) {
|
744
|
-
onFinish(responseMessage);
|
745
|
-
}
|
746
|
-
return responseMessage;
|
747
|
-
}
|
748
|
-
return await callChatApi({
|
749
|
-
api,
|
750
|
-
messages: constructedMessagesPayload,
|
751
|
-
body: {
|
752
|
-
data: chatRequest.data,
|
753
|
-
...extraMetadataRef.current.body,
|
754
|
-
...(_a = chatRequest.options) == null ? void 0 : _a.body,
|
755
|
-
...chatRequest.functions !== void 0 && {
|
756
|
-
functions: chatRequest.functions
|
757
|
-
},
|
758
|
-
...chatRequest.function_call !== void 0 && {
|
759
|
-
function_call: chatRequest.function_call
|
760
|
-
},
|
761
|
-
...chatRequest.tools !== void 0 && {
|
762
|
-
tools: chatRequest.tools
|
763
|
-
},
|
764
|
-
...chatRequest.tool_choice !== void 0 && {
|
765
|
-
tool_choice: chatRequest.tool_choice
|
766
|
-
}
|
767
|
-
},
|
768
|
-
streamMode,
|
769
|
-
credentials: extraMetadataRef.current.credentials,
|
770
|
-
headers: {
|
771
|
-
...extraMetadataRef.current.headers,
|
772
|
-
...(_b = chatRequest.options) == null ? void 0 : _b.headers
|
773
|
-
},
|
774
|
-
abortController: () => abortControllerRef.current,
|
775
|
-
restoreMessagesOnFailure() {
|
776
|
-
mutate(previousMessages, false);
|
777
|
-
},
|
778
|
-
onResponse,
|
779
|
-
onUpdate(merged, data) {
|
780
|
-
mutate([...chatRequest.messages, ...merged], false);
|
781
|
-
mutateStreamData([...existingData || [], ...data || []], false);
|
782
|
-
},
|
783
|
-
onToolCall,
|
784
|
-
onFinish,
|
785
|
-
generateId: generateId2
|
786
|
-
});
|
787
|
-
};
|
788
|
-
function useChat({
|
789
|
-
api = "/api/chat",
|
790
|
-
id,
|
791
|
-
initialMessages,
|
792
|
-
initialInput = "",
|
793
|
-
sendExtraMessageFields,
|
794
|
-
experimental_onFunctionCall,
|
795
|
-
experimental_onToolCall,
|
796
|
-
onToolCall,
|
797
|
-
experimental_maxAutomaticRoundtrips = 0,
|
798
|
-
maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,
|
799
|
-
maxToolRoundtrips = maxAutomaticRoundtrips,
|
800
|
-
streamMode,
|
801
|
-
onResponse,
|
802
|
-
onFinish,
|
803
|
-
onError,
|
804
|
-
credentials,
|
805
|
-
headers,
|
806
|
-
body,
|
807
|
-
generateId: generateId2 = generateId
|
808
|
-
} = {}) {
|
809
|
-
const hookId = (0, import_react.useId)();
|
810
|
-
const idKey = id != null ? id : hookId;
|
811
|
-
const chatKey = typeof api === "string" ? [api, idKey] : idKey;
|
812
|
-
const [initialMessagesFallback] = (0, import_react.useState)([]);
|
813
|
-
const { data: messages, mutate } = (0, import_swr.default)(
|
814
|
-
[chatKey, "messages"],
|
815
|
-
null,
|
816
|
-
{ fallbackData: initialMessages != null ? initialMessages : initialMessagesFallback }
|
817
|
-
);
|
818
|
-
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr.default)(
|
819
|
-
[chatKey, "loading"],
|
820
|
-
null
|
821
|
-
);
|
822
|
-
const { data: streamData, mutate: mutateStreamData } = (0, import_swr.default)([chatKey, "streamData"], null);
|
823
|
-
const { data: error = void 0, mutate: setError } = (0, import_swr.default)([chatKey, "error"], null);
|
824
|
-
const messagesRef = (0, import_react.useRef)(messages || []);
|
825
|
-
(0, import_react.useEffect)(() => {
|
826
|
-
messagesRef.current = messages || [];
|
827
|
-
}, [messages]);
|
828
|
-
const abortControllerRef = (0, import_react.useRef)(null);
|
829
|
-
const extraMetadataRef = (0, import_react.useRef)({
|
830
|
-
credentials,
|
831
|
-
headers,
|
832
|
-
body
|
833
|
-
});
|
834
|
-
(0, import_react.useEffect)(() => {
|
835
|
-
extraMetadataRef.current = {
|
836
|
-
credentials,
|
837
|
-
headers,
|
838
|
-
body
|
839
|
-
};
|
840
|
-
}, [credentials, headers, body]);
|
841
|
-
const triggerRequest = (0, import_react.useCallback)(
|
842
|
-
async (chatRequest) => {
|
843
|
-
try {
|
844
|
-
mutateLoading(true);
|
845
|
-
setError(void 0);
|
846
|
-
const abortController = new AbortController();
|
847
|
-
abortControllerRef.current = abortController;
|
848
|
-
await processChatStream({
|
849
|
-
getStreamedResponse: () => getStreamedResponse(
|
850
|
-
api,
|
851
|
-
chatRequest,
|
852
|
-
mutate,
|
853
|
-
mutateStreamData,
|
854
|
-
streamData,
|
855
|
-
extraMetadataRef,
|
856
|
-
messagesRef,
|
857
|
-
abortControllerRef,
|
858
|
-
generateId2,
|
859
|
-
streamMode,
|
860
|
-
onFinish,
|
861
|
-
onResponse,
|
862
|
-
onToolCall,
|
863
|
-
sendExtraMessageFields
|
864
|
-
),
|
865
|
-
experimental_onFunctionCall,
|
866
|
-
experimental_onToolCall,
|
867
|
-
updateChatRequest: (chatRequestParam) => {
|
868
|
-
chatRequest = chatRequestParam;
|
869
|
-
},
|
870
|
-
getCurrentMessages: () => messagesRef.current
|
871
|
-
});
|
872
|
-
abortControllerRef.current = null;
|
873
|
-
} catch (err) {
|
874
|
-
if (err.name === "AbortError") {
|
875
|
-
abortControllerRef.current = null;
|
876
|
-
return null;
|
877
|
-
}
|
878
|
-
if (onError && err instanceof Error) {
|
879
|
-
onError(err);
|
880
|
-
}
|
881
|
-
setError(err);
|
882
|
-
} finally {
|
883
|
-
mutateLoading(false);
|
884
|
-
}
|
885
|
-
const messages2 = messagesRef.current;
|
886
|
-
const lastMessage = messages2[messages2.length - 1];
|
887
|
-
if (
|
888
|
-
// ensure there is a last message:
|
889
|
-
lastMessage != null && // check if the feature is enabled:
|
890
|
-
maxToolRoundtrips > 0 && // check that roundtrip is possible:
|
891
|
-
isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic roundtrips:
|
892
|
-
countTrailingAssistantMessages(messages2) <= maxToolRoundtrips
|
893
|
-
) {
|
894
|
-
await triggerRequest({ messages: messages2 });
|
895
|
-
}
|
896
|
-
},
|
897
|
-
[
|
898
|
-
mutate,
|
899
|
-
mutateLoading,
|
900
|
-
api,
|
901
|
-
extraMetadataRef,
|
902
|
-
onResponse,
|
903
|
-
onFinish,
|
904
|
-
onError,
|
905
|
-
setError,
|
906
|
-
mutateStreamData,
|
907
|
-
streamData,
|
908
|
-
streamMode,
|
909
|
-
sendExtraMessageFields,
|
910
|
-
experimental_onFunctionCall,
|
911
|
-
experimental_onToolCall,
|
912
|
-
onToolCall,
|
913
|
-
maxToolRoundtrips,
|
914
|
-
messagesRef,
|
915
|
-
abortControllerRef,
|
916
|
-
generateId2
|
917
|
-
]
|
918
|
-
);
|
919
|
-
const append = (0, import_react.useCallback)(
|
920
|
-
async (message, {
|
921
|
-
options,
|
922
|
-
functions,
|
923
|
-
function_call,
|
924
|
-
tools,
|
925
|
-
tool_choice,
|
926
|
-
data
|
927
|
-
} = {}) => {
|
928
|
-
if (!message.id) {
|
929
|
-
message.id = generateId2();
|
930
|
-
}
|
931
|
-
const chatRequest = {
|
932
|
-
messages: messagesRef.current.concat(message),
|
933
|
-
options,
|
934
|
-
data,
|
935
|
-
...functions !== void 0 && { functions },
|
936
|
-
...function_call !== void 0 && { function_call },
|
937
|
-
...tools !== void 0 && { tools },
|
938
|
-
...tool_choice !== void 0 && { tool_choice }
|
939
|
-
};
|
940
|
-
return triggerRequest(chatRequest);
|
941
|
-
},
|
942
|
-
[triggerRequest, generateId2]
|
943
|
-
);
|
944
|
-
const reload = (0, import_react.useCallback)(
|
945
|
-
async ({
|
946
|
-
options,
|
947
|
-
functions,
|
948
|
-
function_call,
|
949
|
-
tools,
|
950
|
-
tool_choice
|
951
|
-
} = {}) => {
|
952
|
-
if (messagesRef.current.length === 0)
|
953
|
-
return null;
|
954
|
-
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
|
955
|
-
if (lastMessage.role === "assistant") {
|
956
|
-
const chatRequest2 = {
|
957
|
-
messages: messagesRef.current.slice(0, -1),
|
958
|
-
options,
|
959
|
-
...functions !== void 0 && { functions },
|
960
|
-
...function_call !== void 0 && { function_call },
|
961
|
-
...tools !== void 0 && { tools },
|
962
|
-
...tool_choice !== void 0 && { tool_choice }
|
963
|
-
};
|
964
|
-
return triggerRequest(chatRequest2);
|
965
|
-
}
|
966
|
-
const chatRequest = {
|
967
|
-
messages: messagesRef.current,
|
968
|
-
options,
|
969
|
-
...functions !== void 0 && { functions },
|
970
|
-
...function_call !== void 0 && { function_call },
|
971
|
-
...tools !== void 0 && { tools },
|
972
|
-
...tool_choice !== void 0 && { tool_choice }
|
973
|
-
};
|
974
|
-
return triggerRequest(chatRequest);
|
975
|
-
},
|
976
|
-
[triggerRequest]
|
977
|
-
);
|
978
|
-
const stop = (0, import_react.useCallback)(() => {
|
979
|
-
if (abortControllerRef.current) {
|
980
|
-
abortControllerRef.current.abort();
|
981
|
-
abortControllerRef.current = null;
|
982
|
-
}
|
983
|
-
}, []);
|
984
|
-
const setMessages = (0, import_react.useCallback)(
|
985
|
-
(messages2) => {
|
986
|
-
mutate(messages2, false);
|
987
|
-
messagesRef.current = messages2;
|
988
|
-
},
|
989
|
-
[mutate]
|
990
|
-
);
|
991
|
-
const [input, setInput] = (0, import_react.useState)(initialInput);
|
992
|
-
const handleSubmit = (0, import_react.useCallback)(
|
993
|
-
(e, options = {}, metadata) => {
|
994
|
-
if (metadata) {
|
995
|
-
extraMetadataRef.current = {
|
996
|
-
...extraMetadataRef.current,
|
997
|
-
...metadata
|
998
|
-
};
|
999
|
-
}
|
1000
|
-
e.preventDefault();
|
1001
|
-
if (!input)
|
1002
|
-
return;
|
1003
|
-
append(
|
1004
|
-
{
|
1005
|
-
content: input,
|
1006
|
-
role: "user",
|
1007
|
-
createdAt: /* @__PURE__ */ new Date()
|
1008
|
-
},
|
1009
|
-
options
|
1010
|
-
);
|
1011
|
-
setInput("");
|
1012
|
-
},
|
1013
|
-
[input, append]
|
1014
|
-
);
|
1015
|
-
const handleInputChange = (e) => {
|
1016
|
-
setInput(e.target.value);
|
1017
|
-
};
|
1018
|
-
const addToolResult = ({
|
1019
|
-
toolCallId,
|
1020
|
-
result
|
1021
|
-
}) => {
|
1022
|
-
const updatedMessages = messagesRef.current.map(
|
1023
|
-
(message, index, arr) => (
|
1024
|
-
// update the tool calls in the last assistant message:
|
1025
|
-
index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? {
|
1026
|
-
...message,
|
1027
|
-
toolInvocations: message.toolInvocations.map(
|
1028
|
-
(toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation
|
1029
|
-
)
|
1030
|
-
} : message
|
1031
|
-
)
|
1032
|
-
);
|
1033
|
-
mutate(updatedMessages, false);
|
1034
|
-
const lastMessage = updatedMessages[updatedMessages.length - 1];
|
1035
|
-
if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
|
1036
|
-
triggerRequest({ messages: updatedMessages });
|
1037
|
-
}
|
1038
|
-
};
|
1039
|
-
return {
|
1040
|
-
messages: messages || [],
|
1041
|
-
error,
|
1042
|
-
append,
|
1043
|
-
reload,
|
1044
|
-
stop,
|
1045
|
-
setMessages,
|
1046
|
-
input,
|
1047
|
-
setInput,
|
1048
|
-
handleInputChange,
|
1049
|
-
handleSubmit,
|
1050
|
-
isLoading,
|
1051
|
-
data: streamData,
|
1052
|
-
addToolResult,
|
1053
|
-
experimental_addToolResult: addToolResult
|
1054
|
-
};
|
1055
|
-
}
|
1056
|
-
function isAssistantMessageWithCompletedToolCalls(message) {
|
1057
|
-
return message.role === "assistant" && message.toolInvocations && message.toolInvocations.length > 0 && message.toolInvocations.every((toolInvocation) => "result" in toolInvocation);
|
1058
|
-
}
|
1059
|
-
function countTrailingAssistantMessages(messages) {
|
1060
|
-
let count = 0;
|
1061
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
1062
|
-
if (messages[i].role === "assistant") {
|
1063
|
-
count++;
|
1064
|
-
} else {
|
1065
|
-
break;
|
1066
|
-
}
|
1067
|
-
}
|
1068
|
-
return count;
|
1069
|
-
}
|
1070
|
-
|
1071
|
-
// react/use-completion.ts
|
1072
|
-
var import_react2 = require("react");
|
1073
|
-
var import_swr2 = __toESM(require("swr"));
|
1074
|
-
|
1075
|
-
// shared/call-completion-api.ts
|
1076
|
-
async function callCompletionApi({
|
1077
|
-
api,
|
1078
|
-
prompt,
|
1079
|
-
credentials,
|
1080
|
-
headers,
|
1081
|
-
body,
|
1082
|
-
streamMode = "stream-data",
|
1083
|
-
setCompletion,
|
1084
|
-
setLoading,
|
1085
|
-
setError,
|
1086
|
-
setAbortController,
|
1087
|
-
onResponse,
|
1088
|
-
onFinish,
|
1089
|
-
onError,
|
1090
|
-
onData
|
1091
|
-
}) {
|
1092
|
-
try {
|
1093
|
-
setLoading(true);
|
1094
|
-
setError(void 0);
|
1095
|
-
const abortController = new AbortController();
|
1096
|
-
setAbortController(abortController);
|
1097
|
-
setCompletion("");
|
1098
|
-
const res = await fetch(api, {
|
1099
|
-
method: "POST",
|
1100
|
-
body: JSON.stringify({
|
1101
|
-
prompt,
|
1102
|
-
...body
|
1103
|
-
}),
|
1104
|
-
credentials,
|
1105
|
-
headers: {
|
1106
|
-
"Content-Type": "application/json",
|
1107
|
-
...headers
|
1108
|
-
},
|
1109
|
-
signal: abortController.signal
|
1110
|
-
}).catch((err) => {
|
1111
|
-
throw err;
|
1112
|
-
});
|
1113
|
-
if (onResponse) {
|
1114
|
-
try {
|
1115
|
-
await onResponse(res);
|
1116
|
-
} catch (err) {
|
1117
|
-
throw err;
|
1118
|
-
}
|
1119
|
-
}
|
1120
|
-
if (!res.ok) {
|
1121
|
-
throw new Error(
|
1122
|
-
await res.text() || "Failed to fetch the chat response."
|
1123
|
-
);
|
1124
|
-
}
|
1125
|
-
if (!res.body) {
|
1126
|
-
throw new Error("The response body is empty.");
|
1127
|
-
}
|
1128
|
-
let result = "";
|
1129
|
-
const reader = res.body.getReader();
|
1130
|
-
switch (streamMode) {
|
1131
|
-
case "text": {
|
1132
|
-
const decoder = createChunkDecoder();
|
1133
|
-
while (true) {
|
1134
|
-
const { done, value } = await reader.read();
|
1135
|
-
if (done) {
|
1136
|
-
break;
|
1137
|
-
}
|
1138
|
-
result += decoder(value);
|
1139
|
-
setCompletion(result);
|
1140
|
-
if (abortController === null) {
|
1141
|
-
reader.cancel();
|
1142
|
-
break;
|
1143
|
-
}
|
1144
|
-
}
|
1145
|
-
break;
|
1146
|
-
}
|
1147
|
-
case "stream-data": {
|
1148
|
-
for await (const { type, value } of readDataStream(reader, {
|
1149
|
-
isAborted: () => abortController === null
|
1150
|
-
})) {
|
1151
|
-
switch (type) {
|
1152
|
-
case "text": {
|
1153
|
-
result += value;
|
1154
|
-
setCompletion(result);
|
1155
|
-
break;
|
1156
|
-
}
|
1157
|
-
case "data": {
|
1158
|
-
onData == null ? void 0 : onData(value);
|
1159
|
-
break;
|
1160
|
-
}
|
1161
|
-
}
|
1162
|
-
}
|
1163
|
-
break;
|
1164
|
-
}
|
1165
|
-
default: {
|
1166
|
-
const exhaustiveCheck = streamMode;
|
1167
|
-
throw new Error(`Unknown stream mode: ${exhaustiveCheck}`);
|
1168
|
-
}
|
1169
|
-
}
|
1170
|
-
if (onFinish) {
|
1171
|
-
onFinish(prompt, result);
|
1172
|
-
}
|
1173
|
-
setAbortController(null);
|
1174
|
-
return result;
|
1175
|
-
} catch (err) {
|
1176
|
-
if (err.name === "AbortError") {
|
1177
|
-
setAbortController(null);
|
1178
|
-
return null;
|
1179
|
-
}
|
1180
|
-
if (err instanceof Error) {
|
1181
|
-
if (onError) {
|
1182
|
-
onError(err);
|
1183
|
-
}
|
1184
|
-
}
|
1185
|
-
setError(err);
|
1186
|
-
} finally {
|
1187
|
-
setLoading(false);
|
1188
|
-
}
|
1189
|
-
}
|
1190
|
-
|
1191
|
-
// react/use-completion.ts
|
1192
|
-
function useCompletion({
|
1193
|
-
api = "/api/completion",
|
1194
|
-
id,
|
1195
|
-
initialCompletion = "",
|
1196
|
-
initialInput = "",
|
1197
|
-
credentials,
|
1198
|
-
headers,
|
1199
|
-
body,
|
1200
|
-
streamMode,
|
1201
|
-
onResponse,
|
1202
|
-
onFinish,
|
1203
|
-
onError
|
1204
|
-
} = {}) {
|
1205
|
-
const hookId = (0, import_react2.useId)();
|
1206
|
-
const completionId = id || hookId;
|
1207
|
-
const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {
|
1208
|
-
fallbackData: initialCompletion
|
1209
|
-
});
|
1210
|
-
const { data: isLoading = false, mutate: mutateLoading } = (0, import_swr2.default)(
|
1211
|
-
[completionId, "loading"],
|
1212
|
-
null
|
1213
|
-
);
|
1214
|
-
const { data: streamData, mutate: mutateStreamData } = (0, import_swr2.default)([completionId, "streamData"], null);
|
1215
|
-
const [error, setError] = (0, import_react2.useState)(void 0);
|
1216
|
-
const completion = data;
|
1217
|
-
const [abortController, setAbortController] = (0, import_react2.useState)(null);
|
1218
|
-
const extraMetadataRef = (0, import_react2.useRef)({
|
1219
|
-
credentials,
|
1220
|
-
headers,
|
1221
|
-
body
|
1222
|
-
});
|
1223
|
-
(0, import_react2.useEffect)(() => {
|
1224
|
-
extraMetadataRef.current = {
|
1225
|
-
credentials,
|
1226
|
-
headers,
|
1227
|
-
body
|
1228
|
-
};
|
1229
|
-
}, [credentials, headers, body]);
|
1230
|
-
const triggerRequest = (0, import_react2.useCallback)(
|
1231
|
-
async (prompt, options) => callCompletionApi({
|
1232
|
-
api,
|
1233
|
-
prompt,
|
1234
|
-
credentials: extraMetadataRef.current.credentials,
|
1235
|
-
headers: { ...extraMetadataRef.current.headers, ...options == null ? void 0 : options.headers },
|
1236
|
-
body: {
|
1237
|
-
...extraMetadataRef.current.body,
|
1238
|
-
...options == null ? void 0 : options.body
|
1239
|
-
},
|
1240
|
-
streamMode,
|
1241
|
-
setCompletion: (completion2) => mutate(completion2, false),
|
1242
|
-
setLoading: mutateLoading,
|
1243
|
-
setError,
|
1244
|
-
setAbortController,
|
1245
|
-
onResponse,
|
1246
|
-
onFinish,
|
1247
|
-
onError,
|
1248
|
-
onData: (data2) => {
|
1249
|
-
mutateStreamData([...streamData || [], ...data2 || []], false);
|
1250
|
-
}
|
1251
|
-
}),
|
1252
|
-
[
|
1253
|
-
mutate,
|
1254
|
-
mutateLoading,
|
1255
|
-
api,
|
1256
|
-
extraMetadataRef,
|
1257
|
-
setAbortController,
|
1258
|
-
onResponse,
|
1259
|
-
onFinish,
|
1260
|
-
onError,
|
1261
|
-
setError,
|
1262
|
-
streamData,
|
1263
|
-
streamMode,
|
1264
|
-
mutateStreamData
|
1265
|
-
]
|
1266
|
-
);
|
1267
|
-
const stop = (0, import_react2.useCallback)(() => {
|
1268
|
-
if (abortController) {
|
1269
|
-
abortController.abort();
|
1270
|
-
setAbortController(null);
|
1271
|
-
}
|
1272
|
-
}, [abortController]);
|
1273
|
-
const setCompletion = (0, import_react2.useCallback)(
|
1274
|
-
(completion2) => {
|
1275
|
-
mutate(completion2, false);
|
1276
|
-
},
|
1277
|
-
[mutate]
|
1278
|
-
);
|
1279
|
-
const complete = (0, import_react2.useCallback)(
|
1280
|
-
async (prompt, options) => {
|
1281
|
-
return triggerRequest(prompt, options);
|
1282
|
-
},
|
1283
|
-
[triggerRequest]
|
1284
|
-
);
|
1285
|
-
const [input, setInput] = (0, import_react2.useState)(initialInput);
|
1286
|
-
const handleSubmit = (0, import_react2.useCallback)(
|
1287
|
-
(e) => {
|
1288
|
-
e.preventDefault();
|
1289
|
-
if (!input)
|
1290
|
-
return;
|
1291
|
-
return complete(input);
|
1292
|
-
},
|
1293
|
-
[input, complete]
|
1294
|
-
);
|
1295
|
-
const handleInputChange = (e) => {
|
1296
|
-
setInput(e.target.value);
|
1297
|
-
};
|
1298
|
-
return {
|
1299
|
-
completion,
|
1300
|
-
complete,
|
1301
|
-
error,
|
1302
|
-
setCompletion,
|
1303
|
-
stop,
|
1304
|
-
input,
|
1305
|
-
setInput,
|
1306
|
-
handleInputChange,
|
1307
|
-
handleSubmit,
|
1308
|
-
isLoading,
|
1309
|
-
data: streamData
|
1310
|
-
};
|
1311
|
-
}
|
1312
|
-
|
1313
|
-
// react/use-assistant.ts
|
1314
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
1315
|
-
var import_react3 = require("react");
|
1316
|
-
function useAssistant({
|
1317
|
-
api,
|
1318
|
-
threadId: threadIdParam,
|
1319
|
-
credentials,
|
1320
|
-
headers,
|
1321
|
-
body,
|
1322
|
-
onError
|
1323
|
-
}) {
|
1324
|
-
const [messages, setMessages] = (0, import_react3.useState)([]);
|
1325
|
-
const [input, setInput] = (0, import_react3.useState)("");
|
1326
|
-
const [threadId, setThreadId] = (0, import_react3.useState)(void 0);
|
1327
|
-
const [status, setStatus] = (0, import_react3.useState)("awaiting_message");
|
1328
|
-
const [error, setError] = (0, import_react3.useState)(void 0);
|
1329
|
-
const handleInputChange = (event) => {
|
1330
|
-
setInput(event.target.value);
|
1331
|
-
};
|
1332
|
-
const abortControllerRef = (0, import_react3.useRef)(null);
|
1333
|
-
const stop = (0, import_react3.useCallback)(() => {
|
1334
|
-
if (abortControllerRef.current) {
|
1335
|
-
abortControllerRef.current.abort();
|
1336
|
-
abortControllerRef.current = null;
|
1337
|
-
}
|
1338
|
-
}, []);
|
1339
|
-
const append = async (message, requestOptions) => {
|
1340
|
-
var _a;
|
1341
|
-
setStatus("in_progress");
|
1342
|
-
setMessages((messages2) => {
|
1343
|
-
var _a2;
|
1344
|
-
return [
|
1345
|
-
...messages2,
|
1346
|
-
{
|
1347
|
-
...message,
|
1348
|
-
id: (_a2 = message.id) != null ? _a2 : generateId()
|
1349
|
-
}
|
1350
|
-
];
|
1351
|
-
});
|
1352
|
-
setInput("");
|
1353
|
-
const abortController = new AbortController();
|
1354
|
-
try {
|
1355
|
-
abortControllerRef.current = abortController;
|
1356
|
-
const result = await fetch(api, {
|
1357
|
-
method: "POST",
|
1358
|
-
credentials,
|
1359
|
-
signal: abortController.signal,
|
1360
|
-
headers: { "Content-Type": "application/json", ...headers },
|
1361
|
-
body: JSON.stringify({
|
1362
|
-
...body,
|
1363
|
-
// always use user-provided threadId when available:
|
1364
|
-
threadId: (_a = threadIdParam != null ? threadIdParam : threadId) != null ? _a : null,
|
1365
|
-
message: message.content,
|
1366
|
-
// optional request data:
|
1367
|
-
data: requestOptions == null ? void 0 : requestOptions.data
|
1368
|
-
})
|
1369
|
-
});
|
1370
|
-
if (result.body == null) {
|
1371
|
-
throw new Error("The response body is empty.");
|
1372
|
-
}
|
1373
|
-
for await (const { type, value } of readDataStream(
|
1374
|
-
result.body.getReader()
|
1375
|
-
)) {
|
1376
|
-
switch (type) {
|
1377
|
-
case "assistant_message": {
|
1378
|
-
setMessages((messages2) => [
|
1379
|
-
...messages2,
|
1380
|
-
{
|
1381
|
-
id: value.id,
|
1382
|
-
role: value.role,
|
1383
|
-
content: value.content[0].text.value
|
1384
|
-
}
|
1385
|
-
]);
|
1386
|
-
break;
|
1387
|
-
}
|
1388
|
-
case "text": {
|
1389
|
-
setMessages((messages2) => {
|
1390
|
-
const lastMessage = messages2[messages2.length - 1];
|
1391
|
-
return [
|
1392
|
-
...messages2.slice(0, messages2.length - 1),
|
1393
|
-
{
|
1394
|
-
id: lastMessage.id,
|
1395
|
-
role: lastMessage.role,
|
1396
|
-
content: lastMessage.content + value
|
1397
|
-
}
|
1398
|
-
];
|
1399
|
-
});
|
1400
|
-
break;
|
1401
|
-
}
|
1402
|
-
case "data_message": {
|
1403
|
-
setMessages((messages2) => {
|
1404
|
-
var _a2;
|
1405
|
-
return [
|
1406
|
-
...messages2,
|
1407
|
-
{
|
1408
|
-
id: (_a2 = value.id) != null ? _a2 : generateId(),
|
1409
|
-
role: "data",
|
1410
|
-
content: "",
|
1411
|
-
data: value.data
|
1412
|
-
}
|
1413
|
-
];
|
1414
|
-
});
|
1415
|
-
break;
|
1416
|
-
}
|
1417
|
-
case "assistant_control_data": {
|
1418
|
-
setThreadId(value.threadId);
|
1419
|
-
setMessages((messages2) => {
|
1420
|
-
const lastMessage = messages2[messages2.length - 1];
|
1421
|
-
lastMessage.id = value.messageId;
|
1422
|
-
return [...messages2.slice(0, messages2.length - 1), lastMessage];
|
1423
|
-
});
|
1424
|
-
break;
|
1425
|
-
}
|
1426
|
-
case "error": {
|
1427
|
-
setError(new Error(value));
|
1428
|
-
break;
|
1429
|
-
}
|
1430
|
-
}
|
1431
|
-
}
|
1432
|
-
} catch (error2) {
|
1433
|
-
if ((0, import_provider_utils.isAbortError)(error2) && abortController.signal.aborted) {
|
1434
|
-
abortControllerRef.current = null;
|
1435
|
-
return;
|
1436
|
-
}
|
1437
|
-
if (onError && error2 instanceof Error) {
|
1438
|
-
onError(error2);
|
1439
|
-
}
|
1440
|
-
setError(error2);
|
1441
|
-
} finally {
|
1442
|
-
abortControllerRef.current = null;
|
1443
|
-
setStatus("awaiting_message");
|
1444
|
-
}
|
1445
|
-
};
|
1446
|
-
const submitMessage = async (event, requestOptions) => {
|
1447
|
-
var _a;
|
1448
|
-
(_a = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a.call(event);
|
1449
|
-
if (input === "") {
|
1450
|
-
return;
|
1451
|
-
}
|
1452
|
-
append({ role: "user", content: input }, requestOptions);
|
1453
|
-
};
|
1454
|
-
return {
|
1455
|
-
append,
|
1456
|
-
messages,
|
1457
|
-
setMessages,
|
1458
|
-
threadId,
|
1459
|
-
input,
|
1460
|
-
setInput,
|
1461
|
-
handleInputChange,
|
1462
|
-
submitMessage,
|
1463
|
-
status,
|
1464
|
-
error,
|
1465
|
-
stop
|
1466
|
-
};
|
1467
|
-
}
|
1468
|
-
var experimental_useAssistant = useAssistant;
|
29
|
+
var import_react = require("@ai-sdk/react");
|
30
|
+
var useChat = import_react.useChat;
|
31
|
+
var useCompletion = import_react.useCompletion;
|
32
|
+
var useAssistant = import_react.useAssistant;
|
1469
33
|
// Annotate the CommonJS export names for ESM import in node:
|
1470
34
|
0 && (module.exports = {
|
1471
|
-
experimental_useAssistant,
|
1472
35
|
useAssistant,
|
1473
36
|
useChat,
|
1474
37
|
useCompletion
|