@openrouter/ai-sdk-provider 1.1.1 → 1.2.0
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.js +152 -107
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +152 -107
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +152 -107
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +152 -107
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -972,6 +972,15 @@ function getFileUrl({
|
|
|
972
972
|
}
|
|
973
973
|
return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
|
|
974
974
|
}
|
|
975
|
+
function getMediaType(dataUrl, defaultMediaType) {
|
|
976
|
+
var _a15;
|
|
977
|
+
const match = dataUrl.match(/^data:([^;]+)/);
|
|
978
|
+
return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
|
|
979
|
+
}
|
|
980
|
+
function getBase64FromDataUrl(dataUrl) {
|
|
981
|
+
const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
|
|
982
|
+
return match ? match[1] : dataUrl;
|
|
983
|
+
}
|
|
975
984
|
|
|
976
985
|
// src/chat/convert-to-openrouter-chat-messages.ts
|
|
977
986
|
function getCacheControl(providerMetadata) {
|
|
@@ -1184,126 +1193,144 @@ function getChatCompletionToolChoice(toolChoice) {
|
|
|
1184
1193
|
}
|
|
1185
1194
|
|
|
1186
1195
|
// src/chat/schemas.ts
|
|
1196
|
+
import { z as z6 } from "zod/v4";
|
|
1197
|
+
|
|
1198
|
+
// src/schemas/image.ts
|
|
1187
1199
|
import { z as z5 } from "zod/v4";
|
|
1188
|
-
var
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1200
|
+
var ImageResponseSchema = z5.object({
|
|
1201
|
+
type: z5.literal("image_url"),
|
|
1202
|
+
image_url: z5.object({
|
|
1203
|
+
url: z5.string()
|
|
1204
|
+
})
|
|
1205
|
+
});
|
|
1206
|
+
var ImageResponseWithUnknownSchema = z5.union([
|
|
1207
|
+
ImageResponseSchema,
|
|
1208
|
+
z5.unknown().transform(() => null)
|
|
1209
|
+
]);
|
|
1210
|
+
var ImageResponseArraySchema = z5.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
|
|
1211
|
+
|
|
1212
|
+
// src/chat/schemas.ts
|
|
1213
|
+
var OpenRouterChatCompletionBaseResponseSchema = z6.object({
|
|
1214
|
+
id: z6.string().optional(),
|
|
1215
|
+
model: z6.string().optional(),
|
|
1216
|
+
provider: z6.string().optional(),
|
|
1217
|
+
usage: z6.object({
|
|
1218
|
+
prompt_tokens: z6.number(),
|
|
1219
|
+
prompt_tokens_details: z6.object({
|
|
1220
|
+
cached_tokens: z6.number()
|
|
1196
1221
|
}).nullish(),
|
|
1197
|
-
completion_tokens:
|
|
1198
|
-
completion_tokens_details:
|
|
1199
|
-
reasoning_tokens:
|
|
1222
|
+
completion_tokens: z6.number(),
|
|
1223
|
+
completion_tokens_details: z6.object({
|
|
1224
|
+
reasoning_tokens: z6.number()
|
|
1200
1225
|
}).nullish(),
|
|
1201
|
-
total_tokens:
|
|
1202
|
-
cost:
|
|
1203
|
-
cost_details:
|
|
1204
|
-
upstream_inference_cost:
|
|
1226
|
+
total_tokens: z6.number(),
|
|
1227
|
+
cost: z6.number().optional(),
|
|
1228
|
+
cost_details: z6.object({
|
|
1229
|
+
upstream_inference_cost: z6.number().nullish()
|
|
1205
1230
|
}).nullish()
|
|
1206
1231
|
}).nullish()
|
|
1207
1232
|
});
|
|
1208
1233
|
var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1209
|
-
choices:
|
|
1210
|
-
|
|
1211
|
-
message:
|
|
1212
|
-
role:
|
|
1213
|
-
content:
|
|
1214
|
-
reasoning:
|
|
1234
|
+
choices: z6.array(
|
|
1235
|
+
z6.object({
|
|
1236
|
+
message: z6.object({
|
|
1237
|
+
role: z6.literal("assistant"),
|
|
1238
|
+
content: z6.string().nullable().optional(),
|
|
1239
|
+
reasoning: z6.string().nullable().optional(),
|
|
1215
1240
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1241
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1242
|
+
tool_calls: z6.array(
|
|
1243
|
+
z6.object({
|
|
1244
|
+
id: z6.string().optional().nullable(),
|
|
1245
|
+
type: z6.literal("function"),
|
|
1246
|
+
function: z6.object({
|
|
1247
|
+
name: z6.string(),
|
|
1248
|
+
arguments: z6.string()
|
|
1223
1249
|
})
|
|
1224
1250
|
})
|
|
1225
1251
|
).optional(),
|
|
1226
|
-
annotations:
|
|
1227
|
-
|
|
1228
|
-
type:
|
|
1229
|
-
url_citation:
|
|
1230
|
-
end_index:
|
|
1231
|
-
start_index:
|
|
1232
|
-
title:
|
|
1233
|
-
url:
|
|
1234
|
-
content:
|
|
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()
|
|
1235
1261
|
})
|
|
1236
1262
|
})
|
|
1237
1263
|
).nullish()
|
|
1238
1264
|
}),
|
|
1239
|
-
index:
|
|
1240
|
-
logprobs:
|
|
1241
|
-
content:
|
|
1242
|
-
|
|
1243
|
-
token:
|
|
1244
|
-
logprob:
|
|
1245
|
-
top_logprobs:
|
|
1246
|
-
|
|
1247
|
-
token:
|
|
1248
|
-
logprob:
|
|
1265
|
+
index: z6.number().nullish(),
|
|
1266
|
+
logprobs: z6.object({
|
|
1267
|
+
content: z6.array(
|
|
1268
|
+
z6.object({
|
|
1269
|
+
token: z6.string(),
|
|
1270
|
+
logprob: z6.number(),
|
|
1271
|
+
top_logprobs: z6.array(
|
|
1272
|
+
z6.object({
|
|
1273
|
+
token: z6.string(),
|
|
1274
|
+
logprob: z6.number()
|
|
1249
1275
|
})
|
|
1250
1276
|
)
|
|
1251
1277
|
})
|
|
1252
1278
|
).nullable()
|
|
1253
1279
|
}).nullable().optional(),
|
|
1254
|
-
finish_reason:
|
|
1280
|
+
finish_reason: z6.string().optional().nullable()
|
|
1255
1281
|
})
|
|
1256
1282
|
)
|
|
1257
1283
|
});
|
|
1258
|
-
var OpenRouterStreamChatCompletionChunkSchema =
|
|
1284
|
+
var OpenRouterStreamChatCompletionChunkSchema = z6.union([
|
|
1259
1285
|
OpenRouterChatCompletionBaseResponseSchema.extend({
|
|
1260
|
-
choices:
|
|
1261
|
-
|
|
1262
|
-
delta:
|
|
1263
|
-
role:
|
|
1264
|
-
content:
|
|
1265
|
-
reasoning:
|
|
1286
|
+
choices: z6.array(
|
|
1287
|
+
z6.object({
|
|
1288
|
+
delta: z6.object({
|
|
1289
|
+
role: z6.enum(["assistant"]).optional(),
|
|
1290
|
+
content: z6.string().nullish(),
|
|
1291
|
+
reasoning: z6.string().nullish().optional(),
|
|
1266
1292
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1293
|
+
images: ImageResponseArraySchema.nullish(),
|
|
1294
|
+
tool_calls: z6.array(
|
|
1295
|
+
z6.object({
|
|
1296
|
+
index: z6.number().nullish(),
|
|
1297
|
+
id: z6.string().nullish(),
|
|
1298
|
+
type: z6.literal("function").optional(),
|
|
1299
|
+
function: z6.object({
|
|
1300
|
+
name: z6.string().nullish(),
|
|
1301
|
+
arguments: z6.string().nullish()
|
|
1275
1302
|
})
|
|
1276
1303
|
})
|
|
1277
1304
|
).nullish(),
|
|
1278
|
-
annotations:
|
|
1279
|
-
|
|
1280
|
-
type:
|
|
1281
|
-
url_citation:
|
|
1282
|
-
end_index:
|
|
1283
|
-
start_index:
|
|
1284
|
-
title:
|
|
1285
|
-
url:
|
|
1286
|
-
content:
|
|
1305
|
+
annotations: z6.array(
|
|
1306
|
+
z6.object({
|
|
1307
|
+
type: z6.enum(["url_citation"]),
|
|
1308
|
+
url_citation: z6.object({
|
|
1309
|
+
end_index: z6.number(),
|
|
1310
|
+
start_index: z6.number(),
|
|
1311
|
+
title: z6.string(),
|
|
1312
|
+
url: z6.string(),
|
|
1313
|
+
content: z6.string().optional()
|
|
1287
1314
|
})
|
|
1288
1315
|
})
|
|
1289
1316
|
).nullish()
|
|
1290
1317
|
}).nullish(),
|
|
1291
|
-
logprobs:
|
|
1292
|
-
content:
|
|
1293
|
-
|
|
1294
|
-
token:
|
|
1295
|
-
logprob:
|
|
1296
|
-
top_logprobs:
|
|
1297
|
-
|
|
1298
|
-
token:
|
|
1299
|
-
logprob:
|
|
1318
|
+
logprobs: z6.object({
|
|
1319
|
+
content: z6.array(
|
|
1320
|
+
z6.object({
|
|
1321
|
+
token: z6.string(),
|
|
1322
|
+
logprob: z6.number(),
|
|
1323
|
+
top_logprobs: z6.array(
|
|
1324
|
+
z6.object({
|
|
1325
|
+
token: z6.string(),
|
|
1326
|
+
logprob: z6.number()
|
|
1300
1327
|
})
|
|
1301
1328
|
)
|
|
1302
1329
|
})
|
|
1303
1330
|
).nullable()
|
|
1304
1331
|
}).nullish(),
|
|
1305
|
-
finish_reason:
|
|
1306
|
-
index:
|
|
1332
|
+
finish_reason: z6.string().nullable().optional(),
|
|
1333
|
+
index: z6.number().nullish()
|
|
1307
1334
|
})
|
|
1308
1335
|
)
|
|
1309
1336
|
}),
|
|
@@ -1500,6 +1527,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1500
1527
|
});
|
|
1501
1528
|
}
|
|
1502
1529
|
}
|
|
1530
|
+
if (choice.message.images) {
|
|
1531
|
+
for (const image of choice.message.images) {
|
|
1532
|
+
content.push({
|
|
1533
|
+
type: "file",
|
|
1534
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1535
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1503
1539
|
if (choice.message.annotations) {
|
|
1504
1540
|
for (const annotation of choice.message.annotations) {
|
|
1505
1541
|
if (annotation.type === "url_citation") {
|
|
@@ -1833,6 +1869,15 @@ var OpenRouterChatLanguageModel = class {
|
|
|
1833
1869
|
}
|
|
1834
1870
|
}
|
|
1835
1871
|
}
|
|
1872
|
+
if (delta.images != null) {
|
|
1873
|
+
for (const image of delta.images) {
|
|
1874
|
+
controller.enqueue({
|
|
1875
|
+
type: "file",
|
|
1876
|
+
mediaType: getMediaType(image.image_url.url, "image/jpeg"),
|
|
1877
|
+
data: getBase64FromDataUrl(image.image_url.url)
|
|
1878
|
+
});
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1836
1881
|
},
|
|
1837
1882
|
flush(controller) {
|
|
1838
1883
|
var _a16;
|
|
@@ -1990,36 +2035,36 @@ ${assistantMessage}
|
|
|
1990
2035
|
}
|
|
1991
2036
|
|
|
1992
2037
|
// src/completion/schemas.ts
|
|
1993
|
-
import { z as
|
|
1994
|
-
var OpenRouterCompletionChunkSchema =
|
|
1995
|
-
|
|
1996
|
-
id:
|
|
1997
|
-
model:
|
|
1998
|
-
choices:
|
|
1999
|
-
|
|
2000
|
-
text:
|
|
2001
|
-
reasoning:
|
|
2038
|
+
import { z as z7 } from "zod/v4";
|
|
2039
|
+
var OpenRouterCompletionChunkSchema = z7.union([
|
|
2040
|
+
z7.object({
|
|
2041
|
+
id: z7.string().optional(),
|
|
2042
|
+
model: z7.string().optional(),
|
|
2043
|
+
choices: z7.array(
|
|
2044
|
+
z7.object({
|
|
2045
|
+
text: z7.string(),
|
|
2046
|
+
reasoning: z7.string().nullish().optional(),
|
|
2002
2047
|
reasoning_details: ReasoningDetailArraySchema.nullish(),
|
|
2003
|
-
finish_reason:
|
|
2004
|
-
index:
|
|
2005
|
-
logprobs:
|
|
2006
|
-
tokens:
|
|
2007
|
-
token_logprobs:
|
|
2008
|
-
top_logprobs:
|
|
2048
|
+
finish_reason: z7.string().nullish(),
|
|
2049
|
+
index: z7.number().nullish(),
|
|
2050
|
+
logprobs: z7.object({
|
|
2051
|
+
tokens: z7.array(z7.string()),
|
|
2052
|
+
token_logprobs: z7.array(z7.number()),
|
|
2053
|
+
top_logprobs: z7.array(z7.record(z7.string(), z7.number())).nullable()
|
|
2009
2054
|
}).nullable().optional()
|
|
2010
2055
|
})
|
|
2011
2056
|
),
|
|
2012
|
-
usage:
|
|
2013
|
-
prompt_tokens:
|
|
2014
|
-
prompt_tokens_details:
|
|
2015
|
-
cached_tokens:
|
|
2057
|
+
usage: z7.object({
|
|
2058
|
+
prompt_tokens: z7.number(),
|
|
2059
|
+
prompt_tokens_details: z7.object({
|
|
2060
|
+
cached_tokens: z7.number()
|
|
2016
2061
|
}).nullish(),
|
|
2017
|
-
completion_tokens:
|
|
2018
|
-
completion_tokens_details:
|
|
2019
|
-
reasoning_tokens:
|
|
2062
|
+
completion_tokens: z7.number(),
|
|
2063
|
+
completion_tokens_details: z7.object({
|
|
2064
|
+
reasoning_tokens: z7.number()
|
|
2020
2065
|
}).nullish(),
|
|
2021
|
-
total_tokens:
|
|
2022
|
-
cost:
|
|
2066
|
+
total_tokens: z7.number(),
|
|
2067
|
+
cost: z7.number().optional()
|
|
2023
2068
|
}).nullish()
|
|
2024
2069
|
}),
|
|
2025
2070
|
OpenRouterErrorResponseSchema
|