@openrouter/ai-sdk-provider 2.0.2 → 2.1.0

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.
@@ -60,7 +60,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
60
60
  var index_exports = {};
61
61
  __export(index_exports, {
62
62
  OpenRouterChatLanguageModel: () => OpenRouterChatLanguageModel,
63
- OpenRouterCompletionLanguageModel: () => OpenRouterCompletionLanguageModel
63
+ OpenRouterCompletionLanguageModel: () => OpenRouterCompletionLanguageModel,
64
+ OpenRouterEmbeddingModel: () => OpenRouterEmbeddingModel,
65
+ OpenRouterImageModel: () => OpenRouterImageModel
64
66
  });
65
67
  module.exports = __toCommonJS(index_exports);
66
68
 
@@ -2442,7 +2444,7 @@ function getCacheControl(providerMetadata) {
2442
2444
  return (_c = (_b16 = (_a16 = openrouter == null ? void 0 : openrouter.cacheControl) != null ? _a16 : openrouter == null ? void 0 : openrouter.cache_control) != null ? _b16 : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
2443
2445
  }
2444
2446
  function convertToOpenRouterChatMessages(prompt) {
2445
- var _a16, _b16, _c, _d, _e, _f, _g;
2447
+ var _a16, _b16, _c, _d, _e, _f, _g, _h;
2446
2448
  const messages = [];
2447
2449
  for (const { role, content, providerOptions } of prompt) {
2448
2450
  switch (role) {
@@ -2471,42 +2473,46 @@ function convertToOpenRouterChatMessages(prompt) {
2471
2473
  break;
2472
2474
  }
2473
2475
  const messageCacheControl = getCacheControl(providerOptions);
2476
+ let lastTextPartIndex = -1;
2477
+ for (let i = content.length - 1; i >= 0; i--) {
2478
+ if (((_c = content[i]) == null ? void 0 : _c.type) === "text") {
2479
+ lastTextPartIndex = i;
2480
+ break;
2481
+ }
2482
+ }
2474
2483
  const contentParts = content.map(
2475
- (part) => {
2476
- var _a17, _b17, _c2, _d2, _e2, _f2, _g2;
2477
- const cacheControl = (_a17 = getCacheControl(part.providerOptions)) != null ? _a17 : messageCacheControl;
2484
+ (part, index) => {
2485
+ var _a17, _b17, _c2, _d2, _e2, _f2;
2486
+ const isLastTextPart = part.type === "text" && index === lastTextPartIndex;
2487
+ const partCacheControl = getCacheControl(part.providerOptions);
2488
+ const cacheControl = part.type === "text" ? partCacheControl != null ? partCacheControl : isLastTextPart ? messageCacheControl : void 0 : partCacheControl;
2478
2489
  switch (part.type) {
2479
2490
  case "text":
2480
- return {
2491
+ return __spreadValues({
2481
2492
  type: "text",
2482
- text: part.text,
2483
- // For text parts, only use part-specific cache control
2484
- cache_control: cacheControl
2485
- };
2493
+ text: part.text
2494
+ }, cacheControl && { cache_control: cacheControl });
2486
2495
  case "file": {
2487
- if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("image/")) {
2496
+ if ((_a17 = part.mediaType) == null ? void 0 : _a17.startsWith("image/")) {
2488
2497
  const url = getFileUrl({
2489
2498
  part,
2490
2499
  defaultMediaType: "image/jpeg"
2491
2500
  });
2492
- return {
2501
+ return __spreadValues({
2493
2502
  type: "image_url",
2494
2503
  image_url: {
2495
2504
  url
2496
- },
2497
- // For image parts, use part-specific or message-level cache control
2498
- cache_control: cacheControl
2499
- };
2505
+ }
2506
+ }, cacheControl && { cache_control: cacheControl });
2500
2507
  }
2501
- if ((_c2 = part.mediaType) == null ? void 0 : _c2.startsWith("audio/")) {
2502
- return {
2508
+ if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("audio/")) {
2509
+ return __spreadValues({
2503
2510
  type: "input_audio",
2504
- input_audio: getInputAudioData(part),
2505
- cache_control: cacheControl
2506
- };
2511
+ input_audio: getInputAudioData(part)
2512
+ }, cacheControl && { cache_control: cacheControl });
2507
2513
  }
2508
2514
  const fileName = String(
2509
- (_g2 = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g2 : ""
2515
+ (_f2 = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f2 : ""
2510
2516
  );
2511
2517
  const fileData = getFileUrl({
2512
2518
  part,
@@ -2524,21 +2530,19 @@ function convertToOpenRouterChatMessages(prompt) {
2524
2530
  }
2525
2531
  };
2526
2532
  }
2527
- return {
2533
+ return __spreadValues({
2528
2534
  type: "file",
2529
2535
  file: {
2530
2536
  filename: fileName,
2531
2537
  file_data: fileData
2532
- },
2533
- cache_control: cacheControl
2534
- };
2538
+ }
2539
+ }, cacheControl && { cache_control: cacheControl });
2535
2540
  }
2536
2541
  default: {
2537
- return {
2542
+ return __spreadValues({
2538
2543
  type: "text",
2539
- text: "",
2540
- cache_control: cacheControl
2541
- };
2544
+ text: ""
2545
+ }, cacheControl && { cache_control: cacheControl });
2542
2546
  }
2543
2547
  }
2544
2548
  }
@@ -2582,8 +2586,8 @@ function convertToOpenRouterChatMessages(prompt) {
2582
2586
  }
2583
2587
  }
2584
2588
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
2585
- const messageReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
2586
- const messageAnnotations = parsedProviderOptions.success ? (_f = (_e = parsedProviderOptions.data) == null ? void 0 : _e.openrouter) == null ? void 0 : _f.annotations : void 0;
2589
+ const messageReasoningDetails = parsedProviderOptions.success ? (_e = (_d = parsedProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details : void 0;
2590
+ const messageAnnotations = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.annotations : void 0;
2587
2591
  const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2588
2592
  messages.push({
2589
2593
  role: "assistant",
@@ -2606,7 +2610,7 @@ function convertToOpenRouterChatMessages(prompt) {
2606
2610
  role: "tool",
2607
2611
  tool_call_id: toolResponse.toolCallId,
2608
2612
  content: content2,
2609
- cache_control: (_g = getCacheControl(providerOptions)) != null ? _g : getCacheControl(toolResponse.providerOptions)
2613
+ cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
2610
2614
  });
2611
2615
  }
2612
2616
  break;
@@ -2746,20 +2750,21 @@ var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
2746
2750
  type: import_v46.z.literal("function"),
2747
2751
  function: import_v46.z.object({
2748
2752
  name: import_v46.z.string(),
2749
- arguments: import_v46.z.string()
2753
+ arguments: import_v46.z.string().optional()
2750
2754
  }).passthrough()
2751
2755
  }).passthrough()
2752
2756
  ).optional(),
2753
2757
  annotations: import_v46.z.array(
2754
2758
  import_v46.z.union([
2755
2759
  // URL citation from web search
2760
+ // title, start_index, end_index are optional as some upstream providers may omit them
2756
2761
  import_v46.z.object({
2757
2762
  type: import_v46.z.literal("url_citation"),
2758
2763
  url_citation: import_v46.z.object({
2759
- end_index: import_v46.z.number(),
2760
- start_index: import_v46.z.number(),
2761
- title: import_v46.z.string(),
2762
2764
  url: import_v46.z.string(),
2765
+ title: import_v46.z.string().optional(),
2766
+ start_index: import_v46.z.number().optional(),
2767
+ end_index: import_v46.z.number().optional(),
2763
2768
  content: import_v46.z.string().optional()
2764
2769
  }).passthrough()
2765
2770
  }).passthrough(),
@@ -2836,13 +2841,14 @@ var OpenRouterStreamChatCompletionChunkSchema = import_v46.z.union([
2836
2841
  annotations: import_v46.z.array(
2837
2842
  import_v46.z.union([
2838
2843
  // URL citation from web search
2844
+ // title, start_index, end_index are optional as some upstream providers may omit them
2839
2845
  import_v46.z.object({
2840
2846
  type: import_v46.z.literal("url_citation"),
2841
2847
  url_citation: import_v46.z.object({
2842
- end_index: import_v46.z.number(),
2843
- start_index: import_v46.z.number(),
2844
- title: import_v46.z.string(),
2845
2848
  url: import_v46.z.string(),
2849
+ title: import_v46.z.string().optional(),
2850
+ start_index: import_v46.z.number().optional(),
2851
+ end_index: import_v46.z.number().optional(),
2846
2852
  content: import_v46.z.string().optional()
2847
2853
  }).passthrough()
2848
2854
  }).passthrough(),
@@ -2989,7 +2995,7 @@ var OpenRouterChatLanguageModel = class {
2989
2995
  return baseArgs;
2990
2996
  }
2991
2997
  async doGenerate(options) {
2992
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
2998
+ 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;
2993
2999
  const providerOptions = options.providerOptions || {};
2994
3000
  const openrouterOptions = providerOptions.openrouter || {};
2995
3001
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3039,7 +3045,8 @@ var OpenRouterChatLanguageModel = class {
3039
3045
  total: (_d = response.usage.completion_tokens) != null ? _d : 0,
3040
3046
  text: void 0,
3041
3047
  reasoning: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0
3042
- }
3048
+ },
3049
+ raw: response.usage
3043
3050
  } : {
3044
3051
  inputTokens: {
3045
3052
  total: 0,
@@ -3051,7 +3058,8 @@ var OpenRouterChatLanguageModel = class {
3051
3058
  total: 0,
3052
3059
  text: void 0,
3053
3060
  reasoning: void 0
3054
- }
3061
+ },
3062
+ raw: void 0
3055
3063
  };
3056
3064
  const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
3057
3065
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
@@ -3124,7 +3132,7 @@ var OpenRouterChatLanguageModel = class {
3124
3132
  type: "tool-call",
3125
3133
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3126
3134
  toolName: toolCall.function.name,
3127
- input: toolCall.function.arguments,
3135
+ input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3128
3136
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3129
3137
  openrouter: {
3130
3138
  reasoning_details: reasoningDetails
@@ -3151,17 +3159,19 @@ var OpenRouterChatLanguageModel = class {
3151
3159
  sourceType: "url",
3152
3160
  id: annotation.url_citation.url,
3153
3161
  url: annotation.url_citation.url,
3154
- title: annotation.url_citation.title,
3162
+ title: (_j = annotation.url_citation.title) != null ? _j : "",
3155
3163
  providerMetadata: {
3156
3164
  openrouter: {
3157
- content: annotation.url_citation.content || ""
3165
+ content: (_k = annotation.url_citation.content) != null ? _k : "",
3166
+ startIndex: (_l = annotation.url_citation.start_index) != null ? _l : 0,
3167
+ endIndex: (_m = annotation.url_citation.end_index) != null ? _m : 0
3158
3168
  }
3159
3169
  }
3160
3170
  });
3161
3171
  }
3162
3172
  }
3163
3173
  }
3164
- const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3174
+ const fileAnnotations = (_n = choice.message.annotations) == null ? void 0 : _n.filter(
3165
3175
  (a) => a.type === "file"
3166
3176
  );
3167
3177
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3169,7 +3179,7 @@ var OpenRouterChatLanguageModel = class {
3169
3179
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3170
3180
  );
3171
3181
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3172
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3182
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_o = choice.finish_reason) != null ? _o : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3173
3183
  return {
3174
3184
  content,
3175
3185
  finishReason: effectiveFinishReason,
@@ -3177,23 +3187,22 @@ var OpenRouterChatLanguageModel = class {
3177
3187
  warnings: [],
3178
3188
  providerMetadata: {
3179
3189
  openrouter: OpenRouterProviderMetadataSchema.parse({
3180
- provider: (_k = response.provider) != null ? _k : "",
3181
- reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3190
+ provider: (_p = response.provider) != null ? _p : "",
3191
+ reasoning_details: (_q = choice.message.reasoning_details) != null ? _q : [],
3182
3192
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3183
- usage: __spreadValues(__spreadValues(__spreadValues({
3184
- promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3185
- completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3186
- totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0),
3187
- cost: (_q = response.usage) == null ? void 0 : _q.cost
3188
- }, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
3193
+ usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3194
+ promptTokens: (_r = usageInfo.inputTokens.total) != null ? _r : 0,
3195
+ completionTokens: (_s = usageInfo.outputTokens.total) != null ? _s : 0,
3196
+ totalTokens: ((_t = usageInfo.inputTokens.total) != null ? _t : 0) + ((_u = usageInfo.outputTokens.total) != null ? _u : 0)
3197
+ }, ((_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 ? {
3189
3198
  promptTokensDetails: {
3190
3199
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3191
3200
  }
3192
- } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3201
+ } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
3193
3202
  completionTokensDetails: {
3194
3203
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3195
3204
  }
3196
- } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3205
+ } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
3197
3206
  costDetails: {
3198
3207
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3199
3208
  }
@@ -3246,9 +3255,11 @@ var OpenRouterChatLanguageModel = class {
3246
3255
  total: void 0,
3247
3256
  text: void 0,
3248
3257
  reasoning: void 0
3249
- }
3258
+ },
3259
+ raw: void 0
3250
3260
  };
3251
3261
  const openrouterUsage = {};
3262
+ let rawUsage;
3252
3263
  const accumulatedReasoningDetails = [];
3253
3264
  let reasoningDetailsAttachedToToolCall = false;
3254
3265
  const accumulatedFileAnnotations = [];
@@ -3262,7 +3273,10 @@ var OpenRouterChatLanguageModel = class {
3262
3273
  stream: response.pipeThrough(
3263
3274
  new TransformStream({
3264
3275
  transform(chunk, controller) {
3265
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
3276
+ var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
3277
+ if (options.includeRawChunks) {
3278
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3279
+ }
3266
3280
  if (!chunk.success) {
3267
3281
  finishReason = createFinishReason("error");
3268
3282
  controller.enqueue({ type: "error", error: chunk.error });
@@ -3293,6 +3307,7 @@ var OpenRouterChatLanguageModel = class {
3293
3307
  if (value.usage != null) {
3294
3308
  usage.inputTokens.total = value.usage.prompt_tokens;
3295
3309
  usage.outputTokens.total = value.usage.completion_tokens;
3310
+ rawUsage = value.usage;
3296
3311
  openrouterUsage.promptTokens = value.usage.prompt_tokens;
3297
3312
  if (value.usage.prompt_tokens_details) {
3298
3313
  const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
@@ -3309,7 +3324,9 @@ var OpenRouterChatLanguageModel = class {
3309
3324
  reasoningTokens
3310
3325
  };
3311
3326
  }
3312
- openrouterUsage.cost = value.usage.cost;
3327
+ if (value.usage.cost != null) {
3328
+ openrouterUsage.cost = value.usage.cost;
3329
+ }
3313
3330
  openrouterUsage.totalTokens = value.usage.total_tokens;
3314
3331
  const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
3315
3332
  if (upstreamInferenceCost != null) {
@@ -3422,10 +3439,12 @@ var OpenRouterChatLanguageModel = class {
3422
3439
  sourceType: "url",
3423
3440
  id: annotation.url_citation.url,
3424
3441
  url: annotation.url_citation.url,
3425
- title: annotation.url_citation.title,
3442
+ title: (_d = annotation.url_citation.title) != null ? _d : "",
3426
3443
  providerMetadata: {
3427
3444
  openrouter: {
3428
- content: annotation.url_citation.content || ""
3445
+ content: (_e = annotation.url_citation.content) != null ? _e : "",
3446
+ startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3447
+ endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3429
3448
  }
3430
3449
  }
3431
3450
  });
@@ -3441,7 +3460,7 @@ var OpenRouterChatLanguageModel = class {
3441
3460
  }
3442
3461
  if (delta.tool_calls != null) {
3443
3462
  for (const toolCallDelta of delta.tool_calls) {
3444
- const index = (_d = toolCallDelta.index) != null ? _d : toolCalls.length - 1;
3463
+ const index = (_h = toolCallDelta.index) != null ? _h : toolCalls.length - 1;
3445
3464
  if (toolCalls[index] == null) {
3446
3465
  if (toolCallDelta.type !== "function") {
3447
3466
  throw new InvalidResponseDataError({
@@ -3455,7 +3474,7 @@ var OpenRouterChatLanguageModel = class {
3455
3474
  message: `Expected 'id' to be a string.`
3456
3475
  });
3457
3476
  }
3458
- if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
3477
+ if (((_i = toolCallDelta.function) == null ? void 0 : _i.name) == null) {
3459
3478
  throw new InvalidResponseDataError({
3460
3479
  data: toolCallDelta,
3461
3480
  message: `Expected 'function.name' to be a string.`
@@ -3466,7 +3485,7 @@ var OpenRouterChatLanguageModel = class {
3466
3485
  type: "function",
3467
3486
  function: {
3468
3487
  name: toolCallDelta.function.name,
3469
- arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
3488
+ arguments: (_j = toolCallDelta.function.arguments) != null ? _j : ""
3470
3489
  },
3471
3490
  inputStarted: false,
3472
3491
  sent: false
@@ -3478,7 +3497,7 @@ var OpenRouterChatLanguageModel = class {
3478
3497
  message: `Tool call at index ${index} is missing after creation.`
3479
3498
  });
3480
3499
  }
3481
- if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3500
+ if (((_k = toolCall2.function) == null ? void 0 : _k.name) != null && ((_l = toolCall2.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3482
3501
  toolCall2.inputStarted = true;
3483
3502
  controller.enqueue({
3484
3503
  type: "tool-input-start",
@@ -3529,18 +3548,18 @@ var OpenRouterChatLanguageModel = class {
3529
3548
  toolName: toolCall.function.name
3530
3549
  });
3531
3550
  }
3532
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
3533
- toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
3551
+ if (((_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null) {
3552
+ toolCall.function.arguments += (_o = (_n = toolCallDelta.function) == null ? void 0 : _n.arguments) != null ? _o : "";
3534
3553
  }
3535
3554
  controller.enqueue({
3536
3555
  type: "tool-input-delta",
3537
3556
  id: toolCall.id,
3538
- delta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
3557
+ delta: (_p = toolCallDelta.function.arguments) != null ? _p : ""
3539
3558
  });
3540
- if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3559
+ if (((_q = toolCall.function) == null ? void 0 : _q.name) != null && ((_r = toolCall.function) == null ? void 0 : _r.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3541
3560
  controller.enqueue({
3542
3561
  type: "tool-call",
3543
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
3562
+ toolCallId: (_s = toolCall.id) != null ? _s : generateId(),
3544
3563
  toolName: toolCall.function.name,
3545
3564
  input: toolCall.function.arguments,
3546
3565
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
@@ -3617,6 +3636,7 @@ var OpenRouterChatLanguageModel = class {
3617
3636
  if (accumulatedFileAnnotations.length > 0) {
3618
3637
  openrouterMetadata.annotations = accumulatedFileAnnotations;
3619
3638
  }
3639
+ usage.raw = rawUsage;
3620
3640
  controller.enqueue({
3621
3641
  type: "finish",
3622
3642
  finishReason,
@@ -3744,6 +3764,7 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
3744
3764
  import_v47.z.object({
3745
3765
  id: import_v47.z.string().optional(),
3746
3766
  model: import_v47.z.string().optional(),
3767
+ provider: import_v47.z.string().optional(),
3747
3768
  choices: import_v47.z.array(
3748
3769
  import_v47.z.object({
3749
3770
  text: import_v47.z.string(),
@@ -3851,7 +3872,7 @@ var OpenRouterCompletionLanguageModel = class {
3851
3872
  }, this.config.extraBody), this.settings.extraBody);
3852
3873
  }
3853
3874
  async doGenerate(options) {
3854
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3875
+ 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;
3855
3876
  const providerOptions = options.providerOptions || {};
3856
3877
  const openrouterOptions = providerOptions.openrouter || {};
3857
3878
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3908,9 +3929,32 @@ var OpenRouterCompletionLanguageModel = class {
3908
3929
  total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0,
3909
3930
  text: void 0,
3910
3931
  reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
3911
- }
3932
+ },
3933
+ raw: (_l = response.usage) != null ? _l : void 0
3912
3934
  },
3913
3935
  warnings: [],
3936
+ providerMetadata: {
3937
+ openrouter: OpenRouterProviderMetadataSchema.parse({
3938
+ provider: (_m = response.provider) != null ? _m : "",
3939
+ usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3940
+ promptTokens: (_o = (_n = response.usage) == null ? void 0 : _n.prompt_tokens) != null ? _o : 0,
3941
+ completionTokens: (_q = (_p = response.usage) == null ? void 0 : _p.completion_tokens) != null ? _q : 0,
3942
+ 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)
3943
+ }, ((_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 ? {
3944
+ promptTokensDetails: {
3945
+ cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3946
+ }
3947
+ } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
3948
+ completionTokensDetails: {
3949
+ reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3950
+ }
3951
+ } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
3952
+ costDetails: {
3953
+ upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3954
+ }
3955
+ } : {})
3956
+ })
3957
+ },
3914
3958
  response: {
3915
3959
  headers: responseHeaders
3916
3960
  }
@@ -3950,14 +3994,20 @@ var OpenRouterCompletionLanguageModel = class {
3950
3994
  total: void 0,
3951
3995
  text: void 0,
3952
3996
  reasoning: void 0
3953
- }
3997
+ },
3998
+ raw: void 0
3954
3999
  };
3955
4000
  const openrouterUsage = {};
4001
+ let provider;
4002
+ let rawUsage;
3956
4003
  return {
3957
4004
  stream: response.pipeThrough(
3958
4005
  new TransformStream({
3959
4006
  transform(chunk, controller) {
3960
4007
  var _a16, _b16, _c;
4008
+ if (options.includeRawChunks) {
4009
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4010
+ }
3961
4011
  if (!chunk.success) {
3962
4012
  finishReason = createFinishReason("error");
3963
4013
  controller.enqueue({ type: "error", error: chunk.error });
@@ -3969,9 +4019,13 @@ var OpenRouterCompletionLanguageModel = class {
3969
4019
  controller.enqueue({ type: "error", error: value.error });
3970
4020
  return;
3971
4021
  }
4022
+ if (value.provider) {
4023
+ provider = value.provider;
4024
+ }
3972
4025
  if (value.usage != null) {
3973
4026
  usage.inputTokens.total = value.usage.prompt_tokens;
3974
4027
  usage.outputTokens.total = value.usage.completion_tokens;
4028
+ rawUsage = value.usage;
3975
4029
  openrouterUsage.promptTokens = value.usage.prompt_tokens;
3976
4030
  if (value.usage.prompt_tokens_details) {
3977
4031
  const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
@@ -3988,7 +4042,9 @@ var OpenRouterCompletionLanguageModel = class {
3988
4042
  reasoningTokens
3989
4043
  };
3990
4044
  }
3991
- openrouterUsage.cost = value.usage.cost;
4045
+ if (value.usage.cost != null) {
4046
+ openrouterUsage.cost = value.usage.cost;
4047
+ }
3992
4048
  openrouterUsage.totalTokens = value.usage.total_tokens;
3993
4049
  const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
3994
4050
  if (upstreamInferenceCost != null) {
@@ -4010,14 +4066,19 @@ var OpenRouterCompletionLanguageModel = class {
4010
4066
  }
4011
4067
  },
4012
4068
  flush(controller) {
4069
+ usage.raw = rawUsage;
4070
+ const openrouterMetadata = {
4071
+ usage: openrouterUsage
4072
+ };
4073
+ if (provider !== void 0) {
4074
+ openrouterMetadata.provider = provider;
4075
+ }
4013
4076
  controller.enqueue({
4014
4077
  type: "finish",
4015
4078
  finishReason,
4016
4079
  usage,
4017
4080
  providerMetadata: {
4018
- openrouter: {
4019
- usage: openrouterUsage
4020
- }
4081
+ openrouter: openrouterMetadata
4021
4082
  }
4022
4083
  });
4023
4084
  }
@@ -4029,9 +4090,228 @@ var OpenRouterCompletionLanguageModel = class {
4029
4090
  };
4030
4091
  }
4031
4092
  };
4093
+
4094
+ // src/embedding/schemas.ts
4095
+ var import_v48 = require("zod/v4");
4096
+ var openrouterEmbeddingUsageSchema = import_v48.z.object({
4097
+ prompt_tokens: import_v48.z.number(),
4098
+ total_tokens: import_v48.z.number(),
4099
+ cost: import_v48.z.number().optional()
4100
+ });
4101
+ var openrouterEmbeddingDataSchema = import_v48.z.object({
4102
+ object: import_v48.z.literal("embedding"),
4103
+ embedding: import_v48.z.array(import_v48.z.number()),
4104
+ index: import_v48.z.number().optional()
4105
+ });
4106
+ var OpenRouterEmbeddingResponseSchema = import_v48.z.object({
4107
+ id: import_v48.z.string().optional(),
4108
+ object: import_v48.z.literal("list"),
4109
+ data: import_v48.z.array(openrouterEmbeddingDataSchema),
4110
+ model: import_v48.z.string(),
4111
+ provider: import_v48.z.string().optional(),
4112
+ usage: openrouterEmbeddingUsageSchema.optional()
4113
+ });
4114
+
4115
+ // src/embedding/index.ts
4116
+ var OpenRouterEmbeddingModel = class {
4117
+ constructor(modelId, settings, config) {
4118
+ this.specificationVersion = "v3";
4119
+ this.provider = "openrouter";
4120
+ this.maxEmbeddingsPerCall = void 0;
4121
+ this.supportsParallelCalls = true;
4122
+ this.modelId = modelId;
4123
+ this.settings = settings;
4124
+ this.config = config;
4125
+ }
4126
+ async doEmbed(options) {
4127
+ var _a16, _b16, _c, _d, _e, _f;
4128
+ const { values, abortSignal, headers } = options;
4129
+ const args = __spreadValues(__spreadValues({
4130
+ model: this.modelId,
4131
+ input: values,
4132
+ user: this.settings.user,
4133
+ provider: this.settings.provider
4134
+ }, this.config.extraBody), this.settings.extraBody);
4135
+ const { value: responseValue, responseHeaders } = await postJsonToApi({
4136
+ url: this.config.url({
4137
+ path: "/embeddings",
4138
+ modelId: this.modelId
4139
+ }),
4140
+ headers: combineHeaders(this.config.headers(), headers),
4141
+ body: args,
4142
+ failedResponseHandler: openrouterFailedResponseHandler,
4143
+ successfulResponseHandler: createJsonResponseHandler(
4144
+ OpenRouterEmbeddingResponseSchema
4145
+ ),
4146
+ abortSignal,
4147
+ fetch: this.config.fetch
4148
+ });
4149
+ return {
4150
+ embeddings: responseValue.data.map((item) => item.embedding),
4151
+ usage: responseValue.usage ? { tokens: responseValue.usage.prompt_tokens } : void 0,
4152
+ providerMetadata: {
4153
+ openrouter: OpenRouterProviderMetadataSchema.parse({
4154
+ provider: (_a16 = responseValue.provider) != null ? _a16 : "",
4155
+ usage: __spreadValues({
4156
+ promptTokens: (_c = (_b16 = responseValue.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
4157
+ completionTokens: 0,
4158
+ totalTokens: (_e = (_d = responseValue.usage) == null ? void 0 : _d.total_tokens) != null ? _e : 0
4159
+ }, ((_f = responseValue.usage) == null ? void 0 : _f.cost) != null ? { cost: responseValue.usage.cost } : {})
4160
+ })
4161
+ },
4162
+ response: {
4163
+ headers: responseHeaders,
4164
+ body: responseValue
4165
+ },
4166
+ warnings: []
4167
+ };
4168
+ }
4169
+ };
4170
+
4171
+ // src/image/schemas.ts
4172
+ var import_v49 = require("zod/v4");
4173
+ var OpenRouterImageResponseSchema = import_v49.z.object({
4174
+ id: import_v49.z.string().optional(),
4175
+ object: import_v49.z.string().optional(),
4176
+ created: import_v49.z.number().optional(),
4177
+ model: import_v49.z.string(),
4178
+ choices: import_v49.z.array(
4179
+ import_v49.z.object({
4180
+ index: import_v49.z.number(),
4181
+ message: import_v49.z.object({
4182
+ role: import_v49.z.string(),
4183
+ content: import_v49.z.string().nullable().optional(),
4184
+ images: import_v49.z.array(
4185
+ import_v49.z.object({
4186
+ type: import_v49.z.literal("image_url"),
4187
+ image_url: import_v49.z.object({
4188
+ url: import_v49.z.string()
4189
+ })
4190
+ }).passthrough()
4191
+ ).optional()
4192
+ }).passthrough(),
4193
+ finish_reason: import_v49.z.string().nullable().optional()
4194
+ }).passthrough()
4195
+ ),
4196
+ usage: import_v49.z.object({
4197
+ prompt_tokens: import_v49.z.number(),
4198
+ completion_tokens: import_v49.z.number(),
4199
+ total_tokens: import_v49.z.number()
4200
+ }).passthrough().optional()
4201
+ }).passthrough();
4202
+
4203
+ // src/image/index.ts
4204
+ var OpenRouterImageModel = class {
4205
+ constructor(modelId, settings, config) {
4206
+ this.specificationVersion = "v3";
4207
+ this.provider = "openrouter";
4208
+ this.maxImagesPerCall = 1;
4209
+ this.modelId = modelId;
4210
+ this.settings = settings;
4211
+ this.config = config;
4212
+ }
4213
+ async doGenerate(options) {
4214
+ var _a16;
4215
+ const {
4216
+ prompt,
4217
+ n,
4218
+ size,
4219
+ aspectRatio,
4220
+ seed,
4221
+ files,
4222
+ mask,
4223
+ abortSignal,
4224
+ headers,
4225
+ providerOptions
4226
+ } = options;
4227
+ const openrouterOptions = (providerOptions == null ? void 0 : providerOptions.openrouter) || {};
4228
+ const warnings = [];
4229
+ if (files !== void 0 && files.length > 0) {
4230
+ throw new UnsupportedFunctionalityError({
4231
+ functionality: "image editing (files parameter)"
4232
+ });
4233
+ }
4234
+ if (mask !== void 0) {
4235
+ throw new UnsupportedFunctionalityError({
4236
+ functionality: "image inpainting (mask parameter)"
4237
+ });
4238
+ }
4239
+ if (n > 1) {
4240
+ warnings.push({
4241
+ type: "unsupported",
4242
+ feature: "n > 1",
4243
+ details: `OpenRouter image generation returns 1 image per call. Requested ${n} images.`
4244
+ });
4245
+ }
4246
+ if (size !== void 0) {
4247
+ warnings.push({
4248
+ type: "unsupported",
4249
+ feature: "size",
4250
+ details: "Use aspectRatio instead. Size parameter is not supported by OpenRouter image generation."
4251
+ });
4252
+ }
4253
+ const imageConfig = aspectRatio !== void 0 ? { aspect_ratio: aspectRatio } : void 0;
4254
+ const body = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
4255
+ model: this.modelId,
4256
+ messages: [
4257
+ {
4258
+ role: "user",
4259
+ content: prompt != null ? prompt : ""
4260
+ }
4261
+ ],
4262
+ modalities: ["image", "text"]
4263
+ }, imageConfig !== void 0 && { image_config: imageConfig }), seed !== void 0 && { seed }), this.settings.user !== void 0 && { user: this.settings.user }), this.settings.provider !== void 0 && {
4264
+ provider: this.settings.provider
4265
+ }), this.config.extraBody), this.settings.extraBody), openrouterOptions);
4266
+ const { value: responseValue, responseHeaders } = await postJsonToApi({
4267
+ url: this.config.url({
4268
+ path: "/chat/completions",
4269
+ modelId: this.modelId
4270
+ }),
4271
+ headers: combineHeaders(this.config.headers(), headers),
4272
+ body,
4273
+ failedResponseHandler: openrouterFailedResponseHandler,
4274
+ successfulResponseHandler: createJsonResponseHandler(
4275
+ OpenRouterImageResponseSchema
4276
+ ),
4277
+ abortSignal,
4278
+ fetch: this.config.fetch
4279
+ });
4280
+ const choice = responseValue.choices[0];
4281
+ if (!choice) {
4282
+ throw new NoContentGeneratedError({
4283
+ message: "No choice in response"
4284
+ });
4285
+ }
4286
+ const images = [];
4287
+ if ((_a16 = choice.message) == null ? void 0 : _a16.images) {
4288
+ for (const image of choice.message.images) {
4289
+ const dataUrl = image.image_url.url;
4290
+ images.push(getBase64FromDataUrl(dataUrl));
4291
+ }
4292
+ }
4293
+ const usage = responseValue.usage ? {
4294
+ inputTokens: responseValue.usage.prompt_tokens,
4295
+ outputTokens: responseValue.usage.completion_tokens,
4296
+ totalTokens: responseValue.usage.total_tokens
4297
+ } : void 0;
4298
+ return {
4299
+ images,
4300
+ warnings,
4301
+ response: {
4302
+ timestamp: /* @__PURE__ */ new Date(),
4303
+ modelId: responseValue.model,
4304
+ headers: responseHeaders
4305
+ },
4306
+ usage
4307
+ };
4308
+ }
4309
+ };
4032
4310
  // Annotate the CommonJS export names for ESM import in node:
4033
4311
  0 && (module.exports = {
4034
4312
  OpenRouterChatLanguageModel,
4035
- OpenRouterCompletionLanguageModel
4313
+ OpenRouterCompletionLanguageModel,
4314
+ OpenRouterEmbeddingModel,
4315
+ OpenRouterImageModel
4036
4316
  });
4037
4317
  //# sourceMappingURL=index.js.map