@openrouter/ai-sdk-provider 2.0.1 → 2.0.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.
@@ -2442,7 +2442,7 @@ function getCacheControl(providerMetadata) {
2442
2442
  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
2443
  }
2444
2444
  function convertToOpenRouterChatMessages(prompt) {
2445
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j;
2445
+ var _a16, _b16, _c, _d, _e, _f, _g, _h;
2446
2446
  const messages = [];
2447
2447
  for (const { role, content, providerOptions } of prompt) {
2448
2448
  switch (role) {
@@ -2471,42 +2471,46 @@ function convertToOpenRouterChatMessages(prompt) {
2471
2471
  break;
2472
2472
  }
2473
2473
  const messageCacheControl = getCacheControl(providerOptions);
2474
+ let lastTextPartIndex = -1;
2475
+ for (let i = content.length - 1; i >= 0; i--) {
2476
+ if (((_c = content[i]) == null ? void 0 : _c.type) === "text") {
2477
+ lastTextPartIndex = i;
2478
+ break;
2479
+ }
2480
+ }
2474
2481
  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;
2482
+ (part, index) => {
2483
+ var _a17, _b17, _c2, _d2, _e2, _f2;
2484
+ const isLastTextPart = part.type === "text" && index === lastTextPartIndex;
2485
+ const partCacheControl = getCacheControl(part.providerOptions);
2486
+ const cacheControl = part.type === "text" ? partCacheControl != null ? partCacheControl : isLastTextPart ? messageCacheControl : void 0 : partCacheControl;
2478
2487
  switch (part.type) {
2479
2488
  case "text":
2480
- return {
2489
+ return __spreadValues({
2481
2490
  type: "text",
2482
- text: part.text,
2483
- // For text parts, only use part-specific cache control
2484
- cache_control: cacheControl
2485
- };
2491
+ text: part.text
2492
+ }, cacheControl && { cache_control: cacheControl });
2486
2493
  case "file": {
2487
- if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("image/")) {
2494
+ if ((_a17 = part.mediaType) == null ? void 0 : _a17.startsWith("image/")) {
2488
2495
  const url = getFileUrl({
2489
2496
  part,
2490
2497
  defaultMediaType: "image/jpeg"
2491
2498
  });
2492
- return {
2499
+ return __spreadValues({
2493
2500
  type: "image_url",
2494
2501
  image_url: {
2495
2502
  url
2496
- },
2497
- // For image parts, use part-specific or message-level cache control
2498
- cache_control: cacheControl
2499
- };
2503
+ }
2504
+ }, cacheControl && { cache_control: cacheControl });
2500
2505
  }
2501
- if ((_c2 = part.mediaType) == null ? void 0 : _c2.startsWith("audio/")) {
2502
- return {
2506
+ if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("audio/")) {
2507
+ return __spreadValues({
2503
2508
  type: "input_audio",
2504
- input_audio: getInputAudioData(part),
2505
- cache_control: cacheControl
2506
- };
2509
+ input_audio: getInputAudioData(part)
2510
+ }, cacheControl && { cache_control: cacheControl });
2507
2511
  }
2508
2512
  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 : ""
2513
+ (_f2 = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f2 : ""
2510
2514
  );
2511
2515
  const fileData = getFileUrl({
2512
2516
  part,
@@ -2524,21 +2528,19 @@ function convertToOpenRouterChatMessages(prompt) {
2524
2528
  }
2525
2529
  };
2526
2530
  }
2527
- return {
2531
+ return __spreadValues({
2528
2532
  type: "file",
2529
2533
  file: {
2530
2534
  filename: fileName,
2531
2535
  file_data: fileData
2532
- },
2533
- cache_control: cacheControl
2534
- };
2536
+ }
2537
+ }, cacheControl && { cache_control: cacheControl });
2535
2538
  }
2536
2539
  default: {
2537
- return {
2540
+ return __spreadValues({
2538
2541
  type: "text",
2539
- text: "",
2540
- cache_control: cacheControl
2541
- };
2542
+ text: ""
2543
+ }, cacheControl && { cache_control: cacheControl });
2542
2544
  }
2543
2545
  }
2544
2546
  }
@@ -2553,7 +2555,6 @@ function convertToOpenRouterChatMessages(prompt) {
2553
2555
  let text = "";
2554
2556
  let reasoning = "";
2555
2557
  const toolCalls = [];
2556
- const accumulatedReasoningDetails = [];
2557
2558
  for (const part of content) {
2558
2559
  switch (part.type) {
2559
2560
  case "text": {
@@ -2561,12 +2562,6 @@ function convertToOpenRouterChatMessages(prompt) {
2561
2562
  break;
2562
2563
  }
2563
2564
  case "tool-call": {
2564
- const partReasoningDetails = (_c = part.providerOptions) == null ? void 0 : _c.openrouter;
2565
- if ((partReasoningDetails == null ? void 0 : partReasoningDetails.reasoning_details) && Array.isArray(partReasoningDetails.reasoning_details)) {
2566
- accumulatedReasoningDetails.push(
2567
- ...partReasoningDetails.reasoning_details
2568
- );
2569
- }
2570
2565
  toolCalls.push({
2571
2566
  id: part.toolCallId,
2572
2567
  type: "function",
@@ -2579,12 +2574,6 @@ function convertToOpenRouterChatMessages(prompt) {
2579
2574
  }
2580
2575
  case "reasoning": {
2581
2576
  reasoning += part.text;
2582
- const parsedPartProviderOptions = OpenRouterProviderOptionsSchema.safeParse(part.providerOptions);
2583
- if (parsedPartProviderOptions.success && ((_e = (_d = parsedPartProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details)) {
2584
- accumulatedReasoningDetails.push(
2585
- ...parsedPartProviderOptions.data.openrouter.reasoning_details
2586
- );
2587
- }
2588
2577
  break;
2589
2578
  }
2590
2579
  case "file":
@@ -2595,9 +2584,9 @@ function convertToOpenRouterChatMessages(prompt) {
2595
2584
  }
2596
2585
  }
2597
2586
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
2598
- const messageReasoningDetails = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.reasoning_details : void 0;
2599
- const messageAnnotations = parsedProviderOptions.success ? (_i = (_h = parsedProviderOptions.data) == null ? void 0 : _h.openrouter) == null ? void 0 : _i.annotations : void 0;
2600
- const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : accumulatedReasoningDetails.length > 0 ? accumulatedReasoningDetails : void 0;
2587
+ const messageReasoningDetails = parsedProviderOptions.success ? (_e = (_d = parsedProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details : void 0;
2588
+ const messageAnnotations = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.annotations : void 0;
2589
+ const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2601
2590
  messages.push({
2602
2591
  role: "assistant",
2603
2592
  content: text,
@@ -2619,7 +2608,7 @@ function convertToOpenRouterChatMessages(prompt) {
2619
2608
  role: "tool",
2620
2609
  tool_call_id: toolResponse.toolCallId,
2621
2610
  content: content2,
2622
- cache_control: (_j = getCacheControl(providerOptions)) != null ? _j : getCacheControl(toolResponse.providerOptions)
2611
+ cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
2623
2612
  });
2624
2613
  }
2625
2614
  break;
@@ -2645,6 +2634,29 @@ function getToolResultContent(input) {
2645
2634
  return (_a16 = input.output.reason) != null ? _a16 : "Tool execution denied";
2646
2635
  }
2647
2636
  }
2637
+ function findFirstReasoningDetails(content) {
2638
+ var _a16, _b16, _c;
2639
+ for (const part of content) {
2640
+ if (part.type === "tool-call") {
2641
+ const openrouter = (_a16 = part.providerOptions) == null ? void 0 : _a16.openrouter;
2642
+ const details = openrouter == null ? void 0 : openrouter.reasoning_details;
2643
+ if (Array.isArray(details) && details.length > 0) {
2644
+ return details;
2645
+ }
2646
+ }
2647
+ }
2648
+ for (const part of content) {
2649
+ if (part.type === "reasoning") {
2650
+ const parsed = OpenRouterProviderOptionsSchema.safeParse(
2651
+ part.providerOptions
2652
+ );
2653
+ if (parsed.success && ((_c = (_b16 = parsed.data) == null ? void 0 : _b16.openrouter) == null ? void 0 : _c.reasoning_details) && parsed.data.openrouter.reasoning_details.length > 0) {
2654
+ return parsed.data.openrouter.reasoning_details;
2655
+ }
2656
+ }
2657
+ }
2658
+ return void 0;
2659
+ }
2648
2660
 
2649
2661
  // src/chat/get-tool-choice.ts
2650
2662
  var import_v44 = require("zod/v4");
@@ -2736,7 +2748,7 @@ var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
2736
2748
  type: import_v46.z.literal("function"),
2737
2749
  function: import_v46.z.object({
2738
2750
  name: import_v46.z.string(),
2739
- arguments: import_v46.z.string()
2751
+ arguments: import_v46.z.string().optional()
2740
2752
  }).passthrough()
2741
2753
  }).passthrough()
2742
2754
  ).optional(),
@@ -2979,7 +2991,7 @@ var OpenRouterChatLanguageModel = class {
2979
2991
  return baseArgs;
2980
2992
  }
2981
2993
  async doGenerate(options) {
2982
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
2994
+ 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;
2983
2995
  const providerOptions = options.providerOptions || {};
2984
2996
  const openrouterOptions = providerOptions.openrouter || {};
2985
2997
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3108,18 +3120,20 @@ var OpenRouterChatLanguageModel = class {
3108
3120
  });
3109
3121
  }
3110
3122
  if (choice.message.tool_calls) {
3123
+ let reasoningDetailsAttachedToToolCall = false;
3111
3124
  for (const toolCall of choice.message.tool_calls) {
3112
3125
  content.push({
3113
3126
  type: "tool-call",
3114
3127
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3115
3128
  toolName: toolCall.function.name,
3116
- input: toolCall.function.arguments,
3117
- providerMetadata: {
3129
+ input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3130
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3118
3131
  openrouter: {
3119
3132
  reasoning_details: reasoningDetails
3120
3133
  }
3121
- }
3134
+ } : void 0
3122
3135
  });
3136
+ reasoningDetailsAttachedToToolCall = true;
3123
3137
  }
3124
3138
  }
3125
3139
  if (choice.message.images) {
@@ -3149,7 +3163,7 @@ var OpenRouterChatLanguageModel = class {
3149
3163
  }
3150
3164
  }
3151
3165
  }
3152
- const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3166
+ const fileAnnotations = (_j = choice.message.annotations) == null ? void 0 : _j.filter(
3153
3167
  (a) => a.type === "file"
3154
3168
  );
3155
3169
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3157,7 +3171,7 @@ var OpenRouterChatLanguageModel = class {
3157
3171
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3158
3172
  );
3159
3173
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3160
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3174
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_k = choice.finish_reason) != null ? _k : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3161
3175
  return {
3162
3176
  content,
3163
3177
  finishReason: effectiveFinishReason,
@@ -3165,23 +3179,23 @@ var OpenRouterChatLanguageModel = class {
3165
3179
  warnings: [],
3166
3180
  providerMetadata: {
3167
3181
  openrouter: OpenRouterProviderMetadataSchema.parse({
3168
- provider: (_k = response.provider) != null ? _k : "",
3169
- reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3182
+ provider: (_l = response.provider) != null ? _l : "",
3183
+ reasoning_details: (_m = choice.message.reasoning_details) != null ? _m : [],
3170
3184
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3171
3185
  usage: __spreadValues(__spreadValues(__spreadValues({
3172
- promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3173
- completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3174
- totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0),
3175
- cost: (_q = response.usage) == null ? void 0 : _q.cost
3176
- }, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
3186
+ promptTokens: (_n = usageInfo.inputTokens.total) != null ? _n : 0,
3187
+ completionTokens: (_o = usageInfo.outputTokens.total) != null ? _o : 0,
3188
+ totalTokens: ((_p = usageInfo.inputTokens.total) != null ? _p : 0) + ((_q = usageInfo.outputTokens.total) != null ? _q : 0),
3189
+ cost: (_r = response.usage) == null ? void 0 : _r.cost
3190
+ }, ((_t = (_s = response.usage) == null ? void 0 : _s.prompt_tokens_details) == null ? void 0 : _t.cached_tokens) != null ? {
3177
3191
  promptTokensDetails: {
3178
3192
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3179
3193
  }
3180
- } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3194
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.completion_tokens_details) == null ? void 0 : _v.reasoning_tokens) != null ? {
3181
3195
  completionTokensDetails: {
3182
3196
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3183
3197
  }
3184
- } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3198
+ } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? {
3185
3199
  costDetails: {
3186
3200
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3187
3201
  }
@@ -3238,6 +3252,7 @@ var OpenRouterChatLanguageModel = class {
3238
3252
  };
3239
3253
  const openrouterUsage = {};
3240
3254
  const accumulatedReasoningDetails = [];
3255
+ let reasoningDetailsAttachedToToolCall = false;
3241
3256
  const accumulatedFileAnnotations = [];
3242
3257
  let textStarted = false;
3243
3258
  let reasoningStarted = false;
@@ -3486,12 +3501,13 @@ var OpenRouterChatLanguageModel = class {
3486
3501
  toolCallId: toolCall2.id,
3487
3502
  toolName: toolCall2.function.name,
3488
3503
  input: toolCall2.function.arguments,
3489
- providerMetadata: {
3504
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3490
3505
  openrouter: {
3491
3506
  reasoning_details: accumulatedReasoningDetails
3492
3507
  }
3493
- }
3508
+ } : void 0
3494
3509
  });
3510
+ reasoningDetailsAttachedToToolCall = true;
3495
3511
  toolCall2.sent = true;
3496
3512
  }
3497
3513
  continue;
@@ -3529,12 +3545,13 @@ var OpenRouterChatLanguageModel = class {
3529
3545
  toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
3530
3546
  toolName: toolCall.function.name,
3531
3547
  input: toolCall.function.arguments,
3532
- providerMetadata: {
3548
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3533
3549
  openrouter: {
3534
3550
  reasoning_details: accumulatedReasoningDetails
3535
3551
  }
3536
- }
3552
+ } : void 0
3537
3553
  });
3554
+ reasoningDetailsAttachedToToolCall = true;
3538
3555
  toolCall.sent = true;
3539
3556
  }
3540
3557
  }
@@ -3567,12 +3584,13 @@ var OpenRouterChatLanguageModel = class {
3567
3584
  toolName: toolCall.function.name,
3568
3585
  // Coerce invalid arguments to an empty JSON object
3569
3586
  input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}",
3570
- providerMetadata: {
3587
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3571
3588
  openrouter: {
3572
3589
  reasoning_details: accumulatedReasoningDetails
3573
3590
  }
3574
- }
3591
+ } : void 0
3575
3592
  });
3593
+ reasoningDetailsAttachedToToolCall = true;
3576
3594
  toolCall.sent = true;
3577
3595
  }
3578
3596
  }