@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.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) {
|
|
@@ -990,7 +1054,7 @@ function getCacheControl(providerMetadata) {
|
|
|
990
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;
|
|
991
1055
|
}
|
|
992
1056
|
function convertToOpenRouterChatMessages(prompt) {
|
|
993
|
-
var _a15, _b, _c;
|
|
1057
|
+
var _a15, _b, _c, _d, _e;
|
|
994
1058
|
const messages = [];
|
|
995
1059
|
for (const { role, content, providerOptions } of prompt) {
|
|
996
1060
|
switch (role) {
|
|
@@ -1021,7 +1085,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1021
1085
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1022
1086
|
const contentParts = content.map(
|
|
1023
1087
|
(part) => {
|
|
1024
|
-
var _a16, _b2, _c2,
|
|
1088
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
1025
1089
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
1026
1090
|
switch (part.type) {
|
|
1027
1091
|
case "text":
|
|
@@ -1047,7 +1111,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1047
1111
|
};
|
|
1048
1112
|
}
|
|
1049
1113
|
const fileName = String(
|
|
1050
|
-
(_f = (
|
|
1114
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1051
1115
|
);
|
|
1052
1116
|
const fileData = getFileUrl({
|
|
1053
1117
|
part,
|
|
@@ -1093,7 +1157,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1093
1157
|
case "assistant": {
|
|
1094
1158
|
let text = "";
|
|
1095
1159
|
let reasoning = "";
|
|
1096
|
-
const reasoningDetails = [];
|
|
1097
1160
|
const toolCalls = [];
|
|
1098
1161
|
for (const part of content) {
|
|
1099
1162
|
switch (part.type) {
|
|
@@ -1114,10 +1177,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1114
1177
|
}
|
|
1115
1178
|
case "reasoning": {
|
|
1116
1179
|
reasoning += part.text;
|
|
1117
|
-
reasoningDetails.push({
|
|
1118
|
-
type: "reasoning.text" /* Text */,
|
|
1119
|
-
text: part.text
|
|
1120
|
-
});
|
|
1121
1180
|
break;
|
|
1122
1181
|
}
|
|
1123
1182
|
case "file":
|
|
@@ -1127,12 +1186,17 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1127
1186
|
}
|
|
1128
1187
|
}
|
|
1129
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;
|
|
1130
1193
|
messages.push({
|
|
1131
1194
|
role: "assistant",
|
|
1132
1195
|
content: text,
|
|
1133
1196
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1134
1197
|
reasoning: reasoning || void 0,
|
|
1135
|
-
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,
|
|
1136
1200
|
cache_control: getCacheControl(providerOptions)
|
|
1137
1201
|
});
|
|
1138
1202
|
break;
|
|
@@ -1144,7 +1208,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1144
1208
|
role: "tool",
|
|
1145
1209
|
tool_call_id: toolResponse.toolCallId,
|
|
1146
1210
|
content: content2,
|
|
1147
|
-
cache_control: (
|
|
1211
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1148
1212
|
});
|
|
1149
1213
|
}
|
|
1150
1214
|
break;
|
|
@@ -1161,15 +1225,15 @@ function getToolResultContent(input) {
|
|
|
1161
1225
|
}
|
|
1162
1226
|
|
|
1163
1227
|
// src/chat/get-tool-choice.ts
|
|
1164
|
-
import { z as
|
|
1165
|
-
var ChatCompletionToolChoiceSchema =
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
type:
|
|
1171
|
-
function:
|
|
1172
|
-
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()
|
|
1173
1237
|
})
|
|
1174
1238
|
})
|
|
1175
1239
|
]);
|
|
@@ -1193,144 +1257,144 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1193
1257
|
}
|
|
1194
1258
|
|
|
1195
1259
|
// src/chat/schemas.ts
|
|
1196
|
-
import { z as
|
|
1260
|
+
import { z as z7 } from "zod/v4";
|
|
1197
1261
|
|
|
1198
1262
|
// src/schemas/image.ts
|
|
1199
|
-
import { z as
|
|
1200
|
-
var ImageResponseSchema =
|
|
1201
|
-
type:
|
|
1202
|
-
image_url:
|
|
1203
|
-
url:
|
|
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()
|
|
1204
1268
|
})
|
|
1205
1269
|
});
|
|
1206
|
-
var ImageResponseWithUnknownSchema =
|
|
1270
|
+
var ImageResponseWithUnknownSchema = z6.union([
|
|
1207
1271
|
ImageResponseSchema,
|
|
1208
|
-
|
|
1272
|
+
z6.unknown().transform(() => null)
|
|
1209
1273
|
]);
|
|
1210
|
-
var ImageResponseArraySchema =
|
|
1274
|
+
var ImageResponseArraySchema = z6.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1211
1275
|
|
|
1212
1276
|
// src/chat/schemas.ts
|
|
1213
|
-
var OpenRouterChatCompletionBaseResponseSchema =
|
|
1214
|
-
id:
|
|
1215
|
-
model:
|
|
1216
|
-
provider:
|
|
1217
|
-
usage:
|
|
1218
|
-
prompt_tokens:
|
|
1219
|
-
prompt_tokens_details:
|
|
1220
|
-
cached_tokens:
|
|
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()
|
|
1221
1285
|
}).nullish(),
|
|
1222
|
-
completion_tokens:
|
|
1223
|
-
completion_tokens_details:
|
|
1224
|
-
reasoning_tokens:
|
|
1286
|
+
completion_tokens: z7.number(),
|
|
1287
|
+
completion_tokens_details: z7.object({
|
|
1288
|
+
reasoning_tokens: z7.number()
|
|
1225
1289
|
}).nullish(),
|
|
1226
|
-
total_tokens:
|
|
1227
|
-
cost:
|
|
1228
|
-
cost_details:
|
|
1229
|
-
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()
|
|
1230
1294
|
}).nullish()
|
|
1231
1295
|
}).nullish()
|
|
1232
1296
|
});
|
|
1233
1297
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1234
|
-
choices:
|
|
1235
|
-
|
|
1236
|
-
message:
|
|
1237
|
-
role:
|
|
1238
|
-
content:
|
|
1239
|
-
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(),
|
|
1240
1304
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1241
1305
|
images: ImageResponseArraySchema.nullish(),
|
|
1242
|
-
tool_calls:
|
|
1243
|
-
|
|
1244
|
-
id:
|
|
1245
|
-
type:
|
|
1246
|
-
function:
|
|
1247
|
-
name:
|
|
1248
|
-
arguments:
|
|
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()
|
|
1249
1313
|
})
|
|
1250
1314
|
})
|
|
1251
1315
|
).optional(),
|
|
1252
|
-
annotations:
|
|
1253
|
-
|
|
1254
|
-
type:
|
|
1255
|
-
url_citation:
|
|
1256
|
-
end_index:
|
|
1257
|
-
start_index:
|
|
1258
|
-
title:
|
|
1259
|
-
url:
|
|
1260
|
-
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()
|
|
1261
1325
|
})
|
|
1262
1326
|
})
|
|
1263
1327
|
).nullish()
|
|
1264
1328
|
}),
|
|
1265
|
-
index:
|
|
1266
|
-
logprobs:
|
|
1267
|
-
content:
|
|
1268
|
-
|
|
1269
|
-
token:
|
|
1270
|
-
logprob:
|
|
1271
|
-
top_logprobs:
|
|
1272
|
-
|
|
1273
|
-
token:
|
|
1274
|
-
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()
|
|
1275
1339
|
})
|
|
1276
1340
|
)
|
|
1277
1341
|
})
|
|
1278
1342
|
).nullable()
|
|
1279
1343
|
}).nullable().optional(),
|
|
1280
|
-
finish_reason:
|
|
1344
|
+
finish_reason: z7.string().optional().nullable()
|
|
1281
1345
|
})
|
|
1282
1346
|
)
|
|
1283
1347
|
});
|
|
1284
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1348
|
+
var OpenRouterStreamChatCompletionChunkSchema = z7.union([
|
|
1285
1349
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1286
|
-
choices:
|
|
1287
|
-
|
|
1288
|
-
delta:
|
|
1289
|
-
role:
|
|
1290
|
-
content:
|
|
1291
|
-
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(),
|
|
1292
1356
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1293
1357
|
images: ImageResponseArraySchema.nullish(),
|
|
1294
|
-
tool_calls:
|
|
1295
|
-
|
|
1296
|
-
index:
|
|
1297
|
-
id:
|
|
1298
|
-
type:
|
|
1299
|
-
function:
|
|
1300
|
-
name:
|
|
1301
|
-
arguments:
|
|
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()
|
|
1302
1366
|
})
|
|
1303
1367
|
})
|
|
1304
1368
|
).nullish(),
|
|
1305
|
-
annotations:
|
|
1306
|
-
|
|
1307
|
-
type:
|
|
1308
|
-
url_citation:
|
|
1309
|
-
end_index:
|
|
1310
|
-
start_index:
|
|
1311
|
-
title:
|
|
1312
|
-
url:
|
|
1313
|
-
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()
|
|
1314
1378
|
})
|
|
1315
1379
|
})
|
|
1316
1380
|
).nullish()
|
|
1317
1381
|
}).nullish(),
|
|
1318
|
-
logprobs:
|
|
1319
|
-
content:
|
|
1320
|
-
|
|
1321
|
-
token:
|
|
1322
|
-
logprob:
|
|
1323
|
-
top_logprobs:
|
|
1324
|
-
|
|
1325
|
-
token:
|
|
1326
|
-
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()
|
|
1327
1391
|
})
|
|
1328
1392
|
)
|
|
1329
1393
|
})
|
|
1330
1394
|
).nullable()
|
|
1331
1395
|
}).nullish(),
|
|
1332
|
-
finish_reason:
|
|
1333
|
-
index:
|
|
1396
|
+
finish_reason: z7.string().nullable().optional(),
|
|
1397
|
+
index: z7.number().nullish()
|
|
1334
1398
|
})
|
|
1335
1399
|
)
|
|
1336
1400
|
}),
|
|
@@ -1433,7 +1497,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1433
1497
|
return baseArgs;
|
|
1434
1498
|
}
|
|
1435
1499
|
async doGenerate(options) {
|
|
1436
|
-
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;
|
|
1437
1501
|
const providerOptions = options.providerOptions || {};
|
|
1438
1502
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1439
1503
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1560,24 +1624,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1560
1624
|
usage: usageInfo,
|
|
1561
1625
|
warnings: [],
|
|
1562
1626
|
providerMetadata: {
|
|
1563
|
-
openrouter: {
|
|
1627
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1564
1628
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1629
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1565
1630
|
usage: {
|
|
1566
|
-
promptTokens: (
|
|
1567
|
-
completionTokens: (
|
|
1568
|
-
totalTokens: (
|
|
1569
|
-
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,
|
|
1570
1635
|
promptTokensDetails: {
|
|
1571
|
-
cachedTokens: (
|
|
1636
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1572
1637
|
},
|
|
1573
1638
|
completionTokensDetails: {
|
|
1574
|
-
reasoningTokens: (
|
|
1639
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1575
1640
|
},
|
|
1576
1641
|
costDetails: {
|
|
1577
|
-
upstreamInferenceCost: (
|
|
1642
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1578
1643
|
}
|
|
1579
1644
|
}
|
|
1580
|
-
}
|
|
1645
|
+
})
|
|
1581
1646
|
},
|
|
1582
1647
|
request: { body: args },
|
|
1583
1648
|
response: {
|
|
@@ -1622,6 +1687,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1622
1687
|
cachedInputTokens: Number.NaN
|
|
1623
1688
|
};
|
|
1624
1689
|
const openrouterUsage = {};
|
|
1690
|
+
const accumulatedReasoningDetails = [];
|
|
1625
1691
|
let textStarted = false;
|
|
1626
1692
|
let reasoningStarted = false;
|
|
1627
1693
|
let textId;
|
|
@@ -1707,6 +1773,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1707
1773
|
});
|
|
1708
1774
|
};
|
|
1709
1775
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1776
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1710
1777
|
for (const detail of delta.reasoning_details) {
|
|
1711
1778
|
switch (detail.type) {
|
|
1712
1779
|
case "reasoning.text" /* Text */: {
|
|
@@ -1913,6 +1980,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1913
1980
|
if (provider !== void 0) {
|
|
1914
1981
|
openrouterMetadata.provider = provider;
|
|
1915
1982
|
}
|
|
1983
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
1984
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
1985
|
+
}
|
|
1916
1986
|
controller.enqueue({
|
|
1917
1987
|
type: "finish",
|
|
1918
1988
|
finishReason,
|
|
@@ -2035,36 +2105,36 @@ ${assistantMessage}
|
|
|
2035
2105
|
}
|
|
2036
2106
|
|
|
2037
2107
|
// src/completion/schemas.ts
|
|
2038
|
-
import { z as
|
|
2039
|
-
var OpenRouterCompletionChunkSchema =
|
|
2040
|
-
|
|
2041
|
-
id:
|
|
2042
|
-
model:
|
|
2043
|
-
choices:
|
|
2044
|
-
|
|
2045
|
-
text:
|
|
2046
|
-
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(),
|
|
2047
2117
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2048
|
-
finish_reason:
|
|
2049
|
-
index:
|
|
2050
|
-
logprobs:
|
|
2051
|
-
tokens:
|
|
2052
|
-
token_logprobs:
|
|
2053
|
-
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()
|
|
2054
2124
|
}).nullable().optional()
|
|
2055
2125
|
})
|
|
2056
2126
|
),
|
|
2057
|
-
usage:
|
|
2058
|
-
prompt_tokens:
|
|
2059
|
-
prompt_tokens_details:
|
|
2060
|
-
cached_tokens:
|
|
2127
|
+
usage: z8.object({
|
|
2128
|
+
prompt_tokens: z8.number(),
|
|
2129
|
+
prompt_tokens_details: z8.object({
|
|
2130
|
+
cached_tokens: z8.number()
|
|
2061
2131
|
}).nullish(),
|
|
2062
|
-
completion_tokens:
|
|
2063
|
-
completion_tokens_details:
|
|
2064
|
-
reasoning_tokens:
|
|
2132
|
+
completion_tokens: z8.number(),
|
|
2133
|
+
completion_tokens_details: z8.object({
|
|
2134
|
+
reasoning_tokens: z8.number()
|
|
2065
2135
|
}).nullish(),
|
|
2066
|
-
total_tokens:
|
|
2067
|
-
cost:
|
|
2136
|
+
total_tokens: z8.number(),
|
|
2137
|
+
cost: z8.number().optional()
|
|
2068
2138
|
}).nullish()
|
|
2069
2139
|
}),
|
|
2070
2140
|
OpenRouterErrorResponseSchema
|
|
@@ -2333,18 +2403,43 @@ var OpenRouter = class {
|
|
|
2333
2403
|
}
|
|
2334
2404
|
};
|
|
2335
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
|
+
|
|
2336
2428
|
// src/provider.ts
|
|
2337
2429
|
function createOpenRouter(options = {}) {
|
|
2338
2430
|
var _a15, _b, _c;
|
|
2339
2431
|
const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
2340
2432
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2341
|
-
const getHeaders = () =>
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
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
|
+
);
|
|
2348
2443
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
2349
2444
|
provider: "openrouter.chat",
|
|
2350
2445
|
url: ({ path }) => `${baseURL}${path}`,
|