@llmgateway/ai-sdk-provider 2.2.1 → 2.3.1

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.mjs CHANGED
@@ -975,6 +975,15 @@ function getFileUrl({
975
975
  }
976
976
  return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b = part.mediaType) != null ? _b : defaultMediaType};base64,${stringUrl}`;
977
977
  }
978
+ function getMediaType(dataUrl, defaultMediaType) {
979
+ var _a15;
980
+ const match = dataUrl.match(/^data:([^;]+)/);
981
+ return match ? (_a15 = match[1]) != null ? _a15 : defaultMediaType : defaultMediaType;
982
+ }
983
+ function getBase64FromDataUrl(dataUrl) {
984
+ const match = dataUrl.match(/^data:[^;]*;base64,(.+)$/);
985
+ return match ? match[1] : dataUrl;
986
+ }
978
987
 
979
988
  // src/chat/convert-to-llmgateway-chat-messages.ts
980
989
  function getCacheControl(providerMetadata) {
@@ -1187,101 +1196,119 @@ function getChatCompletionToolChoice(toolChoice) {
1187
1196
  }
1188
1197
 
1189
1198
  // src/chat/schemas.ts
1199
+ import { z as z6 } from "zod/v4";
1200
+
1201
+ // src/schemas/image.ts
1190
1202
  import { z as z5 } from "zod/v4";
1191
- var LLMGatewayChatCompletionBaseResponseSchema = z5.object({
1192
- id: z5.string().optional(),
1193
- model: z5.string().optional(),
1194
- usage: z5.object({
1195
- prompt_tokens: z5.number(),
1196
- prompt_tokens_details: z5.object({
1197
- cached_tokens: z5.number()
1203
+ var ImageResponseSchema = z5.object({
1204
+ type: z5.literal("image_url"),
1205
+ image_url: z5.object({
1206
+ url: z5.string()
1207
+ })
1208
+ });
1209
+ var ImageResponseWithUnknownSchema = z5.union([
1210
+ ImageResponseSchema,
1211
+ z5.unknown().transform(() => null)
1212
+ ]);
1213
+ var ImageResponseArraySchema = z5.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1214
+
1215
+ // src/chat/schemas.ts
1216
+ var LLMGatewayChatCompletionBaseResponseSchema = z6.object({
1217
+ id: z6.string().optional(),
1218
+ model: z6.string().optional(),
1219
+ usage: z6.object({
1220
+ prompt_tokens: z6.number(),
1221
+ prompt_tokens_details: z6.object({
1222
+ cached_tokens: z6.number()
1198
1223
  }).nullish(),
1199
- completion_tokens: z5.number(),
1200
- completion_tokens_details: z5.object({
1201
- reasoning_tokens: z5.number()
1224
+ completion_tokens: z6.number(),
1225
+ completion_tokens_details: z6.object({
1226
+ reasoning_tokens: z6.number()
1202
1227
  }).nullish(),
1203
- total_tokens: z5.number(),
1204
- cost: z5.number().optional(),
1205
- cost_details: z5.object({
1206
- upstream_inference_cost: z5.number().nullish()
1228
+ total_tokens: z6.number(),
1229
+ cost: z6.number().optional(),
1230
+ cost_details: z6.object({
1231
+ upstream_inference_cost: z6.number().nullish()
1207
1232
  }).nullish()
1208
1233
  }).nullish()
1209
1234
  });
1210
1235
  var LLMGatewayNonStreamChatCompletionResponseSchema = LLMGatewayChatCompletionBaseResponseSchema.extend({
1211
- choices: z5.array(
1212
- z5.object({
1213
- message: z5.object({
1214
- role: z5.literal("assistant"),
1215
- content: z5.string().nullable().optional(),
1216
- reasoning: z5.string().nullable().optional(),
1236
+ choices: z6.array(
1237
+ z6.object({
1238
+ message: z6.object({
1239
+ role: z6.literal("assistant"),
1240
+ content: z6.string().nullable().optional(),
1241
+ reasoning: z6.string().nullable().optional(),
1217
1242
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1218
- tool_calls: z5.array(
1219
- z5.object({
1220
- id: z5.string().optional().nullable(),
1221
- type: z5.literal("function"),
1222
- function: z5.object({
1223
- name: z5.string(),
1224
- arguments: z5.string()
1243
+ images: ImageResponseArraySchema.nullish(),
1244
+ tool_calls: z6.array(
1245
+ z6.object({
1246
+ id: z6.string().optional().nullable(),
1247
+ type: z6.literal("function"),
1248
+ function: z6.object({
1249
+ name: z6.string(),
1250
+ arguments: z6.string()
1225
1251
  })
1226
1252
  })
1227
1253
  ).optional()
1228
1254
  }),
1229
- index: z5.number().nullish(),
1230
- logprobs: z5.object({
1231
- content: z5.array(
1232
- z5.object({
1233
- token: z5.string(),
1234
- logprob: z5.number(),
1235
- top_logprobs: z5.array(
1236
- z5.object({
1237
- token: z5.string(),
1238
- logprob: z5.number()
1255
+ index: z6.number().nullish(),
1256
+ logprobs: z6.object({
1257
+ content: z6.array(
1258
+ z6.object({
1259
+ token: z6.string(),
1260
+ logprob: z6.number(),
1261
+ top_logprobs: z6.array(
1262
+ z6.object({
1263
+ token: z6.string(),
1264
+ logprob: z6.number()
1239
1265
  })
1240
1266
  )
1241
1267
  })
1242
1268
  ).nullable()
1243
1269
  }).nullable().optional(),
1244
- finish_reason: z5.string().optional().nullable()
1270
+ finish_reason: z6.string().optional().nullable()
1245
1271
  })
1246
1272
  )
1247
1273
  });
1248
- var LLMGatewayStreamChatCompletionChunkSchema = z5.union([
1274
+ var LLMGatewayStreamChatCompletionChunkSchema = z6.union([
1249
1275
  LLMGatewayChatCompletionBaseResponseSchema.extend({
1250
- choices: z5.array(
1251
- z5.object({
1252
- delta: z5.object({
1253
- role: z5.enum(["assistant"]).optional(),
1254
- content: z5.string().nullish(),
1255
- reasoning: z5.string().nullish().optional(),
1276
+ choices: z6.array(
1277
+ z6.object({
1278
+ delta: z6.object({
1279
+ role: z6.enum(["assistant"]).optional(),
1280
+ content: z6.string().nullish(),
1281
+ reasoning: z6.string().nullish().optional(),
1256
1282
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1257
- tool_calls: z5.array(
1258
- z5.object({
1259
- index: z5.number().nullish(),
1260
- id: z5.string().nullish(),
1261
- type: z5.literal("function").optional(),
1262
- function: z5.object({
1263
- name: z5.string().nullish(),
1264
- arguments: z5.string().nullish()
1283
+ images: ImageResponseArraySchema.nullish(),
1284
+ tool_calls: z6.array(
1285
+ z6.object({
1286
+ index: z6.number().nullish(),
1287
+ id: z6.string().nullish(),
1288
+ type: z6.literal("function").optional(),
1289
+ function: z6.object({
1290
+ name: z6.string().nullish(),
1291
+ arguments: z6.string().nullish()
1265
1292
  })
1266
1293
  })
1267
1294
  ).nullish()
1268
1295
  }).nullish(),
1269
- logprobs: z5.object({
1270
- content: z5.array(
1271
- z5.object({
1272
- token: z5.string(),
1273
- logprob: z5.number(),
1274
- top_logprobs: z5.array(
1275
- z5.object({
1276
- token: z5.string(),
1277
- logprob: z5.number()
1296
+ logprobs: z6.object({
1297
+ content: z6.array(
1298
+ z6.object({
1299
+ token: z6.string(),
1300
+ logprob: z6.number(),
1301
+ top_logprobs: z6.array(
1302
+ z6.object({
1303
+ token: z6.string(),
1304
+ logprob: z6.number()
1278
1305
  })
1279
1306
  )
1280
1307
  })
1281
1308
  ).nullable()
1282
1309
  }).nullish(),
1283
- finish_reason: z5.string().nullable().optional(),
1284
- index: z5.number().nullish()
1310
+ finish_reason: z6.string().nullable().optional(),
1311
+ index: z6.number().nullish()
1285
1312
  })
1286
1313
  )
1287
1314
  }),
@@ -1479,6 +1506,33 @@ var LLMGatewayChatLanguageModel = class {
1479
1506
  });
1480
1507
  }
1481
1508
  }
1509
+ if (choice.message.images) {
1510
+ for (const image of choice.message.images) {
1511
+ content.push({
1512
+ type: "file",
1513
+ mediaType: getMediaType(image.image_url.url, "image/jpeg"),
1514
+ data: getBase64FromDataUrl(image.image_url.url)
1515
+ });
1516
+ }
1517
+ }
1518
+ if (choice.message.annotations) {
1519
+ for (const annotation of choice.message.annotations) {
1520
+ if (annotation.type === "url_citation") {
1521
+ content.push({
1522
+ type: "source",
1523
+ sourceType: "url",
1524
+ id: annotation.url_citation.url,
1525
+ url: annotation.url_citation.url,
1526
+ title: annotation.url_citation.title,
1527
+ providerMetadata: {
1528
+ openrouter: {
1529
+ content: annotation.url_citation.content || ""
1530
+ }
1531
+ }
1532
+ });
1533
+ }
1534
+ }
1535
+ }
1482
1536
  return {
1483
1537
  content,
1484
1538
  finishReason: mapLLMGatewayFinishReason(choice.finish_reason),
@@ -1764,6 +1818,15 @@ var LLMGatewayChatLanguageModel = class {
1764
1818
  }
1765
1819
  }
1766
1820
  }
1821
+ if (delta.images != null) {
1822
+ for (const image of delta.images) {
1823
+ controller.enqueue({
1824
+ type: "file",
1825
+ mediaType: getMediaType(image.image_url.url, "image/jpeg"),
1826
+ data: getBase64FromDataUrl(image.image_url.url)
1827
+ });
1828
+ }
1829
+ }
1767
1830
  },
1768
1831
  flush(controller) {
1769
1832
  var _a16;
@@ -1917,36 +1980,36 @@ ${assistantMessage}
1917
1980
  }
1918
1981
 
1919
1982
  // src/completion/schemas.ts
1920
- import { z as z6 } from "zod/v4";
1921
- var LLMGatewayCompletionChunkSchema = z6.union([
1922
- z6.object({
1923
- id: z6.string().optional(),
1924
- model: z6.string().optional(),
1925
- choices: z6.array(
1926
- z6.object({
1927
- text: z6.string(),
1928
- reasoning: z6.string().nullish().optional(),
1983
+ import { z as z7 } from "zod/v4";
1984
+ var LLMGatewayCompletionChunkSchema = z7.union([
1985
+ z7.object({
1986
+ id: z7.string().optional(),
1987
+ model: z7.string().optional(),
1988
+ choices: z7.array(
1989
+ z7.object({
1990
+ text: z7.string(),
1991
+ reasoning: z7.string().nullish().optional(),
1929
1992
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1930
- finish_reason: z6.string().nullish(),
1931
- index: z6.number().nullish(),
1932
- logprobs: z6.object({
1933
- tokens: z6.array(z6.string()),
1934
- token_logprobs: z6.array(z6.number()),
1935
- top_logprobs: z6.array(z6.record(z6.string(), z6.number())).nullable()
1993
+ finish_reason: z7.string().nullish(),
1994
+ index: z7.number().nullish(),
1995
+ logprobs: z7.object({
1996
+ tokens: z7.array(z7.string()),
1997
+ token_logprobs: z7.array(z7.number()),
1998
+ top_logprobs: z7.array(z7.record(z7.string(), z7.number())).nullable()
1936
1999
  }).nullable().optional()
1937
2000
  })
1938
2001
  ),
1939
- usage: z6.object({
1940
- prompt_tokens: z6.number(),
1941
- prompt_tokens_details: z6.object({
1942
- cached_tokens: z6.number()
2002
+ usage: z7.object({
2003
+ prompt_tokens: z7.number(),
2004
+ prompt_tokens_details: z7.object({
2005
+ cached_tokens: z7.number()
1943
2006
  }).nullish(),
1944
- completion_tokens: z6.number(),
1945
- completion_tokens_details: z6.object({
1946
- reasoning_tokens: z6.number()
2007
+ completion_tokens: z7.number(),
2008
+ completion_tokens_details: z7.object({
2009
+ reasoning_tokens: z7.number()
1947
2010
  }).nullish(),
1948
- total_tokens: z6.number(),
1949
- cost: z6.number().optional()
2011
+ total_tokens: z7.number(),
2012
+ cost: z7.number().optional()
1950
2013
  }).nullish()
1951
2014
  }),
1952
2015
  LLMGatewayErrorResponseSchema
@@ -2217,9 +2280,9 @@ var LLMGateway = class {
2217
2280
 
2218
2281
  // src/provider.ts
2219
2282
  function createLLMGateway(options = {}) {
2220
- var _a15, _b, _c;
2221
- const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://api.llmgateway.io/v1";
2222
- const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
2283
+ var _a15, _b, _c, _d;
2284
+ const baseURL = (_c = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : process.env.LLM_GATEWAY_API_BASE) != null ? _c : "https://api.llmgateway.io/v1";
2285
+ const compatibility = (_d = options.compatibility) != null ? _d : "compatible";
2223
2286
  const getHeaders = () => __spreadValues({
2224
2287
  Authorization: `Bearer ${loadApiKey({
2225
2288
  apiKey: options.apiKey,