@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.
@@ -208,6 +208,18 @@ var name9 = "AI_NoContentGeneratedError";
208
208
  var marker10 = `vercel.ai.error.${name9}`;
209
209
  var symbol10 = Symbol.for(marker10);
210
210
  var _a10;
211
+ var NoContentGeneratedError = class extends AISDKError {
212
+ // used in isInstance
213
+ constructor({
214
+ message = "No content generated."
215
+ } = {}) {
216
+ super({ name: name9, message });
217
+ this[_a10] = true;
218
+ }
219
+ static isInstance(error) {
220
+ return AISDKError.hasMarker(error, marker10);
221
+ }
222
+ };
211
223
  _a10 = symbol10;
212
224
  var name10 = "AI_NoSuchModelError";
213
225
  var marker11 = `vercel.ai.error.${name10}`;
@@ -888,12 +900,16 @@ var OutputUnionToReasoningDetailsSchema = z.union([
888
900
  delta: z.object({
889
901
  reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
890
902
  })
891
- }).transform((data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)),
903
+ }).transform(
904
+ (data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)
905
+ ),
892
906
  z.object({
893
907
  message: z.object({
894
908
  reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
895
909
  })
896
- }).transform((data) => data.message.reasoning_details.filter(isDefinedOrNotNull)),
910
+ }).transform(
911
+ (data) => data.message.reasoning_details.filter(isDefinedOrNotNull)
912
+ ),
897
913
  z.object({
898
914
  text: z.string(),
899
915
  reasoning_details: z.array(ReasoningDetailsWithUnknownSchema)
@@ -1141,9 +1157,7 @@ function convertToOpenRouterChatMessages(prompt) {
1141
1157
  }
1142
1158
  }
1143
1159
  }
1144
- const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(
1145
- providerOptions
1146
- );
1160
+ const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
1147
1161
  const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
1148
1162
  messages.push({
1149
1163
  role: "assistant",
@@ -1206,7 +1220,10 @@ function getChatCompletionToolChoice(toolChoice) {
1206
1220
  }
1207
1221
  default: {
1208
1222
  toolChoice;
1209
- throw new Error(`Invalid tool choice type: ${toolChoice}`);
1223
+ throw new InvalidArgumentError({
1224
+ argument: "toolChoice",
1225
+ message: `Invalid tool choice type: ${JSON.stringify(toolChoice)}`
1226
+ });
1210
1227
  }
1211
1228
  }
1212
1229
  }
@@ -1249,57 +1266,89 @@ var OpenRouterChatCompletionBaseResponseSchema = z7.object({
1249
1266
  }).nullish()
1250
1267
  }).nullish()
1251
1268
  });
1252
- var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1253
- choices: z7.array(
1254
- z7.object({
1255
- message: z7.object({
1256
- role: z7.literal("assistant"),
1257
- content: z7.string().nullable().optional(),
1258
- reasoning: z7.string().nullable().optional(),
1259
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1260
- images: ImageResponseArraySchema.nullish(),
1261
- tool_calls: z7.array(
1262
- z7.object({
1263
- id: z7.string().optional().nullable(),
1264
- type: z7.literal("function"),
1265
- function: z7.object({
1266
- name: z7.string(),
1267
- arguments: z7.string()
1268
- })
1269
- })
1270
- ).optional(),
1271
- annotations: z7.array(
1272
- z7.object({
1273
- type: z7.enum(["url_citation"]),
1274
- url_citation: z7.object({
1275
- end_index: z7.number(),
1276
- start_index: z7.number(),
1277
- title: z7.string(),
1278
- url: z7.string(),
1279
- content: z7.string().optional()
1269
+ var OpenRouterNonStreamChatCompletionResponseSchema = z7.union([
1270
+ // Success response with choices
1271
+ OpenRouterChatCompletionBaseResponseSchema.extend({
1272
+ choices: z7.array(
1273
+ z7.object({
1274
+ message: z7.object({
1275
+ role: z7.literal("assistant"),
1276
+ content: z7.string().nullable().optional(),
1277
+ reasoning: z7.string().nullable().optional(),
1278
+ reasoning_details: ReasoningDetailArraySchema.nullish(),
1279
+ images: ImageResponseArraySchema.nullish(),
1280
+ tool_calls: z7.array(
1281
+ z7.object({
1282
+ id: z7.string().optional().nullable(),
1283
+ type: z7.literal("function"),
1284
+ function: z7.object({
1285
+ name: z7.string(),
1286
+ arguments: z7.string()
1287
+ })
1280
1288
  })
1281
- })
1282
- ).nullish()
1283
- }),
1284
- index: z7.number().nullish(),
1285
- logprobs: z7.object({
1286
- content: z7.array(
1287
- z7.object({
1288
- token: z7.string(),
1289
- logprob: z7.number(),
1290
- top_logprobs: z7.array(
1289
+ ).optional(),
1290
+ annotations: z7.array(
1291
+ z7.union([
1292
+ // URL citation from web search
1293
+ z7.object({
1294
+ type: z7.literal("url_citation"),
1295
+ url_citation: z7.object({
1296
+ end_index: z7.number(),
1297
+ start_index: z7.number(),
1298
+ title: z7.string(),
1299
+ url: z7.string(),
1300
+ content: z7.string().optional()
1301
+ })
1302
+ }),
1303
+ // File annotation from FileParserPlugin (old format)
1304
+ z7.object({
1305
+ type: z7.literal("file_annotation"),
1306
+ file_annotation: z7.object({
1307
+ file_id: z7.string(),
1308
+ quote: z7.string().optional()
1309
+ })
1310
+ }),
1311
+ // File annotation from FileParserPlugin (new format)
1291
1312
  z7.object({
1292
- token: z7.string(),
1293
- logprob: z7.number()
1313
+ type: z7.literal("file"),
1314
+ file: z7.object({
1315
+ hash: z7.string(),
1316
+ name: z7.string(),
1317
+ content: z7.array(
1318
+ z7.object({
1319
+ type: z7.string(),
1320
+ text: z7.string()
1321
+ })
1322
+ ).optional()
1323
+ })
1294
1324
  })
1295
- )
1296
- })
1297
- ).nullable()
1298
- }).nullable().optional(),
1299
- finish_reason: z7.string().optional().nullable()
1300
- })
1301
- )
1302
- });
1325
+ ])
1326
+ ).nullish()
1327
+ }),
1328
+ index: z7.number().nullish(),
1329
+ logprobs: z7.object({
1330
+ content: z7.array(
1331
+ z7.object({
1332
+ token: z7.string(),
1333
+ logprob: z7.number(),
1334
+ top_logprobs: z7.array(
1335
+ z7.object({
1336
+ token: z7.string(),
1337
+ logprob: z7.number()
1338
+ })
1339
+ )
1340
+ })
1341
+ ).nullable()
1342
+ }).nullable().optional(),
1343
+ finish_reason: z7.string().optional().nullable()
1344
+ })
1345
+ )
1346
+ }),
1347
+ // Error response (HTTP 200 with error payload)
1348
+ OpenRouterErrorResponseSchema.extend({
1349
+ user_id: z7.string().optional()
1350
+ })
1351
+ ]);
1303
1352
  var OpenRouterStreamChatCompletionChunkSchema = z7.union([
1304
1353
  OpenRouterChatCompletionBaseResponseSchema.extend({
1305
1354
  choices: z7.array(
@@ -1322,16 +1371,41 @@ var OpenRouterStreamChatCompletionChunkSchema = z7.union([
1322
1371
  })
1323
1372
  ).nullish(),
1324
1373
  annotations: z7.array(
1325
- z7.object({
1326
- type: z7.enum(["url_citation"]),
1327
- url_citation: z7.object({
1328
- end_index: z7.number(),
1329
- start_index: z7.number(),
1330
- title: z7.string(),
1331
- url: z7.string(),
1332
- content: z7.string().optional()
1374
+ z7.union([
1375
+ // URL citation from web search
1376
+ z7.object({
1377
+ type: z7.literal("url_citation"),
1378
+ url_citation: z7.object({
1379
+ end_index: z7.number(),
1380
+ start_index: z7.number(),
1381
+ title: z7.string(),
1382
+ url: z7.string(),
1383
+ content: z7.string().optional()
1384
+ })
1385
+ }),
1386
+ // File annotation from FileParserPlugin (old format)
1387
+ z7.object({
1388
+ type: z7.literal("file_annotation"),
1389
+ file_annotation: z7.object({
1390
+ file_id: z7.string(),
1391
+ quote: z7.string().optional()
1392
+ })
1393
+ }),
1394
+ // File annotation from FileParserPlugin (new format)
1395
+ z7.object({
1396
+ type: z7.literal("file"),
1397
+ file: z7.object({
1398
+ hash: z7.string(),
1399
+ name: z7.string(),
1400
+ content: z7.array(
1401
+ z7.object({
1402
+ type: z7.string(),
1403
+ text: z7.string()
1404
+ })
1405
+ ).optional()
1406
+ })
1333
1407
  })
1334
- })
1408
+ ])
1335
1409
  ).nullish()
1336
1410
  }).nullish(),
1337
1411
  logprobs: z7.object({
@@ -1456,7 +1530,7 @@ var OpenRouterChatLanguageModel = class {
1456
1530
  const providerOptions = options.providerOptions || {};
1457
1531
  const openrouterOptions = providerOptions.openrouter || {};
1458
1532
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
1459
- const { value: response, responseHeaders } = await postJsonToApi({
1533
+ const { value: responseValue, responseHeaders } = await postJsonToApi({
1460
1534
  url: this.config.url({
1461
1535
  path: "/chat/completions",
1462
1536
  modelId: this.modelId
@@ -1470,9 +1544,25 @@ var OpenRouterChatLanguageModel = class {
1470
1544
  abortSignal: options.abortSignal,
1471
1545
  fetch: this.config.fetch
1472
1546
  });
1547
+ if ("error" in responseValue) {
1548
+ throw new APICallError({
1549
+ message: responseValue.error.message,
1550
+ url: this.config.url({
1551
+ path: "/chat/completions",
1552
+ modelId: this.modelId
1553
+ }),
1554
+ requestBodyValues: args,
1555
+ statusCode: 200,
1556
+ responseHeaders,
1557
+ data: responseValue.error
1558
+ });
1559
+ }
1560
+ const response = responseValue;
1473
1561
  const choice = response.choices[0];
1474
1562
  if (!choice) {
1475
- throw new Error("No choice in response");
1563
+ throw new NoContentGeneratedError({
1564
+ message: "No choice in response"
1565
+ });
1476
1566
  }
1477
1567
  const usageInfo = response.usage ? {
1478
1568
  inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
@@ -1832,7 +1922,10 @@ var OpenRouterChatLanguageModel = class {
1832
1922
  };
1833
1923
  const toolCall2 = toolCalls[index];
1834
1924
  if (toolCall2 == null) {
1835
- throw new Error("Tool call is missing");
1925
+ throw new InvalidResponseDataError({
1926
+ data: { index, toolCallsLength: toolCalls.length },
1927
+ message: `Tool call at index ${index} is missing after creation.`
1928
+ });
1836
1929
  }
1837
1930
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1838
1931
  toolCall2.inputStarted = true;
@@ -1862,7 +1955,14 @@ var OpenRouterChatLanguageModel = class {
1862
1955
  }
1863
1956
  const toolCall = toolCalls[index];
1864
1957
  if (toolCall == null) {
1865
- throw new Error("Tool call is missing");
1958
+ throw new InvalidResponseDataError({
1959
+ data: {
1960
+ index,
1961
+ toolCallsLength: toolCalls.length,
1962
+ toolCallDelta
1963
+ },
1964
+ message: `Tool call at index ${index} is missing during merge.`
1965
+ });
1866
1966
  }
1867
1967
  if (!toolCall.inputStarted) {
1868
1968
  toolCall.inputStarted = true;
@@ -2187,11 +2287,23 @@ var OpenRouterCompletionLanguageModel = class {
2187
2287
  fetch: this.config.fetch
2188
2288
  });
2189
2289
  if ("error" in response) {
2190
- throw new Error(`${response.error.message}`);
2290
+ throw new APICallError({
2291
+ message: response.error.message,
2292
+ url: this.config.url({
2293
+ path: "/completions",
2294
+ modelId: this.modelId
2295
+ }),
2296
+ requestBodyValues: args,
2297
+ statusCode: 200,
2298
+ responseHeaders,
2299
+ data: response.error
2300
+ });
2191
2301
  }
2192
2302
  const choice = response.choices[0];
2193
2303
  if (!choice) {
2194
- throw new Error("No choice in OpenRouter completion response");
2304
+ throw new NoContentGeneratedError({
2305
+ message: "No choice in OpenRouter completion response"
2306
+ });
2195
2307
  }
2196
2308
  return {
2197
2309
  content: [