@openrouter/ai-sdk-provider 1.2.0 → 1.2.1

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.js CHANGED
@@ -919,19 +919,40 @@ function withoutTrailingSlash(url) {
919
919
 
920
920
  // src/schemas/reasoning-details.ts
921
921
  var import_v4 = require("zod/v4");
922
+
923
+ // src/utils/type-guards.ts
924
+ function isDefinedOrNotNull(value) {
925
+ return value !== null && value !== void 0;
926
+ }
927
+
928
+ // src/schemas/format.ts
929
+ var ReasoningFormat = /* @__PURE__ */ ((ReasoningFormat2) => {
930
+ ReasoningFormat2["Unknown"] = "unknown";
931
+ ReasoningFormat2["OpenAIResponsesV1"] = "openai-responses-v1";
932
+ ReasoningFormat2["XAIResponsesV1"] = "xai-responses-v1";
933
+ ReasoningFormat2["AnthropicClaudeV1"] = "anthropic-claude-v1";
934
+ return ReasoningFormat2;
935
+ })(ReasoningFormat || {});
936
+
937
+ // src/schemas/reasoning-details.ts
938
+ var CommonReasoningDetailSchema = import_v4.z.object({
939
+ id: import_v4.z.string().nullish(),
940
+ format: import_v4.z.nativeEnum(ReasoningFormat).nullish(),
941
+ index: import_v4.z.number().optional()
942
+ });
922
943
  var ReasoningDetailSummarySchema = import_v4.z.object({
923
944
  type: import_v4.z.literal("reasoning.summary" /* Summary */),
924
945
  summary: import_v4.z.string()
925
- });
946
+ }).extend(CommonReasoningDetailSchema.shape);
926
947
  var ReasoningDetailEncryptedSchema = import_v4.z.object({
927
948
  type: import_v4.z.literal("reasoning.encrypted" /* Encrypted */),
928
949
  data: import_v4.z.string()
929
- });
950
+ }).extend(CommonReasoningDetailSchema.shape);
930
951
  var ReasoningDetailTextSchema = import_v4.z.object({
931
952
  type: import_v4.z.literal("reasoning.text" /* Text */),
932
953
  text: import_v4.z.string().nullish(),
933
954
  signature: import_v4.z.string().nullish()
934
- });
955
+ }).extend(CommonReasoningDetailSchema.shape);
935
956
  var ReasoningDetailUnionSchema = import_v4.z.union([
936
957
  ReasoningDetailSummarySchema,
937
958
  ReasoningDetailEncryptedSchema,
@@ -942,6 +963,22 @@ var ReasoningDetailsWithUnknownSchema = import_v4.z.union([
942
963
  import_v4.z.unknown().transform(() => null)
943
964
  ]);
944
965
  var ReasoningDetailArraySchema = import_v4.z.array(ReasoningDetailsWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
966
+ var OutputUnionToReasoningDetailsSchema = import_v4.z.union([
967
+ import_v4.z.object({
968
+ delta: import_v4.z.object({
969
+ reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
970
+ })
971
+ }).transform((data) => data.delta.reasoning_details.filter(isDefinedOrNotNull)),
972
+ import_v4.z.object({
973
+ message: import_v4.z.object({
974
+ reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
975
+ })
976
+ }).transform((data) => data.message.reasoning_details.filter(isDefinedOrNotNull)),
977
+ import_v4.z.object({
978
+ text: import_v4.z.string(),
979
+ reasoning_details: import_v4.z.array(ReasoningDetailsWithUnknownSchema)
980
+ }).transform((data) => data.reasoning_details.filter(isDefinedOrNotNull))
981
+ ]);
945
982
 
946
983
  // src/schemas/error-response.ts
947
984
  var import_v42 = require("zod/v4");
@@ -958,6 +995,33 @@ var openrouterFailedResponseHandler = createJsonErrorResponseHandler({
958
995
  errorToMessage: (data) => data.error.message
959
996
  });
960
997
 
998
+ // src/schemas/provider-metadata.ts
999
+ var import_v43 = require("zod/v4");
1000
+ var OpenRouterProviderMetadataSchema = import_v43.z.object({
1001
+ provider: import_v43.z.string(),
1002
+ reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional(),
1003
+ usage: import_v43.z.object({
1004
+ promptTokens: import_v43.z.number(),
1005
+ promptTokensDetails: import_v43.z.object({
1006
+ cachedTokens: import_v43.z.number()
1007
+ }).optional(),
1008
+ completionTokens: import_v43.z.number(),
1009
+ completionTokensDetails: import_v43.z.object({
1010
+ reasoningTokens: import_v43.z.number()
1011
+ }).optional(),
1012
+ totalTokens: import_v43.z.number(),
1013
+ cost: import_v43.z.number().optional(),
1014
+ costDetails: import_v43.z.object({
1015
+ upstreamInferenceCost: import_v43.z.number()
1016
+ })
1017
+ })
1018
+ });
1019
+ var OpenRouterProviderOptionsSchema = import_v43.z.object({
1020
+ openrouter: import_v43.z.object({
1021
+ reasoning_details: import_v43.z.array(ReasoningDetailUnionSchema).optional()
1022
+ }).optional()
1023
+ }).optional();
1024
+
961
1025
  // src/utils/map-finish-reason.ts
962
1026
  function mapOpenRouterFinishReason(finishReason) {
963
1027
  switch (finishReason) {
@@ -1025,7 +1089,7 @@ function getCacheControl(providerMetadata) {
1025
1089
  return (_c = (_b = (_a15 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a15 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
1026
1090
  }
1027
1091
  function convertToOpenRouterChatMessages(prompt) {
1028
- var _a15, _b, _c;
1092
+ var _a15, _b, _c, _d, _e;
1029
1093
  const messages = [];
1030
1094
  for (const { role, content, providerOptions } of prompt) {
1031
1095
  switch (role) {
@@ -1056,7 +1120,7 @@ function convertToOpenRouterChatMessages(prompt) {
1056
1120
  const messageCacheControl = getCacheControl(providerOptions);
1057
1121
  const contentParts = content.map(
1058
1122
  (part) => {
1059
- var _a16, _b2, _c2, _d, _e, _f;
1123
+ var _a16, _b2, _c2, _d2, _e2, _f;
1060
1124
  const cacheControl = (_a16 = getCacheControl(part.providerOptions)) != null ? _a16 : messageCacheControl;
1061
1125
  switch (part.type) {
1062
1126
  case "text":
@@ -1082,7 +1146,7 @@ function convertToOpenRouterChatMessages(prompt) {
1082
1146
  };
1083
1147
  }
1084
1148
  const fileName = String(
1085
- (_f = (_e = (_d = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d.filename) != null ? _e : part.filename) != null ? _f : ""
1149
+ (_f = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f : ""
1086
1150
  );
1087
1151
  const fileData = getFileUrl({
1088
1152
  part,
@@ -1128,7 +1192,6 @@ function convertToOpenRouterChatMessages(prompt) {
1128
1192
  case "assistant": {
1129
1193
  let text = "";
1130
1194
  let reasoning = "";
1131
- const reasoningDetails = [];
1132
1195
  const toolCalls = [];
1133
1196
  for (const part of content) {
1134
1197
  switch (part.type) {
@@ -1149,10 +1212,6 @@ function convertToOpenRouterChatMessages(prompt) {
1149
1212
  }
1150
1213
  case "reasoning": {
1151
1214
  reasoning += part.text;
1152
- reasoningDetails.push({
1153
- type: "reasoning.text" /* Text */,
1154
- text: part.text
1155
- });
1156
1215
  break;
1157
1216
  }
1158
1217
  case "file":
@@ -1162,12 +1221,17 @@ function convertToOpenRouterChatMessages(prompt) {
1162
1221
  }
1163
1222
  }
1164
1223
  }
1224
+ const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(
1225
+ providerOptions
1226
+ );
1227
+ const preservedReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
1165
1228
  messages.push({
1166
1229
  role: "assistant",
1167
1230
  content: text,
1168
1231
  tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
1169
1232
  reasoning: reasoning || void 0,
1170
- reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : void 0,
1233
+ // Only include reasoning_details if we have the preserved original version
1234
+ reasoning_details: preservedReasoningDetails && Array.isArray(preservedReasoningDetails) && preservedReasoningDetails.length > 0 ? preservedReasoningDetails : void 0,
1171
1235
  cache_control: getCacheControl(providerOptions)
1172
1236
  });
1173
1237
  break;
@@ -1179,7 +1243,7 @@ function convertToOpenRouterChatMessages(prompt) {
1179
1243
  role: "tool",
1180
1244
  tool_call_id: toolResponse.toolCallId,
1181
1245
  content: content2,
1182
- cache_control: (_c = getCacheControl(providerOptions)) != null ? _c : getCacheControl(toolResponse.providerOptions)
1246
+ cache_control: (_e = getCacheControl(providerOptions)) != null ? _e : getCacheControl(toolResponse.providerOptions)
1183
1247
  });
1184
1248
  }
1185
1249
  break;
@@ -1196,15 +1260,15 @@ function getToolResultContent(input) {
1196
1260
  }
1197
1261
 
1198
1262
  // src/chat/get-tool-choice.ts
1199
- var import_v43 = require("zod/v4");
1200
- var ChatCompletionToolChoiceSchema = import_v43.z.union([
1201
- import_v43.z.literal("auto"),
1202
- import_v43.z.literal("none"),
1203
- import_v43.z.literal("required"),
1204
- import_v43.z.object({
1205
- type: import_v43.z.literal("function"),
1206
- function: import_v43.z.object({
1207
- name: import_v43.z.string()
1263
+ var import_v44 = require("zod/v4");
1264
+ var ChatCompletionToolChoiceSchema = import_v44.z.union([
1265
+ import_v44.z.literal("auto"),
1266
+ import_v44.z.literal("none"),
1267
+ import_v44.z.literal("required"),
1268
+ import_v44.z.object({
1269
+ type: import_v44.z.literal("function"),
1270
+ function: import_v44.z.object({
1271
+ name: import_v44.z.string()
1208
1272
  })
1209
1273
  })
1210
1274
  ]);
@@ -1228,144 +1292,144 @@ function getChatCompletionToolChoice(toolChoice) {
1228
1292
  }
1229
1293
 
1230
1294
  // src/chat/schemas.ts
1231
- var import_v45 = require("zod/v4");
1295
+ var import_v46 = require("zod/v4");
1232
1296
 
1233
1297
  // src/schemas/image.ts
1234
- var import_v44 = require("zod/v4");
1235
- var ImageResponseSchema = import_v44.z.object({
1236
- type: import_v44.z.literal("image_url"),
1237
- image_url: import_v44.z.object({
1238
- url: import_v44.z.string()
1298
+ var import_v45 = require("zod/v4");
1299
+ var ImageResponseSchema = import_v45.z.object({
1300
+ type: import_v45.z.literal("image_url"),
1301
+ image_url: import_v45.z.object({
1302
+ url: import_v45.z.string()
1239
1303
  })
1240
1304
  });
1241
- var ImageResponseWithUnknownSchema = import_v44.z.union([
1305
+ var ImageResponseWithUnknownSchema = import_v45.z.union([
1242
1306
  ImageResponseSchema,
1243
- import_v44.z.unknown().transform(() => null)
1307
+ import_v45.z.unknown().transform(() => null)
1244
1308
  ]);
1245
- var ImageResponseArraySchema = import_v44.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1309
+ var ImageResponseArraySchema = import_v45.z.array(ImageResponseWithUnknownSchema).transform((d) => d.filter((d2) => !!d2));
1246
1310
 
1247
1311
  // src/chat/schemas.ts
1248
- var OpenRouterChatCompletionBaseResponseSchema = import_v45.z.object({
1249
- id: import_v45.z.string().optional(),
1250
- model: import_v45.z.string().optional(),
1251
- provider: import_v45.z.string().optional(),
1252
- usage: import_v45.z.object({
1253
- prompt_tokens: import_v45.z.number(),
1254
- prompt_tokens_details: import_v45.z.object({
1255
- cached_tokens: import_v45.z.number()
1312
+ var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
1313
+ id: import_v46.z.string().optional(),
1314
+ model: import_v46.z.string().optional(),
1315
+ provider: import_v46.z.string().optional(),
1316
+ usage: import_v46.z.object({
1317
+ prompt_tokens: import_v46.z.number(),
1318
+ prompt_tokens_details: import_v46.z.object({
1319
+ cached_tokens: import_v46.z.number()
1256
1320
  }).nullish(),
1257
- completion_tokens: import_v45.z.number(),
1258
- completion_tokens_details: import_v45.z.object({
1259
- reasoning_tokens: import_v45.z.number()
1321
+ completion_tokens: import_v46.z.number(),
1322
+ completion_tokens_details: import_v46.z.object({
1323
+ reasoning_tokens: import_v46.z.number()
1260
1324
  }).nullish(),
1261
- total_tokens: import_v45.z.number(),
1262
- cost: import_v45.z.number().optional(),
1263
- cost_details: import_v45.z.object({
1264
- upstream_inference_cost: import_v45.z.number().nullish()
1325
+ total_tokens: import_v46.z.number(),
1326
+ cost: import_v46.z.number().optional(),
1327
+ cost_details: import_v46.z.object({
1328
+ upstream_inference_cost: import_v46.z.number().nullish()
1265
1329
  }).nullish()
1266
1330
  }).nullish()
1267
1331
  });
1268
1332
  var OpenRouterNonStreamChatCompletionResponseSchema = OpenRouterChatCompletionBaseResponseSchema.extend({
1269
- choices: import_v45.z.array(
1270
- import_v45.z.object({
1271
- message: import_v45.z.object({
1272
- role: import_v45.z.literal("assistant"),
1273
- content: import_v45.z.string().nullable().optional(),
1274
- reasoning: import_v45.z.string().nullable().optional(),
1333
+ choices: import_v46.z.array(
1334
+ import_v46.z.object({
1335
+ message: import_v46.z.object({
1336
+ role: import_v46.z.literal("assistant"),
1337
+ content: import_v46.z.string().nullable().optional(),
1338
+ reasoning: import_v46.z.string().nullable().optional(),
1275
1339
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1276
1340
  images: ImageResponseArraySchema.nullish(),
1277
- tool_calls: import_v45.z.array(
1278
- import_v45.z.object({
1279
- id: import_v45.z.string().optional().nullable(),
1280
- type: import_v45.z.literal("function"),
1281
- function: import_v45.z.object({
1282
- name: import_v45.z.string(),
1283
- arguments: import_v45.z.string()
1341
+ tool_calls: import_v46.z.array(
1342
+ import_v46.z.object({
1343
+ id: import_v46.z.string().optional().nullable(),
1344
+ type: import_v46.z.literal("function"),
1345
+ function: import_v46.z.object({
1346
+ name: import_v46.z.string(),
1347
+ arguments: import_v46.z.string()
1284
1348
  })
1285
1349
  })
1286
1350
  ).optional(),
1287
- annotations: import_v45.z.array(
1288
- import_v45.z.object({
1289
- type: import_v45.z.enum(["url_citation"]),
1290
- url_citation: import_v45.z.object({
1291
- end_index: import_v45.z.number(),
1292
- start_index: import_v45.z.number(),
1293
- title: import_v45.z.string(),
1294
- url: import_v45.z.string(),
1295
- content: import_v45.z.string().optional()
1351
+ annotations: import_v46.z.array(
1352
+ import_v46.z.object({
1353
+ type: import_v46.z.enum(["url_citation"]),
1354
+ url_citation: import_v46.z.object({
1355
+ end_index: import_v46.z.number(),
1356
+ start_index: import_v46.z.number(),
1357
+ title: import_v46.z.string(),
1358
+ url: import_v46.z.string(),
1359
+ content: import_v46.z.string().optional()
1296
1360
  })
1297
1361
  })
1298
1362
  ).nullish()
1299
1363
  }),
1300
- index: import_v45.z.number().nullish(),
1301
- logprobs: import_v45.z.object({
1302
- content: import_v45.z.array(
1303
- import_v45.z.object({
1304
- token: import_v45.z.string(),
1305
- logprob: import_v45.z.number(),
1306
- top_logprobs: import_v45.z.array(
1307
- import_v45.z.object({
1308
- token: import_v45.z.string(),
1309
- logprob: import_v45.z.number()
1364
+ index: import_v46.z.number().nullish(),
1365
+ logprobs: import_v46.z.object({
1366
+ content: import_v46.z.array(
1367
+ import_v46.z.object({
1368
+ token: import_v46.z.string(),
1369
+ logprob: import_v46.z.number(),
1370
+ top_logprobs: import_v46.z.array(
1371
+ import_v46.z.object({
1372
+ token: import_v46.z.string(),
1373
+ logprob: import_v46.z.number()
1310
1374
  })
1311
1375
  )
1312
1376
  })
1313
1377
  ).nullable()
1314
1378
  }).nullable().optional(),
1315
- finish_reason: import_v45.z.string().optional().nullable()
1379
+ finish_reason: import_v46.z.string().optional().nullable()
1316
1380
  })
1317
1381
  )
1318
1382
  });
1319
- var OpenRouterStreamChatCompletionChunkSchema = import_v45.z.union([
1383
+ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
1320
1384
  OpenRouterChatCompletionBaseResponseSchema.extend({
1321
- choices: import_v45.z.array(
1322
- import_v45.z.object({
1323
- delta: import_v45.z.object({
1324
- role: import_v45.z.enum(["assistant"]).optional(),
1325
- content: import_v45.z.string().nullish(),
1326
- reasoning: import_v45.z.string().nullish().optional(),
1385
+ choices: import_v46.z.array(
1386
+ import_v46.z.object({
1387
+ delta: import_v46.z.object({
1388
+ role: import_v46.z.enum(["assistant"]).optional(),
1389
+ content: import_v46.z.string().nullish(),
1390
+ reasoning: import_v46.z.string().nullish().optional(),
1327
1391
  reasoning_details: ReasoningDetailArraySchema.nullish(),
1328
1392
  images: ImageResponseArraySchema.nullish(),
1329
- tool_calls: import_v45.z.array(
1330
- import_v45.z.object({
1331
- index: import_v45.z.number().nullish(),
1332
- id: import_v45.z.string().nullish(),
1333
- type: import_v45.z.literal("function").optional(),
1334
- function: import_v45.z.object({
1335
- name: import_v45.z.string().nullish(),
1336
- arguments: import_v45.z.string().nullish()
1393
+ tool_calls: import_v46.z.array(
1394
+ import_v46.z.object({
1395
+ index: import_v46.z.number().nullish(),
1396
+ id: import_v46.z.string().nullish(),
1397
+ type: import_v46.z.literal("function").optional(),
1398
+ function: import_v46.z.object({
1399
+ name: import_v46.z.string().nullish(),
1400
+ arguments: import_v46.z.string().nullish()
1337
1401
  })
1338
1402
  })
1339
1403
  ).nullish(),
1340
- annotations: import_v45.z.array(
1341
- import_v45.z.object({
1342
- type: import_v45.z.enum(["url_citation"]),
1343
- url_citation: import_v45.z.object({
1344
- end_index: import_v45.z.number(),
1345
- start_index: import_v45.z.number(),
1346
- title: import_v45.z.string(),
1347
- url: import_v45.z.string(),
1348
- content: import_v45.z.string().optional()
1404
+ annotations: import_v46.z.array(
1405
+ import_v46.z.object({
1406
+ type: import_v46.z.enum(["url_citation"]),
1407
+ url_citation: import_v46.z.object({
1408
+ end_index: import_v46.z.number(),
1409
+ start_index: import_v46.z.number(),
1410
+ title: import_v46.z.string(),
1411
+ url: import_v46.z.string(),
1412
+ content: import_v46.z.string().optional()
1349
1413
  })
1350
1414
  })
1351
1415
  ).nullish()
1352
1416
  }).nullish(),
1353
- logprobs: import_v45.z.object({
1354
- content: import_v45.z.array(
1355
- import_v45.z.object({
1356
- token: import_v45.z.string(),
1357
- logprob: import_v45.z.number(),
1358
- top_logprobs: import_v45.z.array(
1359
- import_v45.z.object({
1360
- token: import_v45.z.string(),
1361
- logprob: import_v45.z.number()
1417
+ logprobs: import_v46.z.object({
1418
+ content: import_v46.z.array(
1419
+ import_v46.z.object({
1420
+ token: import_v46.z.string(),
1421
+ logprob: import_v46.z.number(),
1422
+ top_logprobs: import_v46.z.array(
1423
+ import_v46.z.object({
1424
+ token: import_v46.z.string(),
1425
+ logprob: import_v46.z.number()
1362
1426
  })
1363
1427
  )
1364
1428
  })
1365
1429
  ).nullable()
1366
1430
  }).nullish(),
1367
- finish_reason: import_v45.z.string().nullable().optional(),
1368
- index: import_v45.z.number().nullish()
1431
+ finish_reason: import_v46.z.string().nullable().optional(),
1432
+ index: import_v46.z.number().nullish()
1369
1433
  })
1370
1434
  )
1371
1435
  }),
@@ -1468,7 +1532,7 @@ var OpenRouterChatLanguageModel = class {
1468
1532
  return baseArgs;
1469
1533
  }
1470
1534
  async doGenerate(options) {
1471
- 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;
1535
+ 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;
1472
1536
  const providerOptions = options.providerOptions || {};
1473
1537
  const openrouterOptions = providerOptions.openrouter || {};
1474
1538
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -1595,24 +1659,25 @@ var OpenRouterChatLanguageModel = class {
1595
1659
  usage: usageInfo,
1596
1660
  warnings: [],
1597
1661
  providerMetadata: {
1598
- openrouter: {
1662
+ openrouter: OpenRouterProviderMetadataSchema.parse({
1599
1663
  provider: (_k = response.provider) != null ? _k : "",
1664
+ reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
1600
1665
  usage: {
1601
- promptTokens: (_l = usageInfo.inputTokens) != null ? _l : 0,
1602
- completionTokens: (_m = usageInfo.outputTokens) != null ? _m : 0,
1603
- totalTokens: (_n = usageInfo.totalTokens) != null ? _n : 0,
1604
- cost: (_o = response.usage) == null ? void 0 : _o.cost,
1666
+ promptTokens: (_m = usageInfo.inputTokens) != null ? _m : 0,
1667
+ completionTokens: (_n = usageInfo.outputTokens) != null ? _n : 0,
1668
+ totalTokens: (_o = usageInfo.totalTokens) != null ? _o : 0,
1669
+ cost: (_p = response.usage) == null ? void 0 : _p.cost,
1605
1670
  promptTokensDetails: {
1606
- cachedTokens: (_r = (_q = (_p = response.usage) == null ? void 0 : _p.prompt_tokens_details) == null ? void 0 : _q.cached_tokens) != null ? _r : 0
1671
+ cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
1607
1672
  },
1608
1673
  completionTokensDetails: {
1609
- reasoningTokens: (_u = (_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? _u : 0
1674
+ reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
1610
1675
  },
1611
1676
  costDetails: {
1612
- upstreamInferenceCost: (_x = (_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? _x : 0
1677
+ upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
1613
1678
  }
1614
1679
  }
1615
- }
1680
+ })
1616
1681
  },
1617
1682
  request: { body: args },
1618
1683
  response: {
@@ -1657,6 +1722,7 @@ var OpenRouterChatLanguageModel = class {
1657
1722
  cachedInputTokens: Number.NaN
1658
1723
  };
1659
1724
  const openrouterUsage = {};
1725
+ const accumulatedReasoningDetails = [];
1660
1726
  let textStarted = false;
1661
1727
  let reasoningStarted = false;
1662
1728
  let textId;
@@ -1742,6 +1808,7 @@ var OpenRouterChatLanguageModel = class {
1742
1808
  });
1743
1809
  };
1744
1810
  if (delta.reasoning_details && delta.reasoning_details.length > 0) {
1811
+ accumulatedReasoningDetails.push(...delta.reasoning_details);
1745
1812
  for (const detail of delta.reasoning_details) {
1746
1813
  switch (detail.type) {
1747
1814
  case "reasoning.text" /* Text */: {
@@ -1948,6 +2015,9 @@ var OpenRouterChatLanguageModel = class {
1948
2015
  if (provider !== void 0) {
1949
2016
  openrouterMetadata.provider = provider;
1950
2017
  }
2018
+ if (accumulatedReasoningDetails.length > 0) {
2019
+ openrouterMetadata.reasoning_details = accumulatedReasoningDetails;
2020
+ }
1951
2021
  controller.enqueue({
1952
2022
  type: "finish",
1953
2023
  finishReason,
@@ -2070,36 +2140,36 @@ ${assistantMessage}
2070
2140
  }
2071
2141
 
2072
2142
  // src/completion/schemas.ts
2073
- var import_v46 = require("zod/v4");
2074
- var OpenRouterCompletionChunkSchema = import_v46.z.union([
2075
- import_v46.z.object({
2076
- id: import_v46.z.string().optional(),
2077
- model: import_v46.z.string().optional(),
2078
- choices: import_v46.z.array(
2079
- import_v46.z.object({
2080
- text: import_v46.z.string(),
2081
- reasoning: import_v46.z.string().nullish().optional(),
2143
+ var import_v47 = require("zod/v4");
2144
+ var OpenRouterCompletionChunkSchema = import_v47.z.union([
2145
+ import_v47.z.object({
2146
+ id: import_v47.z.string().optional(),
2147
+ model: import_v47.z.string().optional(),
2148
+ choices: import_v47.z.array(
2149
+ import_v47.z.object({
2150
+ text: import_v47.z.string(),
2151
+ reasoning: import_v47.z.string().nullish().optional(),
2082
2152
  reasoning_details: ReasoningDetailArraySchema.nullish(),
2083
- finish_reason: import_v46.z.string().nullish(),
2084
- index: import_v46.z.number().nullish(),
2085
- logprobs: import_v46.z.object({
2086
- tokens: import_v46.z.array(import_v46.z.string()),
2087
- token_logprobs: import_v46.z.array(import_v46.z.number()),
2088
- top_logprobs: import_v46.z.array(import_v46.z.record(import_v46.z.string(), import_v46.z.number())).nullable()
2153
+ finish_reason: import_v47.z.string().nullish(),
2154
+ index: import_v47.z.number().nullish(),
2155
+ logprobs: import_v47.z.object({
2156
+ tokens: import_v47.z.array(import_v47.z.string()),
2157
+ token_logprobs: import_v47.z.array(import_v47.z.number()),
2158
+ top_logprobs: import_v47.z.array(import_v47.z.record(import_v47.z.string(), import_v47.z.number())).nullable()
2089
2159
  }).nullable().optional()
2090
2160
  })
2091
2161
  ),
2092
- usage: import_v46.z.object({
2093
- prompt_tokens: import_v46.z.number(),
2094
- prompt_tokens_details: import_v46.z.object({
2095
- cached_tokens: import_v46.z.number()
2162
+ usage: import_v47.z.object({
2163
+ prompt_tokens: import_v47.z.number(),
2164
+ prompt_tokens_details: import_v47.z.object({
2165
+ cached_tokens: import_v47.z.number()
2096
2166
  }).nullish(),
2097
- completion_tokens: import_v46.z.number(),
2098
- completion_tokens_details: import_v46.z.object({
2099
- reasoning_tokens: import_v46.z.number()
2167
+ completion_tokens: import_v47.z.number(),
2168
+ completion_tokens_details: import_v47.z.object({
2169
+ reasoning_tokens: import_v47.z.number()
2100
2170
  }).nullish(),
2101
- total_tokens: import_v46.z.number(),
2102
- cost: import_v46.z.number().optional()
2171
+ total_tokens: import_v47.z.number(),
2172
+ cost: import_v47.z.number().optional()
2103
2173
  }).nullish()
2104
2174
  }),
2105
2175
  OpenRouterErrorResponseSchema
@@ -2368,18 +2438,43 @@ var OpenRouter = class {
2368
2438
  }
2369
2439
  };
2370
2440
 
2441
+ // src/version.ts
2442
+ var VERSION = false ? "0.0.0-test" : "1.2.1";
2443
+
2444
+ // src/utils/remove-undefined.ts
2445
+ function removeUndefinedEntries2(record) {
2446
+ return Object.fromEntries(
2447
+ Object.entries(record).filter(([, value]) => value !== null)
2448
+ );
2449
+ }
2450
+
2451
+ // src/utils/with-user-agent-suffix.ts
2452
+ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
2453
+ const cleanedHeaders = removeUndefinedEntries2(
2454
+ headers != null ? headers : {}
2455
+ );
2456
+ const currentUserAgentHeader = cleanedHeaders["user-agent"] || "";
2457
+ const newUserAgent = [currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ");
2458
+ return __spreadProps(__spreadValues({}, cleanedHeaders), {
2459
+ "user-agent": newUserAgent
2460
+ });
2461
+ }
2462
+
2371
2463
  // src/provider.ts
2372
2464
  function createOpenRouter(options = {}) {
2373
2465
  var _a15, _b, _c;
2374
2466
  const baseURL = (_b = withoutTrailingSlash((_a15 = options.baseURL) != null ? _a15 : options.baseUrl)) != null ? _b : "https://openrouter.ai/api/v1";
2375
2467
  const compatibility = (_c = options.compatibility) != null ? _c : "compatible";
2376
- const getHeaders = () => __spreadValues({
2377
- Authorization: `Bearer ${loadApiKey({
2378
- apiKey: options.apiKey,
2379
- environmentVariableName: "OPENROUTER_API_KEY",
2380
- description: "OpenRouter"
2381
- })}`
2382
- }, options.headers);
2468
+ const getHeaders = () => withUserAgentSuffix(
2469
+ __spreadValues({
2470
+ Authorization: `Bearer ${loadApiKey({
2471
+ apiKey: options.apiKey,
2472
+ environmentVariableName: "OPENROUTER_API_KEY",
2473
+ description: "OpenRouter"
2474
+ })}`
2475
+ }, options.headers),
2476
+ `ai-sdk/openrouter/${VERSION}`
2477
+ );
2383
2478
  const createChatModel = (modelId, settings = {}) => new OpenRouterChatLanguageModel(modelId, settings, {
2384
2479
  provider: "openrouter.chat",
2385
2480
  url: ({ path }) => `${baseURL}${path}`,