@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.mjs
CHANGED
|
@@ -884,19 +884,40 @@ function withoutTrailingSlash(url) {
|
|
|
884
884
|
|
|
885
885
|
// src/schemas/reasoning-details.ts
|
|
886
886
|
import { z } from "zod/v4";
|
|
887
|
+
|
|
888
|
+
// src/utils/type-guards.ts
|
|
889
|
+
function isDefinedOrNotNull(value) {
|
|
890
|
+
return value !== null && value !== void 0;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
// src/schemas/format.ts
|
|
894
|
+
var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
|
|
895
|
+
ReasoningFormat2["Unknown"] = "unknown";
|
|
896
|
+
ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
|
|
897
|
+
ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
|
|
898
|
+
ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
|
|
899
|
+
return ReasoningFormat2;
|
|
900
|
+
})(ReasoningFormat || {});
|
|
901
|
+
|
|
902
|
+
// src/schemas/reasoning-details.ts
|
|
903
|
+
var CommonReasoningDetailSchema = z.object({
|
|
904
|
+
id: z.string().nullish(),
|
|
905
|
+
format: z.nativeEnum(ReasoningFormat).nullish(),
|
|
906
|
+
index: z.number().optional()
|
|
907
|
+
});
|
|
887
908
|
var ReasoningDetailSummarySchema = z.object({
|
|
888
909
|
type: z.literal("reasoning.summary" /* Summary */),
|
|
889
910
|
summary: z.string()
|
|
890
|
-
});
|
|
911
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
891
912
|
var ReasoningDetailEncryptedSchema = z.object({
|
|
892
913
|
type: z.literal("reasoning.encrypted" /* Encrypted */),
|
|
893
914
|
data: z.string()
|
|
894
|
-
});
|
|
915
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
895
916
|
var ReasoningDetailTextSchema = z.object({
|
|
896
917
|
type: z.literal("reasoning.text" /* Text */),
|
|
897
918
|
text: z.string().nullish(),
|
|
898
919
|
signature: z.string().nullish()
|
|
899
|
-
});
|
|
920
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
900
921
|
var ReasoningDetailUnionSchema = z.union([
|
|
901
922
|
ReasoningDetailSummarySchema,
|
|
902
923
|
ReasoningDetailEncryptedSchema,
|
|
@@ -907,6 +928,22 @@ var ReasoningDetailsWithUnknownSchema = z.union([
|
|
|
907
928
|
z.unknown().transform(() => null)
|
|
908
929
|
]);
|
|
909
930
|
var ReasoningDetailArraySchema = z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
931
|
+
var OutputUnionToReasoningDetailsSchema = z.union([
|
|
932
|
+
z.object({
|
|
933
|
+
delta: z.object({
|
|
934
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
935
|
+
})
|
|
936
|
+
}).transform((data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)),
|
|
937
|
+
z.object({
|
|
938
|
+
message: z.object({
|
|
939
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
940
|
+
})
|
|
941
|
+
}).transform((data) => data.message.reasoning_details.filter(isDefinedOrNotNull)),
|
|
942
|
+
z.object({
|
|
943
|
+
text: z.string(),
|
|
944
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
945
|
+
}).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
|
|
946
|
+
]);
|
|
910
947
|
|
|
911
948
|
// src/schemas/error-response.ts
|
|
912
949
|
import { z as z2 } from "zod/v4";
|
|
@@ -923,6 +960,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
923
960
|
errorToMessage: (data) => data.error.message
|
|
924
961
|
});
|
|
925
962
|
|
|
963
|
+
// src/schemas/provider-metadata.ts
|
|
964
|
+
import { z as z3 } from "zod/v4";
|
|
965
|
+
var OpenRouterProviderMetadataSchema = z3.object({
|
|
966
|
+
provider: z3.string(),
|
|
967
|
+
reasoning_details: z3.array(ReasoningDetailUnionSchema).optional(),
|
|
968
|
+
usage: z3.object({
|
|
969
|
+
promptTokens: z3.number(),
|
|
970
|
+
promptTokensDetails: z3.object({
|
|
971
|
+
cachedTokens: z3.number()
|
|
972
|
+
}).optional(),
|
|
973
|
+
completionTokens: z3.number(),
|
|
974
|
+
completionTokensDetails: z3.object({
|
|
975
|
+
reasoningTokens: z3.number()
|
|
976
|
+
}).optional(),
|
|
977
|
+
totalTokens: z3.number(),
|
|
978
|
+
cost: z3.number().optional(),
|
|
979
|
+
costDetails: z3.object({
|
|
980
|
+
upstreamInferenceCost: z3.number()
|
|
981
|
+
})
|
|
982
|
+
})
|
|
983
|
+
});
|
|
984
|
+
var OpenRouterProviderOptionsSchema = z3.object({
|
|
985
|
+
openrouter: z3.object({
|
|
986
|
+
reasoning_details: z3.array(ReasoningDetailUnionSchema).optional()
|
|
987
|
+
}).optional()
|
|
988
|
+
}).optional();
|
|
989
|
+
|
|
926
990
|
// src/utils/map-finish-reason.ts
|
|
927
991
|
function mapOpenRouterFinishReason(finishReason) {
|
|
928
992
|
switch (finishReason) {
|
|
@@ -972,6 +1036,15 @@ function getFileUrl({
|
|
|
972
1036
|
}
|
|
973
1037
|
return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
|
|
974
1038
|
}
|
|
1039
|
+
function getMediaType(dataUrl, defaultMediaType) {
|
|
1040
|
+
var _a15;
|
|
1041
|
+
const match = dataUrl.match(/^data:([^;]+)/);
|
|
1042
|
+
return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
|
|
1043
|
+
}
|
|
1044
|
+
function getBase64FromDataUrl(dataUrl) {
|
|
1045
|
+
const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
|
|
1046
|
+
return match ? match[1] : dataUrl;
|
|
1047
|
+
}
|
|
975
1048
|
|
|
976
1049
|
// src/chat/convert-to-openrouter-chat-messages.ts
|
|
977
1050
|
function getCacheControl(providerMetadata) {
|
|
@@ -981,7 +1054,7 @@ function getCacheControl(providerMetadata) {
|
|
|
981
1054
|
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;
|
|
982
1055
|
}
|
|
983
1056
|
function convertToOpenRouterChatMessages(prompt) {
|
|
984
|
-
var _a15, _b, _c;
|
|
1057
|
+
var _a15, _b, _c, _d, _e;
|
|
985
1058
|
const messages = [];
|
|
986
1059
|
for (const { role, content, providerOptions } of prompt) {
|
|
987
1060
|
switch (role) {
|
|
@@ -1012,7 +1085,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1012
1085
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1013
1086
|
const contentParts = content.map(
|
|
1014
1087
|
(part) => {
|
|
1015
|
-
var _a16, _b2, _c2,
|
|
1088
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
1016
1089
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
1017
1090
|
switch (part.type) {
|
|
1018
1091
|
case "text":
|
|
@@ -1038,7 +1111,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1038
1111
|
};
|
|
1039
1112
|
}
|
|
1040
1113
|
const fileName = String(
|
|
1041
|
-
(_f = (
|
|
1114
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1042
1115
|
);
|
|
1043
1116
|
const fileData = getFileUrl({
|
|
1044
1117
|
part,
|
|
@@ -1084,7 +1157,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1084
1157
|
case "assistant": {
|
|
1085
1158
|
let text = "";
|
|
1086
1159
|
let reasoning = "";
|
|
1087
|
-
const reasoningDetails = [];
|
|
1088
1160
|
const toolCalls = [];
|
|
1089
1161
|
for (const part of content) {
|
|
1090
1162
|
switch (part.type) {
|
|
@@ -1105,10 +1177,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1105
1177
|
}
|
|
1106
1178
|
case "reasoning": {
|
|
1107
1179
|
reasoning += part.text;
|
|
1108
|
-
reasoningDetails.push({
|
|
1109
|
-
type: "reasoning.text" /* Text */,
|
|
1110
|
-
text: part.text
|
|
1111
|
-
});
|
|
1112
1180
|
break;
|
|
1113
1181
|
}
|
|
1114
1182
|
case "file":
|
|
@@ -1118,12 +1186,17 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1118
1186
|
}
|
|
1119
1187
|
}
|
|
1120
1188
|
}
|
|
1189
|
+
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(
|
|
1190
|
+
providerOptions
|
|
1191
|
+
);
|
|
1192
|
+
const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
|
|
1121
1193
|
messages.push({
|
|
1122
1194
|
role: "assistant",
|
|
1123
1195
|
content: text,
|
|
1124
1196
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1125
1197
|
reasoning: reasoning || void 0,
|
|
1126
|
-
reasoning_details
|
|
1198
|
+
// Only include reasoning_details if we have the preserved original version
|
|
1199
|
+
reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
|
|
1127
1200
|
cache_control: getCacheControl(providerOptions)
|
|
1128
1201
|
});
|
|
1129
1202
|
break;
|
|
@@ -1135,7 +1208,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1135
1208
|
role: "tool",
|
|
1136
1209
|
tool_call_id: toolResponse.toolCallId,
|
|
1137
1210
|
content: content2,
|
|
1138
|
-
cache_control: (
|
|
1211
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1139
1212
|
});
|
|
1140
1213
|
}
|
|
1141
1214
|
break;
|
|
@@ -1152,15 +1225,15 @@ function getToolResultContent(input) {
|
|
|
1152
1225
|
}
|
|
1153
1226
|
|
|
1154
1227
|
// src/chat/get-tool-choice.ts
|
|
1155
|
-
import { z as
|
|
1156
|
-
var ChatCompletionToolChoiceSchema =
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
type:
|
|
1162
|
-
function:
|
|
1163
|
-
name:
|
|
1228
|
+
import { z as z5 } from "zod/v4";
|
|
1229
|
+
var ChatCompletionToolChoiceSchema = z5.union([
|
|
1230
|
+
z5.literal("auto"),
|
|
1231
|
+
z5.literal("none"),
|
|
1232
|
+
z5.literal("required"),
|
|
1233
|
+
z5.object({
|
|
1234
|
+
type: z5.literal("function"),
|
|
1235
|
+
function: z5.object({
|
|
1236
|
+
name: z5.string()
|
|
1164
1237
|
})
|
|
1165
1238
|
})
|
|
1166
1239
|
]);
|
|
@@ -1184,126 +1257,144 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1184
1257
|
}
|
|
1185
1258
|
|
|
1186
1259
|
// src/chat/schemas.ts
|
|
1187
|
-
import { z as
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1260
|
+
import { z as z7 } from "zod/v4";
|
|
1261
|
+
|
|
1262
|
+
// src/schemas/image.ts
|
|
1263
|
+
import { z as z6 } from "zod/v4";
|
|
1264
|
+
var ImageResponseSchema = z6.object({
|
|
1265
|
+
type: z6.literal("image_url"),
|
|
1266
|
+
image_url: z6.object({
|
|
1267
|
+
url: z6.string()
|
|
1268
|
+
})
|
|
1269
|
+
});
|
|
1270
|
+
var ImageResponseWithUnknownSchema = z6.union([
|
|
1271
|
+
ImageResponseSchema,
|
|
1272
|
+
z6.unknown().transform(() => null)
|
|
1273
|
+
]);
|
|
1274
|
+
var ImageResponseArraySchema = z6.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1275
|
+
|
|
1276
|
+
// src/chat/schemas.ts
|
|
1277
|
+
var OpenRouterChatCompletionBaseResponseSchema = z7.object({
|
|
1278
|
+
id: z7.string().optional(),
|
|
1279
|
+
model: z7.string().optional(),
|
|
1280
|
+
provider: z7.string().optional(),
|
|
1281
|
+
usage: z7.object({
|
|
1282
|
+
prompt_tokens: z7.number(),
|
|
1283
|
+
prompt_tokens_details: z7.object({
|
|
1284
|
+
cached_tokens: z7.number()
|
|
1196
1285
|
}).nullish(),
|
|
1197
|
-
completion_tokens:
|
|
1198
|
-
completion_tokens_details:
|
|
1199
|
-
reasoning_tokens:
|
|
1286
|
+
completion_tokens: z7.number(),
|
|
1287
|
+
completion_tokens_details: z7.object({
|
|
1288
|
+
reasoning_tokens: z7.number()
|
|
1200
1289
|
}).nullish(),
|
|
1201
|
-
total_tokens:
|
|
1202
|
-
cost:
|
|
1203
|
-
cost_details:
|
|
1204
|
-
upstream_inference_cost:
|
|
1290
|
+
total_tokens: z7.number(),
|
|
1291
|
+
cost: z7.number().optional(),
|
|
1292
|
+
cost_details: z7.object({
|
|
1293
|
+
upstream_inference_cost: z7.number().nullish()
|
|
1205
1294
|
}).nullish()
|
|
1206
1295
|
}).nullish()
|
|
1207
1296
|
});
|
|
1208
1297
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1209
|
-
choices:
|
|
1210
|
-
|
|
1211
|
-
message:
|
|
1212
|
-
role:
|
|
1213
|
-
content:
|
|
1214
|
-
reasoning:
|
|
1298
|
+
choices: z7.array(
|
|
1299
|
+
z7.object({
|
|
1300
|
+
message: z7.object({
|
|
1301
|
+
role: z7.literal("assistant"),
|
|
1302
|
+
content: z7.string().nullable().optional(),
|
|
1303
|
+
reasoning: z7.string().nullable().optional(),
|
|
1215
1304
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1305
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1306
|
+
tool_calls: z7.array(
|
|
1307
|
+
z7.object({
|
|
1308
|
+
id: z7.string().optional().nullable(),
|
|
1309
|
+
type: z7.literal("function"),
|
|
1310
|
+
function: z7.object({
|
|
1311
|
+
name: z7.string(),
|
|
1312
|
+
arguments: z7.string()
|
|
1223
1313
|
})
|
|
1224
1314
|
})
|
|
1225
1315
|
).optional(),
|
|
1226
|
-
annotations:
|
|
1227
|
-
|
|
1228
|
-
type:
|
|
1229
|
-
url_citation:
|
|
1230
|
-
end_index:
|
|
1231
|
-
start_index:
|
|
1232
|
-
title:
|
|
1233
|
-
url:
|
|
1234
|
-
content:
|
|
1316
|
+
annotations: z7.array(
|
|
1317
|
+
z7.object({
|
|
1318
|
+
type: z7.enum(["url_citation"]),
|
|
1319
|
+
url_citation: z7.object({
|
|
1320
|
+
end_index: z7.number(),
|
|
1321
|
+
start_index: z7.number(),
|
|
1322
|
+
title: z7.string(),
|
|
1323
|
+
url: z7.string(),
|
|
1324
|
+
content: z7.string().optional()
|
|
1235
1325
|
})
|
|
1236
1326
|
})
|
|
1237
1327
|
).nullish()
|
|
1238
1328
|
}),
|
|
1239
|
-
index:
|
|
1240
|
-
logprobs:
|
|
1241
|
-
content:
|
|
1242
|
-
|
|
1243
|
-
token:
|
|
1244
|
-
logprob:
|
|
1245
|
-
top_logprobs:
|
|
1246
|
-
|
|
1247
|
-
token:
|
|
1248
|
-
logprob:
|
|
1329
|
+
index: z7.number().nullish(),
|
|
1330
|
+
logprobs: z7.object({
|
|
1331
|
+
content: z7.array(
|
|
1332
|
+
z7.object({
|
|
1333
|
+
token: z7.string(),
|
|
1334
|
+
logprob: z7.number(),
|
|
1335
|
+
top_logprobs: z7.array(
|
|
1336
|
+
z7.object({
|
|
1337
|
+
token: z7.string(),
|
|
1338
|
+
logprob: z7.number()
|
|
1249
1339
|
})
|
|
1250
1340
|
)
|
|
1251
1341
|
})
|
|
1252
1342
|
).nullable()
|
|
1253
1343
|
}).nullable().optional(),
|
|
1254
|
-
finish_reason:
|
|
1344
|
+
finish_reason: z7.string().optional().nullable()
|
|
1255
1345
|
})
|
|
1256
1346
|
)
|
|
1257
1347
|
});
|
|
1258
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1348
|
+
var OpenRouterStreamChatCompletionChunkSchema = z7.union([
|
|
1259
1349
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1260
|
-
choices:
|
|
1261
|
-
|
|
1262
|
-
delta:
|
|
1263
|
-
role:
|
|
1264
|
-
content:
|
|
1265
|
-
reasoning:
|
|
1350
|
+
choices: z7.array(
|
|
1351
|
+
z7.object({
|
|
1352
|
+
delta: z7.object({
|
|
1353
|
+
role: z7.enum(["assistant"]).optional(),
|
|
1354
|
+
content: z7.string().nullish(),
|
|
1355
|
+
reasoning: z7.string().nullish().optional(),
|
|
1266
1356
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1357
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1358
|
+
tool_calls: z7.array(
|
|
1359
|
+
z7.object({
|
|
1360
|
+
index: z7.number().nullish(),
|
|
1361
|
+
id: z7.string().nullish(),
|
|
1362
|
+
type: z7.literal("function").optional(),
|
|
1363
|
+
function: z7.object({
|
|
1364
|
+
name: z7.string().nullish(),
|
|
1365
|
+
arguments: z7.string().nullish()
|
|
1275
1366
|
})
|
|
1276
1367
|
})
|
|
1277
1368
|
).nullish(),
|
|
1278
|
-
annotations:
|
|
1279
|
-
|
|
1280
|
-
type:
|
|
1281
|
-
url_citation:
|
|
1282
|
-
end_index:
|
|
1283
|
-
start_index:
|
|
1284
|
-
title:
|
|
1285
|
-
url:
|
|
1286
|
-
content:
|
|
1369
|
+
annotations: z7.array(
|
|
1370
|
+
z7.object({
|
|
1371
|
+
type: z7.enum(["url_citation"]),
|
|
1372
|
+
url_citation: z7.object({
|
|
1373
|
+
end_index: z7.number(),
|
|
1374
|
+
start_index: z7.number(),
|
|
1375
|
+
title: z7.string(),
|
|
1376
|
+
url: z7.string(),
|
|
1377
|
+
content: z7.string().optional()
|
|
1287
1378
|
})
|
|
1288
1379
|
})
|
|
1289
1380
|
).nullish()
|
|
1290
1381
|
}).nullish(),
|
|
1291
|
-
logprobs:
|
|
1292
|
-
content:
|
|
1293
|
-
|
|
1294
|
-
token:
|
|
1295
|
-
logprob:
|
|
1296
|
-
top_logprobs:
|
|
1297
|
-
|
|
1298
|
-
token:
|
|
1299
|
-
logprob:
|
|
1382
|
+
logprobs: z7.object({
|
|
1383
|
+
content: z7.array(
|
|
1384
|
+
z7.object({
|
|
1385
|
+
token: z7.string(),
|
|
1386
|
+
logprob: z7.number(),
|
|
1387
|
+
top_logprobs: z7.array(
|
|
1388
|
+
z7.object({
|
|
1389
|
+
token: z7.string(),
|
|
1390
|
+
logprob: z7.number()
|
|
1300
1391
|
})
|
|
1301
1392
|
)
|
|
1302
1393
|
})
|
|
1303
1394
|
).nullable()
|
|
1304
1395
|
}).nullish(),
|
|
1305
|
-
finish_reason:
|
|
1306
|
-
index:
|
|
1396
|
+
finish_reason: z7.string().nullable().optional(),
|
|
1397
|
+
index: z7.number().nullish()
|
|
1307
1398
|
})
|
|
1308
1399
|
)
|
|
1309
1400
|
}),
|
|
@@ -1406,7 +1497,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1406
1497
|
return baseArgs;
|
|
1407
1498
|
}
|
|
1408
1499
|
async doGenerate(options) {
|
|
1409
|
-
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;
|
|
1500
|
+
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;
|
|
1410
1501
|
const providerOptions = options.providerOptions || {};
|
|
1411
1502
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1412
1503
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1500,6 +1591,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1500
1591
|
});
|
|
1501
1592
|
}
|
|
1502
1593
|
}
|
|
1594
|
+
if (choice.message.images) {
|
|
1595
|
+
for (const image of choice.message.images) {
|
|
1596
|
+
content.push({
|
|
1597
|
+
type: "file",
|
|
1598
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1599
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1600
|
+
});
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1503
1603
|
if (choice.message.annotations) {
|
|
1504
1604
|
for (const annotation of choice.message.annotations) {
|
|
1505
1605
|
if (annotation.type === "url_citation") {
|
|
@@ -1524,24 +1624,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1524
1624
|
usage: usageInfo,
|
|
1525
1625
|
warnings: [],
|
|
1526
1626
|
providerMetadata: {
|
|
1527
|
-
openrouter: {
|
|
1627
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1528
1628
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1629
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1529
1630
|
usage: {
|
|
1530
|
-
promptTokens: (
|
|
1531
|
-
completionTokens: (
|
|
1532
|
-
totalTokens: (
|
|
1533
|
-
cost: (
|
|
1631
|
+
promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
|
|
1632
|
+
completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
|
|
1633
|
+
totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
|
|
1634
|
+
cost: (_p = response.usage) == null ? void 0 : _p.cost,
|
|
1534
1635
|
promptTokensDetails: {
|
|
1535
|
-
cachedTokens: (
|
|
1636
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1536
1637
|
},
|
|
1537
1638
|
completionTokensDetails: {
|
|
1538
|
-
reasoningTokens: (
|
|
1639
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1539
1640
|
},
|
|
1540
1641
|
costDetails: {
|
|
1541
|
-
upstreamInferenceCost: (
|
|
1642
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1542
1643
|
}
|
|
1543
1644
|
}
|
|
1544
|
-
}
|
|
1645
|
+
})
|
|
1545
1646
|
},
|
|
1546
1647
|
request: { body: args },
|
|
1547
1648
|
response: {
|
|
@@ -1586,6 +1687,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1586
1687
|
cachedInputTokens: Number.NaN
|
|
1587
1688
|
};
|
|
1588
1689
|
const openrouterUsage = {};
|
|
1690
|
+
const accumulatedReasoningDetails = [];
|
|
1589
1691
|
let textStarted = false;
|
|
1590
1692
|
let reasoningStarted = false;
|
|
1591
1693
|
let textId;
|
|
@@ -1671,6 +1773,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1671
1773
|
});
|
|
1672
1774
|
};
|
|
1673
1775
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1776
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1674
1777
|
for (const detail of delta.reasoning_details) {
|
|
1675
1778
|
switch (detail.type) {
|
|
1676
1779
|
case "reasoning.text" /* Text */: {
|
|
@@ -1833,6 +1936,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1833
1936
|
}
|
|
1834
1937
|
}
|
|
1835
1938
|
}
|
|
1939
|
+
if (delta.images != null) {
|
|
1940
|
+
for (const image of delta.images) {
|
|
1941
|
+
controller.enqueue({
|
|
1942
|
+
type: "file",
|
|
1943
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1944
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1836
1948
|
},
|
|
1837
1949
|
flush(controller) {
|
|
1838
1950
|
var _a16;
|
|
@@ -1868,6 +1980,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1868
1980
|
if (provider !== void 0) {
|
|
1869
1981
|
openrouterMetadata.provider = provider;
|
|
1870
1982
|
}
|
|
1983
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
1984
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
1985
|
+
}
|
|
1871
1986
|
controller.enqueue({
|
|
1872
1987
|
type: "finish",
|
|
1873
1988
|
finishReason,
|
|
@@ -1990,36 +2105,36 @@ ${assistantMessage}
|
|
|
1990
2105
|
}
|
|
1991
2106
|
|
|
1992
2107
|
// src/completion/schemas.ts
|
|
1993
|
-
import { z as
|
|
1994
|
-
var OpenRouterCompletionChunkSchema =
|
|
1995
|
-
|
|
1996
|
-
id:
|
|
1997
|
-
model:
|
|
1998
|
-
choices:
|
|
1999
|
-
|
|
2000
|
-
text:
|
|
2001
|
-
reasoning:
|
|
2108
|
+
import { z as z8 } from "zod/v4";
|
|
2109
|
+
var OpenRouterCompletionChunkSchema = z8.union([
|
|
2110
|
+
z8.object({
|
|
2111
|
+
id: z8.string().optional(),
|
|
2112
|
+
model: z8.string().optional(),
|
|
2113
|
+
choices: z8.array(
|
|
2114
|
+
z8.object({
|
|
2115
|
+
text: z8.string(),
|
|
2116
|
+
reasoning: z8.string().nullish().optional(),
|
|
2002
2117
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2003
|
-
finish_reason:
|
|
2004
|
-
index:
|
|
2005
|
-
logprobs:
|
|
2006
|
-
tokens:
|
|
2007
|
-
token_logprobs:
|
|
2008
|
-
top_logprobs:
|
|
2118
|
+
finish_reason: z8.string().nullish(),
|
|
2119
|
+
index: z8.number().nullish(),
|
|
2120
|
+
logprobs: z8.object({
|
|
2121
|
+
tokens: z8.array(z8.string()),
|
|
2122
|
+
token_logprobs: z8.array(z8.number()),
|
|
2123
|
+
top_logprobs: z8.array(z8.record(z8.string(), z8.number())).nullable()
|
|
2009
2124
|
}).nullable().optional()
|
|
2010
2125
|
})
|
|
2011
2126
|
),
|
|
2012
|
-
usage:
|
|
2013
|
-
prompt_tokens:
|
|
2014
|
-
prompt_tokens_details:
|
|
2015
|
-
cached_tokens:
|
|
2127
|
+
usage: z8.object({
|
|
2128
|
+
prompt_tokens: z8.number(),
|
|
2129
|
+
prompt_tokens_details: z8.object({
|
|
2130
|
+
cached_tokens: z8.number()
|
|
2016
2131
|
}).nullish(),
|
|
2017
|
-
completion_tokens:
|
|
2018
|
-
completion_tokens_details:
|
|
2019
|
-
reasoning_tokens:
|
|
2132
|
+
completion_tokens: z8.number(),
|
|
2133
|
+
completion_tokens_details: z8.object({
|
|
2134
|
+
reasoning_tokens: z8.number()
|
|
2020
2135
|
}).nullish(),
|
|
2021
|
-
total_tokens:
|
|
2022
|
-
cost:
|
|
2136
|
+
total_tokens: z8.number(),
|
|
2137
|
+
cost: z8.number().optional()
|
|
2023
2138
|
}).nullish()
|
|
2024
2139
|
}),
|
|
2025
2140
|
OpenRouterErrorResponseSchema
|
|
@@ -2288,18 +2403,43 @@ var OpenRouter = class {
|
|
|
2288
2403
|
}
|
|
2289
2404
|
};
|
|
2290
2405
|
|
|
2406
|
+
// src/version.ts
|
|
2407
|
+
var VERSION = false ? "0.0.0-test" : "1.2.1";
|
|
2408
|
+
|
|
2409
|
+
// src/utils/remove-undefined.ts
|
|
2410
|
+
function removeUndefinedEntries2(record) {
|
|
2411
|
+
return Object.fromEntries(
|
|
2412
|
+
Object.entries(record).filter(([, value]) => value !== null)
|
|
2413
|
+
);
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
// src/utils/with-user-agent-suffix.ts
|
|
2417
|
+
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
2418
|
+
const cleanedHeaders = removeUndefinedEntries2(
|
|
2419
|
+
headers != null ? headers : {}
|
|
2420
|
+
);
|
|
2421
|
+
const currentUserAgentHeader = cleanedHeaders["user-agent"] || "";
|
|
2422
|
+
const newUserAgent = [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ");
|
|
2423
|
+
return __spreadProps(__spreadValues({}, cleanedHeaders), {
|
|
2424
|
+
"user-agent": newUserAgent
|
|
2425
|
+
});
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2291
2428
|
// src/provider.ts
|
|
2292
2429
|
function createOpenRouter(options = {}) {
|
|
2293
2430
|
var _a15, _b, _c;
|
|
2294
2431
|
const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
2295
2432
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2296
|
-
const getHeaders = () =>
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2433
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
2434
|
+
__spreadValues({
|
|
2435
|
+
Authorization: `Bearer ${loadApiKey({
|
|
2436
|
+
apiKey: options.apiKey,
|
|
2437
|
+
environmentVariableName: "OPENROUTER_API_KEY",
|
|
2438
|
+
description: "OpenRouter"
|
|
2439
|
+
})}`
|
|
2440
|
+
}, options.headers),
|
|
2441
|
+
`ai-sdk/openrouter/${VERSION}`
|
|
2442
|
+
);
|
|
2303
2443
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
2304
2444
|
provider: "openrouter.chat",
|
|
2305
2445
|
url: ({ path }) => `${baseURL}${path}`,
|