## Review of `market-terminal.ts` — unbrowser-fin-terminal
**File:** `.pi/extensions/market-terminal.ts` (≈239KB)
**Repository:** [protostatis/unbrowser-fin-terminal](https://github.com/protostatis/unbrowser-fin-terminal)
### Overview
This is a **TypeScript terminal UI (TUI) extension** for a financial market terminal, built on the `@earendil-works/pi-*` framework (pi-ai, pi-coding-agent, pi-tui). It provides an interactive, keyboard-driven market dashboard with real-time quotes, technical analysis, research capabilities, and charting — all rendered in the terminal.
### Key Components
#### 1. **Data Types & Configuration**
- **ChartScope**: `"day" | "week" | "month" | "year" | "max"` — configurable timeframes with Yahoo Finance API mappings (range + interval)
- **Quote**: Full quote data model including price, change, day range, volume, market state, time series points, and session classification (pre/regular/post)
- **TechnicalSnapshot**: Technical indicators — SMA20, EMA12/26, RSI14, MACD, momentum, signal scoring
- **Canvas**: A rich research output document with typed blocks (text, metrics, tables, news, bullets, sources, charts)
- **CanvasBlock** union type: 7 block kinds for structured research presentation
#### 2. **MarketTerminal Class** (Main Terminal View)
- Dual-tab interface: Tab 0 = Quote view, Tab 1 = Research Canvas view
- Keyboard-driven navigation with J/K for selection, Enter to open, E to remove from watchlist
- Real-time quote refresh with abort controller support (prevents stale data races)
- Watchlist management with toggle functionality
- Research dispatch with cache decision prompts (use cached vs. refresh)
- Archive browsing (older/newer research snapshots)
- Chart rendering with scope switching (1/2/3/4/5 keys for day/week/month/year/max)
#### 3. **MarketHub Class** (Aggregate Market View)
- Multi-screen navigation: Market → Signals → Events → Watch
- Aggregates quotes, movers, headlines, and watchlist into a unified feed
- Two-column layout rendering on wide terminals (≥84 cols, ≥24 rows)
- Signal story view with scrollable technical analysis narratives
- Catalyst radar for earnings/events
- Research archive browsing for market-level research
#### 4. **Research System**
- `ResearchJob` lifecycle: queued → running → partial → complete/failed/cancelled
- Activity states: seeding → fetching → extracting → synthesizing
- Archive persistence to `.pi/market-research-archive.json`
- Cache-first strategy with user prompt for using cached research
#### 5. **Rendering & UI**
- Terminal-aware layout with `LayoutMetrics` (viewport rows, columns)
- `chartLines()` function for ASCII/Unicode chart rendering in terminal
- `twoColumn()` and `stretchBlocks()` layout helpers
- Themed output using `Theme` with color functions (fg, bg, bold, dim)
- Status bar with loading states and research progress
### Architecture Observations
- **Well-structured type system**: Extensive use of TypeScript union types, discriminated unions, and branded types for type safety
- **Reactive state management**: Uses generation counters and abort controllers to handle stale async operations
- **Modular design**: Clean separation between data fetching, state management, and rendering
- **Research pipeline**: Sophisticated research workflow with caching, archiving, and user-in-the-loop decisions
- **Terminal-first UX**: Optimized for keyboard navigation with minimal mouse dependency
### Dependencies
- `@earendil-works/pi-ai` — AI/StringEnum utilities
- `@earendil-works/pi-coding-agent` — Extension API, command context, theme
- `@earendil-works/pi-tui` — Terminal UI primitives (matchesKey, truncateToWidth, visibleWidth, OverlayHandle)
- `typebox` — Runtime type validation schemas
- `node:fs/promises` — File system for research archive persistence
### Summary
This is a sophisticated, production-quality terminal-based financial market dashboard. It combines real-time market data, technical analysis, AI-powered research, and an interactive TUI into a cohesive extension. The code is well-typed, handles edge cases (stale data, aborted requests, archive navigation), and provides a rich user experience entirely within the terminal.
1navigatehttps://github.com/protostatis/unbrowser-fin-terminal/blob/main/.pi/extensions/market-terminal.ts
2get_text
3navigatehttps://raw.githubusercontent.com/protostatis/unbrowser-fin-terminal/main/.pi/extensions/market-terminal.ts
4get_text
5navigatehttps://github.com/protostatis/unbrowser-fin-terminal/blob/main/.pi/extensions/market-terminal.ts
6navigatehttps://api.github.com/repos/protostatis/unbrowser-fin-terminal/contents/.pi/extensions/market-terminal.ts
7js_evalconst base64 = document.querySelector('pre')?.textContent || document.body.textC
8navigatehttps://raw.githubusercontent.com/protostatis/unbrowser-fin-terminal/main/.pi/extensions/market-terminal.ts
9js_evalfetch('https://raw.githubusercontent.com/protostatis/unbrowser-fin-terminal/main
10js_evalvar xhr = new XMLHttpRequest();
xhr.open('GET', 'https://raw.githubusercontent.c
11js_evalvar xhr = new XMLHttpRequest();
xhr.open('GET', 'https://raw.githubusercontent.c
12js_evalvar xhr = new XMLHttpRequest();
xhr.open('GET', 'https://raw.githubusercontent.c
13js_evalvar xhr = new XMLHttpRequest();
xhr.open('GET', 'https://raw.githubusercontent.c