@assistant-ui/react 0.4.4 → 0.4.5
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/edge.d.mts +94 -3
- package/dist/edge.d.ts +94 -3
- package/dist/edge.js +666 -15
- package/dist/edge.js.map +1 -1
- package/dist/edge.mjs +656 -15
- package/dist/edge.mjs.map +1 -1
- package/dist/index.d.mts +23 -17
- package/dist/index.d.ts +23 -17
- package/dist/index.js +227 -172
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +227 -172
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/edge.js
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __create = Object.create;
|
2
3
|
var __defProp = Object.defineProperty;
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
8
|
var __export = (target, all) => {
|
7
9
|
for (var name in all)
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
15
17
|
}
|
16
18
|
return to;
|
17
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
|
+
));
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
29
|
|
20
30
|
// src/edge.ts
|
@@ -60,6 +70,15 @@ function assistantEncoderStream() {
|
|
60
70
|
}
|
61
71
|
case "tool-call":
|
62
72
|
break;
|
73
|
+
case "tool-result": {
|
74
|
+
controller.enqueue(
|
75
|
+
formatStreamPart("3" /* ToolCallResult */, {
|
76
|
+
id: chunk.toolCallId,
|
77
|
+
result: chunk.result
|
78
|
+
})
|
79
|
+
);
|
80
|
+
break;
|
81
|
+
}
|
63
82
|
case "finish": {
|
64
83
|
const { type, ...rest } = chunk;
|
65
84
|
controller.enqueue(
|
@@ -203,21 +222,658 @@ function toLanguageModelMessages(message) {
|
|
203
222
|
}
|
204
223
|
|
205
224
|
// src/runtimes/edge/createEdgeRuntimeAPI.ts
|
206
|
-
var
|
225
|
+
var import_zod3 = require("zod");
|
226
|
+
|
227
|
+
// src/runtimes/edge/converters/toLanguageModelTools.ts
|
228
|
+
var import_zod = require("zod");
|
229
|
+
var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
|
230
|
+
var toLanguageModelTools = (tools) => {
|
231
|
+
if (!tools) return [];
|
232
|
+
return Object.entries(tools).map(([name, tool]) => ({
|
233
|
+
type: "function",
|
234
|
+
name,
|
235
|
+
...tool.description ? { description: tool.description } : void 0,
|
236
|
+
parameters: tool.parameters instanceof import_zod.z.ZodType ? (0, import_zod_to_json_schema.default)(tool.parameters) : tool.parameters
|
237
|
+
}));
|
238
|
+
};
|
239
|
+
|
240
|
+
// src/runtimes/edge/streams/toolResultStream.ts
|
241
|
+
var import_zod2 = require("zod");
|
242
|
+
var import_secure_json_parse = __toESM(require("secure-json-parse"));
|
243
|
+
function toolResultStream(tools) {
|
244
|
+
const toolCallExecutions = /* @__PURE__ */ new Map();
|
245
|
+
return new TransformStream({
|
246
|
+
transform(chunk, controller) {
|
247
|
+
controller.enqueue(chunk);
|
248
|
+
const chunkType = chunk.type;
|
249
|
+
switch (chunkType) {
|
250
|
+
case "tool-call": {
|
251
|
+
const { toolCallId, toolCallType, toolName, args: argsText } = chunk;
|
252
|
+
const tool = tools?.[toolName];
|
253
|
+
if (!tool || !tool.execute) return;
|
254
|
+
const args = import_secure_json_parse.default.parse(argsText);
|
255
|
+
if (tool.parameters instanceof import_zod2.z.ZodType) {
|
256
|
+
const result = tool.parameters.safeParse(args);
|
257
|
+
if (!result.success) {
|
258
|
+
controller.enqueue({
|
259
|
+
type: "error",
|
260
|
+
error: new Error("Invalid tool call arguments")
|
261
|
+
});
|
262
|
+
return;
|
263
|
+
} else {
|
264
|
+
toolCallExecutions.set(
|
265
|
+
toolCallId,
|
266
|
+
(async () => {
|
267
|
+
try {
|
268
|
+
const result2 = await tool.execute(args);
|
269
|
+
controller.enqueue({
|
270
|
+
type: "tool-result",
|
271
|
+
toolCallType,
|
272
|
+
toolCallId,
|
273
|
+
toolName,
|
274
|
+
result: result2
|
275
|
+
});
|
276
|
+
} catch (error) {
|
277
|
+
controller.enqueue({
|
278
|
+
type: "error",
|
279
|
+
error
|
280
|
+
});
|
281
|
+
} finally {
|
282
|
+
toolCallExecutions.delete(toolCallId);
|
283
|
+
}
|
284
|
+
})()
|
285
|
+
);
|
286
|
+
}
|
287
|
+
}
|
288
|
+
break;
|
289
|
+
}
|
290
|
+
case "text-delta":
|
291
|
+
case "tool-call-delta":
|
292
|
+
case "tool-result":
|
293
|
+
case "finish":
|
294
|
+
case "error":
|
295
|
+
break;
|
296
|
+
default: {
|
297
|
+
const unhandledType = chunkType;
|
298
|
+
throw new Error(`Unhandled chunk type: ${unhandledType}`);
|
299
|
+
}
|
300
|
+
}
|
301
|
+
},
|
302
|
+
async flush() {
|
303
|
+
await Promise.all(toolCallExecutions.values());
|
304
|
+
}
|
305
|
+
});
|
306
|
+
}
|
307
|
+
|
308
|
+
// src/runtimes/edge/partial-json/parse-partial-json.ts
|
309
|
+
var import_secure_json_parse2 = __toESM(require("secure-json-parse"));
|
310
|
+
|
311
|
+
// src/runtimes/edge/partial-json/fix-json.ts
|
312
|
+
function fixJson(input) {
|
313
|
+
const stack = ["ROOT"];
|
314
|
+
let lastValidIndex = -1;
|
315
|
+
let literalStart = null;
|
316
|
+
function processValueStart(char, i, swapState) {
|
317
|
+
{
|
318
|
+
switch (char) {
|
319
|
+
case '"': {
|
320
|
+
lastValidIndex = i;
|
321
|
+
stack.pop();
|
322
|
+
stack.push(swapState);
|
323
|
+
stack.push("INSIDE_STRING");
|
324
|
+
break;
|
325
|
+
}
|
326
|
+
case "f":
|
327
|
+
case "t":
|
328
|
+
case "n": {
|
329
|
+
lastValidIndex = i;
|
330
|
+
literalStart = i;
|
331
|
+
stack.pop();
|
332
|
+
stack.push(swapState);
|
333
|
+
stack.push("INSIDE_LITERAL");
|
334
|
+
break;
|
335
|
+
}
|
336
|
+
case "-": {
|
337
|
+
stack.pop();
|
338
|
+
stack.push(swapState);
|
339
|
+
stack.push("INSIDE_NUMBER");
|
340
|
+
break;
|
341
|
+
}
|
342
|
+
case "0":
|
343
|
+
case "1":
|
344
|
+
case "2":
|
345
|
+
case "3":
|
346
|
+
case "4":
|
347
|
+
case "5":
|
348
|
+
case "6":
|
349
|
+
case "7":
|
350
|
+
case "8":
|
351
|
+
case "9": {
|
352
|
+
lastValidIndex = i;
|
353
|
+
stack.pop();
|
354
|
+
stack.push(swapState);
|
355
|
+
stack.push("INSIDE_NUMBER");
|
356
|
+
break;
|
357
|
+
}
|
358
|
+
case "{": {
|
359
|
+
lastValidIndex = i;
|
360
|
+
stack.pop();
|
361
|
+
stack.push(swapState);
|
362
|
+
stack.push("INSIDE_OBJECT_START");
|
363
|
+
break;
|
364
|
+
}
|
365
|
+
case "[": {
|
366
|
+
lastValidIndex = i;
|
367
|
+
stack.pop();
|
368
|
+
stack.push(swapState);
|
369
|
+
stack.push("INSIDE_ARRAY_START");
|
370
|
+
break;
|
371
|
+
}
|
372
|
+
}
|
373
|
+
}
|
374
|
+
}
|
375
|
+
function processAfterObjectValue(char, i) {
|
376
|
+
switch (char) {
|
377
|
+
case ",": {
|
378
|
+
stack.pop();
|
379
|
+
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
380
|
+
break;
|
381
|
+
}
|
382
|
+
case "}": {
|
383
|
+
lastValidIndex = i;
|
384
|
+
stack.pop();
|
385
|
+
break;
|
386
|
+
}
|
387
|
+
}
|
388
|
+
}
|
389
|
+
function processAfterArrayValue(char, i) {
|
390
|
+
switch (char) {
|
391
|
+
case ",": {
|
392
|
+
stack.pop();
|
393
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
394
|
+
break;
|
395
|
+
}
|
396
|
+
case "]": {
|
397
|
+
lastValidIndex = i;
|
398
|
+
stack.pop();
|
399
|
+
break;
|
400
|
+
}
|
401
|
+
}
|
402
|
+
}
|
403
|
+
for (let i = 0; i < input.length; i++) {
|
404
|
+
const char = input[i];
|
405
|
+
const currentState = stack[stack.length - 1];
|
406
|
+
switch (currentState) {
|
407
|
+
case "ROOT":
|
408
|
+
processValueStart(char, i, "FINISH");
|
409
|
+
break;
|
410
|
+
case "INSIDE_OBJECT_START": {
|
411
|
+
switch (char) {
|
412
|
+
case '"': {
|
413
|
+
stack.pop();
|
414
|
+
stack.push("INSIDE_OBJECT_KEY");
|
415
|
+
break;
|
416
|
+
}
|
417
|
+
case "}": {
|
418
|
+
lastValidIndex = i;
|
419
|
+
stack.pop();
|
420
|
+
break;
|
421
|
+
}
|
422
|
+
}
|
423
|
+
break;
|
424
|
+
}
|
425
|
+
case "INSIDE_OBJECT_AFTER_COMMA": {
|
426
|
+
switch (char) {
|
427
|
+
case '"': {
|
428
|
+
stack.pop();
|
429
|
+
stack.push("INSIDE_OBJECT_KEY");
|
430
|
+
break;
|
431
|
+
}
|
432
|
+
}
|
433
|
+
break;
|
434
|
+
}
|
435
|
+
case "INSIDE_OBJECT_KEY": {
|
436
|
+
switch (char) {
|
437
|
+
case '"': {
|
438
|
+
stack.pop();
|
439
|
+
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
440
|
+
break;
|
441
|
+
}
|
442
|
+
}
|
443
|
+
break;
|
444
|
+
}
|
445
|
+
case "INSIDE_OBJECT_AFTER_KEY": {
|
446
|
+
switch (char) {
|
447
|
+
case ":": {
|
448
|
+
stack.pop();
|
449
|
+
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
450
|
+
break;
|
451
|
+
}
|
452
|
+
}
|
453
|
+
break;
|
454
|
+
}
|
455
|
+
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
456
|
+
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
457
|
+
break;
|
458
|
+
}
|
459
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
460
|
+
processAfterObjectValue(char, i);
|
461
|
+
break;
|
462
|
+
}
|
463
|
+
case "INSIDE_STRING": {
|
464
|
+
switch (char) {
|
465
|
+
case '"': {
|
466
|
+
stack.pop();
|
467
|
+
lastValidIndex = i;
|
468
|
+
break;
|
469
|
+
}
|
470
|
+
case "\\": {
|
471
|
+
stack.push("INSIDE_STRING_ESCAPE");
|
472
|
+
break;
|
473
|
+
}
|
474
|
+
default: {
|
475
|
+
lastValidIndex = i;
|
476
|
+
}
|
477
|
+
}
|
478
|
+
break;
|
479
|
+
}
|
480
|
+
case "INSIDE_ARRAY_START": {
|
481
|
+
switch (char) {
|
482
|
+
case "]": {
|
483
|
+
lastValidIndex = i;
|
484
|
+
stack.pop();
|
485
|
+
break;
|
486
|
+
}
|
487
|
+
default: {
|
488
|
+
lastValidIndex = i;
|
489
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
490
|
+
break;
|
491
|
+
}
|
492
|
+
}
|
493
|
+
break;
|
494
|
+
}
|
495
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
496
|
+
switch (char) {
|
497
|
+
case ",": {
|
498
|
+
stack.pop();
|
499
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
500
|
+
break;
|
501
|
+
}
|
502
|
+
case "]": {
|
503
|
+
lastValidIndex = i;
|
504
|
+
stack.pop();
|
505
|
+
break;
|
506
|
+
}
|
507
|
+
default: {
|
508
|
+
lastValidIndex = i;
|
509
|
+
break;
|
510
|
+
}
|
511
|
+
}
|
512
|
+
break;
|
513
|
+
}
|
514
|
+
case "INSIDE_ARRAY_AFTER_COMMA": {
|
515
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
516
|
+
break;
|
517
|
+
}
|
518
|
+
case "INSIDE_STRING_ESCAPE": {
|
519
|
+
stack.pop();
|
520
|
+
lastValidIndex = i;
|
521
|
+
break;
|
522
|
+
}
|
523
|
+
case "INSIDE_NUMBER": {
|
524
|
+
switch (char) {
|
525
|
+
case "0":
|
526
|
+
case "1":
|
527
|
+
case "2":
|
528
|
+
case "3":
|
529
|
+
case "4":
|
530
|
+
case "5":
|
531
|
+
case "6":
|
532
|
+
case "7":
|
533
|
+
case "8":
|
534
|
+
case "9": {
|
535
|
+
lastValidIndex = i;
|
536
|
+
break;
|
537
|
+
}
|
538
|
+
case "e":
|
539
|
+
case "E":
|
540
|
+
case "-":
|
541
|
+
case ".": {
|
542
|
+
break;
|
543
|
+
}
|
544
|
+
case ",": {
|
545
|
+
stack.pop();
|
546
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
547
|
+
processAfterArrayValue(char, i);
|
548
|
+
}
|
549
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
550
|
+
processAfterObjectValue(char, i);
|
551
|
+
}
|
552
|
+
break;
|
553
|
+
}
|
554
|
+
case "}": {
|
555
|
+
stack.pop();
|
556
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
557
|
+
processAfterObjectValue(char, i);
|
558
|
+
}
|
559
|
+
break;
|
560
|
+
}
|
561
|
+
case "]": {
|
562
|
+
stack.pop();
|
563
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
564
|
+
processAfterArrayValue(char, i);
|
565
|
+
}
|
566
|
+
break;
|
567
|
+
}
|
568
|
+
default: {
|
569
|
+
stack.pop();
|
570
|
+
break;
|
571
|
+
}
|
572
|
+
}
|
573
|
+
break;
|
574
|
+
}
|
575
|
+
case "INSIDE_LITERAL": {
|
576
|
+
const partialLiteral = input.substring(literalStart, i + 1);
|
577
|
+
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
578
|
+
stack.pop();
|
579
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
580
|
+
processAfterObjectValue(char, i);
|
581
|
+
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
582
|
+
processAfterArrayValue(char, i);
|
583
|
+
}
|
584
|
+
} else {
|
585
|
+
lastValidIndex = i;
|
586
|
+
}
|
587
|
+
break;
|
588
|
+
}
|
589
|
+
}
|
590
|
+
}
|
591
|
+
let result = input.slice(0, lastValidIndex + 1);
|
592
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
593
|
+
const state = stack[i];
|
594
|
+
switch (state) {
|
595
|
+
case "INSIDE_STRING": {
|
596
|
+
result += '"';
|
597
|
+
break;
|
598
|
+
}
|
599
|
+
case "INSIDE_OBJECT_KEY":
|
600
|
+
case "INSIDE_OBJECT_AFTER_KEY":
|
601
|
+
case "INSIDE_OBJECT_AFTER_COMMA":
|
602
|
+
case "INSIDE_OBJECT_START":
|
603
|
+
case "INSIDE_OBJECT_BEFORE_VALUE":
|
604
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
605
|
+
result += "}";
|
606
|
+
break;
|
607
|
+
}
|
608
|
+
case "INSIDE_ARRAY_START":
|
609
|
+
case "INSIDE_ARRAY_AFTER_COMMA":
|
610
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
611
|
+
result += "]";
|
612
|
+
break;
|
613
|
+
}
|
614
|
+
case "INSIDE_LITERAL": {
|
615
|
+
const partialLiteral = input.substring(literalStart, input.length);
|
616
|
+
if ("true".startsWith(partialLiteral)) {
|
617
|
+
result += "true".slice(partialLiteral.length);
|
618
|
+
} else if ("false".startsWith(partialLiteral)) {
|
619
|
+
result += "false".slice(partialLiteral.length);
|
620
|
+
} else if ("null".startsWith(partialLiteral)) {
|
621
|
+
result += "null".slice(partialLiteral.length);
|
622
|
+
}
|
623
|
+
}
|
624
|
+
}
|
625
|
+
}
|
626
|
+
return result;
|
627
|
+
}
|
628
|
+
|
629
|
+
// src/runtimes/edge/partial-json/parse-partial-json.ts
|
630
|
+
var parsePartialJson = (json) => {
|
631
|
+
try {
|
632
|
+
return import_secure_json_parse2.default.parse(json);
|
633
|
+
} catch {
|
634
|
+
try {
|
635
|
+
return import_secure_json_parse2.default.parse(fixJson(json));
|
636
|
+
} catch {
|
637
|
+
return void 0;
|
638
|
+
}
|
639
|
+
}
|
640
|
+
};
|
641
|
+
|
642
|
+
// src/runtimes/edge/streams/runResultStream.ts
|
643
|
+
function runResultStream(initialContent) {
|
644
|
+
let message = {
|
645
|
+
content: initialContent
|
646
|
+
};
|
647
|
+
const currentToolCall = { toolCallId: "", argsText: "" };
|
648
|
+
return new TransformStream({
|
649
|
+
transform(chunk, controller) {
|
650
|
+
const chunkType = chunk.type;
|
651
|
+
switch (chunkType) {
|
652
|
+
case "text-delta": {
|
653
|
+
message = appendOrUpdateText(message, chunk.textDelta);
|
654
|
+
controller.enqueue(message);
|
655
|
+
break;
|
656
|
+
}
|
657
|
+
case "tool-call-delta": {
|
658
|
+
const { toolCallId, toolName, argsTextDelta } = chunk;
|
659
|
+
if (currentToolCall.toolCallId !== toolCallId) {
|
660
|
+
currentToolCall.toolCallId = toolCallId;
|
661
|
+
currentToolCall.argsText = argsTextDelta;
|
662
|
+
} else {
|
663
|
+
currentToolCall.argsText += argsTextDelta;
|
664
|
+
}
|
665
|
+
message = appendOrUpdateToolCall(
|
666
|
+
message,
|
667
|
+
toolCallId,
|
668
|
+
toolName,
|
669
|
+
currentToolCall.argsText
|
670
|
+
);
|
671
|
+
controller.enqueue(message);
|
672
|
+
break;
|
673
|
+
}
|
674
|
+
case "tool-call": {
|
675
|
+
break;
|
676
|
+
}
|
677
|
+
case "tool-result": {
|
678
|
+
message = appendOrUpdateToolResult(
|
679
|
+
message,
|
680
|
+
chunk.toolCallId,
|
681
|
+
chunk.toolName,
|
682
|
+
chunk.result
|
683
|
+
);
|
684
|
+
controller.enqueue(message);
|
685
|
+
break;
|
686
|
+
}
|
687
|
+
case "finish": {
|
688
|
+
message = appendOrUpdateFinish(message, chunk);
|
689
|
+
controller.enqueue(message);
|
690
|
+
break;
|
691
|
+
}
|
692
|
+
case "error": {
|
693
|
+
throw chunk.error;
|
694
|
+
}
|
695
|
+
default: {
|
696
|
+
const unhandledType = chunkType;
|
697
|
+
throw new Error(`Unhandled chunk type: ${unhandledType}`);
|
698
|
+
}
|
699
|
+
}
|
700
|
+
}
|
701
|
+
});
|
702
|
+
}
|
703
|
+
var appendOrUpdateText = (message, textDelta) => {
|
704
|
+
let contentParts = message.content;
|
705
|
+
let contentPart = message.content.at(-1);
|
706
|
+
if (contentPart?.type !== "text") {
|
707
|
+
contentPart = { type: "text", text: textDelta };
|
708
|
+
} else {
|
709
|
+
contentParts = contentParts.slice(0, -1);
|
710
|
+
contentPart = { type: "text", text: contentPart.text + textDelta };
|
711
|
+
}
|
712
|
+
return {
|
713
|
+
...message,
|
714
|
+
content: contentParts.concat([contentPart])
|
715
|
+
};
|
716
|
+
};
|
717
|
+
var appendOrUpdateToolCall = (message, toolCallId, toolName, argsText) => {
|
718
|
+
let contentParts = message.content;
|
719
|
+
let contentPart = message.content.at(-1);
|
720
|
+
if (contentPart?.type !== "tool-call" || contentPart.toolCallId !== toolCallId) {
|
721
|
+
contentPart = {
|
722
|
+
type: "tool-call",
|
723
|
+
toolCallId,
|
724
|
+
toolName,
|
725
|
+
argsText,
|
726
|
+
args: parsePartialJson(argsText)
|
727
|
+
};
|
728
|
+
} else {
|
729
|
+
contentParts = contentParts.slice(0, -1);
|
730
|
+
contentPart = {
|
731
|
+
...contentPart,
|
732
|
+
argsText,
|
733
|
+
args: parsePartialJson(argsText)
|
734
|
+
};
|
735
|
+
}
|
736
|
+
return {
|
737
|
+
...message,
|
738
|
+
content: contentParts.concat([contentPart])
|
739
|
+
};
|
740
|
+
};
|
741
|
+
var appendOrUpdateToolResult = (message, toolCallId, toolName, result) => {
|
742
|
+
let found = false;
|
743
|
+
const newContentParts = message.content.map((part) => {
|
744
|
+
if (part.type !== "tool-call" || part.toolCallId !== toolCallId)
|
745
|
+
return part;
|
746
|
+
found = true;
|
747
|
+
if (part.toolName !== toolName)
|
748
|
+
throw new Error(
|
749
|
+
`Tool call ${toolCallId} found with tool name ${part.toolName}, but expected ${toolName}`
|
750
|
+
);
|
751
|
+
return {
|
752
|
+
...part,
|
753
|
+
result
|
754
|
+
};
|
755
|
+
});
|
756
|
+
if (!found)
|
757
|
+
throw new Error(
|
758
|
+
`Received tool result for unknown tool call "${toolName}" / "${toolCallId}". This is likely an internal bug in assistant-ui.`
|
759
|
+
);
|
760
|
+
return {
|
761
|
+
...message,
|
762
|
+
content: newContentParts
|
763
|
+
};
|
764
|
+
};
|
765
|
+
var appendOrUpdateFinish = (message, chunk) => {
|
766
|
+
const { type, ...rest } = chunk;
|
767
|
+
return {
|
768
|
+
...message,
|
769
|
+
status: {
|
770
|
+
type: "done",
|
771
|
+
...rest
|
772
|
+
}
|
773
|
+
};
|
774
|
+
};
|
775
|
+
|
776
|
+
// src/runtimes/edge/createEdgeRuntimeAPI.ts
|
777
|
+
var LanguageModelSettingsSchema = import_zod3.z.object({
|
778
|
+
maxTokens: import_zod3.z.number().int().positive().optional(),
|
779
|
+
temperature: import_zod3.z.number().optional(),
|
780
|
+
topP: import_zod3.z.number().optional(),
|
781
|
+
presencePenalty: import_zod3.z.number().optional(),
|
782
|
+
frequencyPenalty: import_zod3.z.number().optional(),
|
783
|
+
seed: import_zod3.z.number().int().optional(),
|
784
|
+
headers: import_zod3.z.record(import_zod3.z.string().optional()).optional()
|
785
|
+
});
|
786
|
+
var voidStream = () => {
|
787
|
+
return new WritableStream({
|
788
|
+
abort(reason) {
|
789
|
+
console.error("Server stream processing aborted:", reason);
|
790
|
+
}
|
791
|
+
});
|
792
|
+
};
|
793
|
+
var createEdgeRuntimeAPI = ({
|
794
|
+
model,
|
795
|
+
system: serverSystem,
|
796
|
+
tools: serverTools = {},
|
797
|
+
toolChoice,
|
798
|
+
onFinish,
|
799
|
+
...unsafeSettings
|
800
|
+
}) => {
|
801
|
+
const settings = LanguageModelSettingsSchema.parse(unsafeSettings);
|
802
|
+
const lmServerTools = toLanguageModelTools(serverTools);
|
803
|
+
const hasServerTools = Object.values(serverTools).some((v) => !!v.execute);
|
207
804
|
const POST = async (request) => {
|
208
|
-
const {
|
209
|
-
|
805
|
+
const {
|
806
|
+
system: clientSystem,
|
807
|
+
tools: clientTools,
|
808
|
+
messages
|
809
|
+
} = await request.json();
|
810
|
+
const systemMessages = [];
|
811
|
+
if (serverSystem) systemMessages.push(serverSystem);
|
812
|
+
if (clientSystem) systemMessages.push(clientSystem);
|
813
|
+
const system = systemMessages.join("\n\n");
|
814
|
+
for (const clientTool of clientTools) {
|
815
|
+
if (serverTools?.[clientTool.name]) {
|
816
|
+
throw new Error(
|
817
|
+
`Tool ${clientTool.name} was defined in both the client and server tools. This is not allowed.`
|
818
|
+
);
|
819
|
+
}
|
820
|
+
}
|
821
|
+
let stream;
|
822
|
+
const streamResult = await streamMessage({
|
823
|
+
...settings,
|
210
824
|
model,
|
211
825
|
abortSignal: request.signal,
|
212
|
-
|
826
|
+
...!!system ? { system } : void 0,
|
213
827
|
messages,
|
214
|
-
tools
|
828
|
+
tools: lmServerTools.concat(clientTools),
|
829
|
+
...toolChoice ? { toolChoice } : void 0
|
215
830
|
});
|
216
|
-
|
217
|
-
|
218
|
-
|
831
|
+
stream = streamResult.stream;
|
832
|
+
const canExecuteTools = hasServerTools && toolChoice?.type !== "none";
|
833
|
+
if (canExecuteTools) {
|
834
|
+
stream = stream.pipeThrough(toolResultStream(serverTools));
|
835
|
+
}
|
836
|
+
if (canExecuteTools || onFinish) {
|
837
|
+
const tees = stream.tee();
|
838
|
+
stream = tees[0];
|
839
|
+
let serverStream = tees[1];
|
840
|
+
if (onFinish) {
|
841
|
+
serverStream = serverStream.pipeThrough(runResultStream([])).pipeThrough(
|
842
|
+
new TransformStream({
|
843
|
+
transform(chunk) {
|
844
|
+
if (chunk.status?.type !== "done") return;
|
845
|
+
const resultingMessages = [
|
846
|
+
...messages,
|
847
|
+
{
|
848
|
+
role: "assistant",
|
849
|
+
content: chunk.content
|
850
|
+
}
|
851
|
+
];
|
852
|
+
onFinish({
|
853
|
+
finishReason: chunk.status.finishReason,
|
854
|
+
usage: chunk.status.usage,
|
855
|
+
messages: resultingMessages,
|
856
|
+
logProbs: chunk.status.logprops,
|
857
|
+
warnings: streamResult.warnings,
|
858
|
+
rawCall: streamResult.rawCall,
|
859
|
+
rawResponse: streamResult.rawResponse
|
860
|
+
});
|
861
|
+
}
|
862
|
+
})
|
863
|
+
);
|
219
864
|
}
|
220
|
-
|
865
|
+
serverStream.pipeTo(voidStream()).catch((e) => {
|
866
|
+
console.error("Server stream processing error:", e);
|
867
|
+
});
|
868
|
+
}
|
869
|
+
return new Response(
|
870
|
+
stream.pipeThrough(assistantEncoderStream()).pipeThrough(new TextEncoderStream()),
|
871
|
+
{
|
872
|
+
headers: {
|
873
|
+
contentType: "text/plain; charset=utf-8"
|
874
|
+
}
|
875
|
+
}
|
876
|
+
);
|
221
877
|
};
|
222
878
|
return { POST };
|
223
879
|
};
|
@@ -229,7 +885,7 @@ async function streamMessage({
|
|
229
885
|
toolChoice,
|
230
886
|
...options
|
231
887
|
}) {
|
232
|
-
|
888
|
+
return model.doStream({
|
233
889
|
inputFormat: "messages",
|
234
890
|
mode: {
|
235
891
|
type: "regular",
|
@@ -239,11 +895,6 @@ async function streamMessage({
|
|
239
895
|
prompt: convertToLanguageModelPrompt(system, messages),
|
240
896
|
...options
|
241
897
|
});
|
242
|
-
return {
|
243
|
-
stream: stream.pipeThrough(assistantEncoderStream()).pipeThrough(new TextEncoderStream()),
|
244
|
-
warnings,
|
245
|
-
rawResponse
|
246
|
-
};
|
247
898
|
}
|
248
899
|
function convertToLanguageModelPrompt(system, messages) {
|
249
900
|
const languageModelMessages = [];
|