/** * Config for pi-peak-hours, read once at extension load from * `~/.pi/agent/settings.json` (or `$PI_CODING_AGENT_DIR/agent/settings.json`) * under the `peakHours` key. `/reload` recreates the extension and re-reads * the config; there is no hot-reload. */ import { readFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; import { SCHEDULES } from "./schedules.ts"; export interface PeakHoursConfig { enabled: boolean; /** "auto" (system zone) | "UTC" | an IANA zone name, e.g. "Asia/Yekaterinburg". */ timezone: string; /** "peak": hide the widget when everything is off-peak. "always": always show. */ show: "peak" | "always"; /** Schedule ids to track. */ providers: string[]; } const DEFAULTS: PeakHoursConfig = { enabled: true, timezone: "auto", show: "peak", providers: SCHEDULES.map((s) => s.id), }; const KNOWN_IDS = new Set(SCHEDULES.map((s) => s.id)); function expandTilde(path: string): string { if (path === "~") return homedir(); if (path.startsWith("~/")) return join(homedir(), path.slice(2)); return path; } /** Path of the settings file this extension reads. */ export function settingsPath(): string { const envDir = process.env.PI_CODING_AGENT_DIR; const base = envDir ? expandTilde(envDir) : join(homedir(), ".pi", "agent"); return join(base, "settings.json"); } export function loadConfig(): PeakHoursConfig { let section: Record = {}; try { const raw = JSON.parse(readFileSync(settingsPath(), "utf8")) as unknown; if (typeof raw === "object" && raw !== null && typeof (raw as Record)["peakHours"] === "object") { section = (raw as Record)["peakHours"] as Record; } } catch { // missing or unreadable settings file → defaults } const providers = Array.isArray(section.providers) ? section.providers.filter((p): p is string => typeof p === "string" && KNOWN_IDS.has(p)) : []; return { enabled: typeof section.enabled === "boolean" ? section.enabled : DEFAULTS.enabled, timezone: typeof section.timezone === "string" ? section.timezone : DEFAULTS.timezone, show: section.show === "always" ? "always" : DEFAULTS.show, providers: providers.length > 0 ? providers : DEFAULTS.providers, }; }