@assistant-ui/react 0.5.20 → 0.5.22

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,819 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }"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
+ var _zod = require('zod');
160
+ var _zodtojsonschema = require('zod-to-json-schema'); var _zodtojsonschema2 = _interopRequireDefault(_zodtojsonschema);
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 _zod.z.ZodType ? _zodtojsonschema2.default.call(void 0, tool.parameters) : tool.parameters
167
+ }));
168
+ };
169
+
170
+ // src/types/ModelConfigTypes.ts
171
+
172
+ var LanguageModelV1CallSettingsSchema = _zod.z.object({
173
+ maxTokens: _zod.z.number().int().positive().optional(),
174
+ temperature: _zod.z.number().optional(),
175
+ topP: _zod.z.number().optional(),
176
+ presencePenalty: _zod.z.number().optional(),
177
+ frequencyPenalty: _zod.z.number().optional(),
178
+ seed: _zod.z.number().int().optional(),
179
+ headers: _zod.z.record(_zod.z.string().optional()).optional()
180
+ });
181
+ var LanguageModelConfigSchema = _zod.z.object({
182
+ apiKey: _zod.z.string().optional(),
183
+ baseUrl: _zod.z.string().optional(),
184
+ modelName: _zod.z.string().optional()
185
+ });
186
+ var mergeModelConfigs = (configSet) => {
187
+ const configs = Array.from(configSet).map((c) => c.getModelConfig()).sort((a, b) => (_nullishCoalesce(b.priority, () => ( 0))) - (_nullishCoalesce(a.priority, () => ( 0))));
188
+ return configs.reduce((acc, config) => {
189
+ if (config.system) {
190
+ if (acc.system) {
191
+ acc.system += `
192
+
193
+ ${config.system}`;
194
+ } else {
195
+ acc.system = config.system;
196
+ }
197
+ }
198
+ if (config.tools) {
199
+ for (const [name, tool] of Object.entries(config.tools)) {
200
+ if (_optionalChain([acc, 'access', _ => _.tools, 'optionalAccess', _2 => _2[name]])) {
201
+ throw new Error(
202
+ `You tried to define a tool with the name ${name}, but it already exists.`
203
+ );
204
+ }
205
+ if (!acc.tools) acc.tools = {};
206
+ acc.tools[name] = tool;
207
+ }
208
+ }
209
+ if (config.config) {
210
+ acc.config = {
211
+ ...acc.config,
212
+ ...config.config
213
+ };
214
+ }
215
+ if (config.callSettings) {
216
+ acc.callSettings = {
217
+ ...acc.callSettings,
218
+ ...config.callSettings
219
+ };
220
+ }
221
+ return acc;
222
+ }, {});
223
+ };
224
+
225
+ // src/runtimes/edge/partial-json/parse-partial-json.ts
226
+ var _securejsonparse = require('secure-json-parse'); var _securejsonparse2 = _interopRequireDefault(_securejsonparse);
227
+
228
+ // src/runtimes/edge/partial-json/fix-json.ts
229
+ function fixJson(input) {
230
+ const stack = ["ROOT"];
231
+ let lastValidIndex = -1;
232
+ let literalStart = null;
233
+ function processValueStart(char, i, swapState) {
234
+ {
235
+ switch (char) {
236
+ case '"': {
237
+ lastValidIndex = i;
238
+ stack.pop();
239
+ stack.push(swapState);
240
+ stack.push("INSIDE_STRING");
241
+ break;
242
+ }
243
+ case "f":
244
+ case "t":
245
+ case "n": {
246
+ lastValidIndex = i;
247
+ literalStart = i;
248
+ stack.pop();
249
+ stack.push(swapState);
250
+ stack.push("INSIDE_LITERAL");
251
+ break;
252
+ }
253
+ case "-": {
254
+ stack.pop();
255
+ stack.push(swapState);
256
+ stack.push("INSIDE_NUMBER");
257
+ break;
258
+ }
259
+ case "0":
260
+ case "1":
261
+ case "2":
262
+ case "3":
263
+ case "4":
264
+ case "5":
265
+ case "6":
266
+ case "7":
267
+ case "8":
268
+ case "9": {
269
+ lastValidIndex = i;
270
+ stack.pop();
271
+ stack.push(swapState);
272
+ stack.push("INSIDE_NUMBER");
273
+ break;
274
+ }
275
+ case "{": {
276
+ lastValidIndex = i;
277
+ stack.pop();
278
+ stack.push(swapState);
279
+ stack.push("INSIDE_OBJECT_START");
280
+ break;
281
+ }
282
+ case "[": {
283
+ lastValidIndex = i;
284
+ stack.pop();
285
+ stack.push(swapState);
286
+ stack.push("INSIDE_ARRAY_START");
287
+ break;
288
+ }
289
+ }
290
+ }
291
+ }
292
+ function processAfterObjectValue(char, i) {
293
+ switch (char) {
294
+ case ",": {
295
+ stack.pop();
296
+ stack.push("INSIDE_OBJECT_AFTER_COMMA");
297
+ break;
298
+ }
299
+ case "}": {
300
+ lastValidIndex = i;
301
+ stack.pop();
302
+ break;
303
+ }
304
+ }
305
+ }
306
+ function processAfterArrayValue(char, i) {
307
+ switch (char) {
308
+ case ",": {
309
+ stack.pop();
310
+ stack.push("INSIDE_ARRAY_AFTER_COMMA");
311
+ break;
312
+ }
313
+ case "]": {
314
+ lastValidIndex = i;
315
+ stack.pop();
316
+ break;
317
+ }
318
+ }
319
+ }
320
+ for (let i = 0; i < input.length; i++) {
321
+ const char = input[i];
322
+ const currentState = stack[stack.length - 1];
323
+ switch (currentState) {
324
+ case "ROOT":
325
+ processValueStart(char, i, "FINISH");
326
+ break;
327
+ case "INSIDE_OBJECT_START": {
328
+ switch (char) {
329
+ case '"': {
330
+ stack.pop();
331
+ stack.push("INSIDE_OBJECT_KEY");
332
+ break;
333
+ }
334
+ case "}": {
335
+ lastValidIndex = i;
336
+ stack.pop();
337
+ break;
338
+ }
339
+ }
340
+ break;
341
+ }
342
+ case "INSIDE_OBJECT_AFTER_COMMA": {
343
+ switch (char) {
344
+ case '"': {
345
+ stack.pop();
346
+ stack.push("INSIDE_OBJECT_KEY");
347
+ break;
348
+ }
349
+ }
350
+ break;
351
+ }
352
+ case "INSIDE_OBJECT_KEY": {
353
+ switch (char) {
354
+ case '"': {
355
+ stack.pop();
356
+ stack.push("INSIDE_OBJECT_AFTER_KEY");
357
+ break;
358
+ }
359
+ }
360
+ break;
361
+ }
362
+ case "INSIDE_OBJECT_AFTER_KEY": {
363
+ switch (char) {
364
+ case ":": {
365
+ stack.pop();
366
+ stack.push("INSIDE_OBJECT_BEFORE_VALUE");
367
+ break;
368
+ }
369
+ }
370
+ break;
371
+ }
372
+ case "INSIDE_OBJECT_BEFORE_VALUE": {
373
+ processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
374
+ break;
375
+ }
376
+ case "INSIDE_OBJECT_AFTER_VALUE": {
377
+ processAfterObjectValue(char, i);
378
+ break;
379
+ }
380
+ case "INSIDE_STRING": {
381
+ switch (char) {
382
+ case '"': {
383
+ stack.pop();
384
+ lastValidIndex = i;
385
+ break;
386
+ }
387
+ case "\\": {
388
+ stack.push("INSIDE_STRING_ESCAPE");
389
+ break;
390
+ }
391
+ default: {
392
+ lastValidIndex = i;
393
+ }
394
+ }
395
+ break;
396
+ }
397
+ case "INSIDE_ARRAY_START": {
398
+ switch (char) {
399
+ case "]": {
400
+ lastValidIndex = i;
401
+ stack.pop();
402
+ break;
403
+ }
404
+ default: {
405
+ lastValidIndex = i;
406
+ processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
407
+ break;
408
+ }
409
+ }
410
+ break;
411
+ }
412
+ case "INSIDE_ARRAY_AFTER_VALUE": {
413
+ switch (char) {
414
+ case ",": {
415
+ stack.pop();
416
+ stack.push("INSIDE_ARRAY_AFTER_COMMA");
417
+ break;
418
+ }
419
+ case "]": {
420
+ lastValidIndex = i;
421
+ stack.pop();
422
+ break;
423
+ }
424
+ default: {
425
+ lastValidIndex = i;
426
+ break;
427
+ }
428
+ }
429
+ break;
430
+ }
431
+ case "INSIDE_ARRAY_AFTER_COMMA": {
432
+ processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
433
+ break;
434
+ }
435
+ case "INSIDE_STRING_ESCAPE": {
436
+ stack.pop();
437
+ lastValidIndex = i;
438
+ break;
439
+ }
440
+ case "INSIDE_NUMBER": {
441
+ switch (char) {
442
+ case "0":
443
+ case "1":
444
+ case "2":
445
+ case "3":
446
+ case "4":
447
+ case "5":
448
+ case "6":
449
+ case "7":
450
+ case "8":
451
+ case "9": {
452
+ lastValidIndex = i;
453
+ break;
454
+ }
455
+ case "e":
456
+ case "E":
457
+ case "-":
458
+ case ".": {
459
+ break;
460
+ }
461
+ case ",": {
462
+ stack.pop();
463
+ if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
464
+ processAfterArrayValue(char, i);
465
+ }
466
+ if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
467
+ processAfterObjectValue(char, i);
468
+ }
469
+ break;
470
+ }
471
+ case "}": {
472
+ stack.pop();
473
+ if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
474
+ processAfterObjectValue(char, i);
475
+ }
476
+ break;
477
+ }
478
+ case "]": {
479
+ stack.pop();
480
+ if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
481
+ processAfterArrayValue(char, i);
482
+ }
483
+ break;
484
+ }
485
+ default: {
486
+ stack.pop();
487
+ break;
488
+ }
489
+ }
490
+ break;
491
+ }
492
+ case "INSIDE_LITERAL": {
493
+ const partialLiteral = input.substring(literalStart, i + 1);
494
+ if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
495
+ stack.pop();
496
+ if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
497
+ processAfterObjectValue(char, i);
498
+ } else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
499
+ processAfterArrayValue(char, i);
500
+ }
501
+ } else {
502
+ lastValidIndex = i;
503
+ }
504
+ break;
505
+ }
506
+ }
507
+ }
508
+ let result = input.slice(0, lastValidIndex + 1);
509
+ for (let i = stack.length - 1; i >= 0; i--) {
510
+ const state = stack[i];
511
+ switch (state) {
512
+ case "INSIDE_STRING": {
513
+ result += '"';
514
+ break;
515
+ }
516
+ case "INSIDE_OBJECT_KEY":
517
+ case "INSIDE_OBJECT_AFTER_KEY":
518
+ case "INSIDE_OBJECT_AFTER_COMMA":
519
+ case "INSIDE_OBJECT_START":
520
+ case "INSIDE_OBJECT_BEFORE_VALUE":
521
+ case "INSIDE_OBJECT_AFTER_VALUE": {
522
+ result += "}";
523
+ break;
524
+ }
525
+ case "INSIDE_ARRAY_START":
526
+ case "INSIDE_ARRAY_AFTER_COMMA":
527
+ case "INSIDE_ARRAY_AFTER_VALUE": {
528
+ result += "]";
529
+ break;
530
+ }
531
+ case "INSIDE_LITERAL": {
532
+ const partialLiteral = input.substring(literalStart, input.length);
533
+ if ("true".startsWith(partialLiteral)) {
534
+ result += "true".slice(partialLiteral.length);
535
+ } else if ("false".startsWith(partialLiteral)) {
536
+ result += "false".slice(partialLiteral.length);
537
+ } else if ("null".startsWith(partialLiteral)) {
538
+ result += "null".slice(partialLiteral.length);
539
+ }
540
+ }
541
+ }
542
+ }
543
+ return result;
544
+ }
545
+
546
+ // src/runtimes/edge/partial-json/parse-partial-json.ts
547
+ var parsePartialJson = (json) => {
548
+ try {
549
+ return _securejsonparse2.default.parse(json);
550
+ } catch (e) {
551
+ try {
552
+ return _securejsonparse2.default.parse(fixJson(json));
553
+ } catch (e2) {
554
+ return void 0;
555
+ }
556
+ }
557
+ };
558
+
559
+ // src/runtimes/edge/streams/runResultStream.ts
560
+ function runResultStream() {
561
+ let message = {
562
+ content: [],
563
+ status: { type: "running" }
564
+ };
565
+ const currentToolCall = { toolCallId: "", argsText: "" };
566
+ return new TransformStream({
567
+ transform(chunk, controller) {
568
+ const chunkType = chunk.type;
569
+ switch (chunkType) {
570
+ case "text-delta": {
571
+ message = appendOrUpdateText(message, chunk.textDelta);
572
+ controller.enqueue(message);
573
+ break;
574
+ }
575
+ case "tool-call-delta": {
576
+ const { toolCallId, toolName, argsTextDelta } = chunk;
577
+ if (currentToolCall.toolCallId !== toolCallId) {
578
+ currentToolCall.toolCallId = toolCallId;
579
+ currentToolCall.argsText = argsTextDelta;
580
+ } else {
581
+ currentToolCall.argsText += argsTextDelta;
582
+ }
583
+ message = appendOrUpdateToolCall(
584
+ message,
585
+ toolCallId,
586
+ toolName,
587
+ currentToolCall.argsText
588
+ );
589
+ controller.enqueue(message);
590
+ break;
591
+ }
592
+ case "tool-call": {
593
+ break;
594
+ }
595
+ case "tool-result": {
596
+ message = appendOrUpdateToolResult(
597
+ message,
598
+ chunk.toolCallId,
599
+ chunk.toolName,
600
+ chunk.result
601
+ );
602
+ controller.enqueue(message);
603
+ break;
604
+ }
605
+ case "finish": {
606
+ message = appendOrUpdateFinish(message, chunk);
607
+ controller.enqueue(message);
608
+ break;
609
+ }
610
+ case "error": {
611
+ if (chunk.error instanceof Error && chunk.error.name === "AbortError") {
612
+ message = appendOrUpdateCancel(message);
613
+ controller.enqueue(message);
614
+ break;
615
+ } else {
616
+ throw chunk.error;
617
+ }
618
+ }
619
+ default: {
620
+ const unhandledType = chunkType;
621
+ throw new Error(`Unhandled chunk type: ${unhandledType}`);
622
+ }
623
+ }
624
+ }
625
+ });
626
+ }
627
+ var appendOrUpdateText = (message, textDelta) => {
628
+ let contentParts = message.content;
629
+ let contentPart = message.content.at(-1);
630
+ if (_optionalChain([contentPart, 'optionalAccess', _3 => _3.type]) !== "text") {
631
+ contentPart = { type: "text", text: textDelta };
632
+ } else {
633
+ contentParts = contentParts.slice(0, -1);
634
+ contentPart = { type: "text", text: contentPart.text + textDelta };
635
+ }
636
+ return {
637
+ ...message,
638
+ content: contentParts.concat([contentPart])
639
+ };
640
+ };
641
+ var appendOrUpdateToolCall = (message, toolCallId, toolName, argsText) => {
642
+ let contentParts = message.content;
643
+ let contentPart = message.content.at(-1);
644
+ if (_optionalChain([contentPart, 'optionalAccess', _4 => _4.type]) !== "tool-call" || contentPart.toolCallId !== toolCallId) {
645
+ contentPart = {
646
+ type: "tool-call",
647
+ toolCallId,
648
+ toolName,
649
+ argsText,
650
+ args: parsePartialJson(argsText)
651
+ };
652
+ } else {
653
+ contentParts = contentParts.slice(0, -1);
654
+ contentPart = {
655
+ ...contentPart,
656
+ argsText,
657
+ args: parsePartialJson(argsText)
658
+ };
659
+ }
660
+ return {
661
+ ...message,
662
+ content: contentParts.concat([contentPart])
663
+ };
664
+ };
665
+ var appendOrUpdateToolResult = (message, toolCallId, toolName, result) => {
666
+ let found = false;
667
+ const newContentParts = message.content.map((part) => {
668
+ if (part.type !== "tool-call" || part.toolCallId !== toolCallId)
669
+ return part;
670
+ found = true;
671
+ if (part.toolName !== toolName)
672
+ throw new Error(
673
+ `Tool call ${toolCallId} found with tool name ${part.toolName}, but expected ${toolName}`
674
+ );
675
+ return {
676
+ ...part,
677
+ result
678
+ };
679
+ });
680
+ if (!found)
681
+ throw new Error(
682
+ `Received tool result for unknown tool call "${toolName}" / "${toolCallId}". This is likely an internal bug in assistant-ui.`
683
+ );
684
+ return {
685
+ ...message,
686
+ content: newContentParts
687
+ };
688
+ };
689
+ var appendOrUpdateFinish = (message, chunk) => {
690
+ const { type, ...rest } = chunk;
691
+ return {
692
+ ...message,
693
+ status: getStatus(chunk),
694
+ roundtrips: [
695
+ ..._nullishCoalesce(message.roundtrips, () => ( [])),
696
+ {
697
+ logprobs: rest.logprobs,
698
+ usage: rest.usage
699
+ }
700
+ ]
701
+ };
702
+ };
703
+ var getStatus = (chunk) => {
704
+ if (chunk.finishReason === "tool-calls") {
705
+ return {
706
+ type: "requires-action",
707
+ reason: "tool-calls"
708
+ };
709
+ } else if (chunk.finishReason === "stop" || chunk.finishReason === "unknown") {
710
+ return {
711
+ type: "complete",
712
+ reason: chunk.finishReason
713
+ };
714
+ } else {
715
+ return {
716
+ type: "incomplete",
717
+ reason: chunk.finishReason
718
+ };
719
+ }
720
+ };
721
+ var appendOrUpdateCancel = (message) => {
722
+ return {
723
+ ...message,
724
+ status: {
725
+ type: "incomplete",
726
+ reason: "cancelled"
727
+ }
728
+ };
729
+ };
730
+
731
+ // src/runtimes/edge/streams/toolResultStream.ts
732
+
733
+
734
+ function toolResultStream(tools) {
735
+ const toolCallExecutions = /* @__PURE__ */ new Map();
736
+ return new TransformStream({
737
+ transform(chunk, controller) {
738
+ controller.enqueue(chunk);
739
+ const chunkType = chunk.type;
740
+ switch (chunkType) {
741
+ case "tool-call": {
742
+ const { toolCallId, toolCallType, toolName, args: argsText } = chunk;
743
+ const tool = _optionalChain([tools, 'optionalAccess', _5 => _5[toolName]]);
744
+ if (!tool || !tool.execute) return;
745
+ const args = _securejsonparse2.default.parse(argsText);
746
+ if (tool.parameters instanceof _zod.z.ZodType) {
747
+ const result = tool.parameters.safeParse(args);
748
+ if (!result.success) {
749
+ controller.enqueue({
750
+ type: "tool-result",
751
+ toolCallType,
752
+ toolCallId,
753
+ toolName,
754
+ result: "Function parameter validation failed. " + JSON.stringify(result.error.issues),
755
+ isError: true
756
+ });
757
+ return;
758
+ } else {
759
+ toolCallExecutions.set(
760
+ toolCallId,
761
+ (async () => {
762
+ try {
763
+ const result2 = await tool.execute(args);
764
+ controller.enqueue({
765
+ type: "tool-result",
766
+ toolCallType,
767
+ toolCallId,
768
+ toolName,
769
+ result: result2
770
+ });
771
+ } catch (error) {
772
+ console.error("Error: ", error);
773
+ controller.enqueue({
774
+ type: "tool-result",
775
+ toolCallType,
776
+ toolCallId,
777
+ toolName,
778
+ result: "Error: " + error,
779
+ isError: true
780
+ });
781
+ } finally {
782
+ toolCallExecutions.delete(toolCallId);
783
+ }
784
+ })()
785
+ );
786
+ }
787
+ }
788
+ break;
789
+ }
790
+ case "text-delta":
791
+ case "tool-call-delta":
792
+ case "tool-result":
793
+ case "finish":
794
+ case "error":
795
+ break;
796
+ default: {
797
+ const unhandledType = chunkType;
798
+ throw new Error(`Unhandled chunk type: ${unhandledType}`);
799
+ }
800
+ }
801
+ },
802
+ async flush() {
803
+ await Promise.all(toolCallExecutions.values());
804
+ }
805
+ });
806
+ }
807
+
808
+
809
+
810
+
811
+
812
+
813
+
814
+
815
+
816
+
817
+
818
+ exports.LanguageModelV1CallSettingsSchema = LanguageModelV1CallSettingsSchema; exports.LanguageModelConfigSchema = LanguageModelConfigSchema; exports.mergeModelConfigs = mergeModelConfigs; exports.toLanguageModelMessages = toLanguageModelMessages; exports.toCoreMessages = toCoreMessages; exports.toCoreMessage = toCoreMessage; exports.toLanguageModelTools = toLanguageModelTools; exports.runResultStream = runResultStream; exports.toolResultStream = toolResultStream;
819
+ //# sourceMappingURL=chunk-NSPHKRLF.js.map