# owb27/grs architecture

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

- Interactive diagram: https://gitdiagram.com/owb27/grs
- Repository: https://github.com/owb27/grs

## Overview

1. **Repository shape**
   - This is a two-tier monorepo: a React/Vite frontend and a FastAPI backend.
   - There is no shared runtime package between them; the boundary is a JSON HTTP API.

2. **User-facing frontend**
   - The frontend lives under `frontend/src` and is organized by page and feature area: home, quiz, result, methodology, plus shared layout and UI components.
   - `App.jsx` and `main.jsx` wire routing and app bootstrap; `i18n.js` and locale files provide bilingual UI (`en`, `zh`).

3. **Frontend state and API access**
   - `hooks/useQuizFlow.js` drives the quiz interaction and result progression.
   - `lib/api.js` is the main client boundary to the backend.
   - Result UI components include debug panels and recommendation cards, so the frontend can render both normal recommendations and optional diagnostic detail.

4. **Backend HTTP surface**
   - The API is split into two route modules: `app/api/routes/questions.py` and `app/api/routes/recommend.py`.
   - `GET /questions` serves quiz content from the database.
   - `POST /recommend` accepts quiz answers and returns ranked game recommendations.

5. **Recommendation pipeline**
   - The core flow is rule-based first: validate answers, build a user preference profile, score games, and select top candidates.
   - The backend then optionally reranks those candidates with an LLM and generates better recommendation reasons.
   - If the LLM path is disabled or fails validation/timeouts, the system falls back to the rule-based ranking.

6. **Service layer responsibilities**
   - `answer_validation_service.py` enforces input correctness.
   - `profile_service.py` turns user answers into a preference profile.
   - `question_service.py` handles question retrieval/business logic.
   - `recommendation_service.py`, `rerank_service.py`, and `reason_service.py` coordinate scoring, candidate selection, reranking, and explanation generation.

7. **LLM subsystem**
   - The LLM-specific code is isolated under `backend/app/llm`.
   - It includes prompt templates, client wrappers for reranking and reasoning, an in-memory cache, and quality checks for model output.
   - `evals/` contains offline evaluation cases and results, showing that prompt/model behavior is treated as a tunable subsystem rather than embedded ad hoc in the main pipeline.

8. **Persistence and domain model**
   - The backend uses SQLModel/SQLAlchemy with PostgreSQL.
   - `db/models.py`, `db/session.py`, and `db/engine.py` define the persistence layer.
   - Alembic migrations under `backend/alembic/versions` evolve the schema for questions, games/tags, recommendation tables, and uniqueness constraints.

9. **Seed and data management**
   - Business data is managed through JSON seed files in `backend/data_pipeline/seeds`.
   - Import and upsert scripts under `backend/app/db` populate or refresh games, tags, questions, and tag weights.
   - This indicates the system is designed around curated offline data rather than live scraping or user-generated content.

10. **Configuration and external services**
   - Environment settings in `app/core/settings.py` and `backend/.env.example` control database access, LLM behavior, caching, diagnostics, and LangSmith tracing.
   - The main external dependencies that affect architecture are PostgreSQL for storage and an LLM provider for optional reranking/reason generation.

11. **Testing and CI**
   - Backend tests cover the recommendation and questions endpoints, including invalid-answer handling and the non-LLM fallback path.
   - GitHub Actions CI runs frontend install/build and backend pytest, so both the UI and API are first-class build targets.

12. **Architecture to draw**
   - The main diagram should show: browser UI → FastAPI API → service layer → PostgreSQL and seed-backed domain data, with an optional LLM branch used only after rule-based candidate selection.
   - The LLM branch should be drawn as a downstream enhancer, not as the primary recommender.

Main entry points: [`frontend/src/App.jsx`](https://github.com/owb27/grs/blob/main/frontend/src/App.jsx) (App Shell), [`backend/app/db`](https://github.com/owb27/grs/tree/main/backend/app/db) (PostgreSQL), [`frontend/src/pages/ResultPage.jsx`](https://github.com/owb27/grs/blob/main/frontend/src/pages/ResultPage.jsx) (Result), [`backend/app/main.py`](https://github.com/owb27/grs/blob/main/backend/app/main.py) (FastAPI API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_browser(("Browser UI<br/>react app"))
  node_app_boot["App Shell<br/>routing/bootstrap<br/>[App.jsx]"]
  node_home_page["Home<br/>page<br/>[HomePage.jsx]"]
  node_quiz_page["Quiz<br/>page<br/>[QuizPage.jsx]"]
  node_result_page["Result<br/>page<br/>[ResultPage.jsx]"]
  node_api_client["API Client<br/>http client<br/>[api.js]"]
  node_quiz_flow["Quiz Flow<br/>state hook<br/>[useQuizFlow.js]"]
end

subgraph group_backend["Backend"]
  node_api["FastAPI API<br/>http server<br/>[main.py]"]
  node_questions_route["Questions Route<br/>[questions.py]"]
  node_recommend_route["Recommend Route<br/>[recommend.py]"]
  node_validation["Validation"]
  node_profile["Profile Builder<br/>[profile_service.py]"]
  node_recommendation["Ranker"]
  node_rerank["Rerank<br/>[rerank_service.py]"]
  node_reason["Reasons<br/>[reason_service.py]"]
  node_llm{{"LLM Layer<br/>enhancer"}}
end

subgraph group_data["Data"]
  node_db[("PostgreSQL<br/>database")]
  node_seed_data["Seed Data<br/>json seeds"]
  node_migrations["Migrations<br/>schema evolution"]
end

node_browser -->|"rendered by"| node_app_boot
node_app_boot -->|"routes to"| node_home_page
node_app_boot -->|"routes to"| node_quiz_page
node_app_boot -->|"routes to"| node_result_page
node_quiz_page -->|"uses"| node_quiz_flow
node_quiz_flow -->|"calls"| node_api_client
node_api_client -->|"HTTP JSON"| node_api
node_api -->|"mounts"| node_questions_route
node_api -->|"mounts"| node_recommend_route
node_questions_route -->|"reads"| node_db
node_questions_route -.->|"refreshes from"| node_seed_data
node_recommend_route -->|"validates"| node_validation
node_recommend_route -->|"builds"| node_profile
node_profile -->|"feeds"| node_recommendation
node_recommendation -->|"scores from"| node_db
node_recommendation -->|"candidates to"| node_rerank
node_rerank -.->|"optional use"| node_llm
node_rerank -->|"improves"| node_reason
node_reason -.->|"optional use"| node_llm
node_db -->|"initialized by"| node_seed_data
node_migrations -->|"evolves"| node_db
node_result_page -->|"reads results"| node_api_client
node_result_page -.->|"shows diagnostics"| node_llm

click node_browser "https://github.com/owb27/grs/tree/main/frontend/src"
click node_app_boot "https://github.com/owb27/grs/blob/main/frontend/src/App.jsx"
click node_home_page "https://github.com/owb27/grs/blob/main/frontend/src/pages/HomePage.jsx"
click node_quiz_page "https://github.com/owb27/grs/blob/main/frontend/src/pages/QuizPage.jsx"
click node_result_page "https://github.com/owb27/grs/blob/main/frontend/src/pages/ResultPage.jsx"
click node_api_client "https://github.com/owb27/grs/blob/main/frontend/src/lib/api.js"
click node_quiz_flow "https://github.com/owb27/grs/blob/main/frontend/src/hooks/useQuizFlow.js"
click node_api "https://github.com/owb27/grs/blob/main/backend/app/main.py"
click node_questions_route "https://github.com/owb27/grs/blob/main/backend/app/api/routes/questions.py"
click node_recommend_route "https://github.com/owb27/grs/blob/main/backend/app/api/routes/recommend.py"
click node_validation "https://github.com/owb27/grs/blob/main/backend/app/services/answer_validation_service.py"
click node_profile "https://github.com/owb27/grs/blob/main/backend/app/services/profile_service.py"
click node_recommendation "https://github.com/owb27/grs/blob/main/backend/app/services/recommendation_service.py"
click node_rerank "https://github.com/owb27/grs/blob/main/backend/app/services/rerank_service.py"
click node_reason "https://github.com/owb27/grs/blob/main/backend/app/services/reason_service.py"
click node_llm "https://github.com/owb27/grs/tree/main/backend/app/llm"
click node_db "https://github.com/owb27/grs/tree/main/backend/app/db"
click node_seed_data "https://github.com/owb27/grs/tree/main/backend/data_pipeline/seeds"
click node_migrations "https://github.com/owb27/grs/tree/main/backend/alembic/versions"

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_browser,node_app_boot,node_home_page,node_quiz_page,node_result_page,node_api_client,node_quiz_flow toneBlue
class node_api,node_questions_route,node_recommend_route,node_validation,node_profile,node_recommendation,node_rerank,node_reason,node_llm toneAmber
class node_db,node_seed_data,node_migrations toneMint
```

## Components

### Frontend

React/Vite browser app for the quiz flow and results.

- [Browser UI](https://github.com/owb27/grs/tree/main/frontend/src): react app · User-facing quiz, methodology, and result experience. · `frontend/src`
- [App Shell](https://github.com/owb27/grs/blob/main/frontend/src/App.jsx): routing/bootstrap · Wires app startup, routing, layout, and localization. · `frontend/src/App.jsx`
- [Home](https://github.com/owb27/grs/blob/main/frontend/src/pages/HomePage.jsx): page · Landing page and pipeline overview. · `frontend/src/pages/HomePage.jsx`
- [Quiz](https://github.com/owb27/grs/blob/main/frontend/src/pages/QuizPage.jsx): page · Question flow, progress, and answer submission UI. · `frontend/src/pages/QuizPage.jsx`
- [Result](https://github.com/owb27/grs/blob/main/frontend/src/pages/ResultPage.jsx): page · Recommendation display and diagnostic panels. · `frontend/src/pages/ResultPage.jsx`
- [API Client](https://github.com/owb27/grs/blob/main/frontend/src/lib/api.js): http client · Frontend boundary for backend requests. · `frontend/src/lib/api.js`
- [Quiz Flow](https://github.com/owb27/grs/blob/main/frontend/src/hooks/useQuizFlow.js): state hook · Coordinates answering, progression, and result loading. · `frontend/src/hooks/useQuizFlow.js`

### Backend

FastAPI API, services, persistence, and optional LLM enhancement.

- [FastAPI API](https://github.com/owb27/grs/blob/main/backend/app/main.py): http server · HTTP surface exposing questions and recommendations. · `backend/app/main.py`
- [Questions Route](https://github.com/owb27/grs/blob/main/backend/app/api/routes/questions.py): route · Serves quiz questions over HTTP. · `backend/app/api/routes/questions.py`
- [Recommend Route](https://github.com/owb27/grs/blob/main/backend/app/api/routes/recommend.py): route · Accepts answers and returns ranked recommendations. · `backend/app/api/routes/recommend.py`
- [Validation](https://github.com/owb27/grs/blob/main/backend/app/services/answer_validation_service.py): service · Checks answer correctness and request shape. · `backend/app/services/answer_validation_service.py`
- [Profile Builder](https://github.com/owb27/grs/blob/main/backend/app/services/profile_service.py): service · Converts quiz answers into a preference profile. · `backend/app/services/profile_service.py`
- [Ranker](https://github.com/owb27/grs/blob/main/backend/app/services/recommendation_service.py): service · Scores games and selects top candidates. · `backend/app/services/recommendation_service.py`
- [Rerank](https://github.com/owb27/grs/blob/main/backend/app/services/rerank_service.py): service · Optionally refines candidate ordering with LLM support. · `backend/app/services/rerank_service.py`
- [Reasons](https://github.com/owb27/grs/blob/main/backend/app/services/reason_service.py): service · Generates user-facing recommendation explanations. · `backend/app/services/reason_service.py`
- [LLM Layer](https://github.com/owb27/grs/tree/main/backend/app/llm): enhancer · Prompting, clients, cache, and quality checks for optional model use. · `backend/app/llm`

### Data

Seed-backed domain data and database schema evolution.

- [PostgreSQL](https://github.com/owb27/grs/tree/main/backend/app/db): database · Persistent store for questions, games, tags, and recommendations. · `backend/app/db`
- [Seed Data](https://github.com/owb27/grs/tree/main/backend/data_pipeline/seeds): json seeds · Curated offline domain data used to populate and refresh the database. · `backend/data_pipeline/seeds`
- [Migrations](https://github.com/owb27/grs/tree/main/backend/alembic/versions): schema evolution · Alembic versions for questions, games/tags, and recommendation tables. · `backend/alembic/versions`

## Connections

- Browser UI → App Shell: rendered by · The browser experience is composed by app bootstrap and routing.
- App Shell → Home: routes to · Top-level navigation reaches the landing page.
- App Shell → Quiz: routes to · Routing exposes the quiz flow page.
- App Shell → Result: routes to · Routing exposes the result page.
- Quiz → Quiz Flow: uses · The quiz UI delegates interaction state to the flow hook.
- Quiz Flow → API Client: calls · The hook loads questions and submits answers through the API client.
- API Client → FastAPI API: HTTP JSON · Frontend communicates with the backend over the JSON API.
- FastAPI API → Questions Route: mounts · The FastAPI app exposes the questions route module.
- FastAPI API → Recommend Route: mounts · The FastAPI app exposes the recommendation route module.
- Questions Route → PostgreSQL: reads · Question retrieval is backed by persisted domain data.
- Questions Route → Seed Data: refreshes from · Question content is managed through curated seed files and import scripts.
- Recommend Route → Validation: validates · Incoming answers are checked before scoring.
- Recommend Route → Profile Builder: builds · Validated answers are turned into a preference profile.
- Profile Builder → Ranker: feeds · The profile is the input to rule-based scoring.
- Ranker → PostgreSQL: scores from · Ranking uses stored game, tag, and weight data.
- Ranker → Rerank: candidates to · Top candidates can be sent downstream for optional reranking.
- Rerank → LLM Layer: optional use · LLM reranking is an enhancer, not the primary path.
- Rerank → Reasons: improves · Reranking can also support better explanations.
- Reasons → LLM Layer: optional use · Reason generation can be assisted by the LLM layer.
- PostgreSQL → Seed Data: initialized by · Database contents are populated from offline seed JSON.
- Migrations → PostgreSQL: evolves · Alembic migrations maintain the schema over time.
- Result → API Client: reads results · Result UI loads recommendation payloads from the backend.
- Result → LLM Layer: shows diagnostics · Debug panels may expose optional model-related details.
