@vikrant82/opencode-cache-keepalive 0.1.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.
Files changed (44) hide show
  1. package/dist/index.d.ts +4 -0
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +582 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/lib/auth.d.ts +11 -0
  6. package/dist/lib/auth.d.ts.map +1 -0
  7. package/dist/lib/config.d.ts +31 -0
  8. package/dist/lib/config.d.ts.map +1 -0
  9. package/dist/lib/control.d.ts +8 -0
  10. package/dist/lib/control.d.ts.map +1 -0
  11. package/dist/lib/keepalive.d.ts +42 -0
  12. package/dist/lib/keepalive.d.ts.map +1 -0
  13. package/dist/lib/logger.d.ts +9 -0
  14. package/dist/lib/logger.d.ts.map +1 -0
  15. package/dist/lib/model.d.ts +8 -0
  16. package/dist/lib/model.d.ts.map +1 -0
  17. package/dist/lib/paths.d.ts +13 -0
  18. package/dist/lib/paths.d.ts.map +1 -0
  19. package/dist/lib/state.d.ts +74 -0
  20. package/dist/lib/state.d.ts.map +1 -0
  21. package/dist/lib/system.d.ts +11 -0
  22. package/dist/lib/system.d.ts.map +1 -0
  23. package/dist/lib/tui/commands.d.ts +3 -0
  24. package/dist/lib/tui/commands.d.ts.map +1 -0
  25. package/dist/lib/tui/footer.d.ts +14 -0
  26. package/dist/lib/tui/footer.d.ts.map +1 -0
  27. package/dist/lib/tui/format.d.ts +3 -0
  28. package/dist/lib/tui/format.d.ts.map +1 -0
  29. package/dist/tui.d.ts +7 -0
  30. package/dist/tui.d.ts.map +1 -0
  31. package/lib/auth.ts +33 -0
  32. package/lib/config.ts +101 -0
  33. package/lib/control.ts +37 -0
  34. package/lib/keepalive.ts +345 -0
  35. package/lib/logger.ts +21 -0
  36. package/lib/model.ts +19 -0
  37. package/lib/paths.ts +31 -0
  38. package/lib/state.ts +157 -0
  39. package/lib/system.ts +19 -0
  40. package/lib/tui/commands.ts +54 -0
  41. package/lib/tui/footer.tsx +156 -0
  42. package/lib/tui/format.ts +13 -0
  43. package/package.json +71 -0
  44. package/tui.tsx +25 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../lib/auth.ts","../lib/config.ts","../lib/control.ts","../lib/paths.ts","../lib/model.ts","../lib/keepalive.ts","../lib/logger.ts","../lib/state.ts","../lib/system.ts","../index.ts"],"sourcesContent":["/**\n * When opencode runs with `OPENCODE_SERVER_PASSWORD`, the local HTTP API requires\n * Basic auth. The SDK client handed to plugins does not carry it automatically,\n * so we attach an interceptor. No-op when the server is not password protected.\n *\n * Same approach as opencode-dynamic-context-pruning's auth helper.\n */\nexport function isSecureMode(): boolean {\n return !!process.env.OPENCODE_SERVER_PASSWORD\n}\n\nexport function getAuthorizationHeader(): string | undefined {\n const password = process.env.OPENCODE_SERVER_PASSWORD\n if (!password) return undefined\n const username = process.env.OPENCODE_SERVER_USERNAME ?? \"opencode\"\n const credentials = Buffer.from(`${username}:${password}`).toString(\"base64\")\n return `Basic ${credentials}`\n}\n\nexport function configureClientAuth(client: any): void {\n const header = getAuthorizationHeader()\n if (!header) return\n\n const inner = client?._client ?? client?.client\n if (!inner?.interceptors?.request) return\n\n inner.interceptors.request.use((request: Request) => {\n if (!request.headers.has(\"Authorization\")) {\n request.headers.set(\"Authorization\", header)\n }\n return request\n })\n}\n","export type KeepaliveConfig = {\n /** Master switch. When false the plugin registers no timers or hooks. */\n enabled: boolean\n /** Milliseconds between pings. Must stay under the shortest supported provider TTL. */\n intervalMs: number\n /**\n * How long to keep warming after the last real response before giving up and\n * letting the cache go cold. The default follows the Copilot Claude\n * write/read break-even; providers with automatic caching may be cheaper.\n */\n windowMs: number\n /**\n * Append a stable keepalive instruction to the system prompt so the model\n * answers a `~` ping with a single `~` token and never calls tools. The\n * instruction is added to real turns too, so the cached prefix stays identical.\n */\n injectSystemInstruction: boolean\n /** The single character/token used as the keepalive suffix. */\n pingToken: string\n /** Remove synthetic ping turns after measuring their cache usage. */\n revertPing: boolean\n /** Warm subagent/child sessions too. Off by default to avoid wasted pings. */\n includeChildSessions: boolean\n debug: boolean\n /** providerID substrings eligible for warming (cache-controlled providers). */\n providerAllowlist: string[]\n /** modelID substrings eligible for warming. */\n modelAllowlist: string[]\n}\n\nconst DEFAULTS: KeepaliveConfig = {\n enabled: true,\n intervalMs: 270_000, // 4.5 min — below the shortest supported cache TTL\n windowMs: 3_300_000, // 55 min — the Copilot Claude write/read break-even\n injectSystemInstruction: true,\n pingToken: \"~\",\n revertPing: true,\n includeChildSessions: false,\n debug: false,\n providerAllowlist: [\"copilot\"],\n modelAllowlist: [\"claude\", \"anthropic\", \"sonnet\", \"opus\", \"haiku\", \"gpt\"],\n}\n\nexport function getConfig(options: Record<string, unknown> | undefined): KeepaliveConfig {\n const o = options ?? {}\n const env = process.env\n\n return {\n enabled: boolOpt(o.enabled, env.OPENCODE_KEEPALIVE_ENABLED !== \"false\" && DEFAULTS.enabled),\n intervalMs: durationOpt(\n o.intervalMs,\n o.intervalSeconds,\n numEnv(env.OPENCODE_KEEPALIVE_INTERVAL_MS, DEFAULTS.intervalMs),\n ),\n windowMs: durationOpt(\n o.windowMs,\n o.windowMinutes ? Number(o.windowMinutes) * 60 : undefined,\n numEnv(env.OPENCODE_KEEPALIVE_WINDOW_MS, DEFAULTS.windowMs),\n ),\n injectSystemInstruction: boolOpt(\n o.injectSystemInstruction,\n DEFAULTS.injectSystemInstruction,\n ),\n pingToken:\n typeof o.pingToken === \"string\" && o.pingToken.length > 0\n ? o.pingToken\n : DEFAULTS.pingToken,\n revertPing: boolOpt(\n o.revertPing,\n env.OPENCODE_KEEPALIVE_REVERT_PING !== \"false\" && DEFAULTS.revertPing,\n ),\n includeChildSessions: boolOpt(o.includeChildSessions, DEFAULTS.includeChildSessions),\n debug: boolOpt(o.debug, env.OPENCODE_KEEPALIVE_DEBUG === \"true\"),\n providerAllowlist: listOpt(o.providerAllowlist, DEFAULTS.providerAllowlist),\n modelAllowlist: listOpt(o.modelAllowlist, DEFAULTS.modelAllowlist),\n }\n}\n\nfunction boolOpt(value: unknown, fallback: boolean): boolean {\n return typeof value === \"boolean\" ? value : fallback\n}\n\nfunction listOpt(value: unknown, fallback: string[]): string[] {\n if (!Array.isArray(value)) return fallback\n const filtered = value.filter((item): item is string => typeof item === \"string\")\n return filtered.length > 0 ? filtered : fallback\n}\n\n/** Accept an explicit `*Ms` option, a `*Seconds` option, or fall back. */\nfunction durationOpt(ms: unknown, seconds: unknown, fallback: number): number {\n if (typeof ms === \"number\" && Number.isFinite(ms) && ms > 0) return ms\n if (typeof seconds === \"number\" && Number.isFinite(seconds) && seconds > 0)\n return seconds * 1000\n return fallback\n}\n\nfunction numEnv(raw: string | undefined, fallback: number): number {\n if (!raw) return fallback\n const value = Number(raw)\n return Number.isFinite(value) && value > 0 ? value : fallback\n}\n","import { readFileSync } from \"node:fs\"\nimport { mkdir, rename, writeFile } from \"node:fs/promises\"\nimport { dirname } from \"node:path\"\nimport { controlFilePath } from \"./paths\"\n\nexport type KeepaliveControl = {\n version: 1\n enabled: boolean\n updatedAt: number\n}\n\nexport function readControl(): KeepaliveControl | undefined {\n try {\n const value = JSON.parse(\n readFileSync(controlFilePath(), \"utf8\"),\n ) as Partial<KeepaliveControl>\n if (value.version !== 1 || typeof value.enabled !== \"boolean\") return undefined\n if (typeof value.updatedAt !== \"number\") return undefined\n return value as KeepaliveControl\n } catch {\n return undefined\n }\n}\n\nexport async function writeControl(enabled: boolean): Promise<KeepaliveControl> {\n const value: KeepaliveControl = {\n version: 1,\n enabled,\n updatedAt: Date.now(),\n }\n const path = controlFilePath()\n const tmp = `${path}.${process.pid}.${value.updatedAt}.tmp`\n await mkdir(dirname(path), { recursive: true })\n await writeFile(tmp, `${JSON.stringify(value)}\\n`, \"utf8\")\n await rename(tmp, path)\n return value\n}\n","import { createHash } from \"node:crypto\"\nimport { homedir } from \"node:os\"\nimport { join } from \"node:path\"\n\n/**\n * Absolute path to the JSON state file shared between the server plugin (writer)\n * and the TUI plugin (reader). Both processes run on the same machine, so a file\n * under opencode's plugin storage dir is the simplest cross-process channel.\n *\n * Mirrors the storage layout used by other opencode plugins:\n * <XDG_DATA_HOME | ~/.local/share>/opencode/storage/plugin/keepalive/state.json\n */\nexport function stateFilePath(directory: string): string {\n const key = createHash(\"sha256\").update(directory).digest(\"hex\").slice(0, 16)\n return join(stateDirectoryPath(), `state-${key}.json`)\n}\n\nexport function stateDirectoryPath(): string {\n return join(\n process.env.XDG_DATA_HOME ?? join(homedir(), \".local\", \"share\"),\n \"opencode\",\n \"storage\",\n \"plugin\",\n \"keepalive\",\n )\n}\n\n/** Runtime on/off control written by the TUI and read by the server plugin. */\nexport function controlFilePath(): string {\n return join(stateDirectoryPath(), \"control.json\")\n}\n","import type { KeepaliveConfig } from \"./config\"\n\n/**\n * Warming only helps on providers that do prefix caching we can refresh by\n * replaying the prefix. Supported defaults include Claude and GPT models served\n * through GitHub Copilot. Everything else is skipped.\n */\nexport function isEligibleModel(\n config: KeepaliveConfig,\n providerID: string | undefined,\n modelID: string | undefined,\n): boolean {\n if (!providerID || !modelID) return false\n const provider = providerID.toLowerCase()\n const model = modelID.toLowerCase()\n const providerOk = config.providerAllowlist.some((needle) => provider.includes(needle))\n const modelOk = config.modelAllowlist.some((needle) => model.includes(needle))\n return providerOk && modelOk\n}\n","import type { KeepaliveConfig } from \"./config\"\nimport { readControl } from \"./control\"\nimport { isEligibleModel } from \"./model\"\nimport type { KeepaliveStore, SessionKeepalive } from \"./state\"\nimport type { Logger } from \"./logger\"\n\nconst TICK_MS = 15_000\nconst JITTER_MS = 15_000\nconst CONTROL_POLL_MS = 1_000\nconst STATUS_POLL_MS = 2_000\n\n/**\n * The keepalive engine.\n *\n * Lifecycle per session:\n * 1. A real turn finishes -> `session.idle` -> arm a warm window and schedule pings.\n * 2. On each tick past `nextPingAt` (and within the window) -> `firePing`.\n * 3. `firePing` sends a `~` prompt through opencode (exact cached prefix, reuses\n * the session's model/auth), reads `usage` to confirm a cache hit, then optionally\n * reverts the `~`/`~` turn so it does not pollute the real conversation.\n * 4. A new real user turn or the window closing stops warming.\n */\nexport class KeepaliveEngine {\n private timer: ReturnType<typeof setInterval> | undefined\n private controlTimer: ReturnType<typeof setInterval> | undefined\n private statusTimer: ReturnType<typeof setInterval> | undefined\n private controlUpdatedAt = 0\n private reconcilingStatus = false\n private enabled: boolean\n\n constructor(\n private readonly client: any,\n private readonly config: KeepaliveConfig,\n private readonly store: KeepaliveStore,\n private readonly logger: Logger,\n ) {\n const control = readControl()\n this.enabled = control?.enabled ?? config.enabled\n this.controlUpdatedAt = control?.updatedAt ?? 0\n this.store.setEnabled(this.enabled)\n }\n\n start(): void {\n if (this.timer) return\n this.timer = setInterval(() => void this.tick(), TICK_MS)\n this.controlTimer = setInterval(() => this.pollControl(), CONTROL_POLL_MS)\n this.statusTimer = setInterval(() => void this.reconcileStatus(), STATUS_POLL_MS)\n this.store.persist()\n this.logger.info(\n `started (${this.enabled ? \"enabled\" : \"disabled\"}, ` +\n `interval=${Math.round(this.config.intervalMs / 1000)}s, ` +\n `window=${Math.round(this.config.windowMs / 60_000)}m)`,\n )\n }\n\n stop(): void {\n if (!this.timer) return\n clearInterval(this.timer)\n this.timer = undefined\n if (this.controlTimer) clearInterval(this.controlTimer)\n this.controlTimer = undefined\n if (this.statusTimer) clearInterval(this.statusTimer)\n this.statusTimer = undefined\n }\n\n /** True while a ping is in flight — used to block tool calls during a ping. */\n isWarming(sessionID: string): boolean {\n return !!this.store.get(sessionID)?.warming\n }\n\n async onEvent(event: any): Promise<void> {\n const type: string | undefined = event?.type\n if (!type) return\n\n if (type === \"session.deleted\") {\n const id = event.properties?.sessionID ?? event.properties?.info?.id\n if (id) {\n this.store.remove(id)\n this.store.persist()\n }\n return\n }\n\n if (type === \"message.updated\") {\n const info = event.properties?.info\n const sessionID: string | undefined = info?.sessionID ?? event.properties?.sessionID\n if (!sessionID) return\n const session = this.store.get(sessionID)\n // Ignore everything that happens inside our own ping turn.\n if (session?.warming) return\n const eventAt = info?.time?.completed ?? info?.time?.created\n // Events are asynchronous: a completed ping may still emit message updates\n // after `warming` clears. Their timestamps predate the recorded ping.\n if (session?.lastPingAt && typeof eventAt === \"number\" && eventAt <= session.lastPingAt)\n return\n\n if (info?.role === \"user\") {\n const s = this.store.ensure(sessionID)\n s.busy = true\n s.active = false\n this.store.persist()\n return\n }\n\n if (info?.role === \"assistant\" && typeof info?.time?.completed === \"number\") {\n const s = this.store.ensure(sessionID)\n s.lastResponseAt = info.time.completed\n this.store.persist()\n }\n return\n }\n\n if (type === \"session.status\") {\n const sessionID: string | undefined = event.properties?.sessionID\n const status: string | undefined = event.properties?.status?.type\n if (!sessionID || !status) return\n\n // The ping's own status transitions must not restart its idle window.\n if (this.store.get(sessionID)?.warming) return\n\n if (status === \"idle\") {\n await this.armWindow(sessionID)\n return\n }\n\n const s = this.store.ensure(sessionID)\n s.busy = true\n s.active = false\n this.store.persist()\n return\n }\n\n // Compatibility fallback for opencode versions predating session.status.\n if (type === \"session.idle\") {\n const sessionID: string | undefined = event.properties?.sessionID\n if (!sessionID) return\n if (this.store.get(sessionID)?.warming) return // idle from our own ping\n await this.armWindow(sessionID)\n }\n }\n\n private async armWindow(sessionID: string): Promise<void> {\n const s = this.store.ensure(sessionID)\n // Duplicate idle events (including late events from a ping) must not reset the\n // current idle stretch or extend its warm window.\n if (s.active && !s.busy) return\n s.busy = false\n await this.resolveSession(s)\n\n // A new turn may have started while session metadata was being resolved.\n if (s.busy || !this.enabled || !s.eligible) {\n s.active = false\n this.store.persist()\n return\n }\n\n const now = Date.now()\n if (!s.lastResponseAt) s.lastResponseAt = now\n s.idleSince = now\n s.windowEndsAt = now + this.config.windowMs\n s.nextPingAt = now + this.config.intervalMs\n s.pingsSent = 0\n s.lastPing = undefined\n s.active = true\n this.store.persist()\n this.logger.dbg(`armed ${short(sessionID)} model=${s.modelLabel}`)\n }\n\n private async tick(): Promise<void> {\n if (!this.enabled) return\n const now = Date.now()\n for (const s of this.store.all()) {\n if (!s.eligible || !s.active || s.busy || s.warming) continue\n\n if (now >= s.windowEndsAt) {\n s.active = false\n this.store.persist()\n this.logger.dbg(\n `window closed ${short(s.sessionID)} after ${s.pingsSent} ping(s) — cache may cool`,\n )\n continue\n }\n\n if (now >= s.nextPingAt) void this.firePing(s.sessionID)\n }\n }\n\n private async firePing(sessionID: string): Promise<void> {\n const s = this.store.get(sessionID)\n if (!s || s.warming || s.busy) return\n\n s.warming = true\n this.store.persist()\n const startAt = Date.now()\n\n try {\n const res = await this.client.session.prompt({\n path: { id: sessionID },\n body: { parts: [{ type: \"text\", text: this.config.pingToken }] },\n })\n\n const info = res?.data?.info ?? res?.info\n const cache = info?.tokens?.cache ?? {}\n const record = {\n at: Date.now(),\n hit: Number(cache.read ?? 0) > 0,\n input: Number(info?.tokens?.input ?? 0),\n cacheRead: Number(cache.read ?? 0),\n cacheWrite: Number(cache.write ?? 0),\n output: Number(info?.tokens?.output ?? 0),\n }\n s.lastPing = record\n s.lastPingAt = record.at\n s.pingsSent += 1\n this.logger.info(\n `ping ${short(sessionID)} ${record.hit ? \"HIT\" : \"MISS\"} ` +\n `in=${record.input} read=${record.cacheRead} write=${record.cacheWrite} out=${record.output}`,\n )\n\n if (this.config.revertPing) await this.revertPing(sessionID, startAt)\n } catch (err) {\n this.logger.warn(`ping ${short(sessionID)} failed`, errText(err))\n } finally {\n s.warming = false\n s.nextPingAt = Date.now() + jitter(this.config.intervalMs)\n this.store.persist()\n }\n }\n\n /** Remove the `~` user + `~` assistant turn so it never enters real context. */\n private async revertPing(sessionID: string, startAt: number): Promise<void> {\n let messages: any[]\n try {\n const res = await this.client.session.messages({ path: { id: sessionID } })\n messages = res?.data ?? res ?? []\n } catch (err) {\n this.logger.warn(`ping ${short(sessionID)} revert: list failed`, errText(err))\n return\n }\n\n let target: string | undefined\n for (const m of messages) {\n if (m?.info?.role !== \"user\") continue\n if (Number(m.info?.time?.created ?? 0) < startAt - 2_000) continue\n const text = (m.parts ?? [])\n .filter((p: any) => p?.type === \"text\")\n .map((p: any) => p?.text ?? \"\")\n .join(\"\")\n .trim()\n if (text === this.config.pingToken) target = m.info.id\n }\n\n if (!target) {\n this.logger.dbg(`ping ${short(sessionID)} revert: ping message not found`)\n return\n }\n\n try {\n await this.client.session.revert({\n path: { id: sessionID },\n body: { messageID: target },\n })\n } catch (err) {\n // BusyError (a real turn started) or similar — leave the ping visible.\n this.logger.warn(`ping ${short(sessionID)} revert failed`, errText(err))\n }\n }\n\n private async resolveSession(s: SessionKeepalive): Promise<void> {\n try {\n const res = await this.client.session.get({ path: { id: s.sessionID } })\n const info = res?.data ?? res\n if (info?.parentID && !this.config.includeChildSessions) {\n s.eligible = false\n s.modelLabel = info?.model?.id ?? \"child\"\n return\n }\n s.modelLabel = info?.model?.id ?? \"unknown\"\n s.eligible = isEligibleModel(this.config, info?.model?.providerID, info?.model?.id)\n } catch (err) {\n s.eligible = false\n this.logger.dbg(`resolve ${short(s.sessionID)} failed`, errText(err))\n }\n }\n\n private pollControl(): void {\n const control = readControl()\n if (!control || control.updatedAt <= this.controlUpdatedAt) return\n this.controlUpdatedAt = control.updatedAt\n if (control.enabled === this.enabled) return\n\n this.enabled = control.enabled\n this.store.setEnabled(control.enabled)\n const now = Date.now()\n for (const session of this.store.all()) {\n if (!control.enabled) {\n session.active = false\n continue\n }\n if (!session.eligible || session.busy || session.warming) continue\n if (!session.lastResponseAt) session.lastResponseAt = now\n session.idleSince = now\n session.windowEndsAt = now + this.config.windowMs\n session.nextPingAt = now + this.config.intervalMs\n session.pingsSent = 0\n session.lastPing = undefined\n session.active = true\n }\n this.store.persist()\n this.logger.info(`runtime ${control.enabled ? \"enabled\" : \"disabled\"}`)\n }\n\n /** Recover when a session.status idle event is missed by reconciling with the API. */\n private async reconcileStatus(): Promise<void> {\n if (this.reconcilingStatus) return\n const busy = this.store.all().filter((session) => session.busy && !session.warming)\n if (busy.length === 0) return\n\n this.reconcilingStatus = true\n try {\n const res = await this.client.session.status()\n const statuses = res?.data ?? res ?? {}\n for (const session of busy) {\n const status = statuses?.[session.sessionID]?.type ?? \"idle\"\n if (status === \"idle\") await this.armWindow(session.sessionID)\n }\n } catch (err) {\n this.logger.dbg(\"status reconciliation failed\", errText(err))\n } finally {\n this.reconcilingStatus = false\n }\n }\n}\n\nfunction short(sessionID: string): string {\n return sessionID.slice(-6)\n}\n\nfunction jitter(base: number): number {\n return Math.max(1_000, base + Math.floor((Math.random() * 2 - 1) * JITTER_MS))\n}\n\nfunction errText(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n","const PREFIX = \"[cache-keepalive]\"\n\nexport class Logger {\n constructor(private readonly debug: boolean) {}\n\n info(message: string, data?: unknown) {\n if (this.debug) console.log(`${PREFIX} ${message}`, data ?? \"\")\n }\n\n warn(message: string, data?: unknown) {\n console.warn(`${PREFIX} ${message}`, data ?? \"\")\n }\n\n error(message: string, data?: unknown) {\n console.error(`${PREFIX} ${message}`, data ?? \"\")\n }\n\n dbg(message: string, data?: unknown) {\n if (this.debug) console.log(`${PREFIX}:debug ${message}`, data ?? \"\")\n }\n}\n","import { mkdir, rename, writeFile } from \"node:fs/promises\"\nimport { dirname } from \"node:path\"\nimport type { KeepaliveConfig } from \"./config\"\nimport { stateFilePath } from \"./paths\"\n\nexport type PingRecord = {\n at: number\n hit: boolean\n input: number\n cacheRead: number\n cacheWrite: number\n output: number\n}\n\n/** Live per-session bookkeeping held by the server plugin. */\nexport type SessionKeepalive = {\n sessionID: string\n /** Whether the session's provider/model pair supports cache warming. */\n eligible: boolean\n modelLabel?: string\n /** Epoch ms of the last real assistant response (excludes ping replies). */\n lastResponseAt: number\n /** Epoch ms the current idle stretch began. */\n idleSince: number\n /** Epoch ms after which warming stops and the cache is allowed to go cold. */\n windowEndsAt: number\n /** Epoch ms the next ping is due. */\n nextPingAt: number\n /** Pings sent during the current idle stretch. */\n pingsSent: number\n /** A real turn is currently running. */\n busy: boolean\n /** A keepalive ping is currently in flight. */\n warming: boolean\n /** Within the warm window and actively scheduling pings. */\n active: boolean\n /** Epoch ms of the last ping; retained only to ignore its late events. */\n lastPingAt?: number\n lastPing?: PingRecord\n}\n\n/** Lean projection persisted to disk for the TUI reader. */\nexport type PersistedSession = {\n eligible: boolean\n modelLabel?: string\n lastResponseAt: number\n idleSince: number\n windowEndsAt: number\n nextPingAt: number\n intervalMs: number\n pingsSent: number\n busy: boolean\n warming: boolean\n active: boolean\n lastPing?: PingRecord\n}\n\nexport type PersistedState = {\n version: 1\n updatedAt: number\n enabled: boolean\n intervalMs: number\n windowMs: number\n sessions: Record<string, PersistedSession>\n}\n\nexport class KeepaliveStore {\n private readonly sessions = new Map<string, SessionKeepalive>()\n private writeQueue: Promise<void> = Promise.resolve()\n private enabled: boolean\n\n constructor(\n private readonly config: KeepaliveConfig,\n private readonly directory: string,\n ) {\n this.enabled = config.enabled\n }\n\n setEnabled(enabled: boolean): void {\n this.enabled = enabled\n }\n\n get(sessionID: string): SessionKeepalive | undefined {\n return this.sessions.get(sessionID)\n }\n\n ensure(sessionID: string): SessionKeepalive {\n const existing = this.sessions.get(sessionID)\n if (existing) return existing\n const created: SessionKeepalive = {\n sessionID,\n eligible: false,\n lastResponseAt: 0,\n idleSince: 0,\n windowEndsAt: 0,\n nextPingAt: 0,\n pingsSent: 0,\n busy: false,\n warming: false,\n active: false,\n }\n this.sessions.set(sessionID, created)\n return created\n }\n\n remove(sessionID: string): void {\n this.sessions.delete(sessionID)\n }\n\n all(): SessionKeepalive[] {\n return [...this.sessions.values()]\n }\n\n /** Atomically persist a lean snapshot for the TUI. Never throws to callers. */\n persist(): void {\n const snapshot: PersistedState = {\n version: 1,\n updatedAt: Date.now(),\n enabled: this.enabled,\n intervalMs: this.config.intervalMs,\n windowMs: this.config.windowMs,\n sessions: Object.fromEntries(\n this.all().map((s) => [\n s.sessionID,\n {\n eligible: s.eligible,\n modelLabel: s.modelLabel,\n lastResponseAt: s.lastResponseAt,\n idleSince: s.idleSince,\n windowEndsAt: s.windowEndsAt,\n nextPingAt: s.nextPingAt,\n intervalMs: this.config.intervalMs,\n pingsSent: s.pingsSent,\n busy: s.busy,\n warming: s.warming,\n active: s.active,\n lastPing: s.lastPing,\n } satisfies PersistedSession,\n ]),\n ),\n }\n\n const write = () => atomicWrite(stateFilePath(this.directory), JSON.stringify(snapshot))\n this.writeQueue = this.writeQueue.then(write, write)\n }\n}\n\nasync function atomicWrite(path: string, contents: string): Promise<void> {\n try {\n const tmp = `${path}.${process.pid}.${Date.now()}.tmp`\n await mkdir(dirname(path), { recursive: true })\n await writeFile(tmp, `${contents}\\n`, \"utf8\")\n await rename(tmp, path)\n } catch {\n // Persistence is best-effort; the TUI readout simply falls behind.\n }\n}\n","/**\n * Stable keepalive instruction appended to the system prompt.\n *\n * IMPORTANT: this text must be byte-stable and applied to *every* request for an\n * eligible model (real turns and pings alike). If it were only added on pings, the\n * system prefix would differ from real turns and the ping would miss the cache.\n * Adding it consistently keeps the cached prefix identical and lets the model\n * answer a ping with a single token instead of a paragraph or a tool call.\n */\nexport function keepaliveInstruction(pingToken: string): string {\n return [\n \"<cache_keepalive>\",\n `A user message consisting of exactly the single token \"${pingToken}\" is an`,\n \"automated cache-keepalive ping, not a real request. When you receive it,\",\n `reply with exactly \"${pingToken}\" and nothing else. Do not call any tools,`,\n \"do not explain, and do not take any action.\",\n \"</cache_keepalive>\",\n ].join(\"\\n\")\n}\n","import type { Plugin } from \"@opencode-ai/plugin\"\nimport { configureClientAuth, isSecureMode } from \"./lib/auth\"\nimport { getConfig } from \"./lib/config\"\nimport { KeepaliveEngine } from \"./lib/keepalive\"\nimport { Logger } from \"./lib/logger\"\nimport { isEligibleModel } from \"./lib/model\"\nimport { KeepaliveStore } from \"./lib/state\"\nimport { keepaliveInstruction } from \"./lib/system\"\n\ndeclare const __KEEPALIVE_VERSION__: string\n\nconst server: Plugin = async (ctx, options) => {\n const config = getConfig(options as Record<string, unknown> | undefined)\n const logger = new Logger(config.debug)\n\n if (!config.enabled) {\n logger.info(\"disabled via config\")\n return {}\n }\n\n if (isSecureMode()) configureClientAuth(ctx.client)\n\n const store = new KeepaliveStore(config, ctx.directory)\n const engine = new KeepaliveEngine(ctx.client, config, store, logger)\n engine.start()\n\n const version = typeof __KEEPALIVE_VERSION__ !== \"undefined\" ? __KEEPALIVE_VERSION__ : \"dev\"\n logger.info(`v${version} ready`)\n\n const instruction = keepaliveInstruction(config.pingToken)\n\n return {\n event: async ({ event }) => {\n await engine.onEvent(event)\n },\n\n // Keep the system prefix identical between real turns and pings, and make\n // the model answer a ping with a single token.\n \"experimental.chat.system.transform\": async (input, output) => {\n if (!config.injectSystemInstruction) return\n const model = input.model as { providerID?: string; id?: string; modelID?: string }\n if (!isEligibleModel(config, model?.providerID, model?.id ?? model?.modelID)) return\n output.system.push(instruction)\n },\n\n // Backstop: if the model ignores the instruction and tries a tool during a\n // ping, block it — the ping turn is going to be reverted anyway.\n \"tool.execute.before\": async (input) => {\n if (engine.isWarming(input.sessionID)) {\n throw new Error(\"cache-keepalive: tools are disabled during a keepalive ping\")\n }\n },\n\n dispose: async () => {\n engine.stop()\n },\n }\n}\n\nexport default server\n"],"mappings":";AAOO,SAAS,eAAwB;AACpC,SAAO,CAAC,CAAC,QAAQ,IAAI;AACzB;AAEO,SAAS,yBAA6C;AACzD,QAAM,WAAW,QAAQ,IAAI;AAC7B,MAAI,CAAC,SAAU,QAAO;AACtB,QAAM,WAAW,QAAQ,IAAI,4BAA4B;AACzD,QAAM,cAAc,OAAO,KAAK,GAAG,QAAQ,IAAI,QAAQ,EAAE,EAAE,SAAS,QAAQ;AAC5E,SAAO,SAAS,WAAW;AAC/B;AAEO,SAAS,oBAAoB,QAAmB;AACnD,QAAM,SAAS,uBAAuB;AACtC,MAAI,CAAC,OAAQ;AAEb,QAAM,QAAQ,QAAQ,WAAW,QAAQ;AACzC,MAAI,CAAC,OAAO,cAAc,QAAS;AAEnC,QAAM,aAAa,QAAQ,IAAI,CAAC,YAAqB;AACjD,QAAI,CAAC,QAAQ,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,YAAY,OAAO,MAAM,OAAO,OAAO,EAAG;AAC9E,aAAO,OAAO,KAAK,WAAW;AAAA,IAClC;AAAA;AAAA;AAAA,IAIA,uBAAuB,OAAO,UAAU;AACpC,UAAI,OAAO,UAAU,MAAM,SAAS,GAAG;AACnC,cAAM,IAAI,MAAM,6DAA6D;AAAA,MACjF;AAAA,IACJ;AAAA,IAEA,SAAS,YAAY;AACjB,aAAO,KAAK;AAAA,IAChB;AAAA,EACJ;AACJ;AAEA,IAAO,gBAAQ;","names":["mkdir","rename","writeFile","dirname","mkdir","dirname","writeFile","rename"]}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * When opencode runs with `OPENCODE_SERVER_PASSWORD`, the local HTTP API requires
3
+ * Basic auth. The SDK client handed to plugins does not carry it automatically,
4
+ * so we attach an interceptor. No-op when the server is not password protected.
5
+ *
6
+ * Same approach as opencode-dynamic-context-pruning's auth helper.
7
+ */
8
+ export declare function isSecureMode(): boolean;
9
+ export declare function getAuthorizationHeader(): string | undefined;
10
+ export declare function configureClientAuth(client: any): void;
11
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../lib/auth.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,YAAY,IAAI,OAAO,CAEtC;AAED,wBAAgB,sBAAsB,IAAI,MAAM,GAAG,SAAS,CAM3D;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,GAAG,GAAG,IAAI,CAarD"}
@@ -0,0 +1,31 @@
1
+ export type KeepaliveConfig = {
2
+ /** Master switch. When false the plugin registers no timers or hooks. */
3
+ enabled: boolean;
4
+ /** Milliseconds between pings. Must stay under the shortest supported provider TTL. */
5
+ intervalMs: number;
6
+ /**
7
+ * How long to keep warming after the last real response before giving up and
8
+ * letting the cache go cold. The default follows the Copilot Claude
9
+ * write/read break-even; providers with automatic caching may be cheaper.
10
+ */
11
+ windowMs: number;
12
+ /**
13
+ * Append a stable keepalive instruction to the system prompt so the model
14
+ * answers a `~` ping with a single `~` token and never calls tools. The
15
+ * instruction is added to real turns too, so the cached prefix stays identical.
16
+ */
17
+ injectSystemInstruction: boolean;
18
+ /** The single character/token used as the keepalive suffix. */
19
+ pingToken: string;
20
+ /** Remove synthetic ping turns after measuring their cache usage. */
21
+ revertPing: boolean;
22
+ /** Warm subagent/child sessions too. Off by default to avoid wasted pings. */
23
+ includeChildSessions: boolean;
24
+ debug: boolean;
25
+ /** providerID substrings eligible for warming (cache-controlled providers). */
26
+ providerAllowlist: string[];
27
+ /** modelID substrings eligible for warming. */
28
+ modelAllowlist: string[];
29
+ };
30
+ export declare function getConfig(options: Record<string, unknown> | undefined): KeepaliveConfig;
31
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../lib/config.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,GAAG;IAC1B,yEAAyE;IACzE,OAAO,EAAE,OAAO,CAAA;IAChB,uFAAuF;IACvF,UAAU,EAAE,MAAM,CAAA;IAClB;;;;OAIG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;;;OAIG;IACH,uBAAuB,EAAE,OAAO,CAAA;IAChC,+DAA+D;IAC/D,SAAS,EAAE,MAAM,CAAA;IACjB,qEAAqE;IACrE,UAAU,EAAE,OAAO,CAAA;IACnB,8EAA8E;IAC9E,oBAAoB,EAAE,OAAO,CAAA;IAC7B,KAAK,EAAE,OAAO,CAAA;IACd,+EAA+E;IAC/E,iBAAiB,EAAE,MAAM,EAAE,CAAA;IAC3B,+CAA+C;IAC/C,cAAc,EAAE,MAAM,EAAE,CAAA;CAC3B,CAAA;AAeD,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,eAAe,CAiCvF"}
@@ -0,0 +1,8 @@
1
+ export type KeepaliveControl = {
2
+ version: 1;
3
+ enabled: boolean;
4
+ updatedAt: number;
5
+ };
6
+ export declare function readControl(): KeepaliveControl | undefined;
7
+ export declare function writeControl(enabled: boolean): Promise<KeepaliveControl>;
8
+ //# sourceMappingURL=control.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"control.d.ts","sourceRoot":"","sources":["../../lib/control.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,gBAAgB,GAAG;IAC3B,OAAO,EAAE,CAAC,CAAA;IACV,OAAO,EAAE,OAAO,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,wBAAgB,WAAW,IAAI,gBAAgB,GAAG,SAAS,CAW1D;AAED,wBAAsB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAY9E"}
@@ -0,0 +1,42 @@
1
+ import type { KeepaliveConfig } from "./config";
2
+ import type { KeepaliveStore } from "./state";
3
+ import type { Logger } from "./logger";
4
+ /**
5
+ * The keepalive engine.
6
+ *
7
+ * Lifecycle per session:
8
+ * 1. A real turn finishes -> `session.idle` -> arm a warm window and schedule pings.
9
+ * 2. On each tick past `nextPingAt` (and within the window) -> `firePing`.
10
+ * 3. `firePing` sends a `~` prompt through opencode (exact cached prefix, reuses
11
+ * the session's model/auth), reads `usage` to confirm a cache hit, then optionally
12
+ * reverts the `~`/`~` turn so it does not pollute the real conversation.
13
+ * 4. A new real user turn or the window closing stops warming.
14
+ */
15
+ export declare class KeepaliveEngine {
16
+ private readonly client;
17
+ private readonly config;
18
+ private readonly store;
19
+ private readonly logger;
20
+ private timer;
21
+ private controlTimer;
22
+ private statusTimer;
23
+ private controlUpdatedAt;
24
+ private reconcilingStatus;
25
+ private enabled;
26
+ constructor(client: any, config: KeepaliveConfig, store: KeepaliveStore, logger: Logger);
27
+ start(): void;
28
+ stop(): void;
29
+ /** True while a ping is in flight — used to block tool calls during a ping. */
30
+ isWarming(sessionID: string): boolean;
31
+ onEvent(event: any): Promise<void>;
32
+ private armWindow;
33
+ private tick;
34
+ private firePing;
35
+ /** Remove the `~` user + `~` assistant turn so it never enters real context. */
36
+ private revertPing;
37
+ private resolveSession;
38
+ private pollControl;
39
+ /** Recover when a session.status idle event is missed by reconciling with the API. */
40
+ private reconcileStatus;
41
+ }
42
+ //# sourceMappingURL=keepalive.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keepalive.d.ts","sourceRoot":"","sources":["../../lib/keepalive.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAG/C,OAAO,KAAK,EAAE,cAAc,EAAoB,MAAM,SAAS,CAAA;AAC/D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAOtC;;;;;;;;;;GAUG;AACH,qBAAa,eAAe;IASpB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAX3B,OAAO,CAAC,KAAK,CAA4C;IACzD,OAAO,CAAC,YAAY,CAA4C;IAChE,OAAO,CAAC,WAAW,CAA4C;IAC/D,OAAO,CAAC,gBAAgB,CAAI;IAC5B,OAAO,CAAC,iBAAiB,CAAQ;IACjC,OAAO,CAAC,OAAO,CAAS;gBAGH,MAAM,EAAE,GAAG,EACX,MAAM,EAAE,eAAe,EACvB,KAAK,EAAE,cAAc,EACrB,MAAM,EAAE,MAAM;IAQnC,KAAK,IAAI,IAAI;IAab,IAAI,IAAI,IAAI;IAUZ,+EAA+E;IAC/E,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAI/B,OAAO,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;YAuE1B,SAAS;YA2BT,IAAI;YAmBJ,QAAQ;IA0CtB,gFAAgF;YAClE,UAAU;YAsCV,cAAc;IAiB5B,OAAO,CAAC,WAAW;IA2BnB,sFAAsF;YACxE,eAAe;CAmBhC"}
@@ -0,0 +1,9 @@
1
+ export declare class Logger {
2
+ private readonly debug;
3
+ constructor(debug: boolean);
4
+ info(message: string, data?: unknown): void;
5
+ warn(message: string, data?: unknown): void;
6
+ error(message: string, data?: unknown): void;
7
+ dbg(message: string, data?: unknown): void;
8
+ }
9
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../lib/logger.ts"],"names":[],"mappings":"AAEA,qBAAa,MAAM;IACH,OAAO,CAAC,QAAQ,CAAC,KAAK;gBAAL,KAAK,EAAE,OAAO;IAE3C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAIpC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAIpC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAIrC,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CAGtC"}
@@ -0,0 +1,8 @@
1
+ import type { KeepaliveConfig } from "./config";
2
+ /**
3
+ * Warming only helps on providers that do prefix caching we can refresh by
4
+ * replaying the prefix. Supported defaults include Claude and GPT models served
5
+ * through GitHub Copilot. Everything else is skipped.
6
+ */
7
+ export declare function isEligibleModel(config: KeepaliveConfig, providerID: string | undefined, modelID: string | undefined): boolean;
8
+ //# sourceMappingURL=model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../lib/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAE/C;;;;GAIG;AACH,wBAAgB,eAAe,CAC3B,MAAM,EAAE,eAAe,EACvB,UAAU,EAAE,MAAM,GAAG,SAAS,EAC9B,OAAO,EAAE,MAAM,GAAG,SAAS,GAC5B,OAAO,CAOT"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Absolute path to the JSON state file shared between the server plugin (writer)
3
+ * and the TUI plugin (reader). Both processes run on the same machine, so a file
4
+ * under opencode's plugin storage dir is the simplest cross-process channel.
5
+ *
6
+ * Mirrors the storage layout used by other opencode plugins:
7
+ * <XDG_DATA_HOME | ~/.local/share>/opencode/storage/plugin/keepalive/state.json
8
+ */
9
+ export declare function stateFilePath(directory: string): string;
10
+ export declare function stateDirectoryPath(): string;
11
+ /** Runtime on/off control written by the TUI and read by the server plugin. */
12
+ export declare function controlFilePath(): string;
13
+ //# sourceMappingURL=paths.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../lib/paths.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAGvD;AAED,wBAAgB,kBAAkB,IAAI,MAAM,CAQ3C;AAED,+EAA+E;AAC/E,wBAAgB,eAAe,IAAI,MAAM,CAExC"}
@@ -0,0 +1,74 @@
1
+ import type { KeepaliveConfig } from "./config";
2
+ export type PingRecord = {
3
+ at: number;
4
+ hit: boolean;
5
+ input: number;
6
+ cacheRead: number;
7
+ cacheWrite: number;
8
+ output: number;
9
+ };
10
+ /** Live per-session bookkeeping held by the server plugin. */
11
+ export type SessionKeepalive = {
12
+ sessionID: string;
13
+ /** Whether the session's provider/model pair supports cache warming. */
14
+ eligible: boolean;
15
+ modelLabel?: string;
16
+ /** Epoch ms of the last real assistant response (excludes ping replies). */
17
+ lastResponseAt: number;
18
+ /** Epoch ms the current idle stretch began. */
19
+ idleSince: number;
20
+ /** Epoch ms after which warming stops and the cache is allowed to go cold. */
21
+ windowEndsAt: number;
22
+ /** Epoch ms the next ping is due. */
23
+ nextPingAt: number;
24
+ /** Pings sent during the current idle stretch. */
25
+ pingsSent: number;
26
+ /** A real turn is currently running. */
27
+ busy: boolean;
28
+ /** A keepalive ping is currently in flight. */
29
+ warming: boolean;
30
+ /** Within the warm window and actively scheduling pings. */
31
+ active: boolean;
32
+ /** Epoch ms of the last ping; retained only to ignore its late events. */
33
+ lastPingAt?: number;
34
+ lastPing?: PingRecord;
35
+ };
36
+ /** Lean projection persisted to disk for the TUI reader. */
37
+ export type PersistedSession = {
38
+ eligible: boolean;
39
+ modelLabel?: string;
40
+ lastResponseAt: number;
41
+ idleSince: number;
42
+ windowEndsAt: number;
43
+ nextPingAt: number;
44
+ intervalMs: number;
45
+ pingsSent: number;
46
+ busy: boolean;
47
+ warming: boolean;
48
+ active: boolean;
49
+ lastPing?: PingRecord;
50
+ };
51
+ export type PersistedState = {
52
+ version: 1;
53
+ updatedAt: number;
54
+ enabled: boolean;
55
+ intervalMs: number;
56
+ windowMs: number;
57
+ sessions: Record<string, PersistedSession>;
58
+ };
59
+ export declare class KeepaliveStore {
60
+ private readonly config;
61
+ private readonly directory;
62
+ private readonly sessions;
63
+ private writeQueue;
64
+ private enabled;
65
+ constructor(config: KeepaliveConfig, directory: string);
66
+ setEnabled(enabled: boolean): void;
67
+ get(sessionID: string): SessionKeepalive | undefined;
68
+ ensure(sessionID: string): SessionKeepalive;
69
+ remove(sessionID: string): void;
70
+ all(): SessionKeepalive[];
71
+ /** Atomically persist a lean snapshot for the TUI. Never throws to callers. */
72
+ persist(): void;
73
+ }
74
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../lib/state.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAG/C,MAAM,MAAM,UAAU,GAAG;IACrB,EAAE,EAAE,MAAM,CAAA;IACV,GAAG,EAAE,OAAO,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,MAAM,CAAA;CACjB,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,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;CAC7C,CAAA;AAED,qBAAa,cAAc;IAMnB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAN9B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAsC;IAC/D,OAAO,CAAC,UAAU,CAAmC;IACrD,OAAO,CAAC,OAAO,CAAS;gBAGH,MAAM,EAAE,eAAe,EACvB,SAAS,EAAE,MAAM;IAKtC,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAIlC,GAAG,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAIpD,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB;IAmB3C,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAI/B,GAAG,IAAI,gBAAgB,EAAE;IAIzB,+EAA+E;IAC/E,OAAO,IAAI,IAAI;CA+BlB"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Stable keepalive instruction appended to the system prompt.
3
+ *
4
+ * IMPORTANT: this text must be byte-stable and applied to *every* request for an
5
+ * eligible model (real turns and pings alike). If it were only added on pings, the
6
+ * system prefix would differ from real turns and the ping would miss the cache.
7
+ * Adding it consistently keeps the cached prefix identical and lets the model
8
+ * answer a ping with a single token instead of a paragraph or a tool call.
9
+ */
10
+ export declare function keepaliveInstruction(pingToken: string): string;
11
+ //# sourceMappingURL=system.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"system.d.ts","sourceRoot":"","sources":["../../lib/system.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAS9D"}
@@ -0,0 +1,3 @@
1
+ import type { TuiPluginApi } from "@opencode-ai/plugin/tui";
2
+ export declare function registerKeepaliveCommands(api: TuiPluginApi): void;
3
+ //# sourceMappingURL=commands.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../../../lib/tui/commands.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAG3D,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,YAAY,GAAG,IAAI,CA8CjE"}
@@ -0,0 +1,14 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+ type Theme = Record<string, any>;
3
+ /**
4
+ * Persistent readout mounted in the `sidebar_footer` slot. Shows time since the
5
+ * last real agent response plus keepalive pings sent / left. Data comes from the
6
+ * server plugin's state file; the idle timer ticks locally every second.
7
+ */
8
+ export declare function KeepaliveFooter(props: {
9
+ theme: Theme;
10
+ directory: string;
11
+ sessionID?: string;
12
+ }): any;
13
+ export {};
14
+ //# sourceMappingURL=footer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"footer.d.ts","sourceRoot":"","sources":["../../../lib/tui/footer.tsx"],"names":[],"mappings":"AAAA,sCAAsC;AAUtC,KAAK,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAUhC;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,OAwG7F"}
@@ -0,0 +1,3 @@
1
+ export declare function mmss(ms: number): string;
2
+ export declare function kfmt(n: number): string;
3
+ //# sourceMappingURL=format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../lib/tui/format.ts"],"names":[],"mappings":"AAAA,wBAAgB,IAAI,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAKvC;AAED,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAKtC"}
package/dist/tui.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /** @jsxImportSource @opentui/solid */
2
+ declare const _default: {
3
+ id: string;
4
+ tui: import("@opencode-ai/plugin/tui").TuiPlugin;
5
+ };
6
+ export default _default;
7
+ //# sourceMappingURL=tui.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tui.d.ts","sourceRoot":"","sources":["../tui.tsx"],"names":[],"mappings":"AAAA,sCAAsC;;;;;AAwBtC,wBAAgF"}
package/lib/auth.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * When opencode runs with `OPENCODE_SERVER_PASSWORD`, the local HTTP API requires
3
+ * Basic auth. The SDK client handed to plugins does not carry it automatically,
4
+ * so we attach an interceptor. No-op when the server is not password protected.
5
+ *
6
+ * Same approach as opencode-dynamic-context-pruning's auth helper.
7
+ */
8
+ export function isSecureMode(): boolean {
9
+ return !!process.env.OPENCODE_SERVER_PASSWORD
10
+ }
11
+
12
+ export function getAuthorizationHeader(): string | undefined {
13
+ const password = process.env.OPENCODE_SERVER_PASSWORD
14
+ if (!password) return undefined
15
+ const username = process.env.OPENCODE_SERVER_USERNAME ?? "opencode"
16
+ const credentials = Buffer.from(`${username}:${password}`).toString("base64")
17
+ return `Basic ${credentials}`
18
+ }
19
+
20
+ export function configureClientAuth(client: any): void {
21
+ const header = getAuthorizationHeader()
22
+ if (!header) return
23
+
24
+ const inner = client?._client ?? client?.client
25
+ if (!inner?.interceptors?.request) return
26
+
27
+ inner.interceptors.request.use((request: Request) => {
28
+ if (!request.headers.has("Authorization")) {
29
+ request.headers.set("Authorization", header)
30
+ }
31
+ return request
32
+ })
33
+ }
package/lib/config.ts ADDED
@@ -0,0 +1,101 @@
1
+ export type KeepaliveConfig = {
2
+ /** Master switch. When false the plugin registers no timers or hooks. */
3
+ enabled: boolean
4
+ /** Milliseconds between pings. Must stay under the shortest supported provider TTL. */
5
+ intervalMs: number
6
+ /**
7
+ * How long to keep warming after the last real response before giving up and
8
+ * letting the cache go cold. The default follows the Copilot Claude
9
+ * write/read break-even; providers with automatic caching may be cheaper.
10
+ */
11
+ windowMs: number
12
+ /**
13
+ * Append a stable keepalive instruction to the system prompt so the model
14
+ * answers a `~` ping with a single `~` token and never calls tools. The
15
+ * instruction is added to real turns too, so the cached prefix stays identical.
16
+ */
17
+ injectSystemInstruction: boolean
18
+ /** The single character/token used as the keepalive suffix. */
19
+ pingToken: string
20
+ /** Remove synthetic ping turns after measuring their cache usage. */
21
+ revertPing: boolean
22
+ /** Warm subagent/child sessions too. Off by default to avoid wasted pings. */
23
+ includeChildSessions: boolean
24
+ debug: boolean
25
+ /** providerID substrings eligible for warming (cache-controlled providers). */
26
+ providerAllowlist: string[]
27
+ /** modelID substrings eligible for warming. */
28
+ modelAllowlist: string[]
29
+ }
30
+
31
+ const DEFAULTS: KeepaliveConfig = {
32
+ enabled: true,
33
+ intervalMs: 270_000, // 4.5 min — below the shortest supported cache TTL
34
+ windowMs: 3_300_000, // 55 min — the Copilot Claude write/read break-even
35
+ injectSystemInstruction: true,
36
+ pingToken: "~",
37
+ revertPing: true,
38
+ includeChildSessions: false,
39
+ debug: false,
40
+ providerAllowlist: ["copilot"],
41
+ modelAllowlist: ["claude", "anthropic", "sonnet", "opus", "haiku", "gpt"],
42
+ }
43
+
44
+ export function getConfig(options: Record<string, unknown> | undefined): KeepaliveConfig {
45
+ const o = options ?? {}
46
+ const env = process.env
47
+
48
+ return {
49
+ enabled: boolOpt(o.enabled, env.OPENCODE_KEEPALIVE_ENABLED !== "false" && DEFAULTS.enabled),
50
+ intervalMs: durationOpt(
51
+ o.intervalMs,
52
+ o.intervalSeconds,
53
+ numEnv(env.OPENCODE_KEEPALIVE_INTERVAL_MS, DEFAULTS.intervalMs),
54
+ ),
55
+ windowMs: durationOpt(
56
+ o.windowMs,
57
+ o.windowMinutes ? Number(o.windowMinutes) * 60 : undefined,
58
+ numEnv(env.OPENCODE_KEEPALIVE_WINDOW_MS, DEFAULTS.windowMs),
59
+ ),
60
+ injectSystemInstruction: boolOpt(
61
+ o.injectSystemInstruction,
62
+ DEFAULTS.injectSystemInstruction,
63
+ ),
64
+ pingToken:
65
+ typeof o.pingToken === "string" && o.pingToken.length > 0
66
+ ? o.pingToken
67
+ : DEFAULTS.pingToken,
68
+ revertPing: boolOpt(
69
+ o.revertPing,
70
+ env.OPENCODE_KEEPALIVE_REVERT_PING !== "false" && DEFAULTS.revertPing,
71
+ ),
72
+ includeChildSessions: boolOpt(o.includeChildSessions, DEFAULTS.includeChildSessions),
73
+ debug: boolOpt(o.debug, env.OPENCODE_KEEPALIVE_DEBUG === "true"),
74
+ providerAllowlist: listOpt(o.providerAllowlist, DEFAULTS.providerAllowlist),
75
+ modelAllowlist: listOpt(o.modelAllowlist, DEFAULTS.modelAllowlist),
76
+ }
77
+ }
78
+
79
+ function boolOpt(value: unknown, fallback: boolean): boolean {
80
+ return typeof value === "boolean" ? value : fallback
81
+ }
82
+
83
+ function listOpt(value: unknown, fallback: string[]): string[] {
84
+ if (!Array.isArray(value)) return fallback
85
+ const filtered = value.filter((item): item is string => typeof item === "string")
86
+ return filtered.length > 0 ? filtered : fallback
87
+ }
88
+
89
+ /** Accept an explicit `*Ms` option, a `*Seconds` option, or fall back. */
90
+ function durationOpt(ms: unknown, seconds: unknown, fallback: number): number {
91
+ if (typeof ms === "number" && Number.isFinite(ms) && ms > 0) return ms
92
+ if (typeof seconds === "number" && Number.isFinite(seconds) && seconds > 0)
93
+ return seconds * 1000
94
+ return fallback
95
+ }
96
+
97
+ function numEnv(raw: string | undefined, fallback: number): number {
98
+ if (!raw) return fallback
99
+ const value = Number(raw)
100
+ return Number.isFinite(value) && value > 0 ? value : fallback
101
+ }
package/lib/control.ts ADDED
@@ -0,0 +1,37 @@
1
+ import { readFileSync } from "node:fs"
2
+ import { mkdir, rename, writeFile } from "node:fs/promises"
3
+ import { dirname } from "node:path"
4
+ import { controlFilePath } from "./paths"
5
+
6
+ export type KeepaliveControl = {
7
+ version: 1
8
+ enabled: boolean
9
+ updatedAt: number
10
+ }
11
+
12
+ export function readControl(): KeepaliveControl | undefined {
13
+ try {
14
+ const value = JSON.parse(
15
+ readFileSync(controlFilePath(), "utf8"),
16
+ ) as Partial<KeepaliveControl>
17
+ if (value.version !== 1 || typeof value.enabled !== "boolean") return undefined
18
+ if (typeof value.updatedAt !== "number") return undefined
19
+ return value as KeepaliveControl
20
+ } catch {
21
+ return undefined
22
+ }
23
+ }
24
+
25
+ export async function writeControl(enabled: boolean): Promise<KeepaliveControl> {
26
+ const value: KeepaliveControl = {
27
+ version: 1,
28
+ enabled,
29
+ updatedAt: Date.now(),
30
+ }
31
+ const path = controlFilePath()
32
+ const tmp = `${path}.${process.pid}.${value.updatedAt}.tmp`
33
+ await mkdir(dirname(path), { recursive: true })
34
+ await writeFile(tmp, `${JSON.stringify(value)}\n`, "utf8")
35
+ await rename(tmp, path)
36
+ return value
37
+ }