@openrouter/ai-sdk-provider 1.2.1 → 1.2.3

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
@@ -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}`;
@@ -904,7 +916,7 @@ var CommonReasoningDetailSchema = z.object({
904
916
  id: z.string().nullish(),
905
917
  format: z.nativeEnum(ReasoningFormat).nullish(),
906
918
  index: z.number().optional()
907
- });
919
+ }).passthrough();
908
920
  var ReasoningDetailSummarySchema = z.object({
909
921
  type: z.literal("reasoning.summary" /* Summary */),
910
922
  summary: z.string()
@@ -933,12 +945,16 @@ var OutputUnionToReasoningDetailsSchema = z.union([
933
945
  delta: z.object({
934
946
  reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
935
947
  })
936
- }).transform((data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)),
948
+ }).transform(
949
+ (data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)
950
+ ),
937
951
  z.object({
938
952
  message: z.object({
939
953
  reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
940
954
  })
941
- }).transform((data) => data.message.reasoning_details.filter(isDefinedOrNotNull)),
955
+ }).transform(
956
+ (data) => data.message.reasoning_details.filter(isDefinedOrNotNull)
957
+ ),
942
958
  z.object({
943
959
  text: z.string(),
944
960
  reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
@@ -953,8 +969,8 @@ var OpenRouterErrorResponseSchema = z2.object({
953
969
  message: z2.string(),
954
970
  type: z2.string().nullable().optional().default(null),
955
971
  param: z2.any().nullable().optional().default(null)
956
- })
957
- });
972
+ }).passthrough()
973
+ }).passthrough();
958
974
  var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
959
975
  errorSchema: OpenRouterErrorResponseSchema,
960
976
  errorToMessage: (data) => data.error.message
@@ -969,18 +985,18 @@ var OpenRouterProviderMetadataSchema = z3.object({
969
985
  promptTokens: z3.number(),
970
986
  promptTokensDetails: z3.object({
971
987
  cachedTokens: z3.number()
972
- }).optional(),
988
+ }).passthrough().optional(),
973
989
  completionTokens: z3.number(),
974
990
  completionTokensDetails: z3.object({
975
991
  reasoningTokens: z3.number()
976
- }).optional(),
992
+ }).passthrough().optional(),
977
993
  totalTokens: z3.number(),
978
994
  cost: z3.number().optional(),
979
995
  costDetails: z3.object({
980
996
  upstreamInferenceCost: z3.number()
981
- })
982
- })
983
- });
997
+ }).passthrough()
998
+ }).passthrough()
999
+ }).passthrough();
984
1000
  var OpenRouterProviderOptionsSchema = z3.object({
985
1001
  openrouter: z3.object({
986
1002
  reasoning_details: z3.array(ReasoningDetailUnionSchema).optional()
@@ -1186,9 +1202,7 @@ function convertToOpenRouterChatMessages(prompt) {
1186
1202
  }
1187
1203
  }
1188
1204
  }
1189
- const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(
1190
- providerOptions
1191
- );
1205
+ const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
1192
1206
  const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
1193
1207
  messages.push({
1194
1208
  role: "assistant",
@@ -1251,7 +1265,10 @@ function getChatCompletionToolChoice(toolChoice) {
1251
1265
  }
1252
1266
  default: {
1253
1267
  toolChoice;
1254
- throw new Error(`Invalid tool choice type: ${toolChoice}`);
1268
+ throw new InvalidArgumentError({
1269
+ argument: "toolChoice",
1270
+ message: `Invalid tool choice type: ${JSON.stringify(toolChoice)}`
1271
+ });
1255
1272
  }
1256
1273
  }
1257
1274
  }
@@ -1265,8 +1282,8 @@ var ImageResponseSchema = z6.object({
1265
1282
  type: z6.literal("image_url"),
1266
1283
  image_url: z6.object({
1267
1284
  url: z6.string()
1268
- })
1269
- });
1285
+ }).passthrough()
1286
+ }).passthrough();
1270
1287
  var ImageResponseWithUnknownSchema = z6.union([
1271
1288
  ImageResponseSchema,
1272
1289
  z6.unknown().transform(() => null)
@@ -1282,69 +1299,101 @@ var OpenRouterChatCompletionBaseResponseSchema = z7.object({
1282
1299
  prompt_tokens: z7.number(),
1283
1300
  prompt_tokens_details: z7.object({
1284
1301
  cached_tokens: z7.number()
1285
- }).nullish(),
1302
+ }).passthrough().nullish(),
1286
1303
  completion_tokens: z7.number(),
1287
1304
  completion_tokens_details: z7.object({
1288
1305
  reasoning_tokens: z7.number()
1289
- }).nullish(),
1306
+ }).passthrough().nullish(),
1290
1307
  total_tokens: z7.number(),
1291
1308
  cost: z7.number().optional(),
1292
1309
  cost_details: z7.object({
1293
1310
  upstream_inference_cost: z7.number().nullish()
1294
- }).nullish()
1295
- }).nullish()
1296
- });
1297
- var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1298
- choices: z7.array(
1299
- z7.object({
1300
- message: z7.object({
1301
- role: z7.literal("assistant"),
1302
- content: z7.string().nullable().optional(),
1303
- reasoning: z7.string().nullable().optional(),
1304
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1305
- images: ImageResponseArraySchema.nullish(),
1306
- tool_calls: z7.array(
1307
- z7.object({
1308
- id: z7.string().optional().nullable(),
1309
- type: z7.literal("function"),
1310
- function: z7.object({
1311
- name: z7.string(),
1312
- arguments: z7.string()
1313
- })
1314
- })
1315
- ).optional(),
1316
- annotations: z7.array(
1317
- z7.object({
1318
- type: z7.enum(["url_citation"]),
1319
- url_citation: z7.object({
1320
- end_index: z7.number(),
1321
- start_index: z7.number(),
1322
- title: z7.string(),
1323
- url: z7.string(),
1324
- content: z7.string().optional()
1325
- })
1326
- })
1327
- ).nullish()
1328
- }),
1329
- index: z7.number().nullish(),
1330
- logprobs: z7.object({
1331
- content: z7.array(
1332
- z7.object({
1333
- token: z7.string(),
1334
- logprob: z7.number(),
1335
- top_logprobs: z7.array(
1311
+ }).passthrough().nullish()
1312
+ }).passthrough().nullish()
1313
+ }).passthrough();
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
+ }).passthrough()
1333
+ }).passthrough()
1334
+ ).optional(),
1335
+ annotations: z7.array(
1336
+ z7.union([
1337
+ // URL citation from web search
1336
1338
  z7.object({
1337
- token: z7.string(),
1338
- logprob: z7.number()
1339
- })
1340
- )
1341
- })
1342
- ).nullable()
1343
- }).nullable().optional(),
1344
- finish_reason: z7.string().optional().nullable()
1345
- })
1346
- )
1347
- });
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
+ }).passthrough()
1347
+ }).passthrough(),
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
+ }).passthrough()
1355
+ }).passthrough(),
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
+ }).passthrough()
1367
+ ).optional()
1368
+ }).passthrough()
1369
+ }).passthrough()
1370
+ ])
1371
+ ).nullish()
1372
+ }).passthrough(),
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
+ }).passthrough()
1384
+ )
1385
+ }).passthrough()
1386
+ ).nullable()
1387
+ }).passthrough().nullable().optional(),
1388
+ finish_reason: z7.string().optional().nullable()
1389
+ }).passthrough()
1390
+ )
1391
+ }),
1392
+ // Error response (HTTP 200 with error payload)
1393
+ OpenRouterErrorResponseSchema.extend({
1394
+ user_id: z7.string().optional()
1395
+ })
1396
+ ]);
1348
1397
  var OpenRouterStreamChatCompletionChunkSchema = z7.union([
1349
1398
  OpenRouterChatCompletionBaseResponseSchema.extend({
1350
1399
  choices: z7.array(
@@ -1363,22 +1412,47 @@ var OpenRouterStreamChatCompletionChunkSchema = z7.union([
1363
1412
  function: z7.object({
1364
1413
  name: z7.string().nullish(),
1365
1414
  arguments: z7.string().nullish()
1366
- })
1367
- })
1415
+ }).passthrough()
1416
+ }).passthrough()
1368
1417
  ).nullish(),
1369
1418
  annotations: z7.array(
1370
- z7.object({
1371
- type: z7.enum(["url_citation"]),
1372
- url_citation: z7.object({
1373
- end_index: z7.number(),
1374
- start_index: z7.number(),
1375
- title: z7.string(),
1376
- url: z7.string(),
1377
- content: z7.string().optional()
1378
- })
1379
- })
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
+ }).passthrough()
1430
+ }).passthrough(),
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
+ }).passthrough()
1438
+ }).passthrough(),
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
+ }).passthrough()
1450
+ ).optional()
1451
+ }).passthrough()
1452
+ }).passthrough()
1453
+ ])
1380
1454
  ).nullish()
1381
- }).nullish(),
1455
+ }).passthrough().nullish(),
1382
1456
  logprobs: z7.object({
1383
1457
  content: z7.array(
1384
1458
  z7.object({
@@ -1388,14 +1462,14 @@ var OpenRouterStreamChatCompletionChunkSchema = z7.union([
1388
1462
  z7.object({
1389
1463
  token: z7.string(),
1390
1464
  logprob: z7.number()
1391
- })
1465
+ }).passthrough()
1392
1466
  )
1393
- })
1467
+ }).passthrough()
1394
1468
  ).nullable()
1395
- }).nullish(),
1469
+ }).passthrough().nullish(),
1396
1470
  finish_reason: z7.string().nullable().optional(),
1397
1471
  index: z7.number().nullish()
1398
- })
1472
+ }).passthrough()
1399
1473
  )
1400
1474
  }),
1401
1475
  OpenRouterErrorResponseSchema
@@ -1501,7 +1575,7 @@ var OpenRouterChatLanguageModel = class {
1501
1575
  const providerOptions = options.providerOptions || {};
1502
1576
  const openrouterOptions = providerOptions.openrouter || {};
1503
1577
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
1504
- const { value: response, responseHeaders } = await postJsonToApi({
1578
+ const { value: responseValue, responseHeaders } = await postJsonToApi({
1505
1579
  url: this.config.url({
1506
1580
  path: "/chat/completions",
1507
1581
  modelId: this.modelId
@@ -1515,9 +1589,26 @@ var OpenRouterChatLanguageModel = class {
1515
1589
  abortSignal: options.abortSignal,
1516
1590
  fetch: this.config.fetch
1517
1591
  });
1592
+ if ("error" in responseValue) {
1593
+ const errorData = responseValue.error;
1594
+ throw new APICallError({
1595
+ message: errorData.message,
1596
+ url: this.config.url({
1597
+ path: "/chat/completions",
1598
+ modelId: this.modelId
1599
+ }),
1600
+ requestBodyValues: args,
1601
+ statusCode: 200,
1602
+ responseHeaders,
1603
+ data: errorData
1604
+ });
1605
+ }
1606
+ const response = responseValue;
1518
1607
  const choice = response.choices[0];
1519
1608
  if (!choice) {
1520
- throw new Error("No choice in response");
1609
+ throw new NoContentGeneratedError({
1610
+ message: "No choice in response"
1611
+ });
1521
1612
  }
1522
1613
  const usageInfo = response.usage ? {
1523
1614
  inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
@@ -1877,7 +1968,10 @@ var OpenRouterChatLanguageModel = class {
1877
1968
  };
1878
1969
  const toolCall2 = toolCalls[index];
1879
1970
  if (toolCall2 == null) {
1880
- throw new Error("Tool call is missing");
1971
+ throw new InvalidResponseDataError({
1972
+ data: { index, toolCallsLength: toolCalls.length },
1973
+ message: `Tool call at index ${index} is missing after creation.`
1974
+ });
1881
1975
  }
1882
1976
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1883
1977
  toolCall2.inputStarted = true;
@@ -1907,7 +2001,14 @@ var OpenRouterChatLanguageModel = class {
1907
2001
  }
1908
2002
  const toolCall = toolCalls[index];
1909
2003
  if (toolCall == null) {
1910
- throw new Error("Tool call is missing");
2004
+ throw new InvalidResponseDataError({
2005
+ data: {
2006
+ index,
2007
+ toolCallsLength: toolCalls.length,
2008
+ toolCallDelta
2009
+ },
2010
+ message: `Tool call at index ${index} is missing during merge.`
2011
+ });
1911
2012
  }
1912
2013
  if (!toolCall.inputStarted) {
1913
2014
  toolCall.inputStarted = true;
@@ -2121,22 +2222,22 @@ var OpenRouterCompletionChunkSchema = z8.union([
2121
2222
  tokens: z8.array(z8.string()),
2122
2223
  token_logprobs: z8.array(z8.number()),
2123
2224
  top_logprobs: z8.array(z8.record(z8.string(), z8.number())).nullable()
2124
- }).nullable().optional()
2125
- })
2225
+ }).passthrough().nullable().optional()
2226
+ }).passthrough()
2126
2227
  ),
2127
2228
  usage: z8.object({
2128
2229
  prompt_tokens: z8.number(),
2129
2230
  prompt_tokens_details: z8.object({
2130
2231
  cached_tokens: z8.number()
2131
- }).nullish(),
2232
+ }).passthrough().nullish(),
2132
2233
  completion_tokens: z8.number(),
2133
2234
  completion_tokens_details: z8.object({
2134
2235
  reasoning_tokens: z8.number()
2135
- }).nullish(),
2236
+ }).passthrough().nullish(),
2136
2237
  total_tokens: z8.number(),
2137
2238
  cost: z8.number().optional()
2138
- }).nullish()
2139
- }),
2239
+ }).passthrough().nullish()
2240
+ }).passthrough(),
2140
2241
  OpenRouterErrorResponseSchema
2141
2242
  ]);
2142
2243
 
@@ -2232,11 +2333,24 @@ var OpenRouterCompletionLanguageModel = class {
2232
2333
  fetch: this.config.fetch
2233
2334
  });
2234
2335
  if ("error" in response) {
2235
- throw new Error(`${response.error.message}`);
2336
+ const errorData = response.error;
2337
+ throw new APICallError({
2338
+ message: errorData.message,
2339
+ url: this.config.url({
2340
+ path: "/completions",
2341
+ modelId: this.modelId
2342
+ }),
2343
+ requestBodyValues: args,
2344
+ statusCode: 200,
2345
+ responseHeaders,
2346
+ data: errorData
2347
+ });
2236
2348
  }
2237
2349
  const choice = response.choices[0];
2238
2350
  if (!choice) {
2239
- throw new Error("No choice in OpenRouter completion response");
2351
+ throw new NoContentGeneratedError({
2352
+ message: "No choice in OpenRouter completion response"
2353
+ });
2240
2354
  }
2241
2355
  return {
2242
2356
  content: [
@@ -2403,9 +2517,6 @@ var OpenRouter = class {
2403
2517
  }
2404
2518
  };
2405
2519
 
2406
- // src/version.ts
2407
- var VERSION = false ? "0.0.0-test" : "1.2.1";
2408
-
2409
2520
  // src/utils/remove-undefined.ts
2410
2521
  function removeUndefinedEntries2(record) {
2411
2522
  return Object.fromEntries(
@@ -2425,6 +2536,9 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
2425
2536
  });
2426
2537
  }
2427
2538
 
2539
+ // src/version.ts
2540
+ var VERSION = false ? "0.0.0-test" : "1.2.3";
2541
+
2428
2542
  // src/provider.ts
2429
2543
  function createOpenRouter(options = {}) {
2430
2544
  var _a15, _b, _c;