@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.mjs
CHANGED
|
@@ -839,19 +839,40 @@ function convertUint8ArrayToBase64(array) {
|
|
|
839
839
|
|
|
840
840
|
// src/schemas/reasoning-details.ts
|
|
841
841
|
import { z } from "zod/v4";
|
|
842
|
+
|
|
843
|
+
// src/utils/type-guards.ts
|
|
844
|
+
function isDefinedOrNotNull(value) {
|
|
845
|
+
return value !== null && value !== void 0;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// src/schemas/format.ts
|
|
849
|
+
var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
|
|
850
|
+
ReasoningFormat2["Unknown"] = "unknown";
|
|
851
|
+
ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
|
|
852
|
+
ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
|
|
853
|
+
ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
|
|
854
|
+
return ReasoningFormat2;
|
|
855
|
+
})(ReasoningFormat || {});
|
|
856
|
+
|
|
857
|
+
// src/schemas/reasoning-details.ts
|
|
858
|
+
var CommonReasoningDetailSchema = z.object({
|
|
859
|
+
id: z.string().nullish(),
|
|
860
|
+
format: z.nativeEnum(ReasoningFormat).nullish(),
|
|
861
|
+
index: z.number().optional()
|
|
862
|
+
});
|
|
842
863
|
var ReasoningDetailSummarySchema = z.object({
|
|
843
864
|
type: z.literal("reasoning.summary" /* Summary */),
|
|
844
865
|
summary: z.string()
|
|
845
|
-
});
|
|
866
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
846
867
|
var ReasoningDetailEncryptedSchema = z.object({
|
|
847
868
|
type: z.literal("reasoning.encrypted" /* Encrypted */),
|
|
848
869
|
data: z.string()
|
|
849
|
-
});
|
|
870
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
850
871
|
var ReasoningDetailTextSchema = z.object({
|
|
851
872
|
type: z.literal("reasoning.text" /* Text */),
|
|
852
873
|
text: z.string().nullish(),
|
|
853
874
|
signature: z.string().nullish()
|
|
854
|
-
});
|
|
875
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
855
876
|
var ReasoningDetailUnionSchema = z.union([
|
|
856
877
|
ReasoningDetailSummarySchema,
|
|
857
878
|
ReasoningDetailEncryptedSchema,
|
|
@@ -862,6 +883,22 @@ var ReasoningDetailsWithUnknownSchema = z.union([
|
|
|
862
883
|
z.unknown().transform(() => null)
|
|
863
884
|
]);
|
|
864
885
|
var ReasoningDetailArraySchema = z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
886
|
+
var OutputUnionToReasoningDetailsSchema = z.union([
|
|
887
|
+
z.object({
|
|
888
|
+
delta: z.object({
|
|
889
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
890
|
+
})
|
|
891
|
+
}).transform((data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)),
|
|
892
|
+
z.object({
|
|
893
|
+
message: z.object({
|
|
894
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
895
|
+
})
|
|
896
|
+
}).transform((data) => data.message.reasoning_details.filter(isDefinedOrNotNull)),
|
|
897
|
+
z.object({
|
|
898
|
+
text: z.string(),
|
|
899
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
900
|
+
}).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
|
|
901
|
+
]);
|
|
865
902
|
|
|
866
903
|
// src/schemas/error-response.ts
|
|
867
904
|
import { z as z2 } from "zod/v4";
|
|
@@ -878,6 +915,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
878
915
|
errorToMessage: (data) => data.error.message
|
|
879
916
|
});
|
|
880
917
|
|
|
918
|
+
// src/schemas/provider-metadata.ts
|
|
919
|
+
import { z as z3 } from "zod/v4";
|
|
920
|
+
var OpenRouterProviderMetadataSchema = z3.object({
|
|
921
|
+
provider: z3.string(),
|
|
922
|
+
reasoning_details: z3.array(ReasoningDetailUnionSchema).optional(),
|
|
923
|
+
usage: z3.object({
|
|
924
|
+
promptTokens: z3.number(),
|
|
925
|
+
promptTokensDetails: z3.object({
|
|
926
|
+
cachedTokens: z3.number()
|
|
927
|
+
}).optional(),
|
|
928
|
+
completionTokens: z3.number(),
|
|
929
|
+
completionTokensDetails: z3.object({
|
|
930
|
+
reasoningTokens: z3.number()
|
|
931
|
+
}).optional(),
|
|
932
|
+
totalTokens: z3.number(),
|
|
933
|
+
cost: z3.number().optional(),
|
|
934
|
+
costDetails: z3.object({
|
|
935
|
+
upstreamInferenceCost: z3.number()
|
|
936
|
+
})
|
|
937
|
+
})
|
|
938
|
+
});
|
|
939
|
+
var OpenRouterProviderOptionsSchema = z3.object({
|
|
940
|
+
openrouter: z3.object({
|
|
941
|
+
reasoning_details: z3.array(ReasoningDetailUnionSchema).optional()
|
|
942
|
+
}).optional()
|
|
943
|
+
}).optional();
|
|
944
|
+
|
|
881
945
|
// src/utils/map-finish-reason.ts
|
|
882
946
|
function mapOpenRouterFinishReason(finishReason) {
|
|
883
947
|
switch (finishReason) {
|
|
@@ -927,6 +991,15 @@ function getFileUrl({
|
|
|
927
991
|
}
|
|
928
992
|
return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
|
|
929
993
|
}
|
|
994
|
+
function getMediaType(dataUrl, defaultMediaType) {
|
|
995
|
+
var _a15;
|
|
996
|
+
const match = dataUrl.match(/^data:([^;]+)/);
|
|
997
|
+
return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
|
|
998
|
+
}
|
|
999
|
+
function getBase64FromDataUrl(dataUrl) {
|
|
1000
|
+
const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
|
|
1001
|
+
return match ? match[1] : dataUrl;
|
|
1002
|
+
}
|
|
930
1003
|
|
|
931
1004
|
// src/chat/convert-to-openrouter-chat-messages.ts
|
|
932
1005
|
function getCacheControl(providerMetadata) {
|
|
@@ -936,7 +1009,7 @@ function getCacheControl(providerMetadata) {
|
|
|
936
1009
|
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;
|
|
937
1010
|
}
|
|
938
1011
|
function convertToOpenRouterChatMessages(prompt) {
|
|
939
|
-
var _a15, _b, _c;
|
|
1012
|
+
var _a15, _b, _c, _d, _e;
|
|
940
1013
|
const messages = [];
|
|
941
1014
|
for (const { role, content, providerOptions } of prompt) {
|
|
942
1015
|
switch (role) {
|
|
@@ -967,7 +1040,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
967
1040
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
968
1041
|
const contentParts = content.map(
|
|
969
1042
|
(part) => {
|
|
970
|
-
var _a16, _b2, _c2,
|
|
1043
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
971
1044
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
972
1045
|
switch (part.type) {
|
|
973
1046
|
case "text":
|
|
@@ -993,7 +1066,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
993
1066
|
};
|
|
994
1067
|
}
|
|
995
1068
|
const fileName = String(
|
|
996
|
-
(_f = (
|
|
1069
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
997
1070
|
);
|
|
998
1071
|
const fileData = getFileUrl({
|
|
999
1072
|
part,
|
|
@@ -1039,7 +1112,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1039
1112
|
case "assistant": {
|
|
1040
1113
|
let text = "";
|
|
1041
1114
|
let reasoning = "";
|
|
1042
|
-
const reasoningDetails = [];
|
|
1043
1115
|
const toolCalls = [];
|
|
1044
1116
|
for (const part of content) {
|
|
1045
1117
|
switch (part.type) {
|
|
@@ -1060,10 +1132,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1060
1132
|
}
|
|
1061
1133
|
case "reasoning": {
|
|
1062
1134
|
reasoning += part.text;
|
|
1063
|
-
reasoningDetails.push({
|
|
1064
|
-
type: "reasoning.text" /* Text */,
|
|
1065
|
-
text: part.text
|
|
1066
|
-
});
|
|
1067
1135
|
break;
|
|
1068
1136
|
}
|
|
1069
1137
|
case "file":
|
|
@@ -1073,12 +1141,17 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1073
1141
|
}
|
|
1074
1142
|
}
|
|
1075
1143
|
}
|
|
1144
|
+
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(
|
|
1145
|
+
providerOptions
|
|
1146
|
+
);
|
|
1147
|
+
const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
|
|
1076
1148
|
messages.push({
|
|
1077
1149
|
role: "assistant",
|
|
1078
1150
|
content: text,
|
|
1079
1151
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1080
1152
|
reasoning: reasoning || void 0,
|
|
1081
|
-
reasoning_details
|
|
1153
|
+
// Only include reasoning_details if we have the preserved original version
|
|
1154
|
+
reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
|
|
1082
1155
|
cache_control: getCacheControl(providerOptions)
|
|
1083
1156
|
});
|
|
1084
1157
|
break;
|
|
@@ -1090,7 +1163,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1090
1163
|
role: "tool",
|
|
1091
1164
|
tool_call_id: toolResponse.toolCallId,
|
|
1092
1165
|
content: content2,
|
|
1093
|
-
cache_control: (
|
|
1166
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1094
1167
|
});
|
|
1095
1168
|
}
|
|
1096
1169
|
break;
|
|
@@ -1107,15 +1180,15 @@ function getToolResultContent(input) {
|
|
|
1107
1180
|
}
|
|
1108
1181
|
|
|
1109
1182
|
// src/chat/get-tool-choice.ts
|
|
1110
|
-
import { z as
|
|
1111
|
-
var ChatCompletionToolChoiceSchema =
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
type:
|
|
1117
|
-
function:
|
|
1118
|
-
name:
|
|
1183
|
+
import { z as z5 } from "zod/v4";
|
|
1184
|
+
var ChatCompletionToolChoiceSchema = z5.union([
|
|
1185
|
+
z5.literal("auto"),
|
|
1186
|
+
z5.literal("none"),
|
|
1187
|
+
z5.literal("required"),
|
|
1188
|
+
z5.object({
|
|
1189
|
+
type: z5.literal("function"),
|
|
1190
|
+
function: z5.object({
|
|
1191
|
+
name: z5.string()
|
|
1119
1192
|
})
|
|
1120
1193
|
})
|
|
1121
1194
|
]);
|
|
@@ -1139,126 +1212,144 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1139
1212
|
}
|
|
1140
1213
|
|
|
1141
1214
|
// src/chat/schemas.ts
|
|
1142
|
-
import { z as
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1215
|
+
import { z as z7 } from "zod/v4";
|
|
1216
|
+
|
|
1217
|
+
// src/schemas/image.ts
|
|
1218
|
+
import { z as z6 } from "zod/v4";
|
|
1219
|
+
var ImageResponseSchema = z6.object({
|
|
1220
|
+
type: z6.literal("image_url"),
|
|
1221
|
+
image_url: z6.object({
|
|
1222
|
+
url: z6.string()
|
|
1223
|
+
})
|
|
1224
|
+
});
|
|
1225
|
+
var ImageResponseWithUnknownSchema = z6.union([
|
|
1226
|
+
ImageResponseSchema,
|
|
1227
|
+
z6.unknown().transform(() => null)
|
|
1228
|
+
]);
|
|
1229
|
+
var ImageResponseArraySchema = z6.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1230
|
+
|
|
1231
|
+
// src/chat/schemas.ts
|
|
1232
|
+
var OpenRouterChatCompletionBaseResponseSchema = z7.object({
|
|
1233
|
+
id: z7.string().optional(),
|
|
1234
|
+
model: z7.string().optional(),
|
|
1235
|
+
provider: z7.string().optional(),
|
|
1236
|
+
usage: z7.object({
|
|
1237
|
+
prompt_tokens: z7.number(),
|
|
1238
|
+
prompt_tokens_details: z7.object({
|
|
1239
|
+
cached_tokens: z7.number()
|
|
1151
1240
|
}).nullish(),
|
|
1152
|
-
completion_tokens:
|
|
1153
|
-
completion_tokens_details:
|
|
1154
|
-
reasoning_tokens:
|
|
1241
|
+
completion_tokens: z7.number(),
|
|
1242
|
+
completion_tokens_details: z7.object({
|
|
1243
|
+
reasoning_tokens: z7.number()
|
|
1155
1244
|
}).nullish(),
|
|
1156
|
-
total_tokens:
|
|
1157
|
-
cost:
|
|
1158
|
-
cost_details:
|
|
1159
|
-
upstream_inference_cost:
|
|
1245
|
+
total_tokens: z7.number(),
|
|
1246
|
+
cost: z7.number().optional(),
|
|
1247
|
+
cost_details: z7.object({
|
|
1248
|
+
upstream_inference_cost: z7.number().nullish()
|
|
1160
1249
|
}).nullish()
|
|
1161
1250
|
}).nullish()
|
|
1162
1251
|
});
|
|
1163
1252
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1164
|
-
choices:
|
|
1165
|
-
|
|
1166
|
-
message:
|
|
1167
|
-
role:
|
|
1168
|
-
content:
|
|
1169
|
-
reasoning:
|
|
1253
|
+
choices: z7.array(
|
|
1254
|
+
z7.object({
|
|
1255
|
+
message: z7.object({
|
|
1256
|
+
role: z7.literal("assistant"),
|
|
1257
|
+
content: z7.string().nullable().optional(),
|
|
1258
|
+
reasoning: z7.string().nullable().optional(),
|
|
1170
1259
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1260
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1261
|
+
tool_calls: z7.array(
|
|
1262
|
+
z7.object({
|
|
1263
|
+
id: z7.string().optional().nullable(),
|
|
1264
|
+
type: z7.literal("function"),
|
|
1265
|
+
function: z7.object({
|
|
1266
|
+
name: z7.string(),
|
|
1267
|
+
arguments: z7.string()
|
|
1178
1268
|
})
|
|
1179
1269
|
})
|
|
1180
1270
|
).optional(),
|
|
1181
|
-
annotations:
|
|
1182
|
-
|
|
1183
|
-
type:
|
|
1184
|
-
url_citation:
|
|
1185
|
-
end_index:
|
|
1186
|
-
start_index:
|
|
1187
|
-
title:
|
|
1188
|
-
url:
|
|
1189
|
-
content:
|
|
1271
|
+
annotations: z7.array(
|
|
1272
|
+
z7.object({
|
|
1273
|
+
type: z7.enum(["url_citation"]),
|
|
1274
|
+
url_citation: z7.object({
|
|
1275
|
+
end_index: z7.number(),
|
|
1276
|
+
start_index: z7.number(),
|
|
1277
|
+
title: z7.string(),
|
|
1278
|
+
url: z7.string(),
|
|
1279
|
+
content: z7.string().optional()
|
|
1190
1280
|
})
|
|
1191
1281
|
})
|
|
1192
1282
|
).nullish()
|
|
1193
1283
|
}),
|
|
1194
|
-
index:
|
|
1195
|
-
logprobs:
|
|
1196
|
-
content:
|
|
1197
|
-
|
|
1198
|
-
token:
|
|
1199
|
-
logprob:
|
|
1200
|
-
top_logprobs:
|
|
1201
|
-
|
|
1202
|
-
token:
|
|
1203
|
-
logprob:
|
|
1284
|
+
index: z7.number().nullish(),
|
|
1285
|
+
logprobs: z7.object({
|
|
1286
|
+
content: z7.array(
|
|
1287
|
+
z7.object({
|
|
1288
|
+
token: z7.string(),
|
|
1289
|
+
logprob: z7.number(),
|
|
1290
|
+
top_logprobs: z7.array(
|
|
1291
|
+
z7.object({
|
|
1292
|
+
token: z7.string(),
|
|
1293
|
+
logprob: z7.number()
|
|
1204
1294
|
})
|
|
1205
1295
|
)
|
|
1206
1296
|
})
|
|
1207
1297
|
).nullable()
|
|
1208
1298
|
}).nullable().optional(),
|
|
1209
|
-
finish_reason:
|
|
1299
|
+
finish_reason: z7.string().optional().nullable()
|
|
1210
1300
|
})
|
|
1211
1301
|
)
|
|
1212
1302
|
});
|
|
1213
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1303
|
+
var OpenRouterStreamChatCompletionChunkSchema = z7.union([
|
|
1214
1304
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1215
|
-
choices:
|
|
1216
|
-
|
|
1217
|
-
delta:
|
|
1218
|
-
role:
|
|
1219
|
-
content:
|
|
1220
|
-
reasoning:
|
|
1305
|
+
choices: z7.array(
|
|
1306
|
+
z7.object({
|
|
1307
|
+
delta: z7.object({
|
|
1308
|
+
role: z7.enum(["assistant"]).optional(),
|
|
1309
|
+
content: z7.string().nullish(),
|
|
1310
|
+
reasoning: z7.string().nullish().optional(),
|
|
1221
1311
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1312
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1313
|
+
tool_calls: z7.array(
|
|
1314
|
+
z7.object({
|
|
1315
|
+
index: z7.number().nullish(),
|
|
1316
|
+
id: z7.string().nullish(),
|
|
1317
|
+
type: z7.literal("function").optional(),
|
|
1318
|
+
function: z7.object({
|
|
1319
|
+
name: z7.string().nullish(),
|
|
1320
|
+
arguments: z7.string().nullish()
|
|
1230
1321
|
})
|
|
1231
1322
|
})
|
|
1232
1323
|
).nullish(),
|
|
1233
|
-
annotations:
|
|
1234
|
-
|
|
1235
|
-
type:
|
|
1236
|
-
url_citation:
|
|
1237
|
-
end_index:
|
|
1238
|
-
start_index:
|
|
1239
|
-
title:
|
|
1240
|
-
url:
|
|
1241
|
-
content:
|
|
1324
|
+
annotations: z7.array(
|
|
1325
|
+
z7.object({
|
|
1326
|
+
type: z7.enum(["url_citation"]),
|
|
1327
|
+
url_citation: z7.object({
|
|
1328
|
+
end_index: z7.number(),
|
|
1329
|
+
start_index: z7.number(),
|
|
1330
|
+
title: z7.string(),
|
|
1331
|
+
url: z7.string(),
|
|
1332
|
+
content: z7.string().optional()
|
|
1242
1333
|
})
|
|
1243
1334
|
})
|
|
1244
1335
|
).nullish()
|
|
1245
1336
|
}).nullish(),
|
|
1246
|
-
logprobs:
|
|
1247
|
-
content:
|
|
1248
|
-
|
|
1249
|
-
token:
|
|
1250
|
-
logprob:
|
|
1251
|
-
top_logprobs:
|
|
1252
|
-
|
|
1253
|
-
token:
|
|
1254
|
-
logprob:
|
|
1337
|
+
logprobs: z7.object({
|
|
1338
|
+
content: z7.array(
|
|
1339
|
+
z7.object({
|
|
1340
|
+
token: z7.string(),
|
|
1341
|
+
logprob: z7.number(),
|
|
1342
|
+
top_logprobs: z7.array(
|
|
1343
|
+
z7.object({
|
|
1344
|
+
token: z7.string(),
|
|
1345
|
+
logprob: z7.number()
|
|
1255
1346
|
})
|
|
1256
1347
|
)
|
|
1257
1348
|
})
|
|
1258
1349
|
).nullable()
|
|
1259
1350
|
}).nullish(),
|
|
1260
|
-
finish_reason:
|
|
1261
|
-
index:
|
|
1351
|
+
finish_reason: z7.string().nullable().optional(),
|
|
1352
|
+
index: z7.number().nullish()
|
|
1262
1353
|
})
|
|
1263
1354
|
)
|
|
1264
1355
|
}),
|
|
@@ -1361,7 +1452,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1361
1452
|
return baseArgs;
|
|
1362
1453
|
}
|
|
1363
1454
|
async doGenerate(options) {
|
|
1364
|
-
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;
|
|
1455
|
+
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;
|
|
1365
1456
|
const providerOptions = options.providerOptions || {};
|
|
1366
1457
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1367
1458
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1455,6 +1546,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1455
1546
|
});
|
|
1456
1547
|
}
|
|
1457
1548
|
}
|
|
1549
|
+
if (choice.message.images) {
|
|
1550
|
+
for (const image of choice.message.images) {
|
|
1551
|
+
content.push({
|
|
1552
|
+
type: "file",
|
|
1553
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1554
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1458
1558
|
if (choice.message.annotations) {
|
|
1459
1559
|
for (const annotation of choice.message.annotations) {
|
|
1460
1560
|
if (annotation.type === "url_citation") {
|
|
@@ -1479,24 +1579,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1479
1579
|
usage: usageInfo,
|
|
1480
1580
|
warnings: [],
|
|
1481
1581
|
providerMetadata: {
|
|
1482
|
-
openrouter: {
|
|
1582
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1483
1583
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1584
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1484
1585
|
usage: {
|
|
1485
|
-
promptTokens: (
|
|
1486
|
-
completionTokens: (
|
|
1487
|
-
totalTokens: (
|
|
1488
|
-
cost: (
|
|
1586
|
+
promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
|
|
1587
|
+
completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
|
|
1588
|
+
totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
|
|
1589
|
+
cost: (_p = response.usage) == null ? void 0 : _p.cost,
|
|
1489
1590
|
promptTokensDetails: {
|
|
1490
|
-
cachedTokens: (
|
|
1591
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1491
1592
|
},
|
|
1492
1593
|
completionTokensDetails: {
|
|
1493
|
-
reasoningTokens: (
|
|
1594
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1494
1595
|
},
|
|
1495
1596
|
costDetails: {
|
|
1496
|
-
upstreamInferenceCost: (
|
|
1597
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1497
1598
|
}
|
|
1498
1599
|
}
|
|
1499
|
-
}
|
|
1600
|
+
})
|
|
1500
1601
|
},
|
|
1501
1602
|
request: { body: args },
|
|
1502
1603
|
response: {
|
|
@@ -1541,6 +1642,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1541
1642
|
cachedInputTokens: Number.NaN
|
|
1542
1643
|
};
|
|
1543
1644
|
const openrouterUsage = {};
|
|
1645
|
+
const accumulatedReasoningDetails = [];
|
|
1544
1646
|
let textStarted = false;
|
|
1545
1647
|
let reasoningStarted = false;
|
|
1546
1648
|
let textId;
|
|
@@ -1626,6 +1728,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1626
1728
|
});
|
|
1627
1729
|
};
|
|
1628
1730
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1731
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1629
1732
|
for (const detail of delta.reasoning_details) {
|
|
1630
1733
|
switch (detail.type) {
|
|
1631
1734
|
case "reasoning.text" /* Text */: {
|
|
@@ -1788,6 +1891,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1788
1891
|
}
|
|
1789
1892
|
}
|
|
1790
1893
|
}
|
|
1894
|
+
if (delta.images != null) {
|
|
1895
|
+
for (const image of delta.images) {
|
|
1896
|
+
controller.enqueue({
|
|
1897
|
+
type: "file",
|
|
1898
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1899
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1900
|
+
});
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1791
1903
|
},
|
|
1792
1904
|
flush(controller) {
|
|
1793
1905
|
var _a16;
|
|
@@ -1823,6 +1935,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1823
1935
|
if (provider !== void 0) {
|
|
1824
1936
|
openrouterMetadata.provider = provider;
|
|
1825
1937
|
}
|
|
1938
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
1939
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
1940
|
+
}
|
|
1826
1941
|
controller.enqueue({
|
|
1827
1942
|
type: "finish",
|
|
1828
1943
|
finishReason,
|
|
@@ -1945,36 +2060,36 @@ ${assistantMessage}
|
|
|
1945
2060
|
}
|
|
1946
2061
|
|
|
1947
2062
|
// src/completion/schemas.ts
|
|
1948
|
-
import { z as
|
|
1949
|
-
var OpenRouterCompletionChunkSchema =
|
|
1950
|
-
|
|
1951
|
-
id:
|
|
1952
|
-
model:
|
|
1953
|
-
choices:
|
|
1954
|
-
|
|
1955
|
-
text:
|
|
1956
|
-
reasoning:
|
|
2063
|
+
import { z as z8 } from "zod/v4";
|
|
2064
|
+
var OpenRouterCompletionChunkSchema = z8.union([
|
|
2065
|
+
z8.object({
|
|
2066
|
+
id: z8.string().optional(),
|
|
2067
|
+
model: z8.string().optional(),
|
|
2068
|
+
choices: z8.array(
|
|
2069
|
+
z8.object({
|
|
2070
|
+
text: z8.string(),
|
|
2071
|
+
reasoning: z8.string().nullish().optional(),
|
|
1957
2072
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1958
|
-
finish_reason:
|
|
1959
|
-
index:
|
|
1960
|
-
logprobs:
|
|
1961
|
-
tokens:
|
|
1962
|
-
token_logprobs:
|
|
1963
|
-
top_logprobs:
|
|
2073
|
+
finish_reason: z8.string().nullish(),
|
|
2074
|
+
index: z8.number().nullish(),
|
|
2075
|
+
logprobs: z8.object({
|
|
2076
|
+
tokens: z8.array(z8.string()),
|
|
2077
|
+
token_logprobs: z8.array(z8.number()),
|
|
2078
|
+
top_logprobs: z8.array(z8.record(z8.string(), z8.number())).nullable()
|
|
1964
2079
|
}).nullable().optional()
|
|
1965
2080
|
})
|
|
1966
2081
|
),
|
|
1967
|
-
usage:
|
|
1968
|
-
prompt_tokens:
|
|
1969
|
-
prompt_tokens_details:
|
|
1970
|
-
cached_tokens:
|
|
2082
|
+
usage: z8.object({
|
|
2083
|
+
prompt_tokens: z8.number(),
|
|
2084
|
+
prompt_tokens_details: z8.object({
|
|
2085
|
+
cached_tokens: z8.number()
|
|
1971
2086
|
}).nullish(),
|
|
1972
|
-
completion_tokens:
|
|
1973
|
-
completion_tokens_details:
|
|
1974
|
-
reasoning_tokens:
|
|
2087
|
+
completion_tokens: z8.number(),
|
|
2088
|
+
completion_tokens_details: z8.object({
|
|
2089
|
+
reasoning_tokens: z8.number()
|
|
1975
2090
|
}).nullish(),
|
|
1976
|
-
total_tokens:
|
|
1977
|
-
cost:
|
|
2091
|
+
total_tokens: z8.number(),
|
|
2092
|
+
cost: z8.number().optional()
|
|
1978
2093
|
}).nullish()
|
|
1979
2094
|
}),
|
|
1980
2095
|
OpenRouterErrorResponseSchema
|