@openrouter/ai-sdk-provider 1.2.0 → 1.2.2
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 +53 -13
- package/dist/index.d.ts +53 -13
- package/dist/index.js +386 -179
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +386 -179
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +51 -11
- package/dist/internal/index.d.ts +51 -11
- package/dist/internal/index.js +354 -172
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +354 -172
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +12 -4
package/dist/internal/index.mjs
CHANGED
|
@@ -208,6 +208,18 @@ var name9 = "AI_NoContentGeneratedError";
|
|
|
208
208
|
var marker10 = `vercel.ai.error.${name9}`;
|
|
209
209
|
var symbol10 = Symbol.for(marker10);
|
|
210
210
|
var _a10;
|
|
211
|
+
var NoContentGeneratedError = class extends AISDKError {
|
|
212
|
+
// used in isInstance
|
|
213
|
+
constructor({
|
|
214
|
+
message = "No content generated."
|
|
215
|
+
} = {}) {
|
|
216
|
+
super({ name: name9, message });
|
|
217
|
+
this[_a10] = true;
|
|
218
|
+
}
|
|
219
|
+
static isInstance(error) {
|
|
220
|
+
return AISDKError.hasMarker(error, marker10);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
211
223
|
_a10 = symbol10;
|
|
212
224
|
var name10 = "AI_NoSuchModelError";
|
|
213
225
|
var marker11 = `vercel.ai.error.${name10}`;
|
|
@@ -839,19 +851,40 @@ function convertUint8ArrayToBase64(array) {
|
|
|
839
851
|
|
|
840
852
|
// src/schemas/reasoning-details.ts
|
|
841
853
|
import { z } from "zod/v4";
|
|
854
|
+
|
|
855
|
+
// src/utils/type-guards.ts
|
|
856
|
+
function isDefinedOrNotNull(value) {
|
|
857
|
+
return value !== null && value !== void 0;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// src/schemas/format.ts
|
|
861
|
+
var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
|
|
862
|
+
ReasoningFormat2["Unknown"] = "unknown";
|
|
863
|
+
ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
|
|
864
|
+
ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
|
|
865
|
+
ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
|
|
866
|
+
return ReasoningFormat2;
|
|
867
|
+
})(ReasoningFormat || {});
|
|
868
|
+
|
|
869
|
+
// src/schemas/reasoning-details.ts
|
|
870
|
+
var CommonReasoningDetailSchema = z.object({
|
|
871
|
+
id: z.string().nullish(),
|
|
872
|
+
format: z.nativeEnum(ReasoningFormat).nullish(),
|
|
873
|
+
index: z.number().optional()
|
|
874
|
+
});
|
|
842
875
|
var ReasoningDetailSummarySchema = z.object({
|
|
843
876
|
type: z.literal("reasoning.summary" /* Summary */),
|
|
844
877
|
summary: z.string()
|
|
845
|
-
});
|
|
878
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
846
879
|
var ReasoningDetailEncryptedSchema = z.object({
|
|
847
880
|
type: z.literal("reasoning.encrypted" /* Encrypted */),
|
|
848
881
|
data: z.string()
|
|
849
|
-
});
|
|
882
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
850
883
|
var ReasoningDetailTextSchema = z.object({
|
|
851
884
|
type: z.literal("reasoning.text" /* Text */),
|
|
852
885
|
text: z.string().nullish(),
|
|
853
886
|
signature: z.string().nullish()
|
|
854
|
-
});
|
|
887
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
855
888
|
var ReasoningDetailUnionSchema = z.union([
|
|
856
889
|
ReasoningDetailSummarySchema,
|
|
857
890
|
ReasoningDetailEncryptedSchema,
|
|
@@ -862,6 +895,26 @@ var ReasoningDetailsWithUnknownSchema = z.union([
|
|
|
862
895
|
z.unknown().transform(() => null)
|
|
863
896
|
]);
|
|
864
897
|
var ReasoningDetailArraySchema = z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
898
|
+
var OutputUnionToReasoningDetailsSchema = z.union([
|
|
899
|
+
z.object({
|
|
900
|
+
delta: z.object({
|
|
901
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
902
|
+
})
|
|
903
|
+
}).transform(
|
|
904
|
+
(data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)
|
|
905
|
+
),
|
|
906
|
+
z.object({
|
|
907
|
+
message: z.object({
|
|
908
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
909
|
+
})
|
|
910
|
+
}).transform(
|
|
911
|
+
(data) => data.message.reasoning_details.filter(isDefinedOrNotNull)
|
|
912
|
+
),
|
|
913
|
+
z.object({
|
|
914
|
+
text: z.string(),
|
|
915
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
916
|
+
}).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
|
|
917
|
+
]);
|
|
865
918
|
|
|
866
919
|
// src/schemas/error-response.ts
|
|
867
920
|
import { z as z2 } from "zod/v4";
|
|
@@ -878,6 +931,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
878
931
|
errorToMessage: (data) => data.error.message
|
|
879
932
|
});
|
|
880
933
|
|
|
934
|
+
// src/schemas/provider-metadata.ts
|
|
935
|
+
import { z as z3 } from "zod/v4";
|
|
936
|
+
var OpenRouterProviderMetadataSchema = z3.object({
|
|
937
|
+
provider: z3.string(),
|
|
938
|
+
reasoning_details: z3.array(ReasoningDetailUnionSchema).optional(),
|
|
939
|
+
usage: z3.object({
|
|
940
|
+
promptTokens: z3.number(),
|
|
941
|
+
promptTokensDetails: z3.object({
|
|
942
|
+
cachedTokens: z3.number()
|
|
943
|
+
}).optional(),
|
|
944
|
+
completionTokens: z3.number(),
|
|
945
|
+
completionTokensDetails: z3.object({
|
|
946
|
+
reasoningTokens: z3.number()
|
|
947
|
+
}).optional(),
|
|
948
|
+
totalTokens: z3.number(),
|
|
949
|
+
cost: z3.number().optional(),
|
|
950
|
+
costDetails: z3.object({
|
|
951
|
+
upstreamInferenceCost: z3.number()
|
|
952
|
+
})
|
|
953
|
+
})
|
|
954
|
+
});
|
|
955
|
+
var OpenRouterProviderOptionsSchema = z3.object({
|
|
956
|
+
openrouter: z3.object({
|
|
957
|
+
reasoning_details: z3.array(ReasoningDetailUnionSchema).optional()
|
|
958
|
+
}).optional()
|
|
959
|
+
}).optional();
|
|
960
|
+
|
|
881
961
|
// src/utils/map-finish-reason.ts
|
|
882
962
|
function mapOpenRouterFinishReason(finishReason) {
|
|
883
963
|
switch (finishReason) {
|
|
@@ -945,7 +1025,7 @@ function getCacheControl(providerMetadata) {
|
|
|
945
1025
|
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
1026
|
}
|
|
947
1027
|
function convertToOpenRouterChatMessages(prompt) {
|
|
948
|
-
var _a15, _b, _c;
|
|
1028
|
+
var _a15, _b, _c, _d, _e;
|
|
949
1029
|
const messages = [];
|
|
950
1030
|
for (const { role, content, providerOptions } of prompt) {
|
|
951
1031
|
switch (role) {
|
|
@@ -976,7 +1056,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
976
1056
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
977
1057
|
const contentParts = content.map(
|
|
978
1058
|
(part) => {
|
|
979
|
-
var _a16, _b2, _c2,
|
|
1059
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
980
1060
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
981
1061
|
switch (part.type) {
|
|
982
1062
|
case "text":
|
|
@@ -1002,7 +1082,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1002
1082
|
};
|
|
1003
1083
|
}
|
|
1004
1084
|
const fileName = String(
|
|
1005
|
-
(_f = (
|
|
1085
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1006
1086
|
);
|
|
1007
1087
|
const fileData = getFileUrl({
|
|
1008
1088
|
part,
|
|
@@ -1048,7 +1128,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1048
1128
|
case "assistant": {
|
|
1049
1129
|
let text = "";
|
|
1050
1130
|
let reasoning = "";
|
|
1051
|
-
const reasoningDetails = [];
|
|
1052
1131
|
const toolCalls = [];
|
|
1053
1132
|
for (const part of content) {
|
|
1054
1133
|
switch (part.type) {
|
|
@@ -1069,10 +1148,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1069
1148
|
}
|
|
1070
1149
|
case "reasoning": {
|
|
1071
1150
|
reasoning += part.text;
|
|
1072
|
-
reasoningDetails.push({
|
|
1073
|
-
type: "reasoning.text" /* Text */,
|
|
1074
|
-
text: part.text
|
|
1075
|
-
});
|
|
1076
1151
|
break;
|
|
1077
1152
|
}
|
|
1078
1153
|
case "file":
|
|
@@ -1082,12 +1157,15 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1082
1157
|
}
|
|
1083
1158
|
}
|
|
1084
1159
|
}
|
|
1160
|
+
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
|
|
1161
|
+
const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
|
|
1085
1162
|
messages.push({
|
|
1086
1163
|
role: "assistant",
|
|
1087
1164
|
content: text,
|
|
1088
1165
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1089
1166
|
reasoning: reasoning || void 0,
|
|
1090
|
-
reasoning_details
|
|
1167
|
+
// Only include reasoning_details if we have the preserved original version
|
|
1168
|
+
reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
|
|
1091
1169
|
cache_control: getCacheControl(providerOptions)
|
|
1092
1170
|
});
|
|
1093
1171
|
break;
|
|
@@ -1099,7 +1177,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1099
1177
|
role: "tool",
|
|
1100
1178
|
tool_call_id: toolResponse.toolCallId,
|
|
1101
1179
|
content: content2,
|
|
1102
|
-
cache_control: (
|
|
1180
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1103
1181
|
});
|
|
1104
1182
|
}
|
|
1105
1183
|
break;
|
|
@@ -1116,15 +1194,15 @@ function getToolResultContent(input) {
|
|
|
1116
1194
|
}
|
|
1117
1195
|
|
|
1118
1196
|
// src/chat/get-tool-choice.ts
|
|
1119
|
-
import { z as
|
|
1120
|
-
var ChatCompletionToolChoiceSchema =
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
type:
|
|
1126
|
-
function:
|
|
1127
|
-
name:
|
|
1197
|
+
import { z as z5 } from "zod/v4";
|
|
1198
|
+
var ChatCompletionToolChoiceSchema = z5.union([
|
|
1199
|
+
z5.literal("auto"),
|
|
1200
|
+
z5.literal("none"),
|
|
1201
|
+
z5.literal("required"),
|
|
1202
|
+
z5.object({
|
|
1203
|
+
type: z5.literal("function"),
|
|
1204
|
+
function: z5.object({
|
|
1205
|
+
name: z5.string()
|
|
1128
1206
|
})
|
|
1129
1207
|
})
|
|
1130
1208
|
]);
|
|
@@ -1142,150 +1220,210 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1142
1220
|
}
|
|
1143
1221
|
default: {
|
|
1144
1222
|
toolChoice;
|
|
1145
|
-
throw new
|
|
1223
|
+
throw new InvalidArgumentError({
|
|
1224
|
+
argument: "toolChoice",
|
|
1225
|
+
message: `Invalid tool choice type: ${JSON.stringify(toolChoice)}`
|
|
1226
|
+
});
|
|
1146
1227
|
}
|
|
1147
1228
|
}
|
|
1148
1229
|
}
|
|
1149
1230
|
|
|
1150
1231
|
// src/chat/schemas.ts
|
|
1151
|
-
import { z as
|
|
1232
|
+
import { z as z7 } from "zod/v4";
|
|
1152
1233
|
|
|
1153
1234
|
// src/schemas/image.ts
|
|
1154
|
-
import { z as
|
|
1155
|
-
var ImageResponseSchema =
|
|
1156
|
-
type:
|
|
1157
|
-
image_url:
|
|
1158
|
-
url:
|
|
1235
|
+
import { z as z6 } from "zod/v4";
|
|
1236
|
+
var ImageResponseSchema = z6.object({
|
|
1237
|
+
type: z6.literal("image_url"),
|
|
1238
|
+
image_url: z6.object({
|
|
1239
|
+
url: z6.string()
|
|
1159
1240
|
})
|
|
1160
1241
|
});
|
|
1161
|
-
var ImageResponseWithUnknownSchema =
|
|
1242
|
+
var ImageResponseWithUnknownSchema = z6.union([
|
|
1162
1243
|
ImageResponseSchema,
|
|
1163
|
-
|
|
1244
|
+
z6.unknown().transform(() => null)
|
|
1164
1245
|
]);
|
|
1165
|
-
var ImageResponseArraySchema =
|
|
1246
|
+
var ImageResponseArraySchema = z6.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1166
1247
|
|
|
1167
1248
|
// 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:
|
|
1249
|
+
var OpenRouterChatCompletionBaseResponseSchema = z7.object({
|
|
1250
|
+
id: z7.string().optional(),
|
|
1251
|
+
model: z7.string().optional(),
|
|
1252
|
+
provider: z7.string().optional(),
|
|
1253
|
+
usage: z7.object({
|
|
1254
|
+
prompt_tokens: z7.number(),
|
|
1255
|
+
prompt_tokens_details: z7.object({
|
|
1256
|
+
cached_tokens: z7.number()
|
|
1176
1257
|
}).nullish(),
|
|
1177
|
-
completion_tokens:
|
|
1178
|
-
completion_tokens_details:
|
|
1179
|
-
reasoning_tokens:
|
|
1258
|
+
completion_tokens: z7.number(),
|
|
1259
|
+
completion_tokens_details: z7.object({
|
|
1260
|
+
reasoning_tokens: z7.number()
|
|
1180
1261
|
}).nullish(),
|
|
1181
|
-
total_tokens:
|
|
1182
|
-
cost:
|
|
1183
|
-
cost_details:
|
|
1184
|
-
upstream_inference_cost:
|
|
1262
|
+
total_tokens: z7.number(),
|
|
1263
|
+
cost: z7.number().optional(),
|
|
1264
|
+
cost_details: z7.object({
|
|
1265
|
+
upstream_inference_cost: z7.number().nullish()
|
|
1185
1266
|
}).nullish()
|
|
1186
1267
|
}).nullish()
|
|
1187
1268
|
});
|
|
1188
|
-
var OpenRouterNonStreamChatCompletionResponseSchema =
|
|
1189
|
-
choices
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
annotations: z6.array(
|
|
1208
|
-
z6.object({
|
|
1209
|
-
type: z6.enum(["url_citation"]),
|
|
1210
|
-
url_citation: z6.object({
|
|
1211
|
-
end_index: z6.number(),
|
|
1212
|
-
start_index: z6.number(),
|
|
1213
|
-
title: z6.string(),
|
|
1214
|
-
url: z6.string(),
|
|
1215
|
-
content: z6.string().optional()
|
|
1269
|
+
var OpenRouterNonStreamChatCompletionResponseSchema = z7.union([
|
|
1270
|
+
// Success response with choices
|
|
1271
|
+
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1272
|
+
choices: z7.array(
|
|
1273
|
+
z7.object({
|
|
1274
|
+
message: z7.object({
|
|
1275
|
+
role: z7.literal("assistant"),
|
|
1276
|
+
content: z7.string().nullable().optional(),
|
|
1277
|
+
reasoning: z7.string().nullable().optional(),
|
|
1278
|
+
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1279
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1280
|
+
tool_calls: z7.array(
|
|
1281
|
+
z7.object({
|
|
1282
|
+
id: z7.string().optional().nullable(),
|
|
1283
|
+
type: z7.literal("function"),
|
|
1284
|
+
function: z7.object({
|
|
1285
|
+
name: z7.string(),
|
|
1286
|
+
arguments: z7.string()
|
|
1287
|
+
})
|
|
1216
1288
|
})
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1289
|
+
).optional(),
|
|
1290
|
+
annotations: z7.array(
|
|
1291
|
+
z7.union([
|
|
1292
|
+
// URL citation from web search
|
|
1293
|
+
z7.object({
|
|
1294
|
+
type: z7.literal("url_citation"),
|
|
1295
|
+
url_citation: z7.object({
|
|
1296
|
+
end_index: z7.number(),
|
|
1297
|
+
start_index: z7.number(),
|
|
1298
|
+
title: z7.string(),
|
|
1299
|
+
url: z7.string(),
|
|
1300
|
+
content: z7.string().optional()
|
|
1301
|
+
})
|
|
1302
|
+
}),
|
|
1303
|
+
// File annotation from FileParserPlugin (old format)
|
|
1304
|
+
z7.object({
|
|
1305
|
+
type: z7.literal("file_annotation"),
|
|
1306
|
+
file_annotation: z7.object({
|
|
1307
|
+
file_id: z7.string(),
|
|
1308
|
+
quote: z7.string().optional()
|
|
1309
|
+
})
|
|
1310
|
+
}),
|
|
1311
|
+
// File annotation from FileParserPlugin (new format)
|
|
1312
|
+
z7.object({
|
|
1313
|
+
type: z7.literal("file"),
|
|
1314
|
+
file: z7.object({
|
|
1315
|
+
hash: z7.string(),
|
|
1316
|
+
name: z7.string(),
|
|
1317
|
+
content: z7.array(
|
|
1318
|
+
z7.object({
|
|
1319
|
+
type: z7.string(),
|
|
1320
|
+
text: z7.string()
|
|
1321
|
+
})
|
|
1322
|
+
).optional()
|
|
1323
|
+
})
|
|
1230
1324
|
})
|
|
1231
|
-
)
|
|
1232
|
-
|
|
1233
|
-
)
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1325
|
+
])
|
|
1326
|
+
).nullish()
|
|
1327
|
+
}),
|
|
1328
|
+
index: z7.number().nullish(),
|
|
1329
|
+
logprobs: z7.object({
|
|
1330
|
+
content: z7.array(
|
|
1331
|
+
z7.object({
|
|
1332
|
+
token: z7.string(),
|
|
1333
|
+
logprob: z7.number(),
|
|
1334
|
+
top_logprobs: z7.array(
|
|
1335
|
+
z7.object({
|
|
1336
|
+
token: z7.string(),
|
|
1337
|
+
logprob: z7.number()
|
|
1338
|
+
})
|
|
1339
|
+
)
|
|
1340
|
+
})
|
|
1341
|
+
).nullable()
|
|
1342
|
+
}).nullable().optional(),
|
|
1343
|
+
finish_reason: z7.string().optional().nullable()
|
|
1344
|
+
})
|
|
1345
|
+
)
|
|
1346
|
+
}),
|
|
1347
|
+
// Error response (HTTP 200 with error payload)
|
|
1348
|
+
OpenRouterErrorResponseSchema.extend({
|
|
1349
|
+
user_id: z7.string().optional()
|
|
1350
|
+
})
|
|
1351
|
+
]);
|
|
1352
|
+
var OpenRouterStreamChatCompletionChunkSchema = z7.union([
|
|
1240
1353
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1241
|
-
choices:
|
|
1242
|
-
|
|
1243
|
-
delta:
|
|
1244
|
-
role:
|
|
1245
|
-
content:
|
|
1246
|
-
reasoning:
|
|
1354
|
+
choices: z7.array(
|
|
1355
|
+
z7.object({
|
|
1356
|
+
delta: z7.object({
|
|
1357
|
+
role: z7.enum(["assistant"]).optional(),
|
|
1358
|
+
content: z7.string().nullish(),
|
|
1359
|
+
reasoning: z7.string().nullish().optional(),
|
|
1247
1360
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1248
1361
|
images: ImageResponseArraySchema.nullish(),
|
|
1249
|
-
tool_calls:
|
|
1250
|
-
|
|
1251
|
-
index:
|
|
1252
|
-
id:
|
|
1253
|
-
type:
|
|
1254
|
-
function:
|
|
1255
|
-
name:
|
|
1256
|
-
arguments:
|
|
1362
|
+
tool_calls: z7.array(
|
|
1363
|
+
z7.object({
|
|
1364
|
+
index: z7.number().nullish(),
|
|
1365
|
+
id: z7.string().nullish(),
|
|
1366
|
+
type: z7.literal("function").optional(),
|
|
1367
|
+
function: z7.object({
|
|
1368
|
+
name: z7.string().nullish(),
|
|
1369
|
+
arguments: z7.string().nullish()
|
|
1257
1370
|
})
|
|
1258
1371
|
})
|
|
1259
1372
|
).nullish(),
|
|
1260
|
-
annotations:
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1373
|
+
annotations: z7.array(
|
|
1374
|
+
z7.union([
|
|
1375
|
+
// URL citation from web search
|
|
1376
|
+
z7.object({
|
|
1377
|
+
type: z7.literal("url_citation"),
|
|
1378
|
+
url_citation: z7.object({
|
|
1379
|
+
end_index: z7.number(),
|
|
1380
|
+
start_index: z7.number(),
|
|
1381
|
+
title: z7.string(),
|
|
1382
|
+
url: z7.string(),
|
|
1383
|
+
content: z7.string().optional()
|
|
1384
|
+
})
|
|
1385
|
+
}),
|
|
1386
|
+
// File annotation from FileParserPlugin (old format)
|
|
1387
|
+
z7.object({
|
|
1388
|
+
type: z7.literal("file_annotation"),
|
|
1389
|
+
file_annotation: z7.object({
|
|
1390
|
+
file_id: z7.string(),
|
|
1391
|
+
quote: z7.string().optional()
|
|
1392
|
+
})
|
|
1393
|
+
}),
|
|
1394
|
+
// File annotation from FileParserPlugin (new format)
|
|
1395
|
+
z7.object({
|
|
1396
|
+
type: z7.literal("file"),
|
|
1397
|
+
file: z7.object({
|
|
1398
|
+
hash: z7.string(),
|
|
1399
|
+
name: z7.string(),
|
|
1400
|
+
content: z7.array(
|
|
1401
|
+
z7.object({
|
|
1402
|
+
type: z7.string(),
|
|
1403
|
+
text: z7.string()
|
|
1404
|
+
})
|
|
1405
|
+
).optional()
|
|
1406
|
+
})
|
|
1269
1407
|
})
|
|
1270
|
-
|
|
1408
|
+
])
|
|
1271
1409
|
).nullish()
|
|
1272
1410
|
}).nullish(),
|
|
1273
|
-
logprobs:
|
|
1274
|
-
content:
|
|
1275
|
-
|
|
1276
|
-
token:
|
|
1277
|
-
logprob:
|
|
1278
|
-
top_logprobs:
|
|
1279
|
-
|
|
1280
|
-
token:
|
|
1281
|
-
logprob:
|
|
1411
|
+
logprobs: z7.object({
|
|
1412
|
+
content: z7.array(
|
|
1413
|
+
z7.object({
|
|
1414
|
+
token: z7.string(),
|
|
1415
|
+
logprob: z7.number(),
|
|
1416
|
+
top_logprobs: z7.array(
|
|
1417
|
+
z7.object({
|
|
1418
|
+
token: z7.string(),
|
|
1419
|
+
logprob: z7.number()
|
|
1282
1420
|
})
|
|
1283
1421
|
)
|
|
1284
1422
|
})
|
|
1285
1423
|
).nullable()
|
|
1286
1424
|
}).nullish(),
|
|
1287
|
-
finish_reason:
|
|
1288
|
-
index:
|
|
1425
|
+
finish_reason: z7.string().nullable().optional(),
|
|
1426
|
+
index: z7.number().nullish()
|
|
1289
1427
|
})
|
|
1290
1428
|
)
|
|
1291
1429
|
}),
|
|
@@ -1388,11 +1526,11 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1388
1526
|
return baseArgs;
|
|
1389
1527
|
}
|
|
1390
1528
|
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;
|
|
1529
|
+
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
1530
|
const providerOptions = options.providerOptions || {};
|
|
1393
1531
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1394
1532
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
1395
|
-
const { value:
|
|
1533
|
+
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
1396
1534
|
url: this.config.url({
|
|
1397
1535
|
path: "/chat/completions",
|
|
1398
1536
|
modelId: this.modelId
|
|
@@ -1406,9 +1544,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1406
1544
|
abortSignal: options.abortSignal,
|
|
1407
1545
|
fetch: this.config.fetch
|
|
1408
1546
|
});
|
|
1547
|
+
if ("error" in responseValue) {
|
|
1548
|
+
throw new APICallError({
|
|
1549
|
+
message: responseValue.error.message,
|
|
1550
|
+
url: this.config.url({
|
|
1551
|
+
path: "/chat/completions",
|
|
1552
|
+
modelId: this.modelId
|
|
1553
|
+
}),
|
|
1554
|
+
requestBodyValues: args,
|
|
1555
|
+
statusCode: 200,
|
|
1556
|
+
responseHeaders,
|
|
1557
|
+
data: responseValue.error
|
|
1558
|
+
});
|
|
1559
|
+
}
|
|
1560
|
+
const response = responseValue;
|
|
1409
1561
|
const choice = response.choices[0];
|
|
1410
1562
|
if (!choice) {
|
|
1411
|
-
throw new
|
|
1563
|
+
throw new NoContentGeneratedError({
|
|
1564
|
+
message: "No choice in response"
|
|
1565
|
+
});
|
|
1412
1566
|
}
|
|
1413
1567
|
const usageInfo = response.usage ? {
|
|
1414
1568
|
inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
|
|
@@ -1515,24 +1669,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1515
1669
|
usage: usageInfo,
|
|
1516
1670
|
warnings: [],
|
|
1517
1671
|
providerMetadata: {
|
|
1518
|
-
openrouter: {
|
|
1672
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1519
1673
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1674
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1520
1675
|
usage: {
|
|
1521
|
-
promptTokens: (
|
|
1522
|
-
completionTokens: (
|
|
1523
|
-
totalTokens: (
|
|
1524
|
-
cost: (
|
|
1676
|
+
promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
|
|
1677
|
+
completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
|
|
1678
|
+
totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
|
|
1679
|
+
cost: (_p = response.usage) == null ? void 0 : _p.cost,
|
|
1525
1680
|
promptTokensDetails: {
|
|
1526
|
-
cachedTokens: (
|
|
1681
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1527
1682
|
},
|
|
1528
1683
|
completionTokensDetails: {
|
|
1529
|
-
reasoningTokens: (
|
|
1684
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1530
1685
|
},
|
|
1531
1686
|
costDetails: {
|
|
1532
|
-
upstreamInferenceCost: (
|
|
1687
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1533
1688
|
}
|
|
1534
1689
|
}
|
|
1535
|
-
}
|
|
1690
|
+
})
|
|
1536
1691
|
},
|
|
1537
1692
|
request: { body: args },
|
|
1538
1693
|
response: {
|
|
@@ -1577,6 +1732,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1577
1732
|
cachedInputTokens: Number.NaN
|
|
1578
1733
|
};
|
|
1579
1734
|
const openrouterUsage = {};
|
|
1735
|
+
const accumulatedReasoningDetails = [];
|
|
1580
1736
|
let textStarted = false;
|
|
1581
1737
|
let reasoningStarted = false;
|
|
1582
1738
|
let textId;
|
|
@@ -1662,6 +1818,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1662
1818
|
});
|
|
1663
1819
|
};
|
|
1664
1820
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1821
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1665
1822
|
for (const detail of delta.reasoning_details) {
|
|
1666
1823
|
switch (detail.type) {
|
|
1667
1824
|
case "reasoning.text" /* Text */: {
|
|
@@ -1765,7 +1922,10 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1765
1922
|
};
|
|
1766
1923
|
const toolCall2 = toolCalls[index];
|
|
1767
1924
|
if (toolCall2 == null) {
|
|
1768
|
-
throw new
|
|
1925
|
+
throw new InvalidResponseDataError({
|
|
1926
|
+
data: { index, toolCallsLength: toolCalls.length },
|
|
1927
|
+
message: `Tool call at index ${index} is missing after creation.`
|
|
1928
|
+
});
|
|
1769
1929
|
}
|
|
1770
1930
|
if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
|
|
1771
1931
|
toolCall2.inputStarted = true;
|
|
@@ -1795,7 +1955,14 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1795
1955
|
}
|
|
1796
1956
|
const toolCall = toolCalls[index];
|
|
1797
1957
|
if (toolCall == null) {
|
|
1798
|
-
throw new
|
|
1958
|
+
throw new InvalidResponseDataError({
|
|
1959
|
+
data: {
|
|
1960
|
+
index,
|
|
1961
|
+
toolCallsLength: toolCalls.length,
|
|
1962
|
+
toolCallDelta
|
|
1963
|
+
},
|
|
1964
|
+
message: `Tool call at index ${index} is missing during merge.`
|
|
1965
|
+
});
|
|
1799
1966
|
}
|
|
1800
1967
|
if (!toolCall.inputStarted) {
|
|
1801
1968
|
toolCall.inputStarted = true;
|
|
@@ -1868,6 +2035,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1868
2035
|
if (provider !== void 0) {
|
|
1869
2036
|
openrouterMetadata.provider = provider;
|
|
1870
2037
|
}
|
|
2038
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
2039
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
2040
|
+
}
|
|
1871
2041
|
controller.enqueue({
|
|
1872
2042
|
type: "finish",
|
|
1873
2043
|
finishReason,
|
|
@@ -1990,36 +2160,36 @@ ${assistantMessage}
|
|
|
1990
2160
|
}
|
|
1991
2161
|
|
|
1992
2162
|
// src/completion/schemas.ts
|
|
1993
|
-
import { z as
|
|
1994
|
-
var OpenRouterCompletionChunkSchema =
|
|
1995
|
-
|
|
1996
|
-
id:
|
|
1997
|
-
model:
|
|
1998
|
-
choices:
|
|
1999
|
-
|
|
2000
|
-
text:
|
|
2001
|
-
reasoning:
|
|
2163
|
+
import { z as z8 } from "zod/v4";
|
|
2164
|
+
var OpenRouterCompletionChunkSchema = z8.union([
|
|
2165
|
+
z8.object({
|
|
2166
|
+
id: z8.string().optional(),
|
|
2167
|
+
model: z8.string().optional(),
|
|
2168
|
+
choices: z8.array(
|
|
2169
|
+
z8.object({
|
|
2170
|
+
text: z8.string(),
|
|
2171
|
+
reasoning: z8.string().nullish().optional(),
|
|
2002
2172
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2003
|
-
finish_reason:
|
|
2004
|
-
index:
|
|
2005
|
-
logprobs:
|
|
2006
|
-
tokens:
|
|
2007
|
-
token_logprobs:
|
|
2008
|
-
top_logprobs:
|
|
2173
|
+
finish_reason: z8.string().nullish(),
|
|
2174
|
+
index: z8.number().nullish(),
|
|
2175
|
+
logprobs: z8.object({
|
|
2176
|
+
tokens: z8.array(z8.string()),
|
|
2177
|
+
token_logprobs: z8.array(z8.number()),
|
|
2178
|
+
top_logprobs: z8.array(z8.record(z8.string(), z8.number())).nullable()
|
|
2009
2179
|
}).nullable().optional()
|
|
2010
2180
|
})
|
|
2011
2181
|
),
|
|
2012
|
-
usage:
|
|
2013
|
-
prompt_tokens:
|
|
2014
|
-
prompt_tokens_details:
|
|
2015
|
-
cached_tokens:
|
|
2182
|
+
usage: z8.object({
|
|
2183
|
+
prompt_tokens: z8.number(),
|
|
2184
|
+
prompt_tokens_details: z8.object({
|
|
2185
|
+
cached_tokens: z8.number()
|
|
2016
2186
|
}).nullish(),
|
|
2017
|
-
completion_tokens:
|
|
2018
|
-
completion_tokens_details:
|
|
2019
|
-
reasoning_tokens:
|
|
2187
|
+
completion_tokens: z8.number(),
|
|
2188
|
+
completion_tokens_details: z8.object({
|
|
2189
|
+
reasoning_tokens: z8.number()
|
|
2020
2190
|
}).nullish(),
|
|
2021
|
-
total_tokens:
|
|
2022
|
-
cost:
|
|
2191
|
+
total_tokens: z8.number(),
|
|
2192
|
+
cost: z8.number().optional()
|
|
2023
2193
|
}).nullish()
|
|
2024
2194
|
}),
|
|
2025
2195
|
OpenRouterErrorResponseSchema
|
|
@@ -2117,11 +2287,23 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2117
2287
|
fetch: this.config.fetch
|
|
2118
2288
|
});
|
|
2119
2289
|
if ("error" in response) {
|
|
2120
|
-
throw new
|
|
2290
|
+
throw new APICallError({
|
|
2291
|
+
message: response.error.message,
|
|
2292
|
+
url: this.config.url({
|
|
2293
|
+
path: "/completions",
|
|
2294
|
+
modelId: this.modelId
|
|
2295
|
+
}),
|
|
2296
|
+
requestBodyValues: args,
|
|
2297
|
+
statusCode: 200,
|
|
2298
|
+
responseHeaders,
|
|
2299
|
+
data: response.error
|
|
2300
|
+
});
|
|
2121
2301
|
}
|
|
2122
2302
|
const choice = response.choices[0];
|
|
2123
2303
|
if (!choice) {
|
|
2124
|
-
throw new
|
|
2304
|
+
throw new NoContentGeneratedError({
|
|
2305
|
+
message: "No choice in OpenRouter completion response"
|
|
2306
|
+
});
|
|
2125
2307
|
}
|
|
2126
2308
|
return {
|
|
2127
2309
|
content: [
|