@openrouter/ai-sdk-provider 2.0.1 → 2.0.2

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
@@ -2478,7 +2478,7 @@ function getCacheControl(providerMetadata) {
2478
2478
  return (_c = (_b16 = (_a16 = openrouter2 == null ? void 0 : openrouter2.cacheControl) != null ? _a16 : openrouter2 == null ? void 0 : openrouter2.cache_control) != null ? _b16 : anthropic == null ? void 0 : anthropic.cacheControl) != null ? _c : anthropic == null ? void 0 : anthropic.cache_control;
2479
2479
  }
2480
2480
  function convertToOpenRouterChatMessages(prompt) {
2481
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j;
2481
+ var _a16, _b16, _c, _d, _e, _f, _g;
2482
2482
  const messages = [];
2483
2483
  for (const { role, content, providerOptions } of prompt) {
2484
2484
  switch (role) {
@@ -2589,7 +2589,6 @@ function convertToOpenRouterChatMessages(prompt) {
2589
2589
  let text = "";
2590
2590
  let reasoning = "";
2591
2591
  const toolCalls = [];
2592
- const accumulatedReasoningDetails = [];
2593
2592
  for (const part of content) {
2594
2593
  switch (part.type) {
2595
2594
  case "text": {
@@ -2597,12 +2596,6 @@ function convertToOpenRouterChatMessages(prompt) {
2597
2596
  break;
2598
2597
  }
2599
2598
  case "tool-call": {
2600
- const partReasoningDetails = (_c = part.providerOptions) == null ? void 0 : _c.openrouter;
2601
- if ((partReasoningDetails == null ? void 0 : partReasoningDetails.reasoning_details) && Array.isArray(partReasoningDetails.reasoning_details)) {
2602
- accumulatedReasoningDetails.push(
2603
- ...partReasoningDetails.reasoning_details
2604
- );
2605
- }
2606
2599
  toolCalls.push({
2607
2600
  id: part.toolCallId,
2608
2601
  type: "function",
@@ -2615,12 +2608,6 @@ function convertToOpenRouterChatMessages(prompt) {
2615
2608
  }
2616
2609
  case "reasoning": {
2617
2610
  reasoning += part.text;
2618
- const parsedPartProviderOptions = OpenRouterProviderOptionsSchema.safeParse(part.providerOptions);
2619
- if (parsedPartProviderOptions.success && ((_e = (_d = parsedPartProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details)) {
2620
- accumulatedReasoningDetails.push(
2621
- ...parsedPartProviderOptions.data.openrouter.reasoning_details
2622
- );
2623
- }
2624
2611
  break;
2625
2612
  }
2626
2613
  case "file":
@@ -2631,9 +2618,9 @@ function convertToOpenRouterChatMessages(prompt) {
2631
2618
  }
2632
2619
  }
2633
2620
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
2634
- const messageReasoningDetails = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.reasoning_details : void 0;
2635
- const messageAnnotations = parsedProviderOptions.success ? (_i = (_h = parsedProviderOptions.data) == null ? void 0 : _h.openrouter) == null ? void 0 : _i.annotations : void 0;
2636
- const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : accumulatedReasoningDetails.length > 0 ? accumulatedReasoningDetails : void 0;
2621
+ const messageReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
2622
+ const messageAnnotations = parsedProviderOptions.success ? (_f = (_e = parsedProviderOptions.data) == null ? void 0 : _e.openrouter) == null ? void 0 : _f.annotations : void 0;
2623
+ const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2637
2624
  messages.push({
2638
2625
  role: "assistant",
2639
2626
  content: text,
@@ -2655,7 +2642,7 @@ function convertToOpenRouterChatMessages(prompt) {
2655
2642
  role: "tool",
2656
2643
  tool_call_id: toolResponse.toolCallId,
2657
2644
  content: content2,
2658
- cache_control: (_j = getCacheControl(providerOptions)) != null ? _j : getCacheControl(toolResponse.providerOptions)
2645
+ cache_control: (_g = getCacheControl(providerOptions)) != null ? _g : getCacheControl(toolResponse.providerOptions)
2659
2646
  });
2660
2647
  }
2661
2648
  break;
@@ -2681,6 +2668,29 @@ function getToolResultContent(input) {
2681
2668
  return (_a16 = input.output.reason) != null ? _a16 : "Tool execution denied";
2682
2669
  }
2683
2670
  }
2671
+ function findFirstReasoningDetails(content) {
2672
+ var _a16, _b16, _c;
2673
+ for (const part of content) {
2674
+ if (part.type === "tool-call") {
2675
+ const openrouter2 = (_a16 = part.providerOptions) == null ? void 0 : _a16.openrouter;
2676
+ const details = openrouter2 == null ? void 0 : openrouter2.reasoning_details;
2677
+ if (Array.isArray(details) && details.length > 0) {
2678
+ return details;
2679
+ }
2680
+ }
2681
+ }
2682
+ for (const part of content) {
2683
+ if (part.type === "reasoning") {
2684
+ const parsed = OpenRouterProviderOptionsSchema.safeParse(
2685
+ part.providerOptions
2686
+ );
2687
+ 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) {
2688
+ return parsed.data.openrouter.reasoning_details;
2689
+ }
2690
+ }
2691
+ }
2692
+ return void 0;
2693
+ }
2684
2694
 
2685
2695
  // src/chat/get-tool-choice.ts
2686
2696
  var import_v44 = require("zod/v4");
@@ -3144,18 +3154,20 @@ var OpenRouterChatLanguageModel = class {
3144
3154
  });
3145
3155
  }
3146
3156
  if (choice.message.tool_calls) {
3157
+ let reasoningDetailsAttachedToToolCall = false;
3147
3158
  for (const toolCall of choice.message.tool_calls) {
3148
3159
  content.push({
3149
3160
  type: "tool-call",
3150
3161
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3151
3162
  toolName: toolCall.function.name,
3152
3163
  input: toolCall.function.arguments,
3153
- providerMetadata: {
3164
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3154
3165
  openrouter: {
3155
3166
  reasoning_details: reasoningDetails
3156
3167
  }
3157
- }
3168
+ } : void 0
3158
3169
  });
3170
+ reasoningDetailsAttachedToToolCall = true;
3159
3171
  }
3160
3172
  }
3161
3173
  if (choice.message.images) {
@@ -3274,6 +3286,7 @@ var OpenRouterChatLanguageModel = class {
3274
3286
  };
3275
3287
  const openrouterUsage = {};
3276
3288
  const accumulatedReasoningDetails = [];
3289
+ let reasoningDetailsAttachedToToolCall = false;
3277
3290
  const accumulatedFileAnnotations = [];
3278
3291
  let textStarted = false;
3279
3292
  let reasoningStarted = false;
@@ -3522,12 +3535,13 @@ var OpenRouterChatLanguageModel = class {
3522
3535
  toolCallId: toolCall2.id,
3523
3536
  toolName: toolCall2.function.name,
3524
3537
  input: toolCall2.function.arguments,
3525
- providerMetadata: {
3538
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3526
3539
  openrouter: {
3527
3540
  reasoning_details: accumulatedReasoningDetails
3528
3541
  }
3529
- }
3542
+ } : void 0
3530
3543
  });
3544
+ reasoningDetailsAttachedToToolCall = true;
3531
3545
  toolCall2.sent = true;
3532
3546
  }
3533
3547
  continue;
@@ -3565,12 +3579,13 @@ var OpenRouterChatLanguageModel = class {
3565
3579
  toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
3566
3580
  toolName: toolCall.function.name,
3567
3581
  input: toolCall.function.arguments,
3568
- providerMetadata: {
3582
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3569
3583
  openrouter: {
3570
3584
  reasoning_details: accumulatedReasoningDetails
3571
3585
  }
3572
- }
3586
+ } : void 0
3573
3587
  });
3588
+ reasoningDetailsAttachedToToolCall = true;
3574
3589
  toolCall.sent = true;
3575
3590
  }
3576
3591
  }
@@ -3603,12 +3618,13 @@ var OpenRouterChatLanguageModel = class {
3603
3618
  toolName: toolCall.function.name,
3604
3619
  // Coerce invalid arguments to an empty JSON object
3605
3620
  input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}",
3606
- providerMetadata: {
3621
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3607
3622
  openrouter: {
3608
3623
  reasoning_details: accumulatedReasoningDetails
3609
3624
  }
3610
- }
3625
+ } : void 0
3611
3626
  });
3627
+ reasoningDetailsAttachedToToolCall = true;
3612
3628
  toolCall.sent = true;
3613
3629
  }
3614
3630
  }
@@ -4200,7 +4216,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4200
4216
  }
4201
4217
 
4202
4218
  // src/version.ts
4203
- var VERSION2 = false ? "0.0.0-test" : "2.0.1";
4219
+ var VERSION2 = false ? "0.0.0-test" : "2.0.2";
4204
4220
 
4205
4221
  // src/provider.ts
4206
4222
  function createOpenRouter(options = {}) {