@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/internal/index.js
CHANGED
|
@@ -873,19 +873,40 @@ function convertUint8ArrayToBase64(array) {
|
|
|
873
873
|
|
|
874
874
|
// src/schemas/reasoning-details.ts
|
|
875
875
|
var import_v4 = require("zod/v4");
|
|
876
|
+
|
|
877
|
+
// src/utils/type-guards.ts
|
|
878
|
+
function isDefinedOrNotNull(value) {
|
|
879
|
+
return value !== null && value !== void 0;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// src/schemas/format.ts
|
|
883
|
+
var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
|
|
884
|
+
ReasoningFormat2["Unknown"] = "unknown";
|
|
885
|
+
ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
|
|
886
|
+
ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
|
|
887
|
+
ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
|
|
888
|
+
return ReasoningFormat2;
|
|
889
|
+
})(ReasoningFormat || {});
|
|
890
|
+
|
|
891
|
+
// src/schemas/reasoning-details.ts
|
|
892
|
+
var CommonReasoningDetailSchema = import_v4.z.object({
|
|
893
|
+
id: import_v4.z.string().nullish(),
|
|
894
|
+
format: import_v4.z.nativeEnum(ReasoningFormat).nullish(),
|
|
895
|
+
index: import_v4.z.number().optional()
|
|
896
|
+
});
|
|
876
897
|
var ReasoningDetailSummarySchema = import_v4.z.object({
|
|
877
898
|
type: import_v4.z.literal("reasoning.summary" /* Summary */),
|
|
878
899
|
summary: import_v4.z.string()
|
|
879
|
-
});
|
|
900
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
880
901
|
var ReasoningDetailEncryptedSchema = import_v4.z.object({
|
|
881
902
|
type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
|
|
882
903
|
data: import_v4.z.string()
|
|
883
|
-
});
|
|
904
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
884
905
|
var ReasoningDetailTextSchema = import_v4.z.object({
|
|
885
906
|
type: import_v4.z.literal("reasoning.text" /* Text */),
|
|
886
907
|
text: import_v4.z.string().nullish(),
|
|
887
908
|
signature: import_v4.z.string().nullish()
|
|
888
|
-
});
|
|
909
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
889
910
|
var ReasoningDetailUnionSchema = import_v4.z.union([
|
|
890
911
|
ReasoningDetailSummarySchema,
|
|
891
912
|
ReasoningDetailEncryptedSchema,
|
|
@@ -896,6 +917,22 @@ var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
|
|
|
896
917
|
import_v4.z.unknown().transform(() => null)
|
|
897
918
|
]);
|
|
898
919
|
var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
920
|
+
var OutputUnionToReasoningDetailsSchema = import_v4.z.union([
|
|
921
|
+
import_v4.z.object({
|
|
922
|
+
delta: import_v4.z.object({
|
|
923
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
924
|
+
})
|
|
925
|
+
}).transform((data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)),
|
|
926
|
+
import_v4.z.object({
|
|
927
|
+
message: import_v4.z.object({
|
|
928
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
929
|
+
})
|
|
930
|
+
}).transform((data) => data.message.reasoning_details.filter(isDefinedOrNotNull)),
|
|
931
|
+
import_v4.z.object({
|
|
932
|
+
text: import_v4.z.string(),
|
|
933
|
+
reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
|
|
934
|
+
}).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
|
|
935
|
+
]);
|
|
899
936
|
|
|
900
937
|
// src/schemas/error-response.ts
|
|
901
938
|
var import_v42 = require("zod/v4");
|
|
@@ -912,6 +949,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
912
949
|
errorToMessage: (data) => data.error.message
|
|
913
950
|
});
|
|
914
951
|
|
|
952
|
+
// src/schemas/provider-metadata.ts
|
|
953
|
+
var import_v43 = require("zod/v4");
|
|
954
|
+
var OpenRouterProviderMetadataSchema = import_v43.z.object({
|
|
955
|
+
provider: import_v43.z.string(),
|
|
956
|
+
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
|
|
957
|
+
usage: import_v43.z.object({
|
|
958
|
+
promptTokens: import_v43.z.number(),
|
|
959
|
+
promptTokensDetails: import_v43.z.object({
|
|
960
|
+
cachedTokens: import_v43.z.number()
|
|
961
|
+
}).optional(),
|
|
962
|
+
completionTokens: import_v43.z.number(),
|
|
963
|
+
completionTokensDetails: import_v43.z.object({
|
|
964
|
+
reasoningTokens: import_v43.z.number()
|
|
965
|
+
}).optional(),
|
|
966
|
+
totalTokens: import_v43.z.number(),
|
|
967
|
+
cost: import_v43.z.number().optional(),
|
|
968
|
+
costDetails: import_v43.z.object({
|
|
969
|
+
upstreamInferenceCost: import_v43.z.number()
|
|
970
|
+
})
|
|
971
|
+
})
|
|
972
|
+
});
|
|
973
|
+
var OpenRouterProviderOptionsSchema = import_v43.z.object({
|
|
974
|
+
openrouter: import_v43.z.object({
|
|
975
|
+
reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional()
|
|
976
|
+
}).optional()
|
|
977
|
+
}).optional();
|
|
978
|
+
|
|
915
979
|
// src/utils/map-finish-reason.ts
|
|
916
980
|
function mapOpenRouterFinishReason(finishReason) {
|
|
917
981
|
switch (finishReason) {
|
|
@@ -961,6 +1025,15 @@ function getFileUrl({
|
|
|
961
1025
|
}
|
|
962
1026
|
return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
|
|
963
1027
|
}
|
|
1028
|
+
function getMediaType(dataUrl, defaultMediaType) {
|
|
1029
|
+
var _a15;
|
|
1030
|
+
const match = dataUrl.match(/^data:([^;]+)/);
|
|
1031
|
+
return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
|
|
1032
|
+
}
|
|
1033
|
+
function getBase64FromDataUrl(dataUrl) {
|
|
1034
|
+
const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
|
|
1035
|
+
return match ? match[1] : dataUrl;
|
|
1036
|
+
}
|
|
964
1037
|
|
|
965
1038
|
// src/chat/convert-to-openrouter-chat-messages.ts
|
|
966
1039
|
function getCacheControl(providerMetadata) {
|
|
@@ -970,7 +1043,7 @@ function getCacheControl(providerMetadata) {
|
|
|
970
1043
|
return (_c = (_b = (_a15 = openrouter == null ? void 0 : openrouter.cacheControl) != null ? _a15 : openrouter == null ? void 0 : openrouter.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
971
1044
|
}
|
|
972
1045
|
function convertToOpenRouterChatMessages(prompt) {
|
|
973
|
-
var _a15, _b, _c;
|
|
1046
|
+
var _a15, _b, _c, _d, _e;
|
|
974
1047
|
const messages = [];
|
|
975
1048
|
for (const { role, content, providerOptions } of prompt) {
|
|
976
1049
|
switch (role) {
|
|
@@ -1001,7 +1074,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1001
1074
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1002
1075
|
const contentParts = content.map(
|
|
1003
1076
|
(part) => {
|
|
1004
|
-
var _a16, _b2, _c2,
|
|
1077
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
1005
1078
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
1006
1079
|
switch (part.type) {
|
|
1007
1080
|
case "text":
|
|
@@ -1027,7 +1100,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1027
1100
|
};
|
|
1028
1101
|
}
|
|
1029
1102
|
const fileName = String(
|
|
1030
|
-
(_f = (
|
|
1103
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1031
1104
|
);
|
|
1032
1105
|
const fileData = getFileUrl({
|
|
1033
1106
|
part,
|
|
@@ -1073,7 +1146,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1073
1146
|
case "assistant": {
|
|
1074
1147
|
let text = "";
|
|
1075
1148
|
let reasoning = "";
|
|
1076
|
-
const reasoningDetails = [];
|
|
1077
1149
|
const toolCalls = [];
|
|
1078
1150
|
for (const part of content) {
|
|
1079
1151
|
switch (part.type) {
|
|
@@ -1094,10 +1166,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1094
1166
|
}
|
|
1095
1167
|
case "reasoning": {
|
|
1096
1168
|
reasoning += part.text;
|
|
1097
|
-
reasoningDetails.push({
|
|
1098
|
-
type: "reasoning.text" /* Text */,
|
|
1099
|
-
text: part.text
|
|
1100
|
-
});
|
|
1101
1169
|
break;
|
|
1102
1170
|
}
|
|
1103
1171
|
case "file":
|
|
@@ -1107,12 +1175,17 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1107
1175
|
}
|
|
1108
1176
|
}
|
|
1109
1177
|
}
|
|
1178
|
+
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(
|
|
1179
|
+
providerOptions
|
|
1180
|
+
);
|
|
1181
|
+
const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
|
|
1110
1182
|
messages.push({
|
|
1111
1183
|
role: "assistant",
|
|
1112
1184
|
content: text,
|
|
1113
1185
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1114
1186
|
reasoning: reasoning || void 0,
|
|
1115
|
-
reasoning_details
|
|
1187
|
+
// Only include reasoning_details if we have the preserved original version
|
|
1188
|
+
reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
|
|
1116
1189
|
cache_control: getCacheControl(providerOptions)
|
|
1117
1190
|
});
|
|
1118
1191
|
break;
|
|
@@ -1124,7 +1197,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1124
1197
|
role: "tool",
|
|
1125
1198
|
tool_call_id: toolResponse.toolCallId,
|
|
1126
1199
|
content: content2,
|
|
1127
|
-
cache_control: (
|
|
1200
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1128
1201
|
});
|
|
1129
1202
|
}
|
|
1130
1203
|
break;
|
|
@@ -1141,15 +1214,15 @@ function getToolResultContent(input) {
|
|
|
1141
1214
|
}
|
|
1142
1215
|
|
|
1143
1216
|
// src/chat/get-tool-choice.ts
|
|
1144
|
-
var
|
|
1145
|
-
var ChatCompletionToolChoiceSchema =
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
type:
|
|
1151
|
-
function:
|
|
1152
|
-
name:
|
|
1217
|
+
var import_v44 = require("zod/v4");
|
|
1218
|
+
var ChatCompletionToolChoiceSchema = import_v44.z.union([
|
|
1219
|
+
import_v44.z.literal("auto"),
|
|
1220
|
+
import_v44.z.literal("none"),
|
|
1221
|
+
import_v44.z.literal("required"),
|
|
1222
|
+
import_v44.z.object({
|
|
1223
|
+
type: import_v44.z.literal("function"),
|
|
1224
|
+
function: import_v44.z.object({
|
|
1225
|
+
name: import_v44.z.string()
|
|
1153
1226
|
})
|
|
1154
1227
|
})
|
|
1155
1228
|
]);
|
|
@@ -1173,126 +1246,144 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1173
1246
|
}
|
|
1174
1247
|
|
|
1175
1248
|
// src/chat/schemas.ts
|
|
1176
|
-
var
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1249
|
+
var import_v46 = require("zod/v4");
|
|
1250
|
+
|
|
1251
|
+
// src/schemas/image.ts
|
|
1252
|
+
var import_v45 = require("zod/v4");
|
|
1253
|
+
var ImageResponseSchema = import_v45.z.object({
|
|
1254
|
+
type: import_v45.z.literal("image_url"),
|
|
1255
|
+
image_url: import_v45.z.object({
|
|
1256
|
+
url: import_v45.z.string()
|
|
1257
|
+
})
|
|
1258
|
+
});
|
|
1259
|
+
var ImageResponseWithUnknownSchema = import_v45.z.union([
|
|
1260
|
+
ImageResponseSchema,
|
|
1261
|
+
import_v45.z.unknown().transform(() => null)
|
|
1262
|
+
]);
|
|
1263
|
+
var ImageResponseArraySchema = import_v45.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1264
|
+
|
|
1265
|
+
// src/chat/schemas.ts
|
|
1266
|
+
var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
|
|
1267
|
+
id: import_v46.z.string().optional(),
|
|
1268
|
+
model: import_v46.z.string().optional(),
|
|
1269
|
+
provider: import_v46.z.string().optional(),
|
|
1270
|
+
usage: import_v46.z.object({
|
|
1271
|
+
prompt_tokens: import_v46.z.number(),
|
|
1272
|
+
prompt_tokens_details: import_v46.z.object({
|
|
1273
|
+
cached_tokens: import_v46.z.number()
|
|
1185
1274
|
}).nullish(),
|
|
1186
|
-
completion_tokens:
|
|
1187
|
-
completion_tokens_details:
|
|
1188
|
-
reasoning_tokens:
|
|
1275
|
+
completion_tokens: import_v46.z.number(),
|
|
1276
|
+
completion_tokens_details: import_v46.z.object({
|
|
1277
|
+
reasoning_tokens: import_v46.z.number()
|
|
1189
1278
|
}).nullish(),
|
|
1190
|
-
total_tokens:
|
|
1191
|
-
cost:
|
|
1192
|
-
cost_details:
|
|
1193
|
-
upstream_inference_cost:
|
|
1279
|
+
total_tokens: import_v46.z.number(),
|
|
1280
|
+
cost: import_v46.z.number().optional(),
|
|
1281
|
+
cost_details: import_v46.z.object({
|
|
1282
|
+
upstream_inference_cost: import_v46.z.number().nullish()
|
|
1194
1283
|
}).nullish()
|
|
1195
1284
|
}).nullish()
|
|
1196
1285
|
});
|
|
1197
1286
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1198
|
-
choices:
|
|
1199
|
-
|
|
1200
|
-
message:
|
|
1201
|
-
role:
|
|
1202
|
-
content:
|
|
1203
|
-
reasoning:
|
|
1287
|
+
choices: import_v46.z.array(
|
|
1288
|
+
import_v46.z.object({
|
|
1289
|
+
message: import_v46.z.object({
|
|
1290
|
+
role: import_v46.z.literal("assistant"),
|
|
1291
|
+
content: import_v46.z.string().nullable().optional(),
|
|
1292
|
+
reasoning: import_v46.z.string().nullable().optional(),
|
|
1204
1293
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1294
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1295
|
+
tool_calls: import_v46.z.array(
|
|
1296
|
+
import_v46.z.object({
|
|
1297
|
+
id: import_v46.z.string().optional().nullable(),
|
|
1298
|
+
type: import_v46.z.literal("function"),
|
|
1299
|
+
function: import_v46.z.object({
|
|
1300
|
+
name: import_v46.z.string(),
|
|
1301
|
+
arguments: import_v46.z.string()
|
|
1212
1302
|
})
|
|
1213
1303
|
})
|
|
1214
1304
|
).optional(),
|
|
1215
|
-
annotations:
|
|
1216
|
-
|
|
1217
|
-
type:
|
|
1218
|
-
url_citation:
|
|
1219
|
-
end_index:
|
|
1220
|
-
start_index:
|
|
1221
|
-
title:
|
|
1222
|
-
url:
|
|
1223
|
-
content:
|
|
1305
|
+
annotations: import_v46.z.array(
|
|
1306
|
+
import_v46.z.object({
|
|
1307
|
+
type: import_v46.z.enum(["url_citation"]),
|
|
1308
|
+
url_citation: import_v46.z.object({
|
|
1309
|
+
end_index: import_v46.z.number(),
|
|
1310
|
+
start_index: import_v46.z.number(),
|
|
1311
|
+
title: import_v46.z.string(),
|
|
1312
|
+
url: import_v46.z.string(),
|
|
1313
|
+
content: import_v46.z.string().optional()
|
|
1224
1314
|
})
|
|
1225
1315
|
})
|
|
1226
1316
|
).nullish()
|
|
1227
1317
|
}),
|
|
1228
|
-
index:
|
|
1229
|
-
logprobs:
|
|
1230
|
-
content:
|
|
1231
|
-
|
|
1232
|
-
token:
|
|
1233
|
-
logprob:
|
|
1234
|
-
top_logprobs:
|
|
1235
|
-
|
|
1236
|
-
token:
|
|
1237
|
-
logprob:
|
|
1318
|
+
index: import_v46.z.number().nullish(),
|
|
1319
|
+
logprobs: import_v46.z.object({
|
|
1320
|
+
content: import_v46.z.array(
|
|
1321
|
+
import_v46.z.object({
|
|
1322
|
+
token: import_v46.z.string(),
|
|
1323
|
+
logprob: import_v46.z.number(),
|
|
1324
|
+
top_logprobs: import_v46.z.array(
|
|
1325
|
+
import_v46.z.object({
|
|
1326
|
+
token: import_v46.z.string(),
|
|
1327
|
+
logprob: import_v46.z.number()
|
|
1238
1328
|
})
|
|
1239
1329
|
)
|
|
1240
1330
|
})
|
|
1241
1331
|
).nullable()
|
|
1242
1332
|
}).nullable().optional(),
|
|
1243
|
-
finish_reason:
|
|
1333
|
+
finish_reason: import_v46.z.string().optional().nullable()
|
|
1244
1334
|
})
|
|
1245
1335
|
)
|
|
1246
1336
|
});
|
|
1247
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1337
|
+
var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
|
|
1248
1338
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1249
|
-
choices:
|
|
1250
|
-
|
|
1251
|
-
delta:
|
|
1252
|
-
role:
|
|
1253
|
-
content:
|
|
1254
|
-
reasoning:
|
|
1339
|
+
choices: import_v46.z.array(
|
|
1340
|
+
import_v46.z.object({
|
|
1341
|
+
delta: import_v46.z.object({
|
|
1342
|
+
role: import_v46.z.enum(["assistant"]).optional(),
|
|
1343
|
+
content: import_v46.z.string().nullish(),
|
|
1344
|
+
reasoning: import_v46.z.string().nullish().optional(),
|
|
1255
1345
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1346
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1347
|
+
tool_calls: import_v46.z.array(
|
|
1348
|
+
import_v46.z.object({
|
|
1349
|
+
index: import_v46.z.number().nullish(),
|
|
1350
|
+
id: import_v46.z.string().nullish(),
|
|
1351
|
+
type: import_v46.z.literal("function").optional(),
|
|
1352
|
+
function: import_v46.z.object({
|
|
1353
|
+
name: import_v46.z.string().nullish(),
|
|
1354
|
+
arguments: import_v46.z.string().nullish()
|
|
1264
1355
|
})
|
|
1265
1356
|
})
|
|
1266
1357
|
).nullish(),
|
|
1267
|
-
annotations:
|
|
1268
|
-
|
|
1269
|
-
type:
|
|
1270
|
-
url_citation:
|
|
1271
|
-
end_index:
|
|
1272
|
-
start_index:
|
|
1273
|
-
title:
|
|
1274
|
-
url:
|
|
1275
|
-
content:
|
|
1358
|
+
annotations: import_v46.z.array(
|
|
1359
|
+
import_v46.z.object({
|
|
1360
|
+
type: import_v46.z.enum(["url_citation"]),
|
|
1361
|
+
url_citation: import_v46.z.object({
|
|
1362
|
+
end_index: import_v46.z.number(),
|
|
1363
|
+
start_index: import_v46.z.number(),
|
|
1364
|
+
title: import_v46.z.string(),
|
|
1365
|
+
url: import_v46.z.string(),
|
|
1366
|
+
content: import_v46.z.string().optional()
|
|
1276
1367
|
})
|
|
1277
1368
|
})
|
|
1278
1369
|
).nullish()
|
|
1279
1370
|
}).nullish(),
|
|
1280
|
-
logprobs:
|
|
1281
|
-
content:
|
|
1282
|
-
|
|
1283
|
-
token:
|
|
1284
|
-
logprob:
|
|
1285
|
-
top_logprobs:
|
|
1286
|
-
|
|
1287
|
-
token:
|
|
1288
|
-
logprob:
|
|
1371
|
+
logprobs: import_v46.z.object({
|
|
1372
|
+
content: import_v46.z.array(
|
|
1373
|
+
import_v46.z.object({
|
|
1374
|
+
token: import_v46.z.string(),
|
|
1375
|
+
logprob: import_v46.z.number(),
|
|
1376
|
+
top_logprobs: import_v46.z.array(
|
|
1377
|
+
import_v46.z.object({
|
|
1378
|
+
token: import_v46.z.string(),
|
|
1379
|
+
logprob: import_v46.z.number()
|
|
1289
1380
|
})
|
|
1290
1381
|
)
|
|
1291
1382
|
})
|
|
1292
1383
|
).nullable()
|
|
1293
1384
|
}).nullish(),
|
|
1294
|
-
finish_reason:
|
|
1295
|
-
index:
|
|
1385
|
+
finish_reason: import_v46.z.string().nullable().optional(),
|
|
1386
|
+
index: import_v46.z.number().nullish()
|
|
1296
1387
|
})
|
|
1297
1388
|
)
|
|
1298
1389
|
}),
|
|
@@ -1395,7 +1486,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1395
1486
|
return baseArgs;
|
|
1396
1487
|
}
|
|
1397
1488
|
async doGenerate(options) {
|
|
1398
|
-
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;
|
|
1489
|
+
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;
|
|
1399
1490
|
const providerOptions = options.providerOptions || {};
|
|
1400
1491
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1401
1492
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1489,6 +1580,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1489
1580
|
});
|
|
1490
1581
|
}
|
|
1491
1582
|
}
|
|
1583
|
+
if (choice.message.images) {
|
|
1584
|
+
for (const image of choice.message.images) {
|
|
1585
|
+
content.push({
|
|
1586
|
+
type: "file",
|
|
1587
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1588
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1589
|
+
});
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1492
1592
|
if (choice.message.annotations) {
|
|
1493
1593
|
for (const annotation of choice.message.annotations) {
|
|
1494
1594
|
if (annotation.type === "url_citation") {
|
|
@@ -1513,24 +1613,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1513
1613
|
usage: usageInfo,
|
|
1514
1614
|
warnings: [],
|
|
1515
1615
|
providerMetadata: {
|
|
1516
|
-
openrouter: {
|
|
1616
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1517
1617
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1618
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1518
1619
|
usage: {
|
|
1519
|
-
promptTokens: (
|
|
1520
|
-
completionTokens: (
|
|
1521
|
-
totalTokens: (
|
|
1522
|
-
cost: (
|
|
1620
|
+
promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
|
|
1621
|
+
completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
|
|
1622
|
+
totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
|
|
1623
|
+
cost: (_p = response.usage) == null ? void 0 : _p.cost,
|
|
1523
1624
|
promptTokensDetails: {
|
|
1524
|
-
cachedTokens: (
|
|
1625
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1525
1626
|
},
|
|
1526
1627
|
completionTokensDetails: {
|
|
1527
|
-
reasoningTokens: (
|
|
1628
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1528
1629
|
},
|
|
1529
1630
|
costDetails: {
|
|
1530
|
-
upstreamInferenceCost: (
|
|
1631
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1531
1632
|
}
|
|
1532
1633
|
}
|
|
1533
|
-
}
|
|
1634
|
+
})
|
|
1534
1635
|
},
|
|
1535
1636
|
request: { body: args },
|
|
1536
1637
|
response: {
|
|
@@ -1575,6 +1676,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1575
1676
|
cachedInputTokens: Number.NaN
|
|
1576
1677
|
};
|
|
1577
1678
|
const openrouterUsage = {};
|
|
1679
|
+
const accumulatedReasoningDetails = [];
|
|
1578
1680
|
let textStarted = false;
|
|
1579
1681
|
let reasoningStarted = false;
|
|
1580
1682
|
let textId;
|
|
@@ -1660,6 +1762,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1660
1762
|
});
|
|
1661
1763
|
};
|
|
1662
1764
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1765
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1663
1766
|
for (const detail of delta.reasoning_details) {
|
|
1664
1767
|
switch (detail.type) {
|
|
1665
1768
|
case "reasoning.text" /* Text */: {
|
|
@@ -1822,6 +1925,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1822
1925
|
}
|
|
1823
1926
|
}
|
|
1824
1927
|
}
|
|
1928
|
+
if (delta.images != null) {
|
|
1929
|
+
for (const image of delta.images) {
|
|
1930
|
+
controller.enqueue({
|
|
1931
|
+
type: "file",
|
|
1932
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1933
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1934
|
+
});
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1825
1937
|
},
|
|
1826
1938
|
flush(controller) {
|
|
1827
1939
|
var _a16;
|
|
@@ -1857,6 +1969,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1857
1969
|
if (provider !== void 0) {
|
|
1858
1970
|
openrouterMetadata.provider = provider;
|
|
1859
1971
|
}
|
|
1972
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
1973
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
1974
|
+
}
|
|
1860
1975
|
controller.enqueue({
|
|
1861
1976
|
type: "finish",
|
|
1862
1977
|
finishReason,
|
|
@@ -1979,36 +2094,36 @@ ${assistantMessage}
|
|
|
1979
2094
|
}
|
|
1980
2095
|
|
|
1981
2096
|
// src/completion/schemas.ts
|
|
1982
|
-
var
|
|
1983
|
-
var OpenRouterCompletionChunkSchema =
|
|
1984
|
-
|
|
1985
|
-
id:
|
|
1986
|
-
model:
|
|
1987
|
-
choices:
|
|
1988
|
-
|
|
1989
|
-
text:
|
|
1990
|
-
reasoning:
|
|
2097
|
+
var import_v47 = require("zod/v4");
|
|
2098
|
+
var OpenRouterCompletionChunkSchema = import_v47.z.union([
|
|
2099
|
+
import_v47.z.object({
|
|
2100
|
+
id: import_v47.z.string().optional(),
|
|
2101
|
+
model: import_v47.z.string().optional(),
|
|
2102
|
+
choices: import_v47.z.array(
|
|
2103
|
+
import_v47.z.object({
|
|
2104
|
+
text: import_v47.z.string(),
|
|
2105
|
+
reasoning: import_v47.z.string().nullish().optional(),
|
|
1991
2106
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1992
|
-
finish_reason:
|
|
1993
|
-
index:
|
|
1994
|
-
logprobs:
|
|
1995
|
-
tokens:
|
|
1996
|
-
token_logprobs:
|
|
1997
|
-
top_logprobs:
|
|
2107
|
+
finish_reason: import_v47.z.string().nullish(),
|
|
2108
|
+
index: import_v47.z.number().nullish(),
|
|
2109
|
+
logprobs: import_v47.z.object({
|
|
2110
|
+
tokens: import_v47.z.array(import_v47.z.string()),
|
|
2111
|
+
token_logprobs: import_v47.z.array(import_v47.z.number()),
|
|
2112
|
+
top_logprobs: import_v47.z.array(import_v47.z.record(import_v47.z.string(), import_v47.z.number())).nullable()
|
|
1998
2113
|
}).nullable().optional()
|
|
1999
2114
|
})
|
|
2000
2115
|
),
|
|
2001
|
-
usage:
|
|
2002
|
-
prompt_tokens:
|
|
2003
|
-
prompt_tokens_details:
|
|
2004
|
-
cached_tokens:
|
|
2116
|
+
usage: import_v47.z.object({
|
|
2117
|
+
prompt_tokens: import_v47.z.number(),
|
|
2118
|
+
prompt_tokens_details: import_v47.z.object({
|
|
2119
|
+
cached_tokens: import_v47.z.number()
|
|
2005
2120
|
}).nullish(),
|
|
2006
|
-
completion_tokens:
|
|
2007
|
-
completion_tokens_details:
|
|
2008
|
-
reasoning_tokens:
|
|
2121
|
+
completion_tokens: import_v47.z.number(),
|
|
2122
|
+
completion_tokens_details: import_v47.z.object({
|
|
2123
|
+
reasoning_tokens: import_v47.z.number()
|
|
2009
2124
|
}).nullish(),
|
|
2010
|
-
total_tokens:
|
|
2011
|
-
cost:
|
|
2125
|
+
total_tokens: import_v47.z.number(),
|
|
2126
|
+
cost: import_v47.z.number().optional()
|
|
2012
2127
|
}).nullish()
|
|
2013
2128
|
}),
|
|
2014
2129
|
OpenRouterErrorResponseSchema
|