UNPKG

@hasna/projects

Version:

High-level project management CLI, MCP server, SDK, and AI SDK agent loop for coding agents

203 lines (167 loc) • 10.3 kB
# Projects TUI (`projects ui`) — design `projects ui` is the OpenTUI/Solid terminal interface for the projects registry. It is a full-screen alternate-screen app that browses, searches, creates, edits, archives, unarchives, and deletes projects, and manages roots, recipes, and agents from the terminal. This document is the deep dive behind the README section. It describes the architecture, the keymap, theming, and what you touch when you add a view. ## Architecture The TUI is split into three layers, each with a one-way dependency: ``` src/cli/commands/ui.tsx entrypoint: TTY guard, runtime loader │ (never imports opentui) ▼ src/cli/tui/runtime.tsx async loader → tui-solid/runtime.tsx │ ▼ src/cli/tui-solid/ the Solid/OpenTUI shell runtime.tsx renderer lifecycle, alternate screen, signals App.tsx provider tree + routed content routes.ts route model (the view switch keys) context/* state, theme, dialogs, toasts, commands, keymap component/* one component per route (projects, project, roots, recipes, agents, settings) │ (views import ONLY the data-layer names + types) ▼ src/cli/tui/ data.ts the data layer: routes through the store seam format.ts pure display formatting (unit-tested, headless) theme.ts palette + theme-mode resolution (unit-tested) types.ts the data-layer contract (ProjectsDataApi) ``` ### The data layer: one store seam, no mode switch `@hasna/projects` has exactly one store seam — `resolveProjectStore()` in `src/store/project-store.ts` — that routes local sqlite vs hosted `/v1` automatically from the environment. There is **no** `local`/`remote` mode switch the way `apps/emails` has one. `src/cli/tui/data.ts` therefore never touches sqlite or the HTTP client directly: every function resolves the active store through the seam and calls `ProjectStore` methods only. - `listProjects`, `getProject`, `createProject`, `updateProject`, `archiveProject`, `unarchiveProject`, `deleteProject` map 1:1 onto the store. - `listRoots`, `listRecipes`, `listAgents`, `listProjectEvents`, `listProjectLocations`, `listProjectAgents` expose the shared registries and per-project side data for the Roots/Recipes/Agents and Project views. - `transport()` / `authority()` report which registry is serving the session (local sqlite → `null` authority; hosted → the host) for the status line. - `managementView(project)` enriches a project with `stage` / `priority` / `owner` (from `project.metadata` via `lib/project-management.ts`) and `health` (derived from path state: `ok` for an existing primary path, `missing` for a declared path that is gone, `untracked` for hosted-only `remote-only` projects, `unknown` when nothing is declared). The seam **fails closed** (owner ruling 2026-09-04, hasna/apps#1720): a station with a declared authority but no credential — or with nothing configured and no opt-in — refuses. `data.ts` wraps that refusal in a friendly pointer naming the two legitimate routes (configure hosted credentials, or set `HASNA_PROJECTS_LOCAL=1`). The TUI surfaces the same rule as the CLI; there is no "local fallback" for a silent station. `types.ts` is the contract between the data layer and the UI: the views import **only** the types and the named functions in `data.ts` — never the store seam — so the whole data + format layer stays headless and is unit-tested without a renderer. ### The tui-solid shell `tui-solid/runtime.tsx` owns the renderer lifecycle: it forces the alternate screen (`OTUI_USE_ALTERNATE_SCREEN`), creates the renderer with `screenMode: "alternate-screen"`, installs `SIGINT`/`SIGTERM` handlers that set the exit code and `renderer?.destroy()`, awaits the app's destroy event, and restores the alternate-screen env in `finally`. It loads everything (`@opentui/core`, `@opentui/solid`, the keymap, `App`) with dynamic imports so the process stays cold until the UI actually starts. `App.tsx` composes the provider tree: ``` ProjectsProvider ← state + actions (context/projects-state.tsx) ThemeProvider ← theme resolution (context/theme.tsx) ToastProvider ← transient notices (context/toast.tsx) DialogProvider ← modal dialogs (context/dialog.tsx) CommandProvider ← command palette + static bindings (context/commands.tsx) AppShell ← sidebar + routed content + dialog/toast viewports ``` `RoutedContent` switches on `route.name` (from `routes.ts`) with a Solid `<Switch>`/`<Match>` over the six routes: `projects`, `project`, `roots`, `recipes`, `agents`, `settings`. The `settings` route and `ProjectsDialogs` cover create/edit/archive/unarchive/delete confirmation and the shortcuts dialog. ### The state contract `context/state-contract.ts` is the single coordination point between the shell and the views. Views consume `useProjects()` and get `{ state, actions }` shaped exactly like `ProjectsContextValue`. Nothing outside `context/projects-state.tsx` may call `data.ts` directly — a view asks the shell for state and calls `actions`. State carries the route, the filtered project list, loading/error flags, the filter (`status`/`kind`/`query`), the open detail project plus its events, locations, and assignments, the shared registries, the resolved transport and authority, the open dialog, and a `mutating` flag for button busy states. Actions cover load/reload, filtering, navigation, opening the detail route, dialog lifecycle, and the four project mutations (create/update/archive/delete). ## Keymap The keymap is built in `tui-solid/keymap-input.ts` from `createDefaultOpenTuiKeymap` with the event-match resolver trimmed so unnamed or whitespace terminal sequences never bind to a shortcut (they fall through to focused inputs). App-level bindings are registered in `context/commands.tsx` via `useStaticBindings` and duplicate raw-input handlers so both kitty and legacy modifier encodings work: | Key | Action | | --- | --- | | `Ctrl+P` | Open the Keyboard Shortcuts palette | | `Ctrl+R` | Refresh the project list | | `Esc` | Back (project detail → list); exit from the list view | | `Ctrl+C` | Close a dialog; exit when nothing is open | | `↑`/`↓` | Move the selection (default keymap) | | `Tab`/`Enter` | Focus / activate the selected control (default keymap) | Every visible action is also a command in the palette (`View`: Projects, Roots, Recipes, Agents, Settings, Refresh; `Project`: Create/Edit Project; `Help`: Keyboard Shortcuts), so nothing requires remembering a shortcut. ## Theming Themes live in `src/cli/tui/theme.ts` (unit-tested in `theme.test.ts`): two hex palettes — `LIGHT` (Catppuccin Latte accents on a clean white base) and `DARK` (open-aicopilot-style dark) — plus mode resolution. Mode resolution, in precedence order: 1. An explicit mode: `PROJECTS_TUI_THEME` (aliases `HASNA_PROJECTS_TUI_THEME`, `TUI_THEME`, `TERMINAL_THEME`) set to `light` or `dark`. 2. `COLORFGBG` (the `fg;bg` ANSI color indices the terminal reports). 3. OS/terminal appearance hints (`APPLE_INTERFACE_STYLE`, `OS_APPEARANCE`, `TERM_BACKGROUND`, `GTK_THEME`, `KDE_COLOR_SCHEME`, `ITERM_PROFILE`). 4. `light` default when nothing is detectable. `normalizeThemeMode()` maps any non-`light`/`dark`/`auto` input to `auto`, so a typo degrades to auto-detection instead of a broken theme. `tui-solid/context/theme.tsx` adapts the palettes to the renderer's `TuiColorTheme` shape (background roles, text roles, markdown roles, label palette) and serves it through `ThemeProvider` / `useTheme()`. `labelColor()` hashes a label string into the palette for per-label tag colors. The runtime `backgroundColor` and the app shell read straight from `useTheme()`, so a theme change is reflected on the next render. ## Adding a view A new view touches three coordination points (plus the view component itself): 1. **`src/cli/tui-solid/routes.ts`** — add the route to the `ProjectsRoute` union and give it a `routeLabel`. This is the compile-time switch key. 2. **`src/cli/tui-solid/context/state-contract.ts`** — if the view needs new shared state or actions, extend `ProjectsViewState` / `ProjectsActions` and implement them in `context/projects-state.tsx` (the one place allowed to call `data.ts`). If the view only needs existing state, skip this step. 3. **`src/cli/tui-solid/App.tsx`** — register the component in the `RoutedContent` `<Switch>`/`<Match>` block. This is the component registry. 4. **`src/cli/tui-solid/context/commands.tsx`** — add a palette command (a `View`-category navigate command) so the view is reachable without a shortcut. Then create the view component under `src/cli/tui-solid/component/`, importing `useProjects()` for state/actions, `useTheme()` for colors, and the data-layer types (not the store). If the view needs a new data-layer call, extend `ProjectsDataApi` in `src/cli/tui/types.ts` and implement it in `data.ts`, and add a hermetic case to `src/cli/tui/data.test.ts`. ## Testing - `src/cli/tui/format.test.ts` — pure formatting (relative time, truncation, kind/status/stage/priority/health labels). - `src/cli/tui/theme.test.ts` — mode normalization, system detection (forced env + `COLORFGBG`), and palette integrity (every key present and non-empty in both palettes). - `src/cli/tui/data.test.ts` — hermetic lifecycle against a temp sqlite registry (`HASNA_PROJECTS_LOCAL=1` + `HASNA_PROJECTS_DB_PATH`, fake HOME, Keychain-disabled `HASNA_STATION`) and the fail-closed path for a scrubbed environment. - `src/cli/tui/build-contract.test.ts` — the build split stays healthy: runtime bundle external list, lightweight `commands/ui.tsx`, alternate-screen signal cleanup. - `src/cli/tui/ui-runtime-contract.test.ts` — rebuilds `dist/cli/ui-runtime-bundle.js` and asserts every external is a declared runtime dependency, `@opentui/core` is external (its prebuilt platform packages must resolve from core itself), and a non-TTY `ui` run refuses.