@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.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;
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
  }
@@ -2585,8 +2587,8 @@ function convertToOpenRouterChatMessages(prompt) {
2585
2587
  }
2586
2588
  }
2587
2589
  const parsedProviderOptions = OpenRouterProviderOptionsSchema.safeParse(providerOptions);
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 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;
2590
2592
  const finalReasoningDetails = messageReasoningDetails && Array.isArray(messageReasoningDetails) && messageReasoningDetails.length > 0 ? messageReasoningDetails : findFirstReasoningDetails(content);
2591
2593
  messages.push({
2592
2594
  role: "assistant",
@@ -2609,7 +2611,7 @@ function convertToOpenRouterChatMessages(prompt) {
2609
2611
  role: "tool",
2610
2612
  tool_call_id: toolResponse.toolCallId,
2611
2613
  content: content2,
2612
- cache_control: (_g = getCacheControl(providerOptions)) != null ? _g : getCacheControl(toolResponse.providerOptions)
2614
+ cache_control: (_h = getCacheControl(providerOptions)) != null ? _h : getCacheControl(toolResponse.providerOptions)
2613
2615
  });
2614
2616
  }
2615
2617
  break;
@@ -2749,7 +2751,7 @@ var OpenRouterNonStreamChatCompletionResponseSchema = z7.union([
2749
2751
  type: z7.literal("function"),
2750
2752
  function: z7.object({
2751
2753
  name: z7.string(),
2752
- arguments: z7.string()
2754
+ arguments: z7.string().optional()
2753
2755
  }).passthrough()
2754
2756
  }).passthrough()
2755
2757
  ).optional(),
@@ -2992,7 +2994,7 @@ var OpenRouterChatLanguageModel = class {
2992
2994
  return baseArgs;
2993
2995
  }
2994
2996
  async doGenerate(options) {
2995
- 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;
2996
2998
  const providerOptions = options.providerOptions || {};
2997
2999
  const openrouterOptions = providerOptions.openrouter || {};
2998
3000
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3127,7 +3129,7 @@ var OpenRouterChatLanguageModel = class {
3127
3129
  type: "tool-call",
3128
3130
  toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3129
3131
  toolName: toolCall.function.name,
3130
- input: toolCall.function.arguments,
3132
+ input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3131
3133
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3132
3134
  openrouter: {
3133
3135
  reasoning_details: reasoningDetails
@@ -3164,7 +3166,7 @@ var OpenRouterChatLanguageModel = class {
3164
3166
  }
3165
3167
  }
3166
3168
  }
3167
- const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3169
+ const fileAnnotations = (_j = choice.message.annotations) == null ? void 0 : _j.filter(
3168
3170
  (a) => a.type === "file"
3169
3171
  );
3170
3172
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3172,7 +3174,7 @@ var OpenRouterChatLanguageModel = class {
3172
3174
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3173
3175
  );
3174
3176
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3175
- 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);
3176
3178
  return {
3177
3179
  content,
3178
3180
  finishReason: effectiveFinishReason,
@@ -3180,23 +3182,23 @@ var OpenRouterChatLanguageModel = class {
3180
3182
  warnings: [],
3181
3183
  providerMetadata: {
3182
3184
  openrouter: OpenRouterProviderMetadataSchema.parse({
3183
- provider: (_k = response.provider) != null ? _k : "",
3184
- 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 : [],
3185
3187
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3186
3188
  usage: __spreadValues(__spreadValues(__spreadValues({
3187
- promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3188
- completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3189
- totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0),
3190
- cost: (_q = response.usage) == null ? void 0 : _q.cost
3191
- }, ((_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 ? {
3192
3194
  promptTokensDetails: {
3193
3195
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3194
3196
  }
3195
- } : {}), ((_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 ? {
3196
3198
  completionTokensDetails: {
3197
3199
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3198
3200
  }
3199
- } : {}), ((_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 ? {
3200
3202
  costDetails: {
3201
3203
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3202
3204
  }
@@ -4183,7 +4185,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4183
4185
  }
4184
4186
 
4185
4187
  // src/version.ts
4186
- var VERSION2 = false ? "0.0.0-test" : "2.0.2";
4188
+ var VERSION2 = false ? "0.0.0-test" : "2.0.4";
4187
4189
 
4188
4190
  // src/provider.ts
4189
4191
  function createOpenRouter(options = {}) {