ai 5.0.0-canary.15 → 5.0.0-canary.17
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 +55 -0
- package/dist/index.d.mts +145 -120
- package/dist/index.d.ts +145 -120
- package/dist/index.js +276 -391
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +270 -390
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +32 -59
- package/dist/internal/index.d.ts +32 -59
- package/dist/internal/index.js +193 -387
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +186 -380
- package/dist/internal/index.mjs.map +1 -1
- package/dist/mcp-stdio/index.d.mts +2 -2
- package/dist/mcp-stdio/index.d.ts +2 -2
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
@@ -9,9 +9,9 @@ import { createIdGenerator as createIdGenerator5, generateId as generateId2 } fr
|
|
9
9
|
|
10
10
|
// core/util/index.ts
|
11
11
|
import {
|
12
|
+
asSchema,
|
12
13
|
generateId,
|
13
|
-
jsonSchema
|
14
|
-
asSchema
|
14
|
+
jsonSchema
|
15
15
|
} from "@ai-sdk/provider-utils";
|
16
16
|
|
17
17
|
// core/util/process-chat-response.ts
|
@@ -29,6 +29,21 @@ function calculateLanguageModelUsage({
|
|
29
29
|
};
|
30
30
|
}
|
31
31
|
|
32
|
+
// core/ui/get-tool-invocations.ts
|
33
|
+
function getToolInvocations(message) {
|
34
|
+
return message.parts.filter(
|
35
|
+
(part) => part.type === "tool-invocation"
|
36
|
+
).map((part) => part.toolInvocation);
|
37
|
+
}
|
38
|
+
|
39
|
+
// core/util/extract-max-tool-invocation-step.ts
|
40
|
+
function extractMaxToolInvocationStep(toolInvocations) {
|
41
|
+
return toolInvocations == null ? void 0 : toolInvocations.reduce((max, toolInvocation) => {
|
42
|
+
var _a17;
|
43
|
+
return Math.max(max, (_a17 = toolInvocation.step) != null ? _a17 : 0);
|
44
|
+
}, 0);
|
45
|
+
}
|
46
|
+
|
32
47
|
// core/util/parse-partial-json.ts
|
33
48
|
import { safeParseJSON } from "@ai-sdk/provider-utils";
|
34
49
|
|
@@ -571,9 +586,9 @@ var fileStreamPart = {
|
|
571
586
|
code: "k",
|
572
587
|
name: "file",
|
573
588
|
parse: (value) => {
|
574
|
-
if (value == null || typeof value !== "object" || !("
|
589
|
+
if (value == null || typeof value !== "object" || !("url" in value) || typeof value.url !== "string" || !("mediaType" in value) || typeof value.mediaType !== "string") {
|
575
590
|
throw new Error(
|
576
|
-
'"file" parts expect an object with a "
|
591
|
+
'"file" parts expect an object with a "url" and "mediaType" property.'
|
577
592
|
);
|
578
593
|
}
|
579
594
|
return { type: "file", value };
|
@@ -751,13 +766,9 @@ async function processChatResponse({
|
|
751
766
|
getCurrentDate = () => /* @__PURE__ */ new Date(),
|
752
767
|
lastMessage
|
753
768
|
}) {
|
754
|
-
var _a17
|
769
|
+
var _a17;
|
755
770
|
const replaceLastMessage = (lastMessage == null ? void 0 : lastMessage.role) === "assistant";
|
756
|
-
let step = replaceLastMessage ? 1 +
|
757
|
-
((_b = (_a17 = lastMessage.toolInvocations) == null ? void 0 : _a17.reduce((max, toolInvocation) => {
|
758
|
-
var _a18;
|
759
|
-
return Math.max(max, (_a18 = toolInvocation.step) != null ? _a18 : 0);
|
760
|
-
}, 0)) != null ? _b : 0) : 0;
|
771
|
+
let step = replaceLastMessage ? 1 + ((_a17 = extractMaxToolInvocationStep(getToolInvocations(lastMessage))) != null ? _a17 : 0) : 0;
|
761
772
|
const message = replaceLastMessage ? structuredClone(lastMessage) : {
|
762
773
|
id: generateId3(),
|
763
774
|
createdAt: getCurrentDate(),
|
@@ -830,12 +841,12 @@ async function processChatResponse({
|
|
830
841
|
if (currentReasoningPart == null) {
|
831
842
|
currentReasoningPart = {
|
832
843
|
type: "reasoning",
|
833
|
-
|
844
|
+
text: value.text,
|
834
845
|
providerMetadata: value.providerMetadata
|
835
846
|
};
|
836
847
|
message.parts.push(currentReasoningPart);
|
837
848
|
} else {
|
838
|
-
currentReasoningPart.
|
849
|
+
currentReasoningPart.text += value.text;
|
839
850
|
currentReasoningPart.providerMetadata = value.providerMetadata;
|
840
851
|
}
|
841
852
|
execUpdate();
|
@@ -848,8 +859,8 @@ async function processChatResponse({
|
|
848
859
|
onFilePart(value) {
|
849
860
|
message.parts.push({
|
850
861
|
type: "file",
|
851
|
-
mediaType: value.
|
852
|
-
|
862
|
+
mediaType: value.mediaType,
|
863
|
+
url: value.url
|
853
864
|
});
|
854
865
|
execUpdate();
|
855
866
|
},
|
@@ -861,24 +872,20 @@ async function processChatResponse({
|
|
861
872
|
execUpdate();
|
862
873
|
},
|
863
874
|
onToolCallStreamingStartPart(value) {
|
864
|
-
|
865
|
-
message.toolInvocations = [];
|
866
|
-
}
|
875
|
+
const toolInvocations = getToolInvocations(message);
|
867
876
|
partialToolCalls[value.toolCallId] = {
|
868
877
|
text: "",
|
869
878
|
step,
|
870
879
|
toolName: value.toolName,
|
871
|
-
index:
|
880
|
+
index: toolInvocations.length
|
872
881
|
};
|
873
|
-
|
882
|
+
updateToolInvocationPart(value.toolCallId, {
|
874
883
|
state: "partial-call",
|
875
884
|
step,
|
876
885
|
toolCallId: value.toolCallId,
|
877
886
|
toolName: value.toolName,
|
878
887
|
args: void 0
|
879
|
-
};
|
880
|
-
message.toolInvocations.push(invocation);
|
881
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
888
|
+
});
|
882
889
|
execUpdate();
|
883
890
|
},
|
884
891
|
async onToolCallDeltaPart(value) {
|
@@ -887,68 +894,53 @@ async function processChatResponse({
|
|
887
894
|
const { value: partialArgs } = await parsePartialJson(
|
888
895
|
partialToolCall.text
|
889
896
|
);
|
890
|
-
|
897
|
+
updateToolInvocationPart(value.toolCallId, {
|
891
898
|
state: "partial-call",
|
892
899
|
step: partialToolCall.step,
|
893
900
|
toolCallId: value.toolCallId,
|
894
901
|
toolName: partialToolCall.toolName,
|
895
902
|
args: partialArgs
|
896
|
-
};
|
897
|
-
message.toolInvocations[partialToolCall.index] = invocation;
|
898
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
903
|
+
});
|
899
904
|
execUpdate();
|
900
905
|
},
|
901
906
|
async onToolCallPart(value) {
|
902
|
-
|
907
|
+
updateToolInvocationPart(value.toolCallId, {
|
903
908
|
state: "call",
|
904
909
|
step,
|
905
910
|
...value
|
906
|
-
};
|
907
|
-
if (partialToolCalls[value.toolCallId] != null) {
|
908
|
-
message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
|
909
|
-
} else {
|
910
|
-
if (message.toolInvocations == null) {
|
911
|
-
message.toolInvocations = [];
|
912
|
-
}
|
913
|
-
message.toolInvocations.push(invocation);
|
914
|
-
}
|
915
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
911
|
+
});
|
916
912
|
execUpdate();
|
917
913
|
if (onToolCall) {
|
918
914
|
const result = await onToolCall({ toolCall: value });
|
919
915
|
if (result != null) {
|
920
|
-
|
916
|
+
updateToolInvocationPart(value.toolCallId, {
|
921
917
|
state: "result",
|
922
918
|
step,
|
923
919
|
...value,
|
924
920
|
result
|
925
|
-
};
|
926
|
-
message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
|
927
|
-
updateToolInvocationPart(value.toolCallId, invocation2);
|
921
|
+
});
|
928
922
|
execUpdate();
|
929
923
|
}
|
930
924
|
}
|
931
925
|
},
|
932
926
|
onToolResultPart(value) {
|
933
|
-
const toolInvocations = message
|
927
|
+
const toolInvocations = getToolInvocations(message);
|
934
928
|
if (toolInvocations == null) {
|
935
929
|
throw new Error("tool_result must be preceded by a tool_call");
|
936
930
|
}
|
937
931
|
const toolInvocationIndex = toolInvocations.findIndex(
|
938
|
-
(
|
932
|
+
(invocation) => invocation.toolCallId === value.toolCallId
|
939
933
|
);
|
940
934
|
if (toolInvocationIndex === -1) {
|
941
935
|
throw new Error(
|
942
936
|
"tool_result must be preceded by a tool_call with the same toolCallId"
|
943
937
|
);
|
944
938
|
}
|
945
|
-
|
939
|
+
updateToolInvocationPart(value.toolCallId, {
|
946
940
|
...toolInvocations[toolInvocationIndex],
|
947
941
|
state: "result",
|
948
942
|
...value
|
949
|
-
};
|
950
|
-
toolInvocations[toolInvocationIndex] = invocation;
|
951
|
-
updateToolInvocationPart(value.toolCallId, invocation);
|
943
|
+
});
|
952
944
|
execUpdate();
|
953
945
|
},
|
954
946
|
onDataPart(value) {
|
@@ -1056,19 +1048,30 @@ async function callChatApi({
|
|
1056
1048
|
onToolCall,
|
1057
1049
|
generateId: generateId3,
|
1058
1050
|
fetch: fetch2 = getOriginalFetch(),
|
1059
|
-
lastMessage
|
1051
|
+
lastMessage,
|
1052
|
+
getCurrentDate,
|
1053
|
+
requestType = "generate"
|
1060
1054
|
}) {
|
1061
|
-
var _a17, _b;
|
1062
|
-
const
|
1055
|
+
var _a17, _b, _c;
|
1056
|
+
const request = requestType === "resume" ? fetch2(`${api}?chatId=${body.id}`, {
|
1057
|
+
method: "GET",
|
1058
|
+
headers: {
|
1059
|
+
"Content-Type": "application/json",
|
1060
|
+
...headers
|
1061
|
+
},
|
1062
|
+
signal: (_a17 = abortController == null ? void 0 : abortController()) == null ? void 0 : _a17.signal,
|
1063
|
+
credentials
|
1064
|
+
}) : fetch2(api, {
|
1063
1065
|
method: "POST",
|
1064
1066
|
body: JSON.stringify(body),
|
1065
1067
|
headers: {
|
1066
1068
|
"Content-Type": "application/json",
|
1067
1069
|
...headers
|
1068
1070
|
},
|
1069
|
-
signal: (
|
1071
|
+
signal: (_b = abortController == null ? void 0 : abortController()) == null ? void 0 : _b.signal,
|
1070
1072
|
credentials
|
1071
|
-
})
|
1073
|
+
});
|
1074
|
+
const response = await request.catch((err) => {
|
1072
1075
|
restoreMessagesOnFailure();
|
1073
1076
|
throw err;
|
1074
1077
|
});
|
@@ -1082,7 +1085,7 @@ async function callChatApi({
|
|
1082
1085
|
if (!response.ok) {
|
1083
1086
|
restoreMessagesOnFailure();
|
1084
1087
|
throw new Error(
|
1085
|
-
(
|
1088
|
+
(_c = await response.text()) != null ? _c : "Failed to fetch the chat response."
|
1086
1089
|
);
|
1087
1090
|
}
|
1088
1091
|
if (!response.body) {
|
@@ -1109,7 +1112,8 @@ async function callChatApi({
|
|
1109
1112
|
onFinish(message, { usage, finishReason });
|
1110
1113
|
}
|
1111
1114
|
},
|
1112
|
-
generateId: generateId3
|
1115
|
+
generateId: generateId3,
|
1116
|
+
getCurrentDate
|
1113
1117
|
});
|
1114
1118
|
return;
|
1115
1119
|
}
|
@@ -1230,6 +1234,36 @@ async function callCompletionApi({
|
|
1230
1234
|
}
|
1231
1235
|
}
|
1232
1236
|
|
1237
|
+
// core/util/convert-file-list-to-file-ui-parts.ts
|
1238
|
+
async function convertFileListToFileUIParts(files) {
|
1239
|
+
if (files == null) {
|
1240
|
+
return [];
|
1241
|
+
}
|
1242
|
+
if (!globalThis.FileList || !(files instanceof globalThis.FileList)) {
|
1243
|
+
throw new Error("FileList is not supported in the current environment");
|
1244
|
+
}
|
1245
|
+
return Promise.all(
|
1246
|
+
Array.from(files).map(async (file) => {
|
1247
|
+
const { name: name17, type } = file;
|
1248
|
+
const dataUrl = await new Promise((resolve, reject) => {
|
1249
|
+
const reader = new FileReader();
|
1250
|
+
reader.onload = (readerEvent) => {
|
1251
|
+
var _a17;
|
1252
|
+
resolve((_a17 = readerEvent.target) == null ? void 0 : _a17.result);
|
1253
|
+
};
|
1254
|
+
reader.onerror = (error) => reject(error);
|
1255
|
+
reader.readAsDataURL(file);
|
1256
|
+
});
|
1257
|
+
return {
|
1258
|
+
type: "file",
|
1259
|
+
mediaType: type,
|
1260
|
+
filename: name17,
|
1261
|
+
url: dataUrl
|
1262
|
+
};
|
1263
|
+
})
|
1264
|
+
);
|
1265
|
+
}
|
1266
|
+
|
1233
1267
|
// core/util/data-url.ts
|
1234
1268
|
function getTextFromDataUrl(dataUrl) {
|
1235
1269
|
const [header, base64Content] = dataUrl.split(",");
|
@@ -1244,34 +1278,6 @@ function getTextFromDataUrl(dataUrl) {
|
|
1244
1278
|
}
|
1245
1279
|
}
|
1246
1280
|
|
1247
|
-
// core/util/extract-max-tool-invocation-step.ts
|
1248
|
-
function extractMaxToolInvocationStep(toolInvocations) {
|
1249
|
-
return toolInvocations == null ? void 0 : toolInvocations.reduce((max, toolInvocation) => {
|
1250
|
-
var _a17;
|
1251
|
-
return Math.max(max, (_a17 = toolInvocation.step) != null ? _a17 : 0);
|
1252
|
-
}, 0);
|
1253
|
-
}
|
1254
|
-
|
1255
|
-
// core/util/get-message-parts.ts
|
1256
|
-
function getMessageParts(message) {
|
1257
|
-
var _a17;
|
1258
|
-
return (_a17 = message.parts) != null ? _a17 : [
|
1259
|
-
...message.toolInvocations ? message.toolInvocations.map((toolInvocation) => ({
|
1260
|
-
type: "tool-invocation",
|
1261
|
-
toolInvocation
|
1262
|
-
})) : [],
|
1263
|
-
...message.content ? [{ type: "text", text: message.content }] : []
|
1264
|
-
];
|
1265
|
-
}
|
1266
|
-
|
1267
|
-
// core/util/fill-message-parts.ts
|
1268
|
-
function fillMessageParts(messages) {
|
1269
|
-
return messages.map((message) => ({
|
1270
|
-
...message,
|
1271
|
-
parts: getMessageParts(message)
|
1272
|
-
}));
|
1273
|
-
}
|
1274
|
-
|
1275
1281
|
// core/util/is-deep-equal-data.ts
|
1276
1282
|
function isDeepEqualData(obj1, obj2) {
|
1277
1283
|
if (obj1 === obj2)
|
@@ -1307,63 +1313,6 @@ function isDeepEqualData(obj1, obj2) {
|
|
1307
1313
|
return true;
|
1308
1314
|
}
|
1309
1315
|
|
1310
|
-
// core/util/prepare-attachments-for-request.ts
|
1311
|
-
async function prepareAttachmentsForRequest(attachmentsFromOptions) {
|
1312
|
-
if (!attachmentsFromOptions) {
|
1313
|
-
return [];
|
1314
|
-
}
|
1315
|
-
if (attachmentsFromOptions instanceof FileList) {
|
1316
|
-
return Promise.all(
|
1317
|
-
Array.from(attachmentsFromOptions).map(async (attachment) => {
|
1318
|
-
const { name: name17, type } = attachment;
|
1319
|
-
const dataUrl = await new Promise((resolve, reject) => {
|
1320
|
-
const reader = new FileReader();
|
1321
|
-
reader.onload = (readerEvent) => {
|
1322
|
-
var _a17;
|
1323
|
-
resolve((_a17 = readerEvent.target) == null ? void 0 : _a17.result);
|
1324
|
-
};
|
1325
|
-
reader.onerror = (error) => reject(error);
|
1326
|
-
reader.readAsDataURL(attachment);
|
1327
|
-
});
|
1328
|
-
return {
|
1329
|
-
name: name17,
|
1330
|
-
contentType: type,
|
1331
|
-
url: dataUrl
|
1332
|
-
};
|
1333
|
-
})
|
1334
|
-
);
|
1335
|
-
}
|
1336
|
-
if (Array.isArray(attachmentsFromOptions)) {
|
1337
|
-
return attachmentsFromOptions;
|
1338
|
-
}
|
1339
|
-
throw new Error("Invalid attachments type");
|
1340
|
-
}
|
1341
|
-
|
1342
|
-
// core/util/update-tool-call-result.ts
|
1343
|
-
function updateToolCallResult({
|
1344
|
-
messages,
|
1345
|
-
toolCallId,
|
1346
|
-
toolResult: result
|
1347
|
-
}) {
|
1348
|
-
var _a17;
|
1349
|
-
const lastMessage = messages[messages.length - 1];
|
1350
|
-
const invocationPart = lastMessage.parts.find(
|
1351
|
-
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
|
1352
|
-
);
|
1353
|
-
if (invocationPart == null) {
|
1354
|
-
return;
|
1355
|
-
}
|
1356
|
-
const toolResult = {
|
1357
|
-
...invocationPart.toolInvocation,
|
1358
|
-
state: "result",
|
1359
|
-
result
|
1360
|
-
};
|
1361
|
-
invocationPart.toolInvocation = toolResult;
|
1362
|
-
lastMessage.toolInvocations = (_a17 = lastMessage.toolInvocations) == null ? void 0 : _a17.map(
|
1363
|
-
(toolInvocation) => toolInvocation.toolCallId === toolCallId ? toolResult : toolInvocation
|
1364
|
-
);
|
1365
|
-
}
|
1366
|
-
|
1367
1316
|
// core/util/should-resubmit-messages.ts
|
1368
1317
|
function shouldResubmitMessages({
|
1369
1318
|
originalMaxToolInvocationStep,
|
@@ -1377,9 +1326,9 @@ function shouldResubmitMessages({
|
|
1377
1326
|
// check if the feature is enabled:
|
1378
1327
|
maxSteps > 1 && // ensure there is a last message:
|
1379
1328
|
lastMessage != null && // ensure we actually have new steps (to prevent infinite loops in case of errors):
|
1380
|
-
(messages.length > originalMessageCount || extractMaxToolInvocationStep(lastMessage
|
1329
|
+
(messages.length > originalMessageCount || extractMaxToolInvocationStep(getToolInvocations(lastMessage)) !== originalMaxToolInvocationStep) && // check that next step is possible:
|
1381
1330
|
isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic steps:
|
1382
|
-
((_a17 = extractMaxToolInvocationStep(lastMessage
|
1331
|
+
((_a17 = extractMaxToolInvocationStep(getToolInvocations(lastMessage))) != null ? _a17 : 0) < maxSteps
|
1383
1332
|
);
|
1384
1333
|
}
|
1385
1334
|
function isAssistantMessageWithCompletedToolCalls(message) {
|
@@ -1393,6 +1342,26 @@ function isAssistantMessageWithCompletedToolCalls(message) {
|
|
1393
1342
|
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => "result" in part.toolInvocation);
|
1394
1343
|
}
|
1395
1344
|
|
1345
|
+
// core/util/update-tool-call-result.ts
|
1346
|
+
function updateToolCallResult({
|
1347
|
+
messages,
|
1348
|
+
toolCallId,
|
1349
|
+
toolResult: result
|
1350
|
+
}) {
|
1351
|
+
const lastMessage = messages[messages.length - 1];
|
1352
|
+
const invocationPart = lastMessage.parts.find(
|
1353
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
|
1354
|
+
);
|
1355
|
+
if (invocationPart == null) {
|
1356
|
+
return;
|
1357
|
+
}
|
1358
|
+
invocationPart.toolInvocation = {
|
1359
|
+
...invocationPart.toolInvocation,
|
1360
|
+
state: "result",
|
1361
|
+
result
|
1362
|
+
};
|
1363
|
+
}
|
1364
|
+
|
1396
1365
|
// core/data-stream/create-data-stream.ts
|
1397
1366
|
function createDataStream({
|
1398
1367
|
execute,
|
@@ -2413,7 +2382,7 @@ async function generateImage({
|
|
2413
2382
|
abortSignal,
|
2414
2383
|
headers
|
2415
2384
|
}) {
|
2416
|
-
var _a17;
|
2385
|
+
var _a17, _b;
|
2417
2386
|
const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
|
2418
2387
|
const maxImagesPerCall = (_a17 = model.maxImagesPerCall) != null ? _a17 : 1;
|
2419
2388
|
const callCount = Math.ceil(n / maxImagesPerCall);
|
@@ -2443,6 +2412,7 @@ async function generateImage({
|
|
2443
2412
|
const images = [];
|
2444
2413
|
const warnings = [];
|
2445
2414
|
const responses = [];
|
2415
|
+
const providerMetadata = {};
|
2446
2416
|
for (const result of results) {
|
2447
2417
|
images.push(
|
2448
2418
|
...result.images.map(
|
@@ -2459,18 +2429,32 @@ async function generateImage({
|
|
2459
2429
|
)
|
2460
2430
|
);
|
2461
2431
|
warnings.push(...result.warnings);
|
2432
|
+
if (result.providerMetadata) {
|
2433
|
+
for (const [providerName, metadata] of Object.entries(result.providerMetadata)) {
|
2434
|
+
(_b = providerMetadata[providerName]) != null ? _b : providerMetadata[providerName] = { images: [] };
|
2435
|
+
providerMetadata[providerName].images.push(
|
2436
|
+
...result.providerMetadata[providerName].images
|
2437
|
+
);
|
2438
|
+
}
|
2439
|
+
}
|
2462
2440
|
responses.push(result.response);
|
2463
2441
|
}
|
2464
2442
|
if (!images.length) {
|
2465
2443
|
throw new NoImageGeneratedError({ responses });
|
2466
2444
|
}
|
2467
|
-
return new DefaultGenerateImageResult({
|
2445
|
+
return new DefaultGenerateImageResult({
|
2446
|
+
images,
|
2447
|
+
warnings,
|
2448
|
+
responses,
|
2449
|
+
providerMetadata
|
2450
|
+
});
|
2468
2451
|
}
|
2469
2452
|
var DefaultGenerateImageResult = class {
|
2470
2453
|
constructor(options) {
|
2471
2454
|
this.images = options.images;
|
2472
2455
|
this.warnings = options.warnings;
|
2473
2456
|
this.responses = options.responses;
|
2457
|
+
this.providerMetadata = options.providerMetadata;
|
2474
2458
|
}
|
2475
2459
|
get image() {
|
2476
2460
|
return this.images[0];
|
@@ -2523,6 +2507,9 @@ function extractContentText(content) {
|
|
2523
2507
|
return parts.map((content2) => content2.text).join("");
|
2524
2508
|
}
|
2525
2509
|
|
2510
|
+
// core/prompt/convert-to-language-model-prompt.ts
|
2511
|
+
import { isUrlSupported } from "@ai-sdk/provider-utils";
|
2512
|
+
|
2526
2513
|
// util/download-error.ts
|
2527
2514
|
import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
|
2528
2515
|
var name5 = "AI_DownloadError";
|
@@ -2690,13 +2677,6 @@ function convertDataContentToUint8Array(content) {
|
|
2690
2677
|
}
|
2691
2678
|
throw new InvalidDataContentError({ content });
|
2692
2679
|
}
|
2693
|
-
function convertUint8ArrayToText(uint8Array) {
|
2694
|
-
try {
|
2695
|
-
return new TextDecoder().decode(uint8Array);
|
2696
|
-
} catch (error) {
|
2697
|
-
throw new Error("Error decoding Uint8Array to text");
|
2698
|
-
}
|
2699
|
-
}
|
2700
2680
|
|
2701
2681
|
// core/prompt/invalid-message-role-error.ts
|
2702
2682
|
import { AISDKError as AISDKError8 } from "@ai-sdk/provider";
|
@@ -2720,7 +2700,6 @@ var InvalidMessageRoleError = class extends AISDKError8 {
|
|
2720
2700
|
_a7 = symbol7;
|
2721
2701
|
|
2722
2702
|
// core/prompt/convert-to-language-model-prompt.ts
|
2723
|
-
import { isUrlSupported } from "@ai-sdk/provider-utils";
|
2724
2703
|
async function convertToLanguageModelPrompt({
|
2725
2704
|
prompt,
|
2726
2705
|
supportedUrls,
|
@@ -2776,7 +2755,6 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
2776
2755
|
// remove empty text parts:
|
2777
2756
|
(part) => part.type !== "text" || part.text !== ""
|
2778
2757
|
).map((part) => {
|
2779
|
-
var _a17;
|
2780
2758
|
const providerOptions = part.providerOptions;
|
2781
2759
|
switch (part.type) {
|
2782
2760
|
case "file": {
|
@@ -2787,7 +2765,7 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
2787
2765
|
type: "file",
|
2788
2766
|
data,
|
2789
2767
|
filename: part.filename,
|
2790
|
-
mediaType:
|
2768
|
+
mediaType: mediaType != null ? mediaType : part.mediaType,
|
2791
2769
|
providerOptions
|
2792
2770
|
};
|
2793
2771
|
}
|
@@ -2846,8 +2824,8 @@ async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
|
2846
2824
|
).flat().filter(
|
2847
2825
|
(part) => part.type === "image" || part.type === "file"
|
2848
2826
|
).map((part) => {
|
2849
|
-
var _a17
|
2850
|
-
const mediaType = (
|
2827
|
+
var _a17;
|
2828
|
+
const mediaType = (_a17 = part.mediaType) != null ? _a17 : part.type === "image" ? "image/*" : void 0;
|
2851
2829
|
let data = part.type === "image" ? part.image : part.data;
|
2852
2830
|
if (typeof data === "string") {
|
2853
2831
|
try {
|
@@ -2874,7 +2852,7 @@ async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
|
2874
2852
|
);
|
2875
2853
|
}
|
2876
2854
|
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
2877
|
-
var _a17, _b
|
2855
|
+
var _a17, _b;
|
2878
2856
|
if (part.type === "text") {
|
2879
2857
|
return {
|
2880
2858
|
type: "text",
|
@@ -2895,19 +2873,19 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
2895
2873
|
throw new Error(`Unsupported part type: ${type}`);
|
2896
2874
|
}
|
2897
2875
|
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
|
2898
|
-
let mediaType =
|
2876
|
+
let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
|
2899
2877
|
let data = convertedData;
|
2900
2878
|
if (data instanceof URL) {
|
2901
2879
|
const downloadedFile = downloadedAssets[data.toString()];
|
2902
2880
|
if (downloadedFile) {
|
2903
2881
|
data = downloadedFile.data;
|
2904
|
-
mediaType = (
|
2882
|
+
mediaType = (_a17 = downloadedFile.mediaType) != null ? _a17 : mediaType;
|
2905
2883
|
}
|
2906
2884
|
}
|
2907
2885
|
switch (type) {
|
2908
2886
|
case "image": {
|
2909
2887
|
if (data instanceof Uint8Array || typeof data === "string") {
|
2910
|
-
mediaType = (
|
2888
|
+
mediaType = (_b = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _b : mediaType;
|
2911
2889
|
}
|
2912
2890
|
return {
|
2913
2891
|
type: "file",
|
@@ -3031,83 +3009,6 @@ import { InvalidPromptError } from "@ai-sdk/provider";
|
|
3031
3009
|
import { safeValidateTypes } from "@ai-sdk/provider-utils";
|
3032
3010
|
import { z as z7 } from "zod";
|
3033
3011
|
|
3034
|
-
// core/prompt/attachments-to-parts.ts
|
3035
|
-
function attachmentsToParts(attachments) {
|
3036
|
-
var _a17, _b, _c;
|
3037
|
-
const parts = [];
|
3038
|
-
for (const attachment of attachments) {
|
3039
|
-
let url;
|
3040
|
-
try {
|
3041
|
-
url = new URL(attachment.url);
|
3042
|
-
} catch (error) {
|
3043
|
-
throw new Error(`Invalid URL: ${attachment.url}`);
|
3044
|
-
}
|
3045
|
-
switch (url.protocol) {
|
3046
|
-
case "http:":
|
3047
|
-
case "https:": {
|
3048
|
-
if ((_a17 = attachment.contentType) == null ? void 0 : _a17.startsWith("image/")) {
|
3049
|
-
parts.push({ type: "image", image: url });
|
3050
|
-
} else {
|
3051
|
-
if (!attachment.contentType) {
|
3052
|
-
throw new Error(
|
3053
|
-
"If the attachment is not an image, it must specify a content type"
|
3054
|
-
);
|
3055
|
-
}
|
3056
|
-
parts.push({
|
3057
|
-
type: "file",
|
3058
|
-
data: url,
|
3059
|
-
mediaType: attachment.contentType
|
3060
|
-
});
|
3061
|
-
}
|
3062
|
-
break;
|
3063
|
-
}
|
3064
|
-
case "data:": {
|
3065
|
-
let header;
|
3066
|
-
let base64Content;
|
3067
|
-
let mediaType;
|
3068
|
-
try {
|
3069
|
-
[header, base64Content] = attachment.url.split(",");
|
3070
|
-
mediaType = header.split(";")[0].split(":")[1];
|
3071
|
-
} catch (error) {
|
3072
|
-
throw new Error(`Error processing data URL: ${attachment.url}`);
|
3073
|
-
}
|
3074
|
-
if (mediaType == null || base64Content == null) {
|
3075
|
-
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
3076
|
-
}
|
3077
|
-
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
3078
|
-
parts.push({
|
3079
|
-
type: "image",
|
3080
|
-
image: convertDataContentToUint8Array(base64Content)
|
3081
|
-
});
|
3082
|
-
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
3083
|
-
parts.push({
|
3084
|
-
type: "text",
|
3085
|
-
text: convertUint8ArrayToText(
|
3086
|
-
convertDataContentToUint8Array(base64Content)
|
3087
|
-
)
|
3088
|
-
});
|
3089
|
-
} else {
|
3090
|
-
if (!attachment.contentType) {
|
3091
|
-
throw new Error(
|
3092
|
-
"If the attachment is not an image or text, it must specify a content type"
|
3093
|
-
);
|
3094
|
-
}
|
3095
|
-
parts.push({
|
3096
|
-
type: "file",
|
3097
|
-
data: base64Content,
|
3098
|
-
mediaType: attachment.contentType
|
3099
|
-
});
|
3100
|
-
}
|
3101
|
-
break;
|
3102
|
-
}
|
3103
|
-
default: {
|
3104
|
-
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
3105
|
-
}
|
3106
|
-
}
|
3107
|
-
}
|
3108
|
-
return parts;
|
3109
|
-
}
|
3110
|
-
|
3111
3012
|
// core/prompt/message-conversion-error.ts
|
3112
3013
|
import { AISDKError as AISDKError9 } from "@ai-sdk/provider";
|
3113
3014
|
var name8 = "AI_MessageConversionError";
|
@@ -3129,15 +3030,15 @@ var MessageConversionError = class extends AISDKError9 {
|
|
3129
3030
|
};
|
3130
3031
|
_a8 = symbol8;
|
3131
3032
|
|
3132
|
-
// core/prompt/convert-to-
|
3133
|
-
function
|
3033
|
+
// core/prompt/convert-to-model-messages.ts
|
3034
|
+
function convertToModelMessages(messages, options) {
|
3134
3035
|
var _a17, _b;
|
3135
3036
|
const tools = (_a17 = options == null ? void 0 : options.tools) != null ? _a17 : {};
|
3136
3037
|
const coreMessages = [];
|
3137
3038
|
for (let i = 0; i < messages.length; i++) {
|
3138
3039
|
const message = messages[i];
|
3139
3040
|
const isLastMessage = i === messages.length - 1;
|
3140
|
-
const { role, content
|
3041
|
+
const { role, content } = message;
|
3141
3042
|
switch (role) {
|
3142
3043
|
case "system": {
|
3143
3044
|
coreMessages.push({
|
@@ -3147,30 +3048,24 @@ function convertToCoreMessages(messages, options) {
|
|
3147
3048
|
break;
|
3148
3049
|
}
|
3149
3050
|
case "user": {
|
3150
|
-
|
3151
|
-
|
3152
|
-
|
3153
|
-
|
3154
|
-
|
3155
|
-
|
3156
|
-
|
3157
|
-
|
3158
|
-
|
3159
|
-
|
3160
|
-
|
3161
|
-
|
3162
|
-
|
3163
|
-
coreMessages.push({
|
3164
|
-
role: "user",
|
3165
|
-
content: experimental_attachments ? [...textParts, ...attachmentsToParts(experimental_attachments)] : textParts
|
3166
|
-
});
|
3167
|
-
}
|
3051
|
+
coreMessages.push({
|
3052
|
+
role: "user",
|
3053
|
+
content: message.parts.filter(
|
3054
|
+
(part) => part.type === "text" || part.type === "file"
|
3055
|
+
).map(
|
3056
|
+
(part) => part.type === "file" ? {
|
3057
|
+
type: "file",
|
3058
|
+
mediaType: part.mediaType,
|
3059
|
+
filename: part.filename,
|
3060
|
+
data: part.url
|
3061
|
+
} : part
|
3062
|
+
)
|
3063
|
+
});
|
3168
3064
|
break;
|
3169
3065
|
}
|
3170
3066
|
case "assistant": {
|
3171
3067
|
if (message.parts != null) {
|
3172
3068
|
let processBlock2 = function() {
|
3173
|
-
var _a18;
|
3174
3069
|
const content2 = [];
|
3175
3070
|
for (const part of block) {
|
3176
3071
|
switch (part.type) {
|
@@ -3181,16 +3076,15 @@ function convertToCoreMessages(messages, options) {
|
|
3181
3076
|
case "file": {
|
3182
3077
|
content2.push({
|
3183
3078
|
type: "file",
|
3184
|
-
|
3185
|
-
|
3186
|
-
// TODO migration, remove
|
3079
|
+
mediaType: part.mediaType,
|
3080
|
+
data: part.url
|
3187
3081
|
});
|
3188
3082
|
break;
|
3189
3083
|
}
|
3190
3084
|
case "reasoning": {
|
3191
3085
|
content2.push({
|
3192
3086
|
type: "reasoning",
|
3193
|
-
text: part.
|
3087
|
+
text: part.text,
|
3194
3088
|
providerOptions: part.providerMetadata
|
3195
3089
|
});
|
3196
3090
|
break;
|
@@ -3280,73 +3174,11 @@ function convertToCoreMessages(messages, options) {
|
|
3280
3174
|
processBlock2();
|
3281
3175
|
break;
|
3282
3176
|
}
|
3283
|
-
const toolInvocations = message.toolInvocations;
|
3284
|
-
if (toolInvocations == null || toolInvocations.length === 0) {
|
3285
|
-
coreMessages.push({ role: "assistant", content });
|
3286
|
-
break;
|
3287
|
-
}
|
3288
|
-
const maxStep = toolInvocations.reduce((max, toolInvocation) => {
|
3289
|
-
var _a18;
|
3290
|
-
return Math.max(max, (_a18 = toolInvocation.step) != null ? _a18 : 0);
|
3291
|
-
}, 0);
|
3292
|
-
for (let i2 = 0; i2 <= maxStep; i2++) {
|
3293
|
-
const stepInvocations = toolInvocations.filter(
|
3294
|
-
(toolInvocation) => {
|
3295
|
-
var _a18;
|
3296
|
-
return ((_a18 = toolInvocation.step) != null ? _a18 : 0) === i2;
|
3297
|
-
}
|
3298
|
-
);
|
3299
|
-
if (stepInvocations.length === 0) {
|
3300
|
-
continue;
|
3301
|
-
}
|
3302
|
-
coreMessages.push({
|
3303
|
-
role: "assistant",
|
3304
|
-
content: [
|
3305
|
-
...isLastMessage && content && i2 === 0 ? [{ type: "text", text: content }] : [],
|
3306
|
-
...stepInvocations.map(
|
3307
|
-
({ toolCallId, toolName, args }) => ({
|
3308
|
-
type: "tool-call",
|
3309
|
-
toolCallId,
|
3310
|
-
toolName,
|
3311
|
-
args
|
3312
|
-
})
|
3313
|
-
)
|
3314
|
-
]
|
3315
|
-
});
|
3316
|
-
coreMessages.push({
|
3317
|
-
role: "tool",
|
3318
|
-
content: stepInvocations.map((toolInvocation) => {
|
3319
|
-
if (!("result" in toolInvocation)) {
|
3320
|
-
throw new MessageConversionError({
|
3321
|
-
originalMessage: message,
|
3322
|
-
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
3323
|
-
});
|
3324
|
-
}
|
3325
|
-
const { toolCallId, toolName, result } = toolInvocation;
|
3326
|
-
const tool2 = tools[toolName];
|
3327
|
-
return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
|
3328
|
-
type: "tool-result",
|
3329
|
-
toolCallId,
|
3330
|
-
toolName,
|
3331
|
-
result: tool2.experimental_toToolResultContent(result),
|
3332
|
-
experimental_content: tool2.experimental_toToolResultContent(result)
|
3333
|
-
} : {
|
3334
|
-
type: "tool-result",
|
3335
|
-
toolCallId,
|
3336
|
-
toolName,
|
3337
|
-
result
|
3338
|
-
};
|
3339
|
-
})
|
3340
|
-
});
|
3341
|
-
}
|
3342
3177
|
if (content && !isLastMessage) {
|
3343
3178
|
coreMessages.push({ role: "assistant", content });
|
3344
3179
|
}
|
3345
3180
|
break;
|
3346
3181
|
}
|
3347
|
-
case "data": {
|
3348
|
-
break;
|
3349
|
-
}
|
3350
3182
|
default: {
|
3351
3183
|
const _exhaustiveCheck = role;
|
3352
3184
|
throw new MessageConversionError({
|
@@ -3358,6 +3190,7 @@ function convertToCoreMessages(messages, options) {
|
|
3358
3190
|
}
|
3359
3191
|
return coreMessages;
|
3360
3192
|
}
|
3193
|
+
var convertToCoreMessages = convertToModelMessages;
|
3361
3194
|
|
3362
3195
|
// core/prompt/detect-prompt-type.ts
|
3363
3196
|
function detectPromptType(prompt) {
|
@@ -3446,7 +3279,6 @@ var imagePartSchema = z5.object({
|
|
3446
3279
|
type: z5.literal("image"),
|
3447
3280
|
image: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
3448
3281
|
mediaType: z5.string().optional(),
|
3449
|
-
mimeType: z5.string().optional(),
|
3450
3282
|
providerOptions: providerMetadataSchema.optional()
|
3451
3283
|
});
|
3452
3284
|
var filePartSchema = z5.object({
|
@@ -3454,7 +3286,6 @@ var filePartSchema = z5.object({
|
|
3454
3286
|
data: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
3455
3287
|
filename: z5.string().optional(),
|
3456
3288
|
mediaType: z5.string(),
|
3457
|
-
mimeType: z5.string().optional(),
|
3458
3289
|
providerOptions: providerMetadataSchema.optional()
|
3459
3290
|
});
|
3460
3291
|
var reasoningPartSchema = z5.object({
|
@@ -3480,12 +3311,15 @@ var toolResultPartSchema = z5.object({
|
|
3480
3311
|
});
|
3481
3312
|
|
3482
3313
|
// core/prompt/message.ts
|
3483
|
-
var
|
3484
|
-
|
3485
|
-
|
3486
|
-
|
3487
|
-
|
3488
|
-
|
3314
|
+
var systemModelMessageSchema = z6.object(
|
3315
|
+
{
|
3316
|
+
role: z6.literal("system"),
|
3317
|
+
content: z6.string(),
|
3318
|
+
providerOptions: providerMetadataSchema.optional()
|
3319
|
+
}
|
3320
|
+
);
|
3321
|
+
var coreSystemMessageSchema = systemModelMessageSchema;
|
3322
|
+
var userModelMessageSchema = z6.object({
|
3489
3323
|
role: z6.literal("user"),
|
3490
3324
|
content: z6.union([
|
3491
3325
|
z6.string(),
|
@@ -3493,7 +3327,8 @@ var coreUserMessageSchema = z6.object({
|
|
3493
3327
|
]),
|
3494
3328
|
providerOptions: providerMetadataSchema.optional()
|
3495
3329
|
});
|
3496
|
-
var
|
3330
|
+
var coreUserMessageSchema = userModelMessageSchema;
|
3331
|
+
var assistantModelMessageSchema = z6.object({
|
3497
3332
|
role: z6.literal("assistant"),
|
3498
3333
|
content: z6.union([
|
3499
3334
|
z6.string(),
|
@@ -3508,17 +3343,20 @@ var coreAssistantMessageSchema = z6.object({
|
|
3508
3343
|
]),
|
3509
3344
|
providerOptions: providerMetadataSchema.optional()
|
3510
3345
|
});
|
3511
|
-
var
|
3346
|
+
var coreAssistantMessageSchema = assistantModelMessageSchema;
|
3347
|
+
var toolModelMessageSchema = z6.object({
|
3512
3348
|
role: z6.literal("tool"),
|
3513
3349
|
content: z6.array(toolResultPartSchema),
|
3514
3350
|
providerOptions: providerMetadataSchema.optional()
|
3515
3351
|
});
|
3516
|
-
var
|
3517
|
-
|
3518
|
-
|
3519
|
-
|
3520
|
-
|
3352
|
+
var coreToolMessageSchema = toolModelMessageSchema;
|
3353
|
+
var modelMessageSchema = z6.union([
|
3354
|
+
systemModelMessageSchema,
|
3355
|
+
userModelMessageSchema,
|
3356
|
+
assistantModelMessageSchema,
|
3357
|
+
toolModelMessageSchema
|
3521
3358
|
]);
|
3359
|
+
var coreMessageSchema = modelMessageSchema;
|
3522
3360
|
|
3523
3361
|
// core/prompt/standardize-prompt.ts
|
3524
3362
|
async function standardizePrompt({
|
@@ -3565,10 +3403,10 @@ async function standardizePrompt({
|
|
3565
3403
|
if (promptType === "other") {
|
3566
3404
|
throw new InvalidPromptError({
|
3567
3405
|
prompt,
|
3568
|
-
message: "messages must be an array of
|
3406
|
+
message: "messages must be an array of ModelMessage or UIMessage"
|
3569
3407
|
});
|
3570
3408
|
}
|
3571
|
-
const messages = promptType === "ui-messages" ?
|
3409
|
+
const messages = promptType === "ui-messages" ? convertToModelMessages(prompt.messages, {
|
3572
3410
|
tools
|
3573
3411
|
}) : prompt.messages;
|
3574
3412
|
if (messages.length === 0) {
|
@@ -3579,12 +3417,12 @@ async function standardizePrompt({
|
|
3579
3417
|
}
|
3580
3418
|
const validationResult = await safeValidateTypes({
|
3581
3419
|
value: messages,
|
3582
|
-
schema: z7.array(
|
3420
|
+
schema: z7.array(modelMessageSchema)
|
3583
3421
|
});
|
3584
3422
|
if (!validationResult.success) {
|
3585
3423
|
throw new InvalidPromptError({
|
3586
3424
|
prompt,
|
3587
|
-
message: "messages must be an array of
|
3425
|
+
message: "messages must be an array of ModelMessage or UIMessage",
|
3588
3426
|
cause: validationResult.error
|
3589
3427
|
});
|
3590
3428
|
}
|
@@ -3836,10 +3674,36 @@ var enumOutputStrategy = (enumValues) => {
|
|
3836
3674
|
})
|
3837
3675
|
};
|
3838
3676
|
},
|
3839
|
-
validatePartialResult() {
|
3840
|
-
|
3841
|
-
|
3842
|
-
|
3677
|
+
async validatePartialResult({ value, textDelta }) {
|
3678
|
+
if (!isJSONObject(value) || typeof value.result !== "string") {
|
3679
|
+
return {
|
3680
|
+
success: false,
|
3681
|
+
error: new TypeValidationError({
|
3682
|
+
value,
|
3683
|
+
cause: 'value must be an object that contains a string in the "result" property.'
|
3684
|
+
})
|
3685
|
+
};
|
3686
|
+
}
|
3687
|
+
const result = value.result;
|
3688
|
+
const possibleEnumValues = enumValues.filter(
|
3689
|
+
(enumValue) => enumValue.startsWith(result)
|
3690
|
+
);
|
3691
|
+
if (value.result.length === 0 || possibleEnumValues.length === 0) {
|
3692
|
+
return {
|
3693
|
+
success: false,
|
3694
|
+
error: new TypeValidationError({
|
3695
|
+
value,
|
3696
|
+
cause: "value must be a string in the enum"
|
3697
|
+
})
|
3698
|
+
};
|
3699
|
+
}
|
3700
|
+
return {
|
3701
|
+
success: true,
|
3702
|
+
value: {
|
3703
|
+
partial: possibleEnumValues.length > 1 ? result : possibleEnumValues[0],
|
3704
|
+
textDelta
|
3705
|
+
}
|
3706
|
+
};
|
3843
3707
|
},
|
3844
3708
|
createElementStream() {
|
3845
3709
|
throw new UnsupportedFunctionalityError({
|
@@ -4425,6 +4289,7 @@ function streamObject(options) {
|
|
4425
4289
|
} = {},
|
4426
4290
|
...settings
|
4427
4291
|
} = options;
|
4292
|
+
const enumValues = "enum" in options && options.enum ? options.enum : void 0;
|
4428
4293
|
const {
|
4429
4294
|
schema: inputSchema,
|
4430
4295
|
schemaDescription,
|
@@ -4434,9 +4299,14 @@ function streamObject(options) {
|
|
4434
4299
|
output,
|
4435
4300
|
schema: inputSchema,
|
4436
4301
|
schemaName,
|
4437
|
-
schemaDescription
|
4302
|
+
schemaDescription,
|
4303
|
+
enumValues
|
4304
|
+
});
|
4305
|
+
const outputStrategy = getOutputStrategy({
|
4306
|
+
output,
|
4307
|
+
schema: inputSchema,
|
4308
|
+
enumValues
|
4438
4309
|
});
|
4439
|
-
const outputStrategy = getOutputStrategy({ output, schema: inputSchema });
|
4440
4310
|
return new DefaultStreamObjectResult({
|
4441
4311
|
model,
|
4442
4312
|
telemetry,
|
@@ -7203,8 +7073,8 @@ var DefaultStreamTextResult = class {
|
|
7203
7073
|
controller.enqueue(
|
7204
7074
|
// TODO update protocol to v2 or replace with event stream
|
7205
7075
|
formatDataStreamPart("file", {
|
7206
|
-
|
7207
|
-
|
7076
|
+
mediaType: chunk.file.mediaType,
|
7077
|
+
url: `data:${chunk.file.mediaType};base64,${chunk.file.base64}`
|
7208
7078
|
})
|
7209
7079
|
);
|
7210
7080
|
break;
|
@@ -7426,16 +7296,16 @@ var DefaultGeneratedAudioFile = class extends DefaultGeneratedFile {
|
|
7426
7296
|
super({ data, mediaType });
|
7427
7297
|
let format = "mp3";
|
7428
7298
|
if (mediaType) {
|
7429
|
-
const
|
7430
|
-
if (
|
7299
|
+
const mediaTypeParts = mediaType.split("/");
|
7300
|
+
if (mediaTypeParts.length === 2) {
|
7431
7301
|
if (mediaType !== "audio/mpeg") {
|
7432
|
-
format =
|
7302
|
+
format = mediaTypeParts[1];
|
7433
7303
|
}
|
7434
7304
|
}
|
7435
7305
|
}
|
7436
7306
|
if (!format) {
|
7437
7307
|
throw new Error(
|
7438
|
-
"Audio format must be provided or determinable from
|
7308
|
+
"Audio format must be provided or determinable from media type"
|
7439
7309
|
);
|
7440
7310
|
}
|
7441
7311
|
this.format = format;
|
@@ -7864,7 +7734,7 @@ function appendResponseMessages({
|
|
7864
7734
|
responseMessages,
|
7865
7735
|
_internal: { currentDate = () => /* @__PURE__ */ new Date() } = {}
|
7866
7736
|
}) {
|
7867
|
-
var _a17, _b
|
7737
|
+
var _a17, _b;
|
7868
7738
|
const clonedMessages = structuredClone(messages);
|
7869
7739
|
for (const message of responseMessages) {
|
7870
7740
|
const role = message.role;
|
@@ -7872,7 +7742,7 @@ function appendResponseMessages({
|
|
7872
7742
|
const isLastMessageAssistant = lastMessage.role === "assistant";
|
7873
7743
|
switch (role) {
|
7874
7744
|
case "assistant": {
|
7875
|
-
let
|
7745
|
+
let getToolInvocationsForStep2 = function(step) {
|
7876
7746
|
return (typeof message.content === "string" ? [] : message.content.filter((part) => part.type === "tool-call")).map((call) => ({
|
7877
7747
|
state: "call",
|
7878
7748
|
step,
|
@@ -7881,7 +7751,7 @@ function appendResponseMessages({
|
|
7881
7751
|
toolName: call.toolName
|
7882
7752
|
}));
|
7883
7753
|
};
|
7884
|
-
var
|
7754
|
+
var getToolInvocationsForStep = getToolInvocationsForStep2;
|
7885
7755
|
const parts = [{ type: "step-start" }];
|
7886
7756
|
let textContent = "";
|
7887
7757
|
let reasoningTextContent = void 0;
|
@@ -7908,12 +7778,12 @@ function appendResponseMessages({
|
|
7908
7778
|
if (reasoningPart == null) {
|
7909
7779
|
reasoningPart = {
|
7910
7780
|
type: "reasoning",
|
7911
|
-
|
7781
|
+
text: ""
|
7912
7782
|
};
|
7913
7783
|
parts.push(reasoningPart);
|
7914
7784
|
}
|
7915
7785
|
reasoningTextContent = (reasoningTextContent != null ? reasoningTextContent : "") + part.text;
|
7916
|
-
reasoningPart.
|
7786
|
+
reasoningPart.text += part.text;
|
7917
7787
|
reasoningPart.providerMetadata = part.providerOptions;
|
7918
7788
|
break;
|
7919
7789
|
}
|
@@ -7928,8 +7798,8 @@ function appendResponseMessages({
|
|
7928
7798
|
}
|
7929
7799
|
parts.push({
|
7930
7800
|
type: "file",
|
7931
|
-
mediaType:
|
7932
|
-
|
7801
|
+
mediaType: part.mediaType,
|
7802
|
+
url: `data:${part.mediaType};base64,${convertDataContentToBase64String(part.data)}`
|
7933
7803
|
});
|
7934
7804
|
break;
|
7935
7805
|
}
|
@@ -7937,16 +7807,13 @@ function appendResponseMessages({
|
|
7937
7807
|
}
|
7938
7808
|
if (isLastMessageAssistant) {
|
7939
7809
|
const maxStep = extractMaxToolInvocationStep(
|
7940
|
-
lastMessage
|
7810
|
+
getToolInvocations(lastMessage)
|
7811
|
+
// TODO remove once Message is removed
|
7941
7812
|
);
|
7942
|
-
(
|
7813
|
+
(_a17 = lastMessage.parts) != null ? _a17 : lastMessage.parts = [];
|
7943
7814
|
lastMessage.content = textContent;
|
7944
7815
|
lastMessage.parts.push(...parts);
|
7945
|
-
|
7946
|
-
...(_c = lastMessage.toolInvocations) != null ? _c : [],
|
7947
|
-
...getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1)
|
7948
|
-
];
|
7949
|
-
getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1).map((call) => ({
|
7816
|
+
getToolInvocationsForStep2(maxStep === void 0 ? 0 : maxStep + 1).map((call) => ({
|
7950
7817
|
type: "tool-invocation",
|
7951
7818
|
toolInvocation: call
|
7952
7819
|
})).forEach((part) => {
|
@@ -7959,10 +7826,9 @@ function appendResponseMessages({
|
|
7959
7826
|
createdAt: currentDate(),
|
7960
7827
|
// generate a createdAt date for the message, will be overridden by the client
|
7961
7828
|
content: textContent,
|
7962
|
-
toolInvocations: getToolInvocations2(0),
|
7963
7829
|
parts: [
|
7964
7830
|
...parts,
|
7965
|
-
...
|
7831
|
+
...getToolInvocationsForStep2(0).map((call) => ({
|
7966
7832
|
type: "tool-invocation",
|
7967
7833
|
toolInvocation: call
|
7968
7834
|
}))
|
@@ -7972,17 +7838,17 @@ function appendResponseMessages({
|
|
7972
7838
|
break;
|
7973
7839
|
}
|
7974
7840
|
case "tool": {
|
7975
|
-
(_d = lastMessage.toolInvocations) != null ? _d : lastMessage.toolInvocations = [];
|
7976
7841
|
if (lastMessage.role !== "assistant") {
|
7977
7842
|
throw new Error(
|
7978
7843
|
`Tool result must follow an assistant message: ${lastMessage.role}`
|
7979
7844
|
);
|
7980
7845
|
}
|
7981
|
-
(
|
7846
|
+
(_b = lastMessage.parts) != null ? _b : lastMessage.parts = [];
|
7982
7847
|
for (const contentPart of message.content) {
|
7983
|
-
const toolCall =
|
7984
|
-
|
7985
|
-
|
7848
|
+
const toolCall = getToolInvocations(
|
7849
|
+
lastMessage
|
7850
|
+
// TODO remove once Message is removed
|
7851
|
+
).find((call) => call.toolCallId === contentPart.toolCallId);
|
7986
7852
|
const toolCallPart = lastMessage.parts.find(
|
7987
7853
|
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === contentPart.toolCallId
|
7988
7854
|
);
|
@@ -8535,6 +8401,24 @@ var MCPClient = class {
|
|
8535
8401
|
await ((_a17 = this.transport) == null ? void 0 : _a17.close());
|
8536
8402
|
this.onClose();
|
8537
8403
|
}
|
8404
|
+
assertCapability(method) {
|
8405
|
+
switch (method) {
|
8406
|
+
case "initialize":
|
8407
|
+
break;
|
8408
|
+
case "tools/list":
|
8409
|
+
case "tools/call":
|
8410
|
+
if (!this.serverCapabilities.tools) {
|
8411
|
+
throw new MCPClientError({
|
8412
|
+
message: `Server does not support tools`
|
8413
|
+
});
|
8414
|
+
}
|
8415
|
+
break;
|
8416
|
+
default:
|
8417
|
+
throw new MCPClientError({
|
8418
|
+
message: `Unsupported method: ${method}`
|
8419
|
+
});
|
8420
|
+
}
|
8421
|
+
}
|
8538
8422
|
async request({
|
8539
8423
|
request,
|
8540
8424
|
resultSchema,
|
@@ -8548,6 +8432,7 @@ var MCPClient = class {
|
|
8548
8432
|
})
|
8549
8433
|
);
|
8550
8434
|
}
|
8435
|
+
this.assertCapability(request.method);
|
8551
8436
|
const signal = options == null ? void 0 : options.signal;
|
8552
8437
|
signal == null ? void 0 : signal.throwIfAborted();
|
8553
8438
|
const messageId = this.requestMessageId++;
|
@@ -8576,7 +8461,7 @@ var MCPClient = class {
|
|
8576
8461
|
resolve(result);
|
8577
8462
|
} catch (error) {
|
8578
8463
|
const parseError = new MCPClientError({
|
8579
|
-
message: "Failed to parse server
|
8464
|
+
message: "Failed to parse server response",
|
8580
8465
|
cause: error
|
8581
8466
|
});
|
8582
8467
|
reject(parseError);
|
@@ -8592,11 +8477,6 @@ var MCPClient = class {
|
|
8592
8477
|
params,
|
8593
8478
|
options
|
8594
8479
|
} = {}) {
|
8595
|
-
if (!this.serverCapabilities.tools) {
|
8596
|
-
throw new MCPClientError({
|
8597
|
-
message: `Server does not support tools`
|
8598
|
-
});
|
8599
|
-
}
|
8600
8480
|
try {
|
8601
8481
|
return this.request({
|
8602
8482
|
request: { method: "tools/list", params },
|
@@ -8612,11 +8492,6 @@ var MCPClient = class {
|
|
8612
8492
|
args,
|
8613
8493
|
options
|
8614
8494
|
}) {
|
8615
|
-
if (!this.serverCapabilities.tools) {
|
8616
|
-
throw new MCPClientError({
|
8617
|
-
message: `Server does not support tools`
|
8618
|
-
});
|
8619
|
-
}
|
8620
8495
|
try {
|
8621
8496
|
return this.request({
|
8622
8497
|
request: { method: "tools/call", params: { name: name17, arguments: args } },
|
@@ -8861,9 +8736,12 @@ export {
|
|
8861
8736
|
appendClientMessage,
|
8862
8737
|
appendResponseMessages,
|
8863
8738
|
asSchema,
|
8739
|
+
assistantModelMessageSchema,
|
8864
8740
|
callChatApi,
|
8865
8741
|
callCompletionApi,
|
8742
|
+
convertFileListToFileUIParts,
|
8866
8743
|
convertToCoreMessages,
|
8744
|
+
convertToModelMessages,
|
8867
8745
|
coreAssistantMessageSchema,
|
8868
8746
|
coreMessageSchema,
|
8869
8747
|
coreSystemMessageSchema,
|
@@ -8886,20 +8764,19 @@ export {
|
|
8886
8764
|
transcribe as experimental_transcribe,
|
8887
8765
|
extractMaxToolInvocationStep,
|
8888
8766
|
extractReasoningMiddleware,
|
8889
|
-
fillMessageParts,
|
8890
8767
|
formatDataStreamPart,
|
8891
8768
|
generateId2 as generateId,
|
8892
8769
|
generateObject,
|
8893
8770
|
generateText,
|
8894
|
-
getMessageParts,
|
8895
8771
|
getTextFromDataUrl,
|
8772
|
+
getToolInvocations,
|
8896
8773
|
isAssistantMessageWithCompletedToolCalls,
|
8897
8774
|
isDeepEqualData,
|
8898
8775
|
jsonSchema,
|
8776
|
+
modelMessageSchema,
|
8899
8777
|
parseDataStreamPart,
|
8900
8778
|
parsePartialJson,
|
8901
8779
|
pipeDataStreamToResponse,
|
8902
|
-
prepareAttachmentsForRequest,
|
8903
8780
|
processDataStream,
|
8904
8781
|
processTextStream,
|
8905
8782
|
shouldResubmitMessages,
|
@@ -8908,8 +8785,11 @@ export {
|
|
8908
8785
|
smoothStream,
|
8909
8786
|
streamObject,
|
8910
8787
|
streamText,
|
8788
|
+
systemModelMessageSchema,
|
8911
8789
|
tool,
|
8790
|
+
toolModelMessageSchema,
|
8912
8791
|
updateToolCallResult,
|
8792
|
+
userModelMessageSchema,
|
8913
8793
|
wrapLanguageModel
|
8914
8794
|
};
|
8915
8795
|
//# sourceMappingURL=index.mjs.map
|