ai 5.0.0-canary.16 → 5.0.0-canary.18
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 +59 -0
- package/README.md +4 -4
- package/dist/index.d.mts +141 -159
- package/dist/index.d.ts +141 -159
- package/dist/index.js +341 -377
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +336 -378
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +30 -89
- package/dist/internal/index.d.ts +30 -89
- package/dist/internal/index.js +204 -344
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +197 -336
- 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 +4 -4
package/dist/index.mjs
CHANGED
@@ -9,26 +9,14 @@ 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
|
18
18
|
import { generateId as generateIdFunction } from "@ai-sdk/provider-utils";
|
19
19
|
|
20
|
-
// core/types/duplicated/usage.ts
|
21
|
-
function calculateLanguageModelUsage({
|
22
|
-
promptTokens,
|
23
|
-
completionTokens
|
24
|
-
}) {
|
25
|
-
return {
|
26
|
-
promptTokens,
|
27
|
-
completionTokens,
|
28
|
-
totalTokens: promptTokens + completionTokens
|
29
|
-
};
|
30
|
-
}
|
31
|
-
|
32
20
|
// core/ui/get-tool-invocations.ts
|
33
21
|
function getToolInvocations(message) {
|
34
22
|
return message.parts.filter(
|
@@ -494,10 +482,13 @@ var finishMessageStreamPart = {
|
|
494
482
|
const result = {
|
495
483
|
finishReason: value.finishReason
|
496
484
|
};
|
497
|
-
if ("usage" in value && value.usage != null && typeof value.usage === "object"
|
485
|
+
if ("usage" in value && value.usage != null && typeof value.usage === "object") {
|
498
486
|
result.usage = {
|
499
|
-
|
500
|
-
|
487
|
+
inputTokens: "inputTokens" in value.usage && typeof value.usage.inputTokens === "number" ? value.usage.inputTokens : void 0,
|
488
|
+
outputTokens: "outputTokens" in value.usage && typeof value.usage.outputTokens === "number" ? value.usage.outputTokens : void 0,
|
489
|
+
totalTokens: "totalTokens" in value.usage && typeof value.usage.totalTokens === "number" ? value.usage.totalTokens : void 0,
|
490
|
+
reasoningTokens: "reasoningTokens" in value.usage && typeof value.usage.reasoningTokens === "number" ? value.usage.reasoningTokens : void 0,
|
491
|
+
cachedInputTokens: "cachedInputTokens" in value.usage && typeof value.usage.cachedInputTokens === "number" ? value.usage.cachedInputTokens : void 0
|
501
492
|
};
|
502
493
|
}
|
503
494
|
return {
|
@@ -519,10 +510,13 @@ var finishStepStreamPart = {
|
|
519
510
|
finishReason: value.finishReason,
|
520
511
|
isContinued: false
|
521
512
|
};
|
522
|
-
if ("usage" in value && value.usage != null && typeof value.usage === "object"
|
513
|
+
if ("usage" in value && value.usage != null && typeof value.usage === "object") {
|
523
514
|
result.usage = {
|
524
|
-
|
525
|
-
|
515
|
+
inputTokens: "inputTokens" in value.usage && typeof value.usage.inputTokens === "number" ? value.usage.inputTokens : void 0,
|
516
|
+
outputTokens: "outputTokens" in value.usage && typeof value.usage.outputTokens === "number" ? value.usage.outputTokens : void 0,
|
517
|
+
totalTokens: "totalTokens" in value.usage && typeof value.usage.totalTokens === "number" ? value.usage.totalTokens : void 0,
|
518
|
+
reasoningTokens: "reasoningTokens" in value.usage && typeof value.usage.reasoningTokens === "number" ? value.usage.reasoningTokens : void 0,
|
519
|
+
cachedInputTokens: "cachedInputTokens" in value.usage && typeof value.usage.cachedInputTokens === "number" ? value.usage.cachedInputTokens : void 0
|
526
520
|
};
|
527
521
|
}
|
528
522
|
if ("isContinued" in value && typeof value.isContinued === "boolean") {
|
@@ -586,9 +580,9 @@ var fileStreamPart = {
|
|
586
580
|
code: "k",
|
587
581
|
name: "file",
|
588
582
|
parse: (value) => {
|
589
|
-
if (value == null || typeof value !== "object" || !("
|
583
|
+
if (value == null || typeof value !== "object" || !("url" in value) || typeof value.url !== "string" || !("mediaType" in value) || typeof value.mediaType !== "string") {
|
590
584
|
throw new Error(
|
591
|
-
'"file" parts expect an object with a "
|
585
|
+
'"file" parts expect an object with a "url" and "mediaType" property.'
|
592
586
|
);
|
593
587
|
}
|
594
588
|
return { type: "file", value };
|
@@ -795,9 +789,9 @@ async function processChatResponse({
|
|
795
789
|
let messageAnnotations = replaceLastMessage ? lastMessage == null ? void 0 : lastMessage.annotations : void 0;
|
796
790
|
const partialToolCalls = {};
|
797
791
|
let usage = {
|
798
|
-
|
799
|
-
|
800
|
-
totalTokens:
|
792
|
+
inputTokens: void 0,
|
793
|
+
outputTokens: void 0,
|
794
|
+
totalTokens: void 0
|
801
795
|
};
|
802
796
|
let finishReason = "unknown";
|
803
797
|
function execUpdate() {
|
@@ -859,8 +853,8 @@ async function processChatResponse({
|
|
859
853
|
onFilePart(value) {
|
860
854
|
message.parts.push({
|
861
855
|
type: "file",
|
862
|
-
mediaType: value.
|
863
|
-
|
856
|
+
mediaType: value.mediaType,
|
857
|
+
url: value.url
|
864
858
|
});
|
865
859
|
execUpdate();
|
866
860
|
},
|
@@ -970,7 +964,7 @@ async function processChatResponse({
|
|
970
964
|
onFinishMessagePart(value) {
|
971
965
|
finishReason = value.finishReason;
|
972
966
|
if (value.usage != null) {
|
973
|
-
usage =
|
967
|
+
usage = value.usage;
|
974
968
|
}
|
975
969
|
},
|
976
970
|
onErrorPart(error) {
|
@@ -1027,7 +1021,11 @@ async function processChatTextResponse({
|
|
1027
1021
|
}
|
1028
1022
|
});
|
1029
1023
|
onFinish == null ? void 0 : onFinish(resultMessage, {
|
1030
|
-
usage: {
|
1024
|
+
usage: {
|
1025
|
+
inputTokens: void 0,
|
1026
|
+
outputTokens: void 0,
|
1027
|
+
totalTokens: void 0
|
1028
|
+
},
|
1031
1029
|
finishReason: "unknown"
|
1032
1030
|
});
|
1033
1031
|
}
|
@@ -1041,40 +1039,41 @@ async function callChatApi({
|
|
1041
1039
|
credentials,
|
1042
1040
|
headers,
|
1043
1041
|
abortController,
|
1044
|
-
restoreMessagesOnFailure,
|
1045
1042
|
onResponse,
|
1046
1043
|
onUpdate,
|
1047
1044
|
onFinish,
|
1048
1045
|
onToolCall,
|
1049
1046
|
generateId: generateId3,
|
1050
1047
|
fetch: fetch2 = getOriginalFetch(),
|
1051
|
-
lastMessage
|
1048
|
+
lastMessage,
|
1049
|
+
getCurrentDate,
|
1050
|
+
requestType = "generate"
|
1052
1051
|
}) {
|
1053
|
-
var _a17, _b;
|
1054
|
-
const response = await fetch2(api
|
1052
|
+
var _a17, _b, _c;
|
1053
|
+
const response = requestType === "resume" ? await fetch2(`${api}?chatId=${body.id}`, {
|
1054
|
+
method: "GET",
|
1055
|
+
headers: {
|
1056
|
+
"Content-Type": "application/json",
|
1057
|
+
...headers
|
1058
|
+
},
|
1059
|
+
signal: (_a17 = abortController == null ? void 0 : abortController()) == null ? void 0 : _a17.signal,
|
1060
|
+
credentials
|
1061
|
+
}) : await fetch2(api, {
|
1055
1062
|
method: "POST",
|
1056
1063
|
body: JSON.stringify(body),
|
1057
1064
|
headers: {
|
1058
1065
|
"Content-Type": "application/json",
|
1059
1066
|
...headers
|
1060
1067
|
},
|
1061
|
-
signal: (
|
1068
|
+
signal: (_b = abortController == null ? void 0 : abortController()) == null ? void 0 : _b.signal,
|
1062
1069
|
credentials
|
1063
|
-
}).catch((err) => {
|
1064
|
-
restoreMessagesOnFailure();
|
1065
|
-
throw err;
|
1066
1070
|
});
|
1067
|
-
if (onResponse) {
|
1068
|
-
|
1069
|
-
await onResponse(response);
|
1070
|
-
} catch (err) {
|
1071
|
-
throw err;
|
1072
|
-
}
|
1071
|
+
if (onResponse != null) {
|
1072
|
+
await onResponse(response);
|
1073
1073
|
}
|
1074
1074
|
if (!response.ok) {
|
1075
|
-
restoreMessagesOnFailure();
|
1076
1075
|
throw new Error(
|
1077
|
-
(
|
1076
|
+
(_c = await response.text()) != null ? _c : "Failed to fetch the chat response."
|
1078
1077
|
);
|
1079
1078
|
}
|
1080
1079
|
if (!response.body) {
|
@@ -1086,7 +1085,8 @@ async function callChatApi({
|
|
1086
1085
|
stream: response.body,
|
1087
1086
|
update: onUpdate,
|
1088
1087
|
onFinish,
|
1089
|
-
generateId: generateId3
|
1088
|
+
generateId: generateId3,
|
1089
|
+
getCurrentDate
|
1090
1090
|
});
|
1091
1091
|
return;
|
1092
1092
|
}
|
@@ -1101,7 +1101,8 @@ async function callChatApi({
|
|
1101
1101
|
onFinish(message, { usage, finishReason });
|
1102
1102
|
}
|
1103
1103
|
},
|
1104
|
-
generateId: generateId3
|
1104
|
+
generateId: generateId3,
|
1105
|
+
getCurrentDate
|
1105
1106
|
});
|
1106
1107
|
return;
|
1107
1108
|
}
|
@@ -1222,6 +1223,36 @@ async function callCompletionApi({
|
|
1222
1223
|
}
|
1223
1224
|
}
|
1224
1225
|
|
1226
|
+
// core/util/convert-file-list-to-file-ui-parts.ts
|
1227
|
+
async function convertFileListToFileUIParts(files) {
|
1228
|
+
if (files == null) {
|
1229
|
+
return [];
|
1230
|
+
}
|
1231
|
+
if (!globalThis.FileList || !(files instanceof globalThis.FileList)) {
|
1232
|
+
throw new Error("FileList is not supported in the current environment");
|
1233
|
+
}
|
1234
|
+
return Promise.all(
|
1235
|
+
Array.from(files).map(async (file) => {
|
1236
|
+
const { name: name17, type } = file;
|
1237
|
+
const dataUrl = await new Promise((resolve, reject) => {
|
1238
|
+
const reader = new FileReader();
|
1239
|
+
reader.onload = (readerEvent) => {
|
1240
|
+
var _a17;
|
1241
|
+
resolve((_a17 = readerEvent.target) == null ? void 0 : _a17.result);
|
1242
|
+
};
|
1243
|
+
reader.onerror = (error) => reject(error);
|
1244
|
+
reader.readAsDataURL(file);
|
1245
|
+
});
|
1246
|
+
return {
|
1247
|
+
type: "file",
|
1248
|
+
mediaType: type,
|
1249
|
+
filename: name17,
|
1250
|
+
url: dataUrl
|
1251
|
+
};
|
1252
|
+
})
|
1253
|
+
);
|
1254
|
+
}
|
1255
|
+
|
1225
1256
|
// core/util/data-url.ts
|
1226
1257
|
function getTextFromDataUrl(dataUrl) {
|
1227
1258
|
const [header, base64Content] = dataUrl.split(",");
|
@@ -1271,58 +1302,6 @@ function isDeepEqualData(obj1, obj2) {
|
|
1271
1302
|
return true;
|
1272
1303
|
}
|
1273
1304
|
|
1274
|
-
// core/util/prepare-attachments-for-request.ts
|
1275
|
-
async function prepareAttachmentsForRequest(attachmentsFromOptions) {
|
1276
|
-
if (!attachmentsFromOptions) {
|
1277
|
-
return [];
|
1278
|
-
}
|
1279
|
-
if (globalThis.FileList && attachmentsFromOptions instanceof globalThis.FileList) {
|
1280
|
-
return Promise.all(
|
1281
|
-
Array.from(attachmentsFromOptions).map(async (attachment) => {
|
1282
|
-
const { name: name17, type } = attachment;
|
1283
|
-
const dataUrl = await new Promise((resolve, reject) => {
|
1284
|
-
const reader = new FileReader();
|
1285
|
-
reader.onload = (readerEvent) => {
|
1286
|
-
var _a17;
|
1287
|
-
resolve((_a17 = readerEvent.target) == null ? void 0 : _a17.result);
|
1288
|
-
};
|
1289
|
-
reader.onerror = (error) => reject(error);
|
1290
|
-
reader.readAsDataURL(attachment);
|
1291
|
-
});
|
1292
|
-
return {
|
1293
|
-
name: name17,
|
1294
|
-
contentType: type,
|
1295
|
-
url: dataUrl
|
1296
|
-
};
|
1297
|
-
})
|
1298
|
-
);
|
1299
|
-
}
|
1300
|
-
if (Array.isArray(attachmentsFromOptions)) {
|
1301
|
-
return attachmentsFromOptions;
|
1302
|
-
}
|
1303
|
-
throw new Error("Invalid attachments type");
|
1304
|
-
}
|
1305
|
-
|
1306
|
-
// core/util/update-tool-call-result.ts
|
1307
|
-
function updateToolCallResult({
|
1308
|
-
messages,
|
1309
|
-
toolCallId,
|
1310
|
-
toolResult: result
|
1311
|
-
}) {
|
1312
|
-
const lastMessage = messages[messages.length - 1];
|
1313
|
-
const invocationPart = lastMessage.parts.find(
|
1314
|
-
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
|
1315
|
-
);
|
1316
|
-
if (invocationPart == null) {
|
1317
|
-
return;
|
1318
|
-
}
|
1319
|
-
invocationPart.toolInvocation = {
|
1320
|
-
...invocationPart.toolInvocation,
|
1321
|
-
state: "result",
|
1322
|
-
result
|
1323
|
-
};
|
1324
|
-
}
|
1325
|
-
|
1326
1305
|
// core/util/should-resubmit-messages.ts
|
1327
1306
|
function shouldResubmitMessages({
|
1328
1307
|
originalMaxToolInvocationStep,
|
@@ -1352,6 +1331,26 @@ function isAssistantMessageWithCompletedToolCalls(message) {
|
|
1352
1331
|
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => "result" in part.toolInvocation);
|
1353
1332
|
}
|
1354
1333
|
|
1334
|
+
// core/util/update-tool-call-result.ts
|
1335
|
+
function updateToolCallResult({
|
1336
|
+
messages,
|
1337
|
+
toolCallId,
|
1338
|
+
toolResult: result
|
1339
|
+
}) {
|
1340
|
+
const lastMessage = messages[messages.length - 1];
|
1341
|
+
const invocationPart = lastMessage.parts.find(
|
1342
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
|
1343
|
+
);
|
1344
|
+
if (invocationPart == null) {
|
1345
|
+
return;
|
1346
|
+
}
|
1347
|
+
invocationPart.toolInvocation = {
|
1348
|
+
...invocationPart.toolInvocation,
|
1349
|
+
state: "result",
|
1350
|
+
result
|
1351
|
+
};
|
1352
|
+
}
|
1353
|
+
|
1355
1354
|
// core/data-stream/create-data-stream.ts
|
1356
1355
|
function createDataStream({
|
1357
1356
|
execute,
|
@@ -1982,6 +1981,7 @@ function splitArray(array, chunkSize) {
|
|
1982
1981
|
async function embedMany({
|
1983
1982
|
model,
|
1984
1983
|
values,
|
1984
|
+
maxParallelCalls = Infinity,
|
1985
1985
|
maxRetries: maxRetriesArg,
|
1986
1986
|
abortSignal,
|
1987
1987
|
headers,
|
@@ -2011,7 +2011,10 @@ async function embedMany({
|
|
2011
2011
|
}),
|
2012
2012
|
tracer,
|
2013
2013
|
fn: async (span) => {
|
2014
|
-
const maxEmbeddingsPerCall = await
|
2014
|
+
const [maxEmbeddingsPerCall, supportsParallelCalls] = await Promise.all([
|
2015
|
+
model.maxEmbeddingsPerCall,
|
2016
|
+
model.supportsParallelCalls
|
2017
|
+
]);
|
2015
2018
|
if (maxEmbeddingsPerCall == null || maxEmbeddingsPerCall === Infinity) {
|
2016
2019
|
const { embeddings: embeddings2, usage, response } = await retry(() => {
|
2017
2020
|
return recordSpan({
|
@@ -2082,61 +2085,69 @@ async function embedMany({
|
|
2082
2085
|
const embeddings = [];
|
2083
2086
|
const responses = [];
|
2084
2087
|
let tokens = 0;
|
2085
|
-
|
2086
|
-
|
2087
|
-
|
2088
|
-
|
2089
|
-
|
2090
|
-
|
2091
|
-
|
2092
|
-
|
2093
|
-
|
2094
|
-
|
2095
|
-
|
2096
|
-
...assembleOperationName({
|
2097
|
-
operationId: "ai.embedMany.doEmbed",
|
2098
|
-
telemetry
|
2099
|
-
}),
|
2100
|
-
...baseTelemetryAttributes,
|
2101
|
-
// specific settings that only make sense on the outer level:
|
2102
|
-
"ai.values": {
|
2103
|
-
input: () => chunk.map((value) => JSON.stringify(value))
|
2104
|
-
}
|
2105
|
-
}
|
2106
|
-
}),
|
2107
|
-
tracer,
|
2108
|
-
fn: async (doEmbedSpan) => {
|
2109
|
-
var _a17;
|
2110
|
-
const modelResponse = await model.doEmbed({
|
2111
|
-
values: chunk,
|
2112
|
-
abortSignal,
|
2113
|
-
headers,
|
2114
|
-
providerOptions
|
2115
|
-
});
|
2116
|
-
const embeddings2 = modelResponse.embeddings;
|
2117
|
-
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
2118
|
-
doEmbedSpan.setAttributes(
|
2119
|
-
selectTelemetryAttributes({
|
2088
|
+
const parallelChunks = splitArray(
|
2089
|
+
valueChunks,
|
2090
|
+
supportsParallelCalls ? maxParallelCalls : 1
|
2091
|
+
);
|
2092
|
+
for (const parallelChunk of parallelChunks) {
|
2093
|
+
const results = await Promise.all(
|
2094
|
+
parallelChunk.map((chunk) => {
|
2095
|
+
return retry(() => {
|
2096
|
+
return recordSpan({
|
2097
|
+
name: "ai.embedMany.doEmbed",
|
2098
|
+
attributes: selectTelemetryAttributes({
|
2120
2099
|
telemetry,
|
2121
2100
|
attributes: {
|
2122
|
-
|
2123
|
-
|
2124
|
-
|
2125
|
-
|
2101
|
+
...assembleOperationName({
|
2102
|
+
operationId: "ai.embedMany.doEmbed",
|
2103
|
+
telemetry
|
2104
|
+
}),
|
2105
|
+
...baseTelemetryAttributes,
|
2106
|
+
// specific settings that only make sense on the outer level:
|
2107
|
+
"ai.values": {
|
2108
|
+
input: () => chunk.map((value) => JSON.stringify(value))
|
2109
|
+
}
|
2126
2110
|
}
|
2127
|
-
})
|
2128
|
-
|
2129
|
-
|
2130
|
-
|
2131
|
-
|
2132
|
-
|
2133
|
-
|
2134
|
-
|
2135
|
-
|
2136
|
-
|
2137
|
-
|
2138
|
-
|
2139
|
-
|
2111
|
+
}),
|
2112
|
+
tracer,
|
2113
|
+
fn: async (doEmbedSpan) => {
|
2114
|
+
var _a17;
|
2115
|
+
const modelResponse = await model.doEmbed({
|
2116
|
+
values: chunk,
|
2117
|
+
abortSignal,
|
2118
|
+
headers,
|
2119
|
+
providerOptions
|
2120
|
+
});
|
2121
|
+
const embeddings2 = modelResponse.embeddings;
|
2122
|
+
const usage = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
2123
|
+
doEmbedSpan.setAttributes(
|
2124
|
+
selectTelemetryAttributes({
|
2125
|
+
telemetry,
|
2126
|
+
attributes: {
|
2127
|
+
"ai.embeddings": {
|
2128
|
+
output: () => embeddings2.map(
|
2129
|
+
(embedding) => JSON.stringify(embedding)
|
2130
|
+
)
|
2131
|
+
},
|
2132
|
+
"ai.usage.tokens": usage.tokens
|
2133
|
+
}
|
2134
|
+
})
|
2135
|
+
);
|
2136
|
+
return {
|
2137
|
+
embeddings: embeddings2,
|
2138
|
+
usage,
|
2139
|
+
response: modelResponse.response
|
2140
|
+
};
|
2141
|
+
}
|
2142
|
+
});
|
2143
|
+
});
|
2144
|
+
})
|
2145
|
+
);
|
2146
|
+
for (const result of results) {
|
2147
|
+
embeddings.push(...result.embeddings);
|
2148
|
+
responses.push(result.response);
|
2149
|
+
tokens += result.usage.tokens;
|
2150
|
+
}
|
2140
2151
|
}
|
2141
2152
|
span.setAttributes(
|
2142
2153
|
selectTelemetryAttributes({
|
@@ -2372,7 +2383,7 @@ async function generateImage({
|
|
2372
2383
|
abortSignal,
|
2373
2384
|
headers
|
2374
2385
|
}) {
|
2375
|
-
var _a17;
|
2386
|
+
var _a17, _b;
|
2376
2387
|
const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
|
2377
2388
|
const maxImagesPerCall = (_a17 = model.maxImagesPerCall) != null ? _a17 : 1;
|
2378
2389
|
const callCount = Math.ceil(n / maxImagesPerCall);
|
@@ -2402,6 +2413,7 @@ async function generateImage({
|
|
2402
2413
|
const images = [];
|
2403
2414
|
const warnings = [];
|
2404
2415
|
const responses = [];
|
2416
|
+
const providerMetadata = {};
|
2405
2417
|
for (const result of results) {
|
2406
2418
|
images.push(
|
2407
2419
|
...result.images.map(
|
@@ -2418,18 +2430,32 @@ async function generateImage({
|
|
2418
2430
|
)
|
2419
2431
|
);
|
2420
2432
|
warnings.push(...result.warnings);
|
2433
|
+
if (result.providerMetadata) {
|
2434
|
+
for (const [providerName, metadata] of Object.entries(result.providerMetadata)) {
|
2435
|
+
(_b = providerMetadata[providerName]) != null ? _b : providerMetadata[providerName] = { images: [] };
|
2436
|
+
providerMetadata[providerName].images.push(
|
2437
|
+
...result.providerMetadata[providerName].images
|
2438
|
+
);
|
2439
|
+
}
|
2440
|
+
}
|
2421
2441
|
responses.push(result.response);
|
2422
2442
|
}
|
2423
2443
|
if (!images.length) {
|
2424
2444
|
throw new NoImageGeneratedError({ responses });
|
2425
2445
|
}
|
2426
|
-
return new DefaultGenerateImageResult({
|
2446
|
+
return new DefaultGenerateImageResult({
|
2447
|
+
images,
|
2448
|
+
warnings,
|
2449
|
+
responses,
|
2450
|
+
providerMetadata
|
2451
|
+
});
|
2427
2452
|
}
|
2428
2453
|
var DefaultGenerateImageResult = class {
|
2429
2454
|
constructor(options) {
|
2430
2455
|
this.images = options.images;
|
2431
2456
|
this.warnings = options.warnings;
|
2432
2457
|
this.responses = options.responses;
|
2458
|
+
this.providerMetadata = options.providerMetadata;
|
2433
2459
|
}
|
2434
2460
|
get image() {
|
2435
2461
|
return this.images[0];
|
@@ -2482,6 +2508,9 @@ function extractContentText(content) {
|
|
2482
2508
|
return parts.map((content2) => content2.text).join("");
|
2483
2509
|
}
|
2484
2510
|
|
2511
|
+
// core/prompt/convert-to-language-model-prompt.ts
|
2512
|
+
import { isUrlSupported } from "@ai-sdk/provider-utils";
|
2513
|
+
|
2485
2514
|
// util/download-error.ts
|
2486
2515
|
import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
|
2487
2516
|
var name5 = "AI_DownloadError";
|
@@ -2649,13 +2678,6 @@ function convertDataContentToUint8Array(content) {
|
|
2649
2678
|
}
|
2650
2679
|
throw new InvalidDataContentError({ content });
|
2651
2680
|
}
|
2652
|
-
function convertUint8ArrayToText(uint8Array) {
|
2653
|
-
try {
|
2654
|
-
return new TextDecoder().decode(uint8Array);
|
2655
|
-
} catch (error) {
|
2656
|
-
throw new Error("Error decoding Uint8Array to text");
|
2657
|
-
}
|
2658
|
-
}
|
2659
2681
|
|
2660
2682
|
// core/prompt/invalid-message-role-error.ts
|
2661
2683
|
import { AISDKError as AISDKError8 } from "@ai-sdk/provider";
|
@@ -2679,7 +2701,6 @@ var InvalidMessageRoleError = class extends AISDKError8 {
|
|
2679
2701
|
_a7 = symbol7;
|
2680
2702
|
|
2681
2703
|
// core/prompt/convert-to-language-model-prompt.ts
|
2682
|
-
import { isUrlSupported } from "@ai-sdk/provider-utils";
|
2683
2704
|
async function convertToLanguageModelPrompt({
|
2684
2705
|
prompt,
|
2685
2706
|
supportedUrls,
|
@@ -2735,7 +2756,6 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
2735
2756
|
// remove empty text parts:
|
2736
2757
|
(part) => part.type !== "text" || part.text !== ""
|
2737
2758
|
).map((part) => {
|
2738
|
-
var _a17;
|
2739
2759
|
const providerOptions = part.providerOptions;
|
2740
2760
|
switch (part.type) {
|
2741
2761
|
case "file": {
|
@@ -2746,7 +2766,7 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
2746
2766
|
type: "file",
|
2747
2767
|
data,
|
2748
2768
|
filename: part.filename,
|
2749
|
-
mediaType:
|
2769
|
+
mediaType: mediaType != null ? mediaType : part.mediaType,
|
2750
2770
|
providerOptions
|
2751
2771
|
};
|
2752
2772
|
}
|
@@ -2805,8 +2825,8 @@ async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
|
2805
2825
|
).flat().filter(
|
2806
2826
|
(part) => part.type === "image" || part.type === "file"
|
2807
2827
|
).map((part) => {
|
2808
|
-
var _a17
|
2809
|
-
const mediaType = (
|
2828
|
+
var _a17;
|
2829
|
+
const mediaType = (_a17 = part.mediaType) != null ? _a17 : part.type === "image" ? "image/*" : void 0;
|
2810
2830
|
let data = part.type === "image" ? part.image : part.data;
|
2811
2831
|
if (typeof data === "string") {
|
2812
2832
|
try {
|
@@ -2833,7 +2853,7 @@ async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
|
2833
2853
|
);
|
2834
2854
|
}
|
2835
2855
|
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
2836
|
-
var _a17, _b
|
2856
|
+
var _a17, _b;
|
2837
2857
|
if (part.type === "text") {
|
2838
2858
|
return {
|
2839
2859
|
type: "text",
|
@@ -2854,19 +2874,19 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
2854
2874
|
throw new Error(`Unsupported part type: ${type}`);
|
2855
2875
|
}
|
2856
2876
|
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
|
2857
|
-
let mediaType =
|
2877
|
+
let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
|
2858
2878
|
let data = convertedData;
|
2859
2879
|
if (data instanceof URL) {
|
2860
2880
|
const downloadedFile = downloadedAssets[data.toString()];
|
2861
2881
|
if (downloadedFile) {
|
2862
2882
|
data = downloadedFile.data;
|
2863
|
-
mediaType = (
|
2883
|
+
mediaType = (_a17 = downloadedFile.mediaType) != null ? _a17 : mediaType;
|
2864
2884
|
}
|
2865
2885
|
}
|
2866
2886
|
switch (type) {
|
2867
2887
|
case "image": {
|
2868
2888
|
if (data instanceof Uint8Array || typeof data === "string") {
|
2869
|
-
mediaType = (
|
2889
|
+
mediaType = (_b = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _b : mediaType;
|
2870
2890
|
}
|
2871
2891
|
return {
|
2872
2892
|
type: "file",
|
@@ -2990,83 +3010,6 @@ import { InvalidPromptError } from "@ai-sdk/provider";
|
|
2990
3010
|
import { safeValidateTypes } from "@ai-sdk/provider-utils";
|
2991
3011
|
import { z as z7 } from "zod";
|
2992
3012
|
|
2993
|
-
// core/prompt/attachments-to-parts.ts
|
2994
|
-
function attachmentsToParts(attachments) {
|
2995
|
-
var _a17, _b, _c;
|
2996
|
-
const parts = [];
|
2997
|
-
for (const attachment of attachments) {
|
2998
|
-
let url;
|
2999
|
-
try {
|
3000
|
-
url = new URL(attachment.url);
|
3001
|
-
} catch (error) {
|
3002
|
-
throw new Error(`Invalid URL: ${attachment.url}`);
|
3003
|
-
}
|
3004
|
-
switch (url.protocol) {
|
3005
|
-
case "http:":
|
3006
|
-
case "https:": {
|
3007
|
-
if ((_a17 = attachment.contentType) == null ? void 0 : _a17.startsWith("image/")) {
|
3008
|
-
parts.push({ type: "image", image: url });
|
3009
|
-
} else {
|
3010
|
-
if (!attachment.contentType) {
|
3011
|
-
throw new Error(
|
3012
|
-
"If the attachment is not an image, it must specify a content type"
|
3013
|
-
);
|
3014
|
-
}
|
3015
|
-
parts.push({
|
3016
|
-
type: "file",
|
3017
|
-
data: url,
|
3018
|
-
mediaType: attachment.contentType
|
3019
|
-
});
|
3020
|
-
}
|
3021
|
-
break;
|
3022
|
-
}
|
3023
|
-
case "data:": {
|
3024
|
-
let header;
|
3025
|
-
let base64Content;
|
3026
|
-
let mediaType;
|
3027
|
-
try {
|
3028
|
-
[header, base64Content] = attachment.url.split(",");
|
3029
|
-
mediaType = header.split(";")[0].split(":")[1];
|
3030
|
-
} catch (error) {
|
3031
|
-
throw new Error(`Error processing data URL: ${attachment.url}`);
|
3032
|
-
}
|
3033
|
-
if (mediaType == null || base64Content == null) {
|
3034
|
-
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
3035
|
-
}
|
3036
|
-
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
3037
|
-
parts.push({
|
3038
|
-
type: "image",
|
3039
|
-
image: convertDataContentToUint8Array(base64Content)
|
3040
|
-
});
|
3041
|
-
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
3042
|
-
parts.push({
|
3043
|
-
type: "text",
|
3044
|
-
text: convertUint8ArrayToText(
|
3045
|
-
convertDataContentToUint8Array(base64Content)
|
3046
|
-
)
|
3047
|
-
});
|
3048
|
-
} else {
|
3049
|
-
if (!attachment.contentType) {
|
3050
|
-
throw new Error(
|
3051
|
-
"If the attachment is not an image or text, it must specify a content type"
|
3052
|
-
);
|
3053
|
-
}
|
3054
|
-
parts.push({
|
3055
|
-
type: "file",
|
3056
|
-
data: base64Content,
|
3057
|
-
mediaType: attachment.contentType
|
3058
|
-
});
|
3059
|
-
}
|
3060
|
-
break;
|
3061
|
-
}
|
3062
|
-
default: {
|
3063
|
-
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
3064
|
-
}
|
3065
|
-
}
|
3066
|
-
}
|
3067
|
-
return parts;
|
3068
|
-
}
|
3069
|
-
|
3070
3013
|
// core/prompt/message-conversion-error.ts
|
3071
3014
|
import { AISDKError as AISDKError9 } from "@ai-sdk/provider";
|
3072
3015
|
var name8 = "AI_MessageConversionError";
|
@@ -3088,15 +3031,15 @@ var MessageConversionError = class extends AISDKError9 {
|
|
3088
3031
|
};
|
3089
3032
|
_a8 = symbol8;
|
3090
3033
|
|
3091
|
-
// core/prompt/convert-to-
|
3092
|
-
function
|
3034
|
+
// core/prompt/convert-to-model-messages.ts
|
3035
|
+
function convertToModelMessages(messages, options) {
|
3093
3036
|
var _a17, _b;
|
3094
3037
|
const tools = (_a17 = options == null ? void 0 : options.tools) != null ? _a17 : {};
|
3095
3038
|
const coreMessages = [];
|
3096
3039
|
for (let i = 0; i < messages.length; i++) {
|
3097
3040
|
const message = messages[i];
|
3098
3041
|
const isLastMessage = i === messages.length - 1;
|
3099
|
-
const { role, content
|
3042
|
+
const { role, content } = message;
|
3100
3043
|
switch (role) {
|
3101
3044
|
case "system": {
|
3102
3045
|
coreMessages.push({
|
@@ -3106,30 +3049,24 @@ function convertToCoreMessages(messages, options) {
|
|
3106
3049
|
break;
|
3107
3050
|
}
|
3108
3051
|
case "user": {
|
3109
|
-
|
3110
|
-
|
3111
|
-
|
3112
|
-
|
3113
|
-
|
3114
|
-
|
3115
|
-
|
3116
|
-
|
3117
|
-
|
3118
|
-
|
3119
|
-
|
3120
|
-
|
3121
|
-
|
3122
|
-
coreMessages.push({
|
3123
|
-
role: "user",
|
3124
|
-
content: experimental_attachments ? [...textParts, ...attachmentsToParts(experimental_attachments)] : textParts
|
3125
|
-
});
|
3126
|
-
}
|
3052
|
+
coreMessages.push({
|
3053
|
+
role: "user",
|
3054
|
+
content: message.parts.filter(
|
3055
|
+
(part) => part.type === "text" || part.type === "file"
|
3056
|
+
).map(
|
3057
|
+
(part) => part.type === "file" ? {
|
3058
|
+
type: "file",
|
3059
|
+
mediaType: part.mediaType,
|
3060
|
+
filename: part.filename,
|
3061
|
+
data: part.url
|
3062
|
+
} : part
|
3063
|
+
)
|
3064
|
+
});
|
3127
3065
|
break;
|
3128
3066
|
}
|
3129
3067
|
case "assistant": {
|
3130
3068
|
if (message.parts != null) {
|
3131
3069
|
let processBlock2 = function() {
|
3132
|
-
var _a18;
|
3133
3070
|
const content2 = [];
|
3134
3071
|
for (const part of block) {
|
3135
3072
|
switch (part.type) {
|
@@ -3140,9 +3077,8 @@ function convertToCoreMessages(messages, options) {
|
|
3140
3077
|
case "file": {
|
3141
3078
|
content2.push({
|
3142
3079
|
type: "file",
|
3143
|
-
|
3144
|
-
|
3145
|
-
// TODO migration, remove
|
3080
|
+
mediaType: part.mediaType,
|
3081
|
+
data: part.url
|
3146
3082
|
});
|
3147
3083
|
break;
|
3148
3084
|
}
|
@@ -3255,6 +3191,7 @@ function convertToCoreMessages(messages, options) {
|
|
3255
3191
|
}
|
3256
3192
|
return coreMessages;
|
3257
3193
|
}
|
3194
|
+
var convertToCoreMessages = convertToModelMessages;
|
3258
3195
|
|
3259
3196
|
// core/prompt/detect-prompt-type.ts
|
3260
3197
|
function detectPromptType(prompt) {
|
@@ -3343,7 +3280,6 @@ var imagePartSchema = z5.object({
|
|
3343
3280
|
type: z5.literal("image"),
|
3344
3281
|
image: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
3345
3282
|
mediaType: z5.string().optional(),
|
3346
|
-
mimeType: z5.string().optional(),
|
3347
3283
|
providerOptions: providerMetadataSchema.optional()
|
3348
3284
|
});
|
3349
3285
|
var filePartSchema = z5.object({
|
@@ -3351,7 +3287,6 @@ var filePartSchema = z5.object({
|
|
3351
3287
|
data: z5.union([dataContentSchema, z5.instanceof(URL)]),
|
3352
3288
|
filename: z5.string().optional(),
|
3353
3289
|
mediaType: z5.string(),
|
3354
|
-
mimeType: z5.string().optional(),
|
3355
3290
|
providerOptions: providerMetadataSchema.optional()
|
3356
3291
|
});
|
3357
3292
|
var reasoningPartSchema = z5.object({
|
@@ -3377,12 +3312,15 @@ var toolResultPartSchema = z5.object({
|
|
3377
3312
|
});
|
3378
3313
|
|
3379
3314
|
// core/prompt/message.ts
|
3380
|
-
var
|
3381
|
-
|
3382
|
-
|
3383
|
-
|
3384
|
-
|
3385
|
-
|
3315
|
+
var systemModelMessageSchema = z6.object(
|
3316
|
+
{
|
3317
|
+
role: z6.literal("system"),
|
3318
|
+
content: z6.string(),
|
3319
|
+
providerOptions: providerMetadataSchema.optional()
|
3320
|
+
}
|
3321
|
+
);
|
3322
|
+
var coreSystemMessageSchema = systemModelMessageSchema;
|
3323
|
+
var userModelMessageSchema = z6.object({
|
3386
3324
|
role: z6.literal("user"),
|
3387
3325
|
content: z6.union([
|
3388
3326
|
z6.string(),
|
@@ -3390,7 +3328,8 @@ var coreUserMessageSchema = z6.object({
|
|
3390
3328
|
]),
|
3391
3329
|
providerOptions: providerMetadataSchema.optional()
|
3392
3330
|
});
|
3393
|
-
var
|
3331
|
+
var coreUserMessageSchema = userModelMessageSchema;
|
3332
|
+
var assistantModelMessageSchema = z6.object({
|
3394
3333
|
role: z6.literal("assistant"),
|
3395
3334
|
content: z6.union([
|
3396
3335
|
z6.string(),
|
@@ -3405,17 +3344,20 @@ var coreAssistantMessageSchema = z6.object({
|
|
3405
3344
|
]),
|
3406
3345
|
providerOptions: providerMetadataSchema.optional()
|
3407
3346
|
});
|
3408
|
-
var
|
3347
|
+
var coreAssistantMessageSchema = assistantModelMessageSchema;
|
3348
|
+
var toolModelMessageSchema = z6.object({
|
3409
3349
|
role: z6.literal("tool"),
|
3410
3350
|
content: z6.array(toolResultPartSchema),
|
3411
3351
|
providerOptions: providerMetadataSchema.optional()
|
3412
3352
|
});
|
3413
|
-
var
|
3414
|
-
|
3415
|
-
|
3416
|
-
|
3417
|
-
|
3353
|
+
var coreToolMessageSchema = toolModelMessageSchema;
|
3354
|
+
var modelMessageSchema = z6.union([
|
3355
|
+
systemModelMessageSchema,
|
3356
|
+
userModelMessageSchema,
|
3357
|
+
assistantModelMessageSchema,
|
3358
|
+
toolModelMessageSchema
|
3418
3359
|
]);
|
3360
|
+
var coreMessageSchema = modelMessageSchema;
|
3419
3361
|
|
3420
3362
|
// core/prompt/standardize-prompt.ts
|
3421
3363
|
async function standardizePrompt({
|
@@ -3462,10 +3404,10 @@ async function standardizePrompt({
|
|
3462
3404
|
if (promptType === "other") {
|
3463
3405
|
throw new InvalidPromptError({
|
3464
3406
|
prompt,
|
3465
|
-
message: "messages must be an array of
|
3407
|
+
message: "messages must be an array of ModelMessage or UIMessage"
|
3466
3408
|
});
|
3467
3409
|
}
|
3468
|
-
const messages = promptType === "ui-messages" ?
|
3410
|
+
const messages = promptType === "ui-messages" ? convertToModelMessages(prompt.messages, {
|
3469
3411
|
tools
|
3470
3412
|
}) : prompt.messages;
|
3471
3413
|
if (messages.length === 0) {
|
@@ -3476,12 +3418,12 @@ async function standardizePrompt({
|
|
3476
3418
|
}
|
3477
3419
|
const validationResult = await safeValidateTypes({
|
3478
3420
|
value: messages,
|
3479
|
-
schema: z7.array(
|
3421
|
+
schema: z7.array(modelMessageSchema)
|
3480
3422
|
});
|
3481
3423
|
if (!validationResult.success) {
|
3482
3424
|
throw new InvalidPromptError({
|
3483
3425
|
prompt,
|
3484
|
-
message: "messages must be an array of
|
3426
|
+
message: "messages must be an array of ModelMessage or UIMessage",
|
3485
3427
|
cause: validationResult.error
|
3486
3428
|
});
|
3487
3429
|
}
|
@@ -3493,25 +3435,6 @@ async function standardizePrompt({
|
|
3493
3435
|
throw new Error("unreachable");
|
3494
3436
|
}
|
3495
3437
|
|
3496
|
-
// core/types/usage.ts
|
3497
|
-
function calculateLanguageModelUsage2({
|
3498
|
-
inputTokens,
|
3499
|
-
outputTokens
|
3500
|
-
}) {
|
3501
|
-
return {
|
3502
|
-
promptTokens: inputTokens != null ? inputTokens : NaN,
|
3503
|
-
completionTokens: outputTokens != null ? outputTokens : NaN,
|
3504
|
-
totalTokens: (inputTokens != null ? inputTokens : 0) + (outputTokens != null ? outputTokens : 0)
|
3505
|
-
};
|
3506
|
-
}
|
3507
|
-
function addLanguageModelUsage(usage1, usage2) {
|
3508
|
-
return {
|
3509
|
-
promptTokens: usage1.promptTokens + usage2.promptTokens,
|
3510
|
-
completionTokens: usage1.completionTokens + usage2.completionTokens,
|
3511
|
-
totalTokens: usage1.totalTokens + usage2.totalTokens
|
3512
|
-
};
|
3513
|
-
}
|
3514
|
-
|
3515
3438
|
// core/generate-object/output-strategy.ts
|
3516
3439
|
import {
|
3517
3440
|
isJSONArray,
|
@@ -4049,7 +3972,7 @@ async function generateObject(options) {
|
|
4049
3972
|
throw new NoObjectGeneratedError({
|
4050
3973
|
message: "No object generated: the model did not return a response.",
|
4051
3974
|
response: responseData,
|
4052
|
-
usage:
|
3975
|
+
usage: result2.usage,
|
4053
3976
|
finishReason: result2.finishReason
|
4054
3977
|
});
|
4055
3978
|
}
|
@@ -4093,7 +4016,7 @@ async function generateObject(options) {
|
|
4093
4016
|
cause: parseResult.error,
|
4094
4017
|
text: result2,
|
4095
4018
|
response,
|
4096
|
-
usage
|
4019
|
+
usage,
|
4097
4020
|
finishReason
|
4098
4021
|
});
|
4099
4022
|
}
|
@@ -4102,7 +4025,7 @@ async function generateObject(options) {
|
|
4102
4025
|
{
|
4103
4026
|
text: result2,
|
4104
4027
|
response,
|
4105
|
-
usage
|
4028
|
+
usage
|
4106
4029
|
}
|
4107
4030
|
);
|
4108
4031
|
if (!validationResult.success) {
|
@@ -4111,7 +4034,7 @@ async function generateObject(options) {
|
|
4111
4034
|
cause: validationResult.error,
|
4112
4035
|
text: result2,
|
4113
4036
|
response,
|
4114
|
-
usage
|
4037
|
+
usage,
|
4115
4038
|
finishReason
|
4116
4039
|
});
|
4117
4040
|
}
|
@@ -4151,7 +4074,7 @@ async function generateObject(options) {
|
|
4151
4074
|
return new DefaultGenerateObjectResult({
|
4152
4075
|
object: object2,
|
4153
4076
|
finishReason,
|
4154
|
-
usage
|
4077
|
+
usage,
|
4155
4078
|
warnings,
|
4156
4079
|
request,
|
4157
4080
|
response,
|
@@ -4533,7 +4456,11 @@ var DefaultStreamObjectResult = class {
|
|
4533
4456
|
);
|
4534
4457
|
self.requestPromise.resolve(request != null ? request : {});
|
4535
4458
|
let warnings;
|
4536
|
-
let usage
|
4459
|
+
let usage = {
|
4460
|
+
inputTokens: void 0,
|
4461
|
+
outputTokens: void 0,
|
4462
|
+
totalTokens: void 0
|
4463
|
+
};
|
4537
4464
|
let finishReason;
|
4538
4465
|
let providerMetadata;
|
4539
4466
|
let object2;
|
@@ -4613,7 +4540,7 @@ var DefaultStreamObjectResult = class {
|
|
4613
4540
|
controller.enqueue({ type: "text-delta", textDelta });
|
4614
4541
|
}
|
4615
4542
|
finishReason = chunk.finishReason;
|
4616
|
-
usage =
|
4543
|
+
usage = chunk.usage;
|
4617
4544
|
providerMetadata = chunk.providerMetadata;
|
4618
4545
|
controller.enqueue({
|
4619
4546
|
...chunk,
|
@@ -4675,14 +4602,17 @@ var DefaultStreamObjectResult = class {
|
|
4675
4602
|
"ai.response.id": fullResponse.id,
|
4676
4603
|
"ai.response.model": fullResponse.modelId,
|
4677
4604
|
"ai.response.timestamp": fullResponse.timestamp.toISOString(),
|
4678
|
-
"ai.usage.
|
4679
|
-
"ai.usage.
|
4605
|
+
"ai.usage.inputTokens": finalUsage.inputTokens,
|
4606
|
+
"ai.usage.outputTokens": finalUsage.outputTokens,
|
4607
|
+
"ai.usage.totalTokens": finalUsage.totalTokens,
|
4608
|
+
"ai.usage.reasoningTokens": finalUsage.reasoningTokens,
|
4609
|
+
"ai.usage.cachedInputTokens": finalUsage.cachedInputTokens,
|
4680
4610
|
// standardized gen-ai llm span attributes:
|
4681
4611
|
"gen_ai.response.finish_reasons": [finishReason],
|
4682
4612
|
"gen_ai.response.id": fullResponse.id,
|
4683
4613
|
"gen_ai.response.model": fullResponse.modelId,
|
4684
|
-
"gen_ai.usage.input_tokens": finalUsage.
|
4685
|
-
"gen_ai.usage.output_tokens": finalUsage.
|
4614
|
+
"gen_ai.usage.input_tokens": finalUsage.inputTokens,
|
4615
|
+
"gen_ai.usage.output_tokens": finalUsage.outputTokens
|
4686
4616
|
}
|
4687
4617
|
})
|
4688
4618
|
);
|
@@ -4691,8 +4621,11 @@ var DefaultStreamObjectResult = class {
|
|
4691
4621
|
selectTelemetryAttributes({
|
4692
4622
|
telemetry,
|
4693
4623
|
attributes: {
|
4694
|
-
"ai.usage.
|
4695
|
-
"ai.usage.
|
4624
|
+
"ai.usage.inputTokens": finalUsage.inputTokens,
|
4625
|
+
"ai.usage.outputTokens": finalUsage.outputTokens,
|
4626
|
+
"ai.usage.totalTokens": finalUsage.totalTokens,
|
4627
|
+
"ai.usage.reasoningTokens": finalUsage.reasoningTokens,
|
4628
|
+
"ai.usage.cachedInputTokens": finalUsage.cachedInputTokens,
|
4696
4629
|
"ai.response.object": {
|
4697
4630
|
output: () => JSON.stringify(object2)
|
4698
4631
|
}
|
@@ -4922,6 +4855,26 @@ function prepareToolsAndToolChoice({
|
|
4922
4855
|
};
|
4923
4856
|
}
|
4924
4857
|
|
4858
|
+
// core/types/usage.ts
|
4859
|
+
function addLanguageModelUsage(usage1, usage2) {
|
4860
|
+
return {
|
4861
|
+
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
4862
|
+
outputTokens: addTokenCounts(usage1.outputTokens, usage2.outputTokens),
|
4863
|
+
totalTokens: addTokenCounts(usage1.totalTokens, usage2.totalTokens),
|
4864
|
+
reasoningTokens: addTokenCounts(
|
4865
|
+
usage1.reasoningTokens,
|
4866
|
+
usage2.reasoningTokens
|
4867
|
+
),
|
4868
|
+
cachedInputTokens: addTokenCounts(
|
4869
|
+
usage1.cachedInputTokens,
|
4870
|
+
usage2.cachedInputTokens
|
4871
|
+
)
|
4872
|
+
};
|
4873
|
+
}
|
4874
|
+
function addTokenCounts(tokenCount1, tokenCount2) {
|
4875
|
+
return tokenCount1 == null && tokenCount2 == null ? void 0 : (tokenCount1 != null ? tokenCount1 : 0) + (tokenCount2 != null ? tokenCount2 : 0);
|
4876
|
+
}
|
4877
|
+
|
4925
4878
|
// core/util/split-on-last-whitespace.ts
|
4926
4879
|
var lastWhitespaceRegexp = /^([\s\S]*?)(\s+)(\S*)$/;
|
4927
4880
|
function splitOnLastWhitespace(text2) {
|
@@ -5246,9 +5199,9 @@ async function generateText({
|
|
5246
5199
|
const sources = [];
|
5247
5200
|
const steps = [];
|
5248
5201
|
let usage = {
|
5249
|
-
|
5250
|
-
|
5251
|
-
totalTokens: 0
|
5202
|
+
inputTokens: void 0,
|
5203
|
+
outputTokens: void 0,
|
5204
|
+
totalTokens: void 0
|
5252
5205
|
};
|
5253
5206
|
let stepType = "initial";
|
5254
5207
|
do {
|
@@ -5389,10 +5342,7 @@ async function generateText({
|
|
5389
5342
|
messages: stepInputMessages,
|
5390
5343
|
abortSignal
|
5391
5344
|
});
|
5392
|
-
|
5393
|
-
currentModelResponse.usage
|
5394
|
-
);
|
5395
|
-
usage = addLanguageModelUsage(usage, currentUsage);
|
5345
|
+
usage = addLanguageModelUsage(usage, currentModelResponse.usage);
|
5396
5346
|
let nextStepType = "done";
|
5397
5347
|
if (++stepCount < maxSteps) {
|
5398
5348
|
if (continueSteps && currentModelResponse.finishReason === "length" && // only use continue when there are no tool calls:
|
@@ -5457,7 +5407,7 @@ async function generateText({
|
|
5457
5407
|
toolCalls: currentToolCalls,
|
5458
5408
|
toolResults: currentToolResults,
|
5459
5409
|
finishReason: currentModelResponse.finishReason,
|
5460
|
-
usage:
|
5410
|
+
usage: currentModelResponse.usage,
|
5461
5411
|
warnings: currentModelResponse.warnings,
|
5462
5412
|
request: (_e = currentModelResponse.request) != null ? _e : {},
|
5463
5413
|
response: {
|
@@ -5995,6 +5945,7 @@ function runToolsTransformation({
|
|
5995
5945
|
const chunkType = chunk.type;
|
5996
5946
|
switch (chunkType) {
|
5997
5947
|
case "stream-start":
|
5948
|
+
case "finish":
|
5998
5949
|
case "text":
|
5999
5950
|
case "reasoning":
|
6000
5951
|
case "reasoning-part-finish":
|
@@ -6115,15 +6066,6 @@ function runToolsTransformation({
|
|
6115
6066
|
}
|
6116
6067
|
break;
|
6117
6068
|
}
|
6118
|
-
case "finish": {
|
6119
|
-
finishChunk = {
|
6120
|
-
type: "finish",
|
6121
|
-
finishReason: chunk.finishReason,
|
6122
|
-
usage: calculateLanguageModelUsage2(chunk.usage),
|
6123
|
-
providerMetadata: chunk.providerMetadata
|
6124
|
-
};
|
6125
|
-
break;
|
6126
|
-
}
|
6127
6069
|
default: {
|
6128
6070
|
const _exhaustiveCheck = chunkType;
|
6129
6071
|
throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
|
@@ -6486,9 +6428,9 @@ var DefaultStreamTextResult = class {
|
|
6486
6428
|
self.reasoningDetailsPromise.resolve(lastStep.reasoning);
|
6487
6429
|
const finishReason = recordedFinishReason != null ? recordedFinishReason : "unknown";
|
6488
6430
|
const usage = recordedUsage != null ? recordedUsage : {
|
6489
|
-
|
6490
|
-
|
6491
|
-
totalTokens:
|
6431
|
+
inputTokens: void 0,
|
6432
|
+
outputTokens: void 0,
|
6433
|
+
totalTokens: void 0
|
6492
6434
|
};
|
6493
6435
|
self.finishReasonPromise.resolve(finishReason);
|
6494
6436
|
self.usagePromise.resolve(usage);
|
@@ -6524,8 +6466,11 @@ var DefaultStreamTextResult = class {
|
|
6524
6466
|
return ((_a18 = lastStep.toolCalls) == null ? void 0 : _a18.length) ? JSON.stringify(lastStep.toolCalls) : void 0;
|
6525
6467
|
}
|
6526
6468
|
},
|
6527
|
-
"ai.usage.
|
6528
|
-
"ai.usage.
|
6469
|
+
"ai.usage.inputTokens": usage.inputTokens,
|
6470
|
+
"ai.usage.outputTokens": usage.outputTokens,
|
6471
|
+
"ai.usage.totalTokens": usage.totalTokens,
|
6472
|
+
"ai.usage.reasoningTokens": usage.reasoningTokens,
|
6473
|
+
"ai.usage.cachedInputTokens": usage.cachedInputTokens
|
6529
6474
|
}
|
6530
6475
|
})
|
6531
6476
|
);
|
@@ -6690,9 +6635,9 @@ var DefaultStreamTextResult = class {
|
|
6690
6635
|
let activeReasoningPart2 = void 0;
|
6691
6636
|
let stepFinishReason = "unknown";
|
6692
6637
|
let stepUsage = {
|
6693
|
-
|
6694
|
-
|
6695
|
-
totalTokens: 0
|
6638
|
+
inputTokens: void 0,
|
6639
|
+
outputTokens: void 0,
|
6640
|
+
totalTokens: void 0
|
6696
6641
|
};
|
6697
6642
|
let stepProviderMetadata;
|
6698
6643
|
let stepFirstChunk = true;
|
@@ -6721,7 +6666,7 @@ var DefaultStreamTextResult = class {
|
|
6721
6666
|
transformedStream.pipeThrough(
|
6722
6667
|
new TransformStream({
|
6723
6668
|
async transform(chunk, controller) {
|
6724
|
-
var _a17, _b, _c;
|
6669
|
+
var _a17, _b, _c, _d;
|
6725
6670
|
if (chunk.type === "stream-start") {
|
6726
6671
|
warnings = chunk.warnings;
|
6727
6672
|
return;
|
@@ -6817,7 +6762,7 @@ var DefaultStreamTextResult = class {
|
|
6817
6762
|
doStreamSpan.addEvent("ai.stream.finish");
|
6818
6763
|
doStreamSpan.setAttributes({
|
6819
6764
|
"ai.response.msToFinish": msToFinish,
|
6820
|
-
"ai.response.
|
6765
|
+
"ai.response.avgOutputTokensPerSecond": 1e3 * ((_d = stepUsage.outputTokens) != null ? _d : 0) / msToFinish
|
6821
6766
|
});
|
6822
6767
|
break;
|
6823
6768
|
}
|
@@ -6880,14 +6825,17 @@ var DefaultStreamTextResult = class {
|
|
6880
6825
|
"ai.response.id": stepResponse.id,
|
6881
6826
|
"ai.response.model": stepResponse.modelId,
|
6882
6827
|
"ai.response.timestamp": stepResponse.timestamp.toISOString(),
|
6883
|
-
"ai.usage.
|
6884
|
-
"ai.usage.
|
6828
|
+
"ai.usage.inputTokens": stepUsage.inputTokens,
|
6829
|
+
"ai.usage.outputTokens": stepUsage.outputTokens,
|
6830
|
+
"ai.usage.totalTokens": stepUsage.totalTokens,
|
6831
|
+
"ai.usage.reasoningTokens": stepUsage.reasoningTokens,
|
6832
|
+
"ai.usage.cachedInputTokens": stepUsage.cachedInputTokens,
|
6885
6833
|
// standardized gen-ai llm span attributes:
|
6886
6834
|
"gen_ai.response.finish_reasons": [stepFinishReason],
|
6887
6835
|
"gen_ai.response.id": stepResponse.id,
|
6888
6836
|
"gen_ai.response.model": stepResponse.modelId,
|
6889
|
-
"gen_ai.usage.input_tokens": stepUsage.
|
6890
|
-
"gen_ai.usage.output_tokens": stepUsage.
|
6837
|
+
"gen_ai.usage.input_tokens": stepUsage.inputTokens,
|
6838
|
+
"gen_ai.usage.output_tokens": stepUsage.outputTokens
|
6891
6839
|
}
|
6892
6840
|
})
|
6893
6841
|
);
|
@@ -6969,9 +6917,9 @@ var DefaultStreamTextResult = class {
|
|
6969
6917
|
currentStep: 0,
|
6970
6918
|
responseMessages: [],
|
6971
6919
|
usage: {
|
6972
|
-
|
6973
|
-
|
6974
|
-
totalTokens: 0
|
6920
|
+
inputTokens: void 0,
|
6921
|
+
outputTokens: void 0,
|
6922
|
+
totalTokens: void 0
|
6975
6923
|
},
|
6976
6924
|
previousStepText: "",
|
6977
6925
|
stepType: "initial",
|
@@ -7132,8 +7080,8 @@ var DefaultStreamTextResult = class {
|
|
7132
7080
|
controller.enqueue(
|
7133
7081
|
// TODO update protocol to v2 or replace with event stream
|
7134
7082
|
formatDataStreamPart("file", {
|
7135
|
-
|
7136
|
-
|
7083
|
+
mediaType: chunk.file.mediaType,
|
7084
|
+
url: `data:${chunk.file.mediaType};base64,${chunk.file.base64}`
|
7137
7085
|
})
|
7138
7086
|
);
|
7139
7087
|
break;
|
@@ -7200,8 +7148,11 @@ var DefaultStreamTextResult = class {
|
|
7200
7148
|
formatDataStreamPart("finish_step", {
|
7201
7149
|
finishReason: chunk.finishReason,
|
7202
7150
|
usage: sendUsage ? {
|
7203
|
-
|
7204
|
-
|
7151
|
+
inputTokens: chunk.usage.inputTokens,
|
7152
|
+
outputTokens: chunk.usage.outputTokens,
|
7153
|
+
totalTokens: chunk.usage.totalTokens,
|
7154
|
+
reasoningTokens: chunk.usage.reasoningTokens,
|
7155
|
+
cachedInputTokens: chunk.usage.cachedInputTokens
|
7205
7156
|
} : void 0,
|
7206
7157
|
isContinued: chunk.isContinued
|
7207
7158
|
})
|
@@ -7214,8 +7165,11 @@ var DefaultStreamTextResult = class {
|
|
7214
7165
|
formatDataStreamPart("finish_message", {
|
7215
7166
|
finishReason: chunk.finishReason,
|
7216
7167
|
usage: sendUsage ? {
|
7217
|
-
|
7218
|
-
|
7168
|
+
inputTokens: chunk.usage.inputTokens,
|
7169
|
+
outputTokens: chunk.usage.outputTokens,
|
7170
|
+
totalTokens: chunk.usage.totalTokens,
|
7171
|
+
reasoningTokens: chunk.usage.reasoningTokens,
|
7172
|
+
cachedInputTokens: chunk.usage.cachedInputTokens
|
7219
7173
|
} : void 0
|
7220
7174
|
})
|
7221
7175
|
);
|
@@ -7355,16 +7309,16 @@ var DefaultGeneratedAudioFile = class extends DefaultGeneratedFile {
|
|
7355
7309
|
super({ data, mediaType });
|
7356
7310
|
let format = "mp3";
|
7357
7311
|
if (mediaType) {
|
7358
|
-
const
|
7359
|
-
if (
|
7312
|
+
const mediaTypeParts = mediaType.split("/");
|
7313
|
+
if (mediaTypeParts.length === 2) {
|
7360
7314
|
if (mediaType !== "audio/mpeg") {
|
7361
|
-
format =
|
7315
|
+
format = mediaTypeParts[1];
|
7362
7316
|
}
|
7363
7317
|
}
|
7364
7318
|
}
|
7365
7319
|
if (!format) {
|
7366
7320
|
throw new Error(
|
7367
|
-
"Audio format must be provided or determinable from
|
7321
|
+
"Audio format must be provided or determinable from media type"
|
7368
7322
|
);
|
7369
7323
|
}
|
7370
7324
|
this.format = format;
|
@@ -7793,7 +7747,7 @@ function appendResponseMessages({
|
|
7793
7747
|
responseMessages,
|
7794
7748
|
_internal: { currentDate = () => /* @__PURE__ */ new Date() } = {}
|
7795
7749
|
}) {
|
7796
|
-
var _a17, _b
|
7750
|
+
var _a17, _b;
|
7797
7751
|
const clonedMessages = structuredClone(messages);
|
7798
7752
|
for (const message of responseMessages) {
|
7799
7753
|
const role = message.role;
|
@@ -7857,8 +7811,8 @@ function appendResponseMessages({
|
|
7857
7811
|
}
|
7858
7812
|
parts.push({
|
7859
7813
|
type: "file",
|
7860
|
-
mediaType:
|
7861
|
-
|
7814
|
+
mediaType: part.mediaType,
|
7815
|
+
url: `data:${part.mediaType};base64,${convertDataContentToBase64String(part.data)}`
|
7862
7816
|
});
|
7863
7817
|
break;
|
7864
7818
|
}
|
@@ -7867,9 +7821,8 @@ function appendResponseMessages({
|
|
7867
7821
|
if (isLastMessageAssistant) {
|
7868
7822
|
const maxStep = extractMaxToolInvocationStep(
|
7869
7823
|
getToolInvocations(lastMessage)
|
7870
|
-
// TODO remove once Message is removed
|
7871
7824
|
);
|
7872
|
-
(
|
7825
|
+
(_a17 = lastMessage.parts) != null ? _a17 : lastMessage.parts = [];
|
7873
7826
|
lastMessage.content = textContent;
|
7874
7827
|
lastMessage.parts.push(...parts);
|
7875
7828
|
getToolInvocationsForStep2(maxStep === void 0 ? 0 : maxStep + 1).map((call) => ({
|
@@ -7902,12 +7855,11 @@ function appendResponseMessages({
|
|
7902
7855
|
`Tool result must follow an assistant message: ${lastMessage.role}`
|
7903
7856
|
);
|
7904
7857
|
}
|
7905
|
-
(
|
7858
|
+
(_b = lastMessage.parts) != null ? _b : lastMessage.parts = [];
|
7906
7859
|
for (const contentPart of message.content) {
|
7907
|
-
const toolCall = getToolInvocations(
|
7908
|
-
|
7909
|
-
|
7910
|
-
).find((call) => call.toolCallId === contentPart.toolCallId);
|
7860
|
+
const toolCall = getToolInvocations(lastMessage).find(
|
7861
|
+
(call) => call.toolCallId === contentPart.toolCallId
|
7862
|
+
);
|
7911
7863
|
const toolCallPart = lastMessage.parts.find(
|
7912
7864
|
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === contentPart.toolCallId
|
7913
7865
|
);
|
@@ -8795,9 +8747,12 @@ export {
|
|
8795
8747
|
appendClientMessage,
|
8796
8748
|
appendResponseMessages,
|
8797
8749
|
asSchema,
|
8750
|
+
assistantModelMessageSchema,
|
8798
8751
|
callChatApi,
|
8799
8752
|
callCompletionApi,
|
8753
|
+
convertFileListToFileUIParts,
|
8800
8754
|
convertToCoreMessages,
|
8755
|
+
convertToModelMessages,
|
8801
8756
|
coreAssistantMessageSchema,
|
8802
8757
|
coreMessageSchema,
|
8803
8758
|
coreSystemMessageSchema,
|
@@ -8829,10 +8784,10 @@ export {
|
|
8829
8784
|
isAssistantMessageWithCompletedToolCalls,
|
8830
8785
|
isDeepEqualData,
|
8831
8786
|
jsonSchema,
|
8787
|
+
modelMessageSchema,
|
8832
8788
|
parseDataStreamPart,
|
8833
8789
|
parsePartialJson,
|
8834
8790
|
pipeDataStreamToResponse,
|
8835
|
-
prepareAttachmentsForRequest,
|
8836
8791
|
processDataStream,
|
8837
8792
|
processTextStream,
|
8838
8793
|
shouldResubmitMessages,
|
@@ -8841,8 +8796,11 @@ export {
|
|
8841
8796
|
smoothStream,
|
8842
8797
|
streamObject,
|
8843
8798
|
streamText,
|
8799
|
+
systemModelMessageSchema,
|
8844
8800
|
tool,
|
8801
|
+
toolModelMessageSchema,
|
8845
8802
|
updateToolCallResult,
|
8803
|
+
userModelMessageSchema,
|
8846
8804
|
wrapLanguageModel
|
8847
8805
|
};
|
8848
8806
|
//# sourceMappingURL=index.mjs.map
|