@openrouter/ai-sdk-provider 1.2.0 → 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/dist/index.d.mts +36 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +250 -155
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +250 -155
- 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 +218 -148
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +218 -148
- 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) {
|
|
@@ -1025,7 +1089,7 @@ function getCacheControl(providerMetadata) {
|
|
|
1025
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;
|
|
1026
1090
|
}
|
|
1027
1091
|
function convertToOpenRouterChatMessages(prompt) {
|
|
1028
|
-
var _a15, _b, _c;
|
|
1092
|
+
var _a15, _b, _c, _d, _e;
|
|
1029
1093
|
const messages = [];
|
|
1030
1094
|
for (const { role, content, providerOptions } of prompt) {
|
|
1031
1095
|
switch (role) {
|
|
@@ -1056,7 +1120,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1056
1120
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1057
1121
|
const contentParts = content.map(
|
|
1058
1122
|
(part) => {
|
|
1059
|
-
var _a16, _b2, _c2,
|
|
1123
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
1060
1124
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
1061
1125
|
switch (part.type) {
|
|
1062
1126
|
case "text":
|
|
@@ -1082,7 +1146,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1082
1146
|
};
|
|
1083
1147
|
}
|
|
1084
1148
|
const fileName = String(
|
|
1085
|
-
(_f = (
|
|
1149
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1086
1150
|
);
|
|
1087
1151
|
const fileData = getFileUrl({
|
|
1088
1152
|
part,
|
|
@@ -1128,7 +1192,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1128
1192
|
case "assistant": {
|
|
1129
1193
|
let text = "";
|
|
1130
1194
|
let reasoning = "";
|
|
1131
|
-
const reasoningDetails = [];
|
|
1132
1195
|
const toolCalls = [];
|
|
1133
1196
|
for (const part of content) {
|
|
1134
1197
|
switch (part.type) {
|
|
@@ -1149,10 +1212,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1149
1212
|
}
|
|
1150
1213
|
case "reasoning": {
|
|
1151
1214
|
reasoning += part.text;
|
|
1152
|
-
reasoningDetails.push({
|
|
1153
|
-
type: "reasoning.text" /* Text */,
|
|
1154
|
-
text: part.text
|
|
1155
|
-
});
|
|
1156
1215
|
break;
|
|
1157
1216
|
}
|
|
1158
1217
|
case "file":
|
|
@@ -1162,12 +1221,17 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1162
1221
|
}
|
|
1163
1222
|
}
|
|
1164
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;
|
|
1165
1228
|
messages.push({
|
|
1166
1229
|
role: "assistant",
|
|
1167
1230
|
content: text,
|
|
1168
1231
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1169
1232
|
reasoning: reasoning || void 0,
|
|
1170
|
-
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,
|
|
1171
1235
|
cache_control: getCacheControl(providerOptions)
|
|
1172
1236
|
});
|
|
1173
1237
|
break;
|
|
@@ -1179,7 +1243,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1179
1243
|
role: "tool",
|
|
1180
1244
|
tool_call_id: toolResponse.toolCallId,
|
|
1181
1245
|
content: content2,
|
|
1182
|
-
cache_control: (
|
|
1246
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1183
1247
|
});
|
|
1184
1248
|
}
|
|
1185
1249
|
break;
|
|
@@ -1196,15 +1260,15 @@ function getToolResultContent(input) {
|
|
|
1196
1260
|
}
|
|
1197
1261
|
|
|
1198
1262
|
// src/chat/get-tool-choice.ts
|
|
1199
|
-
var
|
|
1200
|
-
var ChatCompletionToolChoiceSchema =
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
type:
|
|
1206
|
-
function:
|
|
1207
|
-
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()
|
|
1208
1272
|
})
|
|
1209
1273
|
})
|
|
1210
1274
|
]);
|
|
@@ -1228,144 +1292,144 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1228
1292
|
}
|
|
1229
1293
|
|
|
1230
1294
|
// src/chat/schemas.ts
|
|
1231
|
-
var
|
|
1295
|
+
var import_v46 = require("zod/v4");
|
|
1232
1296
|
|
|
1233
1297
|
// src/schemas/image.ts
|
|
1234
|
-
var
|
|
1235
|
-
var ImageResponseSchema =
|
|
1236
|
-
type:
|
|
1237
|
-
image_url:
|
|
1238
|
-
url:
|
|
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()
|
|
1239
1303
|
})
|
|
1240
1304
|
});
|
|
1241
|
-
var ImageResponseWithUnknownSchema =
|
|
1305
|
+
var ImageResponseWithUnknownSchema = import_v45.z.union([
|
|
1242
1306
|
ImageResponseSchema,
|
|
1243
|
-
|
|
1307
|
+
import_v45.z.unknown().transform(() => null)
|
|
1244
1308
|
]);
|
|
1245
|
-
var ImageResponseArraySchema =
|
|
1309
|
+
var ImageResponseArraySchema = import_v45.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1246
1310
|
|
|
1247
1311
|
// src/chat/schemas.ts
|
|
1248
|
-
var OpenRouterChatCompletionBaseResponseSchema =
|
|
1249
|
-
id:
|
|
1250
|
-
model:
|
|
1251
|
-
provider:
|
|
1252
|
-
usage:
|
|
1253
|
-
prompt_tokens:
|
|
1254
|
-
prompt_tokens_details:
|
|
1255
|
-
cached_tokens:
|
|
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()
|
|
1256
1320
|
}).nullish(),
|
|
1257
|
-
completion_tokens:
|
|
1258
|
-
completion_tokens_details:
|
|
1259
|
-
reasoning_tokens:
|
|
1321
|
+
completion_tokens: import_v46.z.number(),
|
|
1322
|
+
completion_tokens_details: import_v46.z.object({
|
|
1323
|
+
reasoning_tokens: import_v46.z.number()
|
|
1260
1324
|
}).nullish(),
|
|
1261
|
-
total_tokens:
|
|
1262
|
-
cost:
|
|
1263
|
-
cost_details:
|
|
1264
|
-
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()
|
|
1265
1329
|
}).nullish()
|
|
1266
1330
|
}).nullish()
|
|
1267
1331
|
});
|
|
1268
1332
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1269
|
-
choices:
|
|
1270
|
-
|
|
1271
|
-
message:
|
|
1272
|
-
role:
|
|
1273
|
-
content:
|
|
1274
|
-
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(),
|
|
1275
1339
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1276
1340
|
images: ImageResponseArraySchema.nullish(),
|
|
1277
|
-
tool_calls:
|
|
1278
|
-
|
|
1279
|
-
id:
|
|
1280
|
-
type:
|
|
1281
|
-
function:
|
|
1282
|
-
name:
|
|
1283
|
-
arguments:
|
|
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()
|
|
1284
1348
|
})
|
|
1285
1349
|
})
|
|
1286
1350
|
).optional(),
|
|
1287
|
-
annotations:
|
|
1288
|
-
|
|
1289
|
-
type:
|
|
1290
|
-
url_citation:
|
|
1291
|
-
end_index:
|
|
1292
|
-
start_index:
|
|
1293
|
-
title:
|
|
1294
|
-
url:
|
|
1295
|
-
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()
|
|
1296
1360
|
})
|
|
1297
1361
|
})
|
|
1298
1362
|
).nullish()
|
|
1299
1363
|
}),
|
|
1300
|
-
index:
|
|
1301
|
-
logprobs:
|
|
1302
|
-
content:
|
|
1303
|
-
|
|
1304
|
-
token:
|
|
1305
|
-
logprob:
|
|
1306
|
-
top_logprobs:
|
|
1307
|
-
|
|
1308
|
-
token:
|
|
1309
|
-
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()
|
|
1310
1374
|
})
|
|
1311
1375
|
)
|
|
1312
1376
|
})
|
|
1313
1377
|
).nullable()
|
|
1314
1378
|
}).nullable().optional(),
|
|
1315
|
-
finish_reason:
|
|
1379
|
+
finish_reason: import_v46.z.string().optional().nullable()
|
|
1316
1380
|
})
|
|
1317
1381
|
)
|
|
1318
1382
|
});
|
|
1319
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1383
|
+
var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
|
|
1320
1384
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1321
|
-
choices:
|
|
1322
|
-
|
|
1323
|
-
delta:
|
|
1324
|
-
role:
|
|
1325
|
-
content:
|
|
1326
|
-
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(),
|
|
1327
1391
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1328
1392
|
images: ImageResponseArraySchema.nullish(),
|
|
1329
|
-
tool_calls:
|
|
1330
|
-
|
|
1331
|
-
index:
|
|
1332
|
-
id:
|
|
1333
|
-
type:
|
|
1334
|
-
function:
|
|
1335
|
-
name:
|
|
1336
|
-
arguments:
|
|
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()
|
|
1337
1401
|
})
|
|
1338
1402
|
})
|
|
1339
1403
|
).nullish(),
|
|
1340
|
-
annotations:
|
|
1341
|
-
|
|
1342
|
-
type:
|
|
1343
|
-
url_citation:
|
|
1344
|
-
end_index:
|
|
1345
|
-
start_index:
|
|
1346
|
-
title:
|
|
1347
|
-
url:
|
|
1348
|
-
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()
|
|
1349
1413
|
})
|
|
1350
1414
|
})
|
|
1351
1415
|
).nullish()
|
|
1352
1416
|
}).nullish(),
|
|
1353
|
-
logprobs:
|
|
1354
|
-
content:
|
|
1355
|
-
|
|
1356
|
-
token:
|
|
1357
|
-
logprob:
|
|
1358
|
-
top_logprobs:
|
|
1359
|
-
|
|
1360
|
-
token:
|
|
1361
|
-
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()
|
|
1362
1426
|
})
|
|
1363
1427
|
)
|
|
1364
1428
|
})
|
|
1365
1429
|
).nullable()
|
|
1366
1430
|
}).nullish(),
|
|
1367
|
-
finish_reason:
|
|
1368
|
-
index:
|
|
1431
|
+
finish_reason: import_v46.z.string().nullable().optional(),
|
|
1432
|
+
index: import_v46.z.number().nullish()
|
|
1369
1433
|
})
|
|
1370
1434
|
)
|
|
1371
1435
|
}),
|
|
@@ -1468,7 +1532,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1468
1532
|
return baseArgs;
|
|
1469
1533
|
}
|
|
1470
1534
|
async doGenerate(options) {
|
|
1471
|
-
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;
|
|
1472
1536
|
const providerOptions = options.providerOptions || {};
|
|
1473
1537
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1474
1538
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1595,24 +1659,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1595
1659
|
usage: usageInfo,
|
|
1596
1660
|
warnings: [],
|
|
1597
1661
|
providerMetadata: {
|
|
1598
|
-
openrouter: {
|
|
1662
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1599
1663
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1664
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1600
1665
|
usage: {
|
|
1601
|
-
promptTokens: (
|
|
1602
|
-
completionTokens: (
|
|
1603
|
-
totalTokens: (
|
|
1604
|
-
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,
|
|
1605
1670
|
promptTokensDetails: {
|
|
1606
|
-
cachedTokens: (
|
|
1671
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1607
1672
|
},
|
|
1608
1673
|
completionTokensDetails: {
|
|
1609
|
-
reasoningTokens: (
|
|
1674
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1610
1675
|
},
|
|
1611
1676
|
costDetails: {
|
|
1612
|
-
upstreamInferenceCost: (
|
|
1677
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1613
1678
|
}
|
|
1614
1679
|
}
|
|
1615
|
-
}
|
|
1680
|
+
})
|
|
1616
1681
|
},
|
|
1617
1682
|
request: { body: args },
|
|
1618
1683
|
response: {
|
|
@@ -1657,6 +1722,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1657
1722
|
cachedInputTokens: Number.NaN
|
|
1658
1723
|
};
|
|
1659
1724
|
const openrouterUsage = {};
|
|
1725
|
+
const accumulatedReasoningDetails = [];
|
|
1660
1726
|
let textStarted = false;
|
|
1661
1727
|
let reasoningStarted = false;
|
|
1662
1728
|
let textId;
|
|
@@ -1742,6 +1808,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1742
1808
|
});
|
|
1743
1809
|
};
|
|
1744
1810
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1811
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1745
1812
|
for (const detail of delta.reasoning_details) {
|
|
1746
1813
|
switch (detail.type) {
|
|
1747
1814
|
case "reasoning.text" /* Text */: {
|
|
@@ -1948,6 +2015,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1948
2015
|
if (provider !== void 0) {
|
|
1949
2016
|
openrouterMetadata.provider = provider;
|
|
1950
2017
|
}
|
|
2018
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
2019
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
2020
|
+
}
|
|
1951
2021
|
controller.enqueue({
|
|
1952
2022
|
type: "finish",
|
|
1953
2023
|
finishReason,
|
|
@@ -2070,36 +2140,36 @@ ${assistantMessage}
|
|
|
2070
2140
|
}
|
|
2071
2141
|
|
|
2072
2142
|
// src/completion/schemas.ts
|
|
2073
|
-
var
|
|
2074
|
-
var OpenRouterCompletionChunkSchema =
|
|
2075
|
-
|
|
2076
|
-
id:
|
|
2077
|
-
model:
|
|
2078
|
-
choices:
|
|
2079
|
-
|
|
2080
|
-
text:
|
|
2081
|
-
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(),
|
|
2082
2152
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2083
|
-
finish_reason:
|
|
2084
|
-
index:
|
|
2085
|
-
logprobs:
|
|
2086
|
-
tokens:
|
|
2087
|
-
token_logprobs:
|
|
2088
|
-
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()
|
|
2089
2159
|
}).nullable().optional()
|
|
2090
2160
|
})
|
|
2091
2161
|
),
|
|
2092
|
-
usage:
|
|
2093
|
-
prompt_tokens:
|
|
2094
|
-
prompt_tokens_details:
|
|
2095
|
-
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()
|
|
2096
2166
|
}).nullish(),
|
|
2097
|
-
completion_tokens:
|
|
2098
|
-
completion_tokens_details:
|
|
2099
|
-
reasoning_tokens:
|
|
2167
|
+
completion_tokens: import_v47.z.number(),
|
|
2168
|
+
completion_tokens_details: import_v47.z.object({
|
|
2169
|
+
reasoning_tokens: import_v47.z.number()
|
|
2100
2170
|
}).nullish(),
|
|
2101
|
-
total_tokens:
|
|
2102
|
-
cost:
|
|
2171
|
+
total_tokens: import_v47.z.number(),
|
|
2172
|
+
cost: import_v47.z.number().optional()
|
|
2103
2173
|
}).nullish()
|
|
2104
2174
|
}),
|
|
2105
2175
|
OpenRouterErrorResponseSchema
|
|
@@ -2368,18 +2438,43 @@ var OpenRouter = class {
|
|
|
2368
2438
|
}
|
|
2369
2439
|
};
|
|
2370
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
|
+
|
|
2371
2463
|
// src/provider.ts
|
|
2372
2464
|
function createOpenRouter(options = {}) {
|
|
2373
2465
|
var _a15, _b, _c;
|
|
2374
2466
|
const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
2375
2467
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2376
|
-
const getHeaders = () =>
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
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
|
+
);
|
|
2383
2478
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
2384
2479
|
provider: "openrouter.chat",
|
|
2385
2480
|
url: ({ path }) => `${baseURL}${path}`,
|