@openrouter/ai-sdk-provider 1.2.1 → 1.2.2

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}`;
@@ -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)
@@ -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
  }
@@ -1294,57 +1311,89 @@ var OpenRouterChatCompletionBaseResponseSchema = z7.object({
1294
1311
  }).nullish()
1295
1312
  }).nullish()
1296
1313
  });
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()
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
+ })
1325
1333
  })
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(
1334
+ ).optional(),
1335
+ annotations: z7.array(
1336
+ z7.union([
1337
+ // URL citation from web search
1338
+ z7.object({
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
+ })
1347
+ }),
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
+ })
1355
+ }),
1356
+ // File annotation from FileParserPlugin (new format)
1336
1357
  z7.object({
1337
- token: z7.string(),
1338
- logprob: z7.number()
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
+ })
1367
+ ).optional()
1368
+ })
1339
1369
  })
1340
- )
1341
- })
1342
- ).nullable()
1343
- }).nullable().optional(),
1344
- finish_reason: z7.string().optional().nullable()
1345
- })
1346
- )
1347
- });
1370
+ ])
1371
+ ).nullish()
1372
+ }),
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
+ })
1384
+ )
1385
+ })
1386
+ ).nullable()
1387
+ }).nullable().optional(),
1388
+ finish_reason: z7.string().optional().nullable()
1389
+ })
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(
@@ -1367,16 +1416,41 @@ var OpenRouterStreamChatCompletionChunkSchema = z7.union([
1367
1416
  })
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()
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
+ })
1430
+ }),
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
+ })
1438
+ }),
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
+ })
1450
+ ).optional()
1451
+ })
1378
1452
  })
1379
- })
1453
+ ])
1380
1454
  ).nullish()
1381
1455
  }).nullish(),
1382
1456
  logprobs: z7.object({
@@ -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,25 @@ var OpenRouterChatLanguageModel = class {
1515
1589
  abortSignal: options.abortSignal,
1516
1590
  fetch: this.config.fetch
1517
1591
  });
1592
+ if ("error" in responseValue) {
1593
+ throw new APICallError({
1594
+ message: responseValue.error.message,
1595
+ url: this.config.url({
1596
+ path: "/chat/completions",
1597
+ modelId: this.modelId
1598
+ }),
1599
+ requestBodyValues: args,
1600
+ statusCode: 200,
1601
+ responseHeaders,
1602
+ data: responseValue.error
1603
+ });
1604
+ }
1605
+ const response = responseValue;
1518
1606
  const choice = response.choices[0];
1519
1607
  if (!choice) {
1520
- throw new Error("No choice in response");
1608
+ throw new NoContentGeneratedError({
1609
+ message: "No choice in response"
1610
+ });
1521
1611
  }
1522
1612
  const usageInfo = response.usage ? {
1523
1613
  inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
@@ -1877,7 +1967,10 @@ var OpenRouterChatLanguageModel = class {
1877
1967
  };
1878
1968
  const toolCall2 = toolCalls[index];
1879
1969
  if (toolCall2 == null) {
1880
- throw new Error("Tool call is missing");
1970
+ throw new InvalidResponseDataError({
1971
+ data: { index, toolCallsLength: toolCalls.length },
1972
+ message: `Tool call at index ${index} is missing after creation.`
1973
+ });
1881
1974
  }
1882
1975
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1883
1976
  toolCall2.inputStarted = true;
@@ -1907,7 +2000,14 @@ var OpenRouterChatLanguageModel = class {
1907
2000
  }
1908
2001
  const toolCall = toolCalls[index];
1909
2002
  if (toolCall == null) {
1910
- throw new Error("Tool call is missing");
2003
+ throw new InvalidResponseDataError({
2004
+ data: {
2005
+ index,
2006
+ toolCallsLength: toolCalls.length,
2007
+ toolCallDelta
2008
+ },
2009
+ message: `Tool call at index ${index} is missing during merge.`
2010
+ });
1911
2011
  }
1912
2012
  if (!toolCall.inputStarted) {
1913
2013
  toolCall.inputStarted = true;
@@ -2232,11 +2332,23 @@ var OpenRouterCompletionLanguageModel = class {
2232
2332
  fetch: this.config.fetch
2233
2333
  });
2234
2334
  if ("error" in response) {
2235
- throw new Error(`${response.error.message}`);
2335
+ throw new APICallError({
2336
+ message: response.error.message,
2337
+ url: this.config.url({
2338
+ path: "/completions",
2339
+ modelId: this.modelId
2340
+ }),
2341
+ requestBodyValues: args,
2342
+ statusCode: 200,
2343
+ responseHeaders,
2344
+ data: response.error
2345
+ });
2236
2346
  }
2237
2347
  const choice = response.choices[0];
2238
2348
  if (!choice) {
2239
- throw new Error("No choice in OpenRouter completion response");
2349
+ throw new NoContentGeneratedError({
2350
+ message: "No choice in OpenRouter completion response"
2351
+ });
2240
2352
  }
2241
2353
  return {
2242
2354
  content: [
@@ -2403,9 +2515,6 @@ var OpenRouter = class {
2403
2515
  }
2404
2516
  };
2405
2517
 
2406
- // src/version.ts
2407
- var VERSION = false ? "0.0.0-test" : "1.2.1";
2408
-
2409
2518
  // src/utils/remove-undefined.ts
2410
2519
  function removeUndefinedEntries2(record) {
2411
2520
  return Object.fromEntries(
@@ -2425,6 +2534,9 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
2425
2534
  });
2426
2535
  }
2427
2536
 
2537
+ // src/version.ts
2538
+ var VERSION = false ? "0.0.0-test" : "1.2.2";
2539
+
2428
2540
  // src/provider.ts
2429
2541
  function createOpenRouter(options = {}) {
2430
2542
  var _a15, _b, _c;