@hiper2d/ai-agents 0.32.0 → 0.34.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.
package/dist/index.mjs CHANGED
@@ -869,10 +869,11 @@ var LLM_CONSTANTS = {
869
869
  var DEFAULT_MAX_OUTPUT_TOKENS = 8192;
870
870
  var DEFAULT_REQUEST_TIMEOUT_MS = 6e4;
871
871
  var SupportedAiModels = {
872
- // Claude Fable 5.1 (2026-09-02, was Fable 5) - frontier reasoning model. Thinking is always on (no non-thinking variant).
872
+ // Claude Fable 5.2 (2026-11-10, was 5.1 since 2026-09-02, Fable 5 before) - frontier reasoning model.
873
+ // Thinking is always on (no non-thinking variant). Id verified against GET /v1/models.
873
874
  [LLM_CONSTANTS.CLAUDE_FABLE]: {
874
- displayName: "Claude Fable 5.1",
875
- modelApiName: "claude-fable-5-1",
875
+ displayName: "Claude Fable 5.2",
876
+ modelApiName: "claude-fable-5-2",
876
877
  apiKeyName: API_KEY_CONSTANTS.ANTHROPIC,
877
878
  hasThinking: true,
878
879
  reasoningEffort: "high",
@@ -1507,6 +1508,7 @@ var MODEL_PRICING = {
1507
1508
  [SupportedAiModels[LLM_CONSTANTS.CLAUDE_FABLE].modelApiName]: {
1508
1509
  // Full 1M context window at standard pricing (no extended-context premium).
1509
1510
  // Fable 5.1 (2026-09-02): same $10/$50 as Fable 5, but cache reads dropped to $0.25/MTok.
1511
+ // Fable 5.2 (2026-11-10): same prices as 5.1.
1510
1512
  inputPrice: 10,
1511
1513
  outputPrice: 50,
1512
1514
  cacheHitPrice: 0.25
@@ -2872,22 +2874,29 @@ ${instruction}`);
2872
2874
 
2873
2875
  // src/agents/anthropic-agent.ts
2874
2876
  import { Anthropic } from "@anthropic-ai/sdk";
2877
+ var CACHE_WRITE_MULTIPLIER = { "5m": 1.25, "1h": 2 };
2875
2878
  var rejectsDisabledThinking = (model) => model.includes("fable") || model.includes("opus-5-5");
2876
2879
  var usesBetweenToolsForOff = (model) => model.includes("sonnet-5-5");
2877
2880
  var usesAdaptiveThinking = (model) => !model.includes("haiku-4");
2878
2881
  var ClaudeAgent = class extends AbstractAgent {
2879
2882
  client;
2880
2883
  /**
2881
- * TTL for every breakpoint this agent places (system tiers and the message anchor).
2882
- * Anthropic bills a 5m write at 1.25x input, a 1h write at 2x, reads at 0.1x, and a read
2883
- * refreshes the timer on either TTL. Default '1h' because the main consumer runs at human
2884
- * pace: consecutive calls for one agent measured 12-78 minutes apart, so 5m entries
2885
- * expired before they were ever read (0-16% hit rate over 30 days, hits only on gaps
2886
- * under five minutes). Set '5m' for continuous traffic where every call lands inside the
2887
- * window; there the cheaper write wins. One knob for all breakpoints on purpose: Anthropic
2884
+ * TTL for every breakpoint this agent places (system tiers and the message anchor), or
2885
+ * 'off' to place none. Anthropic bills a 5m write at 1.25x input, a 1h write at 2x, reads
2886
+ * at 0.1x, and a read refreshes the timer on either TTL; with no breakpoint every token is
2887
+ * plain 1x input (Claude has no implicit caching). Caching pays only when enough of what
2888
+ * is written gets read back: break-even is ~22% of the prompt read at 5m and ~53% at 1h.
2889
+ * Measured on Werewolf (human-paced, consecutive calls for one bot 12-78 minutes apart):
2890
+ * 5m read 0-16% back, 1h 42-43% over 30 days of production bot turns (2026-11-06) — both
2891
+ * cost more than no cache, so that host sets 'off'. Default '1h' for continuous traffic
2892
+ * where calls land inside the window. One knob for all breakpoints on purpose: Anthropic
2888
2893
  * requires 1h entries to precede 5m ones, and a single TTL keeps that trivially true.
2889
2894
  */
2890
2895
  cacheTtl = "1h";
2896
+ /** The cache_control to attach to a breakpoint, or none when caching is off. */
2897
+ get cacheControl() {
2898
+ return this.cacheTtl === "off" ? {} : { cache_control: { type: "ephemeral", ttl: this.cacheTtl } };
2899
+ }
2891
2900
  // System-prompt breakpoints, one per cache tier (see CACHE_TIER_MARKER):
2892
2901
  // block 1 - shared static rules, byte-identical across all bots and games with the
2893
2902
  // same rule set. Caches are scoped per model, so one entry serves every bot
@@ -2903,7 +2912,7 @@ var ClaudeAgent = class extends AbstractAgent {
2903
2912
  get defaultParams() {
2904
2913
  return {
2905
2914
  max_tokens: this.maxOutputTokens,
2906
- system: this.instructionParts.map((part) => ({ type: "text", text: part, cache_control: { type: "ephemeral", ttl: this.cacheTtl } })),
2915
+ system: this.instructionParts.map((part) => ({ type: "text", text: part, ...this.cacheControl })),
2907
2916
  model: this.model
2908
2917
  };
2909
2918
  }
@@ -3002,6 +3011,9 @@ var ClaudeAgent = class extends AbstractAgent {
3002
3011
  * which for us is exactly the never-repeated tail — every entry it wrote would be dead.
3003
3012
  */
3004
3013
  applyCacheBreakpoint(messages) {
3014
+ if (this.cacheTtl === "off") {
3015
+ return;
3016
+ }
3005
3017
  if (messages.length < 2) {
3006
3018
  return;
3007
3019
  }
@@ -3024,9 +3036,11 @@ var ClaudeAgent = class extends AbstractAgent {
3024
3036
  * Builds TokenUsage from the response. Anthropic's input_tokens EXCLUDES cached tokens
3025
3037
  * (total prompt = input_tokens + cache_read + cache_creation), unlike the OpenAI-shaped
3026
3038
  * providers whose prompt_tokens include them — so reconstruct the full prompt size here
3027
- * before pricing. Cache reads bill at the cacheHitPrice (~0.1x); cache writes bill at
3028
- * 1.25x input, which MODEL_PRICING doesn't model, so written tokens are priced at the
3029
- * plain input rate (~20% undercount on the written span only).
3039
+ * before pricing. Cache reads bill at the cacheHitPrice (~0.1x). Cache writes bill at a
3040
+ * multiple of input by TTL (1.25x for 5m, 2x for 1h); MODEL_PRICING has no write rate, so
3041
+ * the premium is added on top at the call's effective input rate (which keeps any
3042
+ * long-context tier). Until 0.33.0 writes were priced at plain input, under-reporting
3043
+ * every 1h write by half.
3030
3044
  */
3031
3045
  buildTokenUsage(usage) {
3032
3046
  const cacheReadTokens = usage.cache_read_input_tokens ?? 0;
@@ -3034,7 +3048,14 @@ var ClaudeAgent = class extends AbstractAgent {
3034
3048
  const uncachedInputTokens = usage.input_tokens || 0;
3035
3049
  const inputTokens = uncachedInputTokens + cacheReadTokens + cacheWriteTokens;
3036
3050
  const outputTokens = usage.output_tokens || 0;
3037
- const cost = calculateAnthropicCost(this.model, inputTokens, outputTokens, cacheReadTokens);
3051
+ let cost = calculateAnthropicCost(this.model, inputTokens, outputTokens, cacheReadTokens);
3052
+ if (cacheWriteTokens > 0) {
3053
+ const byTtl = usage.cache_creation;
3054
+ const written1h = byTtl ? byTtl.ephemeral_1h_input_tokens ?? 0 : this.cacheTtl === "1h" ? cacheWriteTokens : 0;
3055
+ const written5m = byTtl ? byTtl.ephemeral_5m_input_tokens ?? 0 : cacheWriteTokens - written1h;
3056
+ const inputRate = inputTokens > 0 ? calculateAnthropicCost(this.model, inputTokens, 0, 0) / inputTokens : 0;
3057
+ cost += inputRate * (written1h * (CACHE_WRITE_MULTIPLIER["1h"] - 1) + written5m * (CACHE_WRITE_MULTIPLIER["5m"] - 1));
3058
+ }
3038
3059
  if (cacheReadTokens > 0 || cacheWriteTokens > 0) {
3039
3060
  this.logger(`\u{1F4BE} Prompt cache: ${cacheReadTokens} read, ${cacheWriteTokens} written, ${uncachedInputTokens} uncached`);
3040
3061
  }
@@ -3043,8 +3064,9 @@ var ClaudeAgent = class extends AbstractAgent {
3043
3064
  outputTokens,
3044
3065
  totalTokens: inputTokens + outputTokens,
3045
3066
  costUSD: cost,
3046
- // Cache reads only — writes are a billing premium, not reuse of prior context.
3047
- ...cacheReadTokens > 0 ? { cachedInputTokens: cacheReadTokens } : {}
3067
+ // Breakdowns of inputTokens, omitted when zero (Firestore rejects undefined).
3068
+ ...cacheReadTokens > 0 ? { cachedInputTokens: cacheReadTokens } : {},
3069
+ ...cacheWriteTokens > 0 ? { cacheWriteTokens } : {}
3048
3070
  };
3049
3071
  }
3050
3072
  convertRole(role) {