@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.
@@ -961,6 +961,15 @@ function getFileUrl({
961
961
  }
962
962
  return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
963
963
  }
964
+ function getMediaType(dataUrl, defaultMediaType) {
965
+ var _a15;
966
+ const match = dataUrl.match(/^data:([^;]+)/);
967
+ return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
968
+ }
969
+ function getBase64FromDataUrl(dataUrl) {
970
+ const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
971
+ return match ? match[1] : dataUrl;
972
+ }
964
973
 
965
974
  // src/chat/convert-to-openrouter-chat-messages.ts
966
975
  function getCacheControl(providerMetadata) {
@@ -1173,126 +1182,144 @@ function getChatCompletionToolChoice(toolChoice) {
1173
1182
  }
1174
1183
 
1175
1184
  // src/chat/schemas.ts
1185
+ var import_v45 = require("zod/v4");
1186
+
1187
+ // src/schemas/image.ts
1176
1188
  var import_v44 = require("zod/v4");
1177
- var OpenRouterChatCompletionBaseResponseSchema = import_v44.z.object({
1178
- id: import_v44.z.string().optional(),
1179
- model: import_v44.z.string().optional(),
1180
- provider: import_v44.z.string().optional(),
1181
- usage: import_v44.z.object({
1182
- prompt_tokens: import_v44.z.number(),
1183
- prompt_tokens_details: import_v44.z.object({
1184
- cached_tokens: import_v44.z.number()
1189
+ var ImageResponseSchema = import_v44.z.object({
1190
+ type: import_v44.z.literal("image_url"),
1191
+ image_url: import_v44.z.object({
1192
+ url: import_v44.z.string()
1193
+ })
1194
+ });
1195
+ var ImageResponseWithUnknownSchema = import_v44.z.union([
1196
+ ImageResponseSchema,
1197
+ import_v44.z.unknown().transform(() => null)
1198
+ ]);
1199
+ var ImageResponseArraySchema = import_v44.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1200
+
1201
+ // src/chat/schemas.ts
1202
+ var OpenRouterChatCompletionBaseResponseSchema = import_v45.z.object({
1203
+ id: import_v45.z.string().optional(),
1204
+ model: import_v45.z.string().optional(),
1205
+ provider: import_v45.z.string().optional(),
1206
+ usage: import_v45.z.object({
1207
+ prompt_tokens: import_v45.z.number(),
1208
+ prompt_tokens_details: import_v45.z.object({
1209
+ cached_tokens: import_v45.z.number()
1185
1210
  }).nullish(),
1186
- completion_tokens: import_v44.z.number(),
1187
- completion_tokens_details: import_v44.z.object({
1188
- reasoning_tokens: import_v44.z.number()
1211
+ completion_tokens: import_v45.z.number(),
1212
+ completion_tokens_details: import_v45.z.object({
1213
+ reasoning_tokens: import_v45.z.number()
1189
1214
  }).nullish(),
1190
- total_tokens: import_v44.z.number(),
1191
- cost: import_v44.z.number().optional(),
1192
- cost_details: import_v44.z.object({
1193
- upstream_inference_cost: import_v44.z.number().nullish()
1215
+ total_tokens: import_v45.z.number(),
1216
+ cost: import_v45.z.number().optional(),
1217
+ cost_details: import_v45.z.object({
1218
+ upstream_inference_cost: import_v45.z.number().nullish()
1194
1219
  }).nullish()
1195
1220
  }).nullish()
1196
1221
  });
1197
1222
  var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1198
- choices: import_v44.z.array(
1199
- import_v44.z.object({
1200
- message: import_v44.z.object({
1201
- role: import_v44.z.literal("assistant"),
1202
- content: import_v44.z.string().nullable().optional(),
1203
- reasoning: import_v44.z.string().nullable().optional(),
1223
+ choices: import_v45.z.array(
1224
+ import_v45.z.object({
1225
+ message: import_v45.z.object({
1226
+ role: import_v45.z.literal("assistant"),
1227
+ content: import_v45.z.string().nullable().optional(),
1228
+ reasoning: import_v45.z.string().nullable().optional(),
1204
1229
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1205
- tool_calls: import_v44.z.array(
1206
- import_v44.z.object({
1207
- id: import_v44.z.string().optional().nullable(),
1208
- type: import_v44.z.literal("function"),
1209
- function: import_v44.z.object({
1210
- name: import_v44.z.string(),
1211
- arguments: import_v44.z.string()
1230
+ images: ImageResponseArraySchema.nullish(),
1231
+ tool_calls: import_v45.z.array(
1232
+ import_v45.z.object({
1233
+ id: import_v45.z.string().optional().nullable(),
1234
+ type: import_v45.z.literal("function"),
1235
+ function: import_v45.z.object({
1236
+ name: import_v45.z.string(),
1237
+ arguments: import_v45.z.string()
1212
1238
  })
1213
1239
  })
1214
1240
  ).optional(),
1215
- annotations: import_v44.z.array(
1216
- import_v44.z.object({
1217
- type: import_v44.z.enum(["url_citation"]),
1218
- url_citation: import_v44.z.object({
1219
- end_index: import_v44.z.number(),
1220
- start_index: import_v44.z.number(),
1221
- title: import_v44.z.string(),
1222
- url: import_v44.z.string(),
1223
- content: import_v44.z.string().optional()
1241
+ annotations: import_v45.z.array(
1242
+ import_v45.z.object({
1243
+ type: import_v45.z.enum(["url_citation"]),
1244
+ url_citation: import_v45.z.object({
1245
+ end_index: import_v45.z.number(),
1246
+ start_index: import_v45.z.number(),
1247
+ title: import_v45.z.string(),
1248
+ url: import_v45.z.string(),
1249
+ content: import_v45.z.string().optional()
1224
1250
  })
1225
1251
  })
1226
1252
  ).nullish()
1227
1253
  }),
1228
- index: import_v44.z.number().nullish(),
1229
- logprobs: import_v44.z.object({
1230
- content: import_v44.z.array(
1231
- import_v44.z.object({
1232
- token: import_v44.z.string(),
1233
- logprob: import_v44.z.number(),
1234
- top_logprobs: import_v44.z.array(
1235
- import_v44.z.object({
1236
- token: import_v44.z.string(),
1237
- logprob: import_v44.z.number()
1254
+ index: import_v45.z.number().nullish(),
1255
+ logprobs: import_v45.z.object({
1256
+ content: import_v45.z.array(
1257
+ import_v45.z.object({
1258
+ token: import_v45.z.string(),
1259
+ logprob: import_v45.z.number(),
1260
+ top_logprobs: import_v45.z.array(
1261
+ import_v45.z.object({
1262
+ token: import_v45.z.string(),
1263
+ logprob: import_v45.z.number()
1238
1264
  })
1239
1265
  )
1240
1266
  })
1241
1267
  ).nullable()
1242
1268
  }).nullable().optional(),
1243
- finish_reason: import_v44.z.string().optional().nullable()
1269
+ finish_reason: import_v45.z.string().optional().nullable()
1244
1270
  })
1245
1271
  )
1246
1272
  });
1247
- var OpenRouterStreamChatCompletionChunkSchema = import_v44.z.union([
1273
+ var OpenRouterStreamChatCompletionChunkSchema = import_v45.z.union([
1248
1274
  OpenRouterChatCompletionBaseResponseSchema.extend({
1249
- choices: import_v44.z.array(
1250
- import_v44.z.object({
1251
- delta: import_v44.z.object({
1252
- role: import_v44.z.enum(["assistant"]).optional(),
1253
- content: import_v44.z.string().nullish(),
1254
- reasoning: import_v44.z.string().nullish().optional(),
1275
+ choices: import_v45.z.array(
1276
+ import_v45.z.object({
1277
+ delta: import_v45.z.object({
1278
+ role: import_v45.z.enum(["assistant"]).optional(),
1279
+ content: import_v45.z.string().nullish(),
1280
+ reasoning: import_v45.z.string().nullish().optional(),
1255
1281
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1256
- tool_calls: import_v44.z.array(
1257
- import_v44.z.object({
1258
- index: import_v44.z.number().nullish(),
1259
- id: import_v44.z.string().nullish(),
1260
- type: import_v44.z.literal("function").optional(),
1261
- function: import_v44.z.object({
1262
- name: import_v44.z.string().nullish(),
1263
- arguments: import_v44.z.string().nullish()
1282
+ images: ImageResponseArraySchema.nullish(),
1283
+ tool_calls: import_v45.z.array(
1284
+ import_v45.z.object({
1285
+ index: import_v45.z.number().nullish(),
1286
+ id: import_v45.z.string().nullish(),
1287
+ type: import_v45.z.literal("function").optional(),
1288
+ function: import_v45.z.object({
1289
+ name: import_v45.z.string().nullish(),
1290
+ arguments: import_v45.z.string().nullish()
1264
1291
  })
1265
1292
  })
1266
1293
  ).nullish(),
1267
- annotations: import_v44.z.array(
1268
- import_v44.z.object({
1269
- type: import_v44.z.enum(["url_citation"]),
1270
- url_citation: import_v44.z.object({
1271
- end_index: import_v44.z.number(),
1272
- start_index: import_v44.z.number(),
1273
- title: import_v44.z.string(),
1274
- url: import_v44.z.string(),
1275
- content: import_v44.z.string().optional()
1294
+ annotations: import_v45.z.array(
1295
+ import_v45.z.object({
1296
+ type: import_v45.z.enum(["url_citation"]),
1297
+ url_citation: import_v45.z.object({
1298
+ end_index: import_v45.z.number(),
1299
+ start_index: import_v45.z.number(),
1300
+ title: import_v45.z.string(),
1301
+ url: import_v45.z.string(),
1302
+ content: import_v45.z.string().optional()
1276
1303
  })
1277
1304
  })
1278
1305
  ).nullish()
1279
1306
  }).nullish(),
1280
- logprobs: import_v44.z.object({
1281
- content: import_v44.z.array(
1282
- import_v44.z.object({
1283
- token: import_v44.z.string(),
1284
- logprob: import_v44.z.number(),
1285
- top_logprobs: import_v44.z.array(
1286
- import_v44.z.object({
1287
- token: import_v44.z.string(),
1288
- logprob: import_v44.z.number()
1307
+ logprobs: import_v45.z.object({
1308
+ content: import_v45.z.array(
1309
+ import_v45.z.object({
1310
+ token: import_v45.z.string(),
1311
+ logprob: import_v45.z.number(),
1312
+ top_logprobs: import_v45.z.array(
1313
+ import_v45.z.object({
1314
+ token: import_v45.z.string(),
1315
+ logprob: import_v45.z.number()
1289
1316
  })
1290
1317
  )
1291
1318
  })
1292
1319
  ).nullable()
1293
1320
  }).nullish(),
1294
- finish_reason: import_v44.z.string().nullable().optional(),
1295
- index: import_v44.z.number().nullish()
1321
+ finish_reason: import_v45.z.string().nullable().optional(),
1322
+ index: import_v45.z.number().nullish()
1296
1323
  })
1297
1324
  )
1298
1325
  }),
@@ -1489,6 +1516,15 @@ var OpenRouterChatLanguageModel = class {
1489
1516
  });
1490
1517
  }
1491
1518
  }
1519
+ if (choice.message.images) {
1520
+ for (const image of choice.message.images) {
1521
+ content.push({
1522
+ type: "file",
1523
+ mediaType: getMediaType(image.image_url.url, "image/jpeg"),
1524
+ data: getBase64FromDataUrl(image.image_url.url)
1525
+ });
1526
+ }
1527
+ }
1492
1528
  if (choice.message.annotations) {
1493
1529
  for (const annotation of choice.message.annotations) {
1494
1530
  if (annotation.type === "url_citation") {
@@ -1822,6 +1858,15 @@ var OpenRouterChatLanguageModel = class {
1822
1858
  }
1823
1859
  }
1824
1860
  }
1861
+ if (delta.images != null) {
1862
+ for (const image of delta.images) {
1863
+ controller.enqueue({
1864
+ type: "file",
1865
+ mediaType: getMediaType(image.image_url.url, "image/jpeg"),
1866
+ data: getBase64FromDataUrl(image.image_url.url)
1867
+ });
1868
+ }
1869
+ }
1825
1870
  },
1826
1871
  flush(controller) {
1827
1872
  var _a16;
@@ -1979,36 +2024,36 @@ ${assistantMessage}
1979
2024
  }
1980
2025
 
1981
2026
  // src/completion/schemas.ts
1982
- var import_v45 = require("zod/v4");
1983
- var OpenRouterCompletionChunkSchema = import_v45.z.union([
1984
- import_v45.z.object({
1985
- id: import_v45.z.string().optional(),
1986
- model: import_v45.z.string().optional(),
1987
- choices: import_v45.z.array(
1988
- import_v45.z.object({
1989
- text: import_v45.z.string(),
1990
- reasoning: import_v45.z.string().nullish().optional(),
2027
+ var import_v46 = require("zod/v4");
2028
+ var OpenRouterCompletionChunkSchema = import_v46.z.union([
2029
+ import_v46.z.object({
2030
+ id: import_v46.z.string().optional(),
2031
+ model: import_v46.z.string().optional(),
2032
+ choices: import_v46.z.array(
2033
+ import_v46.z.object({
2034
+ text: import_v46.z.string(),
2035
+ reasoning: import_v46.z.string().nullish().optional(),
1991
2036
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1992
- finish_reason: import_v45.z.string().nullish(),
1993
- index: import_v45.z.number().nullish(),
1994
- logprobs: import_v45.z.object({
1995
- tokens: import_v45.z.array(import_v45.z.string()),
1996
- token_logprobs: import_v45.z.array(import_v45.z.number()),
1997
- top_logprobs: import_v45.z.array(import_v45.z.record(import_v45.z.string(), import_v45.z.number())).nullable()
2037
+ finish_reason: import_v46.z.string().nullish(),
2038
+ index: import_v46.z.number().nullish(),
2039
+ logprobs: import_v46.z.object({
2040
+ tokens: import_v46.z.array(import_v46.z.string()),
2041
+ token_logprobs: import_v46.z.array(import_v46.z.number()),
2042
+ top_logprobs: import_v46.z.array(import_v46.z.record(import_v46.z.string(), import_v46.z.number())).nullable()
1998
2043
  }).nullable().optional()
1999
2044
  })
2000
2045
  ),
2001
- usage: import_v45.z.object({
2002
- prompt_tokens: import_v45.z.number(),
2003
- prompt_tokens_details: import_v45.z.object({
2004
- cached_tokens: import_v45.z.number()
2046
+ usage: import_v46.z.object({
2047
+ prompt_tokens: import_v46.z.number(),
2048
+ prompt_tokens_details: import_v46.z.object({
2049
+ cached_tokens: import_v46.z.number()
2005
2050
  }).nullish(),
2006
- completion_tokens: import_v45.z.number(),
2007
- completion_tokens_details: import_v45.z.object({
2008
- reasoning_tokens: import_v45.z.number()
2051
+ completion_tokens: import_v46.z.number(),
2052
+ completion_tokens_details: import_v46.z.object({
2053
+ reasoning_tokens: import_v46.z.number()
2009
2054
  }).nullish(),
2010
- total_tokens: import_v45.z.number(),
2011
- cost: import_v45.z.number().optional()
2055
+ total_tokens: import_v46.z.number(),
2056
+ cost: import_v46.z.number().optional()
2012
2057
  }).nullish()
2013
2058
  }),
2014
2059
  OpenRouterErrorResponseSchema