@openrouter/ai-sdk-provider 2.0.0 → 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.mjs CHANGED
@@ -2445,7 +2445,7 @@ function getCacheControl(providerMetadata) {
2445
2445
  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;
2446
2446
  }
2447
2447
  function convertToOpenRouterChatMessages(prompt) {
2448
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j;
2448
+ var _a16, _b16, _c, _d, _e, _f, _g;
2449
2449
  const messages = [];
2450
2450
  for (const { role, content, providerOptions } of prompt) {
2451
2451
  switch (role) {
@@ -2556,7 +2556,6 @@ function convertToOpenRouterChatMessages(prompt) {
2556
2556
  let text = "";
2557
2557
  let reasoning = "";
2558
2558
  const toolCalls = [];
2559
- const accumulatedReasoningDetails = [];
2560
2559
  for (const part of content) {
2561
2560
  switch (part.type) {
2562
2561
  case "text": {
@@ -2564,12 +2563,6 @@ function convertToOpenRouterChatMessages(prompt) {
2564
2563
  break;
2565
2564
  }
2566
2565
  case "tool-call": {
2567
- const partReasoningDetails = (_c = part.providerOptions) == null ? void 0 : _c.openrouter;
2568
- if ((partReasoningDetails == null ? void 0 : partReasoningDetails.reasoning_details) && Array.isArray(partReasoningDetails.reasoning_details)) {
2569
- accumulatedReasoningDetails.push(
2570
- ...partReasoningDetails.reasoning_details
2571
- );
2572
- }
2573
2566
  toolCalls.push({
2574
2567
  id: part.toolCallId,
2575
2568
  type: "function",
@@ -2582,12 +2575,6 @@ function convertToOpenRouterChatMessages(prompt) {
2582
2575
  }
2583
2576
  case "reasoning": {
2584
2577
  reasoning += part.text;
2585
- const parsedPartProviderOptions = OpenRouterProviderOptionsSchema.safeParse(part.providerOptions);
2586
- if (parsedPartProviderOptions.success && ((_e = (_d = parsedPartProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details)) {
2587
- accumulatedReasoningDetails.push(
2588
- ...parsedPartProviderOptions.data.openrouter.reasoning_details
2589
- );
2590
- }
2591
2578
  break;
2592
2579
  }
2593
2580
  case "file":
@@ -2598,9 +2585,9 @@ function convertToOpenRouterChatMessages(prompt) {
2598
2585
  }
2599
2586
  }
2600
2587
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
2601
- const messageReasoningDetails = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.reasoning_details : void 0;
2602
- const messageAnnotations = parsedProviderOptions.success ? (_i = (_h = parsedProviderOptions.data) == null ? void 0 : _h.openrouter) == null ? void 0 : _i.annotations : void 0;
2603
- const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : accumulatedReasoningDetails.length > 0 ? accumulatedReasoningDetails : void 0;
2588
+ const messageReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
2589
+ const messageAnnotations = parsedProviderOptions.success ? (_f = (_e = parsedProviderOptions.data) == null ? void 0 : _e.openrouter) == null ? void 0 : _f.annotations : void 0;
2590
+ const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2604
2591
  messages.push({
2605
2592
  role: "assistant",
2606
2593
  content: text,
@@ -2622,7 +2609,7 @@ function convertToOpenRouterChatMessages(prompt) {
2622
2609
  role: "tool",
2623
2610
  tool_call_id: toolResponse.toolCallId,
2624
2611
  content: content2,
2625
- cache_control: (_j = getCacheControl(providerOptions)) != null ? _j : getCacheControl(toolResponse.providerOptions)
2612
+ cache_control: (_g = getCacheControl(providerOptions)) != null ? _g : getCacheControl(toolResponse.providerOptions)
2626
2613
  });
2627
2614
  }
2628
2615
  break;
@@ -2648,6 +2635,29 @@ function getToolResultContent(input) {
2648
2635
  return (_a16 = input.output.reason) != null ? _a16 : "Tool execution denied";
2649
2636
  }
2650
2637
  }
2638
+ function findFirstReasoningDetails(content) {
2639
+ var _a16, _b16, _c;
2640
+ for (const part of content) {
2641
+ if (part.type === "tool-call") {
2642
+ const openrouter2 = (_a16 = part.providerOptions) == null ? void 0 : _a16.openrouter;
2643
+ const details = openrouter2 == null ? void 0 : openrouter2.reasoning_details;
2644
+ if (Array.isArray(details) && details.length > 0) {
2645
+ return details;
2646
+ }
2647
+ }
2648
+ }
2649
+ for (const part of content) {
2650
+ if (part.type === "reasoning") {
2651
+ const parsed = OpenRouterProviderOptionsSchema.safeParse(
2652
+ part.providerOptions
2653
+ );
2654
+ 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) {
2655
+ return parsed.data.openrouter.reasoning_details;
2656
+ }
2657
+ }
2658
+ }
2659
+ return void 0;
2660
+ }
2651
2661
 
2652
2662
  // src/chat/get-tool-choice.ts
2653
2663
  import { z as z5 } from "zod/v4";
@@ -3111,18 +3121,20 @@ var OpenRouterChatLanguageModel = class {
3111
3121
  });
3112
3122
  }
3113
3123
  if (choice.message.tool_calls) {
3124
+ let reasoningDetailsAttachedToToolCall = false;
3114
3125
  for (const toolCall of choice.message.tool_calls) {
3115
3126
  content.push({
3116
3127
  type: "tool-call",
3117
3128
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3118
3129
  toolName: toolCall.function.name,
3119
3130
  input: toolCall.function.arguments,
3120
- providerMetadata: {
3131
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3121
3132
  openrouter: {
3122
3133
  reasoning_details: reasoningDetails
3123
3134
  }
3124
- }
3135
+ } : void 0
3125
3136
  });
3137
+ reasoningDetailsAttachedToToolCall = true;
3126
3138
  }
3127
3139
  }
3128
3140
  if (choice.message.images) {
@@ -3241,6 +3253,7 @@ var OpenRouterChatLanguageModel = class {
3241
3253
  };
3242
3254
  const openrouterUsage = {};
3243
3255
  const accumulatedReasoningDetails = [];
3256
+ let reasoningDetailsAttachedToToolCall = false;
3244
3257
  const accumulatedFileAnnotations = [];
3245
3258
  let textStarted = false;
3246
3259
  let reasoningStarted = false;
@@ -3489,12 +3502,13 @@ var OpenRouterChatLanguageModel = class {
3489
3502
  toolCallId: toolCall2.id,
3490
3503
  toolName: toolCall2.function.name,
3491
3504
  input: toolCall2.function.arguments,
3492
- providerMetadata: {
3505
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3493
3506
  openrouter: {
3494
3507
  reasoning_details: accumulatedReasoningDetails
3495
3508
  }
3496
- }
3509
+ } : void 0
3497
3510
  });
3511
+ reasoningDetailsAttachedToToolCall = true;
3498
3512
  toolCall2.sent = true;
3499
3513
  }
3500
3514
  continue;
@@ -3532,12 +3546,13 @@ var OpenRouterChatLanguageModel = class {
3532
3546
  toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
3533
3547
  toolName: toolCall.function.name,
3534
3548
  input: toolCall.function.arguments,
3535
- providerMetadata: {
3549
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3536
3550
  openrouter: {
3537
3551
  reasoning_details: accumulatedReasoningDetails
3538
3552
  }
3539
- }
3553
+ } : void 0
3540
3554
  });
3555
+ reasoningDetailsAttachedToToolCall = true;
3541
3556
  toolCall.sent = true;
3542
3557
  }
3543
3558
  }
@@ -3570,12 +3585,13 @@ var OpenRouterChatLanguageModel = class {
3570
3585
  toolName: toolCall.function.name,
3571
3586
  // Coerce invalid arguments to an empty JSON object
3572
3587
  input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}",
3573
- providerMetadata: {
3588
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3574
3589
  openrouter: {
3575
3590
  reasoning_details: accumulatedReasoningDetails
3576
3591
  }
3577
- }
3592
+ } : void 0
3578
3593
  });
3594
+ reasoningDetailsAttachedToToolCall = true;
3579
3595
  toolCall.sent = true;
3580
3596
  }
3581
3597
  }
@@ -4167,7 +4183,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4167
4183
  }
4168
4184
 
4169
4185
  // src/version.ts
4170
- var VERSION2 = false ? "0.0.0-test" : "2.0.0";
4186
+ var VERSION2 = false ? "0.0.0-test" : "2.0.2";
4171
4187
 
4172
4188
  // src/provider.ts
4173
4189
  function createOpenRouter(options = {}) {