@openrouter/ai-sdk-provider 1.1.2 → 1.2.1
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/README.md +15 -1
- package/dist/index.d.mts +36 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +287 -147
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +287 -147
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +36 -0
- package/dist/internal/index.d.ts +36 -0
- package/dist/internal/index.js +255 -140
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +255 -140
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -919,19 +919,40 @@ function withoutTrailingSlash(url) {
|
|
|
919
919
|
|
|
920
920
|
// src/schemas/reasoning-details.ts
|
|
921
921
|
var import_v4 = require("zod/v4");
|
|
922
|
+
|
|
923
|
+
// src/utils/type-guards.ts
|
|
924
|
+
function isDefinedOrNotNull(value) {
|
|
925
|
+
return value !== null && value !== void 0;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// src/schemas/format.ts
|
|
929
|
+
var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
|
|
930
|
+
ReasoningFormat2["Unknown"] = "unknown";
|
|
931
|
+
ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
|
|
932
|
+
ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
|
|
933
|
+
ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
|
|
934
|
+
return ReasoningFormat2;
|
|
935
|
+
})(ReasoningFormat || {});
|
|
936
|
+
|
|
937
|
+
// src/schemas/reasoning-details.ts
|
|
938
|
+
var CommonReasoningDetailSchema = import_v4.z.object({
|
|
939
|
+
id: import_v4.z.string().nullish(),
|
|
940
|
+
format: import_v4.z.nativeEnum(ReasoningFormat).nullish(),
|
|
941
|
+
index: import_v4.z.number().optional()
|
|
942
|
+
});
|
|
922
943
|
var ReasoningDetailSummarySchema = import_v4.z.object({
|
|
923
944
|
type: import_v4.z.literal("reasoning.summary" /* Summary */),
|
|
924
945
|
summary: import_v4.z.string()
|
|
925
|
-
});
|
|
946
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
926
947
|
var ReasoningDetailEncryptedSchema = import_v4.z.object({
|
|
927
948
|
type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
|
|
928
949
|
data: import_v4.z.string()
|
|
929
|
-
});
|
|
950
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
930
951
|
var ReasoningDetailTextSchema = import_v4.z.object({
|
|
931
952
|
type: import_v4.z.literal("reasoning.text" /* Text */),
|
|
932
953
|
text: import_v4.z.string().nullish(),
|
|
933
954
|
signature: import_v4.z.string().nullish()
|
|
934
|
-
});
|
|
955
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
935
956
|
var ReasoningDetailUnionSchema = import_v4.z.union([
|
|
936
957
|
ReasoningDetailSummarySchema,
|
|
937
958
|
ReasoningDetailEncryptedSchema,
|
|
@@ -942,6 +963,22 @@ var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
|
|
|
942
963
|
import_v4.z.unknown().transform(() => null)
|
|
943
964
|
]);
|
|
944
965
|
var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
966
|
+
var OutputUnionToReasoningDetailsSchema = import_v4.z.union([
|
|
967
|
+
import_v4.z.object({
|
|
968
|
+
delta: import_v4.z.object({
|
|
969
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
970
|
+
})
|
|
971
|
+
}).transform((data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)),
|
|
972
|
+
import_v4.z.object({
|
|
973
|
+
message: import_v4.z.object({
|
|
974
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
975
|
+
})
|
|
976
|
+
}).transform((data) => data.message.reasoning_details.filter(isDefinedOrNotNull)),
|
|
977
|
+
import_v4.z.object({
|
|
978
|
+
text: import_v4.z.string(),
|
|
979
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
980
|
+
}).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
|
|
981
|
+
]);
|
|
945
982
|
|
|
946
983
|
// src/schemas/error-response.ts
|
|
947
984
|
var import_v42 = require("zod/v4");
|
|
@@ -958,6 +995,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
958
995
|
errorToMessage: (data) => data.error.message
|
|
959
996
|
});
|
|
960
997
|
|
|
998
|
+
// src/schemas/provider-metadata.ts
|
|
999
|
+
var import_v43 = require("zod/v4");
|
|
1000
|
+
var OpenRouterProviderMetadataSchema = import_v43.z.object({
|
|
1001
|
+
provider: import_v43.z.string(),
|
|
1002
|
+
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
|
|
1003
|
+
usage: import_v43.z.object({
|
|
1004
|
+
promptTokens: import_v43.z.number(),
|
|
1005
|
+
promptTokensDetails: import_v43.z.object({
|
|
1006
|
+
cachedTokens: import_v43.z.number()
|
|
1007
|
+
}).optional(),
|
|
1008
|
+
completionTokens: import_v43.z.number(),
|
|
1009
|
+
completionTokensDetails: import_v43.z.object({
|
|
1010
|
+
reasoningTokens: import_v43.z.number()
|
|
1011
|
+
}).optional(),
|
|
1012
|
+
totalTokens: import_v43.z.number(),
|
|
1013
|
+
cost: import_v43.z.number().optional(),
|
|
1014
|
+
costDetails: import_v43.z.object({
|
|
1015
|
+
upstreamInferenceCost: import_v43.z.number()
|
|
1016
|
+
})
|
|
1017
|
+
})
|
|
1018
|
+
});
|
|
1019
|
+
var OpenRouterProviderOptionsSchema = import_v43.z.object({
|
|
1020
|
+
openrouter: import_v43.z.object({
|
|
1021
|
+
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional()
|
|
1022
|
+
}).optional()
|
|
1023
|
+
}).optional();
|
|
1024
|
+
|
|
961
1025
|
// src/utils/map-finish-reason.ts
|
|
962
1026
|
function mapOpenRouterFinishReason(finishReason) {
|
|
963
1027
|
switch (finishReason) {
|
|
@@ -1007,6 +1071,15 @@ function getFileUrl({
|
|
|
1007
1071
|
}
|
|
1008
1072
|
return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
|
|
1009
1073
|
}
|
|
1074
|
+
function getMediaType(dataUrl, defaultMediaType) {
|
|
1075
|
+
var _a15;
|
|
1076
|
+
const match = dataUrl.match(/^data:([^;]+)/);
|
|
1077
|
+
return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
|
|
1078
|
+
}
|
|
1079
|
+
function getBase64FromDataUrl(dataUrl) {
|
|
1080
|
+
const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
|
|
1081
|
+
return match ? match[1] : dataUrl;
|
|
1082
|
+
}
|
|
1010
1083
|
|
|
1011
1084
|
// src/chat/convert-to-openrouter-chat-messages.ts
|
|
1012
1085
|
function getCacheControl(providerMetadata) {
|
|
@@ -1016,7 +1089,7 @@ function getCacheControl(providerMetadata) {
|
|
|
1016
1089
|
return (_c = (_b = (_a15 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a15 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
1017
1090
|
}
|
|
1018
1091
|
function convertToOpenRouterChatMessages(prompt) {
|
|
1019
|
-
var _a15, _b, _c;
|
|
1092
|
+
var _a15, _b, _c, _d, _e;
|
|
1020
1093
|
const messages = [];
|
|
1021
1094
|
for (const { role, content, providerOptions } of prompt) {
|
|
1022
1095
|
switch (role) {
|
|
@@ -1047,7 +1120,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1047
1120
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1048
1121
|
const contentParts = content.map(
|
|
1049
1122
|
(part) => {
|
|
1050
|
-
var _a16, _b2, _c2,
|
|
1123
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
1051
1124
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
1052
1125
|
switch (part.type) {
|
|
1053
1126
|
case "text":
|
|
@@ -1073,7 +1146,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1073
1146
|
};
|
|
1074
1147
|
}
|
|
1075
1148
|
const fileName = String(
|
|
1076
|
-
(_f = (
|
|
1149
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1077
1150
|
);
|
|
1078
1151
|
const fileData = getFileUrl({
|
|
1079
1152
|
part,
|
|
@@ -1119,7 +1192,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1119
1192
|
case "assistant": {
|
|
1120
1193
|
let text = "";
|
|
1121
1194
|
let reasoning = "";
|
|
1122
|
-
const reasoningDetails = [];
|
|
1123
1195
|
const toolCalls = [];
|
|
1124
1196
|
for (const part of content) {
|
|
1125
1197
|
switch (part.type) {
|
|
@@ -1140,10 +1212,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1140
1212
|
}
|
|
1141
1213
|
case "reasoning": {
|
|
1142
1214
|
reasoning += part.text;
|
|
1143
|
-
reasoningDetails.push({
|
|
1144
|
-
type: "reasoning.text" /* Text */,
|
|
1145
|
-
text: part.text
|
|
1146
|
-
});
|
|
1147
1215
|
break;
|
|
1148
1216
|
}
|
|
1149
1217
|
case "file":
|
|
@@ -1153,12 +1221,17 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1153
1221
|
}
|
|
1154
1222
|
}
|
|
1155
1223
|
}
|
|
1224
|
+
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(
|
|
1225
|
+
providerOptions
|
|
1226
|
+
);
|
|
1227
|
+
const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
|
|
1156
1228
|
messages.push({
|
|
1157
1229
|
role: "assistant",
|
|
1158
1230
|
content: text,
|
|
1159
1231
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1160
1232
|
reasoning: reasoning || void 0,
|
|
1161
|
-
reasoning_details
|
|
1233
|
+
// Only include reasoning_details if we have the preserved original version
|
|
1234
|
+
reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
|
|
1162
1235
|
cache_control: getCacheControl(providerOptions)
|
|
1163
1236
|
});
|
|
1164
1237
|
break;
|
|
@@ -1170,7 +1243,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1170
1243
|
role: "tool",
|
|
1171
1244
|
tool_call_id: toolResponse.toolCallId,
|
|
1172
1245
|
content: content2,
|
|
1173
|
-
cache_control: (
|
|
1246
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1174
1247
|
});
|
|
1175
1248
|
}
|
|
1176
1249
|
break;
|
|
@@ -1187,15 +1260,15 @@ function getToolResultContent(input) {
|
|
|
1187
1260
|
}
|
|
1188
1261
|
|
|
1189
1262
|
// src/chat/get-tool-choice.ts
|
|
1190
|
-
var
|
|
1191
|
-
var ChatCompletionToolChoiceSchema =
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
type:
|
|
1197
|
-
function:
|
|
1198
|
-
name:
|
|
1263
|
+
var import_v44 = require("zod/v4");
|
|
1264
|
+
var ChatCompletionToolChoiceSchema = import_v44.z.union([
|
|
1265
|
+
import_v44.z.literal("auto"),
|
|
1266
|
+
import_v44.z.literal("none"),
|
|
1267
|
+
import_v44.z.literal("required"),
|
|
1268
|
+
import_v44.z.object({
|
|
1269
|
+
type: import_v44.z.literal("function"),
|
|
1270
|
+
function: import_v44.z.object({
|
|
1271
|
+
name: import_v44.z.string()
|
|
1199
1272
|
})
|
|
1200
1273
|
})
|
|
1201
1274
|
]);
|
|
@@ -1219,126 +1292,144 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1219
1292
|
}
|
|
1220
1293
|
|
|
1221
1294
|
// src/chat/schemas.ts
|
|
1222
|
-
var
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1295
|
+
var import_v46 = require("zod/v4");
|
|
1296
|
+
|
|
1297
|
+
// src/schemas/image.ts
|
|
1298
|
+
var import_v45 = require("zod/v4");
|
|
1299
|
+
var ImageResponseSchema = import_v45.z.object({
|
|
1300
|
+
type: import_v45.z.literal("image_url"),
|
|
1301
|
+
image_url: import_v45.z.object({
|
|
1302
|
+
url: import_v45.z.string()
|
|
1303
|
+
})
|
|
1304
|
+
});
|
|
1305
|
+
var ImageResponseWithUnknownSchema = import_v45.z.union([
|
|
1306
|
+
ImageResponseSchema,
|
|
1307
|
+
import_v45.z.unknown().transform(() => null)
|
|
1308
|
+
]);
|
|
1309
|
+
var ImageResponseArraySchema = import_v45.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1310
|
+
|
|
1311
|
+
// src/chat/schemas.ts
|
|
1312
|
+
var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
|
|
1313
|
+
id: import_v46.z.string().optional(),
|
|
1314
|
+
model: import_v46.z.string().optional(),
|
|
1315
|
+
provider: import_v46.z.string().optional(),
|
|
1316
|
+
usage: import_v46.z.object({
|
|
1317
|
+
prompt_tokens: import_v46.z.number(),
|
|
1318
|
+
prompt_tokens_details: import_v46.z.object({
|
|
1319
|
+
cached_tokens: import_v46.z.number()
|
|
1231
1320
|
}).nullish(),
|
|
1232
|
-
completion_tokens:
|
|
1233
|
-
completion_tokens_details:
|
|
1234
|
-
reasoning_tokens:
|
|
1321
|
+
completion_tokens: import_v46.z.number(),
|
|
1322
|
+
completion_tokens_details: import_v46.z.object({
|
|
1323
|
+
reasoning_tokens: import_v46.z.number()
|
|
1235
1324
|
}).nullish(),
|
|
1236
|
-
total_tokens:
|
|
1237
|
-
cost:
|
|
1238
|
-
cost_details:
|
|
1239
|
-
upstream_inference_cost:
|
|
1325
|
+
total_tokens: import_v46.z.number(),
|
|
1326
|
+
cost: import_v46.z.number().optional(),
|
|
1327
|
+
cost_details: import_v46.z.object({
|
|
1328
|
+
upstream_inference_cost: import_v46.z.number().nullish()
|
|
1240
1329
|
}).nullish()
|
|
1241
1330
|
}).nullish()
|
|
1242
1331
|
});
|
|
1243
1332
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1244
|
-
choices:
|
|
1245
|
-
|
|
1246
|
-
message:
|
|
1247
|
-
role:
|
|
1248
|
-
content:
|
|
1249
|
-
reasoning:
|
|
1333
|
+
choices: import_v46.z.array(
|
|
1334
|
+
import_v46.z.object({
|
|
1335
|
+
message: import_v46.z.object({
|
|
1336
|
+
role: import_v46.z.literal("assistant"),
|
|
1337
|
+
content: import_v46.z.string().nullable().optional(),
|
|
1338
|
+
reasoning: import_v46.z.string().nullable().optional(),
|
|
1250
1339
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1340
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1341
|
+
tool_calls: import_v46.z.array(
|
|
1342
|
+
import_v46.z.object({
|
|
1343
|
+
id: import_v46.z.string().optional().nullable(),
|
|
1344
|
+
type: import_v46.z.literal("function"),
|
|
1345
|
+
function: import_v46.z.object({
|
|
1346
|
+
name: import_v46.z.string(),
|
|
1347
|
+
arguments: import_v46.z.string()
|
|
1258
1348
|
})
|
|
1259
1349
|
})
|
|
1260
1350
|
).optional(),
|
|
1261
|
-
annotations:
|
|
1262
|
-
|
|
1263
|
-
type:
|
|
1264
|
-
url_citation:
|
|
1265
|
-
end_index:
|
|
1266
|
-
start_index:
|
|
1267
|
-
title:
|
|
1268
|
-
url:
|
|
1269
|
-
content:
|
|
1351
|
+
annotations: import_v46.z.array(
|
|
1352
|
+
import_v46.z.object({
|
|
1353
|
+
type: import_v46.z.enum(["url_citation"]),
|
|
1354
|
+
url_citation: import_v46.z.object({
|
|
1355
|
+
end_index: import_v46.z.number(),
|
|
1356
|
+
start_index: import_v46.z.number(),
|
|
1357
|
+
title: import_v46.z.string(),
|
|
1358
|
+
url: import_v46.z.string(),
|
|
1359
|
+
content: import_v46.z.string().optional()
|
|
1270
1360
|
})
|
|
1271
1361
|
})
|
|
1272
1362
|
).nullish()
|
|
1273
1363
|
}),
|
|
1274
|
-
index:
|
|
1275
|
-
logprobs:
|
|
1276
|
-
content:
|
|
1277
|
-
|
|
1278
|
-
token:
|
|
1279
|
-
logprob:
|
|
1280
|
-
top_logprobs:
|
|
1281
|
-
|
|
1282
|
-
token:
|
|
1283
|
-
logprob:
|
|
1364
|
+
index: import_v46.z.number().nullish(),
|
|
1365
|
+
logprobs: import_v46.z.object({
|
|
1366
|
+
content: import_v46.z.array(
|
|
1367
|
+
import_v46.z.object({
|
|
1368
|
+
token: import_v46.z.string(),
|
|
1369
|
+
logprob: import_v46.z.number(),
|
|
1370
|
+
top_logprobs: import_v46.z.array(
|
|
1371
|
+
import_v46.z.object({
|
|
1372
|
+
token: import_v46.z.string(),
|
|
1373
|
+
logprob: import_v46.z.number()
|
|
1284
1374
|
})
|
|
1285
1375
|
)
|
|
1286
1376
|
})
|
|
1287
1377
|
).nullable()
|
|
1288
1378
|
}).nullable().optional(),
|
|
1289
|
-
finish_reason:
|
|
1379
|
+
finish_reason: import_v46.z.string().optional().nullable()
|
|
1290
1380
|
})
|
|
1291
1381
|
)
|
|
1292
1382
|
});
|
|
1293
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1383
|
+
var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
|
|
1294
1384
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1295
|
-
choices:
|
|
1296
|
-
|
|
1297
|
-
delta:
|
|
1298
|
-
role:
|
|
1299
|
-
content:
|
|
1300
|
-
reasoning:
|
|
1385
|
+
choices: import_v46.z.array(
|
|
1386
|
+
import_v46.z.object({
|
|
1387
|
+
delta: import_v46.z.object({
|
|
1388
|
+
role: import_v46.z.enum(["assistant"]).optional(),
|
|
1389
|
+
content: import_v46.z.string().nullish(),
|
|
1390
|
+
reasoning: import_v46.z.string().nullish().optional(),
|
|
1301
1391
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1392
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1393
|
+
tool_calls: import_v46.z.array(
|
|
1394
|
+
import_v46.z.object({
|
|
1395
|
+
index: import_v46.z.number().nullish(),
|
|
1396
|
+
id: import_v46.z.string().nullish(),
|
|
1397
|
+
type: import_v46.z.literal("function").optional(),
|
|
1398
|
+
function: import_v46.z.object({
|
|
1399
|
+
name: import_v46.z.string().nullish(),
|
|
1400
|
+
arguments: import_v46.z.string().nullish()
|
|
1310
1401
|
})
|
|
1311
1402
|
})
|
|
1312
1403
|
).nullish(),
|
|
1313
|
-
annotations:
|
|
1314
|
-
|
|
1315
|
-
type:
|
|
1316
|
-
url_citation:
|
|
1317
|
-
end_index:
|
|
1318
|
-
start_index:
|
|
1319
|
-
title:
|
|
1320
|
-
url:
|
|
1321
|
-
content:
|
|
1404
|
+
annotations: import_v46.z.array(
|
|
1405
|
+
import_v46.z.object({
|
|
1406
|
+
type: import_v46.z.enum(["url_citation"]),
|
|
1407
|
+
url_citation: import_v46.z.object({
|
|
1408
|
+
end_index: import_v46.z.number(),
|
|
1409
|
+
start_index: import_v46.z.number(),
|
|
1410
|
+
title: import_v46.z.string(),
|
|
1411
|
+
url: import_v46.z.string(),
|
|
1412
|
+
content: import_v46.z.string().optional()
|
|
1322
1413
|
})
|
|
1323
1414
|
})
|
|
1324
1415
|
).nullish()
|
|
1325
1416
|
}).nullish(),
|
|
1326
|
-
logprobs:
|
|
1327
|
-
content:
|
|
1328
|
-
|
|
1329
|
-
token:
|
|
1330
|
-
logprob:
|
|
1331
|
-
top_logprobs:
|
|
1332
|
-
|
|
1333
|
-
token:
|
|
1334
|
-
logprob:
|
|
1417
|
+
logprobs: import_v46.z.object({
|
|
1418
|
+
content: import_v46.z.array(
|
|
1419
|
+
import_v46.z.object({
|
|
1420
|
+
token: import_v46.z.string(),
|
|
1421
|
+
logprob: import_v46.z.number(),
|
|
1422
|
+
top_logprobs: import_v46.z.array(
|
|
1423
|
+
import_v46.z.object({
|
|
1424
|
+
token: import_v46.z.string(),
|
|
1425
|
+
logprob: import_v46.z.number()
|
|
1335
1426
|
})
|
|
1336
1427
|
)
|
|
1337
1428
|
})
|
|
1338
1429
|
).nullable()
|
|
1339
1430
|
}).nullish(),
|
|
1340
|
-
finish_reason:
|
|
1341
|
-
index:
|
|
1431
|
+
finish_reason: import_v46.z.string().nullable().optional(),
|
|
1432
|
+
index: import_v46.z.number().nullish()
|
|
1342
1433
|
})
|
|
1343
1434
|
)
|
|
1344
1435
|
}),
|
|
@@ -1441,7 +1532,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1441
1532
|
return baseArgs;
|
|
1442
1533
|
}
|
|
1443
1534
|
async doGenerate(options) {
|
|
1444
|
-
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
|
|
1535
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
|
|
1445
1536
|
const providerOptions = options.providerOptions || {};
|
|
1446
1537
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1447
1538
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1535,6 +1626,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1535
1626
|
});
|
|
1536
1627
|
}
|
|
1537
1628
|
}
|
|
1629
|
+
if (choice.message.images) {
|
|
1630
|
+
for (const image of choice.message.images) {
|
|
1631
|
+
content.push({
|
|
1632
|
+
type: "file",
|
|
1633
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1634
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1538
1638
|
if (choice.message.annotations) {
|
|
1539
1639
|
for (const annotation of choice.message.annotations) {
|
|
1540
1640
|
if (annotation.type === "url_citation") {
|
|
@@ -1559,24 +1659,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1559
1659
|
usage: usageInfo,
|
|
1560
1660
|
warnings: [],
|
|
1561
1661
|
providerMetadata: {
|
|
1562
|
-
openrouter: {
|
|
1662
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1563
1663
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1664
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1564
1665
|
usage: {
|
|
1565
|
-
promptTokens: (
|
|
1566
|
-
completionTokens: (
|
|
1567
|
-
totalTokens: (
|
|
1568
|
-
cost: (
|
|
1666
|
+
promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
|
|
1667
|
+
completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
|
|
1668
|
+
totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
|
|
1669
|
+
cost: (_p = response.usage) == null ? void 0 : _p.cost,
|
|
1569
1670
|
promptTokensDetails: {
|
|
1570
|
-
cachedTokens: (
|
|
1671
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1571
1672
|
},
|
|
1572
1673
|
completionTokensDetails: {
|
|
1573
|
-
reasoningTokens: (
|
|
1674
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1574
1675
|
},
|
|
1575
1676
|
costDetails: {
|
|
1576
|
-
upstreamInferenceCost: (
|
|
1677
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1577
1678
|
}
|
|
1578
1679
|
}
|
|
1579
|
-
}
|
|
1680
|
+
})
|
|
1580
1681
|
},
|
|
1581
1682
|
request: { body: args },
|
|
1582
1683
|
response: {
|
|
@@ -1621,6 +1722,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1621
1722
|
cachedInputTokens: Number.NaN
|
|
1622
1723
|
};
|
|
1623
1724
|
const openrouterUsage = {};
|
|
1725
|
+
const accumulatedReasoningDetails = [];
|
|
1624
1726
|
let textStarted = false;
|
|
1625
1727
|
let reasoningStarted = false;
|
|
1626
1728
|
let textId;
|
|
@@ -1706,6 +1808,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1706
1808
|
});
|
|
1707
1809
|
};
|
|
1708
1810
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1811
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1709
1812
|
for (const detail of delta.reasoning_details) {
|
|
1710
1813
|
switch (detail.type) {
|
|
1711
1814
|
case "reasoning.text" /* Text */: {
|
|
@@ -1868,6 +1971,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1868
1971
|
}
|
|
1869
1972
|
}
|
|
1870
1973
|
}
|
|
1974
|
+
if (delta.images != null) {
|
|
1975
|
+
for (const image of delta.images) {
|
|
1976
|
+
controller.enqueue({
|
|
1977
|
+
type: "file",
|
|
1978
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1979
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1980
|
+
});
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1871
1983
|
},
|
|
1872
1984
|
flush(controller) {
|
|
1873
1985
|
var _a16;
|
|
@@ -1903,6 +2015,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1903
2015
|
if (provider !== void 0) {
|
|
1904
2016
|
openrouterMetadata.provider = provider;
|
|
1905
2017
|
}
|
|
2018
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
2019
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
2020
|
+
}
|
|
1906
2021
|
controller.enqueue({
|
|
1907
2022
|
type: "finish",
|
|
1908
2023
|
finishReason,
|
|
@@ -2025,36 +2140,36 @@ ${assistantMessage}
|
|
|
2025
2140
|
}
|
|
2026
2141
|
|
|
2027
2142
|
// src/completion/schemas.ts
|
|
2028
|
-
var
|
|
2029
|
-
var OpenRouterCompletionChunkSchema =
|
|
2030
|
-
|
|
2031
|
-
id:
|
|
2032
|
-
model:
|
|
2033
|
-
choices:
|
|
2034
|
-
|
|
2035
|
-
text:
|
|
2036
|
-
reasoning:
|
|
2143
|
+
var import_v47 = require("zod/v4");
|
|
2144
|
+
var OpenRouterCompletionChunkSchema = import_v47.z.union([
|
|
2145
|
+
import_v47.z.object({
|
|
2146
|
+
id: import_v47.z.string().optional(),
|
|
2147
|
+
model: import_v47.z.string().optional(),
|
|
2148
|
+
choices: import_v47.z.array(
|
|
2149
|
+
import_v47.z.object({
|
|
2150
|
+
text: import_v47.z.string(),
|
|
2151
|
+
reasoning: import_v47.z.string().nullish().optional(),
|
|
2037
2152
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2038
|
-
finish_reason:
|
|
2039
|
-
index:
|
|
2040
|
-
logprobs:
|
|
2041
|
-
tokens:
|
|
2042
|
-
token_logprobs:
|
|
2043
|
-
top_logprobs:
|
|
2153
|
+
finish_reason: import_v47.z.string().nullish(),
|
|
2154
|
+
index: import_v47.z.number().nullish(),
|
|
2155
|
+
logprobs: import_v47.z.object({
|
|
2156
|
+
tokens: import_v47.z.array(import_v47.z.string()),
|
|
2157
|
+
token_logprobs: import_v47.z.array(import_v47.z.number()),
|
|
2158
|
+
top_logprobs: import_v47.z.array(import_v47.z.record(import_v47.z.string(), import_v47.z.number())).nullable()
|
|
2044
2159
|
}).nullable().optional()
|
|
2045
2160
|
})
|
|
2046
2161
|
),
|
|
2047
|
-
usage:
|
|
2048
|
-
prompt_tokens:
|
|
2049
|
-
prompt_tokens_details:
|
|
2050
|
-
cached_tokens:
|
|
2162
|
+
usage: import_v47.z.object({
|
|
2163
|
+
prompt_tokens: import_v47.z.number(),
|
|
2164
|
+
prompt_tokens_details: import_v47.z.object({
|
|
2165
|
+
cached_tokens: import_v47.z.number()
|
|
2051
2166
|
}).nullish(),
|
|
2052
|
-
completion_tokens:
|
|
2053
|
-
completion_tokens_details:
|
|
2054
|
-
reasoning_tokens:
|
|
2167
|
+
completion_tokens: import_v47.z.number(),
|
|
2168
|
+
completion_tokens_details: import_v47.z.object({
|
|
2169
|
+
reasoning_tokens: import_v47.z.number()
|
|
2055
2170
|
}).nullish(),
|
|
2056
|
-
total_tokens:
|
|
2057
|
-
cost:
|
|
2171
|
+
total_tokens: import_v47.z.number(),
|
|
2172
|
+
cost: import_v47.z.number().optional()
|
|
2058
2173
|
}).nullish()
|
|
2059
2174
|
}),
|
|
2060
2175
|
OpenRouterErrorResponseSchema
|
|
@@ -2323,18 +2438,43 @@ var OpenRouter = class {
|
|
|
2323
2438
|
}
|
|
2324
2439
|
};
|
|
2325
2440
|
|
|
2441
|
+
// src/version.ts
|
|
2442
|
+
var VERSION = false ? "0.0.0-test" : "1.2.1";
|
|
2443
|
+
|
|
2444
|
+
// src/utils/remove-undefined.ts
|
|
2445
|
+
function removeUndefinedEntries2(record) {
|
|
2446
|
+
return Object.fromEntries(
|
|
2447
|
+
Object.entries(record).filter(([, value]) => value !== null)
|
|
2448
|
+
);
|
|
2449
|
+
}
|
|
2450
|
+
|
|
2451
|
+
// src/utils/with-user-agent-suffix.ts
|
|
2452
|
+
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
2453
|
+
const cleanedHeaders = removeUndefinedEntries2(
|
|
2454
|
+
headers != null ? headers : {}
|
|
2455
|
+
);
|
|
2456
|
+
const currentUserAgentHeader = cleanedHeaders["user-agent"] || "";
|
|
2457
|
+
const newUserAgent = [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ");
|
|
2458
|
+
return __spreadProps(__spreadValues({}, cleanedHeaders), {
|
|
2459
|
+
"user-agent": newUserAgent
|
|
2460
|
+
});
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2326
2463
|
// src/provider.ts
|
|
2327
2464
|
function createOpenRouter(options = {}) {
|
|
2328
2465
|
var _a15, _b, _c;
|
|
2329
2466
|
const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
2330
2467
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2331
|
-
const getHeaders = () =>
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2468
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
2469
|
+
__spreadValues({
|
|
2470
|
+
Authorization: `Bearer ${loadApiKey({
|
|
2471
|
+
apiKey: options.apiKey,
|
|
2472
|
+
environmentVariableName: "OPENROUTER_API_KEY",
|
|
2473
|
+
description: "OpenRouter"
|
|
2474
|
+
})}`
|
|
2475
|
+
}, options.headers),
|
|
2476
|
+
`ai-sdk/openrouter/${VERSION}`
|
|
2477
|
+
);
|
|
2338
2478
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
2339
2479
|
provider: "openrouter.chat",
|
|
2340
2480
|
url: ({ path }) => `${baseURL}${path}`,
|