import { existsSync, readFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; import { CONFIG_DIR_NAME } from "@earendil-works/pi-coding-agent"; export interface CodexCompactionConfig { autoCompact: boolean; thresholdRatio: number; } const DEFAULT_CONFIG: CodexCompactionConfig = { autoCompact: true, thresholdRatio: 0.9, }; function readConfig(path: string): Partial { if (!existsSync(path)) return {}; try { const parsed = JSON.parse(readFileSync(path, "utf8")) as Record; return { ...(typeof parsed.autoCompact === "boolean" ? { autoCompact: parsed.autoCompact } : {}), ...( typeof parsed.thresholdRatio === "number" && parsed.thresholdRatio > 0 && parsed.thresholdRatio < 1 ? { thresholdRatio: parsed.thresholdRatio } : {} ), }; } catch { return {}; } } export function loadConfig(cwd: string, projectTrusted: boolean): CodexCompactionConfig { const globalConfig = readConfig(join(homedir(), CONFIG_DIR_NAME, "agent", "pi-codex-compaction.json")); const projectConfig = projectTrusted ? readConfig(join(cwd, CONFIG_DIR_NAME, "pi-codex-compaction.json")) : {}; return { ...DEFAULT_CONFIG, ...globalConfig, ...projectConfig }; }