@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.
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, _h;
2449
2449
  const messages = [];
2450
2450
  for (const { role, content, providerOptions } of prompt) {
2451
2451
  switch (role) {
@@ -2474,42 +2474,46 @@ function convertToOpenRouterChatMessages(prompt) {
2474
2474
  break;
2475
2475
  }
2476
2476
  const messageCacheControl = getCacheControl(providerOptions);
2477
+ let lastTextPartIndex = -1;
2478
+ for (let i = content.length - 1; i >= 0; i--) {
2479
+ if (((_c = content[i]) == null ? void 0 : _c.type) === "text") {
2480
+ lastTextPartIndex = i;
2481
+ break;
2482
+ }
2483
+ }
2477
2484
  const contentParts = content.map(
2478
- (part) => {
2479
- var _a17, _b17, _c2, _d2, _e2, _f2, _g2;
2480
- const cacheControl = (_a17 = getCacheControl(part.providerOptions)) != null ? _a17 : messageCacheControl;
2485
+ (part, index) => {
2486
+ var _a17, _b17, _c2, _d2, _e2, _f2;
2487
+ const isLastTextPart = part.type === "text" && index === lastTextPartIndex;
2488
+ const partCacheControl = getCacheControl(part.providerOptions);
2489
+ const cacheControl = part.type === "text" ? partCacheControl != null ? partCacheControl : isLastTextPart ? messageCacheControl : void 0 : partCacheControl;
2481
2490
  switch (part.type) {
2482
2491
  case "text":
2483
- return {
2492
+ return __spreadValues({
2484
2493
  type: "text",
2485
- text: part.text,
2486
- // For text parts, only use part-specific cache control
2487
- cache_control: cacheControl
2488
- };
2494
+ text: part.text
2495
+ }, cacheControl && { cache_control: cacheControl });
2489
2496
  case "file": {
2490
- if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("image/")) {
2497
+ if ((_a17 = part.mediaType) == null ? void 0 : _a17.startsWith("image/")) {
2491
2498
  const url = getFileUrl({
2492
2499
  part,
2493
2500
  defaultMediaType: "image/jpeg"
2494
2501
  });
2495
- return {
2502
+ return __spreadValues({
2496
2503
  type: "image_url",
2497
2504
  image_url: {
2498
2505
  url
2499
- },
2500
- // For image parts, use part-specific or message-level cache control
2501
- cache_control: cacheControl
2502
- };
2506
+ }
2507
+ }, cacheControl && { cache_control: cacheControl });
2503
2508
  }
2504
- if ((_c2 = part.mediaType) == null ? void 0 : _c2.startsWith("audio/")) {
2505
- return {
2509
+ if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("audio/")) {
2510
+ return __spreadValues({
2506
2511
  type: "input_audio",
2507
- input_audio: getInputAudioData(part),
2508
- cache_control: cacheControl
2509
- };
2512
+ input_audio: getInputAudioData(part)
2513
+ }, cacheControl && { cache_control: cacheControl });
2510
2514
  }
2511
2515
  const fileName = String(
2512
- (_g2 = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g2 : ""
2516
+ (_f2 = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f2 : ""
2513
2517
  );
2514
2518
  const fileData = getFileUrl({
2515
2519
  part,
@@ -2527,21 +2531,19 @@ function convertToOpenRouterChatMessages(prompt) {
2527
2531
  }
2528
2532
  };
2529
2533
  }
2530
- return {
2534
+ return __spreadValues({
2531
2535
  type: "file",
2532
2536
  file: {
2533
2537
  filename: fileName,
2534
2538
  file_data: fileData
2535
- },
2536
- cache_control: cacheControl
2537
- };
2539
+ }
2540
+ }, cacheControl && { cache_control: cacheControl });
2538
2541
  }
2539
2542
  default: {
2540
- return {
2543
+ return __spreadValues({
2541
2544
  type: "text",
2542
- text: "",
2543
- cache_control: cacheControl
2544
- };
2545
+ text: ""
2546
+ }, cacheControl && { cache_control: cacheControl });
2545
2547
  }
2546
2548
  }
2547
2549
  }
@@ -2556,7 +2558,6 @@ function convertToOpenRouterChatMessages(prompt) {
2556
2558
  let text = "";
2557
2559
  let reasoning = "";
2558
2560
  const toolCalls = [];
2559
- const accumulatedReasoningDetails = [];
2560
2561
  for (const part of content) {
2561
2562
  switch (part.type) {
2562
2563
  case "text": {
@@ -2564,12 +2565,6 @@ function convertToOpenRouterChatMessages(prompt) {
2564
2565
  break;
2565
2566
  }
2566
2567
  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
2568
  toolCalls.push({
2574
2569
  id: part.toolCallId,
2575
2570
  type: "function",
@@ -2582,12 +2577,6 @@ function convertToOpenRouterChatMessages(prompt) {
2582
2577
  }
2583
2578
  case "reasoning": {
2584
2579
  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
2580
  break;
2592
2581
  }
2593
2582
  case "file":
@@ -2598,9 +2587,9 @@ function convertToOpenRouterChatMessages(prompt) {
2598
2587
  }
2599
2588
  }
2600
2589
  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;
2590
+ const messageReasoningDetails = parsedProviderOptions.success ? (_e = (_d = parsedProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details : void 0;
2591
+ const messageAnnotations = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.annotations : void 0;
2592
+ const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2604
2593
  messages.push({
2605
2594
  role: "assistant",
2606
2595
  content: text,
@@ -2622,7 +2611,7 @@ function convertToOpenRouterChatMessages(prompt) {
2622
2611
  role: "tool",
2623
2612
  tool_call_id: toolResponse.toolCallId,
2624
2613
  content: content2,
2625
- cache_control: (_j = getCacheControl(providerOptions)) != null ? _j : getCacheControl(toolResponse.providerOptions)
2614
+ cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
2626
2615
  });
2627
2616
  }
2628
2617
  break;
@@ -2648,6 +2637,29 @@ function getToolResultContent(input) {
2648
2637
  return (_a16 = input.output.reason) != null ? _a16 : "Tool execution denied";
2649
2638
  }
2650
2639
  }
2640
+ function findFirstReasoningDetails(content) {
2641
+ var _a16, _b16, _c;
2642
+ for (const part of content) {
2643
+ if (part.type === "tool-call") {
2644
+ const openrouter2 = (_a16 = part.providerOptions) == null ? void 0 : _a16.openrouter;
2645
+ const details = openrouter2 == null ? void 0 : openrouter2.reasoning_details;
2646
+ if (Array.isArray(details) && details.length > 0) {
2647
+ return details;
2648
+ }
2649
+ }
2650
+ }
2651
+ for (const part of content) {
2652
+ if (part.type === "reasoning") {
2653
+ const parsed = OpenRouterProviderOptionsSchema.safeParse(
2654
+ part.providerOptions
2655
+ );
2656
+ 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) {
2657
+ return parsed.data.openrouter.reasoning_details;
2658
+ }
2659
+ }
2660
+ }
2661
+ return void 0;
2662
+ }
2651
2663
 
2652
2664
  // src/chat/get-tool-choice.ts
2653
2665
  import { z as z5 } from "zod/v4";
@@ -2739,7 +2751,7 @@ var OpenRouterNonStreamChatCompletionResponseSchema = z7.union([
2739
2751
  type: z7.literal("function"),
2740
2752
  function: z7.object({
2741
2753
  name: z7.string(),
2742
- arguments: z7.string()
2754
+ arguments: z7.string().optional()
2743
2755
  }).passthrough()
2744
2756
  }).passthrough()
2745
2757
  ).optional(),
@@ -2982,7 +2994,7 @@ var OpenRouterChatLanguageModel = class {
2982
2994
  return baseArgs;
2983
2995
  }
2984
2996
  async doGenerate(options) {
2985
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
2997
+ 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;
2986
2998
  const providerOptions = options.providerOptions || {};
2987
2999
  const openrouterOptions = providerOptions.openrouter || {};
2988
3000
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3111,18 +3123,20 @@ var OpenRouterChatLanguageModel = class {
3111
3123
  });
3112
3124
  }
3113
3125
  if (choice.message.tool_calls) {
3126
+ let reasoningDetailsAttachedToToolCall = false;
3114
3127
  for (const toolCall of choice.message.tool_calls) {
3115
3128
  content.push({
3116
3129
  type: "tool-call",
3117
3130
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3118
3131
  toolName: toolCall.function.name,
3119
- input: toolCall.function.arguments,
3120
- providerMetadata: {
3132
+ input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3133
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3121
3134
  openrouter: {
3122
3135
  reasoning_details: reasoningDetails
3123
3136
  }
3124
- }
3137
+ } : void 0
3125
3138
  });
3139
+ reasoningDetailsAttachedToToolCall = true;
3126
3140
  }
3127
3141
  }
3128
3142
  if (choice.message.images) {
@@ -3152,7 +3166,7 @@ var OpenRouterChatLanguageModel = class {
3152
3166
  }
3153
3167
  }
3154
3168
  }
3155
- const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3169
+ const fileAnnotations = (_j = choice.message.annotations) == null ? void 0 : _j.filter(
3156
3170
  (a) => a.type === "file"
3157
3171
  );
3158
3172
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3160,7 +3174,7 @@ var OpenRouterChatLanguageModel = class {
3160
3174
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3161
3175
  );
3162
3176
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3163
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3177
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_k = choice.finish_reason) != null ? _k : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3164
3178
  return {
3165
3179
  content,
3166
3180
  finishReason: effectiveFinishReason,
@@ -3168,23 +3182,23 @@ var OpenRouterChatLanguageModel = class {
3168
3182
  warnings: [],
3169
3183
  providerMetadata: {
3170
3184
  openrouter: OpenRouterProviderMetadataSchema.parse({
3171
- provider: (_k = response.provider) != null ? _k : "",
3172
- reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3185
+ provider: (_l = response.provider) != null ? _l : "",
3186
+ reasoning_details: (_m = choice.message.reasoning_details) != null ? _m : [],
3173
3187
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3174
3188
  usage: __spreadValues(__spreadValues(__spreadValues({
3175
- promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3176
- completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3177
- totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0),
3178
- cost: (_q = response.usage) == null ? void 0 : _q.cost
3179
- }, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
3189
+ promptTokens: (_n = usageInfo.inputTokens.total) != null ? _n : 0,
3190
+ completionTokens: (_o = usageInfo.outputTokens.total) != null ? _o : 0,
3191
+ totalTokens: ((_p = usageInfo.inputTokens.total) != null ? _p : 0) + ((_q = usageInfo.outputTokens.total) != null ? _q : 0),
3192
+ cost: (_r = response.usage) == null ? void 0 : _r.cost
3193
+ }, ((_t = (_s = response.usage) == null ? void 0 : _s.prompt_tokens_details) == null ? void 0 : _t.cached_tokens) != null ? {
3180
3194
  promptTokensDetails: {
3181
3195
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3182
3196
  }
3183
- } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3197
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.completion_tokens_details) == null ? void 0 : _v.reasoning_tokens) != null ? {
3184
3198
  completionTokensDetails: {
3185
3199
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3186
3200
  }
3187
- } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3201
+ } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? {
3188
3202
  costDetails: {
3189
3203
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3190
3204
  }
@@ -3241,6 +3255,7 @@ var OpenRouterChatLanguageModel = class {
3241
3255
  };
3242
3256
  const openrouterUsage = {};
3243
3257
  const accumulatedReasoningDetails = [];
3258
+ let reasoningDetailsAttachedToToolCall = false;
3244
3259
  const accumulatedFileAnnotations = [];
3245
3260
  let textStarted = false;
3246
3261
  let reasoningStarted = false;
@@ -3489,12 +3504,13 @@ var OpenRouterChatLanguageModel = class {
3489
3504
  toolCallId: toolCall2.id,
3490
3505
  toolName: toolCall2.function.name,
3491
3506
  input: toolCall2.function.arguments,
3492
- providerMetadata: {
3507
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3493
3508
  openrouter: {
3494
3509
  reasoning_details: accumulatedReasoningDetails
3495
3510
  }
3496
- }
3511
+ } : void 0
3497
3512
  });
3513
+ reasoningDetailsAttachedToToolCall = true;
3498
3514
  toolCall2.sent = true;
3499
3515
  }
3500
3516
  continue;
@@ -3532,12 +3548,13 @@ var OpenRouterChatLanguageModel = class {
3532
3548
  toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
3533
3549
  toolName: toolCall.function.name,
3534
3550
  input: toolCall.function.arguments,
3535
- providerMetadata: {
3551
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3536
3552
  openrouter: {
3537
3553
  reasoning_details: accumulatedReasoningDetails
3538
3554
  }
3539
- }
3555
+ } : void 0
3540
3556
  });
3557
+ reasoningDetailsAttachedToToolCall = true;
3541
3558
  toolCall.sent = true;
3542
3559
  }
3543
3560
  }
@@ -3570,12 +3587,13 @@ var OpenRouterChatLanguageModel = class {
3570
3587
  toolName: toolCall.function.name,
3571
3588
  // Coerce invalid arguments to an empty JSON object
3572
3589
  input: isParsableJson(toolCall.function.arguments) ? toolCall.function.arguments : "{}",
3573
- providerMetadata: {
3590
+ providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3574
3591
  openrouter: {
3575
3592
  reasoning_details: accumulatedReasoningDetails
3576
3593
  }
3577
- }
3594
+ } : void 0
3578
3595
  });
3596
+ reasoningDetailsAttachedToToolCall = true;
3579
3597
  toolCall.sent = true;
3580
3598
  }
3581
3599
  }
@@ -4167,7 +4185,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4167
4185
  }
4168
4186
 
4169
4187
  // src/version.ts
4170
- var VERSION2 = false ? "0.0.0-test" : "2.0.1";
4188
+ var VERSION2 = false ? "0.0.0-test" : "2.0.4";
4171
4189
 
4172
4190
  // src/provider.ts
4173
4191
  function createOpenRouter(options = {}) {