import { readFileSync } from "node:fs"; export const THEME_DEFINITIONS = [ { id: "frappe", themeName: "trio-catppuccin-frappe", label: "Catppuccin Frappé" }, { id: "dracula", themeName: "trio-dracula", label: "Dracula" }, { id: "one-monokai", themeName: "trio-one-monokai", label: "One Monokai" }, ] as const; export type ThemeId = (typeof THEME_DEFINITIONS)[number]["id"]; export type HexColor = `#${string}`; // All required and optional color roles from Pi 1.0.0's theme schema. export const PI_COLOR_ROLES = [ "accent", "border", "borderAccent", "borderMuted", "success", "error", "warning", "muted", "dim", "text", "thinkingText", "selectedBg", "scrollbarTrack", "scrollbarThumb", "searchMatchBg", "searchMatchText", "userMessageBg", "userMessageText", "customMessageBg", "customMessageText", "customMessageLabel", "toolPendingBg", "toolSuccessBg", "toolErrorBg", "toolTitle", "toolOutput", "mdHeading", "mdLink", "mdLinkUrl", "mdCode", "mdCodeBlock", "mdCodeBlockBorder", "mdQuote", "mdQuoteBorder", "mdHr", "mdListBullet", "toolDiffAdded", "toolDiffRemoved", "toolDiffContext", "syntaxComment", "syntaxKeyword", "syntaxFunction", "syntaxVariable", "syntaxString", "syntaxNumber", "syntaxType", "syntaxOperator", "syntaxPunctuation", "thinkingOff", "thinkingMinimal", "thinkingLow", "thinkingMedium", "thinkingHigh", "thinkingXhigh", "thinkingMax", "bashMode", ] as const; export type PiColorRole = (typeof PI_COLOR_ROLES)[number]; export interface Palette { readonly id: ThemeId; readonly themeName: string; readonly label: string; readonly terminalBg: HexColor; readonly terminalFg: HexColor; readonly inputBg: HexColor; readonly accent: HexColor; readonly colors: Readonly>; } function record(value: unknown, label: string): Record { if (!value || typeof value !== "object" || Array.isArray(value)) { throw new Error(`${label} must be an object`); } return value as Record; } export function resolveColor( value: unknown, vars: Record, visiting: ReadonlySet = new Set(), ): HexColor { if (typeof value !== "string") throw new Error("Expected a color or variable name"); if (/^#[\da-f]{6}$/i.test(value)) return value.toUpperCase() as HexColor; if (visiting.has(value)) throw new Error(`Circular color variable: ${value}`); if (!Object.hasOwn(vars, value)) throw new Error(`Missing color variable: ${value}`); return resolveColor(vars[value], vars, new Set([...visiting, value])); } export function parsePalette(id: ThemeId, document: unknown): Palette { const definition = THEME_DEFINITIONS.find((item) => item.id === id); if (!definition) throw new Error(`Unknown theme id: ${id}`); const theme = record(document, definition.themeName); if (theme.name !== definition.themeName || theme.appearance !== "dark") { throw new Error(`Expected dark theme ${definition.themeName}`); } const vars = record(theme.vars, "vars"); const sourceColors = record(theme.colors, "colors"); const allowedRoles = new Set(PI_COLOR_ROLES); for (const key of Object.keys(sourceColors)) { if (!allowedRoles.has(key)) throw new Error(`Unknown Pi color role: ${key}`); } // Validate unused vars too, so a broken reference never goes unnoticed. for (const key of Object.keys(vars)) resolveColor(key, vars); const colors = Object.fromEntries(PI_COLOR_ROLES.map((role) => { if (!Object.hasOwn(sourceColors, role)) throw new Error(`Missing Pi color role: ${role}`); return [role, resolveColor(sourceColors[role], vars)]; })) as Record; const terminalBg = resolveColor("terminalBg", vars); const terminalFg = resolveColor("terminalFg", vars); const inputBg = resolveColor("inputBg", vars); if (colors.text !== terminalFg) throw new Error("text must match terminalFg"); if (theme.export !== undefined) { const html = record(theme.export, "export"); for (const key of ["pageBg", "cardBg", "infoBg"]) resolveColor(html[key], vars); } return Object.freeze({ ...definition, terminalBg, terminalFg, inputBg, accent: colors.accent, colors: Object.freeze(colors), }); } export function loadPalettes(directory: URL = new URL("../themes/", import.meta.url)): readonly Palette[] { return Object.freeze(THEME_DEFINITIONS.map(({ id, themeName }) => { try { const document: unknown = JSON.parse(readFileSync(new URL(`${themeName}.json`, directory), "utf8")); return parsePalette(id, document); } catch (error) { throw new Error(`Cannot load ${themeName}: ${error instanceof Error ? error.message : String(error)}`); } })); } export function findPalette(palettes: readonly Palette[], idOrName: string | undefined): Palette | undefined { return palettes.find((palette) => palette.id === idOrName || palette.themeName === idOrName); }