ai 5.0.0-canary.22 → 5.0.0-canary.24
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/CHANGELOG.md +29 -0
- package/dist/index.d.mts +1996 -1940
- package/dist/index.d.ts +1996 -1940
- package/dist/index.js +1088 -593
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1088 -593
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +1 -1
- package/dist/internal/index.mjs.map +1 -1
- package/dist/mcp-stdio/index.d.mts +6 -6
- package/dist/mcp-stdio/index.d.ts +6 -6
- package/package.json +4 -4
package/dist/index.js
CHANGED
@@ -22,6 +22,8 @@ var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
23
23
|
AISDKError: () => import_provider16.AISDKError,
|
24
24
|
APICallError: () => import_provider16.APICallError,
|
25
|
+
ChatStore: () => ChatStore,
|
26
|
+
DefaultChatTransport: () => DefaultChatTransport,
|
25
27
|
DownloadError: () => DownloadError,
|
26
28
|
EmptyResponseBodyError: () => import_provider16.EmptyResponseBodyError,
|
27
29
|
InvalidArgumentError: () => InvalidArgumentError,
|
@@ -50,7 +52,7 @@ __export(src_exports, {
|
|
50
52
|
TypeValidationError: () => import_provider16.TypeValidationError,
|
51
53
|
UnsupportedFunctionalityError: () => import_provider16.UnsupportedFunctionalityError,
|
52
54
|
appendClientMessage: () => appendClientMessage,
|
53
|
-
asSchema: () =>
|
55
|
+
asSchema: () => import_provider_utils26.asSchema,
|
54
56
|
assistantModelMessageSchema: () => assistantModelMessageSchema,
|
55
57
|
callChatApi: () => callChatApi,
|
56
58
|
callCompletionApi: () => callCompletionApi,
|
@@ -63,12 +65,13 @@ __export(src_exports, {
|
|
63
65
|
coreToolMessageSchema: () => coreToolMessageSchema,
|
64
66
|
coreUserMessageSchema: () => coreUserMessageSchema,
|
65
67
|
cosineSimilarity: () => cosineSimilarity,
|
66
|
-
createIdGenerator: () =>
|
68
|
+
createIdGenerator: () => import_provider_utils26.createIdGenerator,
|
67
69
|
createProviderRegistry: () => createProviderRegistry,
|
68
70
|
createTextStreamResponse: () => createTextStreamResponse,
|
69
71
|
createUIMessageStream: () => createUIMessageStream,
|
70
72
|
createUIMessageStreamResponse: () => createUIMessageStreamResponse,
|
71
73
|
customProvider: () => customProvider,
|
74
|
+
defaultChatStore: () => defaultChatStore,
|
72
75
|
defaultSettingsMiddleware: () => defaultSettingsMiddleware,
|
73
76
|
embed: () => embed,
|
74
77
|
embedMany: () => embedMany,
|
@@ -80,19 +83,20 @@ __export(src_exports, {
|
|
80
83
|
experimental_transcribe: () => transcribe,
|
81
84
|
extractMaxToolInvocationStep: () => extractMaxToolInvocationStep,
|
82
85
|
extractReasoningMiddleware: () => extractReasoningMiddleware,
|
83
|
-
generateId: () =>
|
86
|
+
generateId: () => import_provider_utils26.generateId,
|
84
87
|
generateObject: () => generateObject,
|
85
88
|
generateText: () => generateText,
|
86
89
|
getTextFromDataUrl: () => getTextFromDataUrl,
|
87
90
|
getToolInvocations: () => getToolInvocations,
|
91
|
+
hasToolCall: () => hasToolCall,
|
88
92
|
isAssistantMessageWithCompletedToolCalls: () => isAssistantMessageWithCompletedToolCalls,
|
89
93
|
isDeepEqualData: () => isDeepEqualData,
|
90
|
-
jsonSchema: () =>
|
94
|
+
jsonSchema: () => import_provider_utils26.jsonSchema,
|
95
|
+
maxSteps: () => maxSteps,
|
91
96
|
modelMessageSchema: () => modelMessageSchema,
|
92
97
|
parsePartialJson: () => parsePartialJson,
|
93
98
|
pipeTextStreamToResponse: () => pipeTextStreamToResponse,
|
94
99
|
pipeUIMessageStreamToResponse: () => pipeUIMessageStreamToResponse,
|
95
|
-
processTextStream: () => processTextStream,
|
96
100
|
shouldResubmitMessages: () => shouldResubmitMessages,
|
97
101
|
simulateReadableStream: () => simulateReadableStream,
|
98
102
|
simulateStreamingMiddleware: () => simulateStreamingMiddleware,
|
@@ -107,7 +111,7 @@ __export(src_exports, {
|
|
107
111
|
wrapLanguageModel: () => wrapLanguageModel
|
108
112
|
});
|
109
113
|
module.exports = __toCommonJS(src_exports);
|
110
|
-
var
|
114
|
+
var import_provider_utils26 = require("@ai-sdk/provider-utils");
|
111
115
|
|
112
116
|
// src/error/index.ts
|
113
117
|
var import_provider16 = require("@ai-sdk/provider");
|
@@ -546,106 +550,88 @@ function appendClientMessage({
|
|
546
550
|
}
|
547
551
|
|
548
552
|
// src/ui/call-chat-api.ts
|
549
|
-
var
|
553
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
550
554
|
|
551
555
|
// src/ui-message-stream/ui-message-stream-parts.ts
|
552
556
|
var import_zod = require("zod");
|
553
|
-
var
|
554
|
-
toolCallId: import_zod.z.string(),
|
555
|
-
toolName: import_zod.z.string(),
|
556
|
-
args: import_zod.z.unknown()
|
557
|
-
});
|
558
|
-
var toolResultValueSchema = import_zod.z.object({
|
559
|
-
toolCallId: import_zod.z.string(),
|
560
|
-
result: import_zod.z.unknown(),
|
561
|
-
providerMetadata: import_zod.z.any().optional()
|
562
|
-
});
|
563
|
-
var sourceSchema = import_zod.z.object({
|
564
|
-
type: import_zod.z.literal("source"),
|
565
|
-
sourceType: import_zod.z.literal("url"),
|
566
|
-
id: import_zod.z.string(),
|
567
|
-
url: import_zod.z.string(),
|
568
|
-
title: import_zod.z.string().optional(),
|
569
|
-
providerMetadata: import_zod.z.any().optional()
|
570
|
-
// Use z.any() for generic metadata
|
571
|
-
});
|
572
|
-
var uiMessageStreamPartSchema = import_zod.z.discriminatedUnion("type", [
|
557
|
+
var uiMessageStreamPartSchema = import_zod.z.union([
|
573
558
|
import_zod.z.object({
|
574
559
|
type: import_zod.z.literal("text"),
|
575
|
-
|
560
|
+
text: import_zod.z.string()
|
576
561
|
}),
|
577
562
|
import_zod.z.object({
|
578
563
|
type: import_zod.z.literal("error"),
|
579
|
-
|
564
|
+
errorText: import_zod.z.string()
|
580
565
|
}),
|
581
566
|
import_zod.z.object({
|
582
|
-
type: import_zod.z.literal("tool-call"),
|
583
|
-
|
567
|
+
type: import_zod.z.literal("tool-call-streaming-start"),
|
568
|
+
toolCallId: import_zod.z.string(),
|
569
|
+
toolName: import_zod.z.string()
|
584
570
|
}),
|
585
571
|
import_zod.z.object({
|
586
|
-
type: import_zod.z.literal("tool-
|
587
|
-
|
572
|
+
type: import_zod.z.literal("tool-call-delta"),
|
573
|
+
toolCallId: import_zod.z.string(),
|
574
|
+
argsTextDelta: import_zod.z.string()
|
588
575
|
}),
|
589
576
|
import_zod.z.object({
|
590
|
-
type: import_zod.z.literal("tool-call
|
591
|
-
|
577
|
+
type: import_zod.z.literal("tool-call"),
|
578
|
+
toolCallId: import_zod.z.string(),
|
579
|
+
toolName: import_zod.z.string(),
|
580
|
+
args: import_zod.z.unknown()
|
592
581
|
}),
|
593
582
|
import_zod.z.object({
|
594
|
-
type: import_zod.z.literal("tool-
|
595
|
-
|
583
|
+
type: import_zod.z.literal("tool-result"),
|
584
|
+
toolCallId: import_zod.z.string(),
|
585
|
+
result: import_zod.z.unknown(),
|
586
|
+
providerMetadata: import_zod.z.any().optional()
|
596
587
|
}),
|
597
588
|
import_zod.z.object({
|
598
589
|
type: import_zod.z.literal("reasoning"),
|
599
|
-
|
600
|
-
|
601
|
-
providerMetadata: import_zod.z.record(import_zod.z.any()).optional()
|
602
|
-
})
|
590
|
+
text: import_zod.z.string(),
|
591
|
+
providerMetadata: import_zod.z.record(import_zod.z.any()).optional()
|
603
592
|
}),
|
604
593
|
import_zod.z.object({
|
605
594
|
type: import_zod.z.literal("source"),
|
606
|
-
|
595
|
+
sourceType: import_zod.z.literal("url"),
|
596
|
+
id: import_zod.z.string(),
|
597
|
+
url: import_zod.z.string(),
|
598
|
+
title: import_zod.z.string().optional(),
|
599
|
+
providerMetadata: import_zod.z.any().optional()
|
600
|
+
// Use z.any() for generic metadata
|
607
601
|
}),
|
608
602
|
import_zod.z.object({
|
609
603
|
type: import_zod.z.literal("file"),
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
604
|
+
url: import_zod.z.string(),
|
605
|
+
mediaType: import_zod.z.string()
|
606
|
+
}),
|
607
|
+
import_zod.z.object({
|
608
|
+
type: import_zod.z.string().startsWith("data-"),
|
609
|
+
id: import_zod.z.string().optional(),
|
610
|
+
data: import_zod.z.unknown()
|
614
611
|
}),
|
615
612
|
import_zod.z.object({
|
616
613
|
type: import_zod.z.literal("metadata"),
|
617
|
-
value: import_zod.z.object({
|
618
|
-
metadata: import_zod.z.unknown()
|
619
|
-
})
|
614
|
+
value: import_zod.z.object({ metadata: import_zod.z.unknown() })
|
620
615
|
}),
|
621
616
|
import_zod.z.object({
|
622
617
|
type: import_zod.z.literal("start-step"),
|
623
|
-
|
624
|
-
metadata: import_zod.z.unknown()
|
625
|
-
})
|
618
|
+
metadata: import_zod.z.unknown().optional()
|
626
619
|
}),
|
627
620
|
import_zod.z.object({
|
628
621
|
type: import_zod.z.literal("finish-step"),
|
629
|
-
|
630
|
-
metadata: import_zod.z.unknown()
|
631
|
-
})
|
622
|
+
metadata: import_zod.z.unknown().optional()
|
632
623
|
}),
|
633
624
|
import_zod.z.object({
|
634
625
|
type: import_zod.z.literal("start"),
|
635
|
-
|
636
|
-
|
637
|
-
metadata: import_zod.z.unknown()
|
638
|
-
})
|
626
|
+
messageId: import_zod.z.string().optional(),
|
627
|
+
metadata: import_zod.z.unknown().optional()
|
639
628
|
}),
|
640
629
|
import_zod.z.object({
|
641
630
|
type: import_zod.z.literal("finish"),
|
642
|
-
|
643
|
-
metadata: import_zod.z.unknown()
|
644
|
-
})
|
631
|
+
metadata: import_zod.z.unknown().optional()
|
645
632
|
}),
|
646
633
|
import_zod.z.object({
|
647
|
-
type: import_zod.z.literal("reasoning-part-finish")
|
648
|
-
value: import_zod.z.null()
|
634
|
+
type: import_zod.z.literal("reasoning-part-finish")
|
649
635
|
})
|
650
636
|
]);
|
651
637
|
|
@@ -1057,303 +1043,305 @@ function getToolInvocations(message) {
|
|
1057
1043
|
}
|
1058
1044
|
|
1059
1045
|
// src/ui/process-ui-message-stream.ts
|
1060
|
-
function
|
1061
|
-
stream,
|
1062
|
-
onUpdate,
|
1063
|
-
onToolCall,
|
1064
|
-
onFinish,
|
1046
|
+
function createStreamingUIMessageState({
|
1065
1047
|
lastMessage,
|
1066
|
-
newMessageId
|
1067
|
-
|
1068
|
-
}) {
|
1048
|
+
newMessageId = "no-id"
|
1049
|
+
} = {}) {
|
1069
1050
|
var _a17;
|
1070
1051
|
const isContinuation = (lastMessage == null ? void 0 : lastMessage.role) === "assistant";
|
1071
|
-
|
1052
|
+
const step = isContinuation ? 1 + ((_a17 = extractMaxToolInvocationStep(getToolInvocations(lastMessage))) != null ? _a17 : 0) : 0;
|
1072
1053
|
const message = isContinuation ? structuredClone(lastMessage) : {
|
1073
1054
|
id: newMessageId,
|
1074
1055
|
metadata: {},
|
1075
1056
|
role: "assistant",
|
1076
1057
|
parts: []
|
1077
1058
|
};
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
}
|
1093
|
-
const partialToolCalls = {};
|
1094
|
-
async function updateMessageMetadata(metadata) {
|
1095
|
-
if (metadata != null) {
|
1096
|
-
const mergedMetadata = message.metadata != null ? mergeObjects(message.metadata, metadata) : metadata;
|
1097
|
-
if (messageMetadataSchema != null) {
|
1098
|
-
await (0, import_provider_utils2.validateTypes)({
|
1099
|
-
value: mergedMetadata,
|
1100
|
-
schema: messageMetadataSchema
|
1101
|
-
});
|
1102
|
-
}
|
1103
|
-
message.metadata = mergedMetadata;
|
1104
|
-
}
|
1105
|
-
}
|
1059
|
+
return {
|
1060
|
+
message,
|
1061
|
+
activeTextPart: void 0,
|
1062
|
+
activeReasoningPart: void 0,
|
1063
|
+
partialToolCalls: {},
|
1064
|
+
step
|
1065
|
+
};
|
1066
|
+
}
|
1067
|
+
function processUIMessageStream({
|
1068
|
+
stream,
|
1069
|
+
onToolCall,
|
1070
|
+
messageMetadataSchema,
|
1071
|
+
runUpdateMessageJob
|
1072
|
+
}) {
|
1106
1073
|
return stream.pipeThrough(
|
1107
1074
|
new TransformStream({
|
1108
|
-
async transform(
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
};
|
1117
|
-
message.parts.push(currentTextPart);
|
1075
|
+
async transform(part, controller) {
|
1076
|
+
await runUpdateMessageJob(async ({ state, write }) => {
|
1077
|
+
function updateToolInvocationPart(toolCallId, invocation) {
|
1078
|
+
const part2 = state.message.parts.find(
|
1079
|
+
(part3) => part3.type === "tool-invocation" && part3.toolInvocation.toolCallId === toolCallId
|
1080
|
+
);
|
1081
|
+
if (part2 != null) {
|
1082
|
+
part2.toolInvocation = invocation;
|
1118
1083
|
} else {
|
1119
|
-
|
1084
|
+
state.message.parts.push({
|
1085
|
+
type: "tool-invocation",
|
1086
|
+
toolInvocation: invocation
|
1087
|
+
});
|
1120
1088
|
}
|
1121
|
-
onUpdate == null ? void 0 : onUpdate({ message });
|
1122
|
-
break;
|
1123
1089
|
}
|
1124
|
-
|
1125
|
-
if (
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
currentReasoningPart.providerMetadata = value.providerMetadata;
|
1090
|
+
async function updateMessageMetadata(metadata) {
|
1091
|
+
if (metadata != null) {
|
1092
|
+
const mergedMetadata = state.message.metadata != null ? mergeObjects(state.message.metadata, metadata) : metadata;
|
1093
|
+
if (messageMetadataSchema != null) {
|
1094
|
+
await (0, import_provider_utils2.validateTypes)({
|
1095
|
+
value: mergedMetadata,
|
1096
|
+
schema: messageMetadataSchema
|
1097
|
+
});
|
1098
|
+
}
|
1099
|
+
state.message.metadata = mergedMetadata;
|
1135
1100
|
}
|
1136
|
-
onUpdate == null ? void 0 : onUpdate({ message });
|
1137
|
-
break;
|
1138
1101
|
}
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1102
|
+
switch (part.type) {
|
1103
|
+
case "text": {
|
1104
|
+
if (state.activeTextPart == null) {
|
1105
|
+
state.activeTextPart = {
|
1106
|
+
type: "text",
|
1107
|
+
text: part.text
|
1108
|
+
};
|
1109
|
+
state.message.parts.push(state.activeTextPart);
|
1110
|
+
} else {
|
1111
|
+
state.activeTextPart.text += part.text;
|
1112
|
+
}
|
1113
|
+
write();
|
1114
|
+
break;
|
1142
1115
|
}
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
toolCallId
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1116
|
+
case "reasoning": {
|
1117
|
+
if (state.activeReasoningPart == null) {
|
1118
|
+
state.activeReasoningPart = {
|
1119
|
+
type: "reasoning",
|
1120
|
+
text: part.text,
|
1121
|
+
providerMetadata: part.providerMetadata
|
1122
|
+
};
|
1123
|
+
state.message.parts.push(state.activeReasoningPart);
|
1124
|
+
} else {
|
1125
|
+
state.activeReasoningPart.text += part.text;
|
1126
|
+
state.activeReasoningPart.providerMetadata = part.providerMetadata;
|
1127
|
+
}
|
1128
|
+
write();
|
1129
|
+
break;
|
1130
|
+
}
|
1131
|
+
case "reasoning-part-finish": {
|
1132
|
+
if (state.activeReasoningPart != null) {
|
1133
|
+
state.activeReasoningPart = void 0;
|
1134
|
+
}
|
1135
|
+
break;
|
1136
|
+
}
|
1137
|
+
case "file": {
|
1138
|
+
state.message.parts.push({
|
1139
|
+
type: "file",
|
1140
|
+
mediaType: part.mediaType,
|
1141
|
+
url: part.url
|
1142
|
+
});
|
1143
|
+
write();
|
1144
|
+
break;
|
1145
|
+
}
|
1146
|
+
case "source": {
|
1147
|
+
state.message.parts.push({
|
1148
|
+
type: "source",
|
1149
|
+
source: {
|
1150
|
+
sourceType: "url",
|
1151
|
+
id: part.id,
|
1152
|
+
url: part.url,
|
1153
|
+
title: part.title,
|
1154
|
+
providerMetadata: part.providerMetadata
|
1155
|
+
}
|
1156
|
+
});
|
1157
|
+
write();
|
1158
|
+
break;
|
1159
|
+
}
|
1160
|
+
case "tool-call-streaming-start": {
|
1161
|
+
const toolInvocations = getToolInvocations(state.message);
|
1162
|
+
state.partialToolCalls[part.toolCallId] = {
|
1163
|
+
text: "",
|
1164
|
+
step: state.step,
|
1165
|
+
toolName: part.toolName,
|
1166
|
+
index: toolInvocations.length
|
1167
|
+
};
|
1168
|
+
updateToolInvocationPart(part.toolCallId, {
|
1169
|
+
state: "partial-call",
|
1170
|
+
step: state.step,
|
1171
|
+
toolCallId: part.toolCallId,
|
1172
|
+
toolName: part.toolName,
|
1173
|
+
args: void 0
|
1174
|
+
});
|
1175
|
+
write();
|
1176
|
+
break;
|
1177
|
+
}
|
1178
|
+
case "tool-call-delta": {
|
1179
|
+
const partialToolCall = state.partialToolCalls[part.toolCallId];
|
1180
|
+
partialToolCall.text += part.argsTextDelta;
|
1181
|
+
const { value: partialArgs } = await parsePartialJson(
|
1182
|
+
partialToolCall.text
|
1183
|
+
);
|
1184
|
+
updateToolInvocationPart(part.toolCallId, {
|
1185
|
+
state: "partial-call",
|
1186
|
+
step: partialToolCall.step,
|
1187
|
+
toolCallId: part.toolCallId,
|
1188
|
+
toolName: partialToolCall.toolName,
|
1189
|
+
args: partialArgs
|
1190
|
+
});
|
1191
|
+
write();
|
1192
|
+
break;
|
1193
|
+
}
|
1194
|
+
case "tool-call": {
|
1195
|
+
updateToolInvocationPart(part.toolCallId, {
|
1196
|
+
state: "call",
|
1197
|
+
step: state.step,
|
1198
|
+
toolCallId: part.toolCallId,
|
1199
|
+
toolName: part.toolName,
|
1200
|
+
args: part.args
|
1207
1201
|
});
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
...call,
|
1213
|
-
result
|
1202
|
+
write();
|
1203
|
+
if (onToolCall) {
|
1204
|
+
const result = await onToolCall({
|
1205
|
+
toolCall: part
|
1214
1206
|
});
|
1215
|
-
|
1207
|
+
if (result != null) {
|
1208
|
+
updateToolInvocationPart(part.toolCallId, {
|
1209
|
+
state: "result",
|
1210
|
+
step: state.step,
|
1211
|
+
toolCallId: part.toolCallId,
|
1212
|
+
toolName: part.toolName,
|
1213
|
+
args: part.args,
|
1214
|
+
result
|
1215
|
+
});
|
1216
|
+
write();
|
1217
|
+
}
|
1216
1218
|
}
|
1219
|
+
break;
|
1217
1220
|
}
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
const toolInvocationIndex = toolInvocations.findIndex(
|
1226
|
-
(invocation) => invocation.toolCallId === value.toolCallId
|
1227
|
-
);
|
1228
|
-
if (toolInvocationIndex === -1) {
|
1229
|
-
throw new Error(
|
1230
|
-
"tool_result must be preceded by a tool_call with the same toolCallId"
|
1221
|
+
case "tool-result": {
|
1222
|
+
const toolInvocations = getToolInvocations(state.message);
|
1223
|
+
if (toolInvocations == null) {
|
1224
|
+
throw new Error("tool_result must be preceded by a tool_call");
|
1225
|
+
}
|
1226
|
+
const toolInvocationIndex = toolInvocations.findIndex(
|
1227
|
+
(invocation) => invocation.toolCallId === part.toolCallId
|
1231
1228
|
);
|
1229
|
+
if (toolInvocationIndex === -1) {
|
1230
|
+
throw new Error(
|
1231
|
+
"tool_result must be preceded by a tool_call with the same toolCallId"
|
1232
|
+
);
|
1233
|
+
}
|
1234
|
+
updateToolInvocationPart(part.toolCallId, {
|
1235
|
+
...toolInvocations[toolInvocationIndex],
|
1236
|
+
state: "result",
|
1237
|
+
result: part.result
|
1238
|
+
});
|
1239
|
+
write();
|
1240
|
+
break;
|
1232
1241
|
}
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
});
|
1239
|
-
onUpdate == null ? void 0 : onUpdate({ message });
|
1240
|
-
break;
|
1241
|
-
}
|
1242
|
-
case "start-step": {
|
1243
|
-
message.parts.push({ type: "step-start" });
|
1244
|
-
await updateMessageMetadata(value.metadata);
|
1245
|
-
onUpdate == null ? void 0 : onUpdate({ message });
|
1246
|
-
break;
|
1247
|
-
}
|
1248
|
-
case "finish-step": {
|
1249
|
-
step += 1;
|
1250
|
-
currentTextPart = void 0;
|
1251
|
-
currentReasoningPart = void 0;
|
1252
|
-
await updateMessageMetadata(value.metadata);
|
1253
|
-
if (value.metadata != null) {
|
1254
|
-
onUpdate == null ? void 0 : onUpdate({ message });
|
1242
|
+
case "start-step": {
|
1243
|
+
state.message.parts.push({ type: "step-start" });
|
1244
|
+
await updateMessageMetadata(part.metadata);
|
1245
|
+
write();
|
1246
|
+
break;
|
1255
1247
|
}
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1248
|
+
case "finish-step": {
|
1249
|
+
state.step += 1;
|
1250
|
+
state.activeTextPart = void 0;
|
1251
|
+
state.activeReasoningPart = void 0;
|
1252
|
+
await updateMessageMetadata(part.metadata);
|
1253
|
+
if (part.metadata != null) {
|
1254
|
+
write();
|
1255
|
+
}
|
1256
|
+
break;
|
1261
1257
|
}
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1258
|
+
case "start": {
|
1259
|
+
if (part.messageId != null) {
|
1260
|
+
state.message.id = part.messageId;
|
1261
|
+
}
|
1262
|
+
await updateMessageMetadata(part.metadata);
|
1263
|
+
if (part.messageId != null || part.metadata != null) {
|
1264
|
+
write();
|
1265
|
+
}
|
1266
|
+
break;
|
1265
1267
|
}
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1268
|
+
case "finish": {
|
1269
|
+
await updateMessageMetadata(part.metadata);
|
1270
|
+
if (part.metadata != null) {
|
1271
|
+
write();
|
1272
|
+
}
|
1273
|
+
break;
|
1272
1274
|
}
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1275
|
+
case "metadata": {
|
1276
|
+
await updateMessageMetadata(part.metadata);
|
1277
|
+
if (part.metadata != null) {
|
1278
|
+
write();
|
1279
|
+
}
|
1280
|
+
break;
|
1281
|
+
}
|
1282
|
+
case "error": {
|
1283
|
+
throw new Error(part.errorText);
|
1284
|
+
}
|
1285
|
+
default: {
|
1286
|
+
if (part.type.startsWith("data-")) {
|
1287
|
+
const existingPart = part.id != null ? state.message.parts.find(
|
1288
|
+
(partArg) => part.type === partArg.type && part.id === partArg.id
|
1289
|
+
) : void 0;
|
1290
|
+
if (existingPart != null) {
|
1291
|
+
existingPart.value = mergeObjects(
|
1292
|
+
existingPart.data,
|
1293
|
+
part.data
|
1294
|
+
);
|
1295
|
+
} else {
|
1296
|
+
state.message.parts.push({
|
1297
|
+
type: part.type,
|
1298
|
+
id: part.id,
|
1299
|
+
value: part.data
|
1300
|
+
});
|
1301
|
+
}
|
1302
|
+
write();
|
1303
|
+
}
|
1279
1304
|
}
|
1280
|
-
break;
|
1281
|
-
}
|
1282
|
-
case "error": {
|
1283
|
-
throw new Error(value);
|
1284
|
-
}
|
1285
|
-
default: {
|
1286
|
-
const _exhaustiveCheck = type;
|
1287
|
-
throw new Error(`Unhandled stream part: ${_exhaustiveCheck}`);
|
1288
1305
|
}
|
1289
|
-
|
1290
|
-
|
1291
|
-
},
|
1292
|
-
flush() {
|
1293
|
-
onFinish == null ? void 0 : onFinish({ message });
|
1306
|
+
controller.enqueue(part);
|
1307
|
+
});
|
1294
1308
|
}
|
1295
1309
|
})
|
1296
1310
|
);
|
1297
1311
|
}
|
1298
1312
|
|
1299
|
-
// src/ui/
|
1300
|
-
|
1301
|
-
|
1302
|
-
// src/ui/process-text-stream.ts
|
1303
|
-
async function processTextStream({
|
1304
|
-
stream,
|
1305
|
-
onTextPart
|
1306
|
-
}) {
|
1307
|
-
const reader = stream.pipeThrough(new TextDecoderStream()).getReader();
|
1308
|
-
while (true) {
|
1309
|
-
const { done, value } = await reader.read();
|
1310
|
-
if (done) {
|
1311
|
-
break;
|
1312
|
-
}
|
1313
|
-
await onTextPart(value);
|
1314
|
-
}
|
1315
|
-
}
|
1316
|
-
|
1317
|
-
// src/ui/process-chat-text-response.ts
|
1318
|
-
async function processChatTextResponse({
|
1319
|
-
stream,
|
1320
|
-
update,
|
1321
|
-
onFinish,
|
1322
|
-
generateId: generateId3 = import_provider_utils3.generateId
|
1313
|
+
// src/ui/transform-text-to-ui-message-stream.ts
|
1314
|
+
function transformTextToUiMessageStream({
|
1315
|
+
stream
|
1323
1316
|
}) {
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1317
|
+
return stream.pipeThrough(
|
1318
|
+
new TransformStream({
|
1319
|
+
start(controller) {
|
1320
|
+
controller.enqueue({ type: "start" });
|
1321
|
+
controller.enqueue({ type: "start-step" });
|
1322
|
+
},
|
1323
|
+
async transform(part, controller) {
|
1324
|
+
controller.enqueue({ type: "text", text: part });
|
1325
|
+
},
|
1326
|
+
async flush(controller) {
|
1327
|
+
controller.enqueue({ type: "finish-step" });
|
1328
|
+
controller.enqueue({ type: "finish" });
|
1329
|
+
}
|
1330
|
+
})
|
1331
|
+
);
|
1338
1332
|
}
|
1339
1333
|
|
1340
1334
|
// src/ui/call-chat-api.ts
|
1341
1335
|
var getOriginalFetch = () => fetch;
|
1342
|
-
async function
|
1336
|
+
async function fetchUIMessageStream({
|
1343
1337
|
api,
|
1344
1338
|
body,
|
1345
1339
|
streamProtocol = "ui-message",
|
1346
1340
|
credentials,
|
1347
1341
|
headers,
|
1348
1342
|
abortController,
|
1349
|
-
onUpdate,
|
1350
|
-
onFinish,
|
1351
|
-
onToolCall,
|
1352
|
-
generateId: generateId3,
|
1353
1343
|
fetch: fetch2 = getOriginalFetch(),
|
1354
|
-
|
1355
|
-
requestType = "generate",
|
1356
|
-
messageMetadataSchema
|
1344
|
+
requestType = "generate"
|
1357
1345
|
}) {
|
1358
1346
|
var _a17, _b, _c;
|
1359
1347
|
const response = requestType === "resume" ? await fetch2(`${api}?chatId=${body.id}`, {
|
@@ -1382,67 +1370,112 @@ async function callChatApi({
|
|
1382
1370
|
if (!response.body) {
|
1383
1371
|
throw new Error("The response body is empty.");
|
1384
1372
|
}
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
case "ui-message": {
|
1396
|
-
await consumeStream({
|
1397
|
-
stream: processUIMessageStream({
|
1398
|
-
stream: (0, import_provider_utils4.parseJsonEventStream)({
|
1399
|
-
stream: response.body,
|
1400
|
-
schema: uiMessageStreamPartSchema
|
1401
|
-
}).pipeThrough(
|
1402
|
-
new TransformStream({
|
1403
|
-
async transform(part, controller) {
|
1404
|
-
if (!part.success) {
|
1405
|
-
throw part.error;
|
1406
|
-
}
|
1407
|
-
controller.enqueue(part.value);
|
1408
|
-
}
|
1409
|
-
})
|
1410
|
-
),
|
1411
|
-
onUpdate({ message }) {
|
1412
|
-
const copiedMessage = {
|
1413
|
-
// deep copy the message to ensure that deep changes (msg attachments) are updated
|
1414
|
-
// with SolidJS. SolidJS uses referential integration of sub-objects to detect changes.
|
1415
|
-
...structuredClone(message),
|
1416
|
-
// add a revision id to ensure that the message is updated with SWR. SWR uses a
|
1417
|
-
// hashing approach by default to detect changes, but it only works for shallow
|
1418
|
-
// changes. This is why we need to add a revision id to ensure that the message
|
1419
|
-
// is updated with SWR (without it, the changes get stuck in SWR and are not
|
1420
|
-
// forwarded to rendering):
|
1421
|
-
revisionId: generateId3()
|
1422
|
-
};
|
1423
|
-
onUpdate({ message: copiedMessage });
|
1424
|
-
},
|
1425
|
-
lastMessage,
|
1426
|
-
onToolCall,
|
1427
|
-
onFinish,
|
1428
|
-
newMessageId: generateId3(),
|
1429
|
-
messageMetadataSchema
|
1430
|
-
}),
|
1431
|
-
onError: (error) => {
|
1432
|
-
throw error;
|
1373
|
+
return streamProtocol === "text" ? transformTextToUiMessageStream({
|
1374
|
+
stream: response.body.pipeThrough(new TextDecoderStream())
|
1375
|
+
}) : (0, import_provider_utils3.parseJsonEventStream)({
|
1376
|
+
stream: response.body,
|
1377
|
+
schema: uiMessageStreamPartSchema
|
1378
|
+
}).pipeThrough(
|
1379
|
+
new TransformStream({
|
1380
|
+
async transform(part, controller) {
|
1381
|
+
if (!part.success) {
|
1382
|
+
throw part.error;
|
1433
1383
|
}
|
1434
|
-
|
1435
|
-
|
1384
|
+
controller.enqueue(part.value);
|
1385
|
+
}
|
1386
|
+
})
|
1387
|
+
);
|
1388
|
+
}
|
1389
|
+
async function consumeUIMessageStream({
|
1390
|
+
stream,
|
1391
|
+
onUpdate,
|
1392
|
+
onFinish,
|
1393
|
+
onToolCall,
|
1394
|
+
generateId: generateId3,
|
1395
|
+
lastMessage,
|
1396
|
+
messageMetadataSchema
|
1397
|
+
}) {
|
1398
|
+
const state = createStreamingUIMessageState({
|
1399
|
+
lastMessage,
|
1400
|
+
newMessageId: generateId3()
|
1401
|
+
});
|
1402
|
+
const runUpdateMessageJob = async (job) => {
|
1403
|
+
await job({
|
1404
|
+
state,
|
1405
|
+
write: () => {
|
1406
|
+
onUpdate({ message: state.message });
|
1407
|
+
}
|
1408
|
+
});
|
1409
|
+
};
|
1410
|
+
await consumeStream({
|
1411
|
+
stream: processUIMessageStream({
|
1412
|
+
stream,
|
1413
|
+
onToolCall,
|
1414
|
+
messageMetadataSchema,
|
1415
|
+
runUpdateMessageJob
|
1416
|
+
}),
|
1417
|
+
onError: (error) => {
|
1418
|
+
throw error;
|
1436
1419
|
}
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1420
|
+
});
|
1421
|
+
onFinish == null ? void 0 : onFinish({ message: state.message });
|
1422
|
+
}
|
1423
|
+
async function callChatApi({
|
1424
|
+
api,
|
1425
|
+
body,
|
1426
|
+
streamProtocol = "ui-message",
|
1427
|
+
credentials,
|
1428
|
+
headers,
|
1429
|
+
abortController,
|
1430
|
+
onUpdate,
|
1431
|
+
onFinish,
|
1432
|
+
onToolCall,
|
1433
|
+
generateId: generateId3,
|
1434
|
+
fetch: fetch2 = getOriginalFetch(),
|
1435
|
+
lastMessage,
|
1436
|
+
requestType = "generate",
|
1437
|
+
messageMetadataSchema
|
1438
|
+
}) {
|
1439
|
+
const stream = await fetchUIMessageStream({
|
1440
|
+
api,
|
1441
|
+
body,
|
1442
|
+
streamProtocol,
|
1443
|
+
credentials,
|
1444
|
+
headers,
|
1445
|
+
abortController,
|
1446
|
+
fetch: fetch2,
|
1447
|
+
requestType
|
1448
|
+
});
|
1449
|
+
await consumeUIMessageStream({
|
1450
|
+
stream,
|
1451
|
+
onUpdate,
|
1452
|
+
onFinish,
|
1453
|
+
onToolCall,
|
1454
|
+
generateId: generateId3,
|
1455
|
+
lastMessage,
|
1456
|
+
messageMetadataSchema
|
1457
|
+
});
|
1458
|
+
}
|
1459
|
+
|
1460
|
+
// src/ui/call-completion-api.ts
|
1461
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
1462
|
+
|
1463
|
+
// src/ui/process-text-stream.ts
|
1464
|
+
async function processTextStream({
|
1465
|
+
stream,
|
1466
|
+
onTextPart
|
1467
|
+
}) {
|
1468
|
+
const reader = stream.pipeThrough(new TextDecoderStream()).getReader();
|
1469
|
+
while (true) {
|
1470
|
+
const { done, value } = await reader.read();
|
1471
|
+
if (done) {
|
1472
|
+
break;
|
1440
1473
|
}
|
1474
|
+
await onTextPart(value);
|
1441
1475
|
}
|
1442
1476
|
}
|
1443
1477
|
|
1444
1478
|
// src/ui/call-completion-api.ts
|
1445
|
-
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
1446
1479
|
var getOriginalFetch2 = () => fetch;
|
1447
1480
|
async function callCompletionApi({
|
1448
1481
|
api,
|
@@ -1501,58 +1534,508 @@ async function callCompletionApi({
|
|
1501
1534
|
});
|
1502
1535
|
break;
|
1503
1536
|
}
|
1504
|
-
case "data": {
|
1505
|
-
await consumeStream({
|
1506
|
-
stream: (0,
|
1507
|
-
stream: response.body,
|
1508
|
-
schema: uiMessageStreamPartSchema
|
1509
|
-
}).pipeThrough(
|
1510
|
-
new TransformStream({
|
1511
|
-
async transform(part) {
|
1512
|
-
if (!part.success) {
|
1513
|
-
throw part.error;
|
1514
|
-
}
|
1515
|
-
const
|
1516
|
-
if (type === "text") {
|
1517
|
-
result +=
|
1518
|
-
setCompletion(result);
|
1519
|
-
} else if (type === "error") {
|
1520
|
-
throw new Error(
|
1521
|
-
}
|
1522
|
-
}
|
1523
|
-
})
|
1524
|
-
),
|
1525
|
-
onError: (error) => {
|
1526
|
-
throw error;
|
1527
|
-
}
|
1537
|
+
case "data": {
|
1538
|
+
await consumeStream({
|
1539
|
+
stream: (0, import_provider_utils4.parseJsonEventStream)({
|
1540
|
+
stream: response.body,
|
1541
|
+
schema: uiMessageStreamPartSchema
|
1542
|
+
}).pipeThrough(
|
1543
|
+
new TransformStream({
|
1544
|
+
async transform(part) {
|
1545
|
+
if (!part.success) {
|
1546
|
+
throw part.error;
|
1547
|
+
}
|
1548
|
+
const streamPart = part.value;
|
1549
|
+
if (streamPart.type === "text") {
|
1550
|
+
result += streamPart.text;
|
1551
|
+
setCompletion(result);
|
1552
|
+
} else if (streamPart.type === "error") {
|
1553
|
+
throw new Error(streamPart.errorText);
|
1554
|
+
}
|
1555
|
+
}
|
1556
|
+
})
|
1557
|
+
),
|
1558
|
+
onError: (error) => {
|
1559
|
+
throw error;
|
1560
|
+
}
|
1561
|
+
});
|
1562
|
+
break;
|
1563
|
+
}
|
1564
|
+
default: {
|
1565
|
+
const exhaustiveCheck = streamProtocol;
|
1566
|
+
throw new Error(`Unknown stream protocol: ${exhaustiveCheck}`);
|
1567
|
+
}
|
1568
|
+
}
|
1569
|
+
if (onFinish) {
|
1570
|
+
onFinish(prompt, result);
|
1571
|
+
}
|
1572
|
+
setAbortController(null);
|
1573
|
+
return result;
|
1574
|
+
} catch (err) {
|
1575
|
+
if (err.name === "AbortError") {
|
1576
|
+
setAbortController(null);
|
1577
|
+
return null;
|
1578
|
+
}
|
1579
|
+
if (err instanceof Error) {
|
1580
|
+
if (onError) {
|
1581
|
+
onError(err);
|
1582
|
+
}
|
1583
|
+
}
|
1584
|
+
setError(err);
|
1585
|
+
} finally {
|
1586
|
+
setLoading(false);
|
1587
|
+
}
|
1588
|
+
}
|
1589
|
+
|
1590
|
+
// src/ui/chat-store.ts
|
1591
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
1592
|
+
|
1593
|
+
// src/util/serial-job-executor.ts
|
1594
|
+
var SerialJobExecutor = class {
|
1595
|
+
constructor() {
|
1596
|
+
this.queue = [];
|
1597
|
+
this.isProcessing = false;
|
1598
|
+
}
|
1599
|
+
async processQueue() {
|
1600
|
+
if (this.isProcessing) {
|
1601
|
+
return;
|
1602
|
+
}
|
1603
|
+
this.isProcessing = true;
|
1604
|
+
while (this.queue.length > 0) {
|
1605
|
+
await this.queue[0]();
|
1606
|
+
this.queue.shift();
|
1607
|
+
}
|
1608
|
+
this.isProcessing = false;
|
1609
|
+
}
|
1610
|
+
async run(job) {
|
1611
|
+
return new Promise((resolve, reject) => {
|
1612
|
+
this.queue.push(async () => {
|
1613
|
+
try {
|
1614
|
+
await job();
|
1615
|
+
resolve();
|
1616
|
+
} catch (error) {
|
1617
|
+
reject(error);
|
1618
|
+
}
|
1619
|
+
});
|
1620
|
+
void this.processQueue();
|
1621
|
+
});
|
1622
|
+
}
|
1623
|
+
};
|
1624
|
+
|
1625
|
+
// src/ui/should-resubmit-messages.ts
|
1626
|
+
function shouldResubmitMessages({
|
1627
|
+
originalMaxToolInvocationStep,
|
1628
|
+
originalMessageCount,
|
1629
|
+
maxSteps: maxSteps2,
|
1630
|
+
messages
|
1631
|
+
}) {
|
1632
|
+
var _a17;
|
1633
|
+
const lastMessage = messages[messages.length - 1];
|
1634
|
+
return (
|
1635
|
+
// check if the feature is enabled:
|
1636
|
+
maxSteps2 > 1 && // ensure there is a last message:
|
1637
|
+
lastMessage != null && // ensure we actually have new steps (to prevent infinite loops in case of errors):
|
1638
|
+
(messages.length > originalMessageCount || extractMaxToolInvocationStep(getToolInvocations(lastMessage)) !== originalMaxToolInvocationStep) && // check that next step is possible:
|
1639
|
+
isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic steps:
|
1640
|
+
((_a17 = extractMaxToolInvocationStep(getToolInvocations(lastMessage))) != null ? _a17 : 0) < maxSteps2
|
1641
|
+
);
|
1642
|
+
}
|
1643
|
+
function isAssistantMessageWithCompletedToolCalls(message) {
|
1644
|
+
if (message.role !== "assistant") {
|
1645
|
+
return false;
|
1646
|
+
}
|
1647
|
+
const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
|
1648
|
+
return part.type === "step-start" ? index : lastIndex;
|
1649
|
+
}, -1);
|
1650
|
+
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter((part) => part.type === "tool-invocation");
|
1651
|
+
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => "result" in part.toolInvocation);
|
1652
|
+
}
|
1653
|
+
|
1654
|
+
// src/ui/update-tool-call-result.ts
|
1655
|
+
function updateToolCallResult({
|
1656
|
+
messages,
|
1657
|
+
toolCallId,
|
1658
|
+
toolResult: result
|
1659
|
+
}) {
|
1660
|
+
const lastMessage = messages[messages.length - 1];
|
1661
|
+
const invocationPart = lastMessage.parts.find(
|
1662
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
|
1663
|
+
);
|
1664
|
+
if (invocationPart == null) {
|
1665
|
+
return;
|
1666
|
+
}
|
1667
|
+
invocationPart.toolInvocation = {
|
1668
|
+
...invocationPart.toolInvocation,
|
1669
|
+
state: "result",
|
1670
|
+
result
|
1671
|
+
};
|
1672
|
+
}
|
1673
|
+
|
1674
|
+
// src/ui/chat-store.ts
|
1675
|
+
var ChatStore = class {
|
1676
|
+
constructor({
|
1677
|
+
chats = {},
|
1678
|
+
generateId: generateId3,
|
1679
|
+
messageMetadataSchema,
|
1680
|
+
transport,
|
1681
|
+
maxSteps: maxSteps2 = 1
|
1682
|
+
}) {
|
1683
|
+
this.chats = new Map(
|
1684
|
+
Object.entries(chats).map(([id, state]) => [
|
1685
|
+
id,
|
1686
|
+
{
|
1687
|
+
messages: [...state.messages],
|
1688
|
+
status: "ready",
|
1689
|
+
activeResponse: void 0,
|
1690
|
+
error: void 0,
|
1691
|
+
jobExecutor: new SerialJobExecutor()
|
1692
|
+
}
|
1693
|
+
])
|
1694
|
+
);
|
1695
|
+
this.maxSteps = maxSteps2;
|
1696
|
+
this.transport = transport;
|
1697
|
+
this.subscribers = /* @__PURE__ */ new Set();
|
1698
|
+
this.generateId = generateId3 != null ? generateId3 : import_provider_utils5.generateId;
|
1699
|
+
this.messageMetadataSchema = messageMetadataSchema;
|
1700
|
+
}
|
1701
|
+
hasChat(id) {
|
1702
|
+
return this.chats.has(id);
|
1703
|
+
}
|
1704
|
+
addChat(id, messages) {
|
1705
|
+
this.chats.set(id, {
|
1706
|
+
messages,
|
1707
|
+
status: "ready",
|
1708
|
+
jobExecutor: new SerialJobExecutor()
|
1709
|
+
});
|
1710
|
+
}
|
1711
|
+
getChats() {
|
1712
|
+
return Array.from(this.chats.entries());
|
1713
|
+
}
|
1714
|
+
get chatCount() {
|
1715
|
+
return this.chats.size;
|
1716
|
+
}
|
1717
|
+
getStatus(id) {
|
1718
|
+
return this.getChat(id).status;
|
1719
|
+
}
|
1720
|
+
setStatus({
|
1721
|
+
id,
|
1722
|
+
status,
|
1723
|
+
error
|
1724
|
+
}) {
|
1725
|
+
const chat = this.getChat(id);
|
1726
|
+
if (chat.status === status)
|
1727
|
+
return;
|
1728
|
+
chat.status = status;
|
1729
|
+
chat.error = error;
|
1730
|
+
this.emit({ type: "chat-status-changed", chatId: id, error });
|
1731
|
+
}
|
1732
|
+
getError(id) {
|
1733
|
+
return this.getChat(id).error;
|
1734
|
+
}
|
1735
|
+
getMessages(id) {
|
1736
|
+
return this.getChat(id).messages;
|
1737
|
+
}
|
1738
|
+
getLastMessage(id) {
|
1739
|
+
const chat = this.getChat(id);
|
1740
|
+
return chat.messages[chat.messages.length - 1];
|
1741
|
+
}
|
1742
|
+
subscribe(subscriber) {
|
1743
|
+
this.subscribers.add(subscriber);
|
1744
|
+
return () => this.subscribers.delete(subscriber);
|
1745
|
+
}
|
1746
|
+
setMessages({
|
1747
|
+
id,
|
1748
|
+
messages
|
1749
|
+
}) {
|
1750
|
+
this.getChat(id).messages = [...messages];
|
1751
|
+
this.emit({ type: "chat-messages-changed", chatId: id });
|
1752
|
+
}
|
1753
|
+
removeAssistantResponse(id) {
|
1754
|
+
const chat = this.getChat(id);
|
1755
|
+
const lastMessage = chat.messages[chat.messages.length - 1];
|
1756
|
+
if (lastMessage == null) {
|
1757
|
+
throw new Error("Cannot remove assistant response from empty chat");
|
1758
|
+
}
|
1759
|
+
if (lastMessage.role !== "assistant") {
|
1760
|
+
throw new Error("Last message is not an assistant message");
|
1761
|
+
}
|
1762
|
+
this.setMessages({ id, messages: chat.messages.slice(0, -1) });
|
1763
|
+
}
|
1764
|
+
async submitMessage({
|
1765
|
+
chatId,
|
1766
|
+
message,
|
1767
|
+
headers,
|
1768
|
+
body,
|
1769
|
+
onError,
|
1770
|
+
onToolCall,
|
1771
|
+
onFinish
|
1772
|
+
}) {
|
1773
|
+
var _a17;
|
1774
|
+
const chat = this.getChat(chatId);
|
1775
|
+
const currentMessages = chat.messages;
|
1776
|
+
await this.triggerRequest({
|
1777
|
+
chatId,
|
1778
|
+
messages: currentMessages.concat({
|
1779
|
+
...message,
|
1780
|
+
id: (_a17 = message.id) != null ? _a17 : this.generateId()
|
1781
|
+
}),
|
1782
|
+
headers,
|
1783
|
+
body,
|
1784
|
+
requestType: "generate",
|
1785
|
+
onError,
|
1786
|
+
onToolCall,
|
1787
|
+
onFinish
|
1788
|
+
});
|
1789
|
+
}
|
1790
|
+
async resubmitLastUserMessage({
|
1791
|
+
chatId,
|
1792
|
+
headers,
|
1793
|
+
body,
|
1794
|
+
onError,
|
1795
|
+
onToolCall,
|
1796
|
+
onFinish
|
1797
|
+
}) {
|
1798
|
+
const messages = this.getChat(chatId).messages;
|
1799
|
+
const messagesToSubmit = messages[messages.length - 1].role === "assistant" ? messages.slice(0, -1) : messages;
|
1800
|
+
if (messagesToSubmit.length === 0) {
|
1801
|
+
return;
|
1802
|
+
}
|
1803
|
+
return this.triggerRequest({
|
1804
|
+
chatId,
|
1805
|
+
requestType: "generate",
|
1806
|
+
messages: messagesToSubmit,
|
1807
|
+
headers,
|
1808
|
+
body,
|
1809
|
+
onError,
|
1810
|
+
onToolCall,
|
1811
|
+
onFinish
|
1812
|
+
});
|
1813
|
+
}
|
1814
|
+
async resumeStream({
|
1815
|
+
chatId,
|
1816
|
+
headers,
|
1817
|
+
body,
|
1818
|
+
onError,
|
1819
|
+
onToolCall,
|
1820
|
+
onFinish
|
1821
|
+
}) {
|
1822
|
+
const chat = this.getChat(chatId);
|
1823
|
+
const currentMessages = chat.messages;
|
1824
|
+
return this.triggerRequest({
|
1825
|
+
chatId,
|
1826
|
+
messages: currentMessages,
|
1827
|
+
requestType: "resume",
|
1828
|
+
headers,
|
1829
|
+
body,
|
1830
|
+
onError,
|
1831
|
+
onToolCall,
|
1832
|
+
onFinish
|
1833
|
+
});
|
1834
|
+
}
|
1835
|
+
async addToolResult({
|
1836
|
+
chatId,
|
1837
|
+
toolCallId,
|
1838
|
+
result
|
1839
|
+
}) {
|
1840
|
+
const chat = this.getChat(chatId);
|
1841
|
+
chat.jobExecutor.run(async () => {
|
1842
|
+
const currentMessages = chat.messages;
|
1843
|
+
updateToolCallResult({
|
1844
|
+
messages: currentMessages,
|
1845
|
+
toolCallId,
|
1846
|
+
toolResult: result
|
1847
|
+
});
|
1848
|
+
this.setMessages({ id: chatId, messages: currentMessages });
|
1849
|
+
if (chat.status === "submitted" || chat.status === "streaming") {
|
1850
|
+
return;
|
1851
|
+
}
|
1852
|
+
const lastMessage = currentMessages[currentMessages.length - 1];
|
1853
|
+
if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
|
1854
|
+
await this.triggerRequest({
|
1855
|
+
messages: currentMessages,
|
1856
|
+
requestType: "generate",
|
1857
|
+
chatId
|
1528
1858
|
});
|
1529
|
-
break;
|
1530
|
-
}
|
1531
|
-
default: {
|
1532
|
-
const exhaustiveCheck = streamProtocol;
|
1533
|
-
throw new Error(`Unknown stream protocol: ${exhaustiveCheck}`);
|
1534
1859
|
}
|
1860
|
+
});
|
1861
|
+
}
|
1862
|
+
async stopStream({ chatId }) {
|
1863
|
+
var _a17;
|
1864
|
+
const chat = this.getChat(chatId);
|
1865
|
+
if (chat.status !== "streaming" && chat.status !== "submitted")
|
1866
|
+
return;
|
1867
|
+
if ((_a17 = chat.activeResponse) == null ? void 0 : _a17.abortController) {
|
1868
|
+
chat.activeResponse.abortController.abort();
|
1869
|
+
chat.activeResponse.abortController = void 0;
|
1535
1870
|
}
|
1536
|
-
|
1537
|
-
|
1871
|
+
}
|
1872
|
+
emit(event) {
|
1873
|
+
for (const subscriber of this.subscribers) {
|
1874
|
+
subscriber.onChatChanged(event);
|
1538
1875
|
}
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
setAbortController(null);
|
1544
|
-
return null;
|
1876
|
+
}
|
1877
|
+
getChat(id) {
|
1878
|
+
if (!this.hasChat(id)) {
|
1879
|
+
throw new Error(`chat '${id}' not found`);
|
1545
1880
|
}
|
1546
|
-
|
1547
|
-
|
1881
|
+
return this.chats.get(id);
|
1882
|
+
}
|
1883
|
+
async triggerRequest({
|
1884
|
+
chatId,
|
1885
|
+
messages: chatMessages,
|
1886
|
+
requestType,
|
1887
|
+
headers,
|
1888
|
+
body,
|
1889
|
+
onError,
|
1890
|
+
onToolCall,
|
1891
|
+
onFinish
|
1892
|
+
}) {
|
1893
|
+
const self = this;
|
1894
|
+
const chat = this.getChat(chatId);
|
1895
|
+
this.setMessages({ id: chatId, messages: chatMessages });
|
1896
|
+
this.setStatus({ id: chatId, status: "submitted", error: void 0 });
|
1897
|
+
const messageCount = chatMessages.length;
|
1898
|
+
const maxStep = extractMaxToolInvocationStep(
|
1899
|
+
getToolInvocations(chatMessages[chatMessages.length - 1])
|
1900
|
+
);
|
1901
|
+
try {
|
1902
|
+
const activeResponse = {
|
1903
|
+
state: createStreamingUIMessageState({
|
1904
|
+
lastMessage: chatMessages[chatMessages.length - 1],
|
1905
|
+
newMessageId: self.generateId()
|
1906
|
+
}),
|
1907
|
+
abortController: new AbortController()
|
1908
|
+
};
|
1909
|
+
chat.activeResponse = activeResponse;
|
1910
|
+
const stream = await self.transport.submitMessages({
|
1911
|
+
chatId,
|
1912
|
+
messages: chatMessages,
|
1913
|
+
body,
|
1914
|
+
headers,
|
1915
|
+
abortController: activeResponse.abortController,
|
1916
|
+
requestType
|
1917
|
+
});
|
1918
|
+
const runUpdateMessageJob = (job) => (
|
1919
|
+
// serialize the job execution to avoid race conditions:
|
1920
|
+
chat.jobExecutor.run(
|
1921
|
+
() => job({
|
1922
|
+
state: activeResponse.state,
|
1923
|
+
write: () => {
|
1924
|
+
self.setStatus({ id: chatId, status: "streaming" });
|
1925
|
+
const replaceLastMessage = activeResponse.state.message.id === chatMessages[chatMessages.length - 1].id;
|
1926
|
+
const newMessages = [
|
1927
|
+
...replaceLastMessage ? chatMessages.slice(0, chatMessages.length - 1) : chatMessages,
|
1928
|
+
activeResponse.state.message
|
1929
|
+
];
|
1930
|
+
self.setMessages({
|
1931
|
+
id: chatId,
|
1932
|
+
messages: newMessages
|
1933
|
+
});
|
1934
|
+
}
|
1935
|
+
})
|
1936
|
+
)
|
1937
|
+
);
|
1938
|
+
await consumeStream({
|
1939
|
+
stream: processUIMessageStream({
|
1940
|
+
stream,
|
1941
|
+
onToolCall,
|
1942
|
+
messageMetadataSchema: self.messageMetadataSchema,
|
1943
|
+
runUpdateMessageJob
|
1944
|
+
}),
|
1945
|
+
onError: (error) => {
|
1946
|
+
throw error;
|
1947
|
+
}
|
1948
|
+
});
|
1949
|
+
onFinish == null ? void 0 : onFinish({ message: activeResponse.state.message });
|
1950
|
+
this.setStatus({ id: chatId, status: "ready" });
|
1951
|
+
} catch (err) {
|
1952
|
+
if (err.name === "AbortError") {
|
1953
|
+
this.setStatus({ id: chatId, status: "ready" });
|
1954
|
+
return null;
|
1955
|
+
}
|
1956
|
+
if (onError && err instanceof Error) {
|
1548
1957
|
onError(err);
|
1549
1958
|
}
|
1959
|
+
this.setStatus({ id: chatId, status: "error", error: err });
|
1960
|
+
} finally {
|
1961
|
+
chat.activeResponse = void 0;
|
1962
|
+
}
|
1963
|
+
const currentMessages = self.getMessages(chatId);
|
1964
|
+
if (shouldResubmitMessages({
|
1965
|
+
originalMaxToolInvocationStep: maxStep,
|
1966
|
+
originalMessageCount: messageCount,
|
1967
|
+
maxSteps: self.maxSteps,
|
1968
|
+
messages: currentMessages
|
1969
|
+
})) {
|
1970
|
+
await self.triggerRequest({
|
1971
|
+
chatId,
|
1972
|
+
requestType,
|
1973
|
+
onError,
|
1974
|
+
onToolCall,
|
1975
|
+
onFinish,
|
1976
|
+
headers,
|
1977
|
+
body,
|
1978
|
+
messages: currentMessages
|
1979
|
+
});
|
1550
1980
|
}
|
1551
|
-
setError(err);
|
1552
|
-
} finally {
|
1553
|
-
setLoading(false);
|
1554
1981
|
}
|
1555
|
-
}
|
1982
|
+
};
|
1983
|
+
|
1984
|
+
// src/ui/chat-transport.ts
|
1985
|
+
var DefaultChatTransport = class {
|
1986
|
+
constructor({
|
1987
|
+
api,
|
1988
|
+
credentials,
|
1989
|
+
headers,
|
1990
|
+
body,
|
1991
|
+
streamProtocol,
|
1992
|
+
fetch: fetch2,
|
1993
|
+
prepareRequestBody
|
1994
|
+
}) {
|
1995
|
+
this.api = api;
|
1996
|
+
this.credentials = credentials;
|
1997
|
+
this.headers = headers;
|
1998
|
+
this.body = body;
|
1999
|
+
this.streamProtocol = streamProtocol;
|
2000
|
+
this.fetch = fetch2;
|
2001
|
+
this.prepareRequestBody = prepareRequestBody;
|
2002
|
+
}
|
2003
|
+
submitMessages({
|
2004
|
+
chatId,
|
2005
|
+
messages,
|
2006
|
+
abortController,
|
2007
|
+
body,
|
2008
|
+
headers,
|
2009
|
+
requestType
|
2010
|
+
}) {
|
2011
|
+
var _a17, _b;
|
2012
|
+
return fetchUIMessageStream({
|
2013
|
+
api: this.api,
|
2014
|
+
headers: {
|
2015
|
+
...this.headers,
|
2016
|
+
...headers
|
2017
|
+
},
|
2018
|
+
body: (_b = (_a17 = this.prepareRequestBody) == null ? void 0 : _a17.call(this, {
|
2019
|
+
id: chatId,
|
2020
|
+
// TODO change to chatId
|
2021
|
+
messages,
|
2022
|
+
...this.body,
|
2023
|
+
...body
|
2024
|
+
})) != null ? _b : {
|
2025
|
+
id: chatId,
|
2026
|
+
// TODO change to chatId
|
2027
|
+
messages,
|
2028
|
+
...this.body,
|
2029
|
+
...body
|
2030
|
+
},
|
2031
|
+
streamProtocol: this.streamProtocol,
|
2032
|
+
credentials: this.credentials,
|
2033
|
+
abortController: () => abortController,
|
2034
|
+
fetch: this.fetch,
|
2035
|
+
requestType
|
2036
|
+
});
|
2037
|
+
}
|
2038
|
+
};
|
1556
2039
|
|
1557
2040
|
// src/ui/convert-file-list-to-file-ui-parts.ts
|
1558
2041
|
async function convertFileListToFileUIParts(files) {
|
@@ -1740,53 +2223,36 @@ function convertToModelMessages(messages, options) {
|
|
1740
2223
|
}
|
1741
2224
|
var convertToCoreMessages = convertToModelMessages;
|
1742
2225
|
|
1743
|
-
// src/ui/
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1757
|
-
isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic steps:
|
1758
|
-
((_a17 = extractMaxToolInvocationStep(getToolInvocations(lastMessage))) != null ? _a17 : 0) < maxSteps
|
1759
|
-
);
|
1760
|
-
}
|
1761
|
-
function isAssistantMessageWithCompletedToolCalls(message) {
|
1762
|
-
if (message.role !== "assistant") {
|
1763
|
-
return false;
|
1764
|
-
}
|
1765
|
-
const lastStepStartIndex = message.parts.reduce((lastIndex, part, index) => {
|
1766
|
-
return part.type === "step-start" ? index : lastIndex;
|
1767
|
-
}, -1);
|
1768
|
-
const lastStepToolInvocations = message.parts.slice(lastStepStartIndex + 1).filter((part) => part.type === "tool-invocation");
|
1769
|
-
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => "result" in part.toolInvocation);
|
1770
|
-
}
|
1771
|
-
|
1772
|
-
// src/ui/update-tool-call-result.ts
|
1773
|
-
function updateToolCallResult({
|
1774
|
-
messages,
|
1775
|
-
toolCallId,
|
1776
|
-
toolResult: result
|
2226
|
+
// src/ui/default-chat-store.ts
|
2227
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
2228
|
+
function defaultChatStore({
|
2229
|
+
api,
|
2230
|
+
fetch: fetch2,
|
2231
|
+
streamProtocol = "ui-message",
|
2232
|
+
credentials,
|
2233
|
+
headers,
|
2234
|
+
body,
|
2235
|
+
prepareRequestBody,
|
2236
|
+
generateId: generateId3 = import_provider_utils6.generateId,
|
2237
|
+
messageMetadataSchema,
|
2238
|
+
maxSteps: maxSteps2 = 1,
|
2239
|
+
chats
|
1777
2240
|
}) {
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1781
|
-
|
1782
|
-
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1786
|
-
|
1787
|
-
|
1788
|
-
|
1789
|
-
|
2241
|
+
return new ChatStore({
|
2242
|
+
transport: new DefaultChatTransport({
|
2243
|
+
api,
|
2244
|
+
fetch: fetch2,
|
2245
|
+
streamProtocol,
|
2246
|
+
credentials,
|
2247
|
+
headers,
|
2248
|
+
body,
|
2249
|
+
prepareRequestBody
|
2250
|
+
}),
|
2251
|
+
generateId: generateId3,
|
2252
|
+
messageMetadataSchema,
|
2253
|
+
maxSteps: maxSteps2,
|
2254
|
+
chats
|
2255
|
+
});
|
1790
2256
|
}
|
1791
2257
|
|
1792
2258
|
// src/ui-message-stream/create-ui-message-stream.ts
|
@@ -1824,7 +2290,7 @@ function createUIMessageStream({
|
|
1824
2290
|
safeEnqueue(value);
|
1825
2291
|
}
|
1826
2292
|
})().catch((error) => {
|
1827
|
-
safeEnqueue({ type: "error",
|
2293
|
+
safeEnqueue({ type: "error", errorText: onError(error) });
|
1828
2294
|
})
|
1829
2295
|
);
|
1830
2296
|
},
|
@@ -1833,12 +2299,12 @@ function createUIMessageStream({
|
|
1833
2299
|
if (result) {
|
1834
2300
|
ongoingStreamPromises.push(
|
1835
2301
|
result.catch((error) => {
|
1836
|
-
safeEnqueue({ type: "error",
|
2302
|
+
safeEnqueue({ type: "error", errorText: onError(error) });
|
1837
2303
|
})
|
1838
2304
|
);
|
1839
2305
|
}
|
1840
2306
|
} catch (error) {
|
1841
|
-
safeEnqueue({ type: "error",
|
2307
|
+
safeEnqueue({ type: "error", errorText: onError(error) });
|
1842
2308
|
}
|
1843
2309
|
const waitForStreams = new Promise(async (resolve) => {
|
1844
2310
|
while (ongoingStreamPromises.length > 0) {
|
@@ -1993,7 +2459,7 @@ function cosineSimilarity(vector1, vector2) {
|
|
1993
2459
|
}
|
1994
2460
|
|
1995
2461
|
// src/util/simulate-readable-stream.ts
|
1996
|
-
var
|
2462
|
+
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
1997
2463
|
function simulateReadableStream({
|
1998
2464
|
chunks,
|
1999
2465
|
initialDelayInMs = 0,
|
@@ -2001,7 +2467,7 @@ function simulateReadableStream({
|
|
2001
2467
|
_internal
|
2002
2468
|
}) {
|
2003
2469
|
var _a17;
|
2004
|
-
const delay2 = (_a17 = _internal == null ? void 0 : _internal.delay) != null ? _a17 :
|
2470
|
+
const delay2 = (_a17 = _internal == null ? void 0 : _internal.delay) != null ? _a17 : import_provider_utils7.delay;
|
2005
2471
|
let index = 0;
|
2006
2472
|
return new ReadableStream({
|
2007
2473
|
async pull(controller) {
|
@@ -2017,7 +2483,7 @@ function simulateReadableStream({
|
|
2017
2483
|
|
2018
2484
|
// src/util/retry-with-exponential-backoff.ts
|
2019
2485
|
var import_provider17 = require("@ai-sdk/provider");
|
2020
|
-
var
|
2486
|
+
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
2021
2487
|
var retryWithExponentialBackoff = ({
|
2022
2488
|
maxRetries = 2,
|
2023
2489
|
initialDelayInMs = 2e3,
|
@@ -2035,13 +2501,13 @@ async function _retryWithExponentialBackoff(f, {
|
|
2035
2501
|
try {
|
2036
2502
|
return await f();
|
2037
2503
|
} catch (error) {
|
2038
|
-
if ((0,
|
2504
|
+
if ((0, import_provider_utils8.isAbortError)(error)) {
|
2039
2505
|
throw error;
|
2040
2506
|
}
|
2041
2507
|
if (maxRetries === 0) {
|
2042
2508
|
throw error;
|
2043
2509
|
}
|
2044
|
-
const errorMessage = (0,
|
2510
|
+
const errorMessage = (0, import_provider_utils8.getErrorMessage)(error);
|
2045
2511
|
const newErrors = [...errors, error];
|
2046
2512
|
const tryNumber = newErrors.length;
|
2047
2513
|
if (tryNumber > maxRetries) {
|
@@ -2052,7 +2518,7 @@ async function _retryWithExponentialBackoff(f, {
|
|
2052
2518
|
});
|
2053
2519
|
}
|
2054
2520
|
if (error instanceof Error && import_provider17.APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
2055
|
-
await (0,
|
2521
|
+
await (0, import_provider_utils8.delay)(delayInMs);
|
2056
2522
|
return _retryWithExponentialBackoff(
|
2057
2523
|
f,
|
2058
2524
|
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
@@ -2611,7 +3077,7 @@ var DefaultEmbedManyResult = class {
|
|
2611
3077
|
};
|
2612
3078
|
|
2613
3079
|
// src/util/detect-media-type.ts
|
2614
|
-
var
|
3080
|
+
var import_provider_utils9 = require("@ai-sdk/provider-utils");
|
2615
3081
|
var imageMediaTypeSignatures = [
|
2616
3082
|
{
|
2617
3083
|
mediaType: "image/gif",
|
@@ -2718,7 +3184,7 @@ var audioMediaTypeSignatures = [
|
|
2718
3184
|
}
|
2719
3185
|
];
|
2720
3186
|
var stripID3 = (data) => {
|
2721
|
-
const bytes = typeof data === "string" ? (0,
|
3187
|
+
const bytes = typeof data === "string" ? (0, import_provider_utils9.convertBase64ToUint8Array)(data) : data;
|
2722
3188
|
const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
|
2723
3189
|
return bytes.slice(id3Size + 10);
|
2724
3190
|
};
|
@@ -2744,7 +3210,7 @@ function detectMediaType({
|
|
2744
3210
|
}
|
2745
3211
|
|
2746
3212
|
// core/generate-text/generated-file.ts
|
2747
|
-
var
|
3213
|
+
var import_provider_utils10 = require("@ai-sdk/provider-utils");
|
2748
3214
|
var DefaultGeneratedFile = class {
|
2749
3215
|
constructor({
|
2750
3216
|
data,
|
@@ -2758,14 +3224,14 @@ var DefaultGeneratedFile = class {
|
|
2758
3224
|
// lazy conversion with caching to avoid unnecessary conversion overhead:
|
2759
3225
|
get base64() {
|
2760
3226
|
if (this.base64Data == null) {
|
2761
|
-
this.base64Data = (0,
|
3227
|
+
this.base64Data = (0, import_provider_utils10.convertUint8ArrayToBase64)(this.uint8ArrayData);
|
2762
3228
|
}
|
2763
3229
|
return this.base64Data;
|
2764
3230
|
}
|
2765
3231
|
// lazy conversion with caching to avoid unnecessary conversion overhead:
|
2766
3232
|
get uint8Array() {
|
2767
3233
|
if (this.uint8ArrayData == null) {
|
2768
|
-
this.uint8ArrayData = (0,
|
3234
|
+
this.uint8ArrayData = (0, import_provider_utils10.convertBase64ToUint8Array)(this.base64Data);
|
2769
3235
|
}
|
2770
3236
|
return this.uint8ArrayData;
|
2771
3237
|
}
|
@@ -2872,7 +3338,7 @@ var DefaultGenerateImageResult = class {
|
|
2872
3338
|
|
2873
3339
|
// core/generate-object/generate-object.ts
|
2874
3340
|
var import_provider21 = require("@ai-sdk/provider");
|
2875
|
-
var
|
3341
|
+
var import_provider_utils15 = require("@ai-sdk/provider-utils");
|
2876
3342
|
|
2877
3343
|
// core/generate-text/extract-content-text.ts
|
2878
3344
|
function extractContentText(content) {
|
@@ -2886,7 +3352,7 @@ function extractContentText(content) {
|
|
2886
3352
|
}
|
2887
3353
|
|
2888
3354
|
// core/prompt/convert-to-language-model-prompt.ts
|
2889
|
-
var
|
3355
|
+
var import_provider_utils12 = require("@ai-sdk/provider-utils");
|
2890
3356
|
|
2891
3357
|
// src/util/download.ts
|
2892
3358
|
async function download({ url }) {
|
@@ -2915,7 +3381,7 @@ async function download({ url }) {
|
|
2915
3381
|
|
2916
3382
|
// core/prompt/data-content.ts
|
2917
3383
|
var import_provider18 = require("@ai-sdk/provider");
|
2918
|
-
var
|
3384
|
+
var import_provider_utils11 = require("@ai-sdk/provider-utils");
|
2919
3385
|
var import_zod2 = require("zod");
|
2920
3386
|
|
2921
3387
|
// core/prompt/split-data-url.ts
|
@@ -2981,7 +3447,7 @@ function convertDataContentToUint8Array(content) {
|
|
2981
3447
|
}
|
2982
3448
|
if (typeof content === "string") {
|
2983
3449
|
try {
|
2984
|
-
return (0,
|
3450
|
+
return (0, import_provider_utils11.convertBase64ToUint8Array)(content);
|
2985
3451
|
} catch (error) {
|
2986
3452
|
throw new InvalidDataContentError({
|
2987
3453
|
message: "Invalid data content. Content string is not a base64-encoded media.",
|
@@ -3132,7 +3598,7 @@ async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
|
3132
3598
|
}
|
3133
3599
|
return { mediaType, data };
|
3134
3600
|
}).filter(
|
3135
|
-
(part) => part.data instanceof URL && part.mediaType != null && !(0,
|
3601
|
+
(part) => part.data instanceof URL && part.mediaType != null && !(0, import_provider_utils12.isUrlSupported)({
|
3136
3602
|
url: part.data.toString(),
|
3137
3603
|
mediaType: part.mediaType,
|
3138
3604
|
supportedUrls
|
@@ -3303,7 +3769,7 @@ function prepareCallSettings({
|
|
3303
3769
|
|
3304
3770
|
// core/prompt/standardize-prompt.ts
|
3305
3771
|
var import_provider19 = require("@ai-sdk/provider");
|
3306
|
-
var
|
3772
|
+
var import_provider_utils13 = require("@ai-sdk/provider-utils");
|
3307
3773
|
var import_zod8 = require("zod");
|
3308
3774
|
|
3309
3775
|
// core/prompt/message.ts
|
@@ -3475,14 +3941,14 @@ async function standardizePrompt(prompt) {
|
|
3475
3941
|
message: "messages must not be empty"
|
3476
3942
|
});
|
3477
3943
|
}
|
3478
|
-
const validationResult = await (0,
|
3944
|
+
const validationResult = await (0, import_provider_utils13.safeValidateTypes)({
|
3479
3945
|
value: messages,
|
3480
3946
|
schema: import_zod8.z.array(modelMessageSchema)
|
3481
3947
|
});
|
3482
3948
|
if (!validationResult.success) {
|
3483
3949
|
throw new import_provider19.InvalidPromptError({
|
3484
3950
|
prompt,
|
3485
|
-
message: "messages must be
|
3951
|
+
message: "The messages must be a ModelMessage[]. If you have passed a UIMessage[], you can use convertToModelMessages to convert them.",
|
3486
3952
|
cause: validationResult.error
|
3487
3953
|
});
|
3488
3954
|
}
|
@@ -3494,7 +3960,7 @@ async function standardizePrompt(prompt) {
|
|
3494
3960
|
|
3495
3961
|
// core/generate-object/output-strategy.ts
|
3496
3962
|
var import_provider20 = require("@ai-sdk/provider");
|
3497
|
-
var
|
3963
|
+
var import_provider_utils14 = require("@ai-sdk/provider-utils");
|
3498
3964
|
|
3499
3965
|
// src/util/async-iterable-stream.ts
|
3500
3966
|
function createAsyncIterableStream(source) {
|
@@ -3550,7 +4016,7 @@ var objectOutputStrategy = (schema) => ({
|
|
3550
4016
|
};
|
3551
4017
|
},
|
3552
4018
|
async validateFinalResult(value) {
|
3553
|
-
return (0,
|
4019
|
+
return (0, import_provider_utils14.safeValidateTypes)({ value, schema });
|
3554
4020
|
},
|
3555
4021
|
createElementStream() {
|
3556
4022
|
throw new import_provider20.UnsupportedFunctionalityError({
|
@@ -3594,7 +4060,7 @@ var arrayOutputStrategy = (schema) => {
|
|
3594
4060
|
const resultArray = [];
|
3595
4061
|
for (let i = 0; i < inputArray.length; i++) {
|
3596
4062
|
const element = inputArray[i];
|
3597
|
-
const result = await (0,
|
4063
|
+
const result = await (0, import_provider_utils14.safeValidateTypes)({ value: element, schema });
|
3598
4064
|
if (i === inputArray.length - 1 && !isFinalDelta) {
|
3599
4065
|
continue;
|
3600
4066
|
}
|
@@ -3635,7 +4101,7 @@ var arrayOutputStrategy = (schema) => {
|
|
3635
4101
|
}
|
3636
4102
|
const inputArray = value.elements;
|
3637
4103
|
for (const element of inputArray) {
|
3638
|
-
const result = await (0,
|
4104
|
+
const result = await (0, import_provider_utils14.safeValidateTypes)({ value: element, schema });
|
3639
4105
|
if (!result.success) {
|
3640
4106
|
return result;
|
3641
4107
|
}
|
@@ -3753,9 +4219,9 @@ function getOutputStrategy({
|
|
3753
4219
|
}) {
|
3754
4220
|
switch (output) {
|
3755
4221
|
case "object":
|
3756
|
-
return objectOutputStrategy((0,
|
4222
|
+
return objectOutputStrategy((0, import_provider_utils14.asSchema)(schema));
|
3757
4223
|
case "array":
|
3758
|
-
return arrayOutputStrategy((0,
|
4224
|
+
return arrayOutputStrategy((0, import_provider_utils14.asSchema)(schema));
|
3759
4225
|
case "enum":
|
3760
4226
|
return enumOutputStrategy(enumValues);
|
3761
4227
|
case "no-schema":
|
@@ -3886,7 +4352,7 @@ function validateObjectGenerationInput({
|
|
3886
4352
|
}
|
3887
4353
|
|
3888
4354
|
// core/generate-object/generate-object.ts
|
3889
|
-
var originalGenerateId = (0,
|
4355
|
+
var originalGenerateId = (0, import_provider_utils15.createIdGenerator)({ prefix: "aiobj", size: 24 });
|
3890
4356
|
async function generateObject(options) {
|
3891
4357
|
const {
|
3892
4358
|
model,
|
@@ -4062,7 +4528,7 @@ async function generateObject(options) {
|
|
4062
4528
|
request = (_a17 = generateResult.request) != null ? _a17 : {};
|
4063
4529
|
response = generateResult.responseData;
|
4064
4530
|
async function processResult(result2) {
|
4065
|
-
const parseResult = await (0,
|
4531
|
+
const parseResult = await (0, import_provider_utils15.safeParseJSON)({ text: result2 });
|
4066
4532
|
if (!parseResult.success) {
|
4067
4533
|
throw new NoObjectGeneratedError({
|
4068
4534
|
message: "No object generated: could not parse the response.",
|
@@ -4158,7 +4624,7 @@ var DefaultGenerateObjectResult = class {
|
|
4158
4624
|
};
|
4159
4625
|
|
4160
4626
|
// core/generate-object/stream-object.ts
|
4161
|
-
var
|
4627
|
+
var import_provider_utils16 = require("@ai-sdk/provider-utils");
|
4162
4628
|
|
4163
4629
|
// src/util/create-resolvable-promise.ts
|
4164
4630
|
function createResolvablePromise() {
|
@@ -4302,7 +4768,7 @@ function now() {
|
|
4302
4768
|
}
|
4303
4769
|
|
4304
4770
|
// core/generate-object/stream-object.ts
|
4305
|
-
var originalGenerateId2 = (0,
|
4771
|
+
var originalGenerateId2 = (0, import_provider_utils16.createIdGenerator)({ prefix: "aiobj", size: 24 });
|
4306
4772
|
function streamObject(options) {
|
4307
4773
|
const {
|
4308
4774
|
model,
|
@@ -4897,10 +5363,10 @@ var DefaultSpeechResult = class {
|
|
4897
5363
|
};
|
4898
5364
|
|
4899
5365
|
// core/generate-text/generate-text.ts
|
4900
|
-
var
|
5366
|
+
var import_provider_utils19 = require("@ai-sdk/provider-utils");
|
4901
5367
|
|
4902
5368
|
// core/prompt/prepare-tools-and-tool-choice.ts
|
4903
|
-
var
|
5369
|
+
var import_provider_utils17 = require("@ai-sdk/provider-utils");
|
4904
5370
|
|
4905
5371
|
// src/util/is-non-empty-object.ts
|
4906
5372
|
function isNonEmptyObject(object2) {
|
@@ -4932,7 +5398,7 @@ function prepareToolsAndToolChoice({
|
|
4932
5398
|
type: "function",
|
4933
5399
|
name: name17,
|
4934
5400
|
description: tool2.description,
|
4935
|
-
parameters: (0,
|
5401
|
+
parameters: (0, import_provider_utils17.asSchema)(tool2.parameters).jsonSchema
|
4936
5402
|
};
|
4937
5403
|
case "provider-defined":
|
4938
5404
|
return {
|
@@ -5002,7 +5468,7 @@ function asContent({
|
|
5002
5468
|
}
|
5003
5469
|
|
5004
5470
|
// core/generate-text/parse-tool-call.ts
|
5005
|
-
var
|
5471
|
+
var import_provider_utils18 = require("@ai-sdk/provider-utils");
|
5006
5472
|
async function parseToolCall({
|
5007
5473
|
toolCall,
|
5008
5474
|
tools,
|
@@ -5026,7 +5492,7 @@ async function parseToolCall({
|
|
5026
5492
|
tools,
|
5027
5493
|
parameterSchema: ({ toolName }) => {
|
5028
5494
|
const { parameters } = tools[toolName];
|
5029
|
-
return (0,
|
5495
|
+
return (0, import_provider_utils18.asSchema)(parameters).jsonSchema;
|
5030
5496
|
},
|
5031
5497
|
system,
|
5032
5498
|
messages,
|
@@ -5056,8 +5522,8 @@ async function doParseToolCall({
|
|
5056
5522
|
availableTools: Object.keys(tools)
|
5057
5523
|
});
|
5058
5524
|
}
|
5059
|
-
const schema = (0,
|
5060
|
-
const parseResult = toolCall.args.trim() === "" ? await (0,
|
5525
|
+
const schema = (0, import_provider_utils18.asSchema)(tool2.parameters);
|
5526
|
+
const parseResult = toolCall.args.trim() === "" ? await (0, import_provider_utils18.safeValidateTypes)({ value: {}, schema }) : await (0, import_provider_utils18.safeParseJSON)({ text: toolCall.args, schema });
|
5061
5527
|
if (parseResult.success === false) {
|
5062
5528
|
throw new InvalidToolArgumentsError({
|
5063
5529
|
toolName,
|
@@ -5115,6 +5581,19 @@ var DefaultStepResult = class {
|
|
5115
5581
|
}
|
5116
5582
|
};
|
5117
5583
|
|
5584
|
+
// core/generate-text/stop-condition.ts
|
5585
|
+
function maxSteps(maxSteps2) {
|
5586
|
+
return ({ steps }) => steps.length >= maxSteps2;
|
5587
|
+
}
|
5588
|
+
function hasToolCall(toolName) {
|
5589
|
+
return ({ steps }) => {
|
5590
|
+
var _a17, _b, _c;
|
5591
|
+
return (_c = (_b = (_a17 = steps[steps.length - 1]) == null ? void 0 : _a17.toolCalls) == null ? void 0 : _b.some(
|
5592
|
+
(toolCall) => toolCall.toolName === toolName
|
5593
|
+
)) != null ? _c : false;
|
5594
|
+
};
|
5595
|
+
}
|
5596
|
+
|
5118
5597
|
// core/generate-text/to-response-messages.ts
|
5119
5598
|
function toResponseMessages({
|
5120
5599
|
content: inputContent,
|
@@ -5174,7 +5653,7 @@ function toResponseMessages({
|
|
5174
5653
|
}
|
5175
5654
|
|
5176
5655
|
// core/generate-text/generate-text.ts
|
5177
|
-
var originalGenerateId3 = (0,
|
5656
|
+
var originalGenerateId3 = (0, import_provider_utils19.createIdGenerator)({
|
5178
5657
|
prefix: "aitxt",
|
5179
5658
|
size: 24
|
5180
5659
|
});
|
@@ -5188,7 +5667,7 @@ async function generateText({
|
|
5188
5667
|
maxRetries: maxRetriesArg,
|
5189
5668
|
abortSignal,
|
5190
5669
|
headers,
|
5191
|
-
|
5670
|
+
continueUntil = maxSteps(1),
|
5192
5671
|
experimental_output: output,
|
5193
5672
|
experimental_telemetry: telemetry,
|
5194
5673
|
providerOptions,
|
@@ -5202,13 +5681,6 @@ async function generateText({
|
|
5202
5681
|
onStepFinish,
|
5203
5682
|
...settings
|
5204
5683
|
}) {
|
5205
|
-
if (maxSteps < 1) {
|
5206
|
-
throw new InvalidArgumentError({
|
5207
|
-
parameter: "maxSteps",
|
5208
|
-
value: maxSteps,
|
5209
|
-
message: "maxSteps must be at least 1"
|
5210
|
-
});
|
5211
|
-
}
|
5212
5684
|
const { maxRetries, retry } = prepareRetries({ maxRetries: maxRetriesArg });
|
5213
5685
|
const callSettings = prepareCallSettings(settings);
|
5214
5686
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
@@ -5239,8 +5711,7 @@ async function generateText({
|
|
5239
5711
|
// specific settings that only make sense on the outer level:
|
5240
5712
|
"ai.prompt": {
|
5241
5713
|
input: () => JSON.stringify({ system, prompt, messages })
|
5242
|
-
}
|
5243
|
-
"ai.settings.maxSteps": maxSteps
|
5714
|
+
}
|
5244
5715
|
}
|
5245
5716
|
}),
|
5246
5717
|
tracer,
|
@@ -5250,7 +5721,6 @@ async function generateText({
|
|
5250
5721
|
let currentModelResponse;
|
5251
5722
|
let currentToolCalls = [];
|
5252
5723
|
let currentToolResults = [];
|
5253
|
-
let stepCount = 0;
|
5254
5724
|
const responseMessages = [];
|
5255
5725
|
const steps = [];
|
5256
5726
|
do {
|
@@ -5261,8 +5731,7 @@ async function generateText({
|
|
5261
5731
|
const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
|
5262
5732
|
model,
|
5263
5733
|
steps,
|
5264
|
-
|
5265
|
-
stepNumber: stepCount
|
5734
|
+
stepNumber: steps.length
|
5266
5735
|
}));
|
5267
5736
|
const promptMessages = await convertToLanguageModelPrompt({
|
5268
5737
|
prompt: {
|
@@ -5417,9 +5886,12 @@ async function generateText({
|
|
5417
5886
|
});
|
5418
5887
|
steps.push(currentStepResult);
|
5419
5888
|
await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
|
5420
|
-
} while (
|
5421
|
-
|
5422
|
-
|
5889
|
+
} while (
|
5890
|
+
// there are tool calls:
|
5891
|
+
currentToolCalls.length > 0 && // all current tool calls have results:
|
5892
|
+
currentToolResults.length === currentToolCalls.length && // continue until the stop condition is met:
|
5893
|
+
!await continueUntil({ steps })
|
5894
|
+
);
|
5423
5895
|
span.setAttributes(
|
5424
5896
|
selectTelemetryAttributes({
|
5425
5897
|
telemetry,
|
@@ -5622,7 +6094,7 @@ __export(output_exports, {
|
|
5622
6094
|
object: () => object,
|
5623
6095
|
text: () => text
|
5624
6096
|
});
|
5625
|
-
var
|
6097
|
+
var import_provider_utils20 = require("@ai-sdk/provider-utils");
|
5626
6098
|
var text = () => ({
|
5627
6099
|
type: "text",
|
5628
6100
|
responseFormat: { type: "text" },
|
@@ -5636,7 +6108,7 @@ var text = () => ({
|
|
5636
6108
|
var object = ({
|
5637
6109
|
schema: inputSchema
|
5638
6110
|
}) => {
|
5639
|
-
const schema = (0,
|
6111
|
+
const schema = (0, import_provider_utils20.asSchema)(inputSchema);
|
5640
6112
|
return {
|
5641
6113
|
type: "object",
|
5642
6114
|
responseFormat: {
|
@@ -5662,7 +6134,7 @@ var object = ({
|
|
5662
6134
|
}
|
5663
6135
|
},
|
5664
6136
|
async parseOutput({ text: text2 }, context) {
|
5665
|
-
const parseResult = await (0,
|
6137
|
+
const parseResult = await (0, import_provider_utils20.safeParseJSON)({ text: text2 });
|
5666
6138
|
if (!parseResult.success) {
|
5667
6139
|
throw new NoObjectGeneratedError({
|
5668
6140
|
message: "No object generated: could not parse the response.",
|
@@ -5673,7 +6145,7 @@ var object = ({
|
|
5673
6145
|
finishReason: context.finishReason
|
5674
6146
|
});
|
5675
6147
|
}
|
5676
|
-
const validationResult = await (0,
|
6148
|
+
const validationResult = await (0, import_provider_utils20.safeValidateTypes)({
|
5677
6149
|
value: parseResult.value,
|
5678
6150
|
schema
|
5679
6151
|
});
|
@@ -5693,7 +6165,7 @@ var object = ({
|
|
5693
6165
|
};
|
5694
6166
|
|
5695
6167
|
// core/generate-text/smooth-stream.ts
|
5696
|
-
var
|
6168
|
+
var import_provider_utils21 = require("@ai-sdk/provider-utils");
|
5697
6169
|
var import_provider23 = require("@ai-sdk/provider");
|
5698
6170
|
var CHUNKING_REGEXPS = {
|
5699
6171
|
word: /\S+\s+/m,
|
@@ -5702,7 +6174,7 @@ var CHUNKING_REGEXPS = {
|
|
5702
6174
|
function smoothStream({
|
5703
6175
|
delayInMs = 10,
|
5704
6176
|
chunking = "word",
|
5705
|
-
_internal: { delay: delay2 =
|
6177
|
+
_internal: { delay: delay2 = import_provider_utils21.delay } = {}
|
5706
6178
|
} = {}) {
|
5707
6179
|
let detectChunk;
|
5708
6180
|
if (typeof chunking === "function") {
|
@@ -5762,7 +6234,7 @@ function smoothStream({
|
|
5762
6234
|
}
|
5763
6235
|
|
5764
6236
|
// core/generate-text/stream-text.ts
|
5765
|
-
var
|
6237
|
+
var import_provider_utils23 = require("@ai-sdk/provider-utils");
|
5766
6238
|
|
5767
6239
|
// src/util/as-array.ts
|
5768
6240
|
function asArray(value) {
|
@@ -5770,7 +6242,7 @@ function asArray(value) {
|
|
5770
6242
|
}
|
5771
6243
|
|
5772
6244
|
// core/generate-text/run-tools-transformation.ts
|
5773
|
-
var
|
6245
|
+
var import_provider_utils22 = require("@ai-sdk/provider-utils");
|
5774
6246
|
function runToolsTransformation({
|
5775
6247
|
tools,
|
5776
6248
|
generatorStream,
|
@@ -5856,7 +6328,7 @@ function runToolsTransformation({
|
|
5856
6328
|
controller.enqueue(toolCall);
|
5857
6329
|
const tool2 = tools[toolCall.toolName];
|
5858
6330
|
if (tool2.execute != null) {
|
5859
|
-
const toolExecutionId = (0,
|
6331
|
+
const toolExecutionId = (0, import_provider_utils22.generateId)();
|
5860
6332
|
outstandingToolResults.add(toolExecutionId);
|
5861
6333
|
recordSpan({
|
5862
6334
|
name: "ai.toolCall",
|
@@ -5965,7 +6437,7 @@ function runToolsTransformation({
|
|
5965
6437
|
}
|
5966
6438
|
|
5967
6439
|
// core/generate-text/stream-text.ts
|
5968
|
-
var originalGenerateId4 = (0,
|
6440
|
+
var originalGenerateId4 = (0, import_provider_utils23.createIdGenerator)({
|
5969
6441
|
prefix: "aitxt",
|
5970
6442
|
size: 24
|
5971
6443
|
});
|
@@ -5979,7 +6451,7 @@ function streamText({
|
|
5979
6451
|
maxRetries,
|
5980
6452
|
abortSignal,
|
5981
6453
|
headers,
|
5982
|
-
maxSteps = 1,
|
6454
|
+
maxSteps: maxSteps2 = 1,
|
5983
6455
|
experimental_output: output,
|
5984
6456
|
experimental_telemetry: telemetry,
|
5985
6457
|
providerOptions,
|
@@ -6015,7 +6487,7 @@ function streamText({
|
|
6015
6487
|
transforms: asArray(transform),
|
6016
6488
|
activeTools,
|
6017
6489
|
repairToolCall,
|
6018
|
-
maxSteps,
|
6490
|
+
maxSteps: maxSteps2,
|
6019
6491
|
output,
|
6020
6492
|
providerOptions,
|
6021
6493
|
onChunk,
|
@@ -6092,7 +6564,7 @@ var DefaultStreamTextResult = class {
|
|
6092
6564
|
transforms,
|
6093
6565
|
activeTools,
|
6094
6566
|
repairToolCall,
|
6095
|
-
maxSteps,
|
6567
|
+
maxSteps: maxSteps2,
|
6096
6568
|
output,
|
6097
6569
|
providerOptions,
|
6098
6570
|
now: now2,
|
@@ -6106,10 +6578,10 @@ var DefaultStreamTextResult = class {
|
|
6106
6578
|
this.totalUsagePromise = new DelayedPromise();
|
6107
6579
|
this.finishReasonPromise = new DelayedPromise();
|
6108
6580
|
this.stepsPromise = new DelayedPromise();
|
6109
|
-
if (
|
6581
|
+
if (maxSteps2 < 1) {
|
6110
6582
|
throw new InvalidArgumentError({
|
6111
6583
|
parameter: "maxSteps",
|
6112
|
-
value:
|
6584
|
+
value: maxSteps2,
|
6113
6585
|
message: "maxSteps must be at least 1"
|
6114
6586
|
});
|
6115
6587
|
}
|
@@ -6266,6 +6738,13 @@ var DefaultStreamTextResult = class {
|
|
6266
6738
|
this.addStream = stitchableStream.addStream;
|
6267
6739
|
this.closeStream = stitchableStream.close;
|
6268
6740
|
let stream = stitchableStream.stream;
|
6741
|
+
stream = stream.pipeThrough(
|
6742
|
+
new TransformStream({
|
6743
|
+
start(controller) {
|
6744
|
+
controller.enqueue({ type: "start" });
|
6745
|
+
}
|
6746
|
+
})
|
6747
|
+
);
|
6269
6748
|
for (const transform of transforms) {
|
6270
6749
|
stream = stream.pipeThrough(
|
6271
6750
|
transform({
|
@@ -6300,7 +6779,7 @@ var DefaultStreamTextResult = class {
|
|
6300
6779
|
"ai.prompt": {
|
6301
6780
|
input: () => JSON.stringify({ system, prompt, messages })
|
6302
6781
|
},
|
6303
|
-
"ai.settings.maxSteps":
|
6782
|
+
"ai.settings.maxSteps": maxSteps2
|
6304
6783
|
}
|
6305
6784
|
}),
|
6306
6785
|
tracer,
|
@@ -6438,7 +6917,6 @@ var DefaultStreamTextResult = class {
|
|
6438
6917
|
var _a17, _b, _c, _d;
|
6439
6918
|
if (chunk.type === "stream-start") {
|
6440
6919
|
warnings = chunk.warnings;
|
6441
|
-
controller.enqueue({ type: "start" });
|
6442
6920
|
return;
|
6443
6921
|
}
|
6444
6922
|
if (stepFirstChunk) {
|
@@ -6588,7 +7066,7 @@ var DefaultStreamTextResult = class {
|
|
6588
7066
|
}
|
6589
7067
|
});
|
6590
7068
|
const combinedUsage = addLanguageModelUsage(usage, stepUsage);
|
6591
|
-
if (currentStep + 1 <
|
7069
|
+
if (currentStep + 1 < maxSteps2 && // there are tool calls:
|
6592
7070
|
stepToolCalls.length > 0 && // all current tool calls have results:
|
6593
7071
|
stepToolResults.length === stepToolCalls.length) {
|
6594
7072
|
responseMessages.push(
|
@@ -6773,125 +7251,122 @@ var DefaultStreamTextResult = class {
|
|
6773
7251
|
const partType = part.type;
|
6774
7252
|
switch (partType) {
|
6775
7253
|
case "text": {
|
6776
|
-
controller.enqueue({
|
7254
|
+
controller.enqueue({
|
7255
|
+
type: "text",
|
7256
|
+
text: part.text
|
7257
|
+
});
|
6777
7258
|
break;
|
6778
7259
|
}
|
6779
7260
|
case "reasoning": {
|
6780
7261
|
if (sendReasoning) {
|
6781
|
-
controller.enqueue({
|
7262
|
+
controller.enqueue({
|
7263
|
+
type: "reasoning",
|
7264
|
+
text: part.text,
|
7265
|
+
providerMetadata: part.providerMetadata
|
7266
|
+
});
|
6782
7267
|
}
|
6783
7268
|
break;
|
6784
7269
|
}
|
6785
7270
|
case "reasoning-part-finish": {
|
6786
7271
|
if (sendReasoning) {
|
6787
|
-
controller.enqueue({
|
6788
|
-
type: "reasoning-part-finish",
|
6789
|
-
value: null
|
6790
|
-
});
|
7272
|
+
controller.enqueue({ type: "reasoning-part-finish" });
|
6791
7273
|
}
|
6792
7274
|
break;
|
6793
7275
|
}
|
6794
7276
|
case "file": {
|
6795
7277
|
controller.enqueue({
|
6796
7278
|
type: "file",
|
6797
|
-
|
6798
|
-
|
6799
|
-
url: `data:${part.file.mediaType};base64,${part.file.base64}`
|
6800
|
-
}
|
7279
|
+
mediaType: part.file.mediaType,
|
7280
|
+
url: `data:${part.file.mediaType};base64,${part.file.base64}`
|
6801
7281
|
});
|
6802
7282
|
break;
|
6803
7283
|
}
|
6804
7284
|
case "source": {
|
6805
7285
|
if (sendSources) {
|
6806
|
-
controller.enqueue({
|
7286
|
+
controller.enqueue({
|
7287
|
+
type: "source",
|
7288
|
+
sourceType: part.sourceType,
|
7289
|
+
id: part.id,
|
7290
|
+
url: part.url,
|
7291
|
+
title: part.title,
|
7292
|
+
providerMetadata: part.providerMetadata
|
7293
|
+
});
|
6807
7294
|
}
|
6808
7295
|
break;
|
6809
7296
|
}
|
6810
7297
|
case "tool-call-streaming-start": {
|
6811
7298
|
controller.enqueue({
|
6812
7299
|
type: "tool-call-streaming-start",
|
6813
|
-
|
6814
|
-
|
6815
|
-
toolName: part.toolName
|
6816
|
-
}
|
7300
|
+
toolCallId: part.toolCallId,
|
7301
|
+
toolName: part.toolName
|
6817
7302
|
});
|
6818
7303
|
break;
|
6819
7304
|
}
|
6820
7305
|
case "tool-call-delta": {
|
6821
7306
|
controller.enqueue({
|
6822
7307
|
type: "tool-call-delta",
|
6823
|
-
|
6824
|
-
|
6825
|
-
argsTextDelta: part.argsTextDelta
|
6826
|
-
}
|
7308
|
+
toolCallId: part.toolCallId,
|
7309
|
+
argsTextDelta: part.argsTextDelta
|
6827
7310
|
});
|
6828
7311
|
break;
|
6829
7312
|
}
|
6830
7313
|
case "tool-call": {
|
6831
7314
|
controller.enqueue({
|
6832
7315
|
type: "tool-call",
|
6833
|
-
|
6834
|
-
|
6835
|
-
|
6836
|
-
args: part.args
|
6837
|
-
}
|
7316
|
+
toolCallId: part.toolCallId,
|
7317
|
+
toolName: part.toolName,
|
7318
|
+
args: part.args
|
6838
7319
|
});
|
6839
7320
|
break;
|
6840
7321
|
}
|
6841
7322
|
case "tool-result": {
|
6842
7323
|
controller.enqueue({
|
6843
7324
|
type: "tool-result",
|
6844
|
-
|
6845
|
-
|
6846
|
-
result: part.result
|
6847
|
-
}
|
7325
|
+
toolCallId: part.toolCallId,
|
7326
|
+
result: part.result
|
6848
7327
|
});
|
6849
7328
|
break;
|
6850
7329
|
}
|
6851
7330
|
case "error": {
|
6852
7331
|
controller.enqueue({
|
6853
7332
|
type: "error",
|
6854
|
-
|
7333
|
+
errorText: onError(part.error)
|
6855
7334
|
});
|
6856
7335
|
break;
|
6857
7336
|
}
|
6858
7337
|
case "start-step": {
|
7338
|
+
const metadata = messageMetadata == null ? void 0 : messageMetadata({ part });
|
6859
7339
|
controller.enqueue({
|
6860
7340
|
type: "start-step",
|
6861
|
-
|
6862
|
-
metadata: messageMetadata == null ? void 0 : messageMetadata({ part })
|
6863
|
-
}
|
7341
|
+
metadata
|
6864
7342
|
});
|
6865
7343
|
break;
|
6866
7344
|
}
|
6867
7345
|
case "finish-step": {
|
7346
|
+
const metadata = messageMetadata == null ? void 0 : messageMetadata({ part });
|
6868
7347
|
controller.enqueue({
|
6869
7348
|
type: "finish-step",
|
6870
|
-
|
6871
|
-
metadata: messageMetadata == null ? void 0 : messageMetadata({ part })
|
6872
|
-
}
|
7349
|
+
metadata
|
6873
7350
|
});
|
6874
7351
|
break;
|
6875
7352
|
}
|
6876
7353
|
case "start": {
|
6877
7354
|
if (experimental_sendStart) {
|
7355
|
+
const metadata = messageMetadata == null ? void 0 : messageMetadata({ part });
|
6878
7356
|
controller.enqueue({
|
6879
7357
|
type: "start",
|
6880
|
-
|
6881
|
-
|
6882
|
-
metadata: messageMetadata == null ? void 0 : messageMetadata({ part })
|
6883
|
-
}
|
7358
|
+
messageId,
|
7359
|
+
metadata
|
6884
7360
|
});
|
6885
7361
|
}
|
6886
7362
|
break;
|
6887
7363
|
}
|
6888
7364
|
case "finish": {
|
6889
7365
|
if (experimental_sendFinish) {
|
7366
|
+
const metadata = messageMetadata == null ? void 0 : messageMetadata({ part });
|
6890
7367
|
controller.enqueue({
|
6891
7368
|
type: "finish",
|
6892
|
-
|
6893
|
-
metadata: messageMetadata == null ? void 0 : messageMetadata({ part })
|
6894
|
-
}
|
7369
|
+
metadata
|
6895
7370
|
});
|
6896
7371
|
}
|
6897
7372
|
break;
|
@@ -6904,22 +7379,38 @@ var DefaultStreamTextResult = class {
|
|
6904
7379
|
}
|
6905
7380
|
})
|
6906
7381
|
);
|
6907
|
-
|
6908
|
-
|
7382
|
+
if (onFinish == null) {
|
7383
|
+
return baseStream;
|
7384
|
+
}
|
7385
|
+
const state = createStreamingUIMessageState({
|
6909
7386
|
lastMessage,
|
6910
|
-
newMessageId: messageId != null ? messageId : this.generateId()
|
6911
|
-
onFinish: ({ message }) => {
|
6912
|
-
const isContinuation2 = message.id === (lastMessage == null ? void 0 : lastMessage.id);
|
6913
|
-
onFinish({
|
6914
|
-
isContinuation: isContinuation2,
|
6915
|
-
responseMessage: message,
|
6916
|
-
messages: [
|
6917
|
-
...isContinuation2 ? originalMessages.slice(0, -1) : originalMessages,
|
6918
|
-
message
|
6919
|
-
]
|
6920
|
-
});
|
6921
|
-
}
|
7387
|
+
newMessageId: messageId != null ? messageId : this.generateId()
|
6922
7388
|
});
|
7389
|
+
const runUpdateMessageJob = async (job) => {
|
7390
|
+
await job({ state, write: () => {
|
7391
|
+
} });
|
7392
|
+
};
|
7393
|
+
return processUIMessageStream({
|
7394
|
+
stream: baseStream,
|
7395
|
+
runUpdateMessageJob
|
7396
|
+
}).pipeThrough(
|
7397
|
+
new TransformStream({
|
7398
|
+
transform(chunk, controller) {
|
7399
|
+
controller.enqueue(chunk);
|
7400
|
+
},
|
7401
|
+
flush() {
|
7402
|
+
const isContinuation2 = state.message.id === (lastMessage == null ? void 0 : lastMessage.id);
|
7403
|
+
onFinish({
|
7404
|
+
isContinuation: isContinuation2,
|
7405
|
+
responseMessage: state.message,
|
7406
|
+
messages: [
|
7407
|
+
...isContinuation2 ? originalMessages.slice(0, -1) : originalMessages,
|
7408
|
+
state.message
|
7409
|
+
]
|
7410
|
+
});
|
7411
|
+
}
|
7412
|
+
})
|
7413
|
+
);
|
6923
7414
|
}
|
6924
7415
|
pipeUIMessageStreamToResponse(response, {
|
6925
7416
|
newMessageId,
|
@@ -7367,7 +7858,7 @@ var DefaultProviderRegistry = class {
|
|
7367
7858
|
};
|
7368
7859
|
|
7369
7860
|
// core/tool/mcp/mcp-client.ts
|
7370
|
-
var
|
7861
|
+
var import_provider_utils25 = require("@ai-sdk/provider-utils");
|
7371
7862
|
|
7372
7863
|
// core/tool/tool.ts
|
7373
7864
|
function tool(tool2) {
|
@@ -7375,7 +7866,7 @@ function tool(tool2) {
|
|
7375
7866
|
}
|
7376
7867
|
|
7377
7868
|
// core/tool/mcp/mcp-sse-transport.ts
|
7378
|
-
var
|
7869
|
+
var import_provider_utils24 = require("@ai-sdk/provider-utils");
|
7379
7870
|
|
7380
7871
|
// core/tool/mcp/json-rpc-message.ts
|
7381
7872
|
var import_zod10 = require("zod");
|
@@ -7546,7 +8037,7 @@ var SseMCPTransport = class {
|
|
7546
8037
|
(_b = this.onerror) == null ? void 0 : _b.call(this, error);
|
7547
8038
|
return reject(error);
|
7548
8039
|
}
|
7549
|
-
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough((0,
|
8040
|
+
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough((0, import_provider_utils24.createEventSourceParserStream)());
|
7550
8041
|
const reader = stream.getReader();
|
7551
8042
|
const processEvents = async () => {
|
7552
8043
|
var _a18, _b2, _c2;
|
@@ -7870,7 +8361,7 @@ var MCPClient = class {
|
|
7870
8361
|
if (schemas !== "automatic" && !(name17 in schemas)) {
|
7871
8362
|
continue;
|
7872
8363
|
}
|
7873
|
-
const parameters = schemas === "automatic" ? (0,
|
8364
|
+
const parameters = schemas === "automatic" ? (0, import_provider_utils25.jsonSchema)({
|
7874
8365
|
...inputSchema,
|
7875
8366
|
properties: (_a17 = inputSchema.properties) != null ? _a17 : {},
|
7876
8367
|
additionalProperties: false
|
@@ -8000,6 +8491,8 @@ var DefaultTranscriptionResult = class {
|
|
8000
8491
|
0 && (module.exports = {
|
8001
8492
|
AISDKError,
|
8002
8493
|
APICallError,
|
8494
|
+
ChatStore,
|
8495
|
+
DefaultChatTransport,
|
8003
8496
|
DownloadError,
|
8004
8497
|
EmptyResponseBodyError,
|
8005
8498
|
InvalidArgumentError,
|
@@ -8047,6 +8540,7 @@ var DefaultTranscriptionResult = class {
|
|
8047
8540
|
createUIMessageStream,
|
8048
8541
|
createUIMessageStreamResponse,
|
8049
8542
|
customProvider,
|
8543
|
+
defaultChatStore,
|
8050
8544
|
defaultSettingsMiddleware,
|
8051
8545
|
embed,
|
8052
8546
|
embedMany,
|
@@ -8063,14 +8557,15 @@ var DefaultTranscriptionResult = class {
|
|
8063
8557
|
generateText,
|
8064
8558
|
getTextFromDataUrl,
|
8065
8559
|
getToolInvocations,
|
8560
|
+
hasToolCall,
|
8066
8561
|
isAssistantMessageWithCompletedToolCalls,
|
8067
8562
|
isDeepEqualData,
|
8068
8563
|
jsonSchema,
|
8564
|
+
maxSteps,
|
8069
8565
|
modelMessageSchema,
|
8070
8566
|
parsePartialJson,
|
8071
8567
|
pipeTextStreamToResponse,
|
8072
8568
|
pipeUIMessageStreamToResponse,
|
8073
|
-
processTextStream,
|
8074
8569
|
shouldResubmitMessages,
|
8075
8570
|
simulateReadableStream,
|
8076
8571
|
simulateStreamingMiddleware,
|