@llmgateway/ai-sdk-provider 2.2.0 → 2.3.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/dist/index.js CHANGED
@@ -1010,6 +1010,15 @@ function getFileUrl({
1010
1010
  }
1011
1011
  return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
1012
1012
  }
1013
+ function getMediaType(dataUrl, defaultMediaType) {
1014
+ var _a15;
1015
+ const match = dataUrl.match(/^data:([^;]+)/);
1016
+ return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
1017
+ }
1018
+ function getBase64FromDataUrl(dataUrl) {
1019
+ const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
1020
+ return match ? match[1] : dataUrl;
1021
+ }
1013
1022
 
1014
1023
  // src/chat/convert-to-llmgateway-chat-messages.ts
1015
1024
  function getCacheControl(providerMetadata) {
@@ -1222,101 +1231,119 @@ function getChatCompletionToolChoice(toolChoice) {
1222
1231
  }
1223
1232
 
1224
1233
  // src/chat/schemas.ts
1234
+ var import_v45 = require("zod/v4");
1235
+
1236
+ // src/schemas/image.ts
1225
1237
  var import_v44 = require("zod/v4");
1226
- var LLMGatewayChatCompletionBaseResponseSchema = import_v44.z.object({
1227
- id: import_v44.z.string().optional(),
1228
- model: import_v44.z.string().optional(),
1229
- usage: import_v44.z.object({
1230
- prompt_tokens: import_v44.z.number(),
1231
- prompt_tokens_details: import_v44.z.object({
1232
- cached_tokens: import_v44.z.number()
1238
+ var ImageResponseSchema = import_v44.z.object({
1239
+ type: import_v44.z.literal("image_url"),
1240
+ image_url: import_v44.z.object({
1241
+ url: import_v44.z.string()
1242
+ })
1243
+ });
1244
+ var ImageResponseWithUnknownSchema = import_v44.z.union([
1245
+ ImageResponseSchema,
1246
+ import_v44.z.unknown().transform(() => null)
1247
+ ]);
1248
+ var ImageResponseArraySchema = import_v44.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1249
+
1250
+ // src/chat/schemas.ts
1251
+ var LLMGatewayChatCompletionBaseResponseSchema = import_v45.z.object({
1252
+ id: import_v45.z.string().optional(),
1253
+ model: import_v45.z.string().optional(),
1254
+ usage: import_v45.z.object({
1255
+ prompt_tokens: import_v45.z.number(),
1256
+ prompt_tokens_details: import_v45.z.object({
1257
+ cached_tokens: import_v45.z.number()
1233
1258
  }).nullish(),
1234
- completion_tokens: import_v44.z.number(),
1235
- completion_tokens_details: import_v44.z.object({
1236
- reasoning_tokens: import_v44.z.number()
1259
+ completion_tokens: import_v45.z.number(),
1260
+ completion_tokens_details: import_v45.z.object({
1261
+ reasoning_tokens: import_v45.z.number()
1237
1262
  }).nullish(),
1238
- total_tokens: import_v44.z.number(),
1239
- cost: import_v44.z.number().optional(),
1240
- cost_details: import_v44.z.object({
1241
- upstream_inference_cost: import_v44.z.number().nullish()
1263
+ total_tokens: import_v45.z.number(),
1264
+ cost: import_v45.z.number().optional(),
1265
+ cost_details: import_v45.z.object({
1266
+ upstream_inference_cost: import_v45.z.number().nullish()
1242
1267
  }).nullish()
1243
1268
  }).nullish()
1244
1269
  });
1245
1270
  var LLMGatewayNonStreamChatCompletionResponseSchema = LLMGatewayChatCompletionBaseResponseSchema.extend({
1246
- choices: import_v44.z.array(
1247
- import_v44.z.object({
1248
- message: import_v44.z.object({
1249
- role: import_v44.z.literal("assistant"),
1250
- content: import_v44.z.string().nullable().optional(),
1251
- reasoning: import_v44.z.string().nullable().optional(),
1271
+ choices: import_v45.z.array(
1272
+ import_v45.z.object({
1273
+ message: import_v45.z.object({
1274
+ role: import_v45.z.literal("assistant"),
1275
+ content: import_v45.z.string().nullable().optional(),
1276
+ reasoning: import_v45.z.string().nullable().optional(),
1252
1277
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1253
- tool_calls: import_v44.z.array(
1254
- import_v44.z.object({
1255
- id: import_v44.z.string().optional().nullable(),
1256
- type: import_v44.z.literal("function"),
1257
- function: import_v44.z.object({
1258
- name: import_v44.z.string(),
1259
- arguments: import_v44.z.string()
1278
+ images: ImageResponseArraySchema.nullish(),
1279
+ tool_calls: import_v45.z.array(
1280
+ import_v45.z.object({
1281
+ id: import_v45.z.string().optional().nullable(),
1282
+ type: import_v45.z.literal("function"),
1283
+ function: import_v45.z.object({
1284
+ name: import_v45.z.string(),
1285
+ arguments: import_v45.z.string()
1260
1286
  })
1261
1287
  })
1262
1288
  ).optional()
1263
1289
  }),
1264
- index: import_v44.z.number().nullish(),
1265
- logprobs: import_v44.z.object({
1266
- content: import_v44.z.array(
1267
- import_v44.z.object({
1268
- token: import_v44.z.string(),
1269
- logprob: import_v44.z.number(),
1270
- top_logprobs: import_v44.z.array(
1271
- import_v44.z.object({
1272
- token: import_v44.z.string(),
1273
- logprob: import_v44.z.number()
1290
+ index: import_v45.z.number().nullish(),
1291
+ logprobs: import_v45.z.object({
1292
+ content: import_v45.z.array(
1293
+ import_v45.z.object({
1294
+ token: import_v45.z.string(),
1295
+ logprob: import_v45.z.number(),
1296
+ top_logprobs: import_v45.z.array(
1297
+ import_v45.z.object({
1298
+ token: import_v45.z.string(),
1299
+ logprob: import_v45.z.number()
1274
1300
  })
1275
1301
  )
1276
1302
  })
1277
1303
  ).nullable()
1278
1304
  }).nullable().optional(),
1279
- finish_reason: import_v44.z.string().optional().nullable()
1305
+ finish_reason: import_v45.z.string().optional().nullable()
1280
1306
  })
1281
1307
  )
1282
1308
  });
1283
- var LLMGatewayStreamChatCompletionChunkSchema = import_v44.z.union([
1309
+ var LLMGatewayStreamChatCompletionChunkSchema = import_v45.z.union([
1284
1310
  LLMGatewayChatCompletionBaseResponseSchema.extend({
1285
- choices: import_v44.z.array(
1286
- import_v44.z.object({
1287
- delta: import_v44.z.object({
1288
- role: import_v44.z.enum(["assistant"]).optional(),
1289
- content: import_v44.z.string().nullish(),
1290
- reasoning: import_v44.z.string().nullish().optional(),
1311
+ choices: import_v45.z.array(
1312
+ import_v45.z.object({
1313
+ delta: import_v45.z.object({
1314
+ role: import_v45.z.enum(["assistant"]).optional(),
1315
+ content: import_v45.z.string().nullish(),
1316
+ reasoning: import_v45.z.string().nullish().optional(),
1291
1317
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1292
- tool_calls: import_v44.z.array(
1293
- import_v44.z.object({
1294
- index: import_v44.z.number().nullish(),
1295
- id: import_v44.z.string().nullish(),
1296
- type: import_v44.z.literal("function").optional(),
1297
- function: import_v44.z.object({
1298
- name: import_v44.z.string().nullish(),
1299
- arguments: import_v44.z.string().nullish()
1318
+ images: ImageResponseArraySchema.nullish(),
1319
+ tool_calls: import_v45.z.array(
1320
+ import_v45.z.object({
1321
+ index: import_v45.z.number().nullish(),
1322
+ id: import_v45.z.string().nullish(),
1323
+ type: import_v45.z.literal("function").optional(),
1324
+ function: import_v45.z.object({
1325
+ name: import_v45.z.string().nullish(),
1326
+ arguments: import_v45.z.string().nullish()
1300
1327
  })
1301
1328
  })
1302
1329
  ).nullish()
1303
1330
  }).nullish(),
1304
- logprobs: import_v44.z.object({
1305
- content: import_v44.z.array(
1306
- import_v44.z.object({
1307
- token: import_v44.z.string(),
1308
- logprob: import_v44.z.number(),
1309
- top_logprobs: import_v44.z.array(
1310
- import_v44.z.object({
1311
- token: import_v44.z.string(),
1312
- logprob: import_v44.z.number()
1331
+ logprobs: import_v45.z.object({
1332
+ content: import_v45.z.array(
1333
+ import_v45.z.object({
1334
+ token: import_v45.z.string(),
1335
+ logprob: import_v45.z.number(),
1336
+ top_logprobs: import_v45.z.array(
1337
+ import_v45.z.object({
1338
+ token: import_v45.z.string(),
1339
+ logprob: import_v45.z.number()
1313
1340
  })
1314
1341
  )
1315
1342
  })
1316
1343
  ).nullable()
1317
1344
  }).nullish(),
1318
- finish_reason: import_v44.z.string().nullable().optional(),
1319
- index: import_v44.z.number().nullish()
1345
+ finish_reason: import_v45.z.string().nullable().optional(),
1346
+ index: import_v45.z.number().nullish()
1320
1347
  })
1321
1348
  )
1322
1349
  }),
@@ -1355,7 +1382,6 @@ var LLMGatewayChatLanguageModel = class {
1355
1382
  tools,
1356
1383
  toolChoice
1357
1384
  }) {
1358
- var _a15;
1359
1385
  const baseArgs = __spreadValues(__spreadValues({
1360
1386
  // model id:
1361
1387
  model: this.modelId,
@@ -1389,10 +1415,10 @@ var LLMGatewayChatLanguageModel = class {
1389
1415
  response_format: {
1390
1416
  type: "json_schema",
1391
1417
  json_schema: {
1392
- name: responseFormat.schema.name || "response",
1393
- description: responseFormat.schema.description,
1418
+ name: responseFormat.name || "response",
1419
+ description: responseFormat.description,
1394
1420
  schema: responseFormat.schema,
1395
- strict: (_a15 = responseFormat.schema.strict) != null ? _a15 : true
1421
+ strict: true
1396
1422
  }
1397
1423
  }
1398
1424
  });
@@ -1515,6 +1541,33 @@ var LLMGatewayChatLanguageModel = class {
1515
1541
  });
1516
1542
  }
1517
1543
  }
1544
+ if (choice.message.images) {
1545
+ for (const image of choice.message.images) {
1546
+ content.push({
1547
+ type: "file",
1548
+ mediaType: getMediaType(image.image_url.url, "image/jpeg"),
1549
+ data: getBase64FromDataUrl(image.image_url.url)
1550
+ });
1551
+ }
1552
+ }
1553
+ if (choice.message.annotations) {
1554
+ for (const annotation of choice.message.annotations) {
1555
+ if (annotation.type === "url_citation") {
1556
+ content.push({
1557
+ type: "source",
1558
+ sourceType: "url",
1559
+ id: annotation.url_citation.url,
1560
+ url: annotation.url_citation.url,
1561
+ title: annotation.url_citation.title,
1562
+ providerMetadata: {
1563
+ openrouter: {
1564
+ content: annotation.url_citation.content || ""
1565
+ }
1566
+ }
1567
+ });
1568
+ }
1569
+ }
1570
+ }
1518
1571
  return {
1519
1572
  content,
1520
1573
  finishReason: mapLLMGatewayFinishReason(choice.finish_reason),
@@ -1800,6 +1853,15 @@ var LLMGatewayChatLanguageModel = class {
1800
1853
  }
1801
1854
  }
1802
1855
  }
1856
+ if (delta.images != null) {
1857
+ for (const image of delta.images) {
1858
+ controller.enqueue({
1859
+ type: "file",
1860
+ mediaType: getMediaType(image.image_url.url, "image/jpeg"),
1861
+ data: getBase64FromDataUrl(image.image_url.url)
1862
+ });
1863
+ }
1864
+ }
1803
1865
  },
1804
1866
  flush(controller) {
1805
1867
  var _a16;
@@ -1953,36 +2015,36 @@ ${assistantMessage}
1953
2015
  }
1954
2016
 
1955
2017
  // src/completion/schemas.ts
1956
- var import_v45 = require("zod/v4");
1957
- var LLMGatewayCompletionChunkSchema = import_v45.z.union([
1958
- import_v45.z.object({
1959
- id: import_v45.z.string().optional(),
1960
- model: import_v45.z.string().optional(),
1961
- choices: import_v45.z.array(
1962
- import_v45.z.object({
1963
- text: import_v45.z.string(),
1964
- reasoning: import_v45.z.string().nullish().optional(),
2018
+ var import_v46 = require("zod/v4");
2019
+ var LLMGatewayCompletionChunkSchema = import_v46.z.union([
2020
+ import_v46.z.object({
2021
+ id: import_v46.z.string().optional(),
2022
+ model: import_v46.z.string().optional(),
2023
+ choices: import_v46.z.array(
2024
+ import_v46.z.object({
2025
+ text: import_v46.z.string(),
2026
+ reasoning: import_v46.z.string().nullish().optional(),
1965
2027
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1966
- finish_reason: import_v45.z.string().nullish(),
1967
- index: import_v45.z.number().nullish(),
1968
- logprobs: import_v45.z.object({
1969
- tokens: import_v45.z.array(import_v45.z.string()),
1970
- token_logprobs: import_v45.z.array(import_v45.z.number()),
1971
- top_logprobs: import_v45.z.array(import_v45.z.record(import_v45.z.string(), import_v45.z.number())).nullable()
2028
+ finish_reason: import_v46.z.string().nullish(),
2029
+ index: import_v46.z.number().nullish(),
2030
+ logprobs: import_v46.z.object({
2031
+ tokens: import_v46.z.array(import_v46.z.string()),
2032
+ token_logprobs: import_v46.z.array(import_v46.z.number()),
2033
+ top_logprobs: import_v46.z.array(import_v46.z.record(import_v46.z.string(), import_v46.z.number())).nullable()
1972
2034
  }).nullable().optional()
1973
2035
  })
1974
2036
  ),
1975
- usage: import_v45.z.object({
1976
- prompt_tokens: import_v45.z.number(),
1977
- prompt_tokens_details: import_v45.z.object({
1978
- cached_tokens: import_v45.z.number()
2037
+ usage: import_v46.z.object({
2038
+ prompt_tokens: import_v46.z.number(),
2039
+ prompt_tokens_details: import_v46.z.object({
2040
+ cached_tokens: import_v46.z.number()
1979
2041
  }).nullish(),
1980
- completion_tokens: import_v45.z.number(),
1981
- completion_tokens_details: import_v45.z.object({
1982
- reasoning_tokens: import_v45.z.number()
2042
+ completion_tokens: import_v46.z.number(),
2043
+ completion_tokens_details: import_v46.z.object({
2044
+ reasoning_tokens: import_v46.z.number()
1983
2045
  }).nullish(),
1984
- total_tokens: import_v45.z.number(),
1985
- cost: import_v45.z.number().optional()
2046
+ total_tokens: import_v46.z.number(),
2047
+ cost: import_v46.z.number().optional()
1986
2048
  }).nullish()
1987
2049
  }),
1988
2050
  LLMGatewayErrorResponseSchema