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.js
CHANGED
@@ -52,9 +52,12 @@ __export(ai_exports, {
|
|
52
52
|
appendClientMessage: () => appendClientMessage,
|
53
53
|
appendResponseMessages: () => appendResponseMessages,
|
54
54
|
asSchema: () => import_provider_utils4.asSchema,
|
55
|
+
assistantModelMessageSchema: () => assistantModelMessageSchema,
|
55
56
|
callChatApi: () => callChatApi,
|
56
57
|
callCompletionApi: () => callCompletionApi,
|
58
|
+
convertFileListToFileUIParts: () => convertFileListToFileUIParts,
|
57
59
|
convertToCoreMessages: () => convertToCoreMessages,
|
60
|
+
convertToModelMessages: () => convertToModelMessages,
|
58
61
|
coreAssistantMessageSchema: () => coreAssistantMessageSchema,
|
59
62
|
coreMessageSchema: () => coreMessageSchema,
|
60
63
|
coreSystemMessageSchema: () => coreSystemMessageSchema,
|
@@ -86,10 +89,10 @@ __export(ai_exports, {
|
|
86
89
|
isAssistantMessageWithCompletedToolCalls: () => isAssistantMessageWithCompletedToolCalls,
|
87
90
|
isDeepEqualData: () => isDeepEqualData,
|
88
91
|
jsonSchema: () => import_provider_utils4.jsonSchema,
|
92
|
+
modelMessageSchema: () => modelMessageSchema,
|
89
93
|
parseDataStreamPart: () => parseDataStreamPart,
|
90
94
|
parsePartialJson: () => parsePartialJson,
|
91
95
|
pipeDataStreamToResponse: () => pipeDataStreamToResponse,
|
92
|
-
prepareAttachmentsForRequest: () => prepareAttachmentsForRequest,
|
93
96
|
processDataStream: () => processDataStream,
|
94
97
|
processTextStream: () => processTextStream,
|
95
98
|
shouldResubmitMessages: () => shouldResubmitMessages,
|
@@ -98,8 +101,11 @@ __export(ai_exports, {
|
|
98
101
|
smoothStream: () => smoothStream,
|
99
102
|
streamObject: () => streamObject,
|
100
103
|
streamText: () => streamText,
|
104
|
+
systemModelMessageSchema: () => systemModelMessageSchema,
|
101
105
|
tool: () => tool,
|
106
|
+
toolModelMessageSchema: () => toolModelMessageSchema,
|
102
107
|
updateToolCallResult: () => updateToolCallResult,
|
108
|
+
userModelMessageSchema: () => userModelMessageSchema,
|
103
109
|
wrapLanguageModel: () => wrapLanguageModel
|
104
110
|
});
|
105
111
|
module.exports = __toCommonJS(ai_exports);
|
@@ -113,18 +119,6 @@ var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
113
119
|
// core/util/process-chat-response.ts
|
114
120
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
115
121
|
|
116
|
-
// core/types/duplicated/usage.ts
|
117
|
-
function calculateLanguageModelUsage({
|
118
|
-
promptTokens,
|
119
|
-
completionTokens
|
120
|
-
}) {
|
121
|
-
return {
|
122
|
-
promptTokens,
|
123
|
-
completionTokens,
|
124
|
-
totalTokens: promptTokens + completionTokens
|
125
|
-
};
|
126
|
-
}
|
127
|
-
|
128
122
|
// core/ui/get-tool-invocations.ts
|
129
123
|
function getToolInvocations(message) {
|
130
124
|
return message.parts.filter(
|
@@ -590,10 +584,13 @@ var finishMessageStreamPart = {
|
|
590
584
|
const result = {
|
591
585
|
finishReason: value.finishReason
|
592
586
|
};
|
593
|
-
if ("usage" in value && value.usage != null && typeof value.usage === "object"
|
587
|
+
if ("usage" in value && value.usage != null && typeof value.usage === "object") {
|
594
588
|
result.usage = {
|
595
|
-
|
596
|
-
|
589
|
+
inputTokens: "inputTokens" in value.usage && typeof value.usage.inputTokens === "number" ? value.usage.inputTokens : void 0,
|
590
|
+
outputTokens: "outputTokens" in value.usage && typeof value.usage.outputTokens === "number" ? value.usage.outputTokens : void 0,
|
591
|
+
totalTokens: "totalTokens" in value.usage && typeof value.usage.totalTokens === "number" ? value.usage.totalTokens : void 0,
|
592
|
+
reasoningTokens: "reasoningTokens" in value.usage && typeof value.usage.reasoningTokens === "number" ? value.usage.reasoningTokens : void 0,
|
593
|
+
cachedInputTokens: "cachedInputTokens" in value.usage && typeof value.usage.cachedInputTokens === "number" ? value.usage.cachedInputTokens : void 0
|
597
594
|
};
|
598
595
|
}
|
599
596
|
return {
|
@@ -615,10 +612,13 @@ var finishStepStreamPart = {
|
|
615
612
|
finishReason: value.finishReason,
|
616
613
|
isContinued: false
|
617
614
|
};
|
618
|
-
if ("usage" in value && value.usage != null && typeof value.usage === "object"
|
615
|
+
if ("usage" in value && value.usage != null && typeof value.usage === "object") {
|
619
616
|
result.usage = {
|
620
|
-
|
621
|
-
|
617
|
+
inputTokens: "inputTokens" in value.usage && typeof value.usage.inputTokens === "number" ? value.usage.inputTokens : void 0,
|
618
|
+
outputTokens: "outputTokens" in value.usage && typeof value.usage.outputTokens === "number" ? value.usage.outputTokens : void 0,
|
619
|
+
totalTokens: "totalTokens" in value.usage && typeof value.usage.totalTokens === "number" ? value.usage.totalTokens : void 0,
|
620
|
+
reasoningTokens: "reasoningTokens" in value.usage && typeof value.usage.reasoningTokens === "number" ? value.usage.reasoningTokens : void 0,
|
621
|
+
cachedInputTokens: "cachedInputTokens" in value.usage && typeof value.usage.cachedInputTokens === "number" ? value.usage.cachedInputTokens : void 0
|
622
622
|
};
|
623
623
|
}
|
624
624
|
if ("isContinued" in value && typeof value.isContinued === "boolean") {
|
@@ -682,9 +682,9 @@ var fileStreamPart = {
|
|
682
682
|
code: "k",
|
683
683
|
name: "file",
|
684
684
|
parse: (value) => {
|
685
|
-
if (value == null || typeof value !== "object" || !("
|
685
|
+
if (value == null || typeof value !== "object" || !("url" in value) || typeof value.url !== "string" || !("mediaType" in value) || typeof value.mediaType !== "string") {
|
686
686
|
throw new Error(
|
687
|
-
'"file" parts expect an object with a "
|
687
|
+
'"file" parts expect an object with a "url" and "mediaType" property.'
|
688
688
|
);
|
689
689
|
}
|
690
690
|
return { type: "file", value };
|
@@ -891,9 +891,9 @@ async function processChatResponse({
|
|
891
891
|
let messageAnnotations = replaceLastMessage ? lastMessage == null ? void 0 : lastMessage.annotations : void 0;
|
892
892
|
const partialToolCalls = {};
|
893
893
|
let usage = {
|
894
|
-
|
895
|
-
|
896
|
-
totalTokens:
|
894
|
+
inputTokens: void 0,
|
895
|
+
outputTokens: void 0,
|
896
|
+
totalTokens: void 0
|
897
897
|
};
|
898
898
|
let finishReason = "unknown";
|
899
899
|
function execUpdate() {
|
@@ -955,8 +955,8 @@ async function processChatResponse({
|
|
955
955
|
onFilePart(value) {
|
956
956
|
message.parts.push({
|
957
957
|
type: "file",
|
958
|
-
mediaType: value.
|
959
|
-
|
958
|
+
mediaType: value.mediaType,
|
959
|
+
url: value.url
|
960
960
|
});
|
961
961
|
execUpdate();
|
962
962
|
},
|
@@ -1066,7 +1066,7 @@ async function processChatResponse({
|
|
1066
1066
|
onFinishMessagePart(value) {
|
1067
1067
|
finishReason = value.finishReason;
|
1068
1068
|
if (value.usage != null) {
|
1069
|
-
usage =
|
1069
|
+
usage = value.usage;
|
1070
1070
|
}
|
1071
1071
|
},
|
1072
1072
|
onErrorPart(error) {
|
@@ -1123,7 +1123,11 @@ async function processChatTextResponse({
|
|
1123
1123
|
}
|
1124
1124
|
});
|
1125
1125
|
onFinish == null ? void 0 : onFinish(resultMessage, {
|
1126
|
-
usage: {
|
1126
|
+
usage: {
|
1127
|
+
inputTokens: void 0,
|
1128
|
+
outputTokens: void 0,
|
1129
|
+
totalTokens: void 0
|
1130
|
+
},
|
1127
1131
|
finishReason: "unknown"
|
1128
1132
|
});
|
1129
1133
|
}
|
@@ -1137,40 +1141,41 @@ async function callChatApi({
|
|
1137
1141
|
credentials,
|
1138
1142
|
headers,
|
1139
1143
|
abortController,
|
1140
|
-
restoreMessagesOnFailure,
|
1141
1144
|
onResponse,
|
1142
1145
|
onUpdate,
|
1143
1146
|
onFinish,
|
1144
1147
|
onToolCall,
|
1145
1148
|
generateId: generateId3,
|
1146
1149
|
fetch: fetch2 = getOriginalFetch(),
|
1147
|
-
lastMessage
|
1150
|
+
lastMessage,
|
1151
|
+
getCurrentDate,
|
1152
|
+
requestType = "generate"
|
1148
1153
|
}) {
|
1149
|
-
var _a17, _b;
|
1150
|
-
const response = await fetch2(api
|
1154
|
+
var _a17, _b, _c;
|
1155
|
+
const response = requestType === "resume" ? await fetch2(`${api}?chatId=${body.id}`, {
|
1156
|
+
method: "GET",
|
1157
|
+
headers: {
|
1158
|
+
"Content-Type": "application/json",
|
1159
|
+
...headers
|
1160
|
+
},
|
1161
|
+
signal: (_a17 = abortController == null ? void 0 : abortController()) == null ? void 0 : _a17.signal,
|
1162
|
+
credentials
|
1163
|
+
}) : await fetch2(api, {
|
1151
1164
|
method: "POST",
|
1152
1165
|
body: JSON.stringify(body),
|
1153
1166
|
headers: {
|
1154
1167
|
"Content-Type": "application/json",
|
1155
1168
|
...headers
|
1156
1169
|
},
|
1157
|
-
signal: (
|
1170
|
+
signal: (_b = abortController == null ? void 0 : abortController()) == null ? void 0 : _b.signal,
|
1158
1171
|
credentials
|
1159
|
-
}).catch((err) => {
|
1160
|
-
restoreMessagesOnFailure();
|
1161
|
-
throw err;
|
1162
1172
|
});
|
1163
|
-
if (onResponse) {
|
1164
|
-
|
1165
|
-
await onResponse(response);
|
1166
|
-
} catch (err) {
|
1167
|
-
throw err;
|
1168
|
-
}
|
1173
|
+
if (onResponse != null) {
|
1174
|
+
await onResponse(response);
|
1169
1175
|
}
|
1170
1176
|
if (!response.ok) {
|
1171
|
-
restoreMessagesOnFailure();
|
1172
1177
|
throw new Error(
|
1173
|
-
(
|
1178
|
+
(_c = await response.text()) != null ? _c : "Failed to fetch the chat response."
|
1174
1179
|
);
|
1175
1180
|
}
|
1176
1181
|
if (!response.body) {
|
@@ -1182,7 +1187,8 @@ async function callChatApi({
|
|
1182
1187
|
stream: response.body,
|
1183
1188
|
update: onUpdate,
|
1184
1189
|
onFinish,
|
1185
|
-
generateId: generateId3
|
1190
|
+
generateId: generateId3,
|
1191
|
+
getCurrentDate
|
1186
1192
|
});
|
1187
1193
|
return;
|
1188
1194
|
}
|
@@ -1197,7 +1203,8 @@ async function callChatApi({
|
|
1197
1203
|
onFinish(message, { usage, finishReason });
|
1198
1204
|
}
|
1199
1205
|
},
|
1200
|
-
generateId: generateId3
|
1206
|
+
generateId: generateId3,
|
1207
|
+
getCurrentDate
|
1201
1208
|
});
|
1202
1209
|
return;
|
1203
1210
|
}
|
@@ -1318,6 +1325,36 @@ async function callCompletionApi({
|
|
1318
1325
|
}
|
1319
1326
|
}
|
1320
1327
|
|
1328
|
+
// core/util/convert-file-list-to-file-ui-parts.ts
|
1329
|
+
async function convertFileListToFileUIParts(files) {
|
1330
|
+
if (files == null) {
|
1331
|
+
return [];
|
1332
|
+
}
|
1333
|
+
if (!globalThis.FileList || !(files instanceof globalThis.FileList)) {
|
1334
|
+
throw new Error("FileList is not supported in the current environment");
|
1335
|
+
}
|
1336
|
+
return Promise.all(
|
1337
|
+
Array.from(files).map(async (file) => {
|
1338
|
+
const { name: name17, type } = file;
|
1339
|
+
const dataUrl = await new Promise((resolve, reject) => {
|
1340
|
+
const reader = new FileReader();
|
1341
|
+
reader.onload = (readerEvent) => {
|
1342
|
+
var _a17;
|
1343
|
+
resolve((_a17 = readerEvent.target) == null ? void 0 : _a17.result);
|
1344
|
+
};
|
1345
|
+
reader.onerror = (error) => reject(error);
|
1346
|
+
reader.readAsDataURL(file);
|
1347
|
+
});
|
1348
|
+
return {
|
1349
|
+
type: "file",
|
1350
|
+
mediaType: type,
|
1351
|
+
filename: name17,
|
1352
|
+
url: dataUrl
|
1353
|
+
};
|
1354
|
+
})
|
1355
|
+
);
|
1356
|
+
}
|
1357
|
+
|
1321
1358
|
// core/util/data-url.ts
|
1322
1359
|
function getTextFromDataUrl(dataUrl) {
|
1323
1360
|
const [header, base64Content] = dataUrl.split(",");
|
@@ -1367,58 +1404,6 @@ function isDeepEqualData(obj1, obj2) {
|
|
1367
1404
|
return true;
|
1368
1405
|
}
|
1369
1406
|
|
1370
|
-
// core/util/prepare-attachments-for-request.ts
|
1371
|
-
async function prepareAttachmentsForRequest(attachmentsFromOptions) {
|
1372
|
-
if (!attachmentsFromOptions) {
|
1373
|
-
return [];
|
1374
|
-
}
|
1375
|
-
if (globalThis.FileList && attachmentsFromOptions instanceof globalThis.FileList) {
|
1376
|
-
return Promise.all(
|
1377
|
-
Array.from(attachmentsFromOptions).map(async (attachment) => {
|
1378
|
-
const { name: name17, type } = attachment;
|
1379
|
-
const dataUrl = await new Promise((resolve, reject) => {
|
1380
|
-
const reader = new FileReader();
|
1381
|
-
reader.onload = (readerEvent) => {
|
1382
|
-
var _a17;
|
1383
|
-
resolve((_a17 = readerEvent.target) == null ? void 0 : _a17.result);
|
1384
|
-
};
|
1385
|
-
reader.onerror = (error) => reject(error);
|
1386
|
-
reader.readAsDataURL(attachment);
|
1387
|
-
});
|
1388
|
-
return {
|
1389
|
-
name: name17,
|
1390
|
-
contentType: type,
|
1391
|
-
url: dataUrl
|
1392
|
-
};
|
1393
|
-
})
|
1394
|
-
);
|
1395
|
-
}
|
1396
|
-
if (Array.isArray(attachmentsFromOptions)) {
|
1397
|
-
return attachmentsFromOptions;
|
1398
|
-
}
|
1399
|
-
throw new Error("Invalid attachments type");
|
1400
|
-
}
|
1401
|
-
|
1402
|
-
// core/util/update-tool-call-result.ts
|
1403
|
-
function updateToolCallResult({
|
1404
|
-
messages,
|
1405
|
-
toolCallId,
|
1406
|
-
toolResult: result
|
1407
|
-
}) {
|
1408
|
-
const lastMessage = messages[messages.length - 1];
|
1409
|
-
const invocationPart = lastMessage.parts.find(
|
1410
|
-
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
|
1411
|
-
);
|
1412
|
-
if (invocationPart == null) {
|
1413
|
-
return;
|
1414
|
-
}
|
1415
|
-
invocationPart.toolInvocation = {
|
1416
|
-
...invocationPart.toolInvocation,
|
1417
|
-
state: "result",
|
1418
|
-
result
|
1419
|
-
};
|
1420
|
-
}
|
1421
|
-
|
1422
1407
|
// core/util/should-resubmit-messages.ts
|
1423
1408
|
function shouldResubmitMessages({
|
1424
1409
|
originalMaxToolInvocationStep,
|
@@ -1448,6 +1433,26 @@ function isAssistantMessageWithCompletedToolCalls(message) {
|
|
1448
1433
|
return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => "result" in part.toolInvocation);
|
1449
1434
|
}
|
1450
1435
|
|
1436
|
+
// core/util/update-tool-call-result.ts
|
1437
|
+
function updateToolCallResult({
|
1438
|
+
messages,
|
1439
|
+
toolCallId,
|
1440
|
+
toolResult: result
|
1441
|
+
}) {
|
1442
|
+
const lastMessage = messages[messages.length - 1];
|
1443
|
+
const invocationPart = lastMessage.parts.find(
|
1444
|
+
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
|
1445
|
+
);
|
1446
|
+
if (invocationPart == null) {
|
1447
|
+
return;
|
1448
|
+
}
|
1449
|
+
invocationPart.toolInvocation = {
|
1450
|
+
...invocationPart.toolInvocation,
|
1451
|
+
state: "result",
|
1452
|
+
result
|
1453
|
+
};
|
1454
|
+
}
|
1455
|
+
|
1451
1456
|
// core/data-stream/create-data-stream.ts
|
1452
1457
|
function createDataStream({
|
1453
1458
|
execute,
|
@@ -2078,6 +2083,7 @@ function splitArray(array, chunkSize) {
|
|
2078
2083
|
async function embedMany({
|
2079
2084
|
model,
|
2080
2085
|
values,
|
2086
|
+
maxParallelCalls = Infinity,
|
2081
2087
|
maxRetries: maxRetriesArg,
|
2082
2088
|
abortSignal,
|
2083
2089
|
headers,
|
@@ -2107,7 +2113,10 @@ async function embedMany({
|
|
2107
2113
|
}),
|
2108
2114
|
tracer,
|
2109
2115
|
fn: async (span) => {
|
2110
|
-
const maxEmbeddingsPerCall = await
|
2116
|
+
const [maxEmbeddingsPerCall, supportsParallelCalls] = await Promise.all([
|
2117
|
+
model.maxEmbeddingsPerCall,
|
2118
|
+
model.supportsParallelCalls
|
2119
|
+
]);
|
2111
2120
|
if (maxEmbeddingsPerCall == null || maxEmbeddingsPerCall === Infinity) {
|
2112
2121
|
const { embeddings: embeddings2, usage, response } = await retry(() => {
|
2113
2122
|
return recordSpan({
|
@@ -2178,61 +2187,69 @@ async function embedMany({
|
|
2178
2187
|
const embeddings = [];
|
2179
2188
|
const responses = [];
|
2180
2189
|
let tokens = 0;
|
2181
|
-
|
2182
|
-
|
2183
|
-
|
2184
|
-
|
2185
|
-
|
2186
|
-
|
2187
|
-
|
2188
|
-
|
2189
|
-
|
2190
|
-
|
2191
|
-
|
2192
|
-
...assembleOperationName({
|
2193
|
-
operationId: "ai.embedMany.doEmbed",
|
2194
|
-
telemetry
|
2195
|
-
}),
|
2196
|
-
...baseTelemetryAttributes,
|
2197
|
-
// specific settings that only make sense on the outer level:
|
2198
|
-
"ai.values": {
|
2199
|
-
input: () => chunk.map((value) => JSON.stringify(value))
|
2200
|
-
}
|
2201
|
-
}
|
2202
|
-
}),
|
2203
|
-
tracer,
|
2204
|
-
fn: async (doEmbedSpan) => {
|
2205
|
-
var _a17;
|
2206
|
-
const modelResponse = await model.doEmbed({
|
2207
|
-
values: chunk,
|
2208
|
-
abortSignal,
|
2209
|
-
headers,
|
2210
|
-
providerOptions
|
2211
|
-
});
|
2212
|
-
const embeddings2 = modelResponse.embeddings;
|
2213
|
-
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
2214
|
-
doEmbedSpan.setAttributes(
|
2215
|
-
selectTelemetryAttributes({
|
2190
|
+
const parallelChunks = splitArray(
|
2191
|
+
valueChunks,
|
2192
|
+
supportsParallelCalls ? maxParallelCalls : 1
|
2193
|
+
);
|
2194
|
+
for (const parallelChunk of parallelChunks) {
|
2195
|
+
const results = await Promise.all(
|
2196
|
+
parallelChunk.map((chunk) => {
|
2197
|
+
return retry(() => {
|
2198
|
+
return recordSpan({
|
2199
|
+
name: "ai.embedMany.doEmbed",
|
2200
|
+
attributes: selectTelemetryAttributes({
|
2216
2201
|
telemetry,
|
2217
2202
|
attributes: {
|
2218
|
-
|
2219
|
-
|
2220
|
-
|
2221
|
-
|
2203
|
+
...assembleOperationName({
|
2204
|
+
operationId: "ai.embedMany.doEmbed",
|
2205
|
+
telemetry
|
2206
|
+
}),
|
2207
|
+
...baseTelemetryAttributes,
|
2208
|
+
// specific settings that only make sense on the outer level:
|
2209
|
+
"ai.values": {
|
2210
|
+
input: () => chunk.map((value) => JSON.stringify(value))
|
2211
|
+
}
|
2222
2212
|
}
|
2223
|
-
})
|
2224
|
-
|
2225
|
-
|
2226
|
-
|
2227
|
-
|
2228
|
-
|
2229
|
-
|
2230
|
-
|
2231
|
-
|
2232
|
-
|
2233
|
-
|
2234
|
-
|
2235
|
-
|
2213
|
+
}),
|
2214
|
+
tracer,
|
2215
|
+
fn: async (doEmbedSpan) => {
|
2216
|
+
var _a17;
|
2217
|
+
const modelResponse = await model.doEmbed({
|
2218
|
+
values: chunk,
|
2219
|
+
abortSignal,
|
2220
|
+
headers,
|
2221
|
+
providerOptions
|
2222
|
+
});
|
2223
|
+
const embeddings2 = modelResponse.embeddings;
|
2224
|
+
const usage = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
2225
|
+
doEmbedSpan.setAttributes(
|
2226
|
+
selectTelemetryAttributes({
|
2227
|
+
telemetry,
|
2228
|
+
attributes: {
|
2229
|
+
"ai.embeddings": {
|
2230
|
+
output: () => embeddings2.map(
|
2231
|
+
(embedding) => JSON.stringify(embedding)
|
2232
|
+
)
|
2233
|
+
},
|
2234
|
+
"ai.usage.tokens": usage.tokens
|
2235
|
+
}
|
2236
|
+
})
|
2237
|
+
);
|
2238
|
+
return {
|
2239
|
+
embeddings: embeddings2,
|
2240
|
+
usage,
|
2241
|
+
response: modelResponse.response
|
2242
|
+
};
|
2243
|
+
}
|
2244
|
+
});
|
2245
|
+
});
|
2246
|
+
})
|
2247
|
+
);
|
2248
|
+
for (const result of results) {
|
2249
|
+
embeddings.push(...result.embeddings);
|
2250
|
+
responses.push(result.response);
|
2251
|
+
tokens += result.usage.tokens;
|
2252
|
+
}
|
2236
2253
|
}
|
2237
2254
|
span.setAttributes(
|
2238
2255
|
selectTelemetryAttributes({
|
@@ -2465,7 +2482,7 @@ async function generateImage({
|
|
2465
2482
|
abortSignal,
|
2466
2483
|
headers
|
2467
2484
|
}) {
|
2468
|
-
var _a17;
|
2485
|
+
var _a17, _b;
|
2469
2486
|
const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
|
2470
2487
|
const maxImagesPerCall = (_a17 = model.maxImagesPerCall) != null ? _a17 : 1;
|
2471
2488
|
const callCount = Math.ceil(n / maxImagesPerCall);
|
@@ -2495,6 +2512,7 @@ async function generateImage({
|
|
2495
2512
|
const images = [];
|
2496
2513
|
const warnings = [];
|
2497
2514
|
const responses = [];
|
2515
|
+
const providerMetadata = {};
|
2498
2516
|
for (const result of results) {
|
2499
2517
|
images.push(
|
2500
2518
|
...result.images.map(
|
@@ -2511,18 +2529,32 @@ async function generateImage({
|
|
2511
2529
|
)
|
2512
2530
|
);
|
2513
2531
|
warnings.push(...result.warnings);
|
2532
|
+
if (result.providerMetadata) {
|
2533
|
+
for (const [providerName, metadata] of Object.entries(result.providerMetadata)) {
|
2534
|
+
(_b = providerMetadata[providerName]) != null ? _b : providerMetadata[providerName] = { images: [] };
|
2535
|
+
providerMetadata[providerName].images.push(
|
2536
|
+
...result.providerMetadata[providerName].images
|
2537
|
+
);
|
2538
|
+
}
|
2539
|
+
}
|
2514
2540
|
responses.push(result.response);
|
2515
2541
|
}
|
2516
2542
|
if (!images.length) {
|
2517
2543
|
throw new NoImageGeneratedError({ responses });
|
2518
2544
|
}
|
2519
|
-
return new DefaultGenerateImageResult({
|
2545
|
+
return new DefaultGenerateImageResult({
|
2546
|
+
images,
|
2547
|
+
warnings,
|
2548
|
+
responses,
|
2549
|
+
providerMetadata
|
2550
|
+
});
|
2520
2551
|
}
|
2521
2552
|
var DefaultGenerateImageResult = class {
|
2522
2553
|
constructor(options) {
|
2523
2554
|
this.images = options.images;
|
2524
2555
|
this.warnings = options.warnings;
|
2525
2556
|
this.responses = options.responses;
|
2557
|
+
this.providerMetadata = options.providerMetadata;
|
2526
2558
|
}
|
2527
2559
|
get image() {
|
2528
2560
|
return this.images[0];
|
@@ -2572,6 +2604,9 @@ function extractContentText(content) {
|
|
2572
2604
|
return parts.map((content2) => content2.text).join("");
|
2573
2605
|
}
|
2574
2606
|
|
2607
|
+
// core/prompt/convert-to-language-model-prompt.ts
|
2608
|
+
var import_provider_utils9 = require("@ai-sdk/provider-utils");
|
2609
|
+
|
2575
2610
|
// util/download-error.ts
|
2576
2611
|
var import_provider6 = require("@ai-sdk/provider");
|
2577
2612
|
var name5 = "AI_DownloadError";
|
@@ -2736,13 +2771,6 @@ function convertDataContentToUint8Array(content) {
|
|
2736
2771
|
}
|
2737
2772
|
throw new InvalidDataContentError({ content });
|
2738
2773
|
}
|
2739
|
-
function convertUint8ArrayToText(uint8Array) {
|
2740
|
-
try {
|
2741
|
-
return new TextDecoder().decode(uint8Array);
|
2742
|
-
} catch (error) {
|
2743
|
-
throw new Error("Error decoding Uint8Array to text");
|
2744
|
-
}
|
2745
|
-
}
|
2746
2774
|
|
2747
2775
|
// core/prompt/invalid-message-role-error.ts
|
2748
2776
|
var import_provider9 = require("@ai-sdk/provider");
|
@@ -2766,7 +2794,6 @@ var InvalidMessageRoleError = class extends import_provider9.AISDKError {
|
|
2766
2794
|
_a7 = symbol7;
|
2767
2795
|
|
2768
2796
|
// core/prompt/convert-to-language-model-prompt.ts
|
2769
|
-
var import_provider_utils9 = require("@ai-sdk/provider-utils");
|
2770
2797
|
async function convertToLanguageModelPrompt({
|
2771
2798
|
prompt,
|
2772
2799
|
supportedUrls,
|
@@ -2822,7 +2849,6 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
2822
2849
|
// remove empty text parts:
|
2823
2850
|
(part) => part.type !== "text" || part.text !== ""
|
2824
2851
|
).map((part) => {
|
2825
|
-
var _a17;
|
2826
2852
|
const providerOptions = part.providerOptions;
|
2827
2853
|
switch (part.type) {
|
2828
2854
|
case "file": {
|
@@ -2833,7 +2859,7 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
2833
2859
|
type: "file",
|
2834
2860
|
data,
|
2835
2861
|
filename: part.filename,
|
2836
|
-
mediaType:
|
2862
|
+
mediaType: mediaType != null ? mediaType : part.mediaType,
|
2837
2863
|
providerOptions
|
2838
2864
|
};
|
2839
2865
|
}
|
@@ -2892,8 +2918,8 @@ async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
|
2892
2918
|
).flat().filter(
|
2893
2919
|
(part) => part.type === "image" || part.type === "file"
|
2894
2920
|
).map((part) => {
|
2895
|
-
var _a17
|
2896
|
-
const mediaType = (
|
2921
|
+
var _a17;
|
2922
|
+
const mediaType = (_a17 = part.mediaType) != null ? _a17 : part.type === "image" ? "image/*" : void 0;
|
2897
2923
|
let data = part.type === "image" ? part.image : part.data;
|
2898
2924
|
if (typeof data === "string") {
|
2899
2925
|
try {
|
@@ -2920,7 +2946,7 @@ async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
|
2920
2946
|
);
|
2921
2947
|
}
|
2922
2948
|
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
2923
|
-
var _a17, _b
|
2949
|
+
var _a17, _b;
|
2924
2950
|
if (part.type === "text") {
|
2925
2951
|
return {
|
2926
2952
|
type: "text",
|
@@ -2941,19 +2967,19 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
2941
2967
|
throw new Error(`Unsupported part type: ${type}`);
|
2942
2968
|
}
|
2943
2969
|
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
|
2944
|
-
let mediaType =
|
2970
|
+
let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
|
2945
2971
|
let data = convertedData;
|
2946
2972
|
if (data instanceof URL) {
|
2947
2973
|
const downloadedFile = downloadedAssets[data.toString()];
|
2948
2974
|
if (downloadedFile) {
|
2949
2975
|
data = downloadedFile.data;
|
2950
|
-
mediaType = (
|
2976
|
+
mediaType = (_a17 = downloadedFile.mediaType) != null ? _a17 : mediaType;
|
2951
2977
|
}
|
2952
2978
|
}
|
2953
2979
|
switch (type) {
|
2954
2980
|
case "image": {
|
2955
2981
|
if (data instanceof Uint8Array || typeof data === "string") {
|
2956
|
-
mediaType = (
|
2982
|
+
mediaType = (_b = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _b : mediaType;
|
2957
2983
|
}
|
2958
2984
|
return {
|
2959
2985
|
type: "file",
|
@@ -3077,83 +3103,6 @@ var import_provider11 = require("@ai-sdk/provider");
|
|
3077
3103
|
var import_provider_utils10 = require("@ai-sdk/provider-utils");
|
3078
3104
|
var import_zod7 = require("zod");
|
3079
3105
|
|
3080
|
-
// core/prompt/attachments-to-parts.ts
|
3081
|
-
function attachmentsToParts(attachments) {
|
3082
|
-
var _a17, _b, _c;
|
3083
|
-
const parts = [];
|
3084
|
-
for (const attachment of attachments) {
|
3085
|
-
let url;
|
3086
|
-
try {
|
3087
|
-
url = new URL(attachment.url);
|
3088
|
-
} catch (error) {
|
3089
|
-
throw new Error(`Invalid URL: ${attachment.url}`);
|
3090
|
-
}
|
3091
|
-
switch (url.protocol) {
|
3092
|
-
case "http:":
|
3093
|
-
case "https:": {
|
3094
|
-
if ((_a17 = attachment.contentType) == null ? void 0 : _a17.startsWith("image/")) {
|
3095
|
-
parts.push({ type: "image", image: url });
|
3096
|
-
} else {
|
3097
|
-
if (!attachment.contentType) {
|
3098
|
-
throw new Error(
|
3099
|
-
"If the attachment is not an image, it must specify a content type"
|
3100
|
-
);
|
3101
|
-
}
|
3102
|
-
parts.push({
|
3103
|
-
type: "file",
|
3104
|
-
data: url,
|
3105
|
-
mediaType: attachment.contentType
|
3106
|
-
});
|
3107
|
-
}
|
3108
|
-
break;
|
3109
|
-
}
|
3110
|
-
case "data:": {
|
3111
|
-
let header;
|
3112
|
-
let base64Content;
|
3113
|
-
let mediaType;
|
3114
|
-
try {
|
3115
|
-
[header, base64Content] = attachment.url.split(",");
|
3116
|
-
mediaType = header.split(";")[0].split(":")[1];
|
3117
|
-
} catch (error) {
|
3118
|
-
throw new Error(`Error processing data URL: ${attachment.url}`);
|
3119
|
-
}
|
3120
|
-
if (mediaType == null || base64Content == null) {
|
3121
|
-
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
3122
|
-
}
|
3123
|
-
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
3124
|
-
parts.push({
|
3125
|
-
type: "image",
|
3126
|
-
image: convertDataContentToUint8Array(base64Content)
|
3127
|
-
});
|
3128
|
-
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
3129
|
-
parts.push({
|
3130
|
-
type: "text",
|
3131
|
-
text: convertUint8ArrayToText(
|
3132
|
-
convertDataContentToUint8Array(base64Content)
|
3133
|
-
)
|
3134
|
-
});
|
3135
|
-
} else {
|
3136
|
-
if (!attachment.contentType) {
|
3137
|
-
throw new Error(
|
3138
|
-
"If the attachment is not an image or text, it must specify a content type"
|
3139
|
-
);
|
3140
|
-
}
|
3141
|
-
parts.push({
|
3142
|
-
type: "file",
|
3143
|
-
data: base64Content,
|
3144
|
-
mediaType: attachment.contentType
|
3145
|
-
});
|
3146
|
-
}
|
3147
|
-
break;
|
3148
|
-
}
|
3149
|
-
default: {
|
3150
|
-
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
3151
|
-
}
|
3152
|
-
}
|
3153
|
-
}
|
3154
|
-
return parts;
|
3155
|
-
}
|
3156
|
-
|
3157
3106
|
// core/prompt/message-conversion-error.ts
|
3158
3107
|
var import_provider10 = require("@ai-sdk/provider");
|
3159
3108
|
var name8 = "AI_MessageConversionError";
|
@@ -3175,15 +3124,15 @@ var MessageConversionError = class extends import_provider10.AISDKError {
|
|
3175
3124
|
};
|
3176
3125
|
_a8 = symbol8;
|
3177
3126
|
|
3178
|
-
// core/prompt/convert-to-
|
3179
|
-
function
|
3127
|
+
// core/prompt/convert-to-model-messages.ts
|
3128
|
+
function convertToModelMessages(messages, options) {
|
3180
3129
|
var _a17, _b;
|
3181
3130
|
const tools = (_a17 = options == null ? void 0 : options.tools) != null ? _a17 : {};
|
3182
3131
|
const coreMessages = [];
|
3183
3132
|
for (let i = 0; i < messages.length; i++) {
|
3184
3133
|
const message = messages[i];
|
3185
3134
|
const isLastMessage = i === messages.length - 1;
|
3186
|
-
const { role, content
|
3135
|
+
const { role, content } = message;
|
3187
3136
|
switch (role) {
|
3188
3137
|
case "system": {
|
3189
3138
|
coreMessages.push({
|
@@ -3193,30 +3142,24 @@ function convertToCoreMessages(messages, options) {
|
|
3193
3142
|
break;
|
3194
3143
|
}
|
3195
3144
|
case "user": {
|
3196
|
-
|
3197
|
-
|
3198
|
-
|
3199
|
-
|
3200
|
-
|
3201
|
-
|
3202
|
-
|
3203
|
-
|
3204
|
-
|
3205
|
-
|
3206
|
-
|
3207
|
-
|
3208
|
-
|
3209
|
-
coreMessages.push({
|
3210
|
-
role: "user",
|
3211
|
-
content: experimental_attachments ? [...textParts, ...attachmentsToParts(experimental_attachments)] : textParts
|
3212
|
-
});
|
3213
|
-
}
|
3145
|
+
coreMessages.push({
|
3146
|
+
role: "user",
|
3147
|
+
content: message.parts.filter(
|
3148
|
+
(part) => part.type === "text" || part.type === "file"
|
3149
|
+
).map(
|
3150
|
+
(part) => part.type === "file" ? {
|
3151
|
+
type: "file",
|
3152
|
+
mediaType: part.mediaType,
|
3153
|
+
filename: part.filename,
|
3154
|
+
data: part.url
|
3155
|
+
} : part
|
3156
|
+
)
|
3157
|
+
});
|
3214
3158
|
break;
|
3215
3159
|
}
|
3216
3160
|
case "assistant": {
|
3217
3161
|
if (message.parts != null) {
|
3218
3162
|
let processBlock2 = function() {
|
3219
|
-
var _a18;
|
3220
3163
|
const content2 = [];
|
3221
3164
|
for (const part of block) {
|
3222
3165
|
switch (part.type) {
|
@@ -3227,9 +3170,8 @@ function convertToCoreMessages(messages, options) {
|
|
3227
3170
|
case "file": {
|
3228
3171
|
content2.push({
|
3229
3172
|
type: "file",
|
3230
|
-
|
3231
|
-
|
3232
|
-
// TODO migration, remove
|
3173
|
+
mediaType: part.mediaType,
|
3174
|
+
data: part.url
|
3233
3175
|
});
|
3234
3176
|
break;
|
3235
3177
|
}
|
@@ -3342,6 +3284,7 @@ function convertToCoreMessages(messages, options) {
|
|
3342
3284
|
}
|
3343
3285
|
return coreMessages;
|
3344
3286
|
}
|
3287
|
+
var convertToCoreMessages = convertToModelMessages;
|
3345
3288
|
|
3346
3289
|
// core/prompt/detect-prompt-type.ts
|
3347
3290
|
function detectPromptType(prompt) {
|
@@ -3430,7 +3373,6 @@ var imagePartSchema = import_zod5.z.object({
|
|
3430
3373
|
type: import_zod5.z.literal("image"),
|
3431
3374
|
image: import_zod5.z.union([dataContentSchema, import_zod5.z.instanceof(URL)]),
|
3432
3375
|
mediaType: import_zod5.z.string().optional(),
|
3433
|
-
mimeType: import_zod5.z.string().optional(),
|
3434
3376
|
providerOptions: providerMetadataSchema.optional()
|
3435
3377
|
});
|
3436
3378
|
var filePartSchema = import_zod5.z.object({
|
@@ -3438,7 +3380,6 @@ var filePartSchema = import_zod5.z.object({
|
|
3438
3380
|
data: import_zod5.z.union([dataContentSchema, import_zod5.z.instanceof(URL)]),
|
3439
3381
|
filename: import_zod5.z.string().optional(),
|
3440
3382
|
mediaType: import_zod5.z.string(),
|
3441
|
-
mimeType: import_zod5.z.string().optional(),
|
3442
3383
|
providerOptions: providerMetadataSchema.optional()
|
3443
3384
|
});
|
3444
3385
|
var reasoningPartSchema = import_zod5.z.object({
|
@@ -3464,12 +3405,15 @@ var toolResultPartSchema = import_zod5.z.object({
|
|
3464
3405
|
});
|
3465
3406
|
|
3466
3407
|
// core/prompt/message.ts
|
3467
|
-
var
|
3468
|
-
|
3469
|
-
|
3470
|
-
|
3471
|
-
|
3472
|
-
|
3408
|
+
var systemModelMessageSchema = import_zod6.z.object(
|
3409
|
+
{
|
3410
|
+
role: import_zod6.z.literal("system"),
|
3411
|
+
content: import_zod6.z.string(),
|
3412
|
+
providerOptions: providerMetadataSchema.optional()
|
3413
|
+
}
|
3414
|
+
);
|
3415
|
+
var coreSystemMessageSchema = systemModelMessageSchema;
|
3416
|
+
var userModelMessageSchema = import_zod6.z.object({
|
3473
3417
|
role: import_zod6.z.literal("user"),
|
3474
3418
|
content: import_zod6.z.union([
|
3475
3419
|
import_zod6.z.string(),
|
@@ -3477,7 +3421,8 @@ var coreUserMessageSchema = import_zod6.z.object({
|
|
3477
3421
|
]),
|
3478
3422
|
providerOptions: providerMetadataSchema.optional()
|
3479
3423
|
});
|
3480
|
-
var
|
3424
|
+
var coreUserMessageSchema = userModelMessageSchema;
|
3425
|
+
var assistantModelMessageSchema = import_zod6.z.object({
|
3481
3426
|
role: import_zod6.z.literal("assistant"),
|
3482
3427
|
content: import_zod6.z.union([
|
3483
3428
|
import_zod6.z.string(),
|
@@ -3492,17 +3437,20 @@ var coreAssistantMessageSchema = import_zod6.z.object({
|
|
3492
3437
|
]),
|
3493
3438
|
providerOptions: providerMetadataSchema.optional()
|
3494
3439
|
});
|
3495
|
-
var
|
3440
|
+
var coreAssistantMessageSchema = assistantModelMessageSchema;
|
3441
|
+
var toolModelMessageSchema = import_zod6.z.object({
|
3496
3442
|
role: import_zod6.z.literal("tool"),
|
3497
3443
|
content: import_zod6.z.array(toolResultPartSchema),
|
3498
3444
|
providerOptions: providerMetadataSchema.optional()
|
3499
3445
|
});
|
3500
|
-
var
|
3501
|
-
|
3502
|
-
|
3503
|
-
|
3504
|
-
|
3446
|
+
var coreToolMessageSchema = toolModelMessageSchema;
|
3447
|
+
var modelMessageSchema = import_zod6.z.union([
|
3448
|
+
systemModelMessageSchema,
|
3449
|
+
userModelMessageSchema,
|
3450
|
+
assistantModelMessageSchema,
|
3451
|
+
toolModelMessageSchema
|
3505
3452
|
]);
|
3453
|
+
var coreMessageSchema = modelMessageSchema;
|
3506
3454
|
|
3507
3455
|
// core/prompt/standardize-prompt.ts
|
3508
3456
|
async function standardizePrompt({
|
@@ -3549,10 +3497,10 @@ async function standardizePrompt({
|
|
3549
3497
|
if (promptType === "other") {
|
3550
3498
|
throw new import_provider11.InvalidPromptError({
|
3551
3499
|
prompt,
|
3552
|
-
message: "messages must be an array of
|
3500
|
+
message: "messages must be an array of ModelMessage or UIMessage"
|
3553
3501
|
});
|
3554
3502
|
}
|
3555
|
-
const messages = promptType === "ui-messages" ?
|
3503
|
+
const messages = promptType === "ui-messages" ? convertToModelMessages(prompt.messages, {
|
3556
3504
|
tools
|
3557
3505
|
}) : prompt.messages;
|
3558
3506
|
if (messages.length === 0) {
|
@@ -3563,12 +3511,12 @@ async function standardizePrompt({
|
|
3563
3511
|
}
|
3564
3512
|
const validationResult = await (0, import_provider_utils10.safeValidateTypes)({
|
3565
3513
|
value: messages,
|
3566
|
-
schema: import_zod7.z.array(
|
3514
|
+
schema: import_zod7.z.array(modelMessageSchema)
|
3567
3515
|
});
|
3568
3516
|
if (!validationResult.success) {
|
3569
3517
|
throw new import_provider11.InvalidPromptError({
|
3570
3518
|
prompt,
|
3571
|
-
message: "messages must be an array of
|
3519
|
+
message: "messages must be an array of ModelMessage or UIMessage",
|
3572
3520
|
cause: validationResult.error
|
3573
3521
|
});
|
3574
3522
|
}
|
@@ -3580,25 +3528,6 @@ async function standardizePrompt({
|
|
3580
3528
|
throw new Error("unreachable");
|
3581
3529
|
}
|
3582
3530
|
|
3583
|
-
// core/types/usage.ts
|
3584
|
-
function calculateLanguageModelUsage2({
|
3585
|
-
inputTokens,
|
3586
|
-
outputTokens
|
3587
|
-
}) {
|
3588
|
-
return {
|
3589
|
-
promptTokens: inputTokens != null ? inputTokens : NaN,
|
3590
|
-
completionTokens: outputTokens != null ? outputTokens : NaN,
|
3591
|
-
totalTokens: (inputTokens != null ? inputTokens : 0) + (outputTokens != null ? outputTokens : 0)
|
3592
|
-
};
|
3593
|
-
}
|
3594
|
-
function addLanguageModelUsage(usage1, usage2) {
|
3595
|
-
return {
|
3596
|
-
promptTokens: usage1.promptTokens + usage2.promptTokens,
|
3597
|
-
completionTokens: usage1.completionTokens + usage2.completionTokens,
|
3598
|
-
totalTokens: usage1.totalTokens + usage2.totalTokens
|
3599
|
-
};
|
3600
|
-
}
|
3601
|
-
|
3602
3531
|
// core/generate-object/output-strategy.ts
|
3603
3532
|
var import_provider12 = require("@ai-sdk/provider");
|
3604
3533
|
var import_provider_utils11 = require("@ai-sdk/provider-utils");
|
@@ -4131,7 +4060,7 @@ async function generateObject(options) {
|
|
4131
4060
|
throw new NoObjectGeneratedError({
|
4132
4061
|
message: "No object generated: the model did not return a response.",
|
4133
4062
|
response: responseData,
|
4134
|
-
usage:
|
4063
|
+
usage: result2.usage,
|
4135
4064
|
finishReason: result2.finishReason
|
4136
4065
|
});
|
4137
4066
|
}
|
@@ -4175,7 +4104,7 @@ async function generateObject(options) {
|
|
4175
4104
|
cause: parseResult.error,
|
4176
4105
|
text: result2,
|
4177
4106
|
response,
|
4178
|
-
usage
|
4107
|
+
usage,
|
4179
4108
|
finishReason
|
4180
4109
|
});
|
4181
4110
|
}
|
@@ -4184,7 +4113,7 @@ async function generateObject(options) {
|
|
4184
4113
|
{
|
4185
4114
|
text: result2,
|
4186
4115
|
response,
|
4187
|
-
usage
|
4116
|
+
usage
|
4188
4117
|
}
|
4189
4118
|
);
|
4190
4119
|
if (!validationResult.success) {
|
@@ -4193,7 +4122,7 @@ async function generateObject(options) {
|
|
4193
4122
|
cause: validationResult.error,
|
4194
4123
|
text: result2,
|
4195
4124
|
response,
|
4196
|
-
usage
|
4125
|
+
usage,
|
4197
4126
|
finishReason
|
4198
4127
|
});
|
4199
4128
|
}
|
@@ -4233,7 +4162,7 @@ async function generateObject(options) {
|
|
4233
4162
|
return new DefaultGenerateObjectResult({
|
4234
4163
|
object: object2,
|
4235
4164
|
finishReason,
|
4236
|
-
usage
|
4165
|
+
usage,
|
4237
4166
|
warnings,
|
4238
4167
|
request,
|
4239
4168
|
response,
|
@@ -4615,7 +4544,11 @@ var DefaultStreamObjectResult = class {
|
|
4615
4544
|
);
|
4616
4545
|
self.requestPromise.resolve(request != null ? request : {});
|
4617
4546
|
let warnings;
|
4618
|
-
let usage
|
4547
|
+
let usage = {
|
4548
|
+
inputTokens: void 0,
|
4549
|
+
outputTokens: void 0,
|
4550
|
+
totalTokens: void 0
|
4551
|
+
};
|
4619
4552
|
let finishReason;
|
4620
4553
|
let providerMetadata;
|
4621
4554
|
let object2;
|
@@ -4695,7 +4628,7 @@ var DefaultStreamObjectResult = class {
|
|
4695
4628
|
controller.enqueue({ type: "text-delta", textDelta });
|
4696
4629
|
}
|
4697
4630
|
finishReason = chunk.finishReason;
|
4698
|
-
usage =
|
4631
|
+
usage = chunk.usage;
|
4699
4632
|
providerMetadata = chunk.providerMetadata;
|
4700
4633
|
controller.enqueue({
|
4701
4634
|
...chunk,
|
@@ -4757,14 +4690,17 @@ var DefaultStreamObjectResult = class {
|
|
4757
4690
|
"ai.response.id": fullResponse.id,
|
4758
4691
|
"ai.response.model": fullResponse.modelId,
|
4759
4692
|
"ai.response.timestamp": fullResponse.timestamp.toISOString(),
|
4760
|
-
"ai.usage.
|
4761
|
-
"ai.usage.
|
4693
|
+
"ai.usage.inputTokens": finalUsage.inputTokens,
|
4694
|
+
"ai.usage.outputTokens": finalUsage.outputTokens,
|
4695
|
+
"ai.usage.totalTokens": finalUsage.totalTokens,
|
4696
|
+
"ai.usage.reasoningTokens": finalUsage.reasoningTokens,
|
4697
|
+
"ai.usage.cachedInputTokens": finalUsage.cachedInputTokens,
|
4762
4698
|
// standardized gen-ai llm span attributes:
|
4763
4699
|
"gen_ai.response.finish_reasons": [finishReason],
|
4764
4700
|
"gen_ai.response.id": fullResponse.id,
|
4765
4701
|
"gen_ai.response.model": fullResponse.modelId,
|
4766
|
-
"gen_ai.usage.input_tokens": finalUsage.
|
4767
|
-
"gen_ai.usage.output_tokens": finalUsage.
|
4702
|
+
"gen_ai.usage.input_tokens": finalUsage.inputTokens,
|
4703
|
+
"gen_ai.usage.output_tokens": finalUsage.outputTokens
|
4768
4704
|
}
|
4769
4705
|
})
|
4770
4706
|
);
|
@@ -4773,8 +4709,11 @@ var DefaultStreamObjectResult = class {
|
|
4773
4709
|
selectTelemetryAttributes({
|
4774
4710
|
telemetry,
|
4775
4711
|
attributes: {
|
4776
|
-
"ai.usage.
|
4777
|
-
"ai.usage.
|
4712
|
+
"ai.usage.inputTokens": finalUsage.inputTokens,
|
4713
|
+
"ai.usage.outputTokens": finalUsage.outputTokens,
|
4714
|
+
"ai.usage.totalTokens": finalUsage.totalTokens,
|
4715
|
+
"ai.usage.reasoningTokens": finalUsage.reasoningTokens,
|
4716
|
+
"ai.usage.cachedInputTokens": finalUsage.cachedInputTokens,
|
4778
4717
|
"ai.response.object": {
|
4779
4718
|
output: () => JSON.stringify(object2)
|
4780
4719
|
}
|
@@ -5004,6 +4943,26 @@ function prepareToolsAndToolChoice({
|
|
5004
4943
|
};
|
5005
4944
|
}
|
5006
4945
|
|
4946
|
+
// core/types/usage.ts
|
4947
|
+
function addLanguageModelUsage(usage1, usage2) {
|
4948
|
+
return {
|
4949
|
+
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
4950
|
+
outputTokens: addTokenCounts(usage1.outputTokens, usage2.outputTokens),
|
4951
|
+
totalTokens: addTokenCounts(usage1.totalTokens, usage2.totalTokens),
|
4952
|
+
reasoningTokens: addTokenCounts(
|
4953
|
+
usage1.reasoningTokens,
|
4954
|
+
usage2.reasoningTokens
|
4955
|
+
),
|
4956
|
+
cachedInputTokens: addTokenCounts(
|
4957
|
+
usage1.cachedInputTokens,
|
4958
|
+
usage2.cachedInputTokens
|
4959
|
+
)
|
4960
|
+
};
|
4961
|
+
}
|
4962
|
+
function addTokenCounts(tokenCount1, tokenCount2) {
|
4963
|
+
return tokenCount1 == null && tokenCount2 == null ? void 0 : (tokenCount1 != null ? tokenCount1 : 0) + (tokenCount2 != null ? tokenCount2 : 0);
|
4964
|
+
}
|
4965
|
+
|
5007
4966
|
// core/util/split-on-last-whitespace.ts
|
5008
4967
|
var lastWhitespaceRegexp = /^([\s\S]*?)(\s+)(\S*)$/;
|
5009
4968
|
function splitOnLastWhitespace(text2) {
|
@@ -5328,9 +5287,9 @@ async function generateText({
|
|
5328
5287
|
const sources = [];
|
5329
5288
|
const steps = [];
|
5330
5289
|
let usage = {
|
5331
|
-
|
5332
|
-
|
5333
|
-
totalTokens: 0
|
5290
|
+
inputTokens: void 0,
|
5291
|
+
outputTokens: void 0,
|
5292
|
+
totalTokens: void 0
|
5334
5293
|
};
|
5335
5294
|
let stepType = "initial";
|
5336
5295
|
do {
|
@@ -5471,10 +5430,7 @@ async function generateText({
|
|
5471
5430
|
messages: stepInputMessages,
|
5472
5431
|
abortSignal
|
5473
5432
|
});
|
5474
|
-
|
5475
|
-
currentModelResponse.usage
|
5476
|
-
);
|
5477
|
-
usage = addLanguageModelUsage(usage, currentUsage);
|
5433
|
+
usage = addLanguageModelUsage(usage, currentModelResponse.usage);
|
5478
5434
|
let nextStepType = "done";
|
5479
5435
|
if (++stepCount < maxSteps) {
|
5480
5436
|
if (continueSteps && currentModelResponse.finishReason === "length" && // only use continue when there are no tool calls:
|
@@ -5539,7 +5495,7 @@ async function generateText({
|
|
5539
5495
|
toolCalls: currentToolCalls,
|
5540
5496
|
toolResults: currentToolResults,
|
5541
5497
|
finishReason: currentModelResponse.finishReason,
|
5542
|
-
usage:
|
5498
|
+
usage: currentModelResponse.usage,
|
5543
5499
|
warnings: currentModelResponse.warnings,
|
5544
5500
|
request: (_e = currentModelResponse.request) != null ? _e : {},
|
5545
5501
|
response: {
|
@@ -6065,6 +6021,7 @@ function runToolsTransformation({
|
|
6065
6021
|
const chunkType = chunk.type;
|
6066
6022
|
switch (chunkType) {
|
6067
6023
|
case "stream-start":
|
6024
|
+
case "finish":
|
6068
6025
|
case "text":
|
6069
6026
|
case "reasoning":
|
6070
6027
|
case "reasoning-part-finish":
|
@@ -6185,15 +6142,6 @@ function runToolsTransformation({
|
|
6185
6142
|
}
|
6186
6143
|
break;
|
6187
6144
|
}
|
6188
|
-
case "finish": {
|
6189
|
-
finishChunk = {
|
6190
|
-
type: "finish",
|
6191
|
-
finishReason: chunk.finishReason,
|
6192
|
-
usage: calculateLanguageModelUsage2(chunk.usage),
|
6193
|
-
providerMetadata: chunk.providerMetadata
|
6194
|
-
};
|
6195
|
-
break;
|
6196
|
-
}
|
6197
6145
|
default: {
|
6198
6146
|
const _exhaustiveCheck = chunkType;
|
6199
6147
|
throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
|
@@ -6556,9 +6504,9 @@ var DefaultStreamTextResult = class {
|
|
6556
6504
|
self.reasoningDetailsPromise.resolve(lastStep.reasoning);
|
6557
6505
|
const finishReason = recordedFinishReason != null ? recordedFinishReason : "unknown";
|
6558
6506
|
const usage = recordedUsage != null ? recordedUsage : {
|
6559
|
-
|
6560
|
-
|
6561
|
-
totalTokens:
|
6507
|
+
inputTokens: void 0,
|
6508
|
+
outputTokens: void 0,
|
6509
|
+
totalTokens: void 0
|
6562
6510
|
};
|
6563
6511
|
self.finishReasonPromise.resolve(finishReason);
|
6564
6512
|
self.usagePromise.resolve(usage);
|
@@ -6594,8 +6542,11 @@ var DefaultStreamTextResult = class {
|
|
6594
6542
|
return ((_a18 = lastStep.toolCalls) == null ? void 0 : _a18.length) ? JSON.stringify(lastStep.toolCalls) : void 0;
|
6595
6543
|
}
|
6596
6544
|
},
|
6597
|
-
"ai.usage.
|
6598
|
-
"ai.usage.
|
6545
|
+
"ai.usage.inputTokens": usage.inputTokens,
|
6546
|
+
"ai.usage.outputTokens": usage.outputTokens,
|
6547
|
+
"ai.usage.totalTokens": usage.totalTokens,
|
6548
|
+
"ai.usage.reasoningTokens": usage.reasoningTokens,
|
6549
|
+
"ai.usage.cachedInputTokens": usage.cachedInputTokens
|
6599
6550
|
}
|
6600
6551
|
})
|
6601
6552
|
);
|
@@ -6760,9 +6711,9 @@ var DefaultStreamTextResult = class {
|
|
6760
6711
|
let activeReasoningPart2 = void 0;
|
6761
6712
|
let stepFinishReason = "unknown";
|
6762
6713
|
let stepUsage = {
|
6763
|
-
|
6764
|
-
|
6765
|
-
totalTokens: 0
|
6714
|
+
inputTokens: void 0,
|
6715
|
+
outputTokens: void 0,
|
6716
|
+
totalTokens: void 0
|
6766
6717
|
};
|
6767
6718
|
let stepProviderMetadata;
|
6768
6719
|
let stepFirstChunk = true;
|
@@ -6791,7 +6742,7 @@ var DefaultStreamTextResult = class {
|
|
6791
6742
|
transformedStream.pipeThrough(
|
6792
6743
|
new TransformStream({
|
6793
6744
|
async transform(chunk, controller) {
|
6794
|
-
var _a17, _b, _c;
|
6745
|
+
var _a17, _b, _c, _d;
|
6795
6746
|
if (chunk.type === "stream-start") {
|
6796
6747
|
warnings = chunk.warnings;
|
6797
6748
|
return;
|
@@ -6887,7 +6838,7 @@ var DefaultStreamTextResult = class {
|
|
6887
6838
|
doStreamSpan.addEvent("ai.stream.finish");
|
6888
6839
|
doStreamSpan.setAttributes({
|
6889
6840
|
"ai.response.msToFinish": msToFinish,
|
6890
|
-
"ai.response.
|
6841
|
+
"ai.response.avgOutputTokensPerSecond": 1e3 * ((_d = stepUsage.outputTokens) != null ? _d : 0) / msToFinish
|
6891
6842
|
});
|
6892
6843
|
break;
|
6893
6844
|
}
|
@@ -6950,14 +6901,17 @@ var DefaultStreamTextResult = class {
|
|
6950
6901
|
"ai.response.id": stepResponse.id,
|
6951
6902
|
"ai.response.model": stepResponse.modelId,
|
6952
6903
|
"ai.response.timestamp": stepResponse.timestamp.toISOString(),
|
6953
|
-
"ai.usage.
|
6954
|
-
"ai.usage.
|
6904
|
+
"ai.usage.inputTokens": stepUsage.inputTokens,
|
6905
|
+
"ai.usage.outputTokens": stepUsage.outputTokens,
|
6906
|
+
"ai.usage.totalTokens": stepUsage.totalTokens,
|
6907
|
+
"ai.usage.reasoningTokens": stepUsage.reasoningTokens,
|
6908
|
+
"ai.usage.cachedInputTokens": stepUsage.cachedInputTokens,
|
6955
6909
|
// standardized gen-ai llm span attributes:
|
6956
6910
|
"gen_ai.response.finish_reasons": [stepFinishReason],
|
6957
6911
|
"gen_ai.response.id": stepResponse.id,
|
6958
6912
|
"gen_ai.response.model": stepResponse.modelId,
|
6959
|
-
"gen_ai.usage.input_tokens": stepUsage.
|
6960
|
-
"gen_ai.usage.output_tokens": stepUsage.
|
6913
|
+
"gen_ai.usage.input_tokens": stepUsage.inputTokens,
|
6914
|
+
"gen_ai.usage.output_tokens": stepUsage.outputTokens
|
6961
6915
|
}
|
6962
6916
|
})
|
6963
6917
|
);
|
@@ -7039,9 +6993,9 @@ var DefaultStreamTextResult = class {
|
|
7039
6993
|
currentStep: 0,
|
7040
6994
|
responseMessages: [],
|
7041
6995
|
usage: {
|
7042
|
-
|
7043
|
-
|
7044
|
-
totalTokens: 0
|
6996
|
+
inputTokens: void 0,
|
6997
|
+
outputTokens: void 0,
|
6998
|
+
totalTokens: void 0
|
7045
6999
|
},
|
7046
7000
|
previousStepText: "",
|
7047
7001
|
stepType: "initial",
|
@@ -7202,8 +7156,8 @@ var DefaultStreamTextResult = class {
|
|
7202
7156
|
controller.enqueue(
|
7203
7157
|
// TODO update protocol to v2 or replace with event stream
|
7204
7158
|
formatDataStreamPart("file", {
|
7205
|
-
|
7206
|
-
|
7159
|
+
mediaType: chunk.file.mediaType,
|
7160
|
+
url: `data:${chunk.file.mediaType};base64,${chunk.file.base64}`
|
7207
7161
|
})
|
7208
7162
|
);
|
7209
7163
|
break;
|
@@ -7270,8 +7224,11 @@ var DefaultStreamTextResult = class {
|
|
7270
7224
|
formatDataStreamPart("finish_step", {
|
7271
7225
|
finishReason: chunk.finishReason,
|
7272
7226
|
usage: sendUsage ? {
|
7273
|
-
|
7274
|
-
|
7227
|
+
inputTokens: chunk.usage.inputTokens,
|
7228
|
+
outputTokens: chunk.usage.outputTokens,
|
7229
|
+
totalTokens: chunk.usage.totalTokens,
|
7230
|
+
reasoningTokens: chunk.usage.reasoningTokens,
|
7231
|
+
cachedInputTokens: chunk.usage.cachedInputTokens
|
7275
7232
|
} : void 0,
|
7276
7233
|
isContinued: chunk.isContinued
|
7277
7234
|
})
|
@@ -7284,8 +7241,11 @@ var DefaultStreamTextResult = class {
|
|
7284
7241
|
formatDataStreamPart("finish_message", {
|
7285
7242
|
finishReason: chunk.finishReason,
|
7286
7243
|
usage: sendUsage ? {
|
7287
|
-
|
7288
|
-
|
7244
|
+
inputTokens: chunk.usage.inputTokens,
|
7245
|
+
outputTokens: chunk.usage.outputTokens,
|
7246
|
+
totalTokens: chunk.usage.totalTokens,
|
7247
|
+
reasoningTokens: chunk.usage.reasoningTokens,
|
7248
|
+
cachedInputTokens: chunk.usage.cachedInputTokens
|
7289
7249
|
} : void 0
|
7290
7250
|
})
|
7291
7251
|
);
|
@@ -7425,16 +7385,16 @@ var DefaultGeneratedAudioFile = class extends DefaultGeneratedFile {
|
|
7425
7385
|
super({ data, mediaType });
|
7426
7386
|
let format = "mp3";
|
7427
7387
|
if (mediaType) {
|
7428
|
-
const
|
7429
|
-
if (
|
7388
|
+
const mediaTypeParts = mediaType.split("/");
|
7389
|
+
if (mediaTypeParts.length === 2) {
|
7430
7390
|
if (mediaType !== "audio/mpeg") {
|
7431
|
-
format =
|
7391
|
+
format = mediaTypeParts[1];
|
7432
7392
|
}
|
7433
7393
|
}
|
7434
7394
|
}
|
7435
7395
|
if (!format) {
|
7436
7396
|
throw new Error(
|
7437
|
-
"Audio format must be provided or determinable from
|
7397
|
+
"Audio format must be provided or determinable from media type"
|
7438
7398
|
);
|
7439
7399
|
}
|
7440
7400
|
this.format = format;
|
@@ -7863,7 +7823,7 @@ function appendResponseMessages({
|
|
7863
7823
|
responseMessages,
|
7864
7824
|
_internal: { currentDate = () => /* @__PURE__ */ new Date() } = {}
|
7865
7825
|
}) {
|
7866
|
-
var _a17, _b
|
7826
|
+
var _a17, _b;
|
7867
7827
|
const clonedMessages = structuredClone(messages);
|
7868
7828
|
for (const message of responseMessages) {
|
7869
7829
|
const role = message.role;
|
@@ -7927,8 +7887,8 @@ function appendResponseMessages({
|
|
7927
7887
|
}
|
7928
7888
|
parts.push({
|
7929
7889
|
type: "file",
|
7930
|
-
mediaType:
|
7931
|
-
|
7890
|
+
mediaType: part.mediaType,
|
7891
|
+
url: `data:${part.mediaType};base64,${convertDataContentToBase64String(part.data)}`
|
7932
7892
|
});
|
7933
7893
|
break;
|
7934
7894
|
}
|
@@ -7937,9 +7897,8 @@ function appendResponseMessages({
|
|
7937
7897
|
if (isLastMessageAssistant) {
|
7938
7898
|
const maxStep = extractMaxToolInvocationStep(
|
7939
7899
|
getToolInvocations(lastMessage)
|
7940
|
-
// TODO remove once Message is removed
|
7941
7900
|
);
|
7942
|
-
(
|
7901
|
+
(_a17 = lastMessage.parts) != null ? _a17 : lastMessage.parts = [];
|
7943
7902
|
lastMessage.content = textContent;
|
7944
7903
|
lastMessage.parts.push(...parts);
|
7945
7904
|
getToolInvocationsForStep2(maxStep === void 0 ? 0 : maxStep + 1).map((call) => ({
|
@@ -7972,12 +7931,11 @@ function appendResponseMessages({
|
|
7972
7931
|
`Tool result must follow an assistant message: ${lastMessage.role}`
|
7973
7932
|
);
|
7974
7933
|
}
|
7975
|
-
(
|
7934
|
+
(_b = lastMessage.parts) != null ? _b : lastMessage.parts = [];
|
7976
7935
|
for (const contentPart of message.content) {
|
7977
|
-
const toolCall = getToolInvocations(
|
7978
|
-
|
7979
|
-
|
7980
|
-
).find((call) => call.toolCallId === contentPart.toolCallId);
|
7936
|
+
const toolCall = getToolInvocations(lastMessage).find(
|
7937
|
+
(call) => call.toolCallId === contentPart.toolCallId
|
7938
|
+
);
|
7981
7939
|
const toolCallPart = lastMessage.parts.find(
|
7982
7940
|
(part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === contentPart.toolCallId
|
7983
7941
|
);
|
@@ -8866,9 +8824,12 @@ var StreamData = class {
|
|
8866
8824
|
appendClientMessage,
|
8867
8825
|
appendResponseMessages,
|
8868
8826
|
asSchema,
|
8827
|
+
assistantModelMessageSchema,
|
8869
8828
|
callChatApi,
|
8870
8829
|
callCompletionApi,
|
8830
|
+
convertFileListToFileUIParts,
|
8871
8831
|
convertToCoreMessages,
|
8832
|
+
convertToModelMessages,
|
8872
8833
|
coreAssistantMessageSchema,
|
8873
8834
|
coreMessageSchema,
|
8874
8835
|
coreSystemMessageSchema,
|
@@ -8900,10 +8861,10 @@ var StreamData = class {
|
|
8900
8861
|
isAssistantMessageWithCompletedToolCalls,
|
8901
8862
|
isDeepEqualData,
|
8902
8863
|
jsonSchema,
|
8864
|
+
modelMessageSchema,
|
8903
8865
|
parseDataStreamPart,
|
8904
8866
|
parsePartialJson,
|
8905
8867
|
pipeDataStreamToResponse,
|
8906
|
-
prepareAttachmentsForRequest,
|
8907
8868
|
processDataStream,
|
8908
8869
|
processTextStream,
|
8909
8870
|
shouldResubmitMessages,
|
@@ -8912,8 +8873,11 @@ var StreamData = class {
|
|
8912
8873
|
smoothStream,
|
8913
8874
|
streamObject,
|
8914
8875
|
streamText,
|
8876
|
+
systemModelMessageSchema,
|
8915
8877
|
tool,
|
8878
|
+
toolModelMessageSchema,
|
8916
8879
|
updateToolCallResult,
|
8880
|
+
userModelMessageSchema,
|
8917
8881
|
wrapLanguageModel
|
8918
8882
|
});
|
8919
8883
|
//# sourceMappingURL=index.js.map
|