Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

16.1 KB
Raw Blame
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';
import { REHYDRATE } from 'redux-persist';

import { DEFAULT_LAYOUT, resolveLayout, type ThemeLayout } from '../lib/theme/layout';
import {
  familyForThemeId,
  findFamily,
  findPreset,
  resolveFamilyVariant,
  THEME_FAMILIES,
} from '../lib/theme/presets';
import type { FontRole } from '../lib/theme/tokens';
import type { Theme, ThemeFamily } from '../lib/theme/types';

export type ThemeMode = 'light' | 'dark' | 'system';
/** Theme variant preference: explicit light/dark, or follow the OS. */
export type ThemeVariant = 'light' | 'dark' | 'system';

/** Sentinel active-theme id meaning "follow OS light/dark preference". */
const SYSTEM_THEME_ID = 'system';
/** Default theme family selected on first run. */
const DEFAULT_FAMILY_ID = 'classic';
export type TabBarLabels = 'hover' | 'always';
/**
 * Global app font size (issue #3120). Drives the root `<html>` font-size, which
 * scales every rem-based Tailwind text utility — including chat messages and the
 * composer — independently of the OS / system font setting.
 */
export type FontSize = 'small' | 'medium' | 'large' | 'xlarge';

/**
 * Single source of truth mapping each {@link FontSize} preset to the concrete
 * root `font-size` applied to `<html>`. `medium` (16px) matches the historical
 * `:root` size, so existing users see no change after the field defaults in.
 * Consumed by `ThemeProvider`; keep this the only place the preset px live.
 */
export const FONT_SIZE_PX: Record<FontSize, string> = {
  small: '14px',
  medium: '16px',
  large: '18px',
  xlarge: '20px',
};

/**
 * Fine-tuning bounds for {@link ThemeState.customFontSizePx} (issue #4246), in
 * px. The range brackets the presets (14–20px) on both sides so users who find
 * even "Extra large" too small can push further, and users who prefer denser
 * text can go below "Small". Applied via {@link selectRootFontSizePx}.
 */
export const MIN_FONT_SIZE_PX = 12;
export const MAX_FONT_SIZE_PX = 28;
/** Root size used when no preset/custom value resolves (matches `medium`). */
const DEFAULT_FONT_SIZE_PX = 16;

/** Clamp an arbitrary px value into the supported range, rounded to whole px. */
export function clampFontSizePx(px: number): number {
  if (!Number.isFinite(px)) return DEFAULT_FONT_SIZE_PX;
  return Math.min(MAX_FONT_SIZE_PX, Math.max(MIN_FONT_SIZE_PX, Math.round(px)));
}

interface ThemeState {
  mode: ThemeMode;
  tabBarLabels: TabBarLabels;
  fontSize: FontSize;
  /**
   * Fine-tuned root font size in px (issue #4246). When set, it overrides the
   * {@link ThemeState.fontSize} preset so users can pick any size in the
   * {@link MIN_FONT_SIZE_PX}–{@link MAX_FONT_SIZE_PX} range. `null` (the default,
   * and the value for all pre-existing persisted state) means "follow the
   * preset", keeping historical behaviour byte-identical. Selecting a preset
   * tile clears this back to `null`.
   */
  customFontSizePx: number | null;
  /**
   * Runtime Developer Mode (default OFF).
   * When true, all developer and diagnostic surfaces become visible.
   * Combines with the build-time `IS_DEV` flag — either one enables the gate.
   * Gating is UI-only: the Rust SecurityPolicy / autonomy tier enforcement
   * is authoritative and is never relaxed by this toggle.
   */
  developerMode: boolean;
  /**
   * Active selection: a theme **family** id (`classic`, `ocean`, `matrix`,
   * `hal9000`, `sepia`) or a custom theme id. Combined with
   * {@link ThemeState.themeVariant} to resolve the concrete theme. Legacy values
   * (`light`/`dark`/`system`/`ocean`/`midnight`) from older persisted state are
   * normalized by {@link selectEffectiveTheme}.
   */
  activeThemeId: string;
  /**
   * Which variant of the active family to apply: explicit `light`/`dark` or
   * `system` (follow OS). Mirrors {@link ThemeState.mode} so the simple
   * Appearance toggle and the Theme Studio variant control stay in sync.
   */
  themeVariant: ThemeVariant;
  /** User-authored themes (full or partial token overrides). */
  customThemes: Theme[];
  /**
   * Appearance → Layout: corner rounding, border contrast and which areas draw
   * borders. Theme-independent. Absent in state persisted before it existed;
   * read it through {@link selectThemeLayout}, which fills the defaults.
   */
  layout: ThemeLayout;
}

const initialState: ThemeState = {
  mode: 'system',
  tabBarLabels: 'hover',
  fontSize: 'medium',
  customFontSizePx: null,
  developerMode: false,
  activeThemeId: DEFAULT_FAMILY_ID,
  themeVariant: 'system',
  customThemes: [],
  layout: DEFAULT_LAYOUT,
};

const themeSlice = createSlice({
  name: 'theme',
  initialState,
  reducers: {
    setThemeMode(state, action: PayloadAction<ThemeMode>) {
      // The simple Appearance light/dark/system toggle drives the variant of the
      // currently-selected family (it no longer forces the Classic family).
      state.mode = action.payload;
      state.themeVariant = action.payload;
    },
    /** Set the light/dark/system variant of the active family. */
    setThemeVariant(state, action: PayloadAction<ThemeVariant>) {
      state.themeVariant = action.payload;
      state.mode = action.payload;
    },
    /** Select a theme family (or a custom theme id). */
    setActiveFamily(state, action: PayloadAction<string>) {
      state.activeThemeId = action.payload;
    },
    /** Back-compat alias: select any family or custom theme by id. */
    setActiveTheme(state, action: PayloadAction<string>) {
      state.activeThemeId = action.payload;
    },
    /** Insert or replace a custom theme (by id) and make it active. */
    upsertCustomTheme(state, action: PayloadAction<Theme>) {
      const theme = action.payload;
      const idx = state.customThemes.findIndex(t => t.id === theme.id);
      if (idx >= 0) state.customThemes[idx] = theme;
      else state.customThemes.push(theme);
      state.activeThemeId = theme.id;
    },
    /** Remove a custom theme; fall back to the default family if it was active. */
    deleteCustomTheme(state, action: PayloadAction<string>) {
      state.customThemes = state.customThemes.filter(t => t.id !== action.payload);
      if (state.activeThemeId === action.payload) {
        state.activeThemeId = DEFAULT_FAMILY_ID;
      }
    },
    /** Set a single colour token (`"R G B"`); auto-forks a preset to custom. */
    setThemeToken(state, action: PayloadAction<{ key: string; value: string }>) {
      const theme = ensureEditableCustom(state);
      theme.colors[action.payload.key] = action.payload.value;
    },
    /** Set a single font role (CSS stack); auto-forks a preset to custom. */
    setFontRole(state, action: PayloadAction<{ role: FontRole; stack: string }>) {
      const theme = ensureEditableCustom(state);
      theme.fonts[action.payload.role] = action.payload.stack;
    },
    /** Patch the backdrop (mesh/solid/image); auto-forks a preset. */
    setThemeBackdrop(
      state,
      action: PayloadAction<{ kind?: 'mesh' | 'solid' | 'image'; imageUrl?: string }>
    ) {
      const theme = ensureEditableCustom(state);
      const prev = theme.backdrop ?? { kind: 'solid' as const };
      theme.backdrop = { ...prev, ...action.payload, kind: action.payload.kind ?? prev.kind };
    },
    /**
     * Clear the user's edits on the active custom theme. For a theme forked
     * from a preset, restore that preset's base palette/fonts (via `basedOn`)
     * rather than the generic Light/Dark defaults; otherwise clear to empty.
     */
    resetActiveTheme(state) {
      const theme = state.customThemes.find(t => t.id === state.activeThemeId);
      if (!theme) return;
      const base = theme.basedOn ? findPreset(theme.basedOn) : undefined;
      theme.colors = base ? { ...base.colors } : {};
      theme.fonts = base ? { ...base.fonts } : {};
      theme.gradient = base?.gradient ? { ...base.gradient } : undefined;
      theme.backdrop = base?.backdrop ? { ...base.backdrop } : undefined;
    },
    setTabBarLabels(state, action: PayloadAction<TabBarLabels>) {
      state.tabBarLabels = action.payload;
    },
    /** Pick a preset size. Clears any fine-tuned override so the preset wins. */
    setFontSize(state, action: PayloadAction<FontSize>) {
      state.fontSize = action.payload;
      state.customFontSizePx = null;
    },
    /**
     * Fine-tune the root font size in px (issue #4246). A number is clamped to
     * the supported range; `null` drops the override back to the active preset.
     */
    setCustomFontSizePx(state, action: PayloadAction<number | null>) {
      state.customFontSizePx = action.payload === null ? null : clampFontSizePx(action.payload);
    },
    /** Merge a partial layout change (e.g. one border area) into the current one. */
    setThemeLayout(
      state,
      action: PayloadAction<
        Partial<Omit<ThemeLayout, 'borderAreas'>> & {
          borderAreas?: Partial<ThemeLayout['borderAreas']>;
        }
      >
    ) {
      const current = resolveLayout(state.layout);
      state.layout = resolveLayout({
        ...current,
        ...action.payload,
        borderAreas: { ...current.borderAreas, ...action.payload.borderAreas },
      });
    },
    resetThemeLayout(state) {
      state.layout = DEFAULT_LAYOUT;
    },
    setDeveloperMode(state, action: PayloadAction<boolean>) {
      state.developerMode = action.payload;
    },
  },
  extraReducers: builder => {
    builder.addCase(REHYDRATE, (state, action) => {
      const rehydrateAction = action as {
        type: typeof REHYDRATE;
        key?: string;
        payload?: Partial<ThemeState>;
      };
      if (rehydrateAction.key !== 'theme') return;
      const inbound = rehydrateAction.payload;
      if (!inbound || typeof inbound !== 'object') return;
      if (
        !Object.prototype.hasOwnProperty.call(inbound, 'themeVariant') &&
        isThemeMode(inbound.mode)
      ) {
        state.themeVariant = inbound.mode;
        console.debug('[theme] migrated persisted mode to themeVariant', { mode: inbound.mode });
      }
    });
  },
});

export const {
  setThemeMode,
  setThemeVariant,
  setActiveFamily,
  setTabBarLabels,
  setFontSize,
  setCustomFontSizePx,
  setDeveloperMode,
  setThemeLayout,
  resetThemeLayout,
  setActiveTheme,
  upsertCustomTheme,
  deleteCustomTheme,
  setThemeToken,
  setFontRole,
  setThemeBackdrop,
  resetActiveTheme,
} = themeSlice.actions;
export default themeSlice.reducer;

/** Built-in theme families (static). */
export const selectThemeFamilies = (): ThemeFamily[] => THEME_FAMILIES;

/** The layout preference with defaults filled for older persisted state. */
export const selectThemeLayout = (state: { theme?: ThemeState }): ThemeLayout =>
  resolveLayout(state.theme?.layout);

export const selectActiveThemeId = (state: { theme?: ThemeState }): string =>
  state.theme?.activeThemeId ?? DEFAULT_FAMILY_ID;

export const selectThemeVariant = (state: { theme?: ThemeState }): ThemeVariant =>
  state.theme?.themeVariant ?? state.theme?.mode ?? 'system';

export const selectCustomThemes = (state: { theme?: ThemeState }): Theme[] =>
  state.theme?.customThemes ?? [];

/**
 * Normalize the active selection to a `{ family, variant, custom }` shape,
 * tolerating legacy persisted ids (`light`/`dark`/`system`/`ocean`/`midnight`).
 * Returns `custom` set when a user theme is selected.
 */
function resolveSelection(ts: ThemeState): {
  family?: ThemeFamily;
  variant: ThemeVariant;
  custom?: Theme;
} {
  const sel = ts.activeThemeId ?? DEFAULT_FAMILY_ID;
  const variantPref = ts.themeVariant ?? ts.mode ?? 'system';

  const custom = ts.customThemes?.find(t => t.id === sel);
  if (custom) return { custom, variant: variantPref };

  // Current family-id selection.
  const direct = findFamily(sel);
  if (direct) return { family: direct, variant: variantPref };

  // Legacy concrete-variant / sentinel ids.
  if (sel === SYSTEM_THEME_ID) return { family: findFamily('classic'), variant: 'system' };
  if (sel === 'midnight') return { family: findFamily('ocean'), variant: 'dark' };
  const owner = familyForThemeId(sel);
  if (owner) return { family: owner, variant: owner.dark?.id === sel ? 'dark' : 'light' };
  return { family: findFamily('classic'), variant: variantPref };
}

function isThemeMode(value: unknown): value is ThemeMode {
  return value === 'light' || value === 'dark' || value === 'system';
}

/** The active family id (`''` when a custom theme is selected). */
export function selectActiveFamilyId(state: { theme?: ThemeState }): string {
  if (!state.theme) return DEFAULT_FAMILY_ID;
  const { family, custom } = resolveSelection(state.theme);
  if (custom) return '';
  return family?.id ?? DEFAULT_FAMILY_ID;
}

/**
 * Return the active custom theme for editing. If a preset (or legacy id) is
 * active, transparently fork the current effective theme into a custom theme,
 * make it active, and return it — so editing a preset "just works" and the
 * original preset stays pristine. Idempotent per source theme (reuses
 * `custom-<sourceId>`), so rapid edits don't spawn duplicates.
 */
function ensureEditableCustom(ts: ThemeState): Theme {
  const existingActive = ts.customThemes.find(t => t.id === ts.activeThemeId);
  if (existingActive) return existingActive;

  const base = effectiveThemeFromState(ts);
  const id = `custom-${base.id}`;
  let theme = ts.customThemes.find(t => t.id === id);
  if (!theme) {
    theme = {
      id,
      name: `${base.name} (custom)`,
      isDark: base.isDark,
      builtIn: false,
      basedOn: base.id,
      colors: { ...base.colors },
      fonts: { ...base.fonts },
      gradient: base.gradient ? { ...base.gradient } : undefined,
      backdrop: base.backdrop ? { ...base.backdrop } : undefined,
    };
    ts.customThemes.push(theme);
  }
  ts.activeThemeId = id;
  return theme;
}

/** Resolve a {@link ThemeState} to the concrete {@link Theme} to apply. */
function effectiveThemeFromState(ts: ThemeState): Theme {
  const { family, variant, custom } = resolveSelection(ts);
  const resolved = variant === 'system' ? resolveTheme('system') : variant;
  if (custom) {
    const isDark = resolved === 'dark';
    return custom.isDark === isDark ? custom : { ...custom, isDark };
  }
  const fam = family ?? findFamily('classic')!;
  return resolveFamilyVariant(fam, resolved);
}

/**
 * Resolve the effective {@link Theme} to apply right now. A custom theme is
 * returned directly; otherwise the active family's variant is resolved, with
 * `system` consulting `prefers-color-scheme`.
 */
export function selectEffectiveTheme(state: { theme?: ThemeState }): Theme {
  if (!state.theme) return findFamily('classic')!.light!;
  return effectiveThemeFromState(state.theme);
}

/**
 * Selector for the persisted `developerMode` preference.
 * Use {@link useDeveloperMode} in components — it combines this with `IS_DEV`.
 */
export const selectDeveloperMode = (state: { theme: ThemeState }): boolean =>
  state.theme.developerMode;

/**
 * The effective root font size in px (numeric). A fine-tuned
 * {@link ThemeState.customFontSizePx} (issue #4246) wins; otherwise the active
 * {@link ThemeState.fontSize} preset drives it. Falls back to `medium` (16) when
 * the slice is absent (SSR / early boot). This is the canonical resolver;
 * {@link selectRootFontSizePx} formats it for the DOM.
 */
export function selectEffectiveFontSizePx(state: { theme?: ThemeState }): number {
  const theme = state.theme;
  const custom = theme?.customFontSizePx;
  if (typeof custom === 'number' && Number.isFinite(custom)) {
    return clampFontSizePx(custom);
  }
  return parseInt(FONT_SIZE_PX[theme?.fontSize ?? 'medium'] ?? FONT_SIZE_PX.medium, 10);
}

/** The effective root `font-size` string to apply to `<html>` (px suffix). */
export function selectRootFontSizePx(state: { theme?: ThemeState }): string {
  return `${selectEffectiveFontSizePx(state)}px`;
}

/**
 * Resolves a `ThemeMode` to the concrete `light` or `dark` value that should
 * be applied to `<html>`. `system` consults `prefers-color-scheme`; in non-DOM
 * contexts (SSR, tests without matchMedia) it falls back to light.
 */
export function resolveTheme(mode: ThemeMode): 'light' | 'dark' {
  if (mode !== 'system') return mode;
  try {
    if (typeof window !== 'undefined' && window.matchMedia) {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    }
  } catch {
    // matchMedia unavailable
  }
  return 'light';
}