@openrouter/ai-sdk-provider 2.2.5 → 2.3.0

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.
@@ -3111,7 +3111,9 @@ var OpenRouterChatLanguageModel = class {
3111
3111
  // Provider routing settings:
3112
3112
  provider: this.settings.provider,
3113
3113
  // Debug settings:
3114
- debug: this.settings.debug
3114
+ debug: this.settings.debug,
3115
+ // Anthropic automatic caching:
3116
+ cache_control: this.settings.cache_control
3115
3117
  }, this.config.extraBody), this.settings.extraBody);
3116
3118
  if (tools && tools.length > 0) {
3117
3119
  const mappedTools = tools.filter(
@@ -3132,10 +3134,11 @@ var OpenRouterChatLanguageModel = class {
3132
3134
  return baseArgs;
3133
3135
  }
3134
3136
  async doGenerate(options) {
3135
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
3137
+ var _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
3136
3138
  const providerOptions = options.providerOptions || {};
3137
3139
  const openrouterOptions = providerOptions.openrouter || {};
3138
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
3140
+ const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
3141
+ const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
3139
3142
  const { value: responseValue, responseHeaders } = await postJsonToApi({
3140
3143
  url: this.config.url({
3141
3144
  path: "/chat/completions",
@@ -3172,7 +3175,7 @@ var OpenRouterChatLanguageModel = class {
3172
3175
  });
3173
3176
  }
3174
3177
  const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
3175
- const reasoningDetails = (_a16 = choice.message.reasoning_details) != null ? _a16 : [];
3178
+ const reasoningDetails = (_b16 = choice.message.reasoning_details) != null ? _b16 : [];
3176
3179
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
3177
3180
  switch (detail.type) {
3178
3181
  case "reasoning.text" /* Text */: {
@@ -3241,9 +3244,9 @@ var OpenRouterChatLanguageModel = class {
3241
3244
  for (const toolCall of choice.message.tool_calls) {
3242
3245
  content.push({
3243
3246
  type: "tool-call",
3244
- toolCallId: (_b16 = toolCall.id) != null ? _b16 : generateId(),
3247
+ toolCallId: (_c = toolCall.id) != null ? _c : generateId(),
3245
3248
  toolName: toolCall.function.name,
3246
- input: (_c = toolCall.function.arguments) != null ? _c : "{}",
3249
+ input: (_d = toolCall.function.arguments) != null ? _d : "{}",
3247
3250
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3248
3251
  openrouter: {
3249
3252
  reasoning_details: reasoningDetails
@@ -3270,19 +3273,19 @@ var OpenRouterChatLanguageModel = class {
3270
3273
  sourceType: "url",
3271
3274
  id: annotation.url_citation.url,
3272
3275
  url: annotation.url_citation.url,
3273
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3276
+ title: (_e = annotation.url_citation.title) != null ? _e : "",
3274
3277
  providerMetadata: {
3275
3278
  openrouter: {
3276
- content: (_e = annotation.url_citation.content) != null ? _e : "",
3277
- startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3278
- endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3279
+ content: (_f = annotation.url_citation.content) != null ? _f : "",
3280
+ startIndex: (_g = annotation.url_citation.start_index) != null ? _g : 0,
3281
+ endIndex: (_h = annotation.url_citation.end_index) != null ? _h : 0
3279
3282
  }
3280
3283
  }
3281
3284
  });
3282
3285
  }
3283
3286
  }
3284
3287
  }
3285
- const fileAnnotations = (_h = choice.message.annotations) == null ? void 0 : _h.filter(
3288
+ const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3286
3289
  (a) => a.type === "file"
3287
3290
  );
3288
3291
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3290,7 +3293,7 @@ var OpenRouterChatLanguageModel = class {
3290
3293
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3291
3294
  );
3292
3295
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3293
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_i = choice.finish_reason) != null ? _i : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3296
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3294
3297
  return {
3295
3298
  content,
3296
3299
  finishReason: effectiveFinishReason,
@@ -3298,22 +3301,22 @@ var OpenRouterChatLanguageModel = class {
3298
3301
  warnings: [],
3299
3302
  providerMetadata: {
3300
3303
  openrouter: OpenRouterProviderMetadataSchema.parse({
3301
- provider: (_j = response.provider) != null ? _j : "",
3302
- reasoning_details: (_k = choice.message.reasoning_details) != null ? _k : [],
3304
+ provider: (_k = response.provider) != null ? _k : "",
3305
+ reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3303
3306
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3304
3307
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3305
- promptTokens: (_l = usageInfo.inputTokens.total) != null ? _l : 0,
3306
- completionTokens: (_m = usageInfo.outputTokens.total) != null ? _m : 0,
3307
- totalTokens: ((_n = usageInfo.inputTokens.total) != null ? _n : 0) + ((_o = usageInfo.outputTokens.total) != null ? _o : 0)
3308
- }, ((_p = response.usage) == null ? void 0 : _p.cost) != null ? { cost: response.usage.cost } : {}), ((_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? {
3308
+ promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3309
+ completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3310
+ totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0)
3311
+ }, ((_q = response.usage) == null ? void 0 : _q.cost) != null ? { cost: response.usage.cost } : {}), ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? {
3309
3312
  promptTokensDetails: {
3310
3313
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3311
3314
  }
3312
- } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
3315
+ } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3313
3316
  completionTokensDetails: {
3314
3317
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3315
3318
  }
3316
- } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
3319
+ } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3317
3320
  costDetails: {
3318
3321
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3319
3322
  }
@@ -3329,10 +3332,11 @@ var OpenRouterChatLanguageModel = class {
3329
3332
  };
3330
3333
  }
3331
3334
  async doStream(options) {
3332
- var _a16;
3335
+ var _b16;
3333
3336
  const providerOptions = options.providerOptions || {};
3334
3337
  const openrouterOptions = providerOptions.openrouter || {};
3335
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
3338
+ const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
3339
+ const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
3336
3340
  const { value: response, responseHeaders } = await postJsonToApi({
3337
3341
  url: this.config.url({
3338
3342
  path: "/chat/completions",
@@ -3344,7 +3348,7 @@ var OpenRouterChatLanguageModel = class {
3344
3348
  // only include stream_options when in strict compatibility mode:
3345
3349
  stream_options: this.config.compatibility === "strict" ? __spreadValues({
3346
3350
  include_usage: true
3347
- }, ((_a16 = this.settings.usage) == null ? void 0 : _a16.include) ? { include_usage: true } : {}) : void 0
3351
+ }, ((_b16 = this.settings.usage) == null ? void 0 : _b16.include) ? { include_usage: true } : {}) : void 0
3348
3352
  }),
3349
3353
  failedResponseHandler: openrouterFailedResponseHandler,
3350
3354
  successfulResponseHandler: createEventSourceResponseHandler(
@@ -3384,7 +3388,7 @@ var OpenRouterChatLanguageModel = class {
3384
3388
  stream: response.pipeThrough(
3385
3389
  new TransformStream({
3386
3390
  transform(chunk, controller) {
3387
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3391
+ var _a17, _b17, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3388
3392
  if (options.includeRawChunks) {
3389
3393
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3390
3394
  }
@@ -3421,7 +3425,7 @@ var OpenRouterChatLanguageModel = class {
3421
3425
  Object.assign(usage.outputTokens, computed.outputTokens);
3422
3426
  rawUsage = value.usage;
3423
3427
  const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
3424
- const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
3428
+ const completionTokens = (_b17 = value.usage.completion_tokens) != null ? _b17 : 0;
3425
3429
  openrouterUsage.promptTokens = promptTokens;
3426
3430
  if (value.usage.prompt_tokens_details) {
3427
3431
  openrouterUsage.promptTokensDetails = {