@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/index.mjs
CHANGED
|
@@ -218,6 +218,18 @@ var name9 = "AI_NoContentGeneratedError";
|
|
|
218
218
|
var marker10 = `vercel.ai.error.${name9}`;
|
|
219
219
|
var symbol10 = Symbol.for(marker10);
|
|
220
220
|
var _a10;
|
|
221
|
+
var NoContentGeneratedError = class extends AISDKError {
|
|
222
|
+
// used in isInstance
|
|
223
|
+
constructor({
|
|
224
|
+
message = "No content generated."
|
|
225
|
+
} = {}) {
|
|
226
|
+
super({ name: name9, message });
|
|
227
|
+
this[_a10] = true;
|
|
228
|
+
}
|
|
229
|
+
static isInstance(error) {
|
|
230
|
+
return AISDKError.hasMarker(error, marker10);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
221
233
|
_a10 = symbol10;
|
|
222
234
|
var name10 = "AI_NoSuchModelError";
|
|
223
235
|
var marker11 = `vercel.ai.error.${name10}`;
|
|
@@ -884,19 +896,40 @@ function withoutTrailingSlash(url) {
|
|
|
884
896
|
|
|
885
897
|
// src/schemas/reasoning-details.ts
|
|
886
898
|
import { z } from "zod/v4";
|
|
899
|
+
|
|
900
|
+
// src/utils/type-guards.ts
|
|
901
|
+
function isDefinedOrNotNull(value) {
|
|
902
|
+
return value !== null && value !== void 0;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// src/schemas/format.ts
|
|
906
|
+
var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
|
|
907
|
+
ReasoningFormat2["Unknown"] = "unknown";
|
|
908
|
+
ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
|
|
909
|
+
ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
|
|
910
|
+
ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
|
|
911
|
+
return ReasoningFormat2;
|
|
912
|
+
})(ReasoningFormat || {});
|
|
913
|
+
|
|
914
|
+
// src/schemas/reasoning-details.ts
|
|
915
|
+
var CommonReasoningDetailSchema = z.object({
|
|
916
|
+
id: z.string().nullish(),
|
|
917
|
+
format: z.nativeEnum(ReasoningFormat).nullish(),
|
|
918
|
+
index: z.number().optional()
|
|
919
|
+
});
|
|
887
920
|
var ReasoningDetailSummarySchema = z.object({
|
|
888
921
|
type: z.literal("reasoning.summary" /* Summary */),
|
|
889
922
|
summary: z.string()
|
|
890
|
-
});
|
|
923
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
891
924
|
var ReasoningDetailEncryptedSchema = z.object({
|
|
892
925
|
type: z.literal("reasoning.encrypted" /* Encrypted */),
|
|
893
926
|
data: z.string()
|
|
894
|
-
});
|
|
927
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
895
928
|
var ReasoningDetailTextSchema = z.object({
|
|
896
929
|
type: z.literal("reasoning.text" /* Text */),
|
|
897
930
|
text: z.string().nullish(),
|
|
898
931
|
signature: z.string().nullish()
|
|
899
|
-
});
|
|
932
|
+
}).extend(CommonReasoningDetailSchema.shape);
|
|
900
933
|
var ReasoningDetailUnionSchema = z.union([
|
|
901
934
|
ReasoningDetailSummarySchema,
|
|
902
935
|
ReasoningDetailEncryptedSchema,
|
|
@@ -907,6 +940,26 @@ var ReasoningDetailsWithUnknownSchema = z.union([
|
|
|
907
940
|
z.unknown().transform(() => null)
|
|
908
941
|
]);
|
|
909
942
|
var ReasoningDetailArraySchema = z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
943
|
+
var OutputUnionToReasoningDetailsSchema = z.union([
|
|
944
|
+
z.object({
|
|
945
|
+
delta: z.object({
|
|
946
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
947
|
+
})
|
|
948
|
+
}).transform(
|
|
949
|
+
(data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)
|
|
950
|
+
),
|
|
951
|
+
z.object({
|
|
952
|
+
message: z.object({
|
|
953
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
954
|
+
})
|
|
955
|
+
}).transform(
|
|
956
|
+
(data) => data.message.reasoning_details.filter(isDefinedOrNotNull)
|
|
957
|
+
),
|
|
958
|
+
z.object({
|
|
959
|
+
text: z.string(),
|
|
960
|
+
reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
|
|
961
|
+
}).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
|
|
962
|
+
]);
|
|
910
963
|
|
|
911
964
|
// src/schemas/error-response.ts
|
|
912
965
|
import { z as z2 } from "zod/v4";
|
|
@@ -923,6 +976,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
923
976
|
errorToMessage: (data) => data.error.message
|
|
924
977
|
});
|
|
925
978
|
|
|
979
|
+
// src/schemas/provider-metadata.ts
|
|
980
|
+
import { z as z3 } from "zod/v4";
|
|
981
|
+
var OpenRouterProviderMetadataSchema = z3.object({
|
|
982
|
+
provider: z3.string(),
|
|
983
|
+
reasoning_details: z3.array(ReasoningDetailUnionSchema).optional(),
|
|
984
|
+
usage: z3.object({
|
|
985
|
+
promptTokens: z3.number(),
|
|
986
|
+
promptTokensDetails: z3.object({
|
|
987
|
+
cachedTokens: z3.number()
|
|
988
|
+
}).optional(),
|
|
989
|
+
completionTokens: z3.number(),
|
|
990
|
+
completionTokensDetails: z3.object({
|
|
991
|
+
reasoningTokens: z3.number()
|
|
992
|
+
}).optional(),
|
|
993
|
+
totalTokens: z3.number(),
|
|
994
|
+
cost: z3.number().optional(),
|
|
995
|
+
costDetails: z3.object({
|
|
996
|
+
upstreamInferenceCost: z3.number()
|
|
997
|
+
})
|
|
998
|
+
})
|
|
999
|
+
});
|
|
1000
|
+
var OpenRouterProviderOptionsSchema = z3.object({
|
|
1001
|
+
openrouter: z3.object({
|
|
1002
|
+
reasoning_details: z3.array(ReasoningDetailUnionSchema).optional()
|
|
1003
|
+
}).optional()
|
|
1004
|
+
}).optional();
|
|
1005
|
+
|
|
926
1006
|
// src/utils/map-finish-reason.ts
|
|
927
1007
|
function mapOpenRouterFinishReason(finishReason) {
|
|
928
1008
|
switch (finishReason) {
|
|
@@ -990,7 +1070,7 @@ function getCacheControl(providerMetadata) {
|
|
|
990
1070
|
return (_c = (_b = (_a15 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a15 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
|
|
991
1071
|
}
|
|
992
1072
|
function convertToOpenRouterChatMessages(prompt) {
|
|
993
|
-
var _a15, _b, _c;
|
|
1073
|
+
var _a15, _b, _c, _d, _e;
|
|
994
1074
|
const messages = [];
|
|
995
1075
|
for (const { role, content, providerOptions } of prompt) {
|
|
996
1076
|
switch (role) {
|
|
@@ -1021,7 +1101,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1021
1101
|
const messageCacheControl = getCacheControl(providerOptions);
|
|
1022
1102
|
const contentParts = content.map(
|
|
1023
1103
|
(part) => {
|
|
1024
|
-
var _a16, _b2, _c2,
|
|
1104
|
+
var _a16, _b2, _c2, _d2, _e2, _f;
|
|
1025
1105
|
const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
|
|
1026
1106
|
switch (part.type) {
|
|
1027
1107
|
case "text":
|
|
@@ -1047,7 +1127,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1047
1127
|
};
|
|
1048
1128
|
}
|
|
1049
1129
|
const fileName = String(
|
|
1050
|
-
(_f = (
|
|
1130
|
+
(_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
|
|
1051
1131
|
);
|
|
1052
1132
|
const fileData = getFileUrl({
|
|
1053
1133
|
part,
|
|
@@ -1093,7 +1173,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1093
1173
|
case "assistant": {
|
|
1094
1174
|
let text = "";
|
|
1095
1175
|
let reasoning = "";
|
|
1096
|
-
const reasoningDetails = [];
|
|
1097
1176
|
const toolCalls = [];
|
|
1098
1177
|
for (const part of content) {
|
|
1099
1178
|
switch (part.type) {
|
|
@@ -1114,10 +1193,6 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1114
1193
|
}
|
|
1115
1194
|
case "reasoning": {
|
|
1116
1195
|
reasoning += part.text;
|
|
1117
|
-
reasoningDetails.push({
|
|
1118
|
-
type: "reasoning.text" /* Text */,
|
|
1119
|
-
text: part.text
|
|
1120
|
-
});
|
|
1121
1196
|
break;
|
|
1122
1197
|
}
|
|
1123
1198
|
case "file":
|
|
@@ -1127,12 +1202,15 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1127
1202
|
}
|
|
1128
1203
|
}
|
|
1129
1204
|
}
|
|
1205
|
+
const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
|
|
1206
|
+
const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
|
|
1130
1207
|
messages.push({
|
|
1131
1208
|
role: "assistant",
|
|
1132
1209
|
content: text,
|
|
1133
1210
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1134
1211
|
reasoning: reasoning || void 0,
|
|
1135
|
-
reasoning_details
|
|
1212
|
+
// Only include reasoning_details if we have the preserved original version
|
|
1213
|
+
reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
|
|
1136
1214
|
cache_control: getCacheControl(providerOptions)
|
|
1137
1215
|
});
|
|
1138
1216
|
break;
|
|
@@ -1144,7 +1222,7 @@ function convertToOpenRouterChatMessages(prompt) {
|
|
|
1144
1222
|
role: "tool",
|
|
1145
1223
|
tool_call_id: toolResponse.toolCallId,
|
|
1146
1224
|
content: content2,
|
|
1147
|
-
cache_control: (
|
|
1225
|
+
cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
|
|
1148
1226
|
});
|
|
1149
1227
|
}
|
|
1150
1228
|
break;
|
|
@@ -1161,15 +1239,15 @@ function getToolResultContent(input) {
|
|
|
1161
1239
|
}
|
|
1162
1240
|
|
|
1163
1241
|
// src/chat/get-tool-choice.ts
|
|
1164
|
-
import { z as
|
|
1165
|
-
var ChatCompletionToolChoiceSchema =
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
type:
|
|
1171
|
-
function:
|
|
1172
|
-
name:
|
|
1242
|
+
import { z as z5 } from "zod/v4";
|
|
1243
|
+
var ChatCompletionToolChoiceSchema = z5.union([
|
|
1244
|
+
z5.literal("auto"),
|
|
1245
|
+
z5.literal("none"),
|
|
1246
|
+
z5.literal("required"),
|
|
1247
|
+
z5.object({
|
|
1248
|
+
type: z5.literal("function"),
|
|
1249
|
+
function: z5.object({
|
|
1250
|
+
name: z5.string()
|
|
1173
1251
|
})
|
|
1174
1252
|
})
|
|
1175
1253
|
]);
|
|
@@ -1187,150 +1265,210 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1187
1265
|
}
|
|
1188
1266
|
default: {
|
|
1189
1267
|
toolChoice;
|
|
1190
|
-
throw new
|
|
1268
|
+
throw new InvalidArgumentError({
|
|
1269
|
+
argument: "toolChoice",
|
|
1270
|
+
message: `Invalid tool choice type: ${JSON.stringify(toolChoice)}`
|
|
1271
|
+
});
|
|
1191
1272
|
}
|
|
1192
1273
|
}
|
|
1193
1274
|
}
|
|
1194
1275
|
|
|
1195
1276
|
// src/chat/schemas.ts
|
|
1196
|
-
import { z as
|
|
1277
|
+
import { z as z7 } from "zod/v4";
|
|
1197
1278
|
|
|
1198
1279
|
// src/schemas/image.ts
|
|
1199
|
-
import { z as
|
|
1200
|
-
var ImageResponseSchema =
|
|
1201
|
-
type:
|
|
1202
|
-
image_url:
|
|
1203
|
-
url:
|
|
1280
|
+
import { z as z6 } from "zod/v4";
|
|
1281
|
+
var ImageResponseSchema = z6.object({
|
|
1282
|
+
type: z6.literal("image_url"),
|
|
1283
|
+
image_url: z6.object({
|
|
1284
|
+
url: z6.string()
|
|
1204
1285
|
})
|
|
1205
1286
|
});
|
|
1206
|
-
var ImageResponseWithUnknownSchema =
|
|
1287
|
+
var ImageResponseWithUnknownSchema = z6.union([
|
|
1207
1288
|
ImageResponseSchema,
|
|
1208
|
-
|
|
1289
|
+
z6.unknown().transform(() => null)
|
|
1209
1290
|
]);
|
|
1210
|
-
var ImageResponseArraySchema =
|
|
1291
|
+
var ImageResponseArraySchema = z6.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1211
1292
|
|
|
1212
1293
|
// src/chat/schemas.ts
|
|
1213
|
-
var OpenRouterChatCompletionBaseResponseSchema =
|
|
1214
|
-
id:
|
|
1215
|
-
model:
|
|
1216
|
-
provider:
|
|
1217
|
-
usage:
|
|
1218
|
-
prompt_tokens:
|
|
1219
|
-
prompt_tokens_details:
|
|
1220
|
-
cached_tokens:
|
|
1294
|
+
var OpenRouterChatCompletionBaseResponseSchema = z7.object({
|
|
1295
|
+
id: z7.string().optional(),
|
|
1296
|
+
model: z7.string().optional(),
|
|
1297
|
+
provider: z7.string().optional(),
|
|
1298
|
+
usage: z7.object({
|
|
1299
|
+
prompt_tokens: z7.number(),
|
|
1300
|
+
prompt_tokens_details: z7.object({
|
|
1301
|
+
cached_tokens: z7.number()
|
|
1221
1302
|
}).nullish(),
|
|
1222
|
-
completion_tokens:
|
|
1223
|
-
completion_tokens_details:
|
|
1224
|
-
reasoning_tokens:
|
|
1303
|
+
completion_tokens: z7.number(),
|
|
1304
|
+
completion_tokens_details: z7.object({
|
|
1305
|
+
reasoning_tokens: z7.number()
|
|
1225
1306
|
}).nullish(),
|
|
1226
|
-
total_tokens:
|
|
1227
|
-
cost:
|
|
1228
|
-
cost_details:
|
|
1229
|
-
upstream_inference_cost:
|
|
1307
|
+
total_tokens: z7.number(),
|
|
1308
|
+
cost: z7.number().optional(),
|
|
1309
|
+
cost_details: z7.object({
|
|
1310
|
+
upstream_inference_cost: z7.number().nullish()
|
|
1230
1311
|
}).nullish()
|
|
1231
1312
|
}).nullish()
|
|
1232
1313
|
});
|
|
1233
|
-
var OpenRouterNonStreamChatCompletionResponseSchema =
|
|
1234
|
-
choices
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
annotations: z6.array(
|
|
1253
|
-
z6.object({
|
|
1254
|
-
type: z6.enum(["url_citation"]),
|
|
1255
|
-
url_citation: z6.object({
|
|
1256
|
-
end_index: z6.number(),
|
|
1257
|
-
start_index: z6.number(),
|
|
1258
|
-
title: z6.string(),
|
|
1259
|
-
url: z6.string(),
|
|
1260
|
-
content: z6.string().optional()
|
|
1314
|
+
var OpenRouterNonStreamChatCompletionResponseSchema = z7.union([
|
|
1315
|
+
// Success response with choices
|
|
1316
|
+
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1317
|
+
choices: z7.array(
|
|
1318
|
+
z7.object({
|
|
1319
|
+
message: z7.object({
|
|
1320
|
+
role: z7.literal("assistant"),
|
|
1321
|
+
content: z7.string().nullable().optional(),
|
|
1322
|
+
reasoning: z7.string().nullable().optional(),
|
|
1323
|
+
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1324
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1325
|
+
tool_calls: z7.array(
|
|
1326
|
+
z7.object({
|
|
1327
|
+
id: z7.string().optional().nullable(),
|
|
1328
|
+
type: z7.literal("function"),
|
|
1329
|
+
function: z7.object({
|
|
1330
|
+
name: z7.string(),
|
|
1331
|
+
arguments: z7.string()
|
|
1332
|
+
})
|
|
1261
1333
|
})
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1334
|
+
).optional(),
|
|
1335
|
+
annotations: z7.array(
|
|
1336
|
+
z7.union([
|
|
1337
|
+
// URL citation from web search
|
|
1338
|
+
z7.object({
|
|
1339
|
+
type: z7.literal("url_citation"),
|
|
1340
|
+
url_citation: z7.object({
|
|
1341
|
+
end_index: z7.number(),
|
|
1342
|
+
start_index: z7.number(),
|
|
1343
|
+
title: z7.string(),
|
|
1344
|
+
url: z7.string(),
|
|
1345
|
+
content: z7.string().optional()
|
|
1346
|
+
})
|
|
1347
|
+
}),
|
|
1348
|
+
// File annotation from FileParserPlugin (old format)
|
|
1349
|
+
z7.object({
|
|
1350
|
+
type: z7.literal("file_annotation"),
|
|
1351
|
+
file_annotation: z7.object({
|
|
1352
|
+
file_id: z7.string(),
|
|
1353
|
+
quote: z7.string().optional()
|
|
1354
|
+
})
|
|
1355
|
+
}),
|
|
1356
|
+
// File annotation from FileParserPlugin (new format)
|
|
1357
|
+
z7.object({
|
|
1358
|
+
type: z7.literal("file"),
|
|
1359
|
+
file: z7.object({
|
|
1360
|
+
hash: z7.string(),
|
|
1361
|
+
name: z7.string(),
|
|
1362
|
+
content: z7.array(
|
|
1363
|
+
z7.object({
|
|
1364
|
+
type: z7.string(),
|
|
1365
|
+
text: z7.string()
|
|
1366
|
+
})
|
|
1367
|
+
).optional()
|
|
1368
|
+
})
|
|
1275
1369
|
})
|
|
1276
|
-
)
|
|
1277
|
-
|
|
1278
|
-
)
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1370
|
+
])
|
|
1371
|
+
).nullish()
|
|
1372
|
+
}),
|
|
1373
|
+
index: z7.number().nullish(),
|
|
1374
|
+
logprobs: z7.object({
|
|
1375
|
+
content: z7.array(
|
|
1376
|
+
z7.object({
|
|
1377
|
+
token: z7.string(),
|
|
1378
|
+
logprob: z7.number(),
|
|
1379
|
+
top_logprobs: z7.array(
|
|
1380
|
+
z7.object({
|
|
1381
|
+
token: z7.string(),
|
|
1382
|
+
logprob: z7.number()
|
|
1383
|
+
})
|
|
1384
|
+
)
|
|
1385
|
+
})
|
|
1386
|
+
).nullable()
|
|
1387
|
+
}).nullable().optional(),
|
|
1388
|
+
finish_reason: z7.string().optional().nullable()
|
|
1389
|
+
})
|
|
1390
|
+
)
|
|
1391
|
+
}),
|
|
1392
|
+
// Error response (HTTP 200 with error payload)
|
|
1393
|
+
OpenRouterErrorResponseSchema.extend({
|
|
1394
|
+
user_id: z7.string().optional()
|
|
1395
|
+
})
|
|
1396
|
+
]);
|
|
1397
|
+
var OpenRouterStreamChatCompletionChunkSchema = z7.union([
|
|
1285
1398
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1286
|
-
choices:
|
|
1287
|
-
|
|
1288
|
-
delta:
|
|
1289
|
-
role:
|
|
1290
|
-
content:
|
|
1291
|
-
reasoning:
|
|
1399
|
+
choices: z7.array(
|
|
1400
|
+
z7.object({
|
|
1401
|
+
delta: z7.object({
|
|
1402
|
+
role: z7.enum(["assistant"]).optional(),
|
|
1403
|
+
content: z7.string().nullish(),
|
|
1404
|
+
reasoning: z7.string().nullish().optional(),
|
|
1292
1405
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1293
1406
|
images: ImageResponseArraySchema.nullish(),
|
|
1294
|
-
tool_calls:
|
|
1295
|
-
|
|
1296
|
-
index:
|
|
1297
|
-
id:
|
|
1298
|
-
type:
|
|
1299
|
-
function:
|
|
1300
|
-
name:
|
|
1301
|
-
arguments:
|
|
1407
|
+
tool_calls: z7.array(
|
|
1408
|
+
z7.object({
|
|
1409
|
+
index: z7.number().nullish(),
|
|
1410
|
+
id: z7.string().nullish(),
|
|
1411
|
+
type: z7.literal("function").optional(),
|
|
1412
|
+
function: z7.object({
|
|
1413
|
+
name: z7.string().nullish(),
|
|
1414
|
+
arguments: z7.string().nullish()
|
|
1302
1415
|
})
|
|
1303
1416
|
})
|
|
1304
1417
|
).nullish(),
|
|
1305
|
-
annotations:
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1418
|
+
annotations: z7.array(
|
|
1419
|
+
z7.union([
|
|
1420
|
+
// URL citation from web search
|
|
1421
|
+
z7.object({
|
|
1422
|
+
type: z7.literal("url_citation"),
|
|
1423
|
+
url_citation: z7.object({
|
|
1424
|
+
end_index: z7.number(),
|
|
1425
|
+
start_index: z7.number(),
|
|
1426
|
+
title: z7.string(),
|
|
1427
|
+
url: z7.string(),
|
|
1428
|
+
content: z7.string().optional()
|
|
1429
|
+
})
|
|
1430
|
+
}),
|
|
1431
|
+
// File annotation from FileParserPlugin (old format)
|
|
1432
|
+
z7.object({
|
|
1433
|
+
type: z7.literal("file_annotation"),
|
|
1434
|
+
file_annotation: z7.object({
|
|
1435
|
+
file_id: z7.string(),
|
|
1436
|
+
quote: z7.string().optional()
|
|
1437
|
+
})
|
|
1438
|
+
}),
|
|
1439
|
+
// File annotation from FileParserPlugin (new format)
|
|
1440
|
+
z7.object({
|
|
1441
|
+
type: z7.literal("file"),
|
|
1442
|
+
file: z7.object({
|
|
1443
|
+
hash: z7.string(),
|
|
1444
|
+
name: z7.string(),
|
|
1445
|
+
content: z7.array(
|
|
1446
|
+
z7.object({
|
|
1447
|
+
type: z7.string(),
|
|
1448
|
+
text: z7.string()
|
|
1449
|
+
})
|
|
1450
|
+
).optional()
|
|
1451
|
+
})
|
|
1314
1452
|
})
|
|
1315
|
-
|
|
1453
|
+
])
|
|
1316
1454
|
).nullish()
|
|
1317
1455
|
}).nullish(),
|
|
1318
|
-
logprobs:
|
|
1319
|
-
content:
|
|
1320
|
-
|
|
1321
|
-
token:
|
|
1322
|
-
logprob:
|
|
1323
|
-
top_logprobs:
|
|
1324
|
-
|
|
1325
|
-
token:
|
|
1326
|
-
logprob:
|
|
1456
|
+
logprobs: z7.object({
|
|
1457
|
+
content: z7.array(
|
|
1458
|
+
z7.object({
|
|
1459
|
+
token: z7.string(),
|
|
1460
|
+
logprob: z7.number(),
|
|
1461
|
+
top_logprobs: z7.array(
|
|
1462
|
+
z7.object({
|
|
1463
|
+
token: z7.string(),
|
|
1464
|
+
logprob: z7.number()
|
|
1327
1465
|
})
|
|
1328
1466
|
)
|
|
1329
1467
|
})
|
|
1330
1468
|
).nullable()
|
|
1331
1469
|
}).nullish(),
|
|
1332
|
-
finish_reason:
|
|
1333
|
-
index:
|
|
1470
|
+
finish_reason: z7.string().nullable().optional(),
|
|
1471
|
+
index: z7.number().nullish()
|
|
1334
1472
|
})
|
|
1335
1473
|
)
|
|
1336
1474
|
}),
|
|
@@ -1433,11 +1571,11 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1433
1571
|
return baseArgs;
|
|
1434
1572
|
}
|
|
1435
1573
|
async doGenerate(options) {
|
|
1436
|
-
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
|
|
1574
|
+
var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
|
|
1437
1575
|
const providerOptions = options.providerOptions || {};
|
|
1438
1576
|
const openrouterOptions = providerOptions.openrouter || {};
|
|
1439
1577
|
const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
|
|
1440
|
-
const { value:
|
|
1578
|
+
const { value: responseValue, responseHeaders } = await postJsonToApi({
|
|
1441
1579
|
url: this.config.url({
|
|
1442
1580
|
path: "/chat/completions",
|
|
1443
1581
|
modelId: this.modelId
|
|
@@ -1451,9 +1589,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1451
1589
|
abortSignal: options.abortSignal,
|
|
1452
1590
|
fetch: this.config.fetch
|
|
1453
1591
|
});
|
|
1592
|
+
if ("error" in responseValue) {
|
|
1593
|
+
throw new APICallError({
|
|
1594
|
+
message: responseValue.error.message,
|
|
1595
|
+
url: this.config.url({
|
|
1596
|
+
path: "/chat/completions",
|
|
1597
|
+
modelId: this.modelId
|
|
1598
|
+
}),
|
|
1599
|
+
requestBodyValues: args,
|
|
1600
|
+
statusCode: 200,
|
|
1601
|
+
responseHeaders,
|
|
1602
|
+
data: responseValue.error
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
const response = responseValue;
|
|
1454
1606
|
const choice = response.choices[0];
|
|
1455
1607
|
if (!choice) {
|
|
1456
|
-
throw new
|
|
1608
|
+
throw new NoContentGeneratedError({
|
|
1609
|
+
message: "No choice in response"
|
|
1610
|
+
});
|
|
1457
1611
|
}
|
|
1458
1612
|
const usageInfo = response.usage ? {
|
|
1459
1613
|
inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
|
|
@@ -1560,24 +1714,25 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1560
1714
|
usage: usageInfo,
|
|
1561
1715
|
warnings: [],
|
|
1562
1716
|
providerMetadata: {
|
|
1563
|
-
openrouter: {
|
|
1717
|
+
openrouter: OpenRouterProviderMetadataSchema.parse({
|
|
1564
1718
|
provider: (_k = response.provider) != null ? _k : "",
|
|
1719
|
+
reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
|
|
1565
1720
|
usage: {
|
|
1566
|
-
promptTokens: (
|
|
1567
|
-
completionTokens: (
|
|
1568
|
-
totalTokens: (
|
|
1569
|
-
cost: (
|
|
1721
|
+
promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
|
|
1722
|
+
completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
|
|
1723
|
+
totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
|
|
1724
|
+
cost: (_p = response.usage) == null ? void 0 : _p.cost,
|
|
1570
1725
|
promptTokensDetails: {
|
|
1571
|
-
cachedTokens: (
|
|
1726
|
+
cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
|
|
1572
1727
|
},
|
|
1573
1728
|
completionTokensDetails: {
|
|
1574
|
-
reasoningTokens: (
|
|
1729
|
+
reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
|
|
1575
1730
|
},
|
|
1576
1731
|
costDetails: {
|
|
1577
|
-
upstreamInferenceCost: (
|
|
1732
|
+
upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
|
|
1578
1733
|
}
|
|
1579
1734
|
}
|
|
1580
|
-
}
|
|
1735
|
+
})
|
|
1581
1736
|
},
|
|
1582
1737
|
request: { body: args },
|
|
1583
1738
|
response: {
|
|
@@ -1622,6 +1777,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1622
1777
|
cachedInputTokens: Number.NaN
|
|
1623
1778
|
};
|
|
1624
1779
|
const openrouterUsage = {};
|
|
1780
|
+
const accumulatedReasoningDetails = [];
|
|
1625
1781
|
let textStarted = false;
|
|
1626
1782
|
let reasoningStarted = false;
|
|
1627
1783
|
let textId;
|
|
@@ -1707,6 +1863,7 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1707
1863
|
});
|
|
1708
1864
|
};
|
|
1709
1865
|
if (delta.reasoning_details && delta.reasoning_details.length > 0) {
|
|
1866
|
+
accumulatedReasoningDetails.push(...delta.reasoning_details);
|
|
1710
1867
|
for (const detail of delta.reasoning_details) {
|
|
1711
1868
|
switch (detail.type) {
|
|
1712
1869
|
case "reasoning.text" /* Text */: {
|
|
@@ -1810,7 +1967,10 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1810
1967
|
};
|
|
1811
1968
|
const toolCall2 = toolCalls[index];
|
|
1812
1969
|
if (toolCall2 == null) {
|
|
1813
|
-
throw new
|
|
1970
|
+
throw new InvalidResponseDataError({
|
|
1971
|
+
data: { index, toolCallsLength: toolCalls.length },
|
|
1972
|
+
message: `Tool call at index ${index} is missing after creation.`
|
|
1973
|
+
});
|
|
1814
1974
|
}
|
|
1815
1975
|
if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
|
|
1816
1976
|
toolCall2.inputStarted = true;
|
|
@@ -1840,7 +2000,14 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1840
2000
|
}
|
|
1841
2001
|
const toolCall = toolCalls[index];
|
|
1842
2002
|
if (toolCall == null) {
|
|
1843
|
-
throw new
|
|
2003
|
+
throw new InvalidResponseDataError({
|
|
2004
|
+
data: {
|
|
2005
|
+
index,
|
|
2006
|
+
toolCallsLength: toolCalls.length,
|
|
2007
|
+
toolCallDelta
|
|
2008
|
+
},
|
|
2009
|
+
message: `Tool call at index ${index} is missing during merge.`
|
|
2010
|
+
});
|
|
1844
2011
|
}
|
|
1845
2012
|
if (!toolCall.inputStarted) {
|
|
1846
2013
|
toolCall.inputStarted = true;
|
|
@@ -1913,6 +2080,9 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1913
2080
|
if (provider !== void 0) {
|
|
1914
2081
|
openrouterMetadata.provider = provider;
|
|
1915
2082
|
}
|
|
2083
|
+
if (accumulatedReasoningDetails.length > 0) {
|
|
2084
|
+
openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
|
|
2085
|
+
}
|
|
1916
2086
|
controller.enqueue({
|
|
1917
2087
|
type: "finish",
|
|
1918
2088
|
finishReason,
|
|
@@ -2035,36 +2205,36 @@ ${assistantMessage}
|
|
|
2035
2205
|
}
|
|
2036
2206
|
|
|
2037
2207
|
// src/completion/schemas.ts
|
|
2038
|
-
import { z as
|
|
2039
|
-
var OpenRouterCompletionChunkSchema =
|
|
2040
|
-
|
|
2041
|
-
id:
|
|
2042
|
-
model:
|
|
2043
|
-
choices:
|
|
2044
|
-
|
|
2045
|
-
text:
|
|
2046
|
-
reasoning:
|
|
2208
|
+
import { z as z8 } from "zod/v4";
|
|
2209
|
+
var OpenRouterCompletionChunkSchema = z8.union([
|
|
2210
|
+
z8.object({
|
|
2211
|
+
id: z8.string().optional(),
|
|
2212
|
+
model: z8.string().optional(),
|
|
2213
|
+
choices: z8.array(
|
|
2214
|
+
z8.object({
|
|
2215
|
+
text: z8.string(),
|
|
2216
|
+
reasoning: z8.string().nullish().optional(),
|
|
2047
2217
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2048
|
-
finish_reason:
|
|
2049
|
-
index:
|
|
2050
|
-
logprobs:
|
|
2051
|
-
tokens:
|
|
2052
|
-
token_logprobs:
|
|
2053
|
-
top_logprobs:
|
|
2218
|
+
finish_reason: z8.string().nullish(),
|
|
2219
|
+
index: z8.number().nullish(),
|
|
2220
|
+
logprobs: z8.object({
|
|
2221
|
+
tokens: z8.array(z8.string()),
|
|
2222
|
+
token_logprobs: z8.array(z8.number()),
|
|
2223
|
+
top_logprobs: z8.array(z8.record(z8.string(), z8.number())).nullable()
|
|
2054
2224
|
}).nullable().optional()
|
|
2055
2225
|
})
|
|
2056
2226
|
),
|
|
2057
|
-
usage:
|
|
2058
|
-
prompt_tokens:
|
|
2059
|
-
prompt_tokens_details:
|
|
2060
|
-
cached_tokens:
|
|
2227
|
+
usage: z8.object({
|
|
2228
|
+
prompt_tokens: z8.number(),
|
|
2229
|
+
prompt_tokens_details: z8.object({
|
|
2230
|
+
cached_tokens: z8.number()
|
|
2061
2231
|
}).nullish(),
|
|
2062
|
-
completion_tokens:
|
|
2063
|
-
completion_tokens_details:
|
|
2064
|
-
reasoning_tokens:
|
|
2232
|
+
completion_tokens: z8.number(),
|
|
2233
|
+
completion_tokens_details: z8.object({
|
|
2234
|
+
reasoning_tokens: z8.number()
|
|
2065
2235
|
}).nullish(),
|
|
2066
|
-
total_tokens:
|
|
2067
|
-
cost:
|
|
2236
|
+
total_tokens: z8.number(),
|
|
2237
|
+
cost: z8.number().optional()
|
|
2068
2238
|
}).nullish()
|
|
2069
2239
|
}),
|
|
2070
2240
|
OpenRouterErrorResponseSchema
|
|
@@ -2162,11 +2332,23 @@ var OpenRouterCompletionLanguageModel = class {
|
|
|
2162
2332
|
fetch: this.config.fetch
|
|
2163
2333
|
});
|
|
2164
2334
|
if ("error" in response) {
|
|
2165
|
-
throw new
|
|
2335
|
+
throw new APICallError({
|
|
2336
|
+
message: response.error.message,
|
|
2337
|
+
url: this.config.url({
|
|
2338
|
+
path: "/completions",
|
|
2339
|
+
modelId: this.modelId
|
|
2340
|
+
}),
|
|
2341
|
+
requestBodyValues: args,
|
|
2342
|
+
statusCode: 200,
|
|
2343
|
+
responseHeaders,
|
|
2344
|
+
data: response.error
|
|
2345
|
+
});
|
|
2166
2346
|
}
|
|
2167
2347
|
const choice = response.choices[0];
|
|
2168
2348
|
if (!choice) {
|
|
2169
|
-
throw new
|
|
2349
|
+
throw new NoContentGeneratedError({
|
|
2350
|
+
message: "No choice in OpenRouter completion response"
|
|
2351
|
+
});
|
|
2170
2352
|
}
|
|
2171
2353
|
return {
|
|
2172
2354
|
content: [
|
|
@@ -2333,18 +2515,43 @@ var OpenRouter = class {
|
|
|
2333
2515
|
}
|
|
2334
2516
|
};
|
|
2335
2517
|
|
|
2518
|
+
// src/utils/remove-undefined.ts
|
|
2519
|
+
function removeUndefinedEntries2(record) {
|
|
2520
|
+
return Object.fromEntries(
|
|
2521
|
+
Object.entries(record).filter(([, value]) => value !== null)
|
|
2522
|
+
);
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
// src/utils/with-user-agent-suffix.ts
|
|
2526
|
+
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
2527
|
+
const cleanedHeaders = removeUndefinedEntries2(
|
|
2528
|
+
headers != null ? headers : {}
|
|
2529
|
+
);
|
|
2530
|
+
const currentUserAgentHeader = cleanedHeaders["user-agent"] || "";
|
|
2531
|
+
const newUserAgent = [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ");
|
|
2532
|
+
return __spreadProps(__spreadValues({}, cleanedHeaders), {
|
|
2533
|
+
"user-agent": newUserAgent
|
|
2534
|
+
});
|
|
2535
|
+
}
|
|
2536
|
+
|
|
2537
|
+
// src/version.ts
|
|
2538
|
+
var VERSION = false ? "0.0.0-test" : "1.2.2";
|
|
2539
|
+
|
|
2336
2540
|
// src/provider.ts
|
|
2337
2541
|
function createOpenRouter(options = {}) {
|
|
2338
2542
|
var _a15, _b, _c;
|
|
2339
2543
|
const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
|
|
2340
2544
|
const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
|
|
2341
|
-
const getHeaders = () =>
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2545
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
2546
|
+
__spreadValues({
|
|
2547
|
+
Authorization: `Bearer ${loadApiKey({
|
|
2548
|
+
apiKey: options.apiKey,
|
|
2549
|
+
environmentVariableName: "OPENROUTER_API_KEY",
|
|
2550
|
+
description: "OpenRouter"
|
|
2551
|
+
})}`
|
|
2552
|
+
}, options.headers),
|
|
2553
|
+
`ai-sdk/openrouter/${VERSION}`
|
|
2554
|
+
);
|
|
2348
2555
|
const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
|
|
2349
2556
|
provider: "openrouter.chat",
|
|
2350
2557
|
url: ({ path }) => `${baseURL}${path}`,
|