@assistant-ui/react 0.5.20 → 0.5.21

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