# duy-nguyen-2006/timetable architecture

> Architecture diagram of the GitHub repository duy-nguyen-2006/timetable, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/duy-nguyen-2006/timetable
- Repository: https://github.com/duy-nguyen-2006/timetable

## Overview

1. **Overall shape**
   Tack Timetable is a hybrid **Next.js + React + TypeScript** application with a **browser-side AI orchestration layer**, a **server API layer for LLM/provider access**, a **Python execution/validation layer**, and an optional **Electron desktop shell** for IPC-based local execution.

2. **User-facing application**
   The main UI lives in `src/app/page.tsx` and `src/features/timetable/TimetableApp.tsx`, with settings in `SettingsModal.tsx`. This is where users enter timetable constraints, trigger generation, inspect results, and export the final timetable to Excel.

3. **Domain boundary: timetable feature module**
   Most timetable-specific logic is isolated under `src/features/timetable/`. This includes quick import, constants, UI helpers, and the AI pipeline support code. That folder is the core domain boundary for the product.

4. **Client-side AI orchestrator**
   `src/features/timetable/ai/local-agent.ts` runs the main generation loop in the browser. It coordinates planner/coder/reviewer/repair steps, applies bounded retries, and enforces reject/retry behavior rather than trusting a single model output.

5. **Prompt and model-processing pipeline**
   The AI subsystem is split into focused stages: planning, translation, code generation, repair, skeleton injection, JSON parsing, input compression, and budget guarding. The prompt assets in `prompts/` map to those roles, so the architecture is intentionally multi-stage rather than a single prompt call.

6. **Validation and deterministic checks**
   Before or around execution, the system uses deterministic validators, constraint parsing, AST/syntax checks, and CP-SAT round-trip tests. These layers act as a guardrail so the model’s output is checked against timetable constraints, not just accepted as text.

7. **LLM/provider boundary**
   The browser does not call model providers directly. It goes through server routes such as `POST /api/ai/chat` and `POST /api/provider/test`, which form the secure boundary for provider credentials and chat proxying. This keeps secrets out of the client.

8. **Python execution boundary**
   Generated Python is executed through `src/features/timetable/ai/python-bridge.ts`, which expects an IPC-capable host and now fails explicitly if that bridge is unavailable. This is the key boundary between the TypeScript/React world and the Python solver/executor world.

9. **Python runtime and sandboxing**
   The repository contains a dedicated Python layer in `python/` plus a standalone sandbox in `sandbox/` with Docker and bubblewrap support. That suggests the system can run generated code in a restricted environment, separate from the UI, to reduce risk and isolate execution.

10. **Desktop shell / local host integration**
   The `electron/` folder plus the Windows release workflow indicate a desktop packaging path. In architecture terms, Electron likely provides the local host that enables the Python bridge and IPC-based execution flow described above.

11. **Data flow end-to-end**
   User inputs constraints in the UI → the browser orchestrator compresses and structures them → the app calls the server chat proxy for LLM responses → generated Python is validated and executed through the Python bridge/sandbox → results are rendered back into the timetable table and exported.

12. **Testing and release pipeline**
   There is broad test coverage across route handlers, AI stages, Python executor behavior, sandbox strictness, and integration scripts. GitHub Actions plus the Windows release workflow show this is built as a packaged application with CI checks around both the web app and execution stack.

Main entry points: [`src/features/timetable/ai/local-agent.ts`](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/local-agent.ts) (Local agent), [`src/features/timetable/TimetableApp.tsx`](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/TimetableApp.tsx) (Timetable app), [`.github/workflows/ci.yml`](https://github.com/duy-nguyen-2006/timetable/blob/master/.github/workflows/ci.yml) (CI workflow), [`src/features/timetable/ai/python-bridge.ts`](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/python-bridge.ts) (Python bridge)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g1["Next.js app"]
  node_n1["Main page<br/>Next.js page<br/>[page.tsx]"]
  node_n2["API routes<br/>Server routes"]
end

subgraph group_g2["Timetable feature"]
  node_n3["Timetable app<br/>React shell<br/>[TimetableApp.tsx]"]
  node_n4["Settings modal<br/>UI panel<br/>[SettingsModal.tsx]"]
  node_n5["Domain utils<br/>Feature support"]
end

subgraph group_g3["AI pipeline"]
  node_n6{{"Local agent<br/>Orchestrator<br/>[local-agent.ts]"}}
  node_n7["Planner<br/>Prompt stage<br/>[planner.ts]"]
  node_n8["Translator<br/>Prompt stage<br/>[translator.ts]"]
  node_n9["Coder<br/>Code stage<br/>[coder.ts]"]
  node_n10["Repair<br/>Fixup stage<br/>[repair.ts]"]
  node_n11["Validation<br/>Guards"]
  node_n12["Chat client<br/>LLM client<br/>[chat-client.ts]"]
end

subgraph group_g4["Execution layer"]
  node_n13["Python bridge<br/>IPC bridge<br/>[python-bridge.ts]"]
  node_n14["Python core<br/>Solver runtime"]
  node_n15["Sandbox<br/>Restricted exec"]
end

subgraph group_g5["Desktop shell"]
  node_n16["Electron host<br/>Desktop shell<br/>[main.mjs]"]
  node_n17["Preload<br/>Bridge script<br/>[preload.ts]"]
end

subgraph group_g6["Automation"]
  node_n18["CI workflow<br/>Automation<br/>[ci.yml]"]
  node_n19["Release workflow<br/>Automation"]
end

node_n1 -->|"renders"| node_n3
node_n3 -->|"opens"| node_n4
node_n3 -->|"uses"| node_n5
node_n3 -->|"starts"| node_n6
node_n6 -->|"plans"| node_n7
node_n6 -->|"translates"| node_n8
node_n6 -->|"generates"| node_n9
node_n6 -->|"repairs"| node_n10
node_n6 -->|"checks"| node_n11
node_n6 -->|"calls"| node_n12
node_n12 -->|"proxies"| node_n2
node_n11 -.->|"verifies"| node_n2
node_n9 -->|"executes via"| node_n13
node_n13 -->|"requires"| node_n16
node_n16 -->|"exposes"| node_n17
node_n13 -->|"runs on"| node_n14
node_n14 -->|"isolates"| node_n15
node_n18 -.->|"tests"| node_n2
node_n18 -.->|"tests"| node_n11
node_n18 -.->|"tests"| node_n14
node_n19 -.->|"packages"| node_n16
node_n19 -.->|"bundles"| node_n15

click node_n1 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/app/page.tsx"
click node_n2 "https://github.com/duy-nguyen-2006/timetable/tree/master/src/app/api"
click node_n3 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/TimetableApp.tsx"
click node_n4 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/SettingsModal.tsx"
click node_n5 "https://github.com/duy-nguyen-2006/timetable/tree/master/src/features/timetable"
click node_n6 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/local-agent.ts"
click node_n7 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/planner.ts"
click node_n8 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/translator.ts"
click node_n9 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/coder.ts"
click node_n10 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/repair.ts"
click node_n11 "https://github.com/duy-nguyen-2006/timetable/tree/master/src/features/timetable/ai"
click node_n12 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/chat-client.ts"
click node_n13 "https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/python-bridge.ts"
click node_n14 "https://github.com/duy-nguyen-2006/timetable/tree/master/python"
click node_n15 "https://github.com/duy-nguyen-2006/timetable/tree/master/sandbox"
click node_n16 "https://github.com/duy-nguyen-2006/timetable/blob/master/electron/main.mjs"
click node_n17 "https://github.com/duy-nguyen-2006/timetable/blob/master/electron/preload.ts"
click node_n18 "https://github.com/duy-nguyen-2006/timetable/blob/master/.github/workflows/ci.yml"
click node_n19 "https://github.com/duy-nguyen-2006/timetable/blob/master/.github/workflows/release-windows.yml"

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_n1,node_n2 toneBlue
class node_n3,node_n4,node_n5 toneAmber
class node_n6,node_n7,node_n8,node_n9,node_n10,node_n11,node_n12 toneMint
class node_n13,node_n14,node_n15 toneRose
class node_n16,node_n17 toneIndigo
class node_n18,node_n19 toneTeal
```

## Components

### Next.js app

Browser UI and route handlers

- [Main page](https://github.com/duy-nguyen-2006/timetable/blob/master/src/app/page.tsx): Next.js page · Application entry point and top-level screen · `src/app/page.tsx`
- [API routes](https://github.com/duy-nguyen-2006/timetable/tree/master/src/app/api): Server routes · Chat proxy, provider test, and Python helper endpoints · `src/app/api`

### Timetable feature

Domain-specific timetable UI and orchestration

- [Timetable app](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/TimetableApp.tsx): React shell · Primary timetable workspace and generation flow · `src/features/timetable/TimetableApp.tsx`
- [Settings modal](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/SettingsModal.tsx): UI panel · User settings and generation options · `src/features/timetable/SettingsModal.tsx`
- [Domain utils](https://github.com/duy-nguyen-2006/timetable/tree/master/src/features/timetable): Feature support · Quick import, constants, and shared timetable helpers · `src/features/timetable`

### AI pipeline

Client-side generation, validation, and repair stages

- [Local agent](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/local-agent.ts): Orchestrator · Browser-side loop coordinating planner, coder, reviewer, and repair · `src/features/timetable/ai/local-agent.ts`
- [Planner](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/planner.ts): Prompt stage · Structures constraints and task decomposition · `src/features/timetable/ai/planner.ts`
- [Translator](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/translator.ts): Prompt stage · Converts structured intent into code-oriented form · `src/features/timetable/ai/translator.ts`
- [Coder](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/coder.ts): Code stage · Generates Python solver code from prompts and context · `src/features/timetable/ai/coder.ts`
- [Repair](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/repair.ts): Fixup stage · Retries and repairs invalid model output · `src/features/timetable/ai/repair.ts`
- [Validation](https://github.com/duy-nguyen-2006/timetable/tree/master/src/features/timetable/ai): Guards · Deterministic validator, parser, budget guard, and round-trip checks · `src/features/timetable/ai`
- [Chat client](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/chat-client.ts): LLM client · Browser client for server-backed model requests · `src/features/timetable/ai/chat-client.ts`

### Execution layer

Python execution and sandbox boundary

- [Python bridge](https://github.com/duy-nguyen-2006/timetable/blob/master/src/features/timetable/ai/python-bridge.ts): IPC bridge · Runs generated Python through an IPC-capable host · `src/features/timetable/ai/python-bridge.ts`
- [Python core](https://github.com/duy-nguyen-2006/timetable/tree/master/python): Solver runtime · Execution and validation runtime for generated timetable code · `python`
- [Sandbox](https://github.com/duy-nguyen-2006/timetable/tree/master/sandbox): Restricted exec · Docker and bubblewrap based isolation layer · `sandbox`

### Desktop shell

Electron host and IPC bridge

- [Electron host](https://github.com/duy-nguyen-2006/timetable/blob/master/electron/main.mjs): Desktop shell · Local desktop runtime that enables IPC execution · `electron/main.mjs`
- [Preload](https://github.com/duy-nguyen-2006/timetable/blob/master/electron/preload.ts): Bridge script · Exposes host capabilities to the renderer · `electron/preload.ts`

### Automation

Tests and release workflows

- [CI workflow](https://github.com/duy-nguyen-2006/timetable/blob/master/.github/workflows/ci.yml): Automation · Continuous integration for app and execution stack · `.github/workflows/ci.yml`
- [Release workflow](https://github.com/duy-nguyen-2006/timetable/blob/master/.github/workflows/release-windows.yml): Automation · Windows packaging and release pipeline · `.github/workflows/release-windows.yml`

## Connections

- Main page → Timetable app: renders · The main page mounts the timetable experience
- Timetable app → Settings modal: opens · Workspace uses the settings modal for configuration
- Timetable app → Domain utils: uses · Timetable UI depends on domain helpers and constants
- Timetable app → Local agent: starts · User actions trigger the browser-side generation loop
- Local agent → Planner: plans · Initial constraint structuring and decomposition
- Local agent → Translator: translates · Intermediate representation is prepared for code generation
- Local agent → Coder: generates · Coder produces executable solver code
- Local agent → Repair: repairs · Invalid or rejected outputs are retried and fixed
- Local agent → Validation: checks · Deterministic validation gates each stage
- Local agent → Chat client: calls · Browser agent obtains LLM responses through the client
- Chat client → API routes: proxies · Model requests go through server routes rather than directly to providers
- Validation → API routes: verifies · Route-level checks cover chat and Python helper endpoints
- Coder → Python bridge: executes via · Generated code is handed to the Python bridge
- Python bridge → Electron host: requires · Bridge expects an IPC-capable desktop host
- Electron host → Preload: exposes · Preload script provides renderer access to host APIs
- Python bridge → Python core: runs on · Bridge targets the Python solver/runtime layer
- Python core → Sandbox: isolates · Sandbox constrains execution of generated code
- CI workflow → API routes: tests · CI validates the API boundary and server helpers
- CI workflow → Validation: tests · CI covers AI validation and parser guards
- CI workflow → Python core: tests · CI exercises the Python executor and validator stack
- Release workflow → Electron host: packages · Windows release workflow builds the desktop app
- Release workflow → Sandbox: bundles · Release path includes sandbox execution support
