@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.
@@ -2410,7 +2410,7 @@ function getCacheControl(providerMetadata) {
2410
2410
  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;
2411
2411
  }
2412
2412
  function convertToOpenRouterChatMessages(prompt) {
2413
- var _a16, _b16, _c, _d, _e, _f, _g;
2413
+ var _a16, _b16, _c, _d, _e, _f, _g, _h;
2414
2414
  const messages = [];
2415
2415
  for (const { role, content, providerOptions } of prompt) {
2416
2416
  switch (role) {
@@ -2439,42 +2439,46 @@ function convertToOpenRouterChatMessages(prompt) {
2439
2439
  break;
2440
2440
  }
2441
2441
  const messageCacheControl = getCacheControl(providerOptions);
2442
+ let lastTextPartIndex = -1;
2443
+ for (let i = content.length - 1; i >= 0; i--) {
2444
+ if (((_c = content[i]) == null ? void 0 : _c.type) === "text") {
2445
+ lastTextPartIndex = i;
2446
+ break;
2447
+ }
2448
+ }
2442
2449
  const contentParts = content.map(
2443
- (part) => {
2444
- var _a17, _b17, _c2, _d2, _e2, _f2, _g2;
2445
- const cacheControl = (_a17 = getCacheControl(part.providerOptions)) != null ? _a17 : messageCacheControl;
2450
+ (part, index) => {
2451
+ var _a17, _b17, _c2, _d2, _e2, _f2;
2452
+ const isLastTextPart = part.type === "text" && index === lastTextPartIndex;
2453
+ const partCacheControl = getCacheControl(part.providerOptions);
2454
+ const cacheControl = part.type === "text" ? partCacheControl != null ? partCacheControl : isLastTextPart ? messageCacheControl : void 0 : partCacheControl;
2446
2455
  switch (part.type) {
2447
2456
  case "text":
2448
- return {
2457
+ return __spreadValues({
2449
2458
  type: "text",
2450
- text: part.text,
2451
- // For text parts, only use part-specific cache control
2452
- cache_control: cacheControl
2453
- };
2459
+ text: part.text
2460
+ }, cacheControl && { cache_control: cacheControl });
2454
2461
  case "file": {
2455
- if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("image/")) {
2462
+ if ((_a17 = part.mediaType) == null ? void 0 : _a17.startsWith("image/")) {
2456
2463
  const url = getFileUrl({
2457
2464
  part,
2458
2465
  defaultMediaType: "image/jpeg"
2459
2466
  });
2460
- return {
2467
+ return __spreadValues({
2461
2468
  type: "image_url",
2462
2469
  image_url: {
2463
2470
  url
2464
- },
2465
- // For image parts, use part-specific or message-level cache control
2466
- cache_control: cacheControl
2467
- };
2471
+ }
2472
+ }, cacheControl && { cache_control: cacheControl });
2468
2473
  }
2469
- if ((_c2 = part.mediaType) == null ? void 0 : _c2.startsWith("audio/")) {
2470
- return {
2474
+ if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("audio/")) {
2475
+ return __spreadValues({
2471
2476
  type: "input_audio",
2472
- input_audio: getInputAudioData(part),
2473
- cache_control: cacheControl
2474
- };
2477
+ input_audio: getInputAudioData(part)
2478
+ }, cacheControl && { cache_control: cacheControl });
2475
2479
  }
2476
2480
  const fileName = String(
2477
- (_g2 = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g2 : ""
2481
+ (_f2 = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f2 : ""
2478
2482
  );
2479
2483
  const fileData = getFileUrl({
2480
2484
  part,
@@ -2492,21 +2496,19 @@ function convertToOpenRouterChatMessages(prompt) {
2492
2496
  }
2493
2497
  };
2494
2498
  }
2495
- return {
2499
+ return __spreadValues({
2496
2500
  type: "file",
2497
2501
  file: {
2498
2502
  filename: fileName,
2499
2503
  file_data: fileData
2500
- },
2501
- cache_control: cacheControl
2502
- };
2504
+ }
2505
+ }, cacheControl && { cache_control: cacheControl });
2503
2506
  }
2504
2507
  default: {
2505
- return {
2508
+ return __spreadValues({
2506
2509
  type: "text",
2507
- text: "",
2508
- cache_control: cacheControl
2509
- };
2510
+ text: ""
2511
+ }, cacheControl && { cache_control: cacheControl });
2510
2512
  }
2511
2513
  }
2512
2514
  }
@@ -2550,8 +2552,8 @@ function convertToOpenRouterChatMessages(prompt) {
2550
2552
  }
2551
2553
  }
2552
2554
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
2553
- const messageReasoningDetails = parsedProviderOptions.success ? (_d = (_c = parsedProviderOptions.data) == null ? void 0 : _c.openrouter) == null ? void 0 : _d.reasoning_details : void 0;
2554
- const messageAnnotations = parsedProviderOptions.success ? (_f = (_e = parsedProviderOptions.data) == null ? void 0 : _e.openrouter) == null ? void 0 : _f.annotations : void 0;
2555
+ const messageReasoningDetails = parsedProviderOptions.success ? (_e = (_d = parsedProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details : void 0;
2556
+ const messageAnnotations = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.annotations : void 0;
2555
2557
  const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2556
2558
  messages.push({
2557
2559
  role: "assistant",
@@ -2574,7 +2576,7 @@ function convertToOpenRouterChatMessages(prompt) {
2574
2576
  role: "tool",
2575
2577
  tool_call_id: toolResponse.toolCallId,
2576
2578
  content: content2,
2577
- cache_control: (_g = getCacheControl(providerOptions)) != null ? _g : getCacheControl(toolResponse.providerOptions)
2579
+ cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
2578
2580
  });
2579
2581
  }
2580
2582
  break;
@@ -2714,7 +2716,7 @@ var OpenRouterNonStreamChatCompletionResponseSchema = z7.union([
2714
2716
  type: z7.literal("function"),
2715
2717
  function: z7.object({
2716
2718
  name: z7.string(),
2717
- arguments: z7.string()
2719
+ arguments: z7.string().optional()
2718
2720
  }).passthrough()
2719
2721
  }).passthrough()
2720
2722
  ).optional(),
@@ -2957,7 +2959,7 @@ var OpenRouterChatLanguageModel = class {
2957
2959
  return baseArgs;
2958
2960
  }
2959
2961
  async doGenerate(options) {
2960
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
2962
+ 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;
2961
2963
  const providerOptions = options.providerOptions || {};
2962
2964
  const openrouterOptions = providerOptions.openrouter || {};
2963
2965
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3092,7 +3094,7 @@ var OpenRouterChatLanguageModel = class {
3092
3094
  type: "tool-call",
3093
3095
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3094
3096
  toolName: toolCall.function.name,
3095
- input: toolCall.function.arguments,
3097
+ input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3096
3098
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3097
3099
  openrouter: {
3098
3100
  reasoning_details: reasoningDetails
@@ -3129,7 +3131,7 @@ var OpenRouterChatLanguageModel = class {
3129
3131
  }
3130
3132
  }
3131
3133
  }
3132
- const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3134
+ const fileAnnotations = (_j = choice.message.annotations) == null ? void 0 : _j.filter(
3133
3135
  (a) => a.type === "file"
3134
3136
  );
3135
3137
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3137,7 +3139,7 @@ var OpenRouterChatLanguageModel = class {
3137
3139
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3138
3140
  );
3139
3141
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3140
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3142
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_k = choice.finish_reason) != null ? _k : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3141
3143
  return {
3142
3144
  content,
3143
3145
  finishReason: effectiveFinishReason,
@@ -3145,23 +3147,23 @@ var OpenRouterChatLanguageModel = class {
3145
3147
  warnings: [],
3146
3148
  providerMetadata: {
3147
3149
  openrouter: OpenRouterProviderMetadataSchema.parse({
3148
- provider: (_k = response.provider) != null ? _k : "",
3149
- reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3150
+ provider: (_l = response.provider) != null ? _l : "",
3151
+ reasoning_details: (_m = choice.message.reasoning_details) != null ? _m : [],
3150
3152
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3151
3153
  usage: __spreadValues(__spreadValues(__spreadValues({
3152
- promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3153
- completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3154
- totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0),
3155
- cost: (_q = response.usage) == null ? void 0 : _q.cost
3156
- }, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
3154
+ promptTokens: (_n = usageInfo.inputTokens.total) != null ? _n : 0,
3155
+ completionTokens: (_o = usageInfo.outputTokens.total) != null ? _o : 0,
3156
+ totalTokens: ((_p = usageInfo.inputTokens.total) != null ? _p : 0) + ((_q = usageInfo.outputTokens.total) != null ? _q : 0),
3157
+ cost: (_r = response.usage) == null ? void 0 : _r.cost
3158
+ }, ((_t = (_s = response.usage) == null ? void 0 : _s.prompt_tokens_details) == null ? void 0 : _t.cached_tokens) != null ? {
3157
3159
  promptTokensDetails: {
3158
3160
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3159
3161
  }
3160
- } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3162
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.completion_tokens_details) == null ? void 0 : _v.reasoning_tokens) != null ? {
3161
3163
  completionTokensDetails: {
3162
3164
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3163
3165
  }
3164
- } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3166
+ } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? {
3165
3167
  costDetails: {
3166
3168
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3167
3169
  }