Scout researcher for future UI-framework decoupling in a TypeScript/Bun terminal application.
<critical_rules priority="absolute" enforcement="strict">
Do not modify files or run write operations; research and report only.
Ground recommendations in inspected repository context and relevant UI architecture patterns/docs.
Optimize for making the current OpenTUI layer replaceable later without destabilizing domain logic, tab behavior, tests, or chatbot code.
</critical_rules>
Repository: C:\Users\nicolas\Documents\GitHub\bballgenius. BBallGenius is a Bun monorepo NBA analytics terminal hub using OpenTUI, DuckDB, TypeScript, and a LangGraph chatbot tab. Key local architecture from AGENTS.md: src/index.ts bootstraps OpenTUI; src/core/appShell.ts owns key routing/tab shell/help overlay; src/tabs/* contain tab implementations; src/shared/* contains cross-tab utilities. Tabs must not import sibling tabs. User wants the UI framework detached so it can be changed in the future.
Research and produce an architecture-planning report for decoupling the UI framework from the application. Focus on what should be abstracted, what should remain concrete, and a safe migration path.
Inspect repo structure and relevant files if available: src/index.ts, src/core/appShell.ts, src/tabs/registry.ts, representative tab.ts files, shared theme/keyboard helpers, and tests that assert TUI behavior.
Research applicable patterns for UI framework decoupling in TypeScript/terminal apps: ports/adapters, presentation model/view-model, renderer abstraction boundaries, event/input abstraction, snapshot/test seam design. Use external docs/source only as needed; summarize sources/patterns without over-quoting.
Identify current coupling points likely tied to OpenTUI APIs, renderables, colors/styles, keyboard events, renderer lifecycle, layout measurements, and tests/snapshots.
Recommend a target architecture with named layers/interfaces and file/module boundaries appropriate for this repo.
Provide a phased migration plan that avoids a big-bang rewrite and preserves existing tests; include validation commands likely relevant for this repo.
Call out risks, anti-patterns, and where abstraction would be premature or harmful.
<output_format>
Return only a concise markdown report with these sections:
Findings
Current Coupling Points
Recommended Abstraction Boundary
Proposed Module/Layout Changes
Migration Plan
Testing/Verification Strategy
Risks and Non-Goals
Sources/Patterns Consulted
</output_format>
Assistant
Findings
BBallGenius is currently cleanly separated at the tab/domain boundary, but not at the UI framework boundary. The application already has good seams to preserve:
Tab registration and isolation are centralized in C:\Users\nicolas\Documents\GitHub\bballgenius\src\tabs\registry.ts:52-80.
Tab behavior is expressed through a shared contract in C:\Users\nicolas\Documents\GitHub\bballgenius\src\tabs\types.ts:8-30.
Shell-level routing is centralized in C:\Users\nicolas\Documents\GitHub\bballgenius\src\core\appShell.ts:395-528.
Domain/data logic is mostly outside render construction, e.g. tab queries.ts files and chatbot agent modules.
The main issue is that the existing tab contract exposes OpenTUI types directly: BoxRenderable, CliRenderer, and KeyEvent in C:\Users\nicolas\Documents\GitHub\bballgenius\src\tabs\types.ts:1-37. This makes every tab an OpenTUI adapter rather than a UI-independent feature module.
The safest future-swap strategy is not to hide all rendering immediately. Instead, introduce a narrow app-owned UI port and gradually move tab state/interaction into presentation models while keeping an OpenTUI adapter as the only concrete renderer.
Current Coupling Points
Bootstrap/lifecycle:src/index.ts directly imports createCliRenderer and owns OpenTUI lifecycle: create/start/requestRender/destroy in C:\Users\nicolas\Documents\GitHub\bballgenius\src\index.ts:3-63.
Shell layout/renderables:createAppShell(renderer: CliRenderer) constructs BoxRenderable, TabSelectRenderable, TextRenderable, and mutates .visible, .content, .requestRender() in C:\Users\nicolas\Documents\GitHub\bballgenius\src\core\appShell.ts:70-210 and 244-302.
Input events: Shell and tabs consume OpenTUI KeyEvent directly; test helper also constructs OpenTUI KeyEvent in C:\Users\nicolas\Documents\GitHub\bballgenius\src\core\appShell.ts:424-558.
Tab contract:AppShellTab.container: BoxRenderable, handleKeyPress(event: KeyEvent), and factories taking CliRenderer couple all tab implementations to OpenTUI (src\tabs\types.ts:8-37).
Game Center: BoxRenderable, ScrollBoxRenderable, TextRenderable in src\tabs\gameCenter\tab.ts:1-154.
SQL Sandbox: InputRenderable, ScrollBoxRenderable, TextRenderable in src\tabs\sqlSandbox\tab.ts:1-220.
Time Machine: direct panels/inputs/scrolls in src\tabs\timeMachine\tab.ts:1-180.
Chatbot: ChatApp owns OpenTUI widgets and model selector overlay in src\tabs\chatbot\chatApp.ts:1-225.
Layout measurement: Tabs read OpenTUI dimensions/scroll state directly, e.g. gameListScroll.height, scrollTop in src\tabs\gameCenter\tab.ts:306-328; chatbot auto-scroll depends on scrollTop, scrollHeight, height in src\tabs\chatbot\chatApp.ts:64-74.
Styling/text boundary: ANSI strings are converted to OpenTUI styled text via ansiToStyledText, e.g. src\tabs\gameCenter\tab.ts:363-423; theme mixes semantic roles with concrete hex/OpenTUI values in src\shared\utils\theme.ts:6-23.
Tests: Many tests depend on OpenTUI test renderer, captured frames, renderable visibility, and raw widget fields:
createTestRenderer and .container.visible assertions in src\tests\app_shell.test.ts:58-96.
Manual OpenTUI layout in src\tests\tui_integration.test.ts:73-170.
Golden frame snapshots via captureCharFrame() in src\tests\golden_snapshot.test.ts:74-130.
Span/color helpers import OpenTUI captured frame types in src\tests\helpers\spans.ts:1-57.
Recommended Abstraction Boundary
Use a ports/adapters + presentation model boundary:
Near-term, keep current tab.ts classes as compatibility wrappers. Add presenters behind them one tab at a time.
src/shared/utils/theme.ts should eventually split into:
semantic tokens: primary, background, borderFocused, etc.
terminal ANSI helpers: keep where text formatting needs ANSI.
OpenTUI color mapping: adapter-only.
keyboardHelp.ts currently imports TAB_REGISTRY from tabs in C:\Users\nicolas\Documents\GitHub\bballgenius\src\shared\utils\keyboardHelp.ts:5-18; for stronger decoupling, move shortcut metadata to UI-neutral tab definitions and have both help overlay and generated JSON consume that.
Migration Plan
Introduce app-owned input type
Add UiKeyEvent.
Add converter from OpenTUI KeyEvent.
Keep existing handleKeyPress(KeyEvent) temporarily; internally convert in shell/router first.
Preserve makeTestKeyEvent() until tests are migrated.
Extract shell presenter first
Move active tab index, help visibility, global key routing, footer status composition out of appShell.ts.
Keep createAppShell(renderer) as compatibility facade.
Existing tests should still pass because public shell shape can remain stable.
src/index.ts should depend on createOpenTuiRuntime() rather than createCliRenderer() directly.
Do not attempt a generic renderer tree yet.
Extract tab presenters incrementally
Start with Game Center: it has clear state and limited inputs.
Move selected game/player/focus/render-text generation into gameCenter/presenter.ts.
Leave actual BoxRenderable construction in current tab.ts.
Then SQL Sandbox, Time Machine, Chatbot.
Move layout specs out of tab constructors
Introduce app-owned panel/input/scroll/text descriptors only after one presenter works.
Avoid fully generic layout too early; terminal layouts are highly specific.
Stabilize testing seams
Add presenter tests that assert state transitions and rendered text buffers without OpenTUI.
Keep OpenTUI golden tests as adapter/integration tests.
Gradually reduce assertions against private renderable fields such as ['searchInput'], .container.visible, and .scrollTop.
Only then evaluate alternate UI framework
Once presenters and shell controller are UI-neutral, a second adapter can be prototyped without touching DuckDB, tabs’ domain logic, or chatbot agent logic.
Testing/Verification Strategy
Preserve current commands during migration:
bash
bun test src/tests --concurrency=1
bun test src/tabs/chatbot/__tests__ --concurrency=1
NBA_DUCKDB_PATH=data/fixtures/nba.ci.duckdb bun run ci:integration
bunx tsc --noEmit -p tsconfig.json
Add new test layers:
Presenter unit tests: no OpenTUI, no captured frames; assert commands → state/view model.
Risk: over-abstracting render trees. A generic terminal component DSL can become a second UI framework. Start with input/runtime/presenter seams first.
Risk: breaking snapshot stability. Golden tests depend on exact OpenTUI layout and glyph behavior; keep compatibility wrappers until adapter snapshots are intentionally updated.
Risk: hiding useful OpenTUI features. Focus, scroll, viewport culling, sticky scroll, and input events are real terminal concerns. Abstract only the app-facing contract, not every widget property.
Risk: chatbot coupling.ChatApp combines streaming state, metrics, model selector overlay, and OpenTUI widgets. Do not migrate it first; extract its conversation presentation model after simpler tabs.
Non-goal: replacing OpenTUI now.
Non-goal: abstracting DuckDB, LangGraph, OpenRouter, or SQL query modules.
Non-goal: eliminating all ANSI helpers immediately; ANSI-to-styled conversion is already a useful boundary.
Representative tabs and OpenTUI tests listed above.
External patterns:
Ports/adapters / hexagonal architecture: decouple application logic from external systems via ports implemented by adapters.
Martin Fowler, Presentation Model: move presentation behavior/state out of view widgets into a UI-independent model.
React renderer architecture: useful analogy that reconciliation/application model can be separated from host rendering targets.
OpenTUI project docs/search results: OpenTUI is a terminal UI core with TypeScript bindings and separate renderer layers, reinforcing the adapter approach.