@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.
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;
2481
+ var _a16, _b16, _c, _d, _e, _f, _g, _h;
2482
2482
  const messages = [];
2483
2483
  for (const { role, content, providerOptions } of prompt) {
2484
2484
  switch (role) {
@@ -2507,42 +2507,46 @@ function convertToOpenRouterChatMessages(prompt) {
2507
2507
  break;
2508
2508
  }
2509
2509
  const messageCacheControl = getCacheControl(providerOptions);
2510
+ let lastTextPartIndex = -1;
2511
+ for (let i = content.length - 1; i >= 0; i--) {
2512
+ if (((_c = content[i]) == null ? void 0 : _c.type) === "text") {
2513
+ lastTextPartIndex = i;
2514
+ break;
2515
+ }
2516
+ }
2510
2517
  const contentParts = content.map(
2511
- (part) => {
2512
- var _a17, _b17, _c2, _d2, _e2, _f2, _g2;
2513
- const cacheControl = (_a17 = getCacheControl(part.providerOptions)) != null ? _a17 : messageCacheControl;
2518
+ (part, index) => {
2519
+ var _a17, _b17, _c2, _d2, _e2, _f2;
2520
+ const isLastTextPart = part.type === "text" && index === lastTextPartIndex;
2521
+ const partCacheControl = getCacheControl(part.providerOptions);
2522
+ const cacheControl = part.type === "text" ? partCacheControl != null ? partCacheControl : isLastTextPart ? messageCacheControl : void 0 : partCacheControl;
2514
2523
  switch (part.type) {
2515
2524
  case "text":
2516
- return {
2525
+ return __spreadValues({
2517
2526
  type: "text",
2518
- text: part.text,
2519
- // For text parts, only use part-specific cache control
2520
- cache_control: cacheControl
2521
- };
2527
+ text: part.text
2528
+ }, cacheControl && { cache_control: cacheControl });
2522
2529
  case "file": {
2523
- if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("image/")) {
2530
+ if ((_a17 = part.mediaType) == null ? void 0 : _a17.startsWith("image/")) {
2524
2531
  const url = getFileUrl({
2525
2532
  part,
2526
2533
  defaultMediaType: "image/jpeg"
2527
2534
  });
2528
- return {
2535
+ return __spreadValues({
2529
2536
  type: "image_url",
2530
2537
  image_url: {
2531
2538
  url
2532
- },
2533
- // For image parts, use part-specific or message-level cache control
2534
- cache_control: cacheControl
2535
- };
2539
+ }
2540
+ }, cacheControl && { cache_control: cacheControl });
2536
2541
  }
2537
- if ((_c2 = part.mediaType) == null ? void 0 : _c2.startsWith("audio/")) {
2538
- return {
2542
+ if ((_b17 = part.mediaType) == null ? void 0 : _b17.startsWith("audio/")) {
2543
+ return __spreadValues({
2539
2544
  type: "input_audio",
2540
- input_audio: getInputAudioData(part),
2541
- cache_control: cacheControl
2542
- };
2545
+ input_audio: getInputAudioData(part)
2546
+ }, cacheControl && { cache_control: cacheControl });
2543
2547
  }
2544
2548
  const fileName = String(
2545
- (_g2 = (_f2 = (_e2 = (_d2 = part.providerOptions) == null ? void 0 : _d2.openrouter) == null ? void 0 : _e2.filename) != null ? _f2 : part.filename) != null ? _g2 : ""
2549
+ (_f2 = (_e2 = (_d2 = (_c2 = part.providerOptions) == null ? void 0 : _c2.openrouter) == null ? void 0 : _d2.filename) != null ? _e2 : part.filename) != null ? _f2 : ""
2546
2550
  );
2547
2551
  const fileData = getFileUrl({
2548
2552
  part,
@@ -2560,21 +2564,19 @@ function convertToOpenRouterChatMessages(prompt) {
2560
2564
  }
2561
2565
  };
2562
2566
  }
2563
- return {
2567
+ return __spreadValues({
2564
2568
  type: "file",
2565
2569
  file: {
2566
2570
  filename: fileName,
2567
2571
  file_data: fileData
2568
- },
2569
- cache_control: cacheControl
2570
- };
2572
+ }
2573
+ }, cacheControl && { cache_control: cacheControl });
2571
2574
  }
2572
2575
  default: {
2573
- return {
2576
+ return __spreadValues({
2574
2577
  type: "text",
2575
- text: "",
2576
- cache_control: cacheControl
2577
- };
2578
+ text: ""
2579
+ }, cacheControl && { cache_control: cacheControl });
2578
2580
  }
2579
2581
  }
2580
2582
  }
@@ -2618,8 +2620,8 @@ function convertToOpenRouterChatMessages(prompt) {
2618
2620
  }
2619
2621
  }
2620
2622
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
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 messageReasoningDetails = parsedProviderOptions.success ? (_e = (_d = parsedProviderOptions.data) == null ? void 0 : _d.openrouter) == null ? void 0 : _e.reasoning_details : void 0;
2624
+ const messageAnnotations = parsedProviderOptions.success ? (_g = (_f = parsedProviderOptions.data) == null ? void 0 : _f.openrouter) == null ? void 0 : _g.annotations : void 0;
2623
2625
  const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2624
2626
  messages.push({
2625
2627
  role: "assistant",
@@ -2642,7 +2644,7 @@ function convertToOpenRouterChatMessages(prompt) {
2642
2644
  role: "tool",
2643
2645
  tool_call_id: toolResponse.toolCallId,
2644
2646
  content: content2,
2645
- cache_control: (_g = getCacheControl(providerOptions)) != null ? _g : getCacheControl(toolResponse.providerOptions)
2647
+ cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
2646
2648
  });
2647
2649
  }
2648
2650
  break;
@@ -2782,7 +2784,7 @@ var OpenRouterNonStreamChatCompletionResponseSchema = import_v46.z.union([
2782
2784
  type: import_v46.z.literal("function"),
2783
2785
  function: import_v46.z.object({
2784
2786
  name: import_v46.z.string(),
2785
- arguments: import_v46.z.string()
2787
+ arguments: import_v46.z.string().optional()
2786
2788
  }).passthrough()
2787
2789
  }).passthrough()
2788
2790
  ).optional(),
@@ -3025,7 +3027,7 @@ var OpenRouterChatLanguageModel = class {
3025
3027
  return baseArgs;
3026
3028
  }
3027
3029
  async doGenerate(options) {
3028
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
3030
+ 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;
3029
3031
  const providerOptions = options.providerOptions || {};
3030
3032
  const openrouterOptions = providerOptions.openrouter || {};
3031
3033
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3160,7 +3162,7 @@ var OpenRouterChatLanguageModel = class {
3160
3162
  type: "tool-call",
3161
3163
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3162
3164
  toolName: toolCall.function.name,
3163
- input: toolCall.function.arguments,
3165
+ input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3164
3166
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3165
3167
  openrouter: {
3166
3168
  reasoning_details: reasoningDetails
@@ -3197,7 +3199,7 @@ var OpenRouterChatLanguageModel = class {
3197
3199
  }
3198
3200
  }
3199
3201
  }
3200
- const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3202
+ const fileAnnotations = (_j = choice.message.annotations) == null ? void 0 : _j.filter(
3201
3203
  (a) => a.type === "file"
3202
3204
  );
3203
3205
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3205,7 +3207,7 @@ var OpenRouterChatLanguageModel = class {
3205
3207
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3206
3208
  );
3207
3209
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3208
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3210
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_k = choice.finish_reason) != null ? _k : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3209
3211
  return {
3210
3212
  content,
3211
3213
  finishReason: effectiveFinishReason,
@@ -3213,23 +3215,23 @@ var OpenRouterChatLanguageModel = class {
3213
3215
  warnings: [],
3214
3216
  providerMetadata: {
3215
3217
  openrouter: OpenRouterProviderMetadataSchema.parse({
3216
- provider: (_k = response.provider) != null ? _k : "",
3217
- reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3218
+ provider: (_l = response.provider) != null ? _l : "",
3219
+ reasoning_details: (_m = choice.message.reasoning_details) != null ? _m : [],
3218
3220
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3219
3221
  usage: __spreadValues(__spreadValues(__spreadValues({
3220
- promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3221
- completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3222
- totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0),
3223
- cost: (_q = response.usage) == null ? void 0 : _q.cost
3224
- }, ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
3222
+ promptTokens: (_n = usageInfo.inputTokens.total) != null ? _n : 0,
3223
+ completionTokens: (_o = usageInfo.outputTokens.total) != null ? _o : 0,
3224
+ totalTokens: ((_p = usageInfo.inputTokens.total) != null ? _p : 0) + ((_q = usageInfo.outputTokens.total) != null ? _q : 0),
3225
+ cost: (_r = response.usage) == null ? void 0 : _r.cost
3226
+ }, ((_t = (_s = response.usage) == null ? void 0 : _s.prompt_tokens_details) == null ? void 0 : _t.cached_tokens) != null ? {
3225
3227
  promptTokensDetails: {
3226
3228
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3227
3229
  }
3228
- } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3230
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.completion_tokens_details) == null ? void 0 : _v.reasoning_tokens) != null ? {
3229
3231
  completionTokensDetails: {
3230
3232
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3231
3233
  }
3232
- } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3234
+ } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? {
3233
3235
  costDetails: {
3234
3236
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3235
3237
  }
@@ -4216,7 +4218,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4216
4218
  }
4217
4219
 
4218
4220
  // src/version.ts
4219
- var VERSION2 = false ? "0.0.0-test" : "2.0.2";
4221
+ var VERSION2 = false ? "0.0.0-test" : "2.0.4";
4220
4222
 
4221
4223
  // src/provider.ts
4222
4224
  function createOpenRouter(options = {}) {