@openrouter/ai-sdk-provider 1.2.0 → 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.
@@ -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}`;
@@ -873,19 +885,40 @@ function convertUint8ArrayToBase64(array) {
873
885
 
874
886
  // src/schemas/reasoning-details.ts
875
887
  var import_v4 = require("zod/v4");
888
+
889
+ // src/utils/type-guards.ts
890
+ function isDefinedOrNotNull(value) {
891
+ return value !== null && value !== void 0;
892
+ }
893
+
894
+ // src/schemas/format.ts
895
+ var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
896
+ ReasoningFormat2["Unknown"] = "unknown";
897
+ ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
898
+ ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
899
+ ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
900
+ return ReasoningFormat2;
901
+ })(ReasoningFormat || {});
902
+
903
+ // src/schemas/reasoning-details.ts
904
+ var CommonReasoningDetailSchema = import_v4.z.object({
905
+ id: import_v4.z.string().nullish(),
906
+ format: import_v4.z.nativeEnum(ReasoningFormat).nullish(),
907
+ index: import_v4.z.number().optional()
908
+ });
876
909
  var ReasoningDetailSummarySchema = import_v4.z.object({
877
910
  type: import_v4.z.literal("reasoning.summary" /* Summary */),
878
911
  summary: import_v4.z.string()
879
- });
912
+ }).extend(CommonReasoningDetailSchema.shape);
880
913
  var ReasoningDetailEncryptedSchema = import_v4.z.object({
881
914
  type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
882
915
  data: import_v4.z.string()
883
- });
916
+ }).extend(CommonReasoningDetailSchema.shape);
884
917
  var ReasoningDetailTextSchema = import_v4.z.object({
885
918
  type: import_v4.z.literal("reasoning.text" /* Text */),
886
919
  text: import_v4.z.string().nullish(),
887
920
  signature: import_v4.z.string().nullish()
888
- });
921
+ }).extend(CommonReasoningDetailSchema.shape);
889
922
  var ReasoningDetailUnionSchema = import_v4.z.union([
890
923
  ReasoningDetailSummarySchema,
891
924
  ReasoningDetailEncryptedSchema,
@@ -896,6 +929,26 @@ var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
896
929
  import_v4.z.unknown().transform(() => null)
897
930
  ]);
898
931
  var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
932
+ var OutputUnionToReasoningDetailsSchema = import_v4.z.union([
933
+ import_v4.z.object({
934
+ delta: import_v4.z.object({
935
+ reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
936
+ })
937
+ }).transform(
938
+ (data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)
939
+ ),
940
+ import_v4.z.object({
941
+ message: import_v4.z.object({
942
+ reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
943
+ })
944
+ }).transform(
945
+ (data) => data.message.reasoning_details.filter(isDefinedOrNotNull)
946
+ ),
947
+ import_v4.z.object({
948
+ text: import_v4.z.string(),
949
+ reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
950
+ }).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
951
+ ]);
899
952
 
900
953
  // src/schemas/error-response.ts
901
954
  var import_v42 = require("zod/v4");
@@ -912,6 +965,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
912
965
  errorToMessage: (data) => data.error.message
913
966
  });
914
967
 
968
+ // src/schemas/provider-metadata.ts
969
+ var import_v43 = require("zod/v4");
970
+ var OpenRouterProviderMetadataSchema = import_v43.z.object({
971
+ provider: import_v43.z.string(),
972
+ reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
973
+ usage: import_v43.z.object({
974
+ promptTokens: import_v43.z.number(),
975
+ promptTokensDetails: import_v43.z.object({
976
+ cachedTokens: import_v43.z.number()
977
+ }).optional(),
978
+ completionTokens: import_v43.z.number(),
979
+ completionTokensDetails: import_v43.z.object({
980
+ reasoningTokens: import_v43.z.number()
981
+ }).optional(),
982
+ totalTokens: import_v43.z.number(),
983
+ cost: import_v43.z.number().optional(),
984
+ costDetails: import_v43.z.object({
985
+ upstreamInferenceCost: import_v43.z.number()
986
+ })
987
+ })
988
+ });
989
+ var OpenRouterProviderOptionsSchema = import_v43.z.object({
990
+ openrouter: import_v43.z.object({
991
+ reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional()
992
+ }).optional()
993
+ }).optional();
994
+
915
995
  // src/utils/map-finish-reason.ts
916
996
  function mapOpenRouterFinishReason(finishReason) {
917
997
  switch (finishReason) {
@@ -979,7 +1059,7 @@ function getCacheControl(providerMetadata) {
979
1059
  return (_c = (_b = (_a15 = openrouter == null ? void 0 : openrouter.cacheControl) != null ? _a15 : openrouter == null ? void 0 : openrouter.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
980
1060
  }
981
1061
  function convertToOpenRouterChatMessages(prompt) {
982
- var _a15, _b, _c;
1062
+ var _a15, _b, _c, _d, _e;
983
1063
  const messages = [];
984
1064
  for (const { role, content, providerOptions } of prompt) {
985
1065
  switch (role) {
@@ -1010,7 +1090,7 @@ function convertToOpenRouterChatMessages(prompt) {
1010
1090
  const messageCacheControl = getCacheControl(providerOptions);
1011
1091
  const contentParts = content.map(
1012
1092
  (part) => {
1013
- var _a16, _b2, _c2, _d, _e, _f;
1093
+ var _a16, _b2, _c2, _d2, _e2, _f;
1014
1094
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
1015
1095
  switch (part.type) {
1016
1096
  case "text":
@@ -1036,7 +1116,7 @@ function convertToOpenRouterChatMessages(prompt) {
1036
1116
  };
1037
1117
  }
1038
1118
  const fileName = String(
1039
- (_f = (_e = (_d = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename) != null ? _e : part.filename) != null ? _f : ""
1119
+ (_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
1040
1120
  );
1041
1121
  const fileData = getFileUrl({
1042
1122
  part,
@@ -1082,7 +1162,6 @@ function convertToOpenRouterChatMessages(prompt) {
1082
1162
  case "assistant": {
1083
1163
  let text = "";
1084
1164
  let reasoning = "";
1085
- const reasoningDetails = [];
1086
1165
  const toolCalls = [];
1087
1166
  for (const part of content) {
1088
1167
  switch (part.type) {
@@ -1103,10 +1182,6 @@ function convertToOpenRouterChatMessages(prompt) {
1103
1182
  }
1104
1183
  case "reasoning": {
1105
1184
  reasoning += part.text;
1106
- reasoningDetails.push({
1107
- type: "reasoning.text" /* Text */,
1108
- text: part.text
1109
- });
1110
1185
  break;
1111
1186
  }
1112
1187
  case "file":
@@ -1116,12 +1191,15 @@ function convertToOpenRouterChatMessages(prompt) {
1116
1191
  }
1117
1192
  }
1118
1193
  }
1194
+ const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
1195
+ const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
1119
1196
  messages.push({
1120
1197
  role: "assistant",
1121
1198
  content: text,
1122
1199
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
1123
1200
  reasoning: reasoning || void 0,
1124
- reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : void 0,
1201
+ // Only include reasoning_details if we have the preserved original version
1202
+ reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
1125
1203
  cache_control: getCacheControl(providerOptions)
1126
1204
  });
1127
1205
  break;
@@ -1133,7 +1211,7 @@ function convertToOpenRouterChatMessages(prompt) {
1133
1211
  role: "tool",
1134
1212
  tool_call_id: toolResponse.toolCallId,
1135
1213
  content: content2,
1136
- cache_control: (_c = getCacheControl(providerOptions)) != null ? _c : getCacheControl(toolResponse.providerOptions)
1214
+ cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
1137
1215
  });
1138
1216
  }
1139
1217
  break;
@@ -1150,15 +1228,15 @@ function getToolResultContent(input) {
1150
1228
  }
1151
1229
 
1152
1230
  // src/chat/get-tool-choice.ts
1153
- var import_v43 = require("zod/v4");
1154
- var ChatCompletionToolChoiceSchema = import_v43.z.union([
1155
- import_v43.z.literal("auto"),
1156
- import_v43.z.literal("none"),
1157
- import_v43.z.literal("required"),
1158
- import_v43.z.object({
1159
- type: import_v43.z.literal("function"),
1160
- function: import_v43.z.object({
1161
- name: import_v43.z.string()
1231
+ var import_v44 = require("zod/v4");
1232
+ var ChatCompletionToolChoiceSchema = import_v44.z.union([
1233
+ import_v44.z.literal("auto"),
1234
+ import_v44.z.literal("none"),
1235
+ import_v44.z.literal("required"),
1236
+ import_v44.z.object({
1237
+ type: import_v44.z.literal("function"),
1238
+ function: import_v44.z.object({
1239
+ name: import_v44.z.string()
1162
1240
  })
1163
1241
  })
1164
1242
  ]);
@@ -1176,150 +1254,210 @@ function getChatCompletionToolChoice(toolChoice) {
1176
1254
  }
1177
1255
  default: {
1178
1256
  toolChoice;
1179
- 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
+ });
1180
1261
  }
1181
1262
  }
1182
1263
  }
1183
1264
 
1184
1265
  // src/chat/schemas.ts
1185
- var import_v45 = require("zod/v4");
1266
+ var import_v46 = require("zod/v4");
1186
1267
 
1187
1268
  // src/schemas/image.ts
1188
- var import_v44 = require("zod/v4");
1189
- var ImageResponseSchema = import_v44.z.object({
1190
- type: import_v44.z.literal("image_url"),
1191
- image_url: import_v44.z.object({
1192
- url: import_v44.z.string()
1269
+ var import_v45 = require("zod/v4");
1270
+ var ImageResponseSchema = import_v45.z.object({
1271
+ type: import_v45.z.literal("image_url"),
1272
+ image_url: import_v45.z.object({
1273
+ url: import_v45.z.string()
1193
1274
  })
1194
1275
  });
1195
- var ImageResponseWithUnknownSchema = import_v44.z.union([
1276
+ var ImageResponseWithUnknownSchema = import_v45.z.union([
1196
1277
  ImageResponseSchema,
1197
- import_v44.z.unknown().transform(() => null)
1278
+ import_v45.z.unknown().transform(() => null)
1198
1279
  ]);
1199
- var ImageResponseArraySchema = import_v44.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1280
+ var ImageResponseArraySchema = import_v45.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1200
1281
 
1201
1282
  // src/chat/schemas.ts
1202
- var OpenRouterChatCompletionBaseResponseSchema = import_v45.z.object({
1203
- id: import_v45.z.string().optional(),
1204
- model: import_v45.z.string().optional(),
1205
- provider: import_v45.z.string().optional(),
1206
- usage: import_v45.z.object({
1207
- prompt_tokens: import_v45.z.number(),
1208
- prompt_tokens_details: import_v45.z.object({
1209
- cached_tokens: import_v45.z.number()
1283
+ var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
1284
+ id: import_v46.z.string().optional(),
1285
+ model: import_v46.z.string().optional(),
1286
+ provider: import_v46.z.string().optional(),
1287
+ usage: import_v46.z.object({
1288
+ prompt_tokens: import_v46.z.number(),
1289
+ prompt_tokens_details: import_v46.z.object({
1290
+ cached_tokens: import_v46.z.number()
1210
1291
  }).nullish(),
1211
- completion_tokens: import_v45.z.number(),
1212
- completion_tokens_details: import_v45.z.object({
1213
- reasoning_tokens: import_v45.z.number()
1292
+ completion_tokens: import_v46.z.number(),
1293
+ completion_tokens_details: import_v46.z.object({
1294
+ reasoning_tokens: import_v46.z.number()
1214
1295
  }).nullish(),
1215
- total_tokens: import_v45.z.number(),
1216
- cost: import_v45.z.number().optional(),
1217
- cost_details: import_v45.z.object({
1218
- upstream_inference_cost: import_v45.z.number().nullish()
1296
+ total_tokens: import_v46.z.number(),
1297
+ cost: import_v46.z.number().optional(),
1298
+ cost_details: import_v46.z.object({
1299
+ upstream_inference_cost: import_v46.z.number().nullish()
1219
1300
  }).nullish()
1220
1301
  }).nullish()
1221
1302
  });
1222
- var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1223
- choices: import_v45.z.array(
1224
- import_v45.z.object({
1225
- message: import_v45.z.object({
1226
- role: import_v45.z.literal("assistant"),
1227
- content: import_v45.z.string().nullable().optional(),
1228
- reasoning: import_v45.z.string().nullable().optional(),
1229
- reasoning_details: ReasoningDetailArraySchema.nullish(),
1230
- images: ImageResponseArraySchema.nullish(),
1231
- tool_calls: import_v45.z.array(
1232
- import_v45.z.object({
1233
- id: import_v45.z.string().optional().nullable(),
1234
- type: import_v45.z.literal("function"),
1235
- function: import_v45.z.object({
1236
- name: import_v45.z.string(),
1237
- arguments: import_v45.z.string()
1238
- })
1239
- })
1240
- ).optional(),
1241
- annotations: import_v45.z.array(
1242
- import_v45.z.object({
1243
- type: import_v45.z.enum(["url_citation"]),
1244
- url_citation: import_v45.z.object({
1245
- end_index: import_v45.z.number(),
1246
- start_index: import_v45.z.number(),
1247
- title: import_v45.z.string(),
1248
- url: import_v45.z.string(),
1249
- content: import_v45.z.string().optional()
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
+ })
1250
1322
  })
1251
- })
1252
- ).nullish()
1253
- }),
1254
- index: import_v45.z.number().nullish(),
1255
- logprobs: import_v45.z.object({
1256
- content: import_v45.z.array(
1257
- import_v45.z.object({
1258
- token: import_v45.z.string(),
1259
- logprob: import_v45.z.number(),
1260
- top_logprobs: import_v45.z.array(
1261
- import_v45.z.object({
1262
- token: import_v45.z.string(),
1263
- logprob: import_v45.z.number()
1323
+ ).optional(),
1324
+ annotations: import_v46.z.array(
1325
+ import_v46.z.union([
1326
+ // URL citation from web search
1327
+ import_v46.z.object({
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
+ })
1336
+ }),
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
+ })
1344
+ }),
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
+ })
1356
+ ).optional()
1357
+ })
1264
1358
  })
1265
- )
1266
- })
1267
- ).nullable()
1268
- }).nullable().optional(),
1269
- finish_reason: import_v45.z.string().optional().nullable()
1270
- })
1271
- )
1272
- });
1273
- var OpenRouterStreamChatCompletionChunkSchema = import_v45.z.union([
1359
+ ])
1360
+ ).nullish()
1361
+ }),
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
+ })
1373
+ )
1374
+ })
1375
+ ).nullable()
1376
+ }).nullable().optional(),
1377
+ finish_reason: import_v46.z.string().optional().nullable()
1378
+ })
1379
+ )
1380
+ }),
1381
+ // Error response (HTTP 200 with error payload)
1382
+ OpenRouterErrorResponseSchema.extend({
1383
+ user_id: import_v46.z.string().optional()
1384
+ })
1385
+ ]);
1386
+ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
1274
1387
  OpenRouterChatCompletionBaseResponseSchema.extend({
1275
- choices: import_v45.z.array(
1276
- import_v45.z.object({
1277
- delta: import_v45.z.object({
1278
- role: import_v45.z.enum(["assistant"]).optional(),
1279
- content: import_v45.z.string().nullish(),
1280
- reasoning: import_v45.z.string().nullish().optional(),
1388
+ choices: import_v46.z.array(
1389
+ import_v46.z.object({
1390
+ delta: import_v46.z.object({
1391
+ role: import_v46.z.enum(["assistant"]).optional(),
1392
+ content: import_v46.z.string().nullish(),
1393
+ reasoning: import_v46.z.string().nullish().optional(),
1281
1394
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1282
1395
  images: ImageResponseArraySchema.nullish(),
1283
- tool_calls: import_v45.z.array(
1284
- import_v45.z.object({
1285
- index: import_v45.z.number().nullish(),
1286
- id: import_v45.z.string().nullish(),
1287
- type: import_v45.z.literal("function").optional(),
1288
- function: import_v45.z.object({
1289
- name: import_v45.z.string().nullish(),
1290
- arguments: import_v45.z.string().nullish()
1396
+ tool_calls: import_v46.z.array(
1397
+ import_v46.z.object({
1398
+ index: import_v46.z.number().nullish(),
1399
+ id: import_v46.z.string().nullish(),
1400
+ type: import_v46.z.literal("function").optional(),
1401
+ function: import_v46.z.object({
1402
+ name: import_v46.z.string().nullish(),
1403
+ arguments: import_v46.z.string().nullish()
1291
1404
  })
1292
1405
  })
1293
1406
  ).nullish(),
1294
- annotations: import_v45.z.array(
1295
- import_v45.z.object({
1296
- type: import_v45.z.enum(["url_citation"]),
1297
- url_citation: import_v45.z.object({
1298
- end_index: import_v45.z.number(),
1299
- start_index: import_v45.z.number(),
1300
- title: import_v45.z.string(),
1301
- url: import_v45.z.string(),
1302
- content: import_v45.z.string().optional()
1407
+ annotations: import_v46.z.array(
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
+ })
1419
+ }),
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
+ })
1427
+ }),
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
+ })
1439
+ ).optional()
1440
+ })
1303
1441
  })
1304
- })
1442
+ ])
1305
1443
  ).nullish()
1306
1444
  }).nullish(),
1307
- logprobs: import_v45.z.object({
1308
- content: import_v45.z.array(
1309
- import_v45.z.object({
1310
- token: import_v45.z.string(),
1311
- logprob: import_v45.z.number(),
1312
- top_logprobs: import_v45.z.array(
1313
- import_v45.z.object({
1314
- token: import_v45.z.string(),
1315
- logprob: import_v45.z.number()
1445
+ logprobs: import_v46.z.object({
1446
+ content: import_v46.z.array(
1447
+ import_v46.z.object({
1448
+ token: import_v46.z.string(),
1449
+ logprob: import_v46.z.number(),
1450
+ top_logprobs: import_v46.z.array(
1451
+ import_v46.z.object({
1452
+ token: import_v46.z.string(),
1453
+ logprob: import_v46.z.number()
1316
1454
  })
1317
1455
  )
1318
1456
  })
1319
1457
  ).nullable()
1320
1458
  }).nullish(),
1321
- finish_reason: import_v45.z.string().nullable().optional(),
1322
- index: import_v45.z.number().nullish()
1459
+ finish_reason: import_v46.z.string().nullable().optional(),
1460
+ index: import_v46.z.number().nullish()
1323
1461
  })
1324
1462
  )
1325
1463
  }),
@@ -1422,11 +1560,11 @@ var OpenRouterChatLanguageModel = class {
1422
1560
  return baseArgs;
1423
1561
  }
1424
1562
  async doGenerate(options) {
1425
- var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
1563
+ var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
1426
1564
  const providerOptions = options.providerOptions || {};
1427
1565
  const openrouterOptions = providerOptions.openrouter || {};
1428
1566
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
1429
- const { value: response, responseHeaders } = await postJsonToApi({
1567
+ const { value: responseValue, responseHeaders } = await postJsonToApi({
1430
1568
  url: this.config.url({
1431
1569
  path: "/chat/completions",
1432
1570
  modelId: this.modelId
@@ -1440,9 +1578,25 @@ var OpenRouterChatLanguageModel = class {
1440
1578
  abortSignal: options.abortSignal,
1441
1579
  fetch: this.config.fetch
1442
1580
  });
1581
+ if ("error" in responseValue) {
1582
+ throw new APICallError({
1583
+ message: responseValue.error.message,
1584
+ url: this.config.url({
1585
+ path: "/chat/completions",
1586
+ modelId: this.modelId
1587
+ }),
1588
+ requestBodyValues: args,
1589
+ statusCode: 200,
1590
+ responseHeaders,
1591
+ data: responseValue.error
1592
+ });
1593
+ }
1594
+ const response = responseValue;
1443
1595
  const choice = response.choices[0];
1444
1596
  if (!choice) {
1445
- throw new Error("No choice in response");
1597
+ throw new NoContentGeneratedError({
1598
+ message: "No choice in response"
1599
+ });
1446
1600
  }
1447
1601
  const usageInfo = response.usage ? {
1448
1602
  inputTokens: (_a15 = response.usage.prompt_tokens) != null ? _a15 : 0,
@@ -1549,24 +1703,25 @@ var OpenRouterChatLanguageModel = class {
1549
1703
  usage: usageInfo,
1550
1704
  warnings: [],
1551
1705
  providerMetadata: {
1552
- openrouter: {
1706
+ openrouter: OpenRouterProviderMetadataSchema.parse({
1553
1707
  provider: (_k = response.provider) != null ? _k : "",
1708
+ reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
1554
1709
  usage: {
1555
- promptTokens: (_l = usageInfo.inputTokens) != null ? _l : 0,
1556
- completionTokens: (_m = usageInfo.outputTokens) != null ? _m : 0,
1557
- totalTokens: (_n = usageInfo.totalTokens) != null ? _n : 0,
1558
- cost: (_o = response.usage) == null ? void 0 : _o.cost,
1710
+ promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
1711
+ completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
1712
+ totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
1713
+ cost: (_p = response.usage) == null ? void 0 : _p.cost,
1559
1714
  promptTokensDetails: {
1560
- cachedTokens: (_r = (_q = (_p = response.usage) == null ? void 0 : _p.prompt_tokens_details) == null ? void 0 : _q.cached_tokens) != null ? _r : 0
1715
+ cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
1561
1716
  },
1562
1717
  completionTokensDetails: {
1563
- reasoningTokens: (_u = (_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? _u : 0
1718
+ reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
1564
1719
  },
1565
1720
  costDetails: {
1566
- upstreamInferenceCost: (_x = (_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? _x : 0
1721
+ upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
1567
1722
  }
1568
1723
  }
1569
- }
1724
+ })
1570
1725
  },
1571
1726
  request: { body: args },
1572
1727
  response: {
@@ -1611,6 +1766,7 @@ var OpenRouterChatLanguageModel = class {
1611
1766
  cachedInputTokens: Number.NaN
1612
1767
  };
1613
1768
  const openrouterUsage = {};
1769
+ const accumulatedReasoningDetails = [];
1614
1770
  let textStarted = false;
1615
1771
  let reasoningStarted = false;
1616
1772
  let textId;
@@ -1696,6 +1852,7 @@ var OpenRouterChatLanguageModel = class {
1696
1852
  });
1697
1853
  };
1698
1854
  if (delta.reasoning_details && delta.reasoning_details.length > 0) {
1855
+ accumulatedReasoningDetails.push(...delta.reasoning_details);
1699
1856
  for (const detail of delta.reasoning_details) {
1700
1857
  switch (detail.type) {
1701
1858
  case "reasoning.text" /* Text */: {
@@ -1799,7 +1956,10 @@ var OpenRouterChatLanguageModel = class {
1799
1956
  };
1800
1957
  const toolCall2 = toolCalls[index];
1801
1958
  if (toolCall2 == null) {
1802
- throw new Error("Tool call is missing");
1959
+ throw new InvalidResponseDataError({
1960
+ data: { index, toolCallsLength: toolCalls.length },
1961
+ message: `Tool call at index ${index} is missing after creation.`
1962
+ });
1803
1963
  }
1804
1964
  if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
1805
1965
  toolCall2.inputStarted = true;
@@ -1829,7 +1989,14 @@ var OpenRouterChatLanguageModel = class {
1829
1989
  }
1830
1990
  const toolCall = toolCalls[index];
1831
1991
  if (toolCall == null) {
1832
- throw new Error("Tool call is missing");
1992
+ throw new InvalidResponseDataError({
1993
+ data: {
1994
+ index,
1995
+ toolCallsLength: toolCalls.length,
1996
+ toolCallDelta
1997
+ },
1998
+ message: `Tool call at index ${index} is missing during merge.`
1999
+ });
1833
2000
  }
1834
2001
  if (!toolCall.inputStarted) {
1835
2002
  toolCall.inputStarted = true;
@@ -1902,6 +2069,9 @@ var OpenRouterChatLanguageModel = class {
1902
2069
  if (provider !== void 0) {
1903
2070
  openrouterMetadata.provider = provider;
1904
2071
  }
2072
+ if (accumulatedReasoningDetails.length > 0) {
2073
+ openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
2074
+ }
1905
2075
  controller.enqueue({
1906
2076
  type: "finish",
1907
2077
  finishReason,
@@ -2024,36 +2194,36 @@ ${assistantMessage}
2024
2194
  }
2025
2195
 
2026
2196
  // src/completion/schemas.ts
2027
- var import_v46 = require("zod/v4");
2028
- var OpenRouterCompletionChunkSchema = import_v46.z.union([
2029
- import_v46.z.object({
2030
- id: import_v46.z.string().optional(),
2031
- model: import_v46.z.string().optional(),
2032
- choices: import_v46.z.array(
2033
- import_v46.z.object({
2034
- text: import_v46.z.string(),
2035
- reasoning: import_v46.z.string().nullish().optional(),
2197
+ var import_v47 = require("zod/v4");
2198
+ var OpenRouterCompletionChunkSchema = import_v47.z.union([
2199
+ import_v47.z.object({
2200
+ id: import_v47.z.string().optional(),
2201
+ model: import_v47.z.string().optional(),
2202
+ choices: import_v47.z.array(
2203
+ import_v47.z.object({
2204
+ text: import_v47.z.string(),
2205
+ reasoning: import_v47.z.string().nullish().optional(),
2036
2206
  reasoning_details: ReasoningDetailArraySchema.nullish(),
2037
- finish_reason: import_v46.z.string().nullish(),
2038
- index: import_v46.z.number().nullish(),
2039
- logprobs: import_v46.z.object({
2040
- tokens: import_v46.z.array(import_v46.z.string()),
2041
- token_logprobs: import_v46.z.array(import_v46.z.number()),
2042
- top_logprobs: import_v46.z.array(import_v46.z.record(import_v46.z.string(), import_v46.z.number())).nullable()
2207
+ finish_reason: import_v47.z.string().nullish(),
2208
+ index: import_v47.z.number().nullish(),
2209
+ logprobs: import_v47.z.object({
2210
+ tokens: import_v47.z.array(import_v47.z.string()),
2211
+ token_logprobs: import_v47.z.array(import_v47.z.number()),
2212
+ top_logprobs: import_v47.z.array(import_v47.z.record(import_v47.z.string(), import_v47.z.number())).nullable()
2043
2213
  }).nullable().optional()
2044
2214
  })
2045
2215
  ),
2046
- usage: import_v46.z.object({
2047
- prompt_tokens: import_v46.z.number(),
2048
- prompt_tokens_details: import_v46.z.object({
2049
- cached_tokens: import_v46.z.number()
2216
+ usage: import_v47.z.object({
2217
+ prompt_tokens: import_v47.z.number(),
2218
+ prompt_tokens_details: import_v47.z.object({
2219
+ cached_tokens: import_v47.z.number()
2050
2220
  }).nullish(),
2051
- completion_tokens: import_v46.z.number(),
2052
- completion_tokens_details: import_v46.z.object({
2053
- reasoning_tokens: import_v46.z.number()
2221
+ completion_tokens: import_v47.z.number(),
2222
+ completion_tokens_details: import_v47.z.object({
2223
+ reasoning_tokens: import_v47.z.number()
2054
2224
  }).nullish(),
2055
- total_tokens: import_v46.z.number(),
2056
- cost: import_v46.z.number().optional()
2225
+ total_tokens: import_v47.z.number(),
2226
+ cost: import_v47.z.number().optional()
2057
2227
  }).nullish()
2058
2228
  }),
2059
2229
  OpenRouterErrorResponseSchema
@@ -2151,11 +2321,23 @@ var OpenRouterCompletionLanguageModel = class {
2151
2321
  fetch: this.config.fetch
2152
2322
  });
2153
2323
  if ("error" in response) {
2154
- throw new Error(`${response.error.message}`);
2324
+ throw new APICallError({
2325
+ message: response.error.message,
2326
+ url: this.config.url({
2327
+ path: "/completions",
2328
+ modelId: this.modelId
2329
+ }),
2330
+ requestBodyValues: args,
2331
+ statusCode: 200,
2332
+ responseHeaders,
2333
+ data: response.error
2334
+ });
2155
2335
  }
2156
2336
  const choice = response.choices[0];
2157
2337
  if (!choice) {
2158
- throw new Error("No choice in OpenRouter completion response");
2338
+ throw new NoContentGeneratedError({
2339
+ message: "No choice in OpenRouter completion response"
2340
+ });
2159
2341
  }
2160
2342
  return {
2161
2343
  content: [