@openrouter/ai-sdk-provider 1.2.0 → 1.2.2
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 +53 -13
- package/dist/index.d.ts +53 -13
- package/dist/index.js +386 -179
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +386 -179
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +51 -11
- package/dist/internal/index.d.ts +51 -11
- package/dist/internal/index.js +354 -172
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +354 -172
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +12 -4
package/dist/index.js
CHANGED
|
@@ -253,6 +253,18 @@ var name9 = "AI_NoContentGeneratedError";
|
|
|
253
253
|
var marker10 = `vercel.ai.error.${name9}`;
|
|
254
254
|
var symbol10 = Symbol.for(marker10);
|
|
255
255
|
var _a10;
|
|
256
|
+
var NoContentGeneratedError = class extends AISDKError {
|
|
257
|
+
// used in isInstance
|
|
258
|
+
constructor({
|
|
259
|
+
message = "No content generated."
|
|
260
|
+
} = {}) {
|
|
261
|
+
super({ name: name9, message });
|
|
262
|
+
this[_a10] = true;
|
|
263
|
+
}
|
|
264
|
+
static isInstance(error) {
|
|
265
|
+
return AISDKError.hasMarker(error, marker10);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
256
268
|
_a10 = symbol10;
|
|
257
269
|
var name10 = "AI_NoSuchModelError";
|
|
258
270
|
var marker11 = `vercel.ai.error.${name10}`;
|
|
@@ -919,19 +931,40 @@ function withoutTrailingSlash(url) {
|
|
|
919
931
|
|
|
920
932
|
// src/schemas/reasoning-details.ts
|
|
921
933
|
var import_v4 = require("zod/v4");
|
|
934
|
+
|
|
935
|
+
// src/utils/type-guards.ts
|
|
936
|
+
function isDefinedOrNotNull(value) {
|
|
937
|
+
return value !== null && value !== void 0;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// src/schemas/format.ts
|
|
941
|
+
var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
|
|
942
|
+
ReasoningFormat2["Unknown"] = "unknown";
|
|
943
|
+
ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
|
|
944
|
+
ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
|
|
945
|
+
ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
|
|
946
|
+
return ReasoningFormat2;
|
|
947
|
+
})(ReasoningFormat || {});
|
|
948
|
+
|
|
949
|
+
// src/schemas/reasoning-details.ts
|
|
950
|
+
var CommonReasoningDetailSchema = import_v4.z.object({
|
|
951
|
+
id: import_v4.z.string().nullish(),
|
|
952
|
+
format: import_v4.z.nativeEnum(ReasoningFormat).nullish(),
|
|
953
|
+
index: import_v4.z.number().optional()
|
|
954
|
+
});
|
|
922
955
|
var ReasoningDetailSummarySchema = import_v4.z.object({
|
|
923
956
|
type: import_v4.z.literal("reasoning.summary" /* Summary */),
|
|
924
957
|
summary: import_v4.z.string()
|
|
925
|
-
});
|
|
958
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
926
959
|
var ReasoningDetailEncryptedSchema = import_v4.z.object({
|
|
927
960
|
type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
|
|
928
961
|
data: import_v4.z.string()
|
|
929
|
-
});
|
|
962
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
930
963
|
var ReasoningDetailTextSchema = import_v4.z.object({
|
|
931
964
|
type: import_v4.z.literal("reasoning.text" /* Text */),
|
|
932
965
|
text: import_v4.z.string().nullish(),
|
|
933
966
|
signature: import_v4.z.string().nullish()
|
|
934
|
-
});
|
|
967
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
935
968
|
var ReasoningDetailUnionSchema = import_v4.z.union([
|
|
936
969
|
ReasoningDetailSummarySchema,
|
|
937
970
|
ReasoningDetailEncryptedSchema,
|
|
@@ -942,6 +975,26 @@ var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
|
|
|
942
975
|
import_v4.z.unknown().transform(() => null)
|
|
943
976
|
]);
|
|
944
977
|
var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
978
|
+
var OutputUnionToReasoningDetailsSchema = import_v4.z.union([
|
|
979
|
+
import_v4.z.object({
|
|
980
|
+
delta: import_v4.z.object({
|
|
981
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
982
|
+
})
|
|
983
|
+
}).transform(
|
|
984
|
+
(data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)
|
|
985
|
+
),
|
|
986
|
+
import_v4.z.object({
|
|
987
|
+
message: import_v4.z.object({
|
|
988
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
989
|
+
})
|
|
990
|
+
}).transform(
|
|
991
|
+
(data) => data.message.reasoning_details.filter(isDefinedOrNotNull)
|
|
992
|
+
),
|
|
993
|
+
import_v4.z.object({
|
|
994
|
+
text: import_v4.z.string(),
|
|
995
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
996
|
+
}).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
|
|
997
|
+
]);
|
|
945
998
|
|
|
946
999
|
// src/schemas/error-response.ts
|
|
947
1000
|
var import_v42 = require("zod/v4");
|
|
@@ -958,6 +1011,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
958
1011
|
errorToMessage: (data) => data.error.message
|
|
959
1012
|
});
|
|
960
1013
|
|
|
1014
|
+
// src/schemas/provider-metadata.ts
|
|
1015
|
+
var import_v43 = require("zod/v4");
|
|
1016
|
+
var OpenRouterProviderMetadataSchema = import_v43.z.object({
|
|
1017
|
+
provider: import_v43.z.string(),
|
|
1018
|
+
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
|
|
1019
|
+
usage: import_v43.z.object({
|
|
1020
|
+
promptTokens: import_v43.z.number(),
|
|
1021
|
+
promptTokensDetails: import_v43.z.object({
|
|
1022
|
+
cachedTokens: import_v43.z.number()
|
|
1023
|
+
}).optional(),
|
|
1024
|
+
completionTokens: import_v43.z.number(),
|
|
1025
|
+
completionTokensDetails: import_v43.z.object({
|
|
1026
|
+
reasoningTokens: import_v43.z.number()
|
|
1027
|
+
}).optional(),
|
|
1028
|
+
totalTokens: import_v43.z.number(),
|
|
1029
|
+
cost: import_v43.z.number().optional(),
|
|
1030
|
+
costDetails: import_v43.z.object({
|
|
1031
|
+
upstreamInferenceCost: import_v43.z.number()
|
|
1032
|
+
})
|
|
1033
|
+
})
|
|
1034
|
+
});
|
|
1035
|
+
var OpenRouterProviderOptionsSchema = import_v43.z.object({
|
|
1036
|
+
openrouter: import_v43.z.object({
|
|
1037
|
+
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional()
|
|
1038
|
+
}).optional()
|
|
1039
|
+
}).optional();
|
|
1040
|
+
|
|
961
1041
|
// src/utils/map-finish-reason.ts
|
|
962
1042
|
function mapOpenRouterFinishReason(finishReason) {
|
|
963
1043
|
switch (finishReason) {
|
|
@@ -1025,7 +1105,7 @@ function getCacheControl(providerMetadata) {
|
|
|
1025
1105
|
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
1106
|
}
|
|
1027
1107
|
function convertToOpenRouterChatMessages(prompt) {
|
|
1028
|
-
var _a15, _b, _c;
|
|
1108
|
+
var _a15, _b, _c, _d, _e;
|
|
1029
1109
|
const messages = [];
|
|
1030
1110
|
for (const { role, content, providerOptions } of prompt) {
|
|
1031
1111
|
switch (role) {
|
|
@@ -1056,7 +1136,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1056
1136
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1057
1137
|
const contentParts = content.map(
|
|
1058
1138
|
(part) => {
|
|
1059
|
-
var _a16, _b2, _c2,
|
|
1139
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
1060
1140
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
1061
1141
|
switch (part.type) {
|
|
1062
1142
|
case "text":
|
|
@@ -1082,7 +1162,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1082
1162
|
};
|
|
1083
1163
|
}
|
|
1084
1164
|
const fileName = String(
|
|
1085
|
-
(_f = (
|
|
1165
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1086
1166
|
);
|
|
1087
1167
|
const fileData = getFileUrl({
|
|
1088
1168
|
part,
|
|
@@ -1128,7 +1208,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1128
1208
|
case "assistant": {
|
|
1129
1209
|
let text = "";
|
|
1130
1210
|
let reasoning = "";
|
|
1131
|
-
const reasoningDetails = [];
|
|
1132
1211
|
const toolCalls = [];
|
|
1133
1212
|
for (const part of content) {
|
|
1134
1213
|
switch (part.type) {
|
|
@@ -1149,10 +1228,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1149
1228
|
}
|
|
1150
1229
|
case "reasoning": {
|
|
1151
1230
|
reasoning += part.text;
|
|
1152
|
-
reasoningDetails.push({
|
|
1153
|
-
type: "reasoning.text" /* Text */,
|
|
1154
|
-
text: part.text
|
|
1155
|
-
});
|
|
1156
1231
|
break;
|
|
1157
1232
|
}
|
|
1158
1233
|
case "file":
|
|
@@ -1162,12 +1237,15 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1162
1237
|
}
|
|
1163
1238
|
}
|
|
1164
1239
|
}
|
|
1240
|
+
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
|
|
1241
|
+
const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
|
|
1165
1242
|
messages.push({
|
|
1166
1243
|
role: "assistant",
|
|
1167
1244
|
content: text,
|
|
1168
1245
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1169
1246
|
reasoning: reasoning || void 0,
|
|
1170
|
-
reasoning_details
|
|
1247
|
+
// Only include reasoning_details if we have the preserved original version
|
|
1248
|
+
reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
|
|
1171
1249
|
cache_control: getCacheControl(providerOptions)
|
|
1172
1250
|
});
|
|
1173
1251
|
break;
|
|
@@ -1179,7 +1257,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1179
1257
|
role: "tool",
|
|
1180
1258
|
tool_call_id: toolResponse.toolCallId,
|
|
1181
1259
|
content: content2,
|
|
1182
|
-
cache_control: (
|
|
1260
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1183
1261
|
});
|
|
1184
1262
|
}
|
|
1185
1263
|
break;
|
|
@@ -1196,15 +1274,15 @@ function getToolResultContent(input) {
|
|
|
1196
1274
|
}
|
|
1197
1275
|
|
|
1198
1276
|
// src/chat/get-tool-choice.ts
|
|
1199
|
-
var
|
|
1200
|
-
var ChatCompletionToolChoiceSchema =
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
type:
|
|
1206
|
-
function:
|
|
1207
|
-
name:
|
|
1277
|
+
var import_v44 = require("zod/v4");
|
|
1278
|
+
var ChatCompletionToolChoiceSchema = import_v44.z.union([
|
|
1279
|
+
import_v44.z.literal("auto"),
|
|
1280
|
+
import_v44.z.literal("none"),
|
|
1281
|
+
import_v44.z.literal("required"),
|
|
1282
|
+
import_v44.z.object({
|
|
1283
|
+
type: import_v44.z.literal("function"),
|
|
1284
|
+
function: import_v44.z.object({
|
|
1285
|
+
name: import_v44.z.string()
|
|
1208
1286
|
})
|
|
1209
1287
|
})
|
|
1210
1288
|
]);
|
|
@@ -1222,150 +1300,210 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1222
1300
|
}
|
|
1223
1301
|
default: {
|
|
1224
1302
|
toolChoice;
|
|
1225
|
-
throw new
|
|
1303
|
+
throw new InvalidArgumentError({
|
|
1304
|
+
argument: "toolChoice",
|
|
1305
|
+
message: `Invalid tool choice type: ${JSON.stringify(toolChoice)}`
|
|
1306
|
+
});
|
|
1226
1307
|
}
|
|
1227
1308
|
}
|
|
1228
1309
|
}
|
|
1229
1310
|
|
|
1230
1311
|
// src/chat/schemas.ts
|
|
1231
|
-
var
|
|
1312
|
+
var import_v46 = require("zod/v4");
|
|
1232
1313
|
|
|
1233
1314
|
// src/schemas/image.ts
|
|
1234
|
-
var
|
|
1235
|
-
var ImageResponseSchema =
|
|
1236
|
-
type:
|
|
1237
|
-
image_url:
|
|
1238
|
-
url:
|
|
1315
|
+
var import_v45 = require("zod/v4");
|
|
1316
|
+
var ImageResponseSchema = import_v45.z.object({
|
|
1317
|
+
type: import_v45.z.literal("image_url"),
|
|
1318
|
+
image_url: import_v45.z.object({
|
|
1319
|
+
url: import_v45.z.string()
|
|
1239
1320
|
})
|
|
1240
1321
|
});
|
|
1241
|
-
var ImageResponseWithUnknownSchema =
|
|
1322
|
+
var ImageResponseWithUnknownSchema = import_v45.z.union([
|
|
1242
1323
|
ImageResponseSchema,
|
|
1243
|
-
|
|
1324
|
+
import_v45.z.unknown().transform(() => null)
|
|
1244
1325
|
]);
|
|
1245
|
-
var ImageResponseArraySchema =
|
|
1326
|
+
var ImageResponseArraySchema = import_v45.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1246
1327
|
|
|
1247
1328
|
// 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:
|
|
1329
|
+
var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
|
|
1330
|
+
id: import_v46.z.string().optional(),
|
|
1331
|
+
model: import_v46.z.string().optional(),
|
|
1332
|
+
provider: import_v46.z.string().optional(),
|
|
1333
|
+
usage: import_v46.z.object({
|
|
1334
|
+
prompt_tokens: import_v46.z.number(),
|
|
1335
|
+
prompt_tokens_details: import_v46.z.object({
|
|
1336
|
+
cached_tokens: import_v46.z.number()
|
|
1256
1337
|
}).nullish(),
|
|
1257
|
-
completion_tokens:
|
|
1258
|
-
completion_tokens_details:
|
|
1259
|
-
reasoning_tokens:
|
|
1338
|
+
completion_tokens: import_v46.z.number(),
|
|
1339
|
+
completion_tokens_details: import_v46.z.object({
|
|
1340
|
+
reasoning_tokens: import_v46.z.number()
|
|
1260
1341
|
}).nullish(),
|
|
1261
|
-
total_tokens:
|
|
1262
|
-
cost:
|
|
1263
|
-
cost_details:
|
|
1264
|
-
upstream_inference_cost:
|
|
1342
|
+
total_tokens: import_v46.z.number(),
|
|
1343
|
+
cost: import_v46.z.number().optional(),
|
|
1344
|
+
cost_details: import_v46.z.object({
|
|
1345
|
+
upstream_inference_cost: import_v46.z.number().nullish()
|
|
1265
1346
|
}).nullish()
|
|
1266
1347
|
}).nullish()
|
|
1267
1348
|
});
|
|
1268
|
-
var OpenRouterNonStreamChatCompletionResponseSchema =
|
|
1269
|
-
choices
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
annotations: import_v45.z.array(
|
|
1288
|
-
import_v45.z.object({
|
|
1289
|
-
type: import_v45.z.enum(["url_citation"]),
|
|
1290
|
-
url_citation: import_v45.z.object({
|
|
1291
|
-
end_index: import_v45.z.number(),
|
|
1292
|
-
start_index: import_v45.z.number(),
|
|
1293
|
-
title: import_v45.z.string(),
|
|
1294
|
-
url: import_v45.z.string(),
|
|
1295
|
-
content: import_v45.z.string().optional()
|
|
1349
|
+
var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
|
|
1350
|
+
// Success response with choices
|
|
1351
|
+
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1352
|
+
choices: import_v46.z.array(
|
|
1353
|
+
import_v46.z.object({
|
|
1354
|
+
message: import_v46.z.object({
|
|
1355
|
+
role: import_v46.z.literal("assistant"),
|
|
1356
|
+
content: import_v46.z.string().nullable().optional(),
|
|
1357
|
+
reasoning: import_v46.z.string().nullable().optional(),
|
|
1358
|
+
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1359
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1360
|
+
tool_calls: import_v46.z.array(
|
|
1361
|
+
import_v46.z.object({
|
|
1362
|
+
id: import_v46.z.string().optional().nullable(),
|
|
1363
|
+
type: import_v46.z.literal("function"),
|
|
1364
|
+
function: import_v46.z.object({
|
|
1365
|
+
name: import_v46.z.string(),
|
|
1366
|
+
arguments: import_v46.z.string()
|
|
1367
|
+
})
|
|
1296
1368
|
})
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1369
|
+
).optional(),
|
|
1370
|
+
annotations: import_v46.z.array(
|
|
1371
|
+
import_v46.z.union([
|
|
1372
|
+
// URL citation from web search
|
|
1373
|
+
import_v46.z.object({
|
|
1374
|
+
type: import_v46.z.literal("url_citation"),
|
|
1375
|
+
url_citation: import_v46.z.object({
|
|
1376
|
+
end_index: import_v46.z.number(),
|
|
1377
|
+
start_index: import_v46.z.number(),
|
|
1378
|
+
title: import_v46.z.string(),
|
|
1379
|
+
url: import_v46.z.string(),
|
|
1380
|
+
content: import_v46.z.string().optional()
|
|
1381
|
+
})
|
|
1382
|
+
}),
|
|
1383
|
+
// File annotation from FileParserPlugin (old format)
|
|
1384
|
+
import_v46.z.object({
|
|
1385
|
+
type: import_v46.z.literal("file_annotation"),
|
|
1386
|
+
file_annotation: import_v46.z.object({
|
|
1387
|
+
file_id: import_v46.z.string(),
|
|
1388
|
+
quote: import_v46.z.string().optional()
|
|
1389
|
+
})
|
|
1390
|
+
}),
|
|
1391
|
+
// File annotation from FileParserPlugin (new format)
|
|
1392
|
+
import_v46.z.object({
|
|
1393
|
+
type: import_v46.z.literal("file"),
|
|
1394
|
+
file: import_v46.z.object({
|
|
1395
|
+
hash: import_v46.z.string(),
|
|
1396
|
+
name: import_v46.z.string(),
|
|
1397
|
+
content: import_v46.z.array(
|
|
1398
|
+
import_v46.z.object({
|
|
1399
|
+
type: import_v46.z.string(),
|
|
1400
|
+
text: import_v46.z.string()
|
|
1401
|
+
})
|
|
1402
|
+
).optional()
|
|
1403
|
+
})
|
|
1310
1404
|
})
|
|
1311
|
-
)
|
|
1312
|
-
|
|
1313
|
-
)
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1405
|
+
])
|
|
1406
|
+
).nullish()
|
|
1407
|
+
}),
|
|
1408
|
+
index: import_v46.z.number().nullish(),
|
|
1409
|
+
logprobs: import_v46.z.object({
|
|
1410
|
+
content: import_v46.z.array(
|
|
1411
|
+
import_v46.z.object({
|
|
1412
|
+
token: import_v46.z.string(),
|
|
1413
|
+
logprob: import_v46.z.number(),
|
|
1414
|
+
top_logprobs: import_v46.z.array(
|
|
1415
|
+
import_v46.z.object({
|
|
1416
|
+
token: import_v46.z.string(),
|
|
1417
|
+
logprob: import_v46.z.number()
|
|
1418
|
+
})
|
|
1419
|
+
)
|
|
1420
|
+
})
|
|
1421
|
+
).nullable()
|
|
1422
|
+
}).nullable().optional(),
|
|
1423
|
+
finish_reason: import_v46.z.string().optional().nullable()
|
|
1424
|
+
})
|
|
1425
|
+
)
|
|
1426
|
+
}),
|
|
1427
|
+
// Error response (HTTP 200 with error payload)
|
|
1428
|
+
OpenRouterErrorResponseSchema.extend({
|
|
1429
|
+
user_id: import_v46.z.string().optional()
|
|
1430
|
+
})
|
|
1431
|
+
]);
|
|
1432
|
+
var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
|
|
1320
1433
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1321
|
-
choices:
|
|
1322
|
-
|
|
1323
|
-
delta:
|
|
1324
|
-
role:
|
|
1325
|
-
content:
|
|
1326
|
-
reasoning:
|
|
1434
|
+
choices: import_v46.z.array(
|
|
1435
|
+
import_v46.z.object({
|
|
1436
|
+
delta: import_v46.z.object({
|
|
1437
|
+
role: import_v46.z.enum(["assistant"]).optional(),
|
|
1438
|
+
content: import_v46.z.string().nullish(),
|
|
1439
|
+
reasoning: import_v46.z.string().nullish().optional(),
|
|
1327
1440
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1328
1441
|
images: ImageResponseArraySchema.nullish(),
|
|
1329
|
-
tool_calls:
|
|
1330
|
-
|
|
1331
|
-
index:
|
|
1332
|
-
id:
|
|
1333
|
-
type:
|
|
1334
|
-
function:
|
|
1335
|
-
name:
|
|
1336
|
-
arguments:
|
|
1442
|
+
tool_calls: import_v46.z.array(
|
|
1443
|
+
import_v46.z.object({
|
|
1444
|
+
index: import_v46.z.number().nullish(),
|
|
1445
|
+
id: import_v46.z.string().nullish(),
|
|
1446
|
+
type: import_v46.z.literal("function").optional(),
|
|
1447
|
+
function: import_v46.z.object({
|
|
1448
|
+
name: import_v46.z.string().nullish(),
|
|
1449
|
+
arguments: import_v46.z.string().nullish()
|
|
1337
1450
|
})
|
|
1338
1451
|
})
|
|
1339
1452
|
).nullish(),
|
|
1340
|
-
annotations:
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1453
|
+
annotations: import_v46.z.array(
|
|
1454
|
+
import_v46.z.union([
|
|
1455
|
+
// URL citation from web search
|
|
1456
|
+
import_v46.z.object({
|
|
1457
|
+
type: import_v46.z.literal("url_citation"),
|
|
1458
|
+
url_citation: import_v46.z.object({
|
|
1459
|
+
end_index: import_v46.z.number(),
|
|
1460
|
+
start_index: import_v46.z.number(),
|
|
1461
|
+
title: import_v46.z.string(),
|
|
1462
|
+
url: import_v46.z.string(),
|
|
1463
|
+
content: import_v46.z.string().optional()
|
|
1464
|
+
})
|
|
1465
|
+
}),
|
|
1466
|
+
// File annotation from FileParserPlugin (old format)
|
|
1467
|
+
import_v46.z.object({
|
|
1468
|
+
type: import_v46.z.literal("file_annotation"),
|
|
1469
|
+
file_annotation: import_v46.z.object({
|
|
1470
|
+
file_id: import_v46.z.string(),
|
|
1471
|
+
quote: import_v46.z.string().optional()
|
|
1472
|
+
})
|
|
1473
|
+
}),
|
|
1474
|
+
// File annotation from FileParserPlugin (new format)
|
|
1475
|
+
import_v46.z.object({
|
|
1476
|
+
type: import_v46.z.literal("file"),
|
|
1477
|
+
file: import_v46.z.object({
|
|
1478
|
+
hash: import_v46.z.string(),
|
|
1479
|
+
name: import_v46.z.string(),
|
|
1480
|
+
content: import_v46.z.array(
|
|
1481
|
+
import_v46.z.object({
|
|
1482
|
+
type: import_v46.z.string(),
|
|
1483
|
+
text: import_v46.z.string()
|
|
1484
|
+
})
|
|
1485
|
+
).optional()
|
|
1486
|
+
})
|
|
1349
1487
|
})
|
|
1350
|
-
|
|
1488
|
+
])
|
|
1351
1489
|
).nullish()
|
|
1352
1490
|
}).nullish(),
|
|
1353
|
-
logprobs:
|
|
1354
|
-
content:
|
|
1355
|
-
|
|
1356
|
-
token:
|
|
1357
|
-
logprob:
|
|
1358
|
-
top_logprobs:
|
|
1359
|
-
|
|
1360
|
-
token:
|
|
1361
|
-
logprob:
|
|
1491
|
+
logprobs: import_v46.z.object({
|
|
1492
|
+
content: import_v46.z.array(
|
|
1493
|
+
import_v46.z.object({
|
|
1494
|
+
token: import_v46.z.string(),
|
|
1495
|
+
logprob: import_v46.z.number(),
|
|
1496
|
+
top_logprobs: import_v46.z.array(
|
|
1497
|
+
import_v46.z.object({
|
|
1498
|
+
token: import_v46.z.string(),
|
|
1499
|
+
logprob: import_v46.z.number()
|
|
1362
1500
|
})
|
|
1363
1501
|
)
|
|
1364
1502
|
})
|
|
1365
1503
|
).nullable()
|
|
1366
1504
|
}).nullish(),
|
|
1367
|
-
finish_reason:
|
|
1368
|
-
index:
|
|
1505
|
+
finish_reason: import_v46.z.string().nullable().optional(),
|
|
1506
|
+
index: import_v46.z.number().nullish()
|
|
1369
1507
|
})
|
|
1370
1508
|
)
|
|
1371
1509
|
}),
|
|
@@ -1468,11 +1606,11 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1468
1606
|
return baseArgs;
|
|
1469
1607
|
}
|
|
1470
1608
|
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;
|
|
1609
|
+
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
1610
|
const providerOptions = options.providerOptions || {};
|
|
1473
1611
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1474
1612
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
1475
|
-
const { value:
|
|
1613
|
+
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
1476
1614
|
url: this.config.url({
|
|
1477
1615
|
path: "/chat/completions",
|
|
1478
1616
|
modelId: this.modelId
|
|
@@ -1486,9 +1624,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1486
1624
|
abortSignal: options.abortSignal,
|
|
1487
1625
|
fetch: this.config.fetch
|
|
1488
1626
|
});
|
|
1627
|
+
if ("error" in responseValue) {
|
|
1628
|
+
throw new APICallError({
|
|
1629
|
+
message: responseValue.error.message,
|
|
1630
|
+
url: this.config.url({
|
|
1631
|
+
path: "/chat/completions",
|
|
1632
|
+
modelId: this.modelId
|
|
1633
|
+
}),
|
|
1634
|
+
requestBodyValues: args,
|
|
1635
|
+
statusCode: 200,
|
|
1636
|
+
responseHeaders,
|
|
1637
|
+
data: responseValue.error
|
|
1638
|
+
});
|
|
1639
|
+
}
|
|
1640
|
+
const response = responseValue;
|
|
1489
1641
|
const choice = response.choices[0];
|
|
1490
1642
|
if (!choice) {
|
|
1491
|
-
throw new
|
|
1643
|
+
throw new NoContentGeneratedError({
|
|
1644
|
+
message: "No choice in response"
|
|
1645
|
+
});
|
|
1492
1646
|
}
|
|
1493
1647
|
const usageInfo = response.usage ? {
|
|
1494
1648
|
inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
|
|
@@ -1595,24 +1749,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1595
1749
|
usage: usageInfo,
|
|
1596
1750
|
warnings: [],
|
|
1597
1751
|
providerMetadata: {
|
|
1598
|
-
openrouter: {
|
|
1752
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1599
1753
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1754
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1600
1755
|
usage: {
|
|
1601
|
-
promptTokens: (
|
|
1602
|
-
completionTokens: (
|
|
1603
|
-
totalTokens: (
|
|
1604
|
-
cost: (
|
|
1756
|
+
promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
|
|
1757
|
+
completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
|
|
1758
|
+
totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
|
|
1759
|
+
cost: (_p = response.usage) == null ? void 0 : _p.cost,
|
|
1605
1760
|
promptTokensDetails: {
|
|
1606
|
-
cachedTokens: (
|
|
1761
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1607
1762
|
},
|
|
1608
1763
|
completionTokensDetails: {
|
|
1609
|
-
reasoningTokens: (
|
|
1764
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1610
1765
|
},
|
|
1611
1766
|
costDetails: {
|
|
1612
|
-
upstreamInferenceCost: (
|
|
1767
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1613
1768
|
}
|
|
1614
1769
|
}
|
|
1615
|
-
}
|
|
1770
|
+
})
|
|
1616
1771
|
},
|
|
1617
1772
|
request: { body: args },
|
|
1618
1773
|
response: {
|
|
@@ -1657,6 +1812,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1657
1812
|
cachedInputTokens: Number.NaN
|
|
1658
1813
|
};
|
|
1659
1814
|
const openrouterUsage = {};
|
|
1815
|
+
const accumulatedReasoningDetails = [];
|
|
1660
1816
|
let textStarted = false;
|
|
1661
1817
|
let reasoningStarted = false;
|
|
1662
1818
|
let textId;
|
|
@@ -1742,6 +1898,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1742
1898
|
});
|
|
1743
1899
|
};
|
|
1744
1900
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1901
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1745
1902
|
for (const detail of delta.reasoning_details) {
|
|
1746
1903
|
switch (detail.type) {
|
|
1747
1904
|
case "reasoning.text" /* Text */: {
|
|
@@ -1845,7 +2002,10 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1845
2002
|
};
|
|
1846
2003
|
const toolCall2 = toolCalls[index];
|
|
1847
2004
|
if (toolCall2 == null) {
|
|
1848
|
-
throw new
|
|
2005
|
+
throw new InvalidResponseDataError({
|
|
2006
|
+
data: { index, toolCallsLength: toolCalls.length },
|
|
2007
|
+
message: `Tool call at index ${index} is missing after creation.`
|
|
2008
|
+
});
|
|
1849
2009
|
}
|
|
1850
2010
|
if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
|
|
1851
2011
|
toolCall2.inputStarted = true;
|
|
@@ -1875,7 +2035,14 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1875
2035
|
}
|
|
1876
2036
|
const toolCall = toolCalls[index];
|
|
1877
2037
|
if (toolCall == null) {
|
|
1878
|
-
throw new
|
|
2038
|
+
throw new InvalidResponseDataError({
|
|
2039
|
+
data: {
|
|
2040
|
+
index,
|
|
2041
|
+
toolCallsLength: toolCalls.length,
|
|
2042
|
+
toolCallDelta
|
|
2043
|
+
},
|
|
2044
|
+
message: `Tool call at index ${index} is missing during merge.`
|
|
2045
|
+
});
|
|
1879
2046
|
}
|
|
1880
2047
|
if (!toolCall.inputStarted) {
|
|
1881
2048
|
toolCall.inputStarted = true;
|
|
@@ -1948,6 +2115,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1948
2115
|
if (provider !== void 0) {
|
|
1949
2116
|
openrouterMetadata.provider = provider;
|
|
1950
2117
|
}
|
|
2118
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
2119
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
2120
|
+
}
|
|
1951
2121
|
controller.enqueue({
|
|
1952
2122
|
type: "finish",
|
|
1953
2123
|
finishReason,
|
|
@@ -2070,36 +2240,36 @@ ${assistantMessage}
|
|
|
2070
2240
|
}
|
|
2071
2241
|
|
|
2072
2242
|
// src/completion/schemas.ts
|
|
2073
|
-
var
|
|
2074
|
-
var OpenRouterCompletionChunkSchema =
|
|
2075
|
-
|
|
2076
|
-
id:
|
|
2077
|
-
model:
|
|
2078
|
-
choices:
|
|
2079
|
-
|
|
2080
|
-
text:
|
|
2081
|
-
reasoning:
|
|
2243
|
+
var import_v47 = require("zod/v4");
|
|
2244
|
+
var OpenRouterCompletionChunkSchema = import_v47.z.union([
|
|
2245
|
+
import_v47.z.object({
|
|
2246
|
+
id: import_v47.z.string().optional(),
|
|
2247
|
+
model: import_v47.z.string().optional(),
|
|
2248
|
+
choices: import_v47.z.array(
|
|
2249
|
+
import_v47.z.object({
|
|
2250
|
+
text: import_v47.z.string(),
|
|
2251
|
+
reasoning: import_v47.z.string().nullish().optional(),
|
|
2082
2252
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2083
|
-
finish_reason:
|
|
2084
|
-
index:
|
|
2085
|
-
logprobs:
|
|
2086
|
-
tokens:
|
|
2087
|
-
token_logprobs:
|
|
2088
|
-
top_logprobs:
|
|
2253
|
+
finish_reason: import_v47.z.string().nullish(),
|
|
2254
|
+
index: import_v47.z.number().nullish(),
|
|
2255
|
+
logprobs: import_v47.z.object({
|
|
2256
|
+
tokens: import_v47.z.array(import_v47.z.string()),
|
|
2257
|
+
token_logprobs: import_v47.z.array(import_v47.z.number()),
|
|
2258
|
+
top_logprobs: import_v47.z.array(import_v47.z.record(import_v47.z.string(), import_v47.z.number())).nullable()
|
|
2089
2259
|
}).nullable().optional()
|
|
2090
2260
|
})
|
|
2091
2261
|
),
|
|
2092
|
-
usage:
|
|
2093
|
-
prompt_tokens:
|
|
2094
|
-
prompt_tokens_details:
|
|
2095
|
-
cached_tokens:
|
|
2262
|
+
usage: import_v47.z.object({
|
|
2263
|
+
prompt_tokens: import_v47.z.number(),
|
|
2264
|
+
prompt_tokens_details: import_v47.z.object({
|
|
2265
|
+
cached_tokens: import_v47.z.number()
|
|
2096
2266
|
}).nullish(),
|
|
2097
|
-
completion_tokens:
|
|
2098
|
-
completion_tokens_details:
|
|
2099
|
-
reasoning_tokens:
|
|
2267
|
+
completion_tokens: import_v47.z.number(),
|
|
2268
|
+
completion_tokens_details: import_v47.z.object({
|
|
2269
|
+
reasoning_tokens: import_v47.z.number()
|
|
2100
2270
|
}).nullish(),
|
|
2101
|
-
total_tokens:
|
|
2102
|
-
cost:
|
|
2271
|
+
total_tokens: import_v47.z.number(),
|
|
2272
|
+
cost: import_v47.z.number().optional()
|
|
2103
2273
|
}).nullish()
|
|
2104
2274
|
}),
|
|
2105
2275
|
OpenRouterErrorResponseSchema
|
|
@@ -2197,11 +2367,23 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2197
2367
|
fetch: this.config.fetch
|
|
2198
2368
|
});
|
|
2199
2369
|
if ("error" in response) {
|
|
2200
|
-
throw new
|
|
2370
|
+
throw new APICallError({
|
|
2371
|
+
message: response.error.message,
|
|
2372
|
+
url: this.config.url({
|
|
2373
|
+
path: "/completions",
|
|
2374
|
+
modelId: this.modelId
|
|
2375
|
+
}),
|
|
2376
|
+
requestBodyValues: args,
|
|
2377
|
+
statusCode: 200,
|
|
2378
|
+
responseHeaders,
|
|
2379
|
+
data: response.error
|
|
2380
|
+
});
|
|
2201
2381
|
}
|
|
2202
2382
|
const choice = response.choices[0];
|
|
2203
2383
|
if (!choice) {
|
|
2204
|
-
throw new
|
|
2384
|
+
throw new NoContentGeneratedError({
|
|
2385
|
+
message: "No choice in OpenRouter completion response"
|
|
2386
|
+
});
|
|
2205
2387
|
}
|
|
2206
2388
|
return {
|
|
2207
2389
|
content: [
|
|
@@ -2368,18 +2550,43 @@ var OpenRouter = class {
|
|
|
2368
2550
|
}
|
|
2369
2551
|
};
|
|
2370
2552
|
|
|
2553
|
+
// src/utils/remove-undefined.ts
|
|
2554
|
+
function removeUndefinedEntries2(record) {
|
|
2555
|
+
return Object.fromEntries(
|
|
2556
|
+
Object.entries(record).filter(([, value]) => value !== null)
|
|
2557
|
+
);
|
|
2558
|
+
}
|
|
2559
|
+
|
|
2560
|
+
// src/utils/with-user-agent-suffix.ts
|
|
2561
|
+
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
2562
|
+
const cleanedHeaders = removeUndefinedEntries2(
|
|
2563
|
+
headers != null ? headers : {}
|
|
2564
|
+
);
|
|
2565
|
+
const currentUserAgentHeader = cleanedHeaders["user-agent"] || "";
|
|
2566
|
+
const newUserAgent = [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ");
|
|
2567
|
+
return __spreadProps(__spreadValues({}, cleanedHeaders), {
|
|
2568
|
+
"user-agent": newUserAgent
|
|
2569
|
+
});
|
|
2570
|
+
}
|
|
2571
|
+
|
|
2572
|
+
// src/version.ts
|
|
2573
|
+
var VERSION = false ? "0.0.0-test" : "1.2.2";
|
|
2574
|
+
|
|
2371
2575
|
// src/provider.ts
|
|
2372
2576
|
function createOpenRouter(options = {}) {
|
|
2373
2577
|
var _a15, _b, _c;
|
|
2374
2578
|
const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
2375
2579
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2376
|
-
const getHeaders = () =>
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2580
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
2581
|
+
__spreadValues({
|
|
2582
|
+
Authorization: `Bearer ${loadApiKey({
|
|
2583
|
+
apiKey: options.apiKey,
|
|
2584
|
+
environmentVariableName: "OPENROUTER_API_KEY",
|
|
2585
|
+
description: "OpenRouter"
|
|
2586
|
+
})}`
|
|
2587
|
+
}, options.headers),
|
|
2588
|
+
`ai-sdk/openrouter/${VERSION}`
|
|
2589
|
+
);
|
|
2383
2590
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
2384
2591
|
provider: "openrouter.chat",
|
|
2385
2592
|
url: ({ path }) => `${baseURL}${path}`,
|