@openrouter/ai-sdk-provider 2.2.2 → 2.2.4

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.
@@ -2295,6 +2295,46 @@ var OpenRouterProviderOptionsSchema = import_v43.z.object({
2295
2295
  }).optional()
2296
2296
  }).optional();
2297
2297
 
2298
+ // src/utils/compute-token-usage.ts
2299
+ function computeTokenUsage(usage) {
2300
+ var _a16, _b16, _c, _d, _e, _f, _g, _h;
2301
+ const promptTokens = (_a16 = usage.prompt_tokens) != null ? _a16 : 0;
2302
+ const completionTokens = (_b16 = usage.completion_tokens) != null ? _b16 : 0;
2303
+ const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
2304
+ const cacheWriteTokens = (_f = (_e = usage.prompt_tokens_details) == null ? void 0 : _e.cache_write_tokens) != null ? _f : void 0;
2305
+ const reasoningTokens = (_h = (_g = usage.completion_tokens_details) == null ? void 0 : _g.reasoning_tokens) != null ? _h : 0;
2306
+ return {
2307
+ inputTokens: {
2308
+ total: promptTokens,
2309
+ noCache: promptTokens - cacheReadTokens,
2310
+ cacheRead: cacheReadTokens,
2311
+ cacheWrite: cacheWriteTokens
2312
+ },
2313
+ outputTokens: {
2314
+ total: completionTokens,
2315
+ text: completionTokens - reasoningTokens,
2316
+ reasoning: reasoningTokens
2317
+ },
2318
+ raw: usage
2319
+ };
2320
+ }
2321
+ function emptyUsage() {
2322
+ return {
2323
+ inputTokens: {
2324
+ total: 0,
2325
+ noCache: void 0,
2326
+ cacheRead: void 0,
2327
+ cacheWrite: void 0
2328
+ },
2329
+ outputTokens: {
2330
+ total: 0,
2331
+ text: void 0,
2332
+ reasoning: void 0
2333
+ },
2334
+ raw: void 0
2335
+ };
2336
+ }
2337
+
2298
2338
  // src/utils/map-finish-reason.ts
2299
2339
  function mapToUnified(finishReason) {
2300
2340
  switch (finishReason) {
@@ -2801,7 +2841,8 @@ var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
2801
2841
  usage: import_v46.z.object({
2802
2842
  prompt_tokens: import_v46.z.number(),
2803
2843
  prompt_tokens_details: import_v46.z.object({
2804
- cached_tokens: import_v46.z.number()
2844
+ cached_tokens: import_v46.z.number(),
2845
+ cache_write_tokens: import_v46.z.number().nullish()
2805
2846
  }).passthrough().nullish(),
2806
2847
  completion_tokens: import_v46.z.number(),
2807
2848
  completion_tokens_details: import_v46.z.object({
@@ -3076,7 +3117,7 @@ var OpenRouterChatLanguageModel = class {
3076
3117
  return baseArgs;
3077
3118
  }
3078
3119
  async doGenerate(options) {
3079
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
3120
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
3080
3121
  const providerOptions = options.providerOptions || {};
3081
3122
  const openrouterOptions = providerOptions.openrouter || {};
3082
3123
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3115,34 +3156,8 @@ var OpenRouterChatLanguageModel = class {
3115
3156
  message: "No choice in response"
3116
3157
  });
3117
3158
  }
3118
- const usageInfo = response.usage ? {
3119
- inputTokens: {
3120
- total: (_a16 = response.usage.prompt_tokens) != null ? _a16 : 0,
3121
- noCache: void 0,
3122
- cacheRead: (_c = (_b16 = response.usage.prompt_tokens_details) == null ? void 0 : _b16.cached_tokens) != null ? _c : void 0,
3123
- cacheWrite: void 0
3124
- },
3125
- outputTokens: {
3126
- total: (_d = response.usage.completion_tokens) != null ? _d : 0,
3127
- text: void 0,
3128
- reasoning: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0
3129
- },
3130
- raw: response.usage
3131
- } : {
3132
- inputTokens: {
3133
- total: 0,
3134
- noCache: void 0,
3135
- cacheRead: void 0,
3136
- cacheWrite: void 0
3137
- },
3138
- outputTokens: {
3139
- total: 0,
3140
- text: void 0,
3141
- reasoning: void 0
3142
- },
3143
- raw: void 0
3144
- };
3145
- const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
3159
+ const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
3160
+ const reasoningDetails = (_a16 = choice.message.reasoning_details) != null ? _a16 : [];
3146
3161
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
3147
3162
  switch (detail.type) {
3148
3163
  case "reasoning.text" /* Text */: {
@@ -3211,9 +3226,9 @@ var OpenRouterChatLanguageModel = class {
3211
3226
  for (const toolCall of choice.message.tool_calls) {
3212
3227
  content.push({
3213
3228
  type: "tool-call",
3214
- toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3229
+ toolCallId: (_b16 = toolCall.id) != null ? _b16 : generateId(),
3215
3230
  toolName: toolCall.function.name,
3216
- input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3231
+ input: (_c = toolCall.function.arguments) != null ? _c : "{}",
3217
3232
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3218
3233
  openrouter: {
3219
3234
  reasoning_details: reasoningDetails
@@ -3240,19 +3255,19 @@ var OpenRouterChatLanguageModel = class {
3240
3255
  sourceType: "url",
3241
3256
  id: annotation.url_citation.url,
3242
3257
  url: annotation.url_citation.url,
3243
- title: (_j = annotation.url_citation.title) != null ? _j : "",
3258
+ title: (_d = annotation.url_citation.title) != null ? _d : "",
3244
3259
  providerMetadata: {
3245
3260
  openrouter: {
3246
- content: (_k = annotation.url_citation.content) != null ? _k : "",
3247
- startIndex: (_l = annotation.url_citation.start_index) != null ? _l : 0,
3248
- endIndex: (_m = annotation.url_citation.end_index) != null ? _m : 0
3261
+ content: (_e = annotation.url_citation.content) != null ? _e : "",
3262
+ startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3263
+ endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3249
3264
  }
3250
3265
  }
3251
3266
  });
3252
3267
  }
3253
3268
  }
3254
3269
  }
3255
- const fileAnnotations = (_n = choice.message.annotations) == null ? void 0 : _n.filter(
3270
+ const fileAnnotations = (_h = choice.message.annotations) == null ? void 0 : _h.filter(
3256
3271
  (a) => a.type === "file"
3257
3272
  );
3258
3273
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3260,7 +3275,7 @@ var OpenRouterChatLanguageModel = class {
3260
3275
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3261
3276
  );
3262
3277
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3263
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_o = choice.finish_reason) != null ? _o : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3278
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_i = choice.finish_reason) != null ? _i : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3264
3279
  return {
3265
3280
  content,
3266
3281
  finishReason: effectiveFinishReason,
@@ -3268,22 +3283,22 @@ var OpenRouterChatLanguageModel = class {
3268
3283
  warnings: [],
3269
3284
  providerMetadata: {
3270
3285
  openrouter: OpenRouterProviderMetadataSchema.parse({
3271
- provider: (_p = response.provider) != null ? _p : "",
3272
- reasoning_details: (_q = choice.message.reasoning_details) != null ? _q : [],
3286
+ provider: (_j = response.provider) != null ? _j : "",
3287
+ reasoning_details: (_k = choice.message.reasoning_details) != null ? _k : [],
3273
3288
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3274
3289
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3275
- promptTokens: (_r = usageInfo.inputTokens.total) != null ? _r : 0,
3276
- completionTokens: (_s = usageInfo.outputTokens.total) != null ? _s : 0,
3277
- totalTokens: ((_t = usageInfo.inputTokens.total) != null ? _t : 0) + ((_u = usageInfo.outputTokens.total) != null ? _u : 0)
3278
- }, ((_v = response.usage) == null ? void 0 : _v.cost) != null ? { cost: response.usage.cost } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.prompt_tokens_details) == null ? void 0 : _x.cached_tokens) != null ? {
3290
+ promptTokens: (_l = usageInfo.inputTokens.total) != null ? _l : 0,
3291
+ completionTokens: (_m = usageInfo.outputTokens.total) != null ? _m : 0,
3292
+ totalTokens: ((_n = usageInfo.inputTokens.total) != null ? _n : 0) + ((_o = usageInfo.outputTokens.total) != null ? _o : 0)
3293
+ }, ((_p = response.usage) == null ? void 0 : _p.cost) != null ? { cost: response.usage.cost } : {}), ((_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? {
3279
3294
  promptTokensDetails: {
3280
3295
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3281
3296
  }
3282
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
3297
+ } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
3283
3298
  completionTokensDetails: {
3284
3299
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3285
3300
  }
3286
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
3301
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
3287
3302
  costDetails: {
3288
3303
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3289
3304
  }
@@ -3354,7 +3369,7 @@ var OpenRouterChatLanguageModel = class {
3354
3369
  stream: response.pipeThrough(
3355
3370
  new TransformStream({
3356
3371
  transform(chunk, controller) {
3357
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
3372
+ var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3358
3373
  if (options.includeRawChunks) {
3359
3374
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3360
3375
  }
@@ -3386,30 +3401,29 @@ var OpenRouterChatLanguageModel = class {
3386
3401
  });
3387
3402
  }
3388
3403
  if (value.usage != null) {
3389
- usage.inputTokens.total = value.usage.prompt_tokens;
3390
- usage.outputTokens.total = value.usage.completion_tokens;
3404
+ const computed = computeTokenUsage(value.usage);
3405
+ Object.assign(usage.inputTokens, computed.inputTokens);
3406
+ Object.assign(usage.outputTokens, computed.outputTokens);
3391
3407
  rawUsage = value.usage;
3392
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
3408
+ const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
3409
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
3410
+ openrouterUsage.promptTokens = promptTokens;
3393
3411
  if (value.usage.prompt_tokens_details) {
3394
- const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
3395
- usage.inputTokens.cacheRead = cachedInputTokens;
3396
3412
  openrouterUsage.promptTokensDetails = {
3397
- cachedTokens: cachedInputTokens
3413
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
3398
3414
  };
3399
3415
  }
3400
- openrouterUsage.completionTokens = value.usage.completion_tokens;
3416
+ openrouterUsage.completionTokens = completionTokens;
3401
3417
  if (value.usage.completion_tokens_details) {
3402
- const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
3403
- usage.outputTokens.reasoning = reasoningTokens;
3404
3418
  openrouterUsage.completionTokensDetails = {
3405
- reasoningTokens
3419
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
3406
3420
  };
3407
3421
  }
3408
3422
  if (value.usage.cost != null) {
3409
3423
  openrouterUsage.cost = value.usage.cost;
3410
3424
  }
3411
3425
  openrouterUsage.totalTokens = value.usage.total_tokens;
3412
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
3426
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
3413
3427
  if (upstreamInferenceCost != null) {
3414
3428
  openrouterUsage.costDetails = {
3415
3429
  upstreamInferenceCost
@@ -3458,15 +3472,13 @@ var OpenRouterChatLanguageModel = class {
3458
3472
  }
3459
3473
  const reasoningMetadata = {
3460
3474
  openrouter: {
3461
- reasoning_details: delta.reasoning_details
3475
+ reasoning_details: accumulatedReasoningDetails.map((d) => __spreadValues({}, d))
3462
3476
  }
3463
3477
  };
3464
3478
  for (const detail of delta.reasoning_details) {
3465
3479
  switch (detail.type) {
3466
3480
  case "reasoning.text" /* Text */: {
3467
- if (detail.text) {
3468
- emitReasoningChunk(detail.text, reasoningMetadata);
3469
- }
3481
+ emitReasoningChunk(detail.text || "", reasoningMetadata);
3470
3482
  break;
3471
3483
  }
3472
3484
  case "reasoning.encrypted" /* Encrypted */: {
@@ -3529,12 +3541,12 @@ var OpenRouterChatLanguageModel = class {
3529
3541
  sourceType: "url",
3530
3542
  id: annotation.url_citation.url,
3531
3543
  url: annotation.url_citation.url,
3532
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3544
+ title: (_f = annotation.url_citation.title) != null ? _f : "",
3533
3545
  providerMetadata: {
3534
3546
  openrouter: {
3535
- content: (_e = annotation.url_citation.content) != null ? _e : "",
3536
- startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3537
- endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3547
+ content: (_g = annotation.url_citation.content) != null ? _g : "",
3548
+ startIndex: (_h = annotation.url_citation.start_index) != null ? _h : 0,
3549
+ endIndex: (_i = annotation.url_citation.end_index) != null ? _i : 0
3538
3550
  }
3539
3551
  }
3540
3552
  });
@@ -3550,7 +3562,7 @@ var OpenRouterChatLanguageModel = class {
3550
3562
  }
3551
3563
  if (delta.tool_calls != null) {
3552
3564
  for (const toolCallDelta of delta.tool_calls) {
3553
- const index = (_h = toolCallDelta.index) != null ? _h : toolCalls.length - 1;
3565
+ const index = (_j = toolCallDelta.index) != null ? _j : toolCalls.length - 1;
3554
3566
  if (toolCalls[index] == null) {
3555
3567
  if (toolCallDelta.type !== "function") {
3556
3568
  throw new InvalidResponseDataError({
@@ -3564,7 +3576,7 @@ var OpenRouterChatLanguageModel = class {
3564
3576
  message: `Expected 'id' to be a string.`
3565
3577
  });
3566
3578
  }
3567
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.name) == null) {
3579
+ if (((_k = toolCallDelta.function) == null ? void 0 : _k.name) == null) {
3568
3580
  throw new InvalidResponseDataError({
3569
3581
  data: toolCallDelta,
3570
3582
  message: `Expected 'function.name' to be a string.`
@@ -3575,7 +3587,7 @@ var OpenRouterChatLanguageModel = class {
3575
3587
  type: "function",
3576
3588
  function: {
3577
3589
  name: toolCallDelta.function.name,
3578
- arguments: (_j = toolCallDelta.function.arguments) != null ? _j : ""
3590
+ arguments: (_l = toolCallDelta.function.arguments) != null ? _l : ""
3579
3591
  },
3580
3592
  inputStarted: false,
3581
3593
  sent: false
@@ -3587,7 +3599,7 @@ var OpenRouterChatLanguageModel = class {
3587
3599
  message: `Tool call at index ${index} is missing after creation.`
3588
3600
  });
3589
3601
  }
3590
- if (((_k = toolCall2.function) == null ? void 0 : _k.name) != null && ((_l = toolCall2.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3602
+ if (((_m = toolCall2.function) == null ? void 0 : _m.name) != null && ((_n = toolCall2.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3591
3603
  toolCall2.inputStarted = true;
3592
3604
  controller.enqueue({
3593
3605
  type: "tool-input-start",
@@ -3638,18 +3650,18 @@ var OpenRouterChatLanguageModel = class {
3638
3650
  toolName: toolCall.function.name
3639
3651
  });
3640
3652
  }
3641
- if (((_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null) {
3642
- toolCall.function.arguments += (_o = (_n = toolCallDelta.function) == null ? void 0 : _n.arguments) != null ? _o : "";
3653
+ if (((_o = toolCallDelta.function) == null ? void 0 : _o.arguments) != null) {
3654
+ toolCall.function.arguments += (_q = (_p = toolCallDelta.function) == null ? void 0 : _p.arguments) != null ? _q : "";
3643
3655
  }
3644
3656
  controller.enqueue({
3645
3657
  type: "tool-input-delta",
3646
3658
  id: toolCall.id,
3647
- delta: (_p = toolCallDelta.function.arguments) != null ? _p : ""
3659
+ delta: (_r = toolCallDelta.function.arguments) != null ? _r : ""
3648
3660
  });
3649
- if (((_q = toolCall.function) == null ? void 0 : _q.name) != null && ((_r = toolCall.function) == null ? void 0 : _r.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3661
+ if (((_s = toolCall.function) == null ? void 0 : _s.name) != null && ((_t = toolCall.function) == null ? void 0 : _t.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3650
3662
  controller.enqueue({
3651
3663
  type: "tool-call",
3652
- toolCallId: (_s = toolCall.id) != null ? _s : generateId(),
3664
+ toolCallId: (_u = toolCall.id) != null ? _u : generateId(),
3653
3665
  toolName: toolCall.function.name,
3654
3666
  input: toolCall.function.arguments,
3655
3667
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
@@ -3879,7 +3891,8 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
3879
3891
  usage: import_v47.z.object({
3880
3892
  prompt_tokens: import_v47.z.number(),
3881
3893
  prompt_tokens_details: import_v47.z.object({
3882
- cached_tokens: import_v47.z.number()
3894
+ cached_tokens: import_v47.z.number(),
3895
+ cache_write_tokens: import_v47.z.number().nullish()
3883
3896
  }).passthrough().nullish(),
3884
3897
  completion_tokens: import_v47.z.number(),
3885
3898
  completion_tokens_details: import_v47.z.object({
@@ -3969,7 +3982,7 @@ var OpenRouterCompletionLanguageModel = class {
3969
3982
  }, this.config.extraBody), this.settings.extraBody);
3970
3983
  }
3971
3984
  async doGenerate(options) {
3972
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
3985
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
3973
3986
  const providerOptions = options.providerOptions || {};
3974
3987
  const openrouterOptions = providerOptions.openrouter || {};
3975
3988
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -4015,37 +4028,24 @@ var OpenRouterCompletionLanguageModel = class {
4015
4028
  }
4016
4029
  ],
4017
4030
  finishReason: mapOpenRouterFinishReason(choice.finish_reason),
4018
- usage: {
4019
- inputTokens: {
4020
- total: (_c = (_b16 = response.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
4021
- noCache: void 0,
4022
- cacheRead: (_f = (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : void 0,
4023
- cacheWrite: void 0
4024
- },
4025
- outputTokens: {
4026
- total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0,
4027
- text: void 0,
4028
- reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
4029
- },
4030
- raw: (_l = response.usage) != null ? _l : void 0
4031
- },
4031
+ usage: response.usage ? computeTokenUsage(response.usage) : emptyUsage(),
4032
4032
  warnings: [],
4033
4033
  providerMetadata: {
4034
4034
  openrouter: OpenRouterProviderMetadataSchema.parse({
4035
- provider: (_m = response.provider) != null ? _m : "",
4035
+ provider: (_b16 = response.provider) != null ? _b16 : "",
4036
4036
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
4037
- promptTokens: (_o = (_n = response.usage) == null ? void 0 : _n.prompt_tokens) != null ? _o : 0,
4038
- completionTokens: (_q = (_p = response.usage) == null ? void 0 : _p.completion_tokens) != null ? _q : 0,
4039
- totalTokens: ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens) != null ? _s : 0) + ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens) != null ? _u : 0)
4040
- }, ((_v = response.usage) == null ? void 0 : _v.cost) != null ? { cost: response.usage.cost } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.prompt_tokens_details) == null ? void 0 : _x.cached_tokens) != null ? {
4037
+ promptTokens: (_d = (_c = response.usage) == null ? void 0 : _c.prompt_tokens) != null ? _d : 0,
4038
+ completionTokens: (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens) != null ? _f : 0,
4039
+ totalTokens: ((_h = (_g = response.usage) == null ? void 0 : _g.prompt_tokens) != null ? _h : 0) + ((_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens) != null ? _j : 0)
4040
+ }, ((_k = response.usage) == null ? void 0 : _k.cost) != null ? { cost: response.usage.cost } : {}), ((_m = (_l = response.usage) == null ? void 0 : _l.prompt_tokens_details) == null ? void 0 : _m.cached_tokens) != null ? {
4041
4041
  promptTokensDetails: {
4042
4042
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
4043
4043
  }
4044
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
4044
+ } : {}), ((_o = (_n = response.usage) == null ? void 0 : _n.completion_tokens_details) == null ? void 0 : _o.reasoning_tokens) != null ? {
4045
4045
  completionTokensDetails: {
4046
4046
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
4047
4047
  }
4048
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
4048
+ } : {}), ((_q = (_p = response.usage) == null ? void 0 : _p.cost_details) == null ? void 0 : _q.upstream_inference_cost) != null ? {
4049
4049
  costDetails: {
4050
4050
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
4051
4051
  }
@@ -4101,7 +4101,7 @@ var OpenRouterCompletionLanguageModel = class {
4101
4101
  stream: response.pipeThrough(
4102
4102
  new TransformStream({
4103
4103
  transform(chunk, controller) {
4104
- var _a16, _b16, _c;
4104
+ var _a16, _b16, _c, _d, _e;
4105
4105
  if (options.includeRawChunks) {
4106
4106
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4107
4107
  }
@@ -4120,30 +4120,29 @@ var OpenRouterCompletionLanguageModel = class {
4120
4120
  provider = value.provider;
4121
4121
  }
4122
4122
  if (value.usage != null) {
4123
- usage.inputTokens.total = value.usage.prompt_tokens;
4124
- usage.outputTokens.total = value.usage.completion_tokens;
4123
+ const computed = computeTokenUsage(value.usage);
4124
+ Object.assign(usage.inputTokens, computed.inputTokens);
4125
+ Object.assign(usage.outputTokens, computed.outputTokens);
4125
4126
  rawUsage = value.usage;
4126
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
4127
+ const promptTokens = (_a16 = value.usage.prompt_tokens) != null ? _a16 : 0;
4128
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
4129
+ openrouterUsage.promptTokens = promptTokens;
4127
4130
  if (value.usage.prompt_tokens_details) {
4128
- const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
4129
- usage.inputTokens.cacheRead = cachedInputTokens;
4130
4131
  openrouterUsage.promptTokensDetails = {
4131
- cachedTokens: cachedInputTokens
4132
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
4132
4133
  };
4133
4134
  }
4134
- openrouterUsage.completionTokens = value.usage.completion_tokens;
4135
+ openrouterUsage.completionTokens = completionTokens;
4135
4136
  if (value.usage.completion_tokens_details) {
4136
- const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
4137
- usage.outputTokens.reasoning = reasoningTokens;
4138
4137
  openrouterUsage.completionTokensDetails = {
4139
- reasoningTokens
4138
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
4140
4139
  };
4141
4140
  }
4142
4141
  if (value.usage.cost != null) {
4143
4142
  openrouterUsage.cost = value.usage.cost;
4144
4143
  }
4145
4144
  openrouterUsage.totalTokens = value.usage.total_tokens;
4146
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
4145
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
4147
4146
  if (upstreamInferenceCost != null) {
4148
4147
  openrouterUsage.costDetails = {
4149
4148
  upstreamInferenceCost