/** * Shared settings utilities for pi extensions. * * All extension settings live as namespaced keys in ~/.pi/agent/settings.json. */ import { existsSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { getAgentDir } from "@mariozechner/pi-coding-agent"; const SETTINGS_PATH = join(getAgentDir(), "settings.json"); export interface Settings { lastChangelogVersion?: string; theme?: string; enabledModels?: string[]; defaultProvider?: string; defaultModel?: string; defaultThinkingLevel?: string; thinkingBudgets?: Record; quietStart?: boolean; collapseChangelog?: boolean; editorPaddingX?: number; showHardwareCursor?: boolean; shellPath?: string; shellCommandPrefix?: string; npmCommand?: string[]; sessionDir?: string; hideThinkingBlock?: boolean; ui?: UiSettings; animations?: AnimSettings; autoRename?: AutoRenameSettings; ghost?: GhostSettings; taint?: TaintSettings; dbg?: DbgSettings; mux?: MuxSettings; sessionRecap?: SessionRecapSettings; neko?: NekoSettings; } export interface UiSettings { icons?: Record; colors?: Record; cost?: { showProvider?: boolean }; } export interface AnimSettings { working?: string; thinking?: string; tool?: string; width?: "full" | "default" | number; fps?: number; enabled?: boolean; random?: boolean; } export interface AutoRenameSettings { model?: string; } export interface GhostSettings { model?: { provider?: string; id?: string }; overlay?: { anchor?: string; width?: string; maxHeight?: string; margin?: { top?: number; bottom?: number; left?: number; right?: number }; }; behavior?: { autoOpen?: boolean; hideOnClose?: boolean }; } export interface TaintSettings { taintAllowlist?: string[]; } export interface DbgSettings { enabled: boolean; } export interface MuxSettings { cooldownMs?: number; providers?: Record; } export interface SessionRecapSettings { focusMinSeconds?: number; disable?: boolean; model?: string; } export interface NekoSettings { enabled?: boolean; } function loadRaw(): Settings { try { if (!existsSync(SETTINGS_PATH)) return {}; return JSON.parse(readFileSync(SETTINGS_PATH, "utf-8")); } catch { return {}; } } function saveRaw(s: Settings): void { try { writeFileSync(SETTINGS_PATH, JSON.stringify(s, null, 2) + "\n", "utf-8"); } catch {} } export function getSettings(namespace: keyof Settings): T { const s = loadRaw(); return (s[namespace] ?? {}) as T; } export function updateSettings(namespace: keyof Settings, values: Partial): void { const s = loadRaw(); const existing = (s[namespace] ?? {}) as Record; s[namespace] = { ...existing, ...values }; saveRaw(s); } export function setSettings(namespace: keyof Settings, values: T): void { const s = loadRaw(); s[namespace] = values; saveRaw(s); }