// src/install/patches/ctxSessionstartSuppress.ts
import type { PatchSpec } from "../patcher/PatchManager.ts";

const ANCHOR = "let additionalContext = ROUTING_BLOCK;";

/**
 * Suppress ctx-mode routing block injection when ANTHROPIC_BASE_URL points at a
 * local fewtok proxy (127.x, localhost, or [::1]).
 * Replacement reads this env var in sessionstart.mjs (Node context) and emits ""
 * instead of ROUTING_BLOCK (~700 tokens saved per new session).
 * All downstream additionalContext += lines (compact/resume) run unchanged.
 */
export const CTX_SESSIONSTART_SUPPRESS: PatchSpec = {
  id: "ctx-sessionstart-suppress",
  version: "v1",
  anchor: ANCHOR,
  replacement: [
    "// @fewtok-patch-begin:ctx-sessionstart-suppress:v1",
    'let additionalContext = /^https?:\\/\\/(127\\.|localhost|\\[::1\\])/.test(process.env.ANTHROPIC_BASE_URL || "") ? "" : ROUTING_BLOCK;',
    "// @fewtok-patch-end:ctx-sessionstart-suppress",
  ].join("\n"),
  original: ANCHOR,
};
