# mevikrampawar/chesser architecture

> Architecture diagram of the GitHub repository mevikrampawar/chesser, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mevikrampawar/chesser
- Repository: https://github.com/mevikrampawar/chesser

## Overview

#### Purpose and Runtime
Chesser is a browser-only chess opening trainer. A React/TypeScript SPA lets users play a position, identifies the opening with a locally executed WebLLM model, retrieves aggregate continuation statistics from Lichess, and optionally persists favorites in Firebase. The application starts at `src/main.tsx`, renders the root coordinator in `src/App.tsx`, and is built/served through Vite (`vite.config.ts`).

#### Application Composition
`src/App.tsx` is the primary composition boundary: it connects game state, model readiness, opening analysis, authentication, and the display panels. The interactive workflow is represented by focused UI components:
- Board input: `src/components/ChessBoard.tsx`
- Position/move context: `src/components/MoveHistory.tsx`
- Analysis presentation: `src/components/OpeningPanel.tsx`, `src/components/StatsBar.tsx`, `src/components/TopMoves.tsx`
- Account-specific saved records: `src/components/SavedOpenings.tsx`
- AI initialization state: `src/components/SetupScreen.tsx`

#### Chess Game State
`src/hooks/useChessGame.ts` owns chess gameplay state and move progression, backed by chess-specific dependencies described in `package.json`. It is the source of the move sequence/position passed downstream for analysis and displayed by `ChessBoard` and `MoveHistory`. This isolates rules-valid move handling from presentation and from remote/AI enrichment.

#### Opening Analysis Pipeline
`src/hooks/useOpeningStats.ts` coordinates analysis triggered by game changes. Its service boundary is `src/services/opening.ts`, which merges two independent results:
- Local opening recognition/coaching through `src/services/llm.ts`
- Lichess opening-explorer statistics and popular continuations through `src/services/lichess.ts`

The resulting typed opening data is consumed by the analysis UI rather than allowing components to call external sources directly. Shared domain contracts reside in `src/types/index.ts`.

#### In-Browser AI Boundary
`src/hooks/useLLM.ts` manages WebLLM engine/model lifecycle, including load progress and availability for the setup screen. `src/services/llm.ts` encapsulates inference against Phi-3.5 Mini via WebGPU; prompt construction is separated in `src/utils/prompts.ts`. Model execution and caching remain client-side, so no application backend is required for identification or coaching. Browsers without practical WebGPU support are a material runtime constraint.

#### External Data and Persistence
Lichess is an unauthenticated external data boundary accessed through `src/services/lichess.ts`; requests use the current game state to obtain community game statistics. Firebase configuration and Auth initialization are centralized in `src/services/firebase.ts`. `src/hooks/useAuth.ts` exposes Google sign-in state to the app, while `src/services/firestore.ts` performs cloud save/read operations for user-owned opening records surfaced in `SavedOpenings.tsx`.

#### Deployment and Offline Behavior
The client is hosted by Firebase Hosting (`firebase.json`). Firebase environment values are supplied through `.env` based on `.env.example`. `public/sw.js` provides service-worker support, complementing browser caching of the AI model for repeat/offline use. The deployed system therefore depends on Firebase for identity/storage and Lichess for live statistics, while core chess interaction and LLM inference execute locally.

Main entry points: [`src/App.tsx`](https://github.com/mevikrampawar/chesser/blob/main/src/App.tsx) (App coordinator), [`src/services/llm.ts`](https://github.com/mevikrampawar/chesser/blob/main/src/services/llm.ts) (Local Phi-3.5 inference), [`src/hooks/useChessGame.ts`](https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useChessGame.ts) (Chess game state), [`src/hooks/useOpeningStats.ts`](https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useOpeningStats.ts) (Opening stats coordinator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser SPA"]
  node_vite{{"Vite build/serve<br/>build tool<br/>[vite.config.ts]"}}
  node_entry["Application entry<br/>React entry<br/>[main.tsx]"]
  node_app["App coordinator<br/>root composition<br/>[App.tsx]"]
  node_game["Chess game state<br/>game hook<br/>[useChessGame.ts]"]
  node_board["Chess board<br/>input UI<br/>[ChessBoard.tsx]"]
  node_history["Move history<br/>context UI<br/>[MoveHistory.tsx]"]
  node_opening_ui["Opening analysis panels<br/>analysis UI<br/>[OpeningPanel.tsx]"]
  node_saved_ui["Saved openings<br/>account UI<br/>[SavedOpenings.tsx]"]
  node_setup["AI setup screen<br/>readiness UI<br/>[SetupScreen.tsx]"]
  node_service_worker["Service worker<br/>offline cache<br/>[sw.js]"]
end

subgraph group_analysis["Analysis pipeline"]
  node_opening_hook["Opening stats coordinator<br/>analysis hook<br/>[useOpeningStats.ts]"]
  node_opening_service["Opening merger<br/>domain service<br/>[opening.ts]"]
  node_llm_hook["LLM lifecycle<br/>model hook<br/>[useLLM.ts]"]
  node_llm["Local Phi-3.5 inference<br/>WebLLM service<br/>[llm.ts]"]
  node_prompts["Prompt builder<br/>AI utility<br/>[prompts.ts]"]
  node_contracts["Opening contracts<br/>shared types<br/>[index.ts]"]
end

subgraph group_firebase["Firebase services"]
  node_auth["Authentication state<br/>auth hook<br/>[useAuth.ts]"]
  node_firebase_init["Firebase initialization<br/>Firebase config<br/>[firebase.ts]"]
  node_firestore[("Saved opening records<br/>Firestore service<br/>[firestore.ts]")]
  node_hosting{{"Firebase Hosting<br/>deployment<br/>[firebase.json]"}}
end

subgraph group_external["External runtime"]
  node_lichess["Lichess explorer<br/>statistics service<br/>[lichess.ts]"]
  node_webgpu["WebGPU browser runtime<br/>browser capability"]
end

node_vite -->|"builds and serves"| node_entry
node_entry -->|"renders"| node_app
node_app -->|"uses"| node_game
node_game -->|"position and moves"| node_board
node_game -->|"move sequence"| node_history
node_app -->|"requests analysis"| node_opening_hook
node_game -->|"position changes"| node_opening_hook
node_opening_hook -->|"coordinates"| node_opening_service
node_opening_service -->|"local recognition"| node_llm
node_opening_service -->|"community statistics"| node_lichess
node_opening_hook -->|"typed results"| node_opening_ui
node_contracts -.->|"domain types"| node_opening_service
node_llm_hook -->|"engine lifecycle"| node_llm
node_app -->|"model readiness"| node_llm_hook
node_llm_hook -->|"load state"| node_setup
node_llm -->|"builds prompts"| node_prompts
node_llm -->|"executes on"| node_webgpu
node_app -->|"sign-in state"| node_auth
node_auth -->|"initializes Auth"| node_firebase_init
node_saved_ui -->|"save and read"| node_firestore
node_firestore -->|"uses Firebase"| node_firebase_init
node_app -->|"renders"| node_saved_ui
node_hosting -->|"hosts SPA"| node_entry
node_service_worker -.->|"supports cached reuse"| node_llm

click node_vite "https://github.com/mevikrampawar/chesser/blob/main/vite.config.ts"
click node_entry "https://github.com/mevikrampawar/chesser/blob/main/src/main.tsx"
click node_app "https://github.com/mevikrampawar/chesser/blob/main/src/App.tsx"
click node_game "https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useChessGame.ts"
click node_board "https://github.com/mevikrampawar/chesser/blob/main/src/components/ChessBoard.tsx"
click node_history "https://github.com/mevikrampawar/chesser/blob/main/src/components/MoveHistory.tsx"
click node_opening_ui "https://github.com/mevikrampawar/chesser/blob/main/src/components/OpeningPanel.tsx"
click node_saved_ui "https://github.com/mevikrampawar/chesser/blob/main/src/components/SavedOpenings.tsx"
click node_setup "https://github.com/mevikrampawar/chesser/blob/main/src/components/SetupScreen.tsx"
click node_opening_hook "https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useOpeningStats.ts"
click node_opening_service "https://github.com/mevikrampawar/chesser/blob/main/src/services/opening.ts"
click node_llm_hook "https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useLLM.ts"
click node_llm "https://github.com/mevikrampawar/chesser/blob/main/src/services/llm.ts"
click node_prompts "https://github.com/mevikrampawar/chesser/blob/main/src/utils/prompts.ts"
click node_lichess "https://github.com/mevikrampawar/chesser/blob/main/src/services/lichess.ts"
click node_auth "https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useAuth.ts"
click node_firebase_init "https://github.com/mevikrampawar/chesser/blob/main/src/services/firebase.ts"
click node_firestore "https://github.com/mevikrampawar/chesser/blob/main/src/services/firestore.ts"
click node_contracts "https://github.com/mevikrampawar/chesser/blob/main/src/types/index.ts"
click node_hosting "https://github.com/mevikrampawar/chesser/blob/main/firebase.json"
click node_service_worker "https://github.com/mevikrampawar/chesser/blob/main/public/sw.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_vite,node_entry,node_app,node_game,node_board,node_history,node_opening_ui,node_saved_ui,node_setup,node_service_worker toneBlue
class node_opening_hook,node_opening_service,node_llm_hook,node_llm,node_prompts,node_contracts toneAmber
class node_auth,node_firebase_init,node_firestore,node_hosting toneMint
class node_lichess,node_webgpu toneRose
```

## Components

### Browser SPA

React application running entirely in the browser.

- [Vite build/serve](https://github.com/mevikrampawar/chesser/blob/main/vite.config.ts): build tool · `vite.config.ts`
- [Application entry](https://github.com/mevikrampawar/chesser/blob/main/src/main.tsx): React entry · `src/main.tsx`
- [App coordinator](https://github.com/mevikrampawar/chesser/blob/main/src/App.tsx): root composition · Connects game, analysis, model, auth, and panels. · `src/App.tsx`
- [Chess game state](https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useChessGame.ts): game hook · Owns legal moves, position, and progression. · `src/hooks/useChessGame.ts`
- [Chess board](https://github.com/mevikrampawar/chesser/blob/main/src/components/ChessBoard.tsx): input UI · `src/components/ChessBoard.tsx`
- [Move history](https://github.com/mevikrampawar/chesser/blob/main/src/components/MoveHistory.tsx): context UI · `src/components/MoveHistory.tsx`
- [Opening analysis panels](https://github.com/mevikrampawar/chesser/blob/main/src/components/OpeningPanel.tsx): analysis UI · OpeningPanel, StatsBar, and TopMoves present results. · `src/components/OpeningPanel.tsx`
- [Saved openings](https://github.com/mevikrampawar/chesser/blob/main/src/components/SavedOpenings.tsx): account UI · `src/components/SavedOpenings.tsx`
- [AI setup screen](https://github.com/mevikrampawar/chesser/blob/main/src/components/SetupScreen.tsx): readiness UI · `src/components/SetupScreen.tsx`
- [Service worker](https://github.com/mevikrampawar/chesser/blob/main/public/sw.js): offline cache · Supports repeat and offline browser use. · `public/sw.js`

### Analysis pipeline

Local AI and Lichess enrichment behind typed services.

- [Opening stats coordinator](https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useOpeningStats.ts): analysis hook · Triggers enrichment when the game changes. · `src/hooks/useOpeningStats.ts`
- [Opening merger](https://github.com/mevikrampawar/chesser/blob/main/src/services/opening.ts): domain service · Combines local recognition with Lichess statistics. · `src/services/opening.ts`
- [LLM lifecycle](https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useLLM.ts): model hook · Manages WebLLM readiness and load progress. · `src/hooks/useLLM.ts`
- [Local Phi-3.5 inference](https://github.com/mevikrampawar/chesser/blob/main/src/services/llm.ts): WebLLM service · Runs recognition and coaching client-side. · `src/services/llm.ts`
- [Prompt builder](https://github.com/mevikrampawar/chesser/blob/main/src/utils/prompts.ts): AI utility · `src/utils/prompts.ts`
- [Opening contracts](https://github.com/mevikrampawar/chesser/blob/main/src/types/index.ts): shared types · `src/types/index.ts`

### Firebase services

Optional identity, persistence, and hosting.

- [Authentication state](https://github.com/mevikrampawar/chesser/blob/main/src/hooks/useAuth.ts): auth hook · Exposes Google sign-in state. · `src/hooks/useAuth.ts`
- [Firebase initialization](https://github.com/mevikrampawar/chesser/blob/main/src/services/firebase.ts): Firebase config · `src/services/firebase.ts`
- [Saved opening records](https://github.com/mevikrampawar/chesser/blob/main/src/services/firestore.ts): Firestore service · Reads and saves user-owned opening records. · `src/services/firestore.ts`
- [Firebase Hosting](https://github.com/mevikrampawar/chesser/blob/main/firebase.json): deployment · `firebase.json`

### External runtime

Browser capabilities and third-party data source.

- [Lichess explorer](https://github.com/mevikrampawar/chesser/blob/main/src/services/lichess.ts): statistics service · `src/services/lichess.ts`
- **WebGPU browser runtime**: browser capability · Required for practical local model execution.

## Connections

- Vite build/serve → Application entry: builds and serves
- Application entry → App coordinator: renders
- App coordinator → Chess game state: uses
- Chess game state → Chess board: position and moves
- Chess game state → Move history: move sequence
- App coordinator → Opening stats coordinator: requests analysis
- Chess game state → Opening stats coordinator: position changes
- Opening stats coordinator → Opening merger: coordinates
- Opening merger → Local Phi-3.5 inference: local recognition
- Opening merger → Lichess explorer: community statistics
- Opening stats coordinator → Opening analysis panels: typed results
- Opening contracts → Opening merger: domain types
- LLM lifecycle → Local Phi-3.5 inference: engine lifecycle
- App coordinator → LLM lifecycle: model readiness
- LLM lifecycle → AI setup screen: load state
- Local Phi-3.5 inference → Prompt builder: builds prompts
- Local Phi-3.5 inference → WebGPU browser runtime: executes on
- App coordinator → Authentication state: sign-in state
- Authentication state → Firebase initialization: initializes Auth
- Saved openings → Saved opening records: save and read
- Saved opening records → Firebase initialization: uses Firebase
- App coordinator → Saved openings: renders
- Firebase Hosting → Application entry: hosts SPA
- Service worker → Local Phi-3.5 inference: supports cached reuse
