import { homedir } from "node:os"; import { isAbsolute, join, resolve } from "node:path"; import type { ExtensionAPI, ExtensionContext, } from "@earendil-works/pi-coding-agent"; import { parseBooleanSetting, resolveSetting, type SettingDeclaration, } from "./pi-ext-settings.ts"; export const ANIM_SETTING: SettingDeclaration = { key: "loop.anim", label: "Kitten animation", description: "Use the safe-for-work kitten instead of the vixen. Any present PI_LOOP_ANIM value, including empty, 0, or false, enables it; settings files accept booleans only. Invalid configuration warns and selects the kitten.", env: "PI_LOOP_ANIM", default: false, parse: (raw, source) => source === "env" && typeof raw === "string" ? true : parseBooleanSetting(raw, source), }; export function resolveKitten( pi: Pick, ctx: ExtensionContext, ): boolean { const setting = resolveSetting(pi, ctx, ANIM_SETTING); if (setting.ok) return setting.value; ctx.ui.notify( `loop: invalid ${ANIM_SETTING.key} (${setting.source}): ${setting.error}; using safe-for-work kitten`, "warning", ); return true; } export const DEFAULT_STATE_PATH = ".pi/loop/state.db"; /** Accepts absolute, `~/`, or project-relative paths; relative paths resolve against the session cwd. */ export function parseStatePath(raw: unknown): string { if ( typeof raw !== "string" || !raw.trim() || raw.length > 4096 || /[\x00-\x1f]/u.test(raw) ) throw new Error("expected a state database file path"); return raw.startsWith("~/") || raw.startsWith("~\\") ? join(homedir(), raw.slice(2)) : raw; } export function statePathFor(cwd: string, value: string): string { return isAbsolute(value) ? value : resolve(cwd, value); } export const STATE_PATH_SETTING: SettingDeclaration = { key: "loop.statePath", label: "State database", description: "SQLite file holding runs, gate attempts, and events; relative paths resolve against the project directory.", env: "PI_LOOP_STATE_PATH", default: DEFAULT_STATE_PATH, parse: parseStatePath, };