@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.
Files changed (40) hide show
  1. package/dist/AssistantTypes-D93BmqD5.d.mts +160 -0
  2. package/dist/AssistantTypes-D93BmqD5.d.ts +160 -0
  3. package/dist/Thread-BbLf1cc4.d.mts +156 -0
  4. package/dist/Thread-jfAlPLli.d.ts +156 -0
  5. package/dist/chunk-2RKUKZSZ.mjs +761 -0
  6. package/dist/chunk-2RKUKZSZ.mjs.map +1 -0
  7. package/dist/chunk-BJPOCE4O.mjs +11 -0
  8. package/dist/chunk-BJPOCE4O.mjs.map +1 -0
  9. package/dist/chunk-QBS6JLLN.mjs +63 -0
  10. package/dist/chunk-QBS6JLLN.mjs.map +1 -0
  11. package/dist/chunk-V66MVXBH.mjs +608 -0
  12. package/dist/chunk-V66MVXBH.mjs.map +1 -0
  13. package/dist/edge.d.mts +5 -90
  14. package/dist/edge.d.ts +5 -90
  15. package/dist/edge.js +1 -0
  16. package/dist/edge.js.map +1 -1
  17. package/dist/edge.mjs +49 -799
  18. package/dist/edge.mjs.map +1 -1
  19. package/dist/index.d.mts +17 -293
  20. package/dist/index.d.ts +17 -293
  21. package/dist/index.js +341 -283
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +313 -1601
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/internal.d.mts +9 -0
  26. package/dist/internal.d.ts +9 -0
  27. package/dist/internal.js +620 -0
  28. package/dist/internal.js.map +1 -0
  29. package/dist/internal.mjs +24 -0
  30. package/dist/internal.mjs.map +1 -0
  31. package/dist/styles/index.css +3 -3
  32. package/dist/styles/index.css.map +1 -1
  33. package/dist/styles/tailwindcss/thread.css +1 -1
  34. package/dist/tailwindcss/index.js +1 -0
  35. package/dist/tailwindcss/index.js.map +1 -1
  36. package/dist/tailwindcss/index.mjs +3 -0
  37. package/dist/tailwindcss/index.mjs.map +1 -1
  38. package/internal/README.md +1 -0
  39. package/internal/package.json +5 -0
  40. package/package.json +12 -1
package/dist/edge.mjs CHANGED
@@ -1,3 +1,17 @@
1
+ "use client";
2
+ import {
3
+ runResultStream,
4
+ toCoreMessage,
5
+ toLanguageModelMessages,
6
+ toLanguageModelTools,
7
+ toolResultStream
8
+ } from "./chunk-2RKUKZSZ.mjs";
9
+ import {
10
+ LanguageModelConfigSchema,
11
+ LanguageModelV1CallSettingsSchema
12
+ } from "./chunk-QBS6JLLN.mjs";
13
+ import "./chunk-BJPOCE4O.mjs";
14
+
1
15
  // src/runtimes/edge/streams/assistantEncoderStream.ts
2
16
  function assistantEncoderStream() {
3
17
  const toolCalls = /* @__PURE__ */ new Set();
@@ -69,829 +83,65 @@ function formatStreamPart(...[code, value]) {
69
83
  `;
70
84
  }
71
85
 
72
- // src/types/ModelConfigTypes.ts
73
- import { z } from "zod";
74
- var LanguageModelV1CallSettingsSchema = z.object({
75
- maxTokens: z.number().int().positive().optional(),
76
- temperature: z.number().optional(),
77
- topP: z.number().optional(),
78
- presencePenalty: z.number().optional(),
79
- frequencyPenalty: z.number().optional(),
80
- seed: z.number().int().optional(),
81
- headers: z.record(z.string().optional()).optional()
82
- });
83
- var LanguageModelConfigSchema = z.object({
84
- apiKey: z.string().optional(),
85
- baseUrl: z.string().optional(),
86
- modelName: z.string().optional()
87
- });
88
-
89
86
  // src/runtimes/edge/EdgeRuntimeRequestOptions.ts
90
- import { z as z2 } from "zod";
91
- var LanguageModelV1FunctionToolSchema = z2.object({
92
- type: z2.literal("function"),
93
- name: z2.string(),
94
- description: z2.string().optional(),
95
- parameters: z2.custom(
87
+ import { z } from "zod";
88
+ var LanguageModelV1FunctionToolSchema = z.object({
89
+ type: z.literal("function"),
90
+ name: z.string(),
91
+ description: z.string().optional(),
92
+ parameters: z.custom(
96
93
  (val) => typeof val === "object" && val !== null
97
94
  )
98
95
  });
99
- var TextContentPartSchema = z2.object({
100
- type: z2.literal("text"),
101
- text: z2.string()
96
+ var TextContentPartSchema = z.object({
97
+ type: z.literal("text"),
98
+ text: z.string()
102
99
  });
103
- var ImageContentPartSchema = z2.object({
104
- type: z2.literal("image"),
105
- image: z2.string()
100
+ var ImageContentPartSchema = z.object({
101
+ type: z.literal("image"),
102
+ image: z.string()
106
103
  });
107
- var CoreToolCallContentPartSchema = z2.object({
108
- type: z2.literal("tool-call"),
109
- toolCallId: z2.string(),
110
- toolName: z2.string(),
111
- args: z2.record(z2.unknown()),
112
- result: z2.unknown().optional(),
113
- isError: z2.boolean().optional()
104
+ var CoreToolCallContentPartSchema = z.object({
105
+ type: z.literal("tool-call"),
106
+ toolCallId: z.string(),
107
+ toolName: z.string(),
108
+ args: z.record(z.unknown()),
109
+ result: z.unknown().optional(),
110
+ isError: z.boolean().optional()
114
111
  });
115
- var CoreUserMessageSchema = z2.object({
116
- role: z2.literal("user"),
117
- content: z2.array(
118
- z2.discriminatedUnion("type", [
112
+ var CoreUserMessageSchema = z.object({
113
+ role: z.literal("user"),
114
+ content: z.array(
115
+ z.discriminatedUnion("type", [
119
116
  TextContentPartSchema,
120
117
  ImageContentPartSchema
121
118
  ])
122
119
  ).min(1)
123
120
  });
124
- var CoreAssistantMessageSchema = z2.object({
125
- role: z2.literal("assistant"),
126
- content: z2.array(
127
- z2.discriminatedUnion("type", [
121
+ var CoreAssistantMessageSchema = z.object({
122
+ role: z.literal("assistant"),
123
+ content: z.array(
124
+ z.discriminatedUnion("type", [
128
125
  TextContentPartSchema,
129
126
  CoreToolCallContentPartSchema
130
127
  ])
131
128
  ).min(1)
132
129
  });
133
- var CoreSystemMessageSchema = z2.object({
134
- role: z2.literal("system"),
135
- content: z2.tuple([TextContentPartSchema])
130
+ var CoreSystemMessageSchema = z.object({
131
+ role: z.literal("system"),
132
+ content: z.tuple([TextContentPartSchema])
136
133
  });
137
- var CoreMessageSchema = z2.discriminatedUnion("role", [
134
+ var CoreMessageSchema = z.discriminatedUnion("role", [
138
135
  CoreSystemMessageSchema,
139
136
  CoreUserMessageSchema,
140
137
  CoreAssistantMessageSchema
141
138
  ]);
142
- var EdgeRuntimeRequestOptionsSchema = z2.object({
143
- system: z2.string().optional(),
144
- messages: z2.array(CoreMessageSchema).min(1),
145
- tools: z2.array(LanguageModelV1FunctionToolSchema).optional()
139
+ var EdgeRuntimeRequestOptionsSchema = z.object({
140
+ system: z.string().optional(),
141
+ messages: z.array(CoreMessageSchema).min(1),
142
+ tools: z.array(LanguageModelV1FunctionToolSchema).optional()
146
143
  }).merge(LanguageModelV1CallSettingsSchema).merge(LanguageModelConfigSchema);
147
144
 
148
- // src/runtimes/edge/converters/toLanguageModelMessages.ts
149
- var assistantMessageSplitter = () => {
150
- const stash = [];
151
- let assistantMessage = {
152
- role: "assistant",
153
- content: []
154
- };
155
- let toolMessage = {
156
- role: "tool",
157
- content: []
158
- };
159
- return {
160
- addTextContentPart: (part) => {
161
- if (toolMessage.content.length > 0) {
162
- stash.push(assistantMessage);
163
- stash.push(toolMessage);
164
- assistantMessage = {
165
- role: "assistant",
166
- content: []
167
- };
168
- toolMessage = {
169
- role: "tool",
170
- content: []
171
- };
172
- }
173
- assistantMessage.content.push(part);
174
- },
175
- addToolCallPart: (part) => {
176
- assistantMessage.content.push({
177
- type: "tool-call",
178
- toolCallId: part.toolCallId,
179
- toolName: part.toolName,
180
- args: part.args
181
- });
182
- if (part.result) {
183
- toolMessage.content.push({
184
- type: "tool-result",
185
- toolCallId: part.toolCallId,
186
- toolName: part.toolName,
187
- result: part.result
188
- // isError
189
- });
190
- }
191
- },
192
- getMessages: () => {
193
- if (toolMessage.content.length > 0) {
194
- return [...stash, assistantMessage, toolMessage];
195
- }
196
- return [...stash, assistantMessage];
197
- }
198
- };
199
- };
200
- function toLanguageModelMessages(message) {
201
- return message.flatMap((message2) => {
202
- const role = message2.role;
203
- switch (role) {
204
- case "system": {
205
- return [{ role: "system", content: message2.content[0].text }];
206
- }
207
- case "user": {
208
- const msg = {
209
- role: "user",
210
- content: message2.content.map(
211
- (part) => {
212
- const type = part.type;
213
- switch (type) {
214
- case "text": {
215
- return part;
216
- }
217
- case "image": {
218
- return {
219
- type: "image",
220
- image: new URL(part.image)
221
- };
222
- }
223
- default: {
224
- const unhandledType = type;
225
- throw new Error(
226
- `Unspported content part type: ${unhandledType}`
227
- );
228
- }
229
- }
230
- }
231
- )
232
- };
233
- return [msg];
234
- }
235
- case "assistant": {
236
- const splitter = assistantMessageSplitter();
237
- for (const part of message2.content) {
238
- const type = part.type;
239
- switch (type) {
240
- case "text": {
241
- splitter.addTextContentPart(part);
242
- break;
243
- }
244
- case "tool-call": {
245
- splitter.addToolCallPart(part);
246
- break;
247
- }
248
- default: {
249
- const unhandledType = type;
250
- throw new Error(`Unhandled content part type: ${unhandledType}`);
251
- }
252
- }
253
- }
254
- return splitter.getMessages();
255
- }
256
- default: {
257
- const unhandledRole = role;
258
- throw new Error(`Unknown message role: ${unhandledRole}`);
259
- }
260
- }
261
- });
262
- }
263
-
264
- // src/runtimes/edge/converters/toLanguageModelTools.ts
265
- import { z as z3 } from "zod";
266
- import zodToJsonSchema from "zod-to-json-schema";
267
- var toLanguageModelTools = (tools) => {
268
- return Object.entries(tools).map(([name, tool]) => ({
269
- type: "function",
270
- name,
271
- ...tool.description ? { description: tool.description } : void 0,
272
- parameters: tool.parameters instanceof z3.ZodType ? zodToJsonSchema(tool.parameters) : tool.parameters
273
- }));
274
- };
275
-
276
- // src/runtimes/edge/streams/toolResultStream.ts
277
- import { z as z4 } from "zod";
278
- import sjson from "secure-json-parse";
279
- function toolResultStream(tools) {
280
- const toolCallExecutions = /* @__PURE__ */ new Map();
281
- return new TransformStream({
282
- transform(chunk, controller) {
283
- controller.enqueue(chunk);
284
- const chunkType = chunk.type;
285
- switch (chunkType) {
286
- case "tool-call": {
287
- const { toolCallId, toolCallType, toolName, args: argsText } = chunk;
288
- const tool = tools?.[toolName];
289
- if (!tool || !tool.execute) return;
290
- const args = sjson.parse(argsText);
291
- if (tool.parameters instanceof z4.ZodType) {
292
- const result = tool.parameters.safeParse(args);
293
- if (!result.success) {
294
- controller.enqueue({
295
- type: "tool-result",
296
- toolCallType,
297
- toolCallId,
298
- toolName,
299
- result: "Function parameter validation failed. " + JSON.stringify(result.error.issues),
300
- isError: true
301
- });
302
- return;
303
- } else {
304
- toolCallExecutions.set(
305
- toolCallId,
306
- (async () => {
307
- try {
308
- const result2 = await tool.execute(args);
309
- controller.enqueue({
310
- type: "tool-result",
311
- toolCallType,
312
- toolCallId,
313
- toolName,
314
- result: result2
315
- });
316
- } catch (error) {
317
- console.error("Error: ", error);
318
- controller.enqueue({
319
- type: "tool-result",
320
- toolCallType,
321
- toolCallId,
322
- toolName,
323
- result: "Error: " + error,
324
- isError: true
325
- });
326
- } finally {
327
- toolCallExecutions.delete(toolCallId);
328
- }
329
- })()
330
- );
331
- }
332
- }
333
- break;
334
- }
335
- case "text-delta":
336
- case "tool-call-delta":
337
- case "tool-result":
338
- case "finish":
339
- case "error":
340
- break;
341
- default: {
342
- const unhandledType = chunkType;
343
- throw new Error(`Unhandled chunk type: ${unhandledType}`);
344
- }
345
- }
346
- },
347
- async flush() {
348
- await Promise.all(toolCallExecutions.values());
349
- }
350
- });
351
- }
352
-
353
- // src/runtimes/edge/partial-json/parse-partial-json.ts
354
- import sjson2 from "secure-json-parse";
355
-
356
- // src/runtimes/edge/partial-json/fix-json.ts
357
- function fixJson(input) {
358
- const stack = ["ROOT"];
359
- let lastValidIndex = -1;
360
- let literalStart = null;
361
- function processValueStart(char, i, swapState) {
362
- {
363
- switch (char) {
364
- case '"': {
365
- lastValidIndex = i;
366
- stack.pop();
367
- stack.push(swapState);
368
- stack.push("INSIDE_STRING");
369
- break;
370
- }
371
- case "f":
372
- case "t":
373
- case "n": {
374
- lastValidIndex = i;
375
- literalStart = i;
376
- stack.pop();
377
- stack.push(swapState);
378
- stack.push("INSIDE_LITERAL");
379
- break;
380
- }
381
- case "-": {
382
- stack.pop();
383
- stack.push(swapState);
384
- stack.push("INSIDE_NUMBER");
385
- break;
386
- }
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
- stack.pop();
399
- stack.push(swapState);
400
- stack.push("INSIDE_NUMBER");
401
- break;
402
- }
403
- case "{": {
404
- lastValidIndex = i;
405
- stack.pop();
406
- stack.push(swapState);
407
- stack.push("INSIDE_OBJECT_START");
408
- break;
409
- }
410
- case "[": {
411
- lastValidIndex = i;
412
- stack.pop();
413
- stack.push(swapState);
414
- stack.push("INSIDE_ARRAY_START");
415
- break;
416
- }
417
- }
418
- }
419
- }
420
- function processAfterObjectValue(char, i) {
421
- switch (char) {
422
- case ",": {
423
- stack.pop();
424
- stack.push("INSIDE_OBJECT_AFTER_COMMA");
425
- break;
426
- }
427
- case "}": {
428
- lastValidIndex = i;
429
- stack.pop();
430
- break;
431
- }
432
- }
433
- }
434
- function processAfterArrayValue(char, i) {
435
- switch (char) {
436
- case ",": {
437
- stack.pop();
438
- stack.push("INSIDE_ARRAY_AFTER_COMMA");
439
- break;
440
- }
441
- case "]": {
442
- lastValidIndex = i;
443
- stack.pop();
444
- break;
445
- }
446
- }
447
- }
448
- for (let i = 0; i < input.length; i++) {
449
- const char = input[i];
450
- const currentState = stack[stack.length - 1];
451
- switch (currentState) {
452
- case "ROOT":
453
- processValueStart(char, i, "FINISH");
454
- break;
455
- case "INSIDE_OBJECT_START": {
456
- switch (char) {
457
- case '"': {
458
- stack.pop();
459
- stack.push("INSIDE_OBJECT_KEY");
460
- break;
461
- }
462
- case "}": {
463
- lastValidIndex = i;
464
- stack.pop();
465
- break;
466
- }
467
- }
468
- break;
469
- }
470
- case "INSIDE_OBJECT_AFTER_COMMA": {
471
- switch (char) {
472
- case '"': {
473
- stack.pop();
474
- stack.push("INSIDE_OBJECT_KEY");
475
- break;
476
- }
477
- }
478
- break;
479
- }
480
- case "INSIDE_OBJECT_KEY": {
481
- switch (char) {
482
- case '"': {
483
- stack.pop();
484
- stack.push("INSIDE_OBJECT_AFTER_KEY");
485
- break;
486
- }
487
- }
488
- break;
489
- }
490
- case "INSIDE_OBJECT_AFTER_KEY": {
491
- switch (char) {
492
- case ":": {
493
- stack.pop();
494
- stack.push("INSIDE_OBJECT_BEFORE_VALUE");
495
- break;
496
- }
497
- }
498
- break;
499
- }
500
- case "INSIDE_OBJECT_BEFORE_VALUE": {
501
- processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
502
- break;
503
- }
504
- case "INSIDE_OBJECT_AFTER_VALUE": {
505
- processAfterObjectValue(char, i);
506
- break;
507
- }
508
- case "INSIDE_STRING": {
509
- switch (char) {
510
- case '"': {
511
- stack.pop();
512
- lastValidIndex = i;
513
- break;
514
- }
515
- case "\\": {
516
- stack.push("INSIDE_STRING_ESCAPE");
517
- break;
518
- }
519
- default: {
520
- lastValidIndex = i;
521
- }
522
- }
523
- break;
524
- }
525
- case "INSIDE_ARRAY_START": {
526
- switch (char) {
527
- case "]": {
528
- lastValidIndex = i;
529
- stack.pop();
530
- break;
531
- }
532
- default: {
533
- lastValidIndex = i;
534
- processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
535
- break;
536
- }
537
- }
538
- break;
539
- }
540
- case "INSIDE_ARRAY_AFTER_VALUE": {
541
- switch (char) {
542
- case ",": {
543
- stack.pop();
544
- stack.push("INSIDE_ARRAY_AFTER_COMMA");
545
- break;
546
- }
547
- case "]": {
548
- lastValidIndex = i;
549
- stack.pop();
550
- break;
551
- }
552
- default: {
553
- lastValidIndex = i;
554
- break;
555
- }
556
- }
557
- break;
558
- }
559
- case "INSIDE_ARRAY_AFTER_COMMA": {
560
- processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
561
- break;
562
- }
563
- case "INSIDE_STRING_ESCAPE": {
564
- stack.pop();
565
- lastValidIndex = i;
566
- break;
567
- }
568
- case "INSIDE_NUMBER": {
569
- switch (char) {
570
- case "0":
571
- case "1":
572
- case "2":
573
- case "3":
574
- case "4":
575
- case "5":
576
- case "6":
577
- case "7":
578
- case "8":
579
- case "9": {
580
- lastValidIndex = i;
581
- break;
582
- }
583
- case "e":
584
- case "E":
585
- case "-":
586
- case ".": {
587
- break;
588
- }
589
- case ",": {
590
- stack.pop();
591
- if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
592
- processAfterArrayValue(char, i);
593
- }
594
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
595
- processAfterObjectValue(char, i);
596
- }
597
- break;
598
- }
599
- case "}": {
600
- stack.pop();
601
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
602
- processAfterObjectValue(char, i);
603
- }
604
- break;
605
- }
606
- case "]": {
607
- stack.pop();
608
- if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
609
- processAfterArrayValue(char, i);
610
- }
611
- break;
612
- }
613
- default: {
614
- stack.pop();
615
- break;
616
- }
617
- }
618
- break;
619
- }
620
- case "INSIDE_LITERAL": {
621
- const partialLiteral = input.substring(literalStart, i + 1);
622
- if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
623
- stack.pop();
624
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
625
- processAfterObjectValue(char, i);
626
- } else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
627
- processAfterArrayValue(char, i);
628
- }
629
- } else {
630
- lastValidIndex = i;
631
- }
632
- break;
633
- }
634
- }
635
- }
636
- let result = input.slice(0, lastValidIndex + 1);
637
- for (let i = stack.length - 1; i >= 0; i--) {
638
- const state = stack[i];
639
- switch (state) {
640
- case "INSIDE_STRING": {
641
- result += '"';
642
- break;
643
- }
644
- case "INSIDE_OBJECT_KEY":
645
- case "INSIDE_OBJECT_AFTER_KEY":
646
- case "INSIDE_OBJECT_AFTER_COMMA":
647
- case "INSIDE_OBJECT_START":
648
- case "INSIDE_OBJECT_BEFORE_VALUE":
649
- case "INSIDE_OBJECT_AFTER_VALUE": {
650
- result += "}";
651
- break;
652
- }
653
- case "INSIDE_ARRAY_START":
654
- case "INSIDE_ARRAY_AFTER_COMMA":
655
- case "INSIDE_ARRAY_AFTER_VALUE": {
656
- result += "]";
657
- break;
658
- }
659
- case "INSIDE_LITERAL": {
660
- const partialLiteral = input.substring(literalStart, input.length);
661
- if ("true".startsWith(partialLiteral)) {
662
- result += "true".slice(partialLiteral.length);
663
- } else if ("false".startsWith(partialLiteral)) {
664
- result += "false".slice(partialLiteral.length);
665
- } else if ("null".startsWith(partialLiteral)) {
666
- result += "null".slice(partialLiteral.length);
667
- }
668
- }
669
- }
670
- }
671
- return result;
672
- }
673
-
674
- // src/runtimes/edge/partial-json/parse-partial-json.ts
675
- var parsePartialJson = (json) => {
676
- try {
677
- return sjson2.parse(json);
678
- } catch {
679
- try {
680
- return sjson2.parse(fixJson(json));
681
- } catch {
682
- return void 0;
683
- }
684
- }
685
- };
686
-
687
- // src/runtimes/edge/streams/runResultStream.ts
688
- function runResultStream() {
689
- let message = {
690
- content: [],
691
- status: { type: "running" }
692
- };
693
- const currentToolCall = { toolCallId: "", argsText: "" };
694
- return new TransformStream({
695
- transform(chunk, controller) {
696
- const chunkType = chunk.type;
697
- switch (chunkType) {
698
- case "text-delta": {
699
- message = appendOrUpdateText(message, chunk.textDelta);
700
- controller.enqueue(message);
701
- break;
702
- }
703
- case "tool-call-delta": {
704
- const { toolCallId, toolName, argsTextDelta } = chunk;
705
- if (currentToolCall.toolCallId !== toolCallId) {
706
- currentToolCall.toolCallId = toolCallId;
707
- currentToolCall.argsText = argsTextDelta;
708
- } else {
709
- currentToolCall.argsText += argsTextDelta;
710
- }
711
- message = appendOrUpdateToolCall(
712
- message,
713
- toolCallId,
714
- toolName,
715
- currentToolCall.argsText
716
- );
717
- controller.enqueue(message);
718
- break;
719
- }
720
- case "tool-call": {
721
- break;
722
- }
723
- case "tool-result": {
724
- message = appendOrUpdateToolResult(
725
- message,
726
- chunk.toolCallId,
727
- chunk.toolName,
728
- chunk.result
729
- );
730
- controller.enqueue(message);
731
- break;
732
- }
733
- case "finish": {
734
- message = appendOrUpdateFinish(message, chunk);
735
- controller.enqueue(message);
736
- break;
737
- }
738
- case "error": {
739
- if (chunk.error instanceof Error && chunk.error.name === "AbortError") {
740
- message = appendOrUpdateCancel(message);
741
- controller.enqueue(message);
742
- break;
743
- } else {
744
- throw chunk.error;
745
- }
746
- }
747
- default: {
748
- const unhandledType = chunkType;
749
- throw new Error(`Unhandled chunk type: ${unhandledType}`);
750
- }
751
- }
752
- }
753
- });
754
- }
755
- var appendOrUpdateText = (message, textDelta) => {
756
- let contentParts = message.content;
757
- let contentPart = message.content.at(-1);
758
- if (contentPart?.type !== "text") {
759
- contentPart = { type: "text", text: textDelta };
760
- } else {
761
- contentParts = contentParts.slice(0, -1);
762
- contentPart = { type: "text", text: contentPart.text + textDelta };
763
- }
764
- return {
765
- ...message,
766
- content: contentParts.concat([contentPart])
767
- };
768
- };
769
- var appendOrUpdateToolCall = (message, toolCallId, toolName, argsText) => {
770
- let contentParts = message.content;
771
- let contentPart = message.content.at(-1);
772
- if (contentPart?.type !== "tool-call" || contentPart.toolCallId !== toolCallId) {
773
- contentPart = {
774
- type: "tool-call",
775
- toolCallId,
776
- toolName,
777
- argsText,
778
- args: parsePartialJson(argsText)
779
- };
780
- } else {
781
- contentParts = contentParts.slice(0, -1);
782
- contentPart = {
783
- ...contentPart,
784
- argsText,
785
- args: parsePartialJson(argsText)
786
- };
787
- }
788
- return {
789
- ...message,
790
- content: contentParts.concat([contentPart])
791
- };
792
- };
793
- var appendOrUpdateToolResult = (message, toolCallId, toolName, result) => {
794
- let found = false;
795
- const newContentParts = message.content.map((part) => {
796
- if (part.type !== "tool-call" || part.toolCallId !== toolCallId)
797
- return part;
798
- found = true;
799
- if (part.toolName !== toolName)
800
- throw new Error(
801
- `Tool call ${toolCallId} found with tool name ${part.toolName}, but expected ${toolName}`
802
- );
803
- return {
804
- ...part,
805
- result
806
- };
807
- });
808
- if (!found)
809
- throw new Error(
810
- `Received tool result for unknown tool call "${toolName}" / "${toolCallId}". This is likely an internal bug in assistant-ui.`
811
- );
812
- return {
813
- ...message,
814
- content: newContentParts
815
- };
816
- };
817
- var appendOrUpdateFinish = (message, chunk) => {
818
- const { type, ...rest } = chunk;
819
- return {
820
- ...message,
821
- status: getStatus(chunk),
822
- roundtrips: [
823
- ...message.roundtrips ?? [],
824
- {
825
- logprobs: rest.logprobs,
826
- usage: rest.usage
827
- }
828
- ]
829
- };
830
- };
831
- var getStatus = (chunk) => {
832
- if (chunk.finishReason === "tool-calls") {
833
- return {
834
- type: "requires-action",
835
- reason: "tool-calls"
836
- };
837
- } else if (chunk.finishReason === "stop" || chunk.finishReason === "unknown") {
838
- return {
839
- type: "complete",
840
- reason: chunk.finishReason
841
- };
842
- } else {
843
- return {
844
- type: "incomplete",
845
- reason: chunk.finishReason
846
- };
847
- }
848
- };
849
- var appendOrUpdateCancel = (message) => {
850
- return {
851
- ...message,
852
- status: {
853
- type: "incomplete",
854
- reason: "cancelled"
855
- }
856
- };
857
- };
858
-
859
- // src/runtimes/edge/converters/toCoreMessages.ts
860
- var toCoreMessage = (message) => {
861
- const role = message.role;
862
- switch (role) {
863
- case "assistant":
864
- return {
865
- role,
866
- content: message.content.map((part) => {
867
- if (part.type === "ui") throw new Error("UI parts are not supported");
868
- if (part.type === "tool-call") {
869
- const { argsText, ...rest } = part;
870
- return rest;
871
- }
872
- return part;
873
- })
874
- };
875
- case "user":
876
- return {
877
- role,
878
- content: message.content.map((part) => {
879
- if (part.type === "ui") throw new Error("UI parts are not supported");
880
- return part;
881
- })
882
- };
883
- case "system":
884
- return {
885
- role,
886
- content: message.content
887
- };
888
- default: {
889
- const unsupportedRole = role;
890
- throw new Error(`Unknown message role: ${unsupportedRole}`);
891
- }
892
- }
893
- };
894
-
895
145
  // src/runtimes/edge/createEdgeRuntimeAPI.ts
896
146
  var voidStream = () => {
897
147
  return new WritableStream({