@openrouter/ai-sdk-provider 2.2.1 → 2.2.3

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
@@ -2329,6 +2329,46 @@ var OpenRouterProviderOptionsSchema = import_v43.z.object({
2329
2329
  }).optional()
2330
2330
  }).optional();
2331
2331
 
2332
+ // src/utils/compute-token-usage.ts
2333
+ function computeTokenUsage(usage) {
2334
+ var _a16, _b16, _c, _d, _e, _f, _g, _h;
2335
+ const promptTokens = (_a16 = usage.prompt_tokens) != null ? _a16 : 0;
2336
+ const completionTokens = (_b16 = usage.completion_tokens) != null ? _b16 : 0;
2337
+ const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
2338
+ const cacheWriteTokens = (_f = (_e = usage.prompt_tokens_details) == null ? void 0 : _e.cache_write_tokens) != null ? _f : void 0;
2339
+ const reasoningTokens = (_h = (_g = usage.completion_tokens_details) == null ? void 0 : _g.reasoning_tokens) != null ? _h : 0;
2340
+ return {
2341
+ inputTokens: {
2342
+ total: promptTokens,
2343
+ noCache: promptTokens - cacheReadTokens,
2344
+ cacheRead: cacheReadTokens,
2345
+ cacheWrite: cacheWriteTokens
2346
+ },
2347
+ outputTokens: {
2348
+ total: completionTokens,
2349
+ text: completionTokens - reasoningTokens,
2350
+ reasoning: reasoningTokens
2351
+ },
2352
+ raw: usage
2353
+ };
2354
+ }
2355
+ function emptyUsage() {
2356
+ return {
2357
+ inputTokens: {
2358
+ total: 0,
2359
+ noCache: void 0,
2360
+ cacheRead: void 0,
2361
+ cacheWrite: void 0
2362
+ },
2363
+ outputTokens: {
2364
+ total: 0,
2365
+ text: void 0,
2366
+ reasoning: void 0
2367
+ },
2368
+ raw: void 0
2369
+ };
2370
+ }
2371
+
2332
2372
  // src/utils/map-finish-reason.ts
2333
2373
  function mapToUnified(finishReason) {
2334
2374
  switch (finishReason) {
@@ -2430,23 +2470,34 @@ function isUrl({
2430
2470
  }
2431
2471
 
2432
2472
  // src/chat/file-url-utils.ts
2433
- function getFileUrl({
2434
- part,
2473
+ function buildFileDataUrl({
2474
+ data,
2475
+ mediaType,
2435
2476
  defaultMediaType
2436
2477
  }) {
2437
- var _a16, _b16;
2438
- if (part.data instanceof Uint8Array) {
2439
- const base64 = convertUint8ArrayToBase64(part.data);
2440
- return `data:${(_a16 = part.mediaType) != null ? _a16 : defaultMediaType};base64,${base64}`;
2478
+ if (data instanceof Uint8Array) {
2479
+ const base64 = convertUint8ArrayToBase64(data);
2480
+ return `data:${mediaType != null ? mediaType : defaultMediaType};base64,${base64}`;
2441
2481
  }
2442
- const stringUrl = part.data.toString();
2482
+ const stringData = data.toString();
2443
2483
  if (isUrl({
2444
- url: stringUrl,
2484
+ url: stringData,
2445
2485
  protocols: /* @__PURE__ */ new Set(["http:", "https:"])
2446
2486
  })) {
2447
- return stringUrl;
2487
+ return stringData;
2448
2488
  }
2449
- return stringUrl.startsWith("data:") ? stringUrl : `data:${(_b16 = part.mediaType) != null ? _b16 : defaultMediaType};base64,${stringUrl}`;
2489
+ return stringData.startsWith("data:") ? stringData : `data:${mediaType != null ? mediaType : defaultMediaType};base64,${stringData}`;
2490
+ }
2491
+ function getFileUrl({
2492
+ part,
2493
+ defaultMediaType
2494
+ }) {
2495
+ const data = part.data instanceof URL ? part.data.toString() : part.data;
2496
+ return buildFileDataUrl({
2497
+ data,
2498
+ mediaType: part.mediaType,
2499
+ defaultMediaType
2500
+ });
2450
2501
  }
2451
2502
  function getMediaType(dataUrl, defaultMediaType) {
2452
2503
  var _a16;
@@ -2824,7 +2875,8 @@ var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
2824
2875
  usage: import_v46.z.object({
2825
2876
  prompt_tokens: import_v46.z.number(),
2826
2877
  prompt_tokens_details: import_v46.z.object({
2827
- cached_tokens: import_v46.z.number()
2878
+ cached_tokens: import_v46.z.number(),
2879
+ cache_write_tokens: import_v46.z.number().nullish()
2828
2880
  }).passthrough().nullish(),
2829
2881
  completion_tokens: import_v46.z.number(),
2830
2882
  completion_tokens_details: import_v46.z.object({
@@ -3099,7 +3151,7 @@ var OpenRouterChatLanguageModel = class {
3099
3151
  return baseArgs;
3100
3152
  }
3101
3153
  async doGenerate(options) {
3102
- 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;
3154
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
3103
3155
  const providerOptions = options.providerOptions || {};
3104
3156
  const openrouterOptions = providerOptions.openrouter || {};
3105
3157
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3138,34 +3190,8 @@ var OpenRouterChatLanguageModel = class {
3138
3190
  message: "No choice in response"
3139
3191
  });
3140
3192
  }
3141
- const usageInfo = response.usage ? {
3142
- inputTokens: {
3143
- total: (_a16 = response.usage.prompt_tokens) != null ? _a16 : 0,
3144
- noCache: void 0,
3145
- cacheRead: (_c = (_b16 = response.usage.prompt_tokens_details) == null ? void 0 : _b16.cached_tokens) != null ? _c : void 0,
3146
- cacheWrite: void 0
3147
- },
3148
- outputTokens: {
3149
- total: (_d = response.usage.completion_tokens) != null ? _d : 0,
3150
- text: void 0,
3151
- reasoning: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0
3152
- },
3153
- raw: response.usage
3154
- } : {
3155
- inputTokens: {
3156
- total: 0,
3157
- noCache: void 0,
3158
- cacheRead: void 0,
3159
- cacheWrite: void 0
3160
- },
3161
- outputTokens: {
3162
- total: 0,
3163
- text: void 0,
3164
- reasoning: void 0
3165
- },
3166
- raw: void 0
3167
- };
3168
- const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
3193
+ const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
3194
+ const reasoningDetails = (_a16 = choice.message.reasoning_details) != null ? _a16 : [];
3169
3195
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
3170
3196
  switch (detail.type) {
3171
3197
  case "reasoning.text" /* Text */: {
@@ -3234,9 +3260,9 @@ var OpenRouterChatLanguageModel = class {
3234
3260
  for (const toolCall of choice.message.tool_calls) {
3235
3261
  content.push({
3236
3262
  type: "tool-call",
3237
- toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3263
+ toolCallId: (_b16 = toolCall.id) != null ? _b16 : generateId(),
3238
3264
  toolName: toolCall.function.name,
3239
- input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3265
+ input: (_c = toolCall.function.arguments) != null ? _c : "{}",
3240
3266
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3241
3267
  openrouter: {
3242
3268
  reasoning_details: reasoningDetails
@@ -3263,19 +3289,19 @@ var OpenRouterChatLanguageModel = class {
3263
3289
  sourceType: "url",
3264
3290
  id: annotation.url_citation.url,
3265
3291
  url: annotation.url_citation.url,
3266
- title: (_j = annotation.url_citation.title) != null ? _j : "",
3292
+ title: (_d = annotation.url_citation.title) != null ? _d : "",
3267
3293
  providerMetadata: {
3268
3294
  openrouter: {
3269
- content: (_k = annotation.url_citation.content) != null ? _k : "",
3270
- startIndex: (_l = annotation.url_citation.start_index) != null ? _l : 0,
3271
- endIndex: (_m = annotation.url_citation.end_index) != null ? _m : 0
3295
+ content: (_e = annotation.url_citation.content) != null ? _e : "",
3296
+ startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3297
+ endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3272
3298
  }
3273
3299
  }
3274
3300
  });
3275
3301
  }
3276
3302
  }
3277
3303
  }
3278
- const fileAnnotations = (_n = choice.message.annotations) == null ? void 0 : _n.filter(
3304
+ const fileAnnotations = (_h = choice.message.annotations) == null ? void 0 : _h.filter(
3279
3305
  (a) => a.type === "file"
3280
3306
  );
3281
3307
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3283,7 +3309,7 @@ var OpenRouterChatLanguageModel = class {
3283
3309
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3284
3310
  );
3285
3311
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3286
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_o = choice.finish_reason) != null ? _o : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3312
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_i = choice.finish_reason) != null ? _i : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3287
3313
  return {
3288
3314
  content,
3289
3315
  finishReason: effectiveFinishReason,
@@ -3291,22 +3317,22 @@ var OpenRouterChatLanguageModel = class {
3291
3317
  warnings: [],
3292
3318
  providerMetadata: {
3293
3319
  openrouter: OpenRouterProviderMetadataSchema.parse({
3294
- provider: (_p = response.provider) != null ? _p : "",
3295
- reasoning_details: (_q = choice.message.reasoning_details) != null ? _q : [],
3320
+ provider: (_j = response.provider) != null ? _j : "",
3321
+ reasoning_details: (_k = choice.message.reasoning_details) != null ? _k : [],
3296
3322
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3297
3323
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3298
- promptTokens: (_r = usageInfo.inputTokens.total) != null ? _r : 0,
3299
- completionTokens: (_s = usageInfo.outputTokens.total) != null ? _s : 0,
3300
- totalTokens: ((_t = usageInfo.inputTokens.total) != null ? _t : 0) + ((_u = usageInfo.outputTokens.total) != null ? _u : 0)
3301
- }, ((_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 ? {
3324
+ promptTokens: (_l = usageInfo.inputTokens.total) != null ? _l : 0,
3325
+ completionTokens: (_m = usageInfo.outputTokens.total) != null ? _m : 0,
3326
+ totalTokens: ((_n = usageInfo.inputTokens.total) != null ? _n : 0) + ((_o = usageInfo.outputTokens.total) != null ? _o : 0)
3327
+ }, ((_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 ? {
3302
3328
  promptTokensDetails: {
3303
3329
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3304
3330
  }
3305
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
3331
+ } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
3306
3332
  completionTokensDetails: {
3307
3333
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3308
3334
  }
3309
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
3335
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
3310
3336
  costDetails: {
3311
3337
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3312
3338
  }
@@ -3377,7 +3403,7 @@ var OpenRouterChatLanguageModel = class {
3377
3403
  stream: response.pipeThrough(
3378
3404
  new TransformStream({
3379
3405
  transform(chunk, controller) {
3380
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
3406
+ var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3381
3407
  if (options.includeRawChunks) {
3382
3408
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3383
3409
  }
@@ -3409,30 +3435,29 @@ var OpenRouterChatLanguageModel = class {
3409
3435
  });
3410
3436
  }
3411
3437
  if (value.usage != null) {
3412
- usage.inputTokens.total = value.usage.prompt_tokens;
3413
- usage.outputTokens.total = value.usage.completion_tokens;
3438
+ const computed = computeTokenUsage(value.usage);
3439
+ Object.assign(usage.inputTokens, computed.inputTokens);
3440
+ Object.assign(usage.outputTokens, computed.outputTokens);
3414
3441
  rawUsage = value.usage;
3415
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
3442
+ const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
3443
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
3444
+ openrouterUsage.promptTokens = promptTokens;
3416
3445
  if (value.usage.prompt_tokens_details) {
3417
- const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
3418
- usage.inputTokens.cacheRead = cachedInputTokens;
3419
3446
  openrouterUsage.promptTokensDetails = {
3420
- cachedTokens: cachedInputTokens
3447
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
3421
3448
  };
3422
3449
  }
3423
- openrouterUsage.completionTokens = value.usage.completion_tokens;
3450
+ openrouterUsage.completionTokens = completionTokens;
3424
3451
  if (value.usage.completion_tokens_details) {
3425
- const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
3426
- usage.outputTokens.reasoning = reasoningTokens;
3427
3452
  openrouterUsage.completionTokensDetails = {
3428
- reasoningTokens
3453
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
3429
3454
  };
3430
3455
  }
3431
3456
  if (value.usage.cost != null) {
3432
3457
  openrouterUsage.cost = value.usage.cost;
3433
3458
  }
3434
3459
  openrouterUsage.totalTokens = value.usage.total_tokens;
3435
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
3460
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
3436
3461
  if (upstreamInferenceCost != null) {
3437
3462
  openrouterUsage.costDetails = {
3438
3463
  upstreamInferenceCost
@@ -3552,12 +3577,12 @@ var OpenRouterChatLanguageModel = class {
3552
3577
  sourceType: "url",
3553
3578
  id: annotation.url_citation.url,
3554
3579
  url: annotation.url_citation.url,
3555
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3580
+ title: (_f = annotation.url_citation.title) != null ? _f : "",
3556
3581
  providerMetadata: {
3557
3582
  openrouter: {
3558
- content: (_e = annotation.url_citation.content) != null ? _e : "",
3559
- startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3560
- endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3583
+ content: (_g = annotation.url_citation.content) != null ? _g : "",
3584
+ startIndex: (_h = annotation.url_citation.start_index) != null ? _h : 0,
3585
+ endIndex: (_i = annotation.url_citation.end_index) != null ? _i : 0
3561
3586
  }
3562
3587
  }
3563
3588
  });
@@ -3573,7 +3598,7 @@ var OpenRouterChatLanguageModel = class {
3573
3598
  }
3574
3599
  if (delta.tool_calls != null) {
3575
3600
  for (const toolCallDelta of delta.tool_calls) {
3576
- const index = (_h = toolCallDelta.index) != null ? _h : toolCalls.length - 1;
3601
+ const index = (_j = toolCallDelta.index) != null ? _j : toolCalls.length - 1;
3577
3602
  if (toolCalls[index] == null) {
3578
3603
  if (toolCallDelta.type !== "function") {
3579
3604
  throw new InvalidResponseDataError({
@@ -3587,7 +3612,7 @@ var OpenRouterChatLanguageModel = class {
3587
3612
  message: `Expected 'id' to be a string.`
3588
3613
  });
3589
3614
  }
3590
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.name) == null) {
3615
+ if (((_k = toolCallDelta.function) == null ? void 0 : _k.name) == null) {
3591
3616
  throw new InvalidResponseDataError({
3592
3617
  data: toolCallDelta,
3593
3618
  message: `Expected 'function.name' to be a string.`
@@ -3598,7 +3623,7 @@ var OpenRouterChatLanguageModel = class {
3598
3623
  type: "function",
3599
3624
  function: {
3600
3625
  name: toolCallDelta.function.name,
3601
- arguments: (_j = toolCallDelta.function.arguments) != null ? _j : ""
3626
+ arguments: (_l = toolCallDelta.function.arguments) != null ? _l : ""
3602
3627
  },
3603
3628
  inputStarted: false,
3604
3629
  sent: false
@@ -3610,7 +3635,7 @@ var OpenRouterChatLanguageModel = class {
3610
3635
  message: `Tool call at index ${index} is missing after creation.`
3611
3636
  });
3612
3637
  }
3613
- if (((_k = toolCall2.function) == null ? void 0 : _k.name) != null && ((_l = toolCall2.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3638
+ if (((_m = toolCall2.function) == null ? void 0 : _m.name) != null && ((_n = toolCall2.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3614
3639
  toolCall2.inputStarted = true;
3615
3640
  controller.enqueue({
3616
3641
  type: "tool-input-start",
@@ -3661,18 +3686,18 @@ var OpenRouterChatLanguageModel = class {
3661
3686
  toolName: toolCall.function.name
3662
3687
  });
3663
3688
  }
3664
- if (((_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null) {
3665
- toolCall.function.arguments += (_o = (_n = toolCallDelta.function) == null ? void 0 : _n.arguments) != null ? _o : "";
3689
+ if (((_o = toolCallDelta.function) == null ? void 0 : _o.arguments) != null) {
3690
+ toolCall.function.arguments += (_q = (_p = toolCallDelta.function) == null ? void 0 : _p.arguments) != null ? _q : "";
3666
3691
  }
3667
3692
  controller.enqueue({
3668
3693
  type: "tool-input-delta",
3669
3694
  id: toolCall.id,
3670
- delta: (_p = toolCallDelta.function.arguments) != null ? _p : ""
3695
+ delta: (_r = toolCallDelta.function.arguments) != null ? _r : ""
3671
3696
  });
3672
- if (((_q = toolCall.function) == null ? void 0 : _q.name) != null && ((_r = toolCall.function) == null ? void 0 : _r.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3697
+ if (((_s = toolCall.function) == null ? void 0 : _s.name) != null && ((_t = toolCall.function) == null ? void 0 : _t.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3673
3698
  controller.enqueue({
3674
3699
  type: "tool-call",
3675
- toolCallId: (_s = toolCall.id) != null ? _s : generateId(),
3700
+ toolCallId: (_u = toolCall.id) != null ? _u : generateId(),
3676
3701
  toolName: toolCall.function.name,
3677
3702
  input: toolCall.function.arguments,
3678
3703
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
@@ -3902,7 +3927,8 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
3902
3927
  usage: import_v47.z.object({
3903
3928
  prompt_tokens: import_v47.z.number(),
3904
3929
  prompt_tokens_details: import_v47.z.object({
3905
- cached_tokens: import_v47.z.number()
3930
+ cached_tokens: import_v47.z.number(),
3931
+ cache_write_tokens: import_v47.z.number().nullish()
3906
3932
  }).passthrough().nullish(),
3907
3933
  completion_tokens: import_v47.z.number(),
3908
3934
  completion_tokens_details: import_v47.z.object({
@@ -3992,7 +4018,7 @@ var OpenRouterCompletionLanguageModel = class {
3992
4018
  }, this.config.extraBody), this.settings.extraBody);
3993
4019
  }
3994
4020
  async doGenerate(options) {
3995
- 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;
4021
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
3996
4022
  const providerOptions = options.providerOptions || {};
3997
4023
  const openrouterOptions = providerOptions.openrouter || {};
3998
4024
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -4038,37 +4064,24 @@ var OpenRouterCompletionLanguageModel = class {
4038
4064
  }
4039
4065
  ],
4040
4066
  finishReason: mapOpenRouterFinishReason(choice.finish_reason),
4041
- usage: {
4042
- inputTokens: {
4043
- total: (_c = (_b16 = response.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
4044
- noCache: void 0,
4045
- cacheRead: (_f = (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : void 0,
4046
- cacheWrite: void 0
4047
- },
4048
- outputTokens: {
4049
- total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0,
4050
- text: void 0,
4051
- reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
4052
- },
4053
- raw: (_l = response.usage) != null ? _l : void 0
4054
- },
4067
+ usage: response.usage ? computeTokenUsage(response.usage) : emptyUsage(),
4055
4068
  warnings: [],
4056
4069
  providerMetadata: {
4057
4070
  openrouter: OpenRouterProviderMetadataSchema.parse({
4058
- provider: (_m = response.provider) != null ? _m : "",
4071
+ provider: (_b16 = response.provider) != null ? _b16 : "",
4059
4072
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
4060
- promptTokens: (_o = (_n = response.usage) == null ? void 0 : _n.prompt_tokens) != null ? _o : 0,
4061
- completionTokens: (_q = (_p = response.usage) == null ? void 0 : _p.completion_tokens) != null ? _q : 0,
4062
- 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)
4063
- }, ((_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 ? {
4073
+ promptTokens: (_d = (_c = response.usage) == null ? void 0 : _c.prompt_tokens) != null ? _d : 0,
4074
+ completionTokens: (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens) != null ? _f : 0,
4075
+ 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)
4076
+ }, ((_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 ? {
4064
4077
  promptTokensDetails: {
4065
4078
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
4066
4079
  }
4067
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
4080
+ } : {}), ((_o = (_n = response.usage) == null ? void 0 : _n.completion_tokens_details) == null ? void 0 : _o.reasoning_tokens) != null ? {
4068
4081
  completionTokensDetails: {
4069
4082
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
4070
4083
  }
4071
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
4084
+ } : {}), ((_q = (_p = response.usage) == null ? void 0 : _p.cost_details) == null ? void 0 : _q.upstream_inference_cost) != null ? {
4072
4085
  costDetails: {
4073
4086
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
4074
4087
  }
@@ -4124,7 +4137,7 @@ var OpenRouterCompletionLanguageModel = class {
4124
4137
  stream: response.pipeThrough(
4125
4138
  new TransformStream({
4126
4139
  transform(chunk, controller) {
4127
- var _a16, _b16, _c;
4140
+ var _a16, _b16, _c, _d, _e;
4128
4141
  if (options.includeRawChunks) {
4129
4142
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4130
4143
  }
@@ -4143,30 +4156,29 @@ var OpenRouterCompletionLanguageModel = class {
4143
4156
  provider = value.provider;
4144
4157
  }
4145
4158
  if (value.usage != null) {
4146
- usage.inputTokens.total = value.usage.prompt_tokens;
4147
- usage.outputTokens.total = value.usage.completion_tokens;
4159
+ const computed = computeTokenUsage(value.usage);
4160
+ Object.assign(usage.inputTokens, computed.inputTokens);
4161
+ Object.assign(usage.outputTokens, computed.outputTokens);
4148
4162
  rawUsage = value.usage;
4149
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
4163
+ const promptTokens = (_a16 = value.usage.prompt_tokens) != null ? _a16 : 0;
4164
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
4165
+ openrouterUsage.promptTokens = promptTokens;
4150
4166
  if (value.usage.prompt_tokens_details) {
4151
- const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
4152
- usage.inputTokens.cacheRead = cachedInputTokens;
4153
4167
  openrouterUsage.promptTokensDetails = {
4154
- cachedTokens: cachedInputTokens
4168
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
4155
4169
  };
4156
4170
  }
4157
- openrouterUsage.completionTokens = value.usage.completion_tokens;
4171
+ openrouterUsage.completionTokens = completionTokens;
4158
4172
  if (value.usage.completion_tokens_details) {
4159
- const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
4160
- usage.outputTokens.reasoning = reasoningTokens;
4161
4173
  openrouterUsage.completionTokensDetails = {
4162
- reasoningTokens
4174
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
4163
4175
  };
4164
4176
  }
4165
4177
  if (value.usage.cost != null) {
4166
4178
  openrouterUsage.cost = value.usage.cost;
4167
4179
  }
4168
4180
  openrouterUsage.totalTokens = value.usage.total_tokens;
4169
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
4181
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
4170
4182
  if (upstreamInferenceCost != null) {
4171
4183
  openrouterUsage.costDetails = {
4172
4184
  upstreamInferenceCost
@@ -4403,11 +4415,6 @@ var OpenRouterImageModel = class {
4403
4415
  } = options;
4404
4416
  const openrouterOptions = (providerOptions == null ? void 0 : providerOptions.openrouter) || {};
4405
4417
  const warnings = [];
4406
- if (files !== void 0 && files.length > 0) {
4407
- throw new UnsupportedFunctionalityError({
4408
- functionality: "image editing (files parameter)"
4409
- });
4410
- }
4411
4418
  if (mask !== void 0) {
4412
4419
  throw new UnsupportedFunctionalityError({
4413
4420
  functionality: "image inpainting (mask parameter)"
@@ -4428,12 +4435,19 @@ var OpenRouterImageModel = class {
4428
4435
  });
4429
4436
  }
4430
4437
  const imageConfig = aspectRatio !== void 0 ? { aspect_ratio: aspectRatio } : void 0;
4438
+ const hasFiles = files !== void 0 && files.length > 0;
4439
+ const userContent = hasFiles ? [
4440
+ ...files.map(
4441
+ (file) => convertImageFileToContentPart(file)
4442
+ ),
4443
+ { type: "text", text: prompt != null ? prompt : "" }
4444
+ ] : prompt != null ? prompt : "";
4431
4445
  const body = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
4432
4446
  model: this.modelId,
4433
4447
  messages: [
4434
4448
  {
4435
4449
  role: "user",
4436
- content: prompt != null ? prompt : ""
4450
+ content: userContent
4437
4451
  }
4438
4452
  ],
4439
4453
  modalities: ["image", "text"]
@@ -4484,6 +4498,24 @@ var OpenRouterImageModel = class {
4484
4498
  };
4485
4499
  }
4486
4500
  };
4501
+ var DEFAULT_IMAGE_MEDIA_TYPE = "image/png";
4502
+ function convertImageFileToContentPart(file) {
4503
+ if (file.type === "url") {
4504
+ return {
4505
+ type: "image_url",
4506
+ image_url: { url: file.url }
4507
+ };
4508
+ }
4509
+ const url = buildFileDataUrl({
4510
+ data: file.data,
4511
+ mediaType: file.mediaType,
4512
+ defaultMediaType: DEFAULT_IMAGE_MEDIA_TYPE
4513
+ });
4514
+ return {
4515
+ type: "image_url",
4516
+ image_url: { url }
4517
+ };
4518
+ }
4487
4519
 
4488
4520
  // src/utils/remove-undefined.ts
4489
4521
  function removeUndefinedEntries(record) {
@@ -4526,7 +4558,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4526
4558
  }
4527
4559
 
4528
4560
  // src/version.ts
4529
- var VERSION2 = false ? "0.0.0-test" : "2.2.1";
4561
+ var VERSION2 = false ? "0.0.0-test" : "2.2.3";
4530
4562
 
4531
4563
  // src/provider.ts
4532
4564
  function createOpenRouter(options = {}) {