@jaypie/llm 1.3.14 → 1.3.16

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.
@@ -47,16 +47,24 @@ type BedrockRequest = Omit<ConverseCommandInput, "messages"> & {
47
47
  type AnnotatedBedrockResponse = ConverseCommandOutput & {
48
48
  __jaypieStructuredOutput?: boolean;
49
49
  };
50
+ /** Exported for tests; not part of the package's public surface. */
51
+ export declare function isCachePointUnsupportedError(error: unknown): boolean;
50
52
  export declare class BedrockAdapter extends BaseProviderAdapter {
51
53
  readonly name: "bedrock";
52
54
  readonly defaultModel: "amazon.nova-pro-v1:0";
53
55
  private _modelsFallbackToStructuredOutputTool;
54
56
  private _modelsWithoutTemperature;
57
+ private _modelsWithoutCachePoint;
58
+ private rememberModelRejectsCachePoint;
59
+ private supportsCachePoint;
55
60
  private rememberModelRejectsOutputConfig;
56
61
  private useFakeToolForStructuredOutput;
57
62
  private rememberModelRejectsTemperature;
58
63
  private supportsTemperature;
59
64
  buildRequest(request: OperateRequest): BedrockRequest;
65
+ /** Remove any cachePoint blocks so the request can be retried unsupported. */
66
+ private stripCachePoints;
67
+ private requestHasCachePoints;
60
68
  formatTools(toolkit: Toolkit): ProviderToolDefinition[];
61
69
  formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
62
70
  executeRequest(client: unknown, request: unknown, signal?: AbortSignal): Promise<AnnotatedBedrockResponse>;
@@ -21,6 +21,11 @@ export declare class OpenAiAdapter extends BaseProviderAdapter {
21
21
  private supportsTemperature;
22
22
  /** Whether `reasoning.effort` may be sent for this model. Overridden by xAI. */
23
23
  protected supportsReasoningEffort(model: string): boolean;
24
+ /**
25
+ * Whether to emit `prompt_cache_key` (OpenAI Responses API). Overridable by
26
+ * OpenAI-compatible subclasses whose backend rejects the field.
27
+ */
28
+ protected supportsPromptCacheKey(): boolean;
24
29
  /** Translate a normalized effort to this provider's `reasoning.effort` value. */
25
30
  protected mapReasoningEffort(effort: LlmEffort, model: string): LlmEffortMapping;
26
31
  buildRequest(request: OperateRequest): unknown;
@@ -42,6 +42,13 @@ interface OpenRouterUsage {
42
42
  completionTokensDetails?: {
43
43
  reasoningTokens?: number;
44
44
  };
45
+ promptTokensDetails?: {
46
+ cachedTokens?: number;
47
+ cached_tokens?: number;
48
+ };
49
+ prompt_tokens_details?: {
50
+ cached_tokens?: number;
51
+ };
45
52
  }
46
53
  interface OpenRouterResponse {
47
54
  id: string;
@@ -95,6 +102,10 @@ interface OpenRouterRequest {
95
102
  type OpenRouterContentPart = {
96
103
  type: "text";
97
104
  text: string;
105
+ cache_control?: {
106
+ type: "ephemeral";
107
+ ttl?: "5m" | "1h";
108
+ };
98
109
  } | {
99
110
  type: "image_url";
100
111
  imageUrl: {
@@ -1,6 +1,6 @@
1
1
  import { JsonObject } from "@jaypie/types";
2
2
  import { type LlmEffort } from "../constants.js";
3
- import { LlmHistory, LlmMessageType, LlmOperateOptions, LlmUsageItem } from "../types/LlmProvider.interface.js";
3
+ import { LlmCache, LlmHistory, LlmMessageType, LlmOperateOptions, LlmUsageItem } from "../types/LlmProvider.interface.js";
4
4
  import { Toolkit } from "../tools/Toolkit.class.js";
5
5
  /**
6
6
  * Standardized tool call representation across providers
@@ -73,6 +73,11 @@ export interface OperateRequest {
73
73
  effort?: LlmEffort;
74
74
  /** Provider-specific options */
75
75
  providerOptions?: JsonObject;
76
+ /**
77
+ * Prompt-caching control for the stable prefix (system + tools). `true`/
78
+ * omitted = enabled@5m, `false`/`0` = disabled, `"5m"`/`"1h"` = enabled@ttl.
79
+ */
80
+ cache?: LlmCache;
76
81
  /** Whether the request will execute over a streaming transport */
77
82
  stream?: boolean;
78
83
  /** Sampling temperature (0-2 for most providers) */
@@ -2,10 +2,20 @@ import { JsonObject } from "@jaypie/types";
2
2
  import { LlmMessageRole } from "../../types/LlmProvider.interface.js";
3
3
  export declare const ROLE_MAP: Record<LlmMessageRole, string>;
4
4
  export declare namespace Anthropic {
5
+ interface CacheControlEphemeral {
6
+ type: "ephemeral";
7
+ ttl?: "5m" | "1h";
8
+ }
5
9
  interface TextBlock {
6
10
  type: "text";
7
11
  text: string;
8
12
  }
13
+ /** Top-level `system` block form that can carry a cache breakpoint */
14
+ interface SystemTextBlockParam {
15
+ type: "text";
16
+ text: string;
17
+ cache_control?: CacheControlEphemeral;
18
+ }
9
19
  interface ToolUseBlock {
10
20
  type: "tool_use";
11
21
  id: string;
@@ -63,17 +73,20 @@ export declare namespace Anthropic {
63
73
  description?: string;
64
74
  input_schema: Messages.Tool.InputSchema;
65
75
  type?: "custom";
76
+ cache_control?: CacheControlEphemeral;
66
77
  }
67
78
  interface Usage {
68
79
  input_tokens: number;
69
80
  output_tokens: number;
70
81
  thinking_tokens?: number;
82
+ cache_read_input_tokens?: number;
83
+ cache_creation_input_tokens?: number;
71
84
  }
72
85
  interface MessageCreateParams {
73
86
  model: string;
74
87
  messages: MessageParam[];
75
88
  max_tokens: number;
76
- system?: string;
89
+ system?: string | SystemTextBlockParam[];
77
90
  tools?: Tool[];
78
91
  tool_choice?: {
79
92
  type: "auto" | "any" | "tool";
@@ -217,6 +217,7 @@ export type LlmProgressCallback = (event: LlmProgressEvent) => unknown | Promise
217
217
  * `input` is the raw, pre-interpolation input with multimodal parts preserved.
218
218
  */
219
219
  export interface LlmExchangeRequest {
220
+ cache?: LlmCache;
220
221
  data?: NaturalMap;
221
222
  effort?: LlmEffort;
222
223
  explain?: boolean;
@@ -287,7 +288,21 @@ export interface LlmExchangeEnvelope {
287
288
  timing: LlmExchangeTiming;
288
289
  }
289
290
  export type LlmExchangeCallback = (envelope: LlmExchangeEnvelope) => unknown | Promise<unknown>;
291
+ /**
292
+ * Prompt-caching control for operate()/stream().
293
+ * - `true` / omitted → caching enabled at the default `"5m"` TTL
294
+ * - `false` / `0` → caching disabled
295
+ * - `"5m"` / `"1h"` → enabled at that TTL (TTL honored by Anthropic/OpenRouter;
296
+ * other providers ignore it and cache with their own defaults)
297
+ */
298
+ export type LlmCache = boolean | 0 | "5m" | "1h";
290
299
  export interface LlmOperateOptions {
300
+ /**
301
+ * Prompt caching for the stable request prefix (system prompt + tools).
302
+ * Enabled by default; pass `false`/`0` to opt out, or `"5m"`/`"1h"` to set
303
+ * the TTL. See {@link LlmCache}.
304
+ */
305
+ cache?: LlmCache;
291
306
  data?: NaturalMap;
292
307
  /**
293
308
  * Provider-neutral reasoning effort (lowest | low | medium | high | highest).
@@ -396,6 +411,10 @@ export interface LlmUsageItem {
396
411
  output: number;
397
412
  reasoning: number;
398
413
  total: number;
414
+ /** Prompt-cache tokens served from cache this call (billed at ~0.1x input) */
415
+ cacheRead?: number;
416
+ /** Prompt-cache tokens written to cache this call (billed at ~1.25x input) */
417
+ cacheWrite?: number;
399
418
  provider?: string;
400
419
  model?: string;
401
420
  }
@@ -0,0 +1,21 @@
1
+ import { type LlmCache } from "../types/LlmProvider.interface.js";
2
+ export declare const CACHE_TTL_DEFAULT: "5m";
3
+ export type CacheTtl = "5m" | "1h";
4
+ export interface ResolvedCache {
5
+ enabled: boolean;
6
+ ttl: CacheTtl;
7
+ }
8
+ /**
9
+ * Normalize the caller-facing `cache` option into a concrete decision.
10
+ * - `undefined` / `true` → enabled at the default TTL
11
+ * - `false` / `0` → disabled
12
+ * - `"5m"` / `"1h"` → enabled at that TTL
13
+ */
14
+ export declare function resolveCache(cache: LlmCache | undefined): ResolvedCache;
15
+ /**
16
+ * Deterministic short key for providers with automatic, prefix-based caching
17
+ * (e.g. OpenAI `prompt_cache_key`). Derived from the stable prefix so the same
18
+ * system prompt + tools + model always routes to the same cache. Dependency-
19
+ * free FNV-1a; not cryptographic.
20
+ */
21
+ export declare function promptCacheKey(seed: string): string;
@@ -1,3 +1,4 @@
1
+ export * from "./cacheControl.js";
1
2
  export * from "./determineModelProvider.js";
2
3
  export * from "./extractReasoning.js";
3
4
  export * from "./fillFormatArrays.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/llm",
3
- "version": "1.3.14",
3
+ "version": "1.3.16",
4
4
  "description": "Large language model utilities",
5
5
  "repository": {
6
6
  "type": "git",