@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.
package/dist/index.mjs CHANGED
@@ -1,9 +1,16 @@
1
1
  "use client";
2
- var __defProp = Object.defineProperty;
3
- var __export = (target, all) => {
4
- for (var name in all)
5
- __defProp(target, name, { get: all[name], enumerable: true });
6
- };
2
+ import {
3
+ mergeModelConfigs,
4
+ runResultStream,
5
+ toCoreMessage,
6
+ toCoreMessages,
7
+ toLanguageModelMessages,
8
+ toLanguageModelTools,
9
+ toolResultStream
10
+ } from "./chunk-ZWRFAYHH.mjs";
11
+ import {
12
+ __export
13
+ } from "./chunk-BJPOCE4O.mjs";
7
14
 
8
15
  // src/context/providers/AssistantRuntimeProvider.tsx
9
16
  import { memo } from "react";
@@ -28,61 +35,6 @@ function useAssistantContext(options) {
28
35
  // src/context/stores/AssistantModelConfig.ts
29
36
  import { create } from "zustand";
30
37
 
31
- // src/types/ModelConfigTypes.ts
32
- import { z } from "zod";
33
- var LanguageModelV1CallSettingsSchema = z.object({
34
- maxTokens: z.number().int().positive().optional(),
35
- temperature: z.number().optional(),
36
- topP: z.number().optional(),
37
- presencePenalty: z.number().optional(),
38
- frequencyPenalty: z.number().optional(),
39
- seed: z.number().int().optional(),
40
- headers: z.record(z.string().optional()).optional()
41
- });
42
- var LanguageModelConfigSchema = z.object({
43
- apiKey: z.string().optional(),
44
- baseUrl: z.string().optional(),
45
- modelName: z.string().optional()
46
- });
47
- var mergeModelConfigs = (configSet) => {
48
- const configs = Array.from(configSet).map((c) => c.getModelConfig()).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
49
- return configs.reduce((acc, config) => {
50
- if (config.system) {
51
- if (acc.system) {
52
- acc.system += `
53
-
54
- ${config.system}`;
55
- } else {
56
- acc.system = config.system;
57
- }
58
- }
59
- if (config.tools) {
60
- for (const [name, tool] of Object.entries(config.tools)) {
61
- if (acc.tools?.[name]) {
62
- throw new Error(
63
- `You tried to define a tool with the name ${name}, but it already exists.`
64
- );
65
- }
66
- if (!acc.tools) acc.tools = {};
67
- acc.tools[name] = tool;
68
- }
69
- }
70
- if (config.config) {
71
- acc.config = {
72
- ...acc.config,
73
- ...config.config
74
- };
75
- }
76
- if (config.callSettings) {
77
- acc.callSettings = {
78
- ...acc.callSettings,
79
- ...config.callSettings
80
- };
81
- }
82
- return acc;
83
- }, {});
84
- };
85
-
86
38
  // src/utils/ProxyConfigProvider.ts
87
39
  var ProxyConfigProvider = class {
88
40
  _providers = /* @__PURE__ */ new Set();
@@ -2419,122 +2371,6 @@ var TooltipIconButton = forwardRef18(({ children, tooltip, side = "bottom", ...r
2419
2371
  });
2420
2372
  TooltipIconButton.displayName = "TooltipIconButton";
2421
2373
 
2422
- // src/runtimes/edge/converters/toLanguageModelMessages.ts
2423
- var assistantMessageSplitter = () => {
2424
- const stash = [];
2425
- let assistantMessage = {
2426
- role: "assistant",
2427
- content: []
2428
- };
2429
- let toolMessage = {
2430
- role: "tool",
2431
- content: []
2432
- };
2433
- return {
2434
- addTextContentPart: (part) => {
2435
- if (toolMessage.content.length > 0) {
2436
- stash.push(assistantMessage);
2437
- stash.push(toolMessage);
2438
- assistantMessage = {
2439
- role: "assistant",
2440
- content: []
2441
- };
2442
- toolMessage = {
2443
- role: "tool",
2444
- content: []
2445
- };
2446
- }
2447
- assistantMessage.content.push(part);
2448
- },
2449
- addToolCallPart: (part) => {
2450
- assistantMessage.content.push({
2451
- type: "tool-call",
2452
- toolCallId: part.toolCallId,
2453
- toolName: part.toolName,
2454
- args: part.args
2455
- });
2456
- if (part.result) {
2457
- toolMessage.content.push({
2458
- type: "tool-result",
2459
- toolCallId: part.toolCallId,
2460
- toolName: part.toolName,
2461
- result: part.result
2462
- // isError
2463
- });
2464
- }
2465
- },
2466
- getMessages: () => {
2467
- if (toolMessage.content.length > 0) {
2468
- return [...stash, assistantMessage, toolMessage];
2469
- }
2470
- return [...stash, assistantMessage];
2471
- }
2472
- };
2473
- };
2474
- function toLanguageModelMessages(message) {
2475
- return message.flatMap((message2) => {
2476
- const role = message2.role;
2477
- switch (role) {
2478
- case "system": {
2479
- return [{ role: "system", content: message2.content[0].text }];
2480
- }
2481
- case "user": {
2482
- const msg = {
2483
- role: "user",
2484
- content: message2.content.map(
2485
- (part) => {
2486
- const type = part.type;
2487
- switch (type) {
2488
- case "text": {
2489
- return part;
2490
- }
2491
- case "image": {
2492
- return {
2493
- type: "image",
2494
- image: new URL(part.image)
2495
- };
2496
- }
2497
- default: {
2498
- const unhandledType = type;
2499
- throw new Error(
2500
- `Unspported content part type: ${unhandledType}`
2501
- );
2502
- }
2503
- }
2504
- }
2505
- )
2506
- };
2507
- return [msg];
2508
- }
2509
- case "assistant": {
2510
- const splitter = assistantMessageSplitter();
2511
- for (const part of message2.content) {
2512
- const type = part.type;
2513
- switch (type) {
2514
- case "text": {
2515
- splitter.addTextContentPart(part);
2516
- break;
2517
- }
2518
- case "tool-call": {
2519
- splitter.addToolCallPart(part);
2520
- break;
2521
- }
2522
- default: {
2523
- const unhandledType = type;
2524
- throw new Error(`Unhandled content part type: ${unhandledType}`);
2525
- }
2526
- }
2527
- }
2528
- return splitter.getMessages();
2529
- }
2530
- default: {
2531
- const unhandledRole = role;
2532
- throw new Error(`Unknown message role: ${unhandledRole}`);
2533
- }
2534
- }
2535
- });
2536
- }
2537
-
2538
2374
  // src/runtimes/edge/converters/fromLanguageModelMessages.ts
2539
2375
  var fromLanguageModelMessages = (lm, { mergeRoundtrips }) => {
2540
2376
  const messages = [];
@@ -2639,45 +2475,6 @@ var fromLanguageModelMessages = (lm, { mergeRoundtrips }) => {
2639
2475
  return messages;
2640
2476
  };
2641
2477
 
2642
- // src/runtimes/edge/converters/toCoreMessages.ts
2643
- var toCoreMessages = (message) => {
2644
- return message.map(toCoreMessage);
2645
- };
2646
- var toCoreMessage = (message) => {
2647
- const role = message.role;
2648
- switch (role) {
2649
- case "assistant":
2650
- return {
2651
- role,
2652
- content: message.content.map((part) => {
2653
- if (part.type === "ui") throw new Error("UI parts are not supported");
2654
- if (part.type === "tool-call") {
2655
- const { argsText, ...rest } = part;
2656
- return rest;
2657
- }
2658
- return part;
2659
- })
2660
- };
2661
- case "user":
2662
- return {
2663
- role,
2664
- content: message.content.map((part) => {
2665
- if (part.type === "ui") throw new Error("UI parts are not supported");
2666
- return part;
2667
- })
2668
- };
2669
- case "system":
2670
- return {
2671
- role,
2672
- content: message.content
2673
- };
2674
- default: {
2675
- const unsupportedRole = role;
2676
- throw new Error(`Unknown message role: ${unsupportedRole}`);
2677
- }
2678
- }
2679
- };
2680
-
2681
2478
  // src/runtimes/edge/converters/fromLanguageModelTools.ts
2682
2479
  var fromLanguageModelTools = (tools) => {
2683
2480
  return Object.fromEntries(
@@ -2691,18 +2488,6 @@ var fromLanguageModelTools = (tools) => {
2691
2488
  );
2692
2489
  };
2693
2490
 
2694
- // src/runtimes/edge/converters/toLanguageModelTools.ts
2695
- import { z as z2 } from "zod";
2696
- import zodToJsonSchema from "zod-to-json-schema";
2697
- var toLanguageModelTools = (tools) => {
2698
- return Object.entries(tools).map(([name, tool]) => ({
2699
- type: "function",
2700
- name,
2701
- ...tool.description ? { description: tool.description } : void 0,
2702
- parameters: tool.parameters instanceof z2.ZodType ? zodToJsonSchema(tool.parameters) : tool.parameters
2703
- }));
2704
- };
2705
-
2706
2491
  // src/runtimes/edge/useEdgeRuntime.ts
2707
2492
  import { useState as useState8 } from "react";
2708
2493
 
@@ -2810,589 +2595,6 @@ function chunkByLineStream() {
2810
2595
  });
2811
2596
  }
2812
2597
 
2813
- // src/runtimes/edge/partial-json/parse-partial-json.ts
2814
- import sjson from "secure-json-parse";
2815
-
2816
- // src/runtimes/edge/partial-json/fix-json.ts
2817
- function fixJson(input) {
2818
- const stack = ["ROOT"];
2819
- let lastValidIndex = -1;
2820
- let literalStart = null;
2821
- function processValueStart(char, i, swapState) {
2822
- {
2823
- switch (char) {
2824
- case '"': {
2825
- lastValidIndex = i;
2826
- stack.pop();
2827
- stack.push(swapState);
2828
- stack.push("INSIDE_STRING");
2829
- break;
2830
- }
2831
- case "f":
2832
- case "t":
2833
- case "n": {
2834
- lastValidIndex = i;
2835
- literalStart = i;
2836
- stack.pop();
2837
- stack.push(swapState);
2838
- stack.push("INSIDE_LITERAL");
2839
- break;
2840
- }
2841
- case "-": {
2842
- stack.pop();
2843
- stack.push(swapState);
2844
- stack.push("INSIDE_NUMBER");
2845
- break;
2846
- }
2847
- case "0":
2848
- case "1":
2849
- case "2":
2850
- case "3":
2851
- case "4":
2852
- case "5":
2853
- case "6":
2854
- case "7":
2855
- case "8":
2856
- case "9": {
2857
- lastValidIndex = i;
2858
- stack.pop();
2859
- stack.push(swapState);
2860
- stack.push("INSIDE_NUMBER");
2861
- break;
2862
- }
2863
- case "{": {
2864
- lastValidIndex = i;
2865
- stack.pop();
2866
- stack.push(swapState);
2867
- stack.push("INSIDE_OBJECT_START");
2868
- break;
2869
- }
2870
- case "[": {
2871
- lastValidIndex = i;
2872
- stack.pop();
2873
- stack.push(swapState);
2874
- stack.push("INSIDE_ARRAY_START");
2875
- break;
2876
- }
2877
- }
2878
- }
2879
- }
2880
- function processAfterObjectValue(char, i) {
2881
- switch (char) {
2882
- case ",": {
2883
- stack.pop();
2884
- stack.push("INSIDE_OBJECT_AFTER_COMMA");
2885
- break;
2886
- }
2887
- case "}": {
2888
- lastValidIndex = i;
2889
- stack.pop();
2890
- break;
2891
- }
2892
- }
2893
- }
2894
- function processAfterArrayValue(char, i) {
2895
- switch (char) {
2896
- case ",": {
2897
- stack.pop();
2898
- stack.push("INSIDE_ARRAY_AFTER_COMMA");
2899
- break;
2900
- }
2901
- case "]": {
2902
- lastValidIndex = i;
2903
- stack.pop();
2904
- break;
2905
- }
2906
- }
2907
- }
2908
- for (let i = 0; i < input.length; i++) {
2909
- const char = input[i];
2910
- const currentState = stack[stack.length - 1];
2911
- switch (currentState) {
2912
- case "ROOT":
2913
- processValueStart(char, i, "FINISH");
2914
- break;
2915
- case "INSIDE_OBJECT_START": {
2916
- switch (char) {
2917
- case '"': {
2918
- stack.pop();
2919
- stack.push("INSIDE_OBJECT_KEY");
2920
- break;
2921
- }
2922
- case "}": {
2923
- lastValidIndex = i;
2924
- stack.pop();
2925
- break;
2926
- }
2927
- }
2928
- break;
2929
- }
2930
- case "INSIDE_OBJECT_AFTER_COMMA": {
2931
- switch (char) {
2932
- case '"': {
2933
- stack.pop();
2934
- stack.push("INSIDE_OBJECT_KEY");
2935
- break;
2936
- }
2937
- }
2938
- break;
2939
- }
2940
- case "INSIDE_OBJECT_KEY": {
2941
- switch (char) {
2942
- case '"': {
2943
- stack.pop();
2944
- stack.push("INSIDE_OBJECT_AFTER_KEY");
2945
- break;
2946
- }
2947
- }
2948
- break;
2949
- }
2950
- case "INSIDE_OBJECT_AFTER_KEY": {
2951
- switch (char) {
2952
- case ":": {
2953
- stack.pop();
2954
- stack.push("INSIDE_OBJECT_BEFORE_VALUE");
2955
- break;
2956
- }
2957
- }
2958
- break;
2959
- }
2960
- case "INSIDE_OBJECT_BEFORE_VALUE": {
2961
- processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
2962
- break;
2963
- }
2964
- case "INSIDE_OBJECT_AFTER_VALUE": {
2965
- processAfterObjectValue(char, i);
2966
- break;
2967
- }
2968
- case "INSIDE_STRING": {
2969
- switch (char) {
2970
- case '"': {
2971
- stack.pop();
2972
- lastValidIndex = i;
2973
- break;
2974
- }
2975
- case "\\": {
2976
- stack.push("INSIDE_STRING_ESCAPE");
2977
- break;
2978
- }
2979
- default: {
2980
- lastValidIndex = i;
2981
- }
2982
- }
2983
- break;
2984
- }
2985
- case "INSIDE_ARRAY_START": {
2986
- switch (char) {
2987
- case "]": {
2988
- lastValidIndex = i;
2989
- stack.pop();
2990
- break;
2991
- }
2992
- default: {
2993
- lastValidIndex = i;
2994
- processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
2995
- break;
2996
- }
2997
- }
2998
- break;
2999
- }
3000
- case "INSIDE_ARRAY_AFTER_VALUE": {
3001
- switch (char) {
3002
- case ",": {
3003
- stack.pop();
3004
- stack.push("INSIDE_ARRAY_AFTER_COMMA");
3005
- break;
3006
- }
3007
- case "]": {
3008
- lastValidIndex = i;
3009
- stack.pop();
3010
- break;
3011
- }
3012
- default: {
3013
- lastValidIndex = i;
3014
- break;
3015
- }
3016
- }
3017
- break;
3018
- }
3019
- case "INSIDE_ARRAY_AFTER_COMMA": {
3020
- processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
3021
- break;
3022
- }
3023
- case "INSIDE_STRING_ESCAPE": {
3024
- stack.pop();
3025
- lastValidIndex = i;
3026
- break;
3027
- }
3028
- case "INSIDE_NUMBER": {
3029
- switch (char) {
3030
- case "0":
3031
- case "1":
3032
- case "2":
3033
- case "3":
3034
- case "4":
3035
- case "5":
3036
- case "6":
3037
- case "7":
3038
- case "8":
3039
- case "9": {
3040
- lastValidIndex = i;
3041
- break;
3042
- }
3043
- case "e":
3044
- case "E":
3045
- case "-":
3046
- case ".": {
3047
- break;
3048
- }
3049
- case ",": {
3050
- stack.pop();
3051
- if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
3052
- processAfterArrayValue(char, i);
3053
- }
3054
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
3055
- processAfterObjectValue(char, i);
3056
- }
3057
- break;
3058
- }
3059
- case "}": {
3060
- stack.pop();
3061
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
3062
- processAfterObjectValue(char, i);
3063
- }
3064
- break;
3065
- }
3066
- case "]": {
3067
- stack.pop();
3068
- if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
3069
- processAfterArrayValue(char, i);
3070
- }
3071
- break;
3072
- }
3073
- default: {
3074
- stack.pop();
3075
- break;
3076
- }
3077
- }
3078
- break;
3079
- }
3080
- case "INSIDE_LITERAL": {
3081
- const partialLiteral = input.substring(literalStart, i + 1);
3082
- if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
3083
- stack.pop();
3084
- if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
3085
- processAfterObjectValue(char, i);
3086
- } else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
3087
- processAfterArrayValue(char, i);
3088
- }
3089
- } else {
3090
- lastValidIndex = i;
3091
- }
3092
- break;
3093
- }
3094
- }
3095
- }
3096
- let result = input.slice(0, lastValidIndex + 1);
3097
- for (let i = stack.length - 1; i >= 0; i--) {
3098
- const state = stack[i];
3099
- switch (state) {
3100
- case "INSIDE_STRING": {
3101
- result += '"';
3102
- break;
3103
- }
3104
- case "INSIDE_OBJECT_KEY":
3105
- case "INSIDE_OBJECT_AFTER_KEY":
3106
- case "INSIDE_OBJECT_AFTER_COMMA":
3107
- case "INSIDE_OBJECT_START":
3108
- case "INSIDE_OBJECT_BEFORE_VALUE":
3109
- case "INSIDE_OBJECT_AFTER_VALUE": {
3110
- result += "}";
3111
- break;
3112
- }
3113
- case "INSIDE_ARRAY_START":
3114
- case "INSIDE_ARRAY_AFTER_COMMA":
3115
- case "INSIDE_ARRAY_AFTER_VALUE": {
3116
- result += "]";
3117
- break;
3118
- }
3119
- case "INSIDE_LITERAL": {
3120
- const partialLiteral = input.substring(literalStart, input.length);
3121
- if ("true".startsWith(partialLiteral)) {
3122
- result += "true".slice(partialLiteral.length);
3123
- } else if ("false".startsWith(partialLiteral)) {
3124
- result += "false".slice(partialLiteral.length);
3125
- } else if ("null".startsWith(partialLiteral)) {
3126
- result += "null".slice(partialLiteral.length);
3127
- }
3128
- }
3129
- }
3130
- }
3131
- return result;
3132
- }
3133
-
3134
- // src/runtimes/edge/partial-json/parse-partial-json.ts
3135
- var parsePartialJson = (json) => {
3136
- try {
3137
- return sjson.parse(json);
3138
- } catch {
3139
- try {
3140
- return sjson.parse(fixJson(json));
3141
- } catch {
3142
- return void 0;
3143
- }
3144
- }
3145
- };
3146
-
3147
- // src/runtimes/edge/streams/runResultStream.ts
3148
- function runResultStream() {
3149
- let message = {
3150
- content: [],
3151
- status: { type: "running" }
3152
- };
3153
- const currentToolCall = { toolCallId: "", argsText: "" };
3154
- return new TransformStream({
3155
- transform(chunk, controller) {
3156
- const chunkType = chunk.type;
3157
- switch (chunkType) {
3158
- case "text-delta": {
3159
- message = appendOrUpdateText(message, chunk.textDelta);
3160
- controller.enqueue(message);
3161
- break;
3162
- }
3163
- case "tool-call-delta": {
3164
- const { toolCallId, toolName, argsTextDelta } = chunk;
3165
- if (currentToolCall.toolCallId !== toolCallId) {
3166
- currentToolCall.toolCallId = toolCallId;
3167
- currentToolCall.argsText = argsTextDelta;
3168
- } else {
3169
- currentToolCall.argsText += argsTextDelta;
3170
- }
3171
- message = appendOrUpdateToolCall(
3172
- message,
3173
- toolCallId,
3174
- toolName,
3175
- currentToolCall.argsText
3176
- );
3177
- controller.enqueue(message);
3178
- break;
3179
- }
3180
- case "tool-call": {
3181
- break;
3182
- }
3183
- case "tool-result": {
3184
- message = appendOrUpdateToolResult(
3185
- message,
3186
- chunk.toolCallId,
3187
- chunk.toolName,
3188
- chunk.result
3189
- );
3190
- controller.enqueue(message);
3191
- break;
3192
- }
3193
- case "finish": {
3194
- message = appendOrUpdateFinish(message, chunk);
3195
- controller.enqueue(message);
3196
- break;
3197
- }
3198
- case "error": {
3199
- if (chunk.error instanceof Error && chunk.error.name === "AbortError") {
3200
- message = appendOrUpdateCancel(message);
3201
- controller.enqueue(message);
3202
- break;
3203
- } else {
3204
- throw chunk.error;
3205
- }
3206
- }
3207
- default: {
3208
- const unhandledType = chunkType;
3209
- throw new Error(`Unhandled chunk type: ${unhandledType}`);
3210
- }
3211
- }
3212
- }
3213
- });
3214
- }
3215
- var appendOrUpdateText = (message, textDelta) => {
3216
- let contentParts = message.content;
3217
- let contentPart = message.content.at(-1);
3218
- if (contentPart?.type !== "text") {
3219
- contentPart = { type: "text", text: textDelta };
3220
- } else {
3221
- contentParts = contentParts.slice(0, -1);
3222
- contentPart = { type: "text", text: contentPart.text + textDelta };
3223
- }
3224
- return {
3225
- ...message,
3226
- content: contentParts.concat([contentPart])
3227
- };
3228
- };
3229
- var appendOrUpdateToolCall = (message, toolCallId, toolName, argsText) => {
3230
- let contentParts = message.content;
3231
- let contentPart = message.content.at(-1);
3232
- if (contentPart?.type !== "tool-call" || contentPart.toolCallId !== toolCallId) {
3233
- contentPart = {
3234
- type: "tool-call",
3235
- toolCallId,
3236
- toolName,
3237
- argsText,
3238
- args: parsePartialJson(argsText)
3239
- };
3240
- } else {
3241
- contentParts = contentParts.slice(0, -1);
3242
- contentPart = {
3243
- ...contentPart,
3244
- argsText,
3245
- args: parsePartialJson(argsText)
3246
- };
3247
- }
3248
- return {
3249
- ...message,
3250
- content: contentParts.concat([contentPart])
3251
- };
3252
- };
3253
- var appendOrUpdateToolResult = (message, toolCallId, toolName, result) => {
3254
- let found = false;
3255
- const newContentParts = message.content.map((part) => {
3256
- if (part.type !== "tool-call" || part.toolCallId !== toolCallId)
3257
- return part;
3258
- found = true;
3259
- if (part.toolName !== toolName)
3260
- throw new Error(
3261
- `Tool call ${toolCallId} found with tool name ${part.toolName}, but expected ${toolName}`
3262
- );
3263
- return {
3264
- ...part,
3265
- result
3266
- };
3267
- });
3268
- if (!found)
3269
- throw new Error(
3270
- `Received tool result for unknown tool call "${toolName}" / "${toolCallId}". This is likely an internal bug in assistant-ui.`
3271
- );
3272
- return {
3273
- ...message,
3274
- content: newContentParts
3275
- };
3276
- };
3277
- var appendOrUpdateFinish = (message, chunk) => {
3278
- const { type, ...rest } = chunk;
3279
- return {
3280
- ...message,
3281
- status: getStatus(chunk),
3282
- roundtrips: [
3283
- ...message.roundtrips ?? [],
3284
- {
3285
- logprobs: rest.logprobs,
3286
- usage: rest.usage
3287
- }
3288
- ]
3289
- };
3290
- };
3291
- var getStatus = (chunk) => {
3292
- if (chunk.finishReason === "tool-calls") {
3293
- return {
3294
- type: "requires-action",
3295
- reason: "tool-calls"
3296
- };
3297
- } else if (chunk.finishReason === "stop" || chunk.finishReason === "unknown") {
3298
- return {
3299
- type: "complete",
3300
- reason: chunk.finishReason
3301
- };
3302
- } else {
3303
- return {
3304
- type: "incomplete",
3305
- reason: chunk.finishReason
3306
- };
3307
- }
3308
- };
3309
- var appendOrUpdateCancel = (message) => {
3310
- return {
3311
- ...message,
3312
- status: {
3313
- type: "incomplete",
3314
- reason: "cancelled"
3315
- }
3316
- };
3317
- };
3318
-
3319
- // src/runtimes/edge/streams/toolResultStream.ts
3320
- import { z as z3 } from "zod";
3321
- import sjson2 from "secure-json-parse";
3322
- function toolResultStream(tools) {
3323
- const toolCallExecutions = /* @__PURE__ */ new Map();
3324
- return new TransformStream({
3325
- transform(chunk, controller) {
3326
- controller.enqueue(chunk);
3327
- const chunkType = chunk.type;
3328
- switch (chunkType) {
3329
- case "tool-call": {
3330
- const { toolCallId, toolCallType, toolName, args: argsText } = chunk;
3331
- const tool = tools?.[toolName];
3332
- if (!tool || !tool.execute) return;
3333
- const args = sjson2.parse(argsText);
3334
- if (tool.parameters instanceof z3.ZodType) {
3335
- const result = tool.parameters.safeParse(args);
3336
- if (!result.success) {
3337
- controller.enqueue({
3338
- type: "tool-result",
3339
- toolCallType,
3340
- toolCallId,
3341
- toolName,
3342
- result: "Function parameter validation failed. " + JSON.stringify(result.error.issues),
3343
- isError: true
3344
- });
3345
- return;
3346
- } else {
3347
- toolCallExecutions.set(
3348
- toolCallId,
3349
- (async () => {
3350
- try {
3351
- const result2 = await tool.execute(args);
3352
- controller.enqueue({
3353
- type: "tool-result",
3354
- toolCallType,
3355
- toolCallId,
3356
- toolName,
3357
- result: result2
3358
- });
3359
- } catch (error) {
3360
- console.error("Error: ", error);
3361
- controller.enqueue({
3362
- type: "tool-result",
3363
- toolCallType,
3364
- toolCallId,
3365
- toolName,
3366
- result: "Error: " + error,
3367
- isError: true
3368
- });
3369
- } finally {
3370
- toolCallExecutions.delete(toolCallId);
3371
- }
3372
- })()
3373
- );
3374
- }
3375
- }
3376
- break;
3377
- }
3378
- case "text-delta":
3379
- case "tool-call-delta":
3380
- case "tool-result":
3381
- case "finish":
3382
- case "error":
3383
- break;
3384
- default: {
3385
- const unhandledType = chunkType;
3386
- throw new Error(`Unhandled chunk type: ${unhandledType}`);
3387
- }
3388
- }
3389
- },
3390
- async flush() {
3391
- await Promise.all(toolCallExecutions.values());
3392
- }
3393
- });
3394
- }
3395
-
3396
2598
  // src/runtimes/edge/EdgeChatAdapter.ts
3397
2599
  function asAsyncIterable(source) {
3398
2600
  return {