repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/lib/settings.ts

Raw
File outline
/**
 * 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<string, number>;
  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<string, string>;
  colors?: Record<string, string>;
  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<string, { keys?: string[]; cooldownMs?: number }>;
}

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<T extends object>(namespace: keyof Settings): T {
  const s = loadRaw();
  return (s[namespace] ?? {}) as T;
}

export function updateSettings<T extends object>(namespace: keyof Settings, values: Partial<T>): void {
  const s = loadRaw();
  const existing = (s[namespace] ?? {}) as Record<string, unknown>;
  s[namespace] = { ...existing, ...values };
  saveRaw(s);
}

export function setSettings<T extends object>(namespace: keyof Settings, values: T): void {
  const s = loadRaw();
  s[namespace] = values;
  saveRaw(s);
}