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