@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.
@@ -242,6 +242,18 @@ var name9 = "AI_NoContentGeneratedError";
242
242
  var marker10 = `vercel.ai.error.${name9}`;
243
243
  var symbol10 = Symbol.for(marker10);
244
244
  var _a10;
245
+ var NoContentGeneratedError = class extends AISDKError {
246
+ // used in isInstance
247
+ constructor({
248
+ message = "No content generated."
249
+ } = {}) {
250
+ super({ name: name9, message });
251
+ this[_a10] = true;
252
+ }
253
+ static isInstance(error) {
254
+ return AISDKError.hasMarker(error, marker10);
255
+ }
256
+ };
245
257
  _a10 = symbol10;
246
258
  var name10 = "AI_NoSuchModelError";
247
259
  var marker11 = `vercel.ai.error.${name10}`;
@@ -893,7 +905,7 @@ var CommonReasoningDetailSchema = import_v4.z.object({
893
905
  id: import_v4.z.string().nullish(),
894
906
  format: import_v4.z.nativeEnum(ReasoningFormat).nullish(),
895
907
  index: import_v4.z.number().optional()
896
- });
908
+ }).passthrough();
897
909
  var ReasoningDetailSummarySchema = import_v4.z.object({
898
910
  type: import_v4.z.literal("reasoning.summary" /* Summary */),
899
911
  summary: import_v4.z.string()
@@ -922,12 +934,16 @@ var OutputUnionToReasoningDetailsSchema = import_v4.z.union([
922
934
  delta: import_v4.z.object({
923
935
  reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
924
936
  })
925
- }).transform((data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)),
937
+ }).transform(
938
+ (data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)
939
+ ),
926
940
  import_v4.z.object({
927
941
  message: import_v4.z.object({
928
942
  reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
929
943
  })
930
- }).transform((data) => data.message.reasoning_details.filter(isDefinedOrNotNull)),
944
+ }).transform(
945
+ (data) => data.message.reasoning_details.filter(isDefinedOrNotNull)
946
+ ),
931
947
  import_v4.z.object({
932
948
  text: import_v4.z.string(),
933
949
  reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
@@ -942,8 +958,8 @@ var OpenRouterErrorResponseSchema = import_v42.z.object({
942
958
  message: import_v42.z.string(),
943
959
  type: import_v42.z.string().nullable().optional().default(null),
944
960
  param: import_v42.z.any().nullable().optional().default(null)
945
- })
946
- });
961
+ }).passthrough()
962
+ }).passthrough();
947
963
  var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
948
964
  errorSchema: OpenRouterErrorResponseSchema,
949
965
  errorToMessage: (data) => data.error.message
@@ -958,18 +974,18 @@ var OpenRouterProviderMetadataSchema = import_v43.z.object({
958
974
  promptTokens: import_v43.z.number(),
959
975
  promptTokensDetails: import_v43.z.object({
960
976
  cachedTokens: import_v43.z.number()
961
- }).optional(),
977
+ }).passthrough().optional(),
962
978
  completionTokens: import_v43.z.number(),
963
979
  completionTokensDetails: import_v43.z.object({
964
980
  reasoningTokens: import_v43.z.number()
965
- }).optional(),
981
+ }).passthrough().optional(),
966
982
  totalTokens: import_v43.z.number(),
967
983
  cost: import_v43.z.number().optional(),
968
984
  costDetails: import_v43.z.object({
969
985
  upstreamInferenceCost: import_v43.z.number()
970
- })
971
- })
972
- });
986
+ }).passthrough()
987
+ }).passthrough()
988
+ }).passthrough();
973
989
  var OpenRouterProviderOptionsSchema = import_v43.z.object({
974
990
  openrouter: import_v43.z.object({
975
991
  reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional()
@@ -1175,9 +1191,7 @@ function convertToOpenRouterChatMessages(prompt) {
1175
1191
  }
1176
1192
  }
1177
1193
  }
1178
- const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(
1179
- providerOptions
1180
- );
1194
+ const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
1181
1195
  const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
1182
1196
  messages.push({
1183
1197
  role: "assistant",
@@ -1240,7 +1254,10 @@ function getChatCompletionToolChoice(toolChoice) {
1240
1254
  }
1241
1255
  default: {
1242
1256
  toolChoice;
1243
- throw new Error(`Invalid tool choice type: ${toolChoice}`);
1257
+ throw new InvalidArgumentError({
1258
+ argument: "toolChoice",
1259
+ message: `Invalid tool choice type: ${JSON.stringify(toolChoice)}`
1260
+ });
1244
1261
  }
1245
1262
  }
1246
1263
  }
@@ -1254,8 +1271,8 @@ var ImageResponseSchema = import_v45.z.object({
1254
1271
  type: import_v45.z.literal("image_url"),
1255
1272
  image_url: import_v45.z.object({
1256
1273
  url: import_v45.z.string()
1257
- })
1258
- });
1274
+ }).passthrough()
1275
+ }).passthrough();
1259
1276
  var ImageResponseWithUnknownSchema = import_v45.z.union([
1260
1277
  ImageResponseSchema,
1261
1278
  import_v45.z.unknown().transform(() => null)
@@ -1271,69 +1288,101 @@ var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
1271
1288
  prompt_tokens: import_v46.z.number(),
1272
1289
  prompt_tokens_details: import_v46.z.object({
1273
1290
  cached_tokens: import_v46.z.number()
1274
- }).nullish(),
1291
+ }).passthrough().nullish(),
1275
1292
  completion_tokens: import_v46.z.number(),
1276
1293
  completion_tokens_details: import_v46.z.object({
1277
1294
  reasoning_tokens: import_v46.z.number()
1278
- }).nullish(),
1295
+ }).passthrough().nullish(),
1279
1296
  total_tokens: import_v46.z.number(),
1280
1297
  cost: import_v46.z.number().optional(),
1281
1298
  cost_details: import_v46.z.object({
1282
1299
  upstream_inference_cost: import_v46.z.number().nullish()
1283
- }).nullish()
1284
- }).nullish()
1285
- });
1286
- var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1287
- choices: import_v46.z.array(
1288
- import_v46.z.object({
1289
- message: import_v46.z.object({
1290
- role: import_v46.z.literal("assistant"),
1291
- content: import_v46.z.string().nullable().optional(),
1292
- reasoning: import_v46.z.string().nullable().optional(),
1293
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1294
- images: ImageResponseArraySchema.nullish(),
1295
- tool_calls: import_v46.z.array(
1296
- import_v46.z.object({
1297
- id: import_v46.z.string().optional().nullable(),
1298
- type: import_v46.z.literal("function"),
1299
- function: import_v46.z.object({
1300
- name: import_v46.z.string(),
1301
- arguments: import_v46.z.string()
1302
- })
1303
- })
1304
- ).optional(),
1305
- annotations: import_v46.z.array(
1306
- import_v46.z.object({
1307
- type: import_v46.z.enum(["url_citation"]),
1308
- url_citation: import_v46.z.object({
1309
- end_index: import_v46.z.number(),
1310
- start_index: import_v46.z.number(),
1311
- title: import_v46.z.string(),
1312
- url: import_v46.z.string(),
1313
- content: import_v46.z.string().optional()
1314
- })
1315
- })
1316
- ).nullish()
1317
- }),
1318
- index: import_v46.z.number().nullish(),
1319
- logprobs: import_v46.z.object({
1320
- content: import_v46.z.array(
1321
- import_v46.z.object({
1322
- token: import_v46.z.string(),
1323
- logprob: import_v46.z.number(),
1324
- top_logprobs: import_v46.z.array(
1300
+ }).passthrough().nullish()
1301
+ }).passthrough().nullish()
1302
+ }).passthrough();
1303
+ var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
1304
+ // Success response with choices
1305
+ OpenRouterChatCompletionBaseResponseSchema.extend({
1306
+ choices: import_v46.z.array(
1307
+ import_v46.z.object({
1308
+ message: import_v46.z.object({
1309
+ role: import_v46.z.literal("assistant"),
1310
+ content: import_v46.z.string().nullable().optional(),
1311
+ reasoning: import_v46.z.string().nullable().optional(),
1312
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1313
+ images: ImageResponseArraySchema.nullish(),
1314
+ tool_calls: import_v46.z.array(
1315
+ import_v46.z.object({
1316
+ id: import_v46.z.string().optional().nullable(),
1317
+ type: import_v46.z.literal("function"),
1318
+ function: import_v46.z.object({
1319
+ name: import_v46.z.string(),
1320
+ arguments: import_v46.z.string()
1321
+ }).passthrough()
1322
+ }).passthrough()
1323
+ ).optional(),
1324
+ annotations: import_v46.z.array(
1325
+ import_v46.z.union([
1326
+ // URL citation from web search
1325
1327
  import_v46.z.object({
1326
- token: import_v46.z.string(),
1327
- logprob: import_v46.z.number()
1328
- })
1329
- )
1330
- })
1331
- ).nullable()
1332
- }).nullable().optional(),
1333
- finish_reason: import_v46.z.string().optional().nullable()
1334
- })
1335
- )
1336
- });
1328
+ type: import_v46.z.literal("url_citation"),
1329
+ url_citation: import_v46.z.object({
1330
+ end_index: import_v46.z.number(),
1331
+ start_index: import_v46.z.number(),
1332
+ title: import_v46.z.string(),
1333
+ url: import_v46.z.string(),
1334
+ content: import_v46.z.string().optional()
1335
+ }).passthrough()
1336
+ }).passthrough(),
1337
+ // File annotation from FileParserPlugin (old format)
1338
+ import_v46.z.object({
1339
+ type: import_v46.z.literal("file_annotation"),
1340
+ file_annotation: import_v46.z.object({
1341
+ file_id: import_v46.z.string(),
1342
+ quote: import_v46.z.string().optional()
1343
+ }).passthrough()
1344
+ }).passthrough(),
1345
+ // File annotation from FileParserPlugin (new format)
1346
+ import_v46.z.object({
1347
+ type: import_v46.z.literal("file"),
1348
+ file: import_v46.z.object({
1349
+ hash: import_v46.z.string(),
1350
+ name: import_v46.z.string(),
1351
+ content: import_v46.z.array(
1352
+ import_v46.z.object({
1353
+ type: import_v46.z.string(),
1354
+ text: import_v46.z.string()
1355
+ }).passthrough()
1356
+ ).optional()
1357
+ }).passthrough()
1358
+ }).passthrough()
1359
+ ])
1360
+ ).nullish()
1361
+ }).passthrough(),
1362
+ index: import_v46.z.number().nullish(),
1363
+ logprobs: import_v46.z.object({
1364
+ content: import_v46.z.array(
1365
+ import_v46.z.object({
1366
+ token: import_v46.z.string(),
1367
+ logprob: import_v46.z.number(),
1368
+ top_logprobs: import_v46.z.array(
1369
+ import_v46.z.object({
1370
+ token: import_v46.z.string(),
1371
+ logprob: import_v46.z.number()
1372
+ }).passthrough()
1373
+ )
1374
+ }).passthrough()
1375
+ ).nullable()
1376
+ }).passthrough().nullable().optional(),
1377
+ finish_reason: import_v46.z.string().optional().nullable()
1378
+ }).passthrough()
1379
+ )
1380
+ }),
1381
+ // Error response (HTTP 200 with error payload)
1382
+ OpenRouterErrorResponseSchema.extend({
1383
+ user_id: import_v46.z.string().optional()
1384
+ })
1385
+ ]);
1337
1386
  var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
1338
1387
  OpenRouterChatCompletionBaseResponseSchema.extend({
1339
1388
  choices: import_v46.z.array(
@@ -1352,22 +1401,47 @@ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
1352
1401
  function: import_v46.z.object({
1353
1402
  name: import_v46.z.string().nullish(),
1354
1403
  arguments: import_v46.z.string().nullish()
1355
- })
1356
- })
1404
+ }).passthrough()
1405
+ }).passthrough()
1357
1406
  ).nullish(),
1358
1407
  annotations: import_v46.z.array(
1359
- import_v46.z.object({
1360
- type: import_v46.z.enum(["url_citation"]),
1361
- url_citation: import_v46.z.object({
1362
- end_index: import_v46.z.number(),
1363
- start_index: import_v46.z.number(),
1364
- title: import_v46.z.string(),
1365
- url: import_v46.z.string(),
1366
- content: import_v46.z.string().optional()
1367
- })
1368
- })
1408
+ import_v46.z.union([
1409
+ // URL citation from web search
1410
+ import_v46.z.object({
1411
+ type: import_v46.z.literal("url_citation"),
1412
+ url_citation: import_v46.z.object({
1413
+ end_index: import_v46.z.number(),
1414
+ start_index: import_v46.z.number(),
1415
+ title: import_v46.z.string(),
1416
+ url: import_v46.z.string(),
1417
+ content: import_v46.z.string().optional()
1418
+ }).passthrough()
1419
+ }).passthrough(),
1420
+ // File annotation from FileParserPlugin (old format)
1421
+ import_v46.z.object({
1422
+ type: import_v46.z.literal("file_annotation"),
1423
+ file_annotation: import_v46.z.object({
1424
+ file_id: import_v46.z.string(),
1425
+ quote: import_v46.z.string().optional()
1426
+ }).passthrough()
1427
+ }).passthrough(),
1428
+ // File annotation from FileParserPlugin (new format)
1429
+ import_v46.z.object({
1430
+ type: import_v46.z.literal("file"),
1431
+ file: import_v46.z.object({
1432
+ hash: import_v46.z.string(),
1433
+ name: import_v46.z.string(),
1434
+ content: import_v46.z.array(
1435
+ import_v46.z.object({
1436
+ type: import_v46.z.string(),
1437
+ text: import_v46.z.string()
1438
+ }).passthrough()
1439
+ ).optional()
1440
+ }).passthrough()
1441
+ }).passthrough()
1442
+ ])
1369
1443
  ).nullish()
1370
- }).nullish(),
1444
+ }).passthrough().nullish(),
1371
1445
  logprobs: import_v46.z.object({
1372
1446
  content: import_v46.z.array(
1373
1447
  import_v46.z.object({
@@ -1377,14 +1451,14 @@ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
1377
1451
  import_v46.z.object({
1378
1452
  token: import_v46.z.string(),
1379
1453
  logprob: import_v46.z.number()
1380
- })
1454
+ }).passthrough()
1381
1455
  )
1382
- })
1456
+ }).passthrough()
1383
1457
  ).nullable()
1384
- }).nullish(),
1458
+ }).passthrough().nullish(),
1385
1459
  finish_reason: import_v46.z.string().nullable().optional(),
1386
1460
  index: import_v46.z.number().nullish()
1387
- })
1461
+ }).passthrough()
1388
1462
  )
1389
1463
  }),
1390
1464
  OpenRouterErrorResponseSchema
@@ -1490,7 +1564,7 @@ var OpenRouterChatLanguageModel = class {
1490
1564
  const providerOptions = options.providerOptions || {};
1491
1565
  const openrouterOptions = providerOptions.openrouter || {};
1492
1566
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
1493
- const { value: response, responseHeaders } = await postJsonToApi({
1567
+ const { value: responseValue, responseHeaders } = await postJsonToApi({
1494
1568
  url: this.config.url({
1495
1569
  path: "/chat/completions",
1496
1570
  modelId: this.modelId
@@ -1504,9 +1578,26 @@ var OpenRouterChatLanguageModel = class {
1504
1578
  abortSignal: options.abortSignal,
1505
1579
  fetch: this.config.fetch
1506
1580
  });
1581
+ if ("error" in responseValue) {
1582
+ const errorData = responseValue.error;
1583
+ throw new APICallError({
1584
+ message: errorData.message,
1585
+ url: this.config.url({
1586
+ path: "/chat/completions",
1587
+ modelId: this.modelId
1588
+ }),
1589
+ requestBodyValues: args,
1590
+ statusCode: 200,
1591
+ responseHeaders,
1592
+ data: errorData
1593
+ });
1594
+ }
1595
+ const response = responseValue;
1507
1596
  const choice = response.choices[0];
1508
1597
  if (!choice) {
1509
- throw new Error("No choice in response");
1598
+ throw new NoContentGeneratedError({
1599
+ message: "No choice in response"
1600
+ });
1510
1601
  }
1511
1602
  const usageInfo = response.usage ? {
1512
1603
  inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
@@ -1866,7 +1957,10 @@ var OpenRouterChatLanguageModel = class {
1866
1957
  };
1867
1958
  const toolCall2 = toolCalls[index];
1868
1959
  if (toolCall2 == null) {
1869
- throw new Error("Tool call is missing");
1960
+ throw new InvalidResponseDataError({
1961
+ data: { index, toolCallsLength: toolCalls.length },
1962
+ message: `Tool call at index ${index} is missing after creation.`
1963
+ });
1870
1964
  }
1871
1965
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1872
1966
  toolCall2.inputStarted = true;
@@ -1896,7 +1990,14 @@ var OpenRouterChatLanguageModel = class {
1896
1990
  }
1897
1991
  const toolCall = toolCalls[index];
1898
1992
  if (toolCall == null) {
1899
- throw new Error("Tool call is missing");
1993
+ throw new InvalidResponseDataError({
1994
+ data: {
1995
+ index,
1996
+ toolCallsLength: toolCalls.length,
1997
+ toolCallDelta
1998
+ },
1999
+ message: `Tool call at index ${index} is missing during merge.`
2000
+ });
1900
2001
  }
1901
2002
  if (!toolCall.inputStarted) {
1902
2003
  toolCall.inputStarted = true;
@@ -2110,22 +2211,22 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
2110
2211
  tokens: import_v47.z.array(import_v47.z.string()),
2111
2212
  token_logprobs: import_v47.z.array(import_v47.z.number()),
2112
2213
  top_logprobs: import_v47.z.array(import_v47.z.record(import_v47.z.string(), import_v47.z.number())).nullable()
2113
- }).nullable().optional()
2114
- })
2214
+ }).passthrough().nullable().optional()
2215
+ }).passthrough()
2115
2216
  ),
2116
2217
  usage: import_v47.z.object({
2117
2218
  prompt_tokens: import_v47.z.number(),
2118
2219
  prompt_tokens_details: import_v47.z.object({
2119
2220
  cached_tokens: import_v47.z.number()
2120
- }).nullish(),
2221
+ }).passthrough().nullish(),
2121
2222
  completion_tokens: import_v47.z.number(),
2122
2223
  completion_tokens_details: import_v47.z.object({
2123
2224
  reasoning_tokens: import_v47.z.number()
2124
- }).nullish(),
2225
+ }).passthrough().nullish(),
2125
2226
  total_tokens: import_v47.z.number(),
2126
2227
  cost: import_v47.z.number().optional()
2127
- }).nullish()
2128
- }),
2228
+ }).passthrough().nullish()
2229
+ }).passthrough(),
2129
2230
  OpenRouterErrorResponseSchema
2130
2231
  ]);
2131
2232
 
@@ -2221,11 +2322,24 @@ var OpenRouterCompletionLanguageModel = class {
2221
2322
  fetch: this.config.fetch
2222
2323
  });
2223
2324
  if ("error" in response) {
2224
- throw new Error(`${response.error.message}`);
2325
+ const errorData = response.error;
2326
+ throw new APICallError({
2327
+ message: errorData.message,
2328
+ url: this.config.url({
2329
+ path: "/completions",
2330
+ modelId: this.modelId
2331
+ }),
2332
+ requestBodyValues: args,
2333
+ statusCode: 200,
2334
+ responseHeaders,
2335
+ data: errorData
2336
+ });
2225
2337
  }
2226
2338
  const choice = response.choices[0];
2227
2339
  if (!choice) {
2228
- throw new Error("No choice in OpenRouter completion response");
2340
+ throw new NoContentGeneratedError({
2341
+ message: "No choice in OpenRouter completion response"
2342
+ });
2229
2343
  }
2230
2344
  return {
2231
2345
  content: [