import { boolOr, clampInt, clampNum, isObject } from '@snowluma/common/coerce'; import { createLogger } from '@snowluma/common/logger'; import fs from 'fs'; import path from 'path'; // Server-side store for the WebUI customization config (`config/ui.json`). // // This is the single source of truth for appearance (A) + layout (C). The // browser caches it in localStorage only to avoid a first-paint flash; the // canonical copy lives here so a deployer's look + curated layout follow them // across devices/browsers. // // Trust boundaries that this module enforces: // * `normalizeAppearance` is total — any unknown/corrupt input collapses to // safe defaults. This matters because the cosmetic subset is served to an // UNAUTHENTICATED client (the login page) via `GET /api/ui/public`, so it // must never echo attacker-controlled garbage. // * The background-IMAGE lifecycle fields (`hasImage` / `imageMime` / // `imageVersion`) are server-managed: a plain `POST /api/ui` cannot set // them. Only the dedicated upload/delete endpoints mutate them. A client // save preserves whatever is currently on disk. const log = createLogger('WebUI.UiConfig'); const CONFIG_DIR = 'config'; const UI_CONFIG_PATH = path.join(CONFIG_DIR, 'ui.json'); /** Directory for operator-uploaded UI assets (currently just the background). */ export const UI_ASSETS_DIR = path.join(CONFIG_DIR, 'ui-assets'); /** Fixed path of the single background image (overwrite-on-upload). */ export const BACKGROUND_IMAGE_PATH = path.join(UI_ASSETS_DIR, 'background'); /** Reject uploads larger than this (bytes). */ export const MAX_BACKGROUND_BYTES = 5 * 1024 * 1024; export const UI_CONFIG_VERSION = 1 as const; // ─── Schema ────────────────────────────────────────────────────────────── export type ThemeMode = 'light' | 'dark' | 'system'; export type AccentMode = 'preset' | 'custom'; export type AccentScope = 'sidebar' | 'global'; export type DarkIntensity = 'soft' | 'black'; export type SidebarStyle = 'follow' | 'panel' | 'accent'; export type BackgroundType = 'none' | 'solid' | 'gradient' | 'image'; export type Density = 'cozy' | 'compact'; export type TimeFormat = '12h' | '24h'; /** Full color scheme. 'default' keeps the sky theme (driven by `mode`); the * Catppuccin flavors are complete palettes that also fix light/dark. */ export type Palette = | 'default' | 'catppuccin-latte' | 'catppuccin-frappe' | 'catppuccin-macchiato' | 'catppuccin-mocha' | 'rose-pine' | 'rose-pine-moon' | 'rose-pine-dawn' | 'nord' | 'everforest-dark' | 'everforest-light'; export interface UiBackground { type: BackgroundType; /** Solid-fill colour (hex). Used when type === 'solid'. */ color: string; /** Gradient preset id (frontend owns the catalogue). Used when type === 'gradient'. */ gradient: string; /** Image readability overlay: 0 (image fully visible) … 1 (fully masked). */ imageOpacity: number; /** Image blur in px (0…40). */ imageBlur: number; // ── server-managed (upload/delete endpoints only) ── hasImage: boolean; imageMime: string; /** Bumped on each successful upload so the browser can cache-bust. */ imageVersion: number; } export interface UiAppearance { mode: ThemeMode; accentMode: AccentMode; /** Preset accent id (frontend catalogue: sky/blue/violet/…). */ accentPreset: string; /** Custom accent colour (hex). Used when accentMode === 'custom'. */ accentCustom: string; accentScope: AccentScope; darkIntensity: DarkIntensity; /** Full color scheme; 'default' = the built-in sky theme. */ palette: Palette; sidebarStyle: SidebarStyle; background: UiBackground; /** Sans font preset id (frontend catalogue); 'custom' uses `fontSansCustom`. */ fontSans: string; /** Free-form sans font-family stack, used when `fontSans === 'custom'`. */ fontSansCustom: string; /** Mono font preset id (frontend catalogue); 'custom' uses `fontMonoCustom`. */ fontMono: string; /** Free-form mono font-family stack, used when `fontMono === 'custom'`. */ fontMonoCustom: string; /** Global UI scale, 0.9…1.2. */ uiScale: number; /** Corner radius in rem, 0…2. */ radius: number; density: Density; /** Soften animations (transforms off, shorter durations). */ reduceMotion: boolean; /** Hard-disable all UI animations (stronger than reduceMotion). */ disableMotion: boolean; /** Use SnowLuma's adaptive cursor. */ customPointerSystem: boolean; /** Replace the browser context menu with SnowLuma's context menu. */ customContextMenu: boolean; highContrast: boolean; sidebarPinned: boolean; timeFormat: TimeFormat; /** Dashboard poll interval (ms); 0 = paused. */ pollInterval: number; /** * Operator custom CSS, applied AFTER auth only (never on the login page — * `publicAppearance` strips it). Capped at 50 KB. The client injects it as * the last