@assistant-ui/react 0.5.19 → 0.5.21
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/AssistantTypes-D93BmqD5.d.mts +160 -0
- package/dist/AssistantTypes-D93BmqD5.d.ts +160 -0
- package/dist/Thread-BbLf1cc4.d.mts +156 -0
- package/dist/Thread-jfAlPLli.d.ts +156 -0
- package/dist/chunk-2RKUKZSZ.mjs +761 -0
- package/dist/chunk-2RKUKZSZ.mjs.map +1 -0
- package/dist/chunk-BJPOCE4O.mjs +11 -0
- package/dist/chunk-BJPOCE4O.mjs.map +1 -0
- package/dist/chunk-QBS6JLLN.mjs +63 -0
- package/dist/chunk-QBS6JLLN.mjs.map +1 -0
- package/dist/chunk-V66MVXBH.mjs +608 -0
- package/dist/chunk-V66MVXBH.mjs.map +1 -0
- package/dist/edge.d.mts +5 -90
- package/dist/edge.d.ts +5 -90
- package/dist/edge.js +1 -0
- package/dist/edge.js.map +1 -1
- package/dist/edge.mjs +49 -799
- package/dist/edge.mjs.map +1 -1
- package/dist/index.d.mts +17 -293
- package/dist/index.d.ts +17 -293
- package/dist/index.js +341 -283
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +313 -1601
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.mts +9 -0
- package/dist/internal.d.ts +9 -0
- package/dist/internal.js +620 -0
- package/dist/internal.js.map +1 -0
- package/dist/internal.mjs +24 -0
- package/dist/internal.mjs.map +1 -0
- package/dist/styles/index.css +3 -3
- package/dist/styles/index.css.map +1 -1
- package/dist/styles/tailwindcss/thread.css +1 -1
- package/dist/tailwindcss/index.js +1 -0
- package/dist/tailwindcss/index.js.map +1 -1
- package/dist/tailwindcss/index.mjs +3 -0
- package/dist/tailwindcss/index.mjs.map +1 -1
- package/internal/README.md +1 -0
- package/internal/package.json +5 -0
- package/package.json +12 -1
@@ -0,0 +1,761 @@
|
|
1
|
+
"use client";
|
2
|
+
|
3
|
+
// src/runtimes/edge/converters/toLanguageModelMessages.ts
|
4
|
+
var assistantMessageSplitter = () => {
|
5
|
+
const stash = [];
|
6
|
+
let assistantMessage = {
|
7
|
+
role: "assistant",
|
8
|
+
content: []
|
9
|
+
};
|
10
|
+
let toolMessage = {
|
11
|
+
role: "tool",
|
12
|
+
content: []
|
13
|
+
};
|
14
|
+
return {
|
15
|
+
addTextContentPart: (part) => {
|
16
|
+
if (toolMessage.content.length > 0) {
|
17
|
+
stash.push(assistantMessage);
|
18
|
+
stash.push(toolMessage);
|
19
|
+
assistantMessage = {
|
20
|
+
role: "assistant",
|
21
|
+
content: []
|
22
|
+
};
|
23
|
+
toolMessage = {
|
24
|
+
role: "tool",
|
25
|
+
content: []
|
26
|
+
};
|
27
|
+
}
|
28
|
+
assistantMessage.content.push(part);
|
29
|
+
},
|
30
|
+
addToolCallPart: (part) => {
|
31
|
+
assistantMessage.content.push({
|
32
|
+
type: "tool-call",
|
33
|
+
toolCallId: part.toolCallId,
|
34
|
+
toolName: part.toolName,
|
35
|
+
args: part.args
|
36
|
+
});
|
37
|
+
if (part.result) {
|
38
|
+
toolMessage.content.push({
|
39
|
+
type: "tool-result",
|
40
|
+
toolCallId: part.toolCallId,
|
41
|
+
toolName: part.toolName,
|
42
|
+
result: part.result
|
43
|
+
// isError
|
44
|
+
});
|
45
|
+
}
|
46
|
+
},
|
47
|
+
getMessages: () => {
|
48
|
+
if (toolMessage.content.length > 0) {
|
49
|
+
return [...stash, assistantMessage, toolMessage];
|
50
|
+
}
|
51
|
+
return [...stash, assistantMessage];
|
52
|
+
}
|
53
|
+
};
|
54
|
+
};
|
55
|
+
function toLanguageModelMessages(message) {
|
56
|
+
return message.flatMap((message2) => {
|
57
|
+
const role = message2.role;
|
58
|
+
switch (role) {
|
59
|
+
case "system": {
|
60
|
+
return [{ role: "system", content: message2.content[0].text }];
|
61
|
+
}
|
62
|
+
case "user": {
|
63
|
+
const msg = {
|
64
|
+
role: "user",
|
65
|
+
content: message2.content.map(
|
66
|
+
(part) => {
|
67
|
+
const type = part.type;
|
68
|
+
switch (type) {
|
69
|
+
case "text": {
|
70
|
+
return part;
|
71
|
+
}
|
72
|
+
case "image": {
|
73
|
+
return {
|
74
|
+
type: "image",
|
75
|
+
image: new URL(part.image)
|
76
|
+
};
|
77
|
+
}
|
78
|
+
default: {
|
79
|
+
const unhandledType = type;
|
80
|
+
throw new Error(
|
81
|
+
`Unspported content part type: ${unhandledType}`
|
82
|
+
);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
)
|
87
|
+
};
|
88
|
+
return [msg];
|
89
|
+
}
|
90
|
+
case "assistant": {
|
91
|
+
const splitter = assistantMessageSplitter();
|
92
|
+
for (const part of message2.content) {
|
93
|
+
const type = part.type;
|
94
|
+
switch (type) {
|
95
|
+
case "text": {
|
96
|
+
splitter.addTextContentPart(part);
|
97
|
+
break;
|
98
|
+
}
|
99
|
+
case "tool-call": {
|
100
|
+
splitter.addToolCallPart(part);
|
101
|
+
break;
|
102
|
+
}
|
103
|
+
default: {
|
104
|
+
const unhandledType = type;
|
105
|
+
throw new Error(`Unhandled content part type: ${unhandledType}`);
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
return splitter.getMessages();
|
110
|
+
}
|
111
|
+
default: {
|
112
|
+
const unhandledRole = role;
|
113
|
+
throw new Error(`Unknown message role: ${unhandledRole}`);
|
114
|
+
}
|
115
|
+
}
|
116
|
+
});
|
117
|
+
}
|
118
|
+
|
119
|
+
// src/runtimes/edge/converters/toCoreMessages.ts
|
120
|
+
var toCoreMessages = (message) => {
|
121
|
+
return message.map(toCoreMessage);
|
122
|
+
};
|
123
|
+
var toCoreMessage = (message) => {
|
124
|
+
const role = message.role;
|
125
|
+
switch (role) {
|
126
|
+
case "assistant":
|
127
|
+
return {
|
128
|
+
role,
|
129
|
+
content: message.content.map((part) => {
|
130
|
+
if (part.type === "ui") throw new Error("UI parts are not supported");
|
131
|
+
if (part.type === "tool-call") {
|
132
|
+
const { argsText, ...rest } = part;
|
133
|
+
return rest;
|
134
|
+
}
|
135
|
+
return part;
|
136
|
+
})
|
137
|
+
};
|
138
|
+
case "user":
|
139
|
+
return {
|
140
|
+
role,
|
141
|
+
content: message.content.map((part) => {
|
142
|
+
if (part.type === "ui") throw new Error("UI parts are not supported");
|
143
|
+
return part;
|
144
|
+
})
|
145
|
+
};
|
146
|
+
case "system":
|
147
|
+
return {
|
148
|
+
role,
|
149
|
+
content: message.content
|
150
|
+
};
|
151
|
+
default: {
|
152
|
+
const unsupportedRole = role;
|
153
|
+
throw new Error(`Unknown message role: ${unsupportedRole}`);
|
154
|
+
}
|
155
|
+
}
|
156
|
+
};
|
157
|
+
|
158
|
+
// src/runtimes/edge/converters/toLanguageModelTools.ts
|
159
|
+
import { z } from "zod";
|
160
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
161
|
+
var toLanguageModelTools = (tools) => {
|
162
|
+
return Object.entries(tools).map(([name, tool]) => ({
|
163
|
+
type: "function",
|
164
|
+
name,
|
165
|
+
...tool.description ? { description: tool.description } : void 0,
|
166
|
+
parameters: tool.parameters instanceof z.ZodType ? zodToJsonSchema(tool.parameters) : tool.parameters
|
167
|
+
}));
|
168
|
+
};
|
169
|
+
|
170
|
+
// src/runtimes/edge/partial-json/parse-partial-json.ts
|
171
|
+
import sjson from "secure-json-parse";
|
172
|
+
|
173
|
+
// src/runtimes/edge/partial-json/fix-json.ts
|
174
|
+
function fixJson(input) {
|
175
|
+
const stack = ["ROOT"];
|
176
|
+
let lastValidIndex = -1;
|
177
|
+
let literalStart = null;
|
178
|
+
function processValueStart(char, i, swapState) {
|
179
|
+
{
|
180
|
+
switch (char) {
|
181
|
+
case '"': {
|
182
|
+
lastValidIndex = i;
|
183
|
+
stack.pop();
|
184
|
+
stack.push(swapState);
|
185
|
+
stack.push("INSIDE_STRING");
|
186
|
+
break;
|
187
|
+
}
|
188
|
+
case "f":
|
189
|
+
case "t":
|
190
|
+
case "n": {
|
191
|
+
lastValidIndex = i;
|
192
|
+
literalStart = i;
|
193
|
+
stack.pop();
|
194
|
+
stack.push(swapState);
|
195
|
+
stack.push("INSIDE_LITERAL");
|
196
|
+
break;
|
197
|
+
}
|
198
|
+
case "-": {
|
199
|
+
stack.pop();
|
200
|
+
stack.push(swapState);
|
201
|
+
stack.push("INSIDE_NUMBER");
|
202
|
+
break;
|
203
|
+
}
|
204
|
+
case "0":
|
205
|
+
case "1":
|
206
|
+
case "2":
|
207
|
+
case "3":
|
208
|
+
case "4":
|
209
|
+
case "5":
|
210
|
+
case "6":
|
211
|
+
case "7":
|
212
|
+
case "8":
|
213
|
+
case "9": {
|
214
|
+
lastValidIndex = i;
|
215
|
+
stack.pop();
|
216
|
+
stack.push(swapState);
|
217
|
+
stack.push("INSIDE_NUMBER");
|
218
|
+
break;
|
219
|
+
}
|
220
|
+
case "{": {
|
221
|
+
lastValidIndex = i;
|
222
|
+
stack.pop();
|
223
|
+
stack.push(swapState);
|
224
|
+
stack.push("INSIDE_OBJECT_START");
|
225
|
+
break;
|
226
|
+
}
|
227
|
+
case "[": {
|
228
|
+
lastValidIndex = i;
|
229
|
+
stack.pop();
|
230
|
+
stack.push(swapState);
|
231
|
+
stack.push("INSIDE_ARRAY_START");
|
232
|
+
break;
|
233
|
+
}
|
234
|
+
}
|
235
|
+
}
|
236
|
+
}
|
237
|
+
function processAfterObjectValue(char, i) {
|
238
|
+
switch (char) {
|
239
|
+
case ",": {
|
240
|
+
stack.pop();
|
241
|
+
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
242
|
+
break;
|
243
|
+
}
|
244
|
+
case "}": {
|
245
|
+
lastValidIndex = i;
|
246
|
+
stack.pop();
|
247
|
+
break;
|
248
|
+
}
|
249
|
+
}
|
250
|
+
}
|
251
|
+
function processAfterArrayValue(char, i) {
|
252
|
+
switch (char) {
|
253
|
+
case ",": {
|
254
|
+
stack.pop();
|
255
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
256
|
+
break;
|
257
|
+
}
|
258
|
+
case "]": {
|
259
|
+
lastValidIndex = i;
|
260
|
+
stack.pop();
|
261
|
+
break;
|
262
|
+
}
|
263
|
+
}
|
264
|
+
}
|
265
|
+
for (let i = 0; i < input.length; i++) {
|
266
|
+
const char = input[i];
|
267
|
+
const currentState = stack[stack.length - 1];
|
268
|
+
switch (currentState) {
|
269
|
+
case "ROOT":
|
270
|
+
processValueStart(char, i, "FINISH");
|
271
|
+
break;
|
272
|
+
case "INSIDE_OBJECT_START": {
|
273
|
+
switch (char) {
|
274
|
+
case '"': {
|
275
|
+
stack.pop();
|
276
|
+
stack.push("INSIDE_OBJECT_KEY");
|
277
|
+
break;
|
278
|
+
}
|
279
|
+
case "}": {
|
280
|
+
lastValidIndex = i;
|
281
|
+
stack.pop();
|
282
|
+
break;
|
283
|
+
}
|
284
|
+
}
|
285
|
+
break;
|
286
|
+
}
|
287
|
+
case "INSIDE_OBJECT_AFTER_COMMA": {
|
288
|
+
switch (char) {
|
289
|
+
case '"': {
|
290
|
+
stack.pop();
|
291
|
+
stack.push("INSIDE_OBJECT_KEY");
|
292
|
+
break;
|
293
|
+
}
|
294
|
+
}
|
295
|
+
break;
|
296
|
+
}
|
297
|
+
case "INSIDE_OBJECT_KEY": {
|
298
|
+
switch (char) {
|
299
|
+
case '"': {
|
300
|
+
stack.pop();
|
301
|
+
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
302
|
+
break;
|
303
|
+
}
|
304
|
+
}
|
305
|
+
break;
|
306
|
+
}
|
307
|
+
case "INSIDE_OBJECT_AFTER_KEY": {
|
308
|
+
switch (char) {
|
309
|
+
case ":": {
|
310
|
+
stack.pop();
|
311
|
+
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
312
|
+
break;
|
313
|
+
}
|
314
|
+
}
|
315
|
+
break;
|
316
|
+
}
|
317
|
+
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
318
|
+
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
319
|
+
break;
|
320
|
+
}
|
321
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
322
|
+
processAfterObjectValue(char, i);
|
323
|
+
break;
|
324
|
+
}
|
325
|
+
case "INSIDE_STRING": {
|
326
|
+
switch (char) {
|
327
|
+
case '"': {
|
328
|
+
stack.pop();
|
329
|
+
lastValidIndex = i;
|
330
|
+
break;
|
331
|
+
}
|
332
|
+
case "\\": {
|
333
|
+
stack.push("INSIDE_STRING_ESCAPE");
|
334
|
+
break;
|
335
|
+
}
|
336
|
+
default: {
|
337
|
+
lastValidIndex = i;
|
338
|
+
}
|
339
|
+
}
|
340
|
+
break;
|
341
|
+
}
|
342
|
+
case "INSIDE_ARRAY_START": {
|
343
|
+
switch (char) {
|
344
|
+
case "]": {
|
345
|
+
lastValidIndex = i;
|
346
|
+
stack.pop();
|
347
|
+
break;
|
348
|
+
}
|
349
|
+
default: {
|
350
|
+
lastValidIndex = i;
|
351
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
352
|
+
break;
|
353
|
+
}
|
354
|
+
}
|
355
|
+
break;
|
356
|
+
}
|
357
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
358
|
+
switch (char) {
|
359
|
+
case ",": {
|
360
|
+
stack.pop();
|
361
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
362
|
+
break;
|
363
|
+
}
|
364
|
+
case "]": {
|
365
|
+
lastValidIndex = i;
|
366
|
+
stack.pop();
|
367
|
+
break;
|
368
|
+
}
|
369
|
+
default: {
|
370
|
+
lastValidIndex = i;
|
371
|
+
break;
|
372
|
+
}
|
373
|
+
}
|
374
|
+
break;
|
375
|
+
}
|
376
|
+
case "INSIDE_ARRAY_AFTER_COMMA": {
|
377
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
378
|
+
break;
|
379
|
+
}
|
380
|
+
case "INSIDE_STRING_ESCAPE": {
|
381
|
+
stack.pop();
|
382
|
+
lastValidIndex = i;
|
383
|
+
break;
|
384
|
+
}
|
385
|
+
case "INSIDE_NUMBER": {
|
386
|
+
switch (char) {
|
387
|
+
case "0":
|
388
|
+
case "1":
|
389
|
+
case "2":
|
390
|
+
case "3":
|
391
|
+
case "4":
|
392
|
+
case "5":
|
393
|
+
case "6":
|
394
|
+
case "7":
|
395
|
+
case "8":
|
396
|
+
case "9": {
|
397
|
+
lastValidIndex = i;
|
398
|
+
break;
|
399
|
+
}
|
400
|
+
case "e":
|
401
|
+
case "E":
|
402
|
+
case "-":
|
403
|
+
case ".": {
|
404
|
+
break;
|
405
|
+
}
|
406
|
+
case ",": {
|
407
|
+
stack.pop();
|
408
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
409
|
+
processAfterArrayValue(char, i);
|
410
|
+
}
|
411
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
412
|
+
processAfterObjectValue(char, i);
|
413
|
+
}
|
414
|
+
break;
|
415
|
+
}
|
416
|
+
case "}": {
|
417
|
+
stack.pop();
|
418
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
419
|
+
processAfterObjectValue(char, i);
|
420
|
+
}
|
421
|
+
break;
|
422
|
+
}
|
423
|
+
case "]": {
|
424
|
+
stack.pop();
|
425
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
426
|
+
processAfterArrayValue(char, i);
|
427
|
+
}
|
428
|
+
break;
|
429
|
+
}
|
430
|
+
default: {
|
431
|
+
stack.pop();
|
432
|
+
break;
|
433
|
+
}
|
434
|
+
}
|
435
|
+
break;
|
436
|
+
}
|
437
|
+
case "INSIDE_LITERAL": {
|
438
|
+
const partialLiteral = input.substring(literalStart, i + 1);
|
439
|
+
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
440
|
+
stack.pop();
|
441
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
442
|
+
processAfterObjectValue(char, i);
|
443
|
+
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
444
|
+
processAfterArrayValue(char, i);
|
445
|
+
}
|
446
|
+
} else {
|
447
|
+
lastValidIndex = i;
|
448
|
+
}
|
449
|
+
break;
|
450
|
+
}
|
451
|
+
}
|
452
|
+
}
|
453
|
+
let result = input.slice(0, lastValidIndex + 1);
|
454
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
455
|
+
const state = stack[i];
|
456
|
+
switch (state) {
|
457
|
+
case "INSIDE_STRING": {
|
458
|
+
result += '"';
|
459
|
+
break;
|
460
|
+
}
|
461
|
+
case "INSIDE_OBJECT_KEY":
|
462
|
+
case "INSIDE_OBJECT_AFTER_KEY":
|
463
|
+
case "INSIDE_OBJECT_AFTER_COMMA":
|
464
|
+
case "INSIDE_OBJECT_START":
|
465
|
+
case "INSIDE_OBJECT_BEFORE_VALUE":
|
466
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
467
|
+
result += "}";
|
468
|
+
break;
|
469
|
+
}
|
470
|
+
case "INSIDE_ARRAY_START":
|
471
|
+
case "INSIDE_ARRAY_AFTER_COMMA":
|
472
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
473
|
+
result += "]";
|
474
|
+
break;
|
475
|
+
}
|
476
|
+
case "INSIDE_LITERAL": {
|
477
|
+
const partialLiteral = input.substring(literalStart, input.length);
|
478
|
+
if ("true".startsWith(partialLiteral)) {
|
479
|
+
result += "true".slice(partialLiteral.length);
|
480
|
+
} else if ("false".startsWith(partialLiteral)) {
|
481
|
+
result += "false".slice(partialLiteral.length);
|
482
|
+
} else if ("null".startsWith(partialLiteral)) {
|
483
|
+
result += "null".slice(partialLiteral.length);
|
484
|
+
}
|
485
|
+
}
|
486
|
+
}
|
487
|
+
}
|
488
|
+
return result;
|
489
|
+
}
|
490
|
+
|
491
|
+
// src/runtimes/edge/partial-json/parse-partial-json.ts
|
492
|
+
var parsePartialJson = (json) => {
|
493
|
+
try {
|
494
|
+
return sjson.parse(json);
|
495
|
+
} catch {
|
496
|
+
try {
|
497
|
+
return sjson.parse(fixJson(json));
|
498
|
+
} catch {
|
499
|
+
return void 0;
|
500
|
+
}
|
501
|
+
}
|
502
|
+
};
|
503
|
+
|
504
|
+
// src/runtimes/edge/streams/runResultStream.ts
|
505
|
+
function runResultStream() {
|
506
|
+
let message = {
|
507
|
+
content: [],
|
508
|
+
status: { type: "running" }
|
509
|
+
};
|
510
|
+
const currentToolCall = { toolCallId: "", argsText: "" };
|
511
|
+
return new TransformStream({
|
512
|
+
transform(chunk, controller) {
|
513
|
+
const chunkType = chunk.type;
|
514
|
+
switch (chunkType) {
|
515
|
+
case "text-delta": {
|
516
|
+
message = appendOrUpdateText(message, chunk.textDelta);
|
517
|
+
controller.enqueue(message);
|
518
|
+
break;
|
519
|
+
}
|
520
|
+
case "tool-call-delta": {
|
521
|
+
const { toolCallId, toolName, argsTextDelta } = chunk;
|
522
|
+
if (currentToolCall.toolCallId !== toolCallId) {
|
523
|
+
currentToolCall.toolCallId = toolCallId;
|
524
|
+
currentToolCall.argsText = argsTextDelta;
|
525
|
+
} else {
|
526
|
+
currentToolCall.argsText += argsTextDelta;
|
527
|
+
}
|
528
|
+
message = appendOrUpdateToolCall(
|
529
|
+
message,
|
530
|
+
toolCallId,
|
531
|
+
toolName,
|
532
|
+
currentToolCall.argsText
|
533
|
+
);
|
534
|
+
controller.enqueue(message);
|
535
|
+
break;
|
536
|
+
}
|
537
|
+
case "tool-call": {
|
538
|
+
break;
|
539
|
+
}
|
540
|
+
case "tool-result": {
|
541
|
+
message = appendOrUpdateToolResult(
|
542
|
+
message,
|
543
|
+
chunk.toolCallId,
|
544
|
+
chunk.toolName,
|
545
|
+
chunk.result
|
546
|
+
);
|
547
|
+
controller.enqueue(message);
|
548
|
+
break;
|
549
|
+
}
|
550
|
+
case "finish": {
|
551
|
+
message = appendOrUpdateFinish(message, chunk);
|
552
|
+
controller.enqueue(message);
|
553
|
+
break;
|
554
|
+
}
|
555
|
+
case "error": {
|
556
|
+
if (chunk.error instanceof Error && chunk.error.name === "AbortError") {
|
557
|
+
message = appendOrUpdateCancel(message);
|
558
|
+
controller.enqueue(message);
|
559
|
+
break;
|
560
|
+
} else {
|
561
|
+
throw chunk.error;
|
562
|
+
}
|
563
|
+
}
|
564
|
+
default: {
|
565
|
+
const unhandledType = chunkType;
|
566
|
+
throw new Error(`Unhandled chunk type: ${unhandledType}`);
|
567
|
+
}
|
568
|
+
}
|
569
|
+
}
|
570
|
+
});
|
571
|
+
}
|
572
|
+
var appendOrUpdateText = (message, textDelta) => {
|
573
|
+
let contentParts = message.content;
|
574
|
+
let contentPart = message.content.at(-1);
|
575
|
+
if (contentPart?.type !== "text") {
|
576
|
+
contentPart = { type: "text", text: textDelta };
|
577
|
+
} else {
|
578
|
+
contentParts = contentParts.slice(0, -1);
|
579
|
+
contentPart = { type: "text", text: contentPart.text + textDelta };
|
580
|
+
}
|
581
|
+
return {
|
582
|
+
...message,
|
583
|
+
content: contentParts.concat([contentPart])
|
584
|
+
};
|
585
|
+
};
|
586
|
+
var appendOrUpdateToolCall = (message, toolCallId, toolName, argsText) => {
|
587
|
+
let contentParts = message.content;
|
588
|
+
let contentPart = message.content.at(-1);
|
589
|
+
if (contentPart?.type !== "tool-call" || contentPart.toolCallId !== toolCallId) {
|
590
|
+
contentPart = {
|
591
|
+
type: "tool-call",
|
592
|
+
toolCallId,
|
593
|
+
toolName,
|
594
|
+
argsText,
|
595
|
+
args: parsePartialJson(argsText)
|
596
|
+
};
|
597
|
+
} else {
|
598
|
+
contentParts = contentParts.slice(0, -1);
|
599
|
+
contentPart = {
|
600
|
+
...contentPart,
|
601
|
+
argsText,
|
602
|
+
args: parsePartialJson(argsText)
|
603
|
+
};
|
604
|
+
}
|
605
|
+
return {
|
606
|
+
...message,
|
607
|
+
content: contentParts.concat([contentPart])
|
608
|
+
};
|
609
|
+
};
|
610
|
+
var appendOrUpdateToolResult = (message, toolCallId, toolName, result) => {
|
611
|
+
let found = false;
|
612
|
+
const newContentParts = message.content.map((part) => {
|
613
|
+
if (part.type !== "tool-call" || part.toolCallId !== toolCallId)
|
614
|
+
return part;
|
615
|
+
found = true;
|
616
|
+
if (part.toolName !== toolName)
|
617
|
+
throw new Error(
|
618
|
+
`Tool call ${toolCallId} found with tool name ${part.toolName}, but expected ${toolName}`
|
619
|
+
);
|
620
|
+
return {
|
621
|
+
...part,
|
622
|
+
result
|
623
|
+
};
|
624
|
+
});
|
625
|
+
if (!found)
|
626
|
+
throw new Error(
|
627
|
+
`Received tool result for unknown tool call "${toolName}" / "${toolCallId}". This is likely an internal bug in assistant-ui.`
|
628
|
+
);
|
629
|
+
return {
|
630
|
+
...message,
|
631
|
+
content: newContentParts
|
632
|
+
};
|
633
|
+
};
|
634
|
+
var appendOrUpdateFinish = (message, chunk) => {
|
635
|
+
const { type, ...rest } = chunk;
|
636
|
+
return {
|
637
|
+
...message,
|
638
|
+
status: getStatus(chunk),
|
639
|
+
roundtrips: [
|
640
|
+
...message.roundtrips ?? [],
|
641
|
+
{
|
642
|
+
logprobs: rest.logprobs,
|
643
|
+
usage: rest.usage
|
644
|
+
}
|
645
|
+
]
|
646
|
+
};
|
647
|
+
};
|
648
|
+
var getStatus = (chunk) => {
|
649
|
+
if (chunk.finishReason === "tool-calls") {
|
650
|
+
return {
|
651
|
+
type: "requires-action",
|
652
|
+
reason: "tool-calls"
|
653
|
+
};
|
654
|
+
} else if (chunk.finishReason === "stop" || chunk.finishReason === "unknown") {
|
655
|
+
return {
|
656
|
+
type: "complete",
|
657
|
+
reason: chunk.finishReason
|
658
|
+
};
|
659
|
+
} else {
|
660
|
+
return {
|
661
|
+
type: "incomplete",
|
662
|
+
reason: chunk.finishReason
|
663
|
+
};
|
664
|
+
}
|
665
|
+
};
|
666
|
+
var appendOrUpdateCancel = (message) => {
|
667
|
+
return {
|
668
|
+
...message,
|
669
|
+
status: {
|
670
|
+
type: "incomplete",
|
671
|
+
reason: "cancelled"
|
672
|
+
}
|
673
|
+
};
|
674
|
+
};
|
675
|
+
|
676
|
+
// src/runtimes/edge/streams/toolResultStream.ts
|
677
|
+
import { z as z2 } from "zod";
|
678
|
+
import sjson2 from "secure-json-parse";
|
679
|
+
function toolResultStream(tools) {
|
680
|
+
const toolCallExecutions = /* @__PURE__ */ new Map();
|
681
|
+
return new TransformStream({
|
682
|
+
transform(chunk, controller) {
|
683
|
+
controller.enqueue(chunk);
|
684
|
+
const chunkType = chunk.type;
|
685
|
+
switch (chunkType) {
|
686
|
+
case "tool-call": {
|
687
|
+
const { toolCallId, toolCallType, toolName, args: argsText } = chunk;
|
688
|
+
const tool = tools?.[toolName];
|
689
|
+
if (!tool || !tool.execute) return;
|
690
|
+
const args = sjson2.parse(argsText);
|
691
|
+
if (tool.parameters instanceof z2.ZodType) {
|
692
|
+
const result = tool.parameters.safeParse(args);
|
693
|
+
if (!result.success) {
|
694
|
+
controller.enqueue({
|
695
|
+
type: "tool-result",
|
696
|
+
toolCallType,
|
697
|
+
toolCallId,
|
698
|
+
toolName,
|
699
|
+
result: "Function parameter validation failed. " + JSON.stringify(result.error.issues),
|
700
|
+
isError: true
|
701
|
+
});
|
702
|
+
return;
|
703
|
+
} else {
|
704
|
+
toolCallExecutions.set(
|
705
|
+
toolCallId,
|
706
|
+
(async () => {
|
707
|
+
try {
|
708
|
+
const result2 = await tool.execute(args);
|
709
|
+
controller.enqueue({
|
710
|
+
type: "tool-result",
|
711
|
+
toolCallType,
|
712
|
+
toolCallId,
|
713
|
+
toolName,
|
714
|
+
result: result2
|
715
|
+
});
|
716
|
+
} catch (error) {
|
717
|
+
console.error("Error: ", error);
|
718
|
+
controller.enqueue({
|
719
|
+
type: "tool-result",
|
720
|
+
toolCallType,
|
721
|
+
toolCallId,
|
722
|
+
toolName,
|
723
|
+
result: "Error: " + error,
|
724
|
+
isError: true
|
725
|
+
});
|
726
|
+
} finally {
|
727
|
+
toolCallExecutions.delete(toolCallId);
|
728
|
+
}
|
729
|
+
})()
|
730
|
+
);
|
731
|
+
}
|
732
|
+
}
|
733
|
+
break;
|
734
|
+
}
|
735
|
+
case "text-delta":
|
736
|
+
case "tool-call-delta":
|
737
|
+
case "tool-result":
|
738
|
+
case "finish":
|
739
|
+
case "error":
|
740
|
+
break;
|
741
|
+
default: {
|
742
|
+
const unhandledType = chunkType;
|
743
|
+
throw new Error(`Unhandled chunk type: ${unhandledType}`);
|
744
|
+
}
|
745
|
+
}
|
746
|
+
},
|
747
|
+
async flush() {
|
748
|
+
await Promise.all(toolCallExecutions.values());
|
749
|
+
}
|
750
|
+
});
|
751
|
+
}
|
752
|
+
|
753
|
+
export {
|
754
|
+
toLanguageModelMessages,
|
755
|
+
toCoreMessages,
|
756
|
+
toCoreMessage,
|
757
|
+
toLanguageModelTools,
|
758
|
+
runResultStream,
|
759
|
+
toolResultStream
|
760
|
+
};
|
761
|
+
//# sourceMappingURL=chunk-2RKUKZSZ.mjs.map
|