@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/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) {
|
|
@@ -945,7 +1009,7 @@ function getCacheControl(providerMetadata) {
|
|
|
945
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;
|
|
946
1010
|
}
|
|
947
1011
|
function convertToOpenRouterChatMessages(prompt) {
|
|
948
|
-
var _a15, _b, _c;
|
|
1012
|
+
var _a15, _b, _c, _d, _e;
|
|
949
1013
|
const messages = [];
|
|
950
1014
|
for (const { role, content, providerOptions } of prompt) {
|
|
951
1015
|
switch (role) {
|
|
@@ -976,7 +1040,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
976
1040
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
977
1041
|
const contentParts = content.map(
|
|
978
1042
|
(part) => {
|
|
979
|
-
var _a16, _b2, _c2,
|
|
1043
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
980
1044
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
981
1045
|
switch (part.type) {
|
|
982
1046
|
case "text":
|
|
@@ -1002,7 +1066,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1002
1066
|
};
|
|
1003
1067
|
}
|
|
1004
1068
|
const fileName = String(
|
|
1005
|
-
(_f = (
|
|
1069
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1006
1070
|
);
|
|
1007
1071
|
const fileData = getFileUrl({
|
|
1008
1072
|
part,
|
|
@@ -1048,7 +1112,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1048
1112
|
case "assistant": {
|
|
1049
1113
|
let text = "";
|
|
1050
1114
|
let reasoning = "";
|
|
1051
|
-
const reasoningDetails = [];
|
|
1052
1115
|
const toolCalls = [];
|
|
1053
1116
|
for (const part of content) {
|
|
1054
1117
|
switch (part.type) {
|
|
@@ -1069,10 +1132,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1069
1132
|
}
|
|
1070
1133
|
case "reasoning": {
|
|
1071
1134
|
reasoning += part.text;
|
|
1072
|
-
reasoningDetails.push({
|
|
1073
|
-
type: "reasoning.text" /* Text */,
|
|
1074
|
-
text: part.text
|
|
1075
|
-
});
|
|
1076
1135
|
break;
|
|
1077
1136
|
}
|
|
1078
1137
|
case "file":
|
|
@@ -1082,12 +1141,17 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1082
1141
|
}
|
|
1083
1142
|
}
|
|
1084
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;
|
|
1085
1148
|
messages.push({
|
|
1086
1149
|
role: "assistant",
|
|
1087
1150
|
content: text,
|
|
1088
1151
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1089
1152
|
reasoning: reasoning || void 0,
|
|
1090
|
-
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,
|
|
1091
1155
|
cache_control: getCacheControl(providerOptions)
|
|
1092
1156
|
});
|
|
1093
1157
|
break;
|
|
@@ -1099,7 +1163,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1099
1163
|
role: "tool",
|
|
1100
1164
|
tool_call_id: toolResponse.toolCallId,
|
|
1101
1165
|
content: content2,
|
|
1102
|
-
cache_control: (
|
|
1166
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1103
1167
|
});
|
|
1104
1168
|
}
|
|
1105
1169
|
break;
|
|
@@ -1116,15 +1180,15 @@ function getToolResultContent(input) {
|
|
|
1116
1180
|
}
|
|
1117
1181
|
|
|
1118
1182
|
// src/chat/get-tool-choice.ts
|
|
1119
|
-
import { z as
|
|
1120
|
-
var ChatCompletionToolChoiceSchema =
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
type:
|
|
1126
|
-
function:
|
|
1127
|
-
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()
|
|
1128
1192
|
})
|
|
1129
1193
|
})
|
|
1130
1194
|
]);
|
|
@@ -1148,144 +1212,144 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1148
1212
|
}
|
|
1149
1213
|
|
|
1150
1214
|
// src/chat/schemas.ts
|
|
1151
|
-
import { z as
|
|
1215
|
+
import { z as z7 } from "zod/v4";
|
|
1152
1216
|
|
|
1153
1217
|
// src/schemas/image.ts
|
|
1154
|
-
import { z as
|
|
1155
|
-
var ImageResponseSchema =
|
|
1156
|
-
type:
|
|
1157
|
-
image_url:
|
|
1158
|
-
url:
|
|
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()
|
|
1159
1223
|
})
|
|
1160
1224
|
});
|
|
1161
|
-
var ImageResponseWithUnknownSchema =
|
|
1225
|
+
var ImageResponseWithUnknownSchema = z6.union([
|
|
1162
1226
|
ImageResponseSchema,
|
|
1163
|
-
|
|
1227
|
+
z6.unknown().transform(() => null)
|
|
1164
1228
|
]);
|
|
1165
|
-
var ImageResponseArraySchema =
|
|
1229
|
+
var ImageResponseArraySchema = z6.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1166
1230
|
|
|
1167
1231
|
// src/chat/schemas.ts
|
|
1168
|
-
var OpenRouterChatCompletionBaseResponseSchema =
|
|
1169
|
-
id:
|
|
1170
|
-
model:
|
|
1171
|
-
provider:
|
|
1172
|
-
usage:
|
|
1173
|
-
prompt_tokens:
|
|
1174
|
-
prompt_tokens_details:
|
|
1175
|
-
cached_tokens:
|
|
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()
|
|
1176
1240
|
}).nullish(),
|
|
1177
|
-
completion_tokens:
|
|
1178
|
-
completion_tokens_details:
|
|
1179
|
-
reasoning_tokens:
|
|
1241
|
+
completion_tokens: z7.number(),
|
|
1242
|
+
completion_tokens_details: z7.object({
|
|
1243
|
+
reasoning_tokens: z7.number()
|
|
1180
1244
|
}).nullish(),
|
|
1181
|
-
total_tokens:
|
|
1182
|
-
cost:
|
|
1183
|
-
cost_details:
|
|
1184
|
-
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()
|
|
1185
1249
|
}).nullish()
|
|
1186
1250
|
}).nullish()
|
|
1187
1251
|
});
|
|
1188
1252
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1189
|
-
choices:
|
|
1190
|
-
|
|
1191
|
-
message:
|
|
1192
|
-
role:
|
|
1193
|
-
content:
|
|
1194
|
-
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(),
|
|
1195
1259
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1196
1260
|
images: ImageResponseArraySchema.nullish(),
|
|
1197
|
-
tool_calls:
|
|
1198
|
-
|
|
1199
|
-
id:
|
|
1200
|
-
type:
|
|
1201
|
-
function:
|
|
1202
|
-
name:
|
|
1203
|
-
arguments:
|
|
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()
|
|
1204
1268
|
})
|
|
1205
1269
|
})
|
|
1206
1270
|
).optional(),
|
|
1207
|
-
annotations:
|
|
1208
|
-
|
|
1209
|
-
type:
|
|
1210
|
-
url_citation:
|
|
1211
|
-
end_index:
|
|
1212
|
-
start_index:
|
|
1213
|
-
title:
|
|
1214
|
-
url:
|
|
1215
|
-
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()
|
|
1216
1280
|
})
|
|
1217
1281
|
})
|
|
1218
1282
|
).nullish()
|
|
1219
1283
|
}),
|
|
1220
|
-
index:
|
|
1221
|
-
logprobs:
|
|
1222
|
-
content:
|
|
1223
|
-
|
|
1224
|
-
token:
|
|
1225
|
-
logprob:
|
|
1226
|
-
top_logprobs:
|
|
1227
|
-
|
|
1228
|
-
token:
|
|
1229
|
-
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()
|
|
1230
1294
|
})
|
|
1231
1295
|
)
|
|
1232
1296
|
})
|
|
1233
1297
|
).nullable()
|
|
1234
1298
|
}).nullable().optional(),
|
|
1235
|
-
finish_reason:
|
|
1299
|
+
finish_reason: z7.string().optional().nullable()
|
|
1236
1300
|
})
|
|
1237
1301
|
)
|
|
1238
1302
|
});
|
|
1239
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1303
|
+
var OpenRouterStreamChatCompletionChunkSchema = z7.union([
|
|
1240
1304
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1241
|
-
choices:
|
|
1242
|
-
|
|
1243
|
-
delta:
|
|
1244
|
-
role:
|
|
1245
|
-
content:
|
|
1246
|
-
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(),
|
|
1247
1311
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1248
1312
|
images: ImageResponseArraySchema.nullish(),
|
|
1249
|
-
tool_calls:
|
|
1250
|
-
|
|
1251
|
-
index:
|
|
1252
|
-
id:
|
|
1253
|
-
type:
|
|
1254
|
-
function:
|
|
1255
|
-
name:
|
|
1256
|
-
arguments:
|
|
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()
|
|
1257
1321
|
})
|
|
1258
1322
|
})
|
|
1259
1323
|
).nullish(),
|
|
1260
|
-
annotations:
|
|
1261
|
-
|
|
1262
|
-
type:
|
|
1263
|
-
url_citation:
|
|
1264
|
-
end_index:
|
|
1265
|
-
start_index:
|
|
1266
|
-
title:
|
|
1267
|
-
url:
|
|
1268
|
-
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()
|
|
1269
1333
|
})
|
|
1270
1334
|
})
|
|
1271
1335
|
).nullish()
|
|
1272
1336
|
}).nullish(),
|
|
1273
|
-
logprobs:
|
|
1274
|
-
content:
|
|
1275
|
-
|
|
1276
|
-
token:
|
|
1277
|
-
logprob:
|
|
1278
|
-
top_logprobs:
|
|
1279
|
-
|
|
1280
|
-
token:
|
|
1281
|
-
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()
|
|
1282
1346
|
})
|
|
1283
1347
|
)
|
|
1284
1348
|
})
|
|
1285
1349
|
).nullable()
|
|
1286
1350
|
}).nullish(),
|
|
1287
|
-
finish_reason:
|
|
1288
|
-
index:
|
|
1351
|
+
finish_reason: z7.string().nullable().optional(),
|
|
1352
|
+
index: z7.number().nullish()
|
|
1289
1353
|
})
|
|
1290
1354
|
)
|
|
1291
1355
|
}),
|
|
@@ -1388,7 +1452,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1388
1452
|
return baseArgs;
|
|
1389
1453
|
}
|
|
1390
1454
|
async doGenerate(options) {
|
|
1391
|
-
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;
|
|
1392
1456
|
const providerOptions = options.providerOptions || {};
|
|
1393
1457
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1394
1458
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
@@ -1515,24 +1579,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1515
1579
|
usage: usageInfo,
|
|
1516
1580
|
warnings: [],
|
|
1517
1581
|
providerMetadata: {
|
|
1518
|
-
openrouter: {
|
|
1582
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1519
1583
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1584
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1520
1585
|
usage: {
|
|
1521
|
-
promptTokens: (
|
|
1522
|
-
completionTokens: (
|
|
1523
|
-
totalTokens: (
|
|
1524
|
-
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,
|
|
1525
1590
|
promptTokensDetails: {
|
|
1526
|
-
cachedTokens: (
|
|
1591
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1527
1592
|
},
|
|
1528
1593
|
completionTokensDetails: {
|
|
1529
|
-
reasoningTokens: (
|
|
1594
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1530
1595
|
},
|
|
1531
1596
|
costDetails: {
|
|
1532
|
-
upstreamInferenceCost: (
|
|
1597
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1533
1598
|
}
|
|
1534
1599
|
}
|
|
1535
|
-
}
|
|
1600
|
+
})
|
|
1536
1601
|
},
|
|
1537
1602
|
request: { body: args },
|
|
1538
1603
|
response: {
|
|
@@ -1577,6 +1642,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1577
1642
|
cachedInputTokens: Number.NaN
|
|
1578
1643
|
};
|
|
1579
1644
|
const openrouterUsage = {};
|
|
1645
|
+
const accumulatedReasoningDetails = [];
|
|
1580
1646
|
let textStarted = false;
|
|
1581
1647
|
let reasoningStarted = false;
|
|
1582
1648
|
let textId;
|
|
@@ -1662,6 +1728,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1662
1728
|
});
|
|
1663
1729
|
};
|
|
1664
1730
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1731
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1665
1732
|
for (const detail of delta.reasoning_details) {
|
|
1666
1733
|
switch (detail.type) {
|
|
1667
1734
|
case "reasoning.text" /* Text */: {
|
|
@@ -1868,6 +1935,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1868
1935
|
if (provider !== void 0) {
|
|
1869
1936
|
openrouterMetadata.provider = provider;
|
|
1870
1937
|
}
|
|
1938
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
1939
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
1940
|
+
}
|
|
1871
1941
|
controller.enqueue({
|
|
1872
1942
|
type: "finish",
|
|
1873
1943
|
finishReason,
|
|
@@ -1990,36 +2060,36 @@ ${assistantMessage}
|
|
|
1990
2060
|
}
|
|
1991
2061
|
|
|
1992
2062
|
// src/completion/schemas.ts
|
|
1993
|
-
import { z as
|
|
1994
|
-
var OpenRouterCompletionChunkSchema =
|
|
1995
|
-
|
|
1996
|
-
id:
|
|
1997
|
-
model:
|
|
1998
|
-
choices:
|
|
1999
|
-
|
|
2000
|
-
text:
|
|
2001
|
-
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(),
|
|
2002
2072
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2003
|
-
finish_reason:
|
|
2004
|
-
index:
|
|
2005
|
-
logprobs:
|
|
2006
|
-
tokens:
|
|
2007
|
-
token_logprobs:
|
|
2008
|
-
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()
|
|
2009
2079
|
}).nullable().optional()
|
|
2010
2080
|
})
|
|
2011
2081
|
),
|
|
2012
|
-
usage:
|
|
2013
|
-
prompt_tokens:
|
|
2014
|
-
prompt_tokens_details:
|
|
2015
|
-
cached_tokens:
|
|
2082
|
+
usage: z8.object({
|
|
2083
|
+
prompt_tokens: z8.number(),
|
|
2084
|
+
prompt_tokens_details: z8.object({
|
|
2085
|
+
cached_tokens: z8.number()
|
|
2016
2086
|
}).nullish(),
|
|
2017
|
-
completion_tokens:
|
|
2018
|
-
completion_tokens_details:
|
|
2019
|
-
reasoning_tokens:
|
|
2087
|
+
completion_tokens: z8.number(),
|
|
2088
|
+
completion_tokens_details: z8.object({
|
|
2089
|
+
reasoning_tokens: z8.number()
|
|
2020
2090
|
}).nullish(),
|
|
2021
|
-
total_tokens:
|
|
2022
|
-
cost:
|
|
2091
|
+
total_tokens: z8.number(),
|
|
2092
|
+
cost: z8.number().optional()
|
|
2023
2093
|
}).nullish()
|
|
2024
2094
|
}),
|
|
2025
2095
|
OpenRouterErrorResponseSchema
|