@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.
@@ -186,6 +186,25 @@ type OpenRouterChatSettings = {
186
186
  */
187
187
  engine?: Engine;
188
188
  };
189
+ /**
190
+ * Enable Anthropic automatic prompt caching by setting a top-level cache_control
191
+ * directive on the request body. When set to `{ type: 'ephemeral' }`, Anthropic
192
+ * will automatically cache eligible content in your prompts.
193
+ *
194
+ * Only works with Anthropic models through OpenRouter.
195
+ *
196
+ * @see https://platform.claude.com/docs/en/build-with-claude/prompt-caching#automatic-caching
197
+ * @see https://openrouter.ai/docs
198
+ */
199
+ cache_control?: {
200
+ type: 'ephemeral';
201
+ /**
202
+ * Optional time-to-live for the cache entry.
203
+ * - `'5m'` — 5 minutes (default when omitted)
204
+ * - `'1h'` — 1 hour
205
+ */
206
+ ttl?: '5m' | '1h';
207
+ };
189
208
  /**
190
209
  * Debug options for troubleshooting API requests.
191
210
  * Only works with streaming requests.
@@ -186,6 +186,25 @@ type OpenRouterChatSettings = {
186
186
  */
187
187
  engine?: Engine;
188
188
  };
189
+ /**
190
+ * Enable Anthropic automatic prompt caching by setting a top-level cache_control
191
+ * directive on the request body. When set to `{ type: 'ephemeral' }`, Anthropic
192
+ * will automatically cache eligible content in your prompts.
193
+ *
194
+ * Only works with Anthropic models through OpenRouter.
195
+ *
196
+ * @see https://platform.claude.com/docs/en/build-with-claude/prompt-caching#automatic-caching
197
+ * @see https://openrouter.ai/docs
198
+ */
199
+ cache_control?: {
200
+ type: 'ephemeral';
201
+ /**
202
+ * Optional time-to-live for the cache entry.
203
+ * - `'5m'` — 5 minutes (default when omitted)
204
+ * - `'1h'` — 1 hour
205
+ */
206
+ ttl?: '5m' | '1h';
207
+ };
189
208
  /**
190
209
  * Debug options for troubleshooting API requests.
191
210
  * Only works with streaming requests.
@@ -3145,7 +3145,9 @@ var OpenRouterChatLanguageModel = class {
3145
3145
  // Provider routing settings:
3146
3146
  provider: this.settings.provider,
3147
3147
  // Debug settings:
3148
- debug: this.settings.debug
3148
+ debug: this.settings.debug,
3149
+ // Anthropic automatic caching:
3150
+ cache_control: this.settings.cache_control
3149
3151
  }, this.config.extraBody), this.settings.extraBody);
3150
3152
  if (tools && tools.length > 0) {
3151
3153
  const mappedTools = tools.filter(
@@ -3166,10 +3168,11 @@ var OpenRouterChatLanguageModel = class {
3166
3168
  return baseArgs;
3167
3169
  }
3168
3170
  async doGenerate(options) {
3169
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
3171
+ var _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
3170
3172
  const providerOptions = options.providerOptions || {};
3171
3173
  const openrouterOptions = providerOptions.openrouter || {};
3172
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
3174
+ const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
3175
+ const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
3173
3176
  const { value: responseValue, responseHeaders } = await postJsonToApi({
3174
3177
  url: this.config.url({
3175
3178
  path: "/chat/completions",
@@ -3206,7 +3209,7 @@ var OpenRouterChatLanguageModel = class {
3206
3209
  });
3207
3210
  }
3208
3211
  const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
3209
- const reasoningDetails = (_a16 = choice.message.reasoning_details) != null ? _a16 : [];
3212
+ const reasoningDetails = (_b16 = choice.message.reasoning_details) != null ? _b16 : [];
3210
3213
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
3211
3214
  switch (detail.type) {
3212
3215
  case "reasoning.text" /* Text */: {
@@ -3275,9 +3278,9 @@ var OpenRouterChatLanguageModel = class {
3275
3278
  for (const toolCall of choice.message.tool_calls) {
3276
3279
  content.push({
3277
3280
  type: "tool-call",
3278
- toolCallId: (_b16 = toolCall.id) != null ? _b16 : generateId(),
3281
+ toolCallId: (_c = toolCall.id) != null ? _c : generateId(),
3279
3282
  toolName: toolCall.function.name,
3280
- input: (_c = toolCall.function.arguments) != null ? _c : "{}",
3283
+ input: (_d = toolCall.function.arguments) != null ? _d : "{}",
3281
3284
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3282
3285
  openrouter: {
3283
3286
  reasoning_details: reasoningDetails
@@ -3304,19 +3307,19 @@ var OpenRouterChatLanguageModel = class {
3304
3307
  sourceType: "url",
3305
3308
  id: annotation.url_citation.url,
3306
3309
  url: annotation.url_citation.url,
3307
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3310
+ title: (_e = annotation.url_citation.title) != null ? _e : "",
3308
3311
  providerMetadata: {
3309
3312
  openrouter: {
3310
- content: (_e = annotation.url_citation.content) != null ? _e : "",
3311
- startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3312
- endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3313
+ content: (_f = annotation.url_citation.content) != null ? _f : "",
3314
+ startIndex: (_g = annotation.url_citation.start_index) != null ? _g : 0,
3315
+ endIndex: (_h = annotation.url_citation.end_index) != null ? _h : 0
3313
3316
  }
3314
3317
  }
3315
3318
  });
3316
3319
  }
3317
3320
  }
3318
3321
  }
3319
- const fileAnnotations = (_h = choice.message.annotations) == null ? void 0 : _h.filter(
3322
+ const fileAnnotations = (_i = choice.message.annotations) == null ? void 0 : _i.filter(
3320
3323
  (a) => a.type === "file"
3321
3324
  );
3322
3325
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3324,7 +3327,7 @@ var OpenRouterChatLanguageModel = class {
3324
3327
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3325
3328
  );
3326
3329
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3327
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_i = choice.finish_reason) != null ? _i : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3330
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_j = choice.finish_reason) != null ? _j : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3328
3331
  return {
3329
3332
  content,
3330
3333
  finishReason: effectiveFinishReason,
@@ -3332,22 +3335,22 @@ var OpenRouterChatLanguageModel = class {
3332
3335
  warnings: [],
3333
3336
  providerMetadata: {
3334
3337
  openrouter: OpenRouterProviderMetadataSchema.parse({
3335
- provider: (_j = response.provider) != null ? _j : "",
3336
- reasoning_details: (_k = choice.message.reasoning_details) != null ? _k : [],
3338
+ provider: (_k = response.provider) != null ? _k : "",
3339
+ reasoning_details: (_l = choice.message.reasoning_details) != null ? _l : [],
3337
3340
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3338
3341
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3339
- promptTokens: (_l = usageInfo.inputTokens.total) != null ? _l : 0,
3340
- completionTokens: (_m = usageInfo.outputTokens.total) != null ? _m : 0,
3341
- totalTokens: ((_n = usageInfo.inputTokens.total) != null ? _n : 0) + ((_o = usageInfo.outputTokens.total) != null ? _o : 0)
3342
- }, ((_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 ? {
3342
+ promptTokens: (_m = usageInfo.inputTokens.total) != null ? _m : 0,
3343
+ completionTokens: (_n = usageInfo.outputTokens.total) != null ? _n : 0,
3344
+ totalTokens: ((_o = usageInfo.inputTokens.total) != null ? _o : 0) + ((_p = usageInfo.outputTokens.total) != null ? _p : 0)
3345
+ }, ((_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 ? {
3343
3346
  promptTokensDetails: {
3344
3347
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3345
3348
  }
3346
- } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
3349
+ } : {}), ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? {
3347
3350
  completionTokensDetails: {
3348
3351
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3349
3352
  }
3350
- } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
3353
+ } : {}), ((_w = (_v = response.usage) == null ? void 0 : _v.cost_details) == null ? void 0 : _w.upstream_inference_cost) != null ? {
3351
3354
  costDetails: {
3352
3355
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3353
3356
  }
@@ -3363,10 +3366,11 @@ var OpenRouterChatLanguageModel = class {
3363
3366
  };
3364
3367
  }
3365
3368
  async doStream(options) {
3366
- var _a16;
3369
+ var _b16;
3367
3370
  const providerOptions = options.providerOptions || {};
3368
3371
  const openrouterOptions = providerOptions.openrouter || {};
3369
- const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
3372
+ const _a16 = openrouterOptions, { cacheControl } = _a16, restOpenrouterOptions = __objRest(_a16, ["cacheControl"]);
3373
+ const args = __spreadValues(__spreadValues(__spreadValues({}, this.getArgs(options)), restOpenrouterOptions), cacheControl != null && !("cache_control" in restOpenrouterOptions) ? { cache_control: cacheControl } : {});
3370
3374
  const { value: response, responseHeaders } = await postJsonToApi({
3371
3375
  url: this.config.url({
3372
3376
  path: "/chat/completions",
@@ -3378,7 +3382,7 @@ var OpenRouterChatLanguageModel = class {
3378
3382
  // only include stream_options when in strict compatibility mode:
3379
3383
  stream_options: this.config.compatibility === "strict" ? __spreadValues({
3380
3384
  include_usage: true
3381
- }, ((_a16 = this.settings.usage) == null ? void 0 : _a16.include) ? { include_usage: true } : {}) : void 0
3385
+ }, ((_b16 = this.settings.usage) == null ? void 0 : _b16.include) ? { include_usage: true } : {}) : void 0
3382
3386
  }),
3383
3387
  failedResponseHandler: openrouterFailedResponseHandler,
3384
3388
  successfulResponseHandler: createEventSourceResponseHandler(
@@ -3418,7 +3422,7 @@ var OpenRouterChatLanguageModel = class {
3418
3422
  stream: response.pipeThrough(
3419
3423
  new TransformStream({
3420
3424
  transform(chunk, controller) {
3421
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3425
+ var _a17, _b17, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3422
3426
  if (options.includeRawChunks) {
3423
3427
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3424
3428
  }
@@ -3455,7 +3459,7 @@ var OpenRouterChatLanguageModel = class {
3455
3459
  Object.assign(usage.outputTokens, computed.outputTokens);
3456
3460
  rawUsage = value.usage;
3457
3461
  const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
3458
- const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
3462
+ const completionTokens = (_b17 = value.usage.completion_tokens) != null ? _b17 : 0;
3459
3463
  openrouterUsage.promptTokens = promptTokens;
3460
3464
  if (value.usage.prompt_tokens_details) {
3461
3465
  openrouterUsage.promptTokensDetails = {