@openrouter/ai-sdk-provider 2.0.2 → 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;
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
  }
@@ -2582,8 +2584,8 @@ function convertToOpenRouterChatMessages(prompt) {
2582
2584
  }
2583
2585
  }
2584
2586
  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;
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;
2587
2589
  const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2588
2590
  messages.push({
2589
2591
  role: "assistant",
@@ -2606,7 +2608,7 @@ function convertToOpenRouterChatMessages(prompt) {
2606
2608
  role: "tool",
2607
2609
  tool_call_id: toolResponse.toolCallId,
2608
2610
  content: content2,
2609
- cache_control: (_g = getCacheControl(providerOptions)) != null ? _g : getCacheControl(toolResponse.providerOptions)
2611
+ cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
2610
2612
  });
2611
2613
  }
2612
2614
  break;
@@ -2746,7 +2748,7 @@ var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
2746
2748
  type: import_v46.z.literal("function"),
2747
2749
  function: import_v46.z.object({
2748
2750
  name: import_v46.z.string(),
2749
- arguments: import_v46.z.string()
2751
+ arguments: import_v46.z.string().optional()
2750
2752
  }).passthrough()
2751
2753
  }).passthrough()
2752
2754
  ).optional(),
@@ -2989,7 +2991,7 @@ var OpenRouterChatLanguageModel = class {
2989
2991
  return baseArgs;
2990
2992
  }
2991
2993
  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;
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;
2993
2995
  const providerOptions = options.providerOptions || {};
2994
2996
  const openrouterOptions = providerOptions.openrouter || {};
2995
2997
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3124,7 +3126,7 @@ var OpenRouterChatLanguageModel = class {
3124
3126
  type: "tool-call",
3125
3127
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3126
3128
  toolName: toolCall.function.name,
3127
- input: toolCall.function.arguments,
3129
+ input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3128
3130
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3129
3131
  openrouter: {
3130
3132
  reasoning_details: reasoningDetails
@@ -3161,7 +3163,7 @@ var OpenRouterChatLanguageModel = class {
3161
3163
  }
3162
3164
  }
3163
3165
  }
3164
- const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3166
+ const fileAnnotations = (_j = choice.message.annotations) == null ? void 0 : _j.filter(
3165
3167
  (a) => a.type === "file"
3166
3168
  );
3167
3169
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3169,7 +3171,7 @@ var OpenRouterChatLanguageModel = class {
3169
3171
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3170
3172
  );
3171
3173
  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);
3174
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_k = choice.finish_reason) != null ? _k : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3173
3175
  return {
3174
3176
  content,
3175
3177
  finishReason: effectiveFinishReason,
@@ -3177,23 +3179,23 @@ var OpenRouterChatLanguageModel = class {
3177
3179
  warnings: [],
3178
3180
  providerMetadata: {
3179
3181
  openrouter: OpenRouterProviderMetadataSchema.parse({
3180
- provider: (_k = response.provider) != null ? _k : "",
3181
- 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 : [],
3182
3184
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3183
3185
  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 ? {
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 ? {
3189
3191
  promptTokensDetails: {
3190
3192
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3191
3193
  }
3192
- } : {}), ((_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 ? {
3193
3195
  completionTokensDetails: {
3194
3196
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3195
3197
  }
3196
- } : {}), ((_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 ? {
3197
3199
  costDetails: {
3198
3200
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3199
3201
  }