@jambonz/llm 0.6.3 → 0.6.5

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
@@ -86,6 +86,9 @@ async function* streamFromOpenAI(client, req, options) {
86
86
  if (req.cacheKey && options.includeCacheKey !== false) {
87
87
  body.prompt_cache_key = req.cacheKey;
88
88
  }
89
+ if (req.cacheKey && options.openRouterAutoCache && /^~?anthropic\//.test(req.model)) {
90
+ body.cache_control = { type: "ephemeral" };
91
+ }
89
92
  const effectiveMaxTokens = req.maxTokens ?? options.defaultMaxTokens;
90
93
  if (effectiveMaxTokens !== void 0) {
91
94
  const param = options.tokensParam ?? (needsMaxCompletionTokens(req.model) ? "max_completion_tokens" : "max_tokens");
@@ -510,10 +513,13 @@ var OpenAIAdapter = class {
510
513
  vendor = openAIManifest.vendor;
511
514
  acceptedAuth = ["apiKey"];
512
515
  client;
516
+ /** baseURL is OpenRouter: enables its automatic prompt caching for Anthropic models. */
517
+ isOpenRouter = false;
513
518
  init(auth, client) {
514
519
  if (!auth.apiKey) {
515
520
  throw new Error("OpenAIAdapter: apiKey is required");
516
521
  }
522
+ this.isOpenRouter = /(^|\/\/|\.)openrouter\.ai(\/|:|$)/i.test(auth.baseURL ?? "");
517
523
  this.client = new OpenAI({
518
524
  apiKey: auth.apiKey,
519
525
  ...auth.baseURL ? { baseURL: auth.baseURL } : {},
@@ -525,7 +531,8 @@ var OpenAIAdapter = class {
525
531
  return streamFromOpenAI(this.ensureClient(), req, {
526
532
  knownModels: this.knownModels(),
527
533
  vendorMetadataExtractor: OPENAI_METADATA_EXTRACTOR,
528
- includeCacheKey: this.forwardCacheKey()
534
+ includeCacheKey: this.forwardCacheKey(),
535
+ openRouterAutoCache: this.isOpenRouter
529
536
  });
530
537
  }
531
538
  appendAssistantToolCall(history, toolCalls, assistantText) {
@@ -2799,11 +2806,12 @@ var BedrockAdapter = class {
2799
2806
  if (Object.keys(inferenceConfig).length > 0) {
2800
2807
  commandInput.inferenceConfig = inferenceConfig;
2801
2808
  }
2802
- if (req.cacheKey) {
2809
+ const caching = req.cacheKey ? cachePointSupport(req.model) : void 0;
2810
+ if (caching) {
2803
2811
  if (commandInput.system) {
2804
2812
  commandInput.system.push({ cachePoint: { type: "default" } });
2805
2813
  }
2806
- if (tools) {
2814
+ if (tools && caching.tools) {
2807
2815
  tools.tools.push({ cachePoint: { type: "default" } });
2808
2816
  }
2809
2817
  markLastMessageForCaching2(messages);
@@ -3027,6 +3035,11 @@ function buildWireMessages3(req) {
3027
3035
  }
3028
3036
  return out;
3029
3037
  }
3038
+ function cachePointSupport(model) {
3039
+ if (model.includes("anthropic.claude")) return { tools: true };
3040
+ if (model.includes("amazon.nova")) return { tools: false };
3041
+ return void 0;
3042
+ }
3030
3043
  function markLastMessageForCaching2(messages) {
3031
3044
  if (messages.length === 0) return;
3032
3045
  const last = messages[messages.length - 1];