# hoangnam2464/ai-teacher-copilot architecture

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

- Interactive diagram: https://gitdiagram.com/hoangnam2464/ai-teacher-copilot
- Repository: https://github.com/hoangnam2464/ai-teacher-copilot

## Overview

#### Purpose and topology
AI Teacher Copilot is a K–12 teacher tool for producing cited lesson plans and quizzes from uploaded teaching documents. It is a three-service system: React browser client → Spring Boot public API/gateway → internal FastAPI AI service. Core bootstraps are `frontend/src/main.jsx`, `backend/src/main/java/com/aiteachercopilot/AiTeacherCopilotApplication.java`, and `ai-service/app/main.py`.

#### Client application
The React/Vite frontend owns teacher-facing navigation, authenticated route gating, workspace context, document management, and lesson/quiz screens. It calls the Spring Boot API through a centralized client rather than FastAPI. Key boundaries: `frontend/src/app/routes/AppRoutes.jsx`, `frontend/src/core/services/client.js`, `frontend/src/core/store/authStore.js`, `frontend/src/features/lesson-planner/pages/LessonPlannerPage.jsx`, and `frontend/src/features/quiz-generator/pages/QuizGeneratorPage.jsx`.

#### Identity, tenancy, and gateway
Spring Boot is the externally reachable REST boundary. It authenticates users with JWT, applies Spring Security, and owns application-level user/workspace/document metadata and authorization. Workspace ownership scopes downstream document and generation actions. Key paths: `backend/src/main/java/com/aiteachercopilot/auth/AuthController.java`, `backend/src/main/java/com/aiteachercopilot/auth/JwtAuthenticationFilter.java`, `backend/src/main/java/com/aiteachercopilot/workspace/WorkspaceService.java`, and `backend/src/main/java/com/aiteachercopilot/config/SecurityConfig.java`.

#### Document storage and ingestion
Document uploads are represented and managed in the backend, with binary PDFs/DOCX intended for MinIO object storage. The AI service performs the content pipeline: parse document structure, chunk text, create embeddings, and persist searchable chunk/vector records. This isolates untrusted document content from the public gateway and browser. Key paths: `backend/src/main/java/com/aiteachercopilot/document/DocumentService.java`, `backend/src/main/java/com/aiteachercopilot/config/MinioConfig.java`, `ai-service/app/ingestion/parser.py`, `ai-service/app/ingestion/chunker.py`, and `ai-service/app/ingestion/service.py`.

#### Retrieval and generation
FastAPI exposes internal document, ingestion, retrieval, and generation routes. Retrieval embeds a teacher query and performs metadata-filtered semantic search against pgvector, expected to scope results by workspace/subject/grade. Generation consumes retrieved chunks, wraps them as `<sources>...</sources>` untrusted context, calls an LLM provider, validates structured outputs, and returns citation-capable results to the backend. Key paths: `ai-service/app/api/routes/retrieval.py`, `ai-service/app/retrieval/service.py`, `ai-service/app/api/routes/generation.py`, `ai-service/app/generation/service.py`, and `ai-service/app/generation/schemas.py`.

#### Provider and trust boundary
LLM and embedding access is behind a provider abstraction so Gemini and OpenAI can be selected without coupling ingestion/generation orchestration to a vendor. Prompt construction must retain the explicit sources boundary: uploaded/retrieved text must never become trusted system instructions. Key paths: `ai-service/app/providers/base.py`, `ai-service/app/providers/gemini_provider.py`, `ai-service/app/providers/openai_provider.py`, and `ai-service/app/core/security.py`.

#### Persistence and infrastructure
PostgreSQL 16 with pgvector is the shared relational/vector store; Flyway owns schema evolution, including vector dimensions. MinIO stores original files. Docker Compose provides development/production deployment definitions and container images for all three services. Material paths: `backend/src/main/resources/db/migration/V1__init_schema.sql`, `backend/src/main/resources/db/migration/V2__update_vector_dimension.sql`, `infrastructure/docker-compose/docker-compose.dev.yml`, `infrastructure/docker-compose/docker-compose.prod.yml`, and `infrastructure/docker/ai-service.Dockerfile`.

#### Primary control flow
A teacher logs in, selects/creates a workspace, uploads a source document, and waits for ingestion to make its knowledge base searchable. For a lesson or quiz request, the frontend sends JWT-authenticated REST to Spring Boot; the gateway authorizes and calls FastAPI internally. FastAPI retrieves scoped chunks, generates validated grounded output with source references, and returns it through the gateway for UI review, history/export-oriented generation handling, and citation display. Backend generation integration is centered at `backend/src/main/java/com/aiteachercopilot/generation/GenerationController.java` and `backend/src/main/java/com/aiteachercopilot/generation/GenerationService.java`.

Main entry points: [`ai-service/app/main.py`](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/main.py) (FastAPI AI service), [`backend/src/main/java/com/aiteachercopilot/document/DocumentService.java`](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/document/DocumentService.java) (Document management), [`frontend/src/main.jsx`](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/main.jsx) (React/Vite app), [`backend/src/main/java/com/aiteachercopilot/AiTeacherCopilotApplication.java`](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/AiTeacherCopilotApplication.java) (Spring Boot API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Teacher client"]
  node_react["React/Vite app<br/>browser client<br/>[main.jsx]"]
  node_routes["Protected routes<br/>route layer<br/>[AppRoutes.jsx]"]
  node_auth_store["Auth state<br/>client store<br/>[authStore.js]"]
  node_api_client["Central API client<br/>HTTP client<br/>[client.js]"]
  node_planner["Lesson planner<br/>feature page"]
  node_quiz["Quiz generator<br/>feature page"]
end

subgraph group_gateway["Public gateway"]
  node_spring["Spring Boot API<br/>public REST gateway"]
  node_security["JWT security<br/>auth filter &amp; policy"]
  node_workspace["Workspace authorization<br/>tenant service"]
  node_documents["Document management<br/>document service"]
  node_generation_gateway["Generation integration<br/>generation service"]
end

subgraph group_ai["Internal AI service"]
  node_fastapi["FastAPI AI service<br/>internal API<br/>[main.py]"]
  node_ingestion["Ingestion pipeline<br/>parse, chunk, embed<br/>[service.py]"]
  node_retrieval["Scoped retrieval<br/>semantic search<br/>[service.py]"]
  node_generation["Grounded generation<br/>LLM orchestration<br/>[service.py]"]
  node_providers["LLM providers<br/>provider abstraction<br/>[base.py]"]
end

subgraph group_data["Data &amp; deployment"]
  node_postgres[("PostgreSQL + pgvector<br/>relational/vector store")]
  node_minio[("MinIO<br/>object storage<br/>[MinioConfig.java]")]
  node_compose{{"Docker Compose<br/>deployment"}}
end

node_react -->|"renders"| node_routes
node_routes -->|"gates with"| node_auth_store
node_react -->|"provides"| node_planner
node_react -->|"provides"| node_quiz
node_planner -->|"requests generation"| node_api_client
node_quiz -->|"requests generation"| node_api_client
node_api_client -->|"JWT REST"| node_spring
node_spring -->|"enforces"| node_security
node_security -->|"authorizes tenant actions"| node_workspace
node_workspace -->|"scopes"| node_documents
node_documents -->|"stores originals"| node_minio
node_documents -->|"stores metadata"| node_postgres
node_documents -->|"submits ingestion"| node_fastapi
node_spring -->|"routes requests"| node_generation_gateway
node_generation_gateway -->|"internal call"| node_fastapi
node_fastapi -->|"runs"| node_ingestion
node_ingestion -->|"reads uploads"| node_minio
node_ingestion -->|"persists chunks/vectors"| node_postgres
node_fastapi -->|"runs"| node_retrieval
node_retrieval -->|"semantic search"| node_postgres
node_fastapi -->|"runs"| node_generation
node_generation -->|"gets scoped sources"| node_retrieval
node_generation -->|"uses"| node_providers
node_compose -.->|"deploys"| node_react
node_compose -.->|"deploys"| node_spring
node_compose -.->|"deploys"| node_fastapi

click node_react "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/main.jsx"
click node_routes "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/app/routes/AppRoutes.jsx"
click node_auth_store "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/core/store/authStore.js"
click node_api_client "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/core/services/client.js"
click node_planner "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/features/lesson-planner/pages/LessonPlannerPage.jsx"
click node_quiz "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/features/quiz-generator/pages/QuizGeneratorPage.jsx"
click node_spring "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/AiTeacherCopilotApplication.java"
click node_security "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/auth/JwtAuthenticationFilter.java"
click node_workspace "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/workspace/WorkspaceService.java"
click node_documents "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/document/DocumentService.java"
click node_generation_gateway "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/generation/GenerationService.java"
click node_fastapi "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/main.py"
click node_ingestion "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/ingestion/service.py"
click node_retrieval "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/retrieval/service.py"
click node_generation "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/generation/service.py"
click node_providers "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/providers/base.py"
click node_postgres "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/resources/db/migration/V2__update_vector_dimension.sql"
click node_minio "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/config/MinioConfig.java"
click node_compose "https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/infrastructure/docker-compose/docker-compose.dev.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_react,node_routes,node_auth_store,node_api_client,node_planner,node_quiz toneBlue
class node_spring,node_security,node_workspace,node_documents,node_generation_gateway toneAmber
class node_fastapi,node_ingestion,node_retrieval,node_generation,node_providers toneMint
class node_postgres,node_minio,node_compose toneRose
```

## Components

### Teacher client

- [React/Vite app](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/main.jsx): browser client · Teacher workspace, document, lesson, and quiz UI. · `frontend/src/main.jsx`
- [Protected routes](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/app/routes/AppRoutes.jsx): route layer · `frontend/src/app/routes/AppRoutes.jsx`
- [Auth state](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/core/store/authStore.js): client store · `frontend/src/core/store/authStore.js`
- [Central API client](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/core/services/client.js): HTTP client · Calls Spring Boot rather than the AI service. · `frontend/src/core/services/client.js`
- [Lesson planner](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/features/lesson-planner/pages/LessonPlannerPage.jsx): feature page · `frontend/src/features/lesson-planner/pages/LessonPlannerPage.jsx`
- [Quiz generator](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/frontend/src/features/quiz-generator/pages/QuizGeneratorPage.jsx): feature page · `frontend/src/features/quiz-generator/pages/QuizGeneratorPage.jsx`

### Public gateway

- [Spring Boot API](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/AiTeacherCopilotApplication.java): public REST gateway · External boundary for authenticated teacher actions. · `backend/src/main/java/com/aiteachercopilot/AiTeacherCopilotApplication.java`
- [JWT security](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/auth/JwtAuthenticationFilter.java): auth filter & policy · `backend/src/main/java/com/aiteachercopilot/auth/JwtAuthenticationFilter.java`
- [Workspace authorization](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/workspace/WorkspaceService.java): tenant service · Scopes documents and generation actions to ownership. · `backend/src/main/java/com/aiteachercopilot/workspace/WorkspaceService.java`
- [Document management](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/document/DocumentService.java): document service · `backend/src/main/java/com/aiteachercopilot/document/DocumentService.java`
- [Generation integration](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/generation/GenerationService.java): generation service · Authorizes requests and calls the internal AI service. · `backend/src/main/java/com/aiteachercopilot/generation/GenerationService.java`

### Internal AI service

- [FastAPI AI service](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/main.py): internal API · Internal ingestion, retrieval, and generation boundary. · `ai-service/app/main.py`
- [Ingestion pipeline](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/ingestion/service.py): parse, chunk, embed · Converts untrusted uploads into searchable chunk records. · `ai-service/app/ingestion/service.py`
- [Scoped retrieval](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/retrieval/service.py): semantic search · Filters pgvector results by workspace, subject, and grade. · `ai-service/app/retrieval/service.py`
- [Grounded generation](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/generation/service.py): LLM orchestration · Wraps retrieved text in an untrusted sources boundary and validates output. · `ai-service/app/generation/service.py`
- [LLM providers](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/ai-service/app/providers/base.py): provider abstraction · Selects Gemini or OpenAI without coupling orchestration. · `ai-service/app/providers/base.py`

### Data & deployment

- [PostgreSQL + pgvector](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/resources/db/migration/V2__update_vector_dimension.sql): relational/vector store · Stores application metadata and searchable embeddings. · `backend/src/main/resources/db/migration/V2__update_vector_dimension.sql`
- [MinIO](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/backend/src/main/java/com/aiteachercopilot/config/MinioConfig.java): object storage · Stores original PDF and DOCX files. · `backend/src/main/java/com/aiteachercopilot/config/MinioConfig.java`
- [Docker Compose](https://github.com/hoangnam2464/ai-teacher-copilot/blob/main/infrastructure/docker-compose/docker-compose.dev.yml): deployment · Runs the three services and supporting stores. · `infrastructure/docker-compose/docker-compose.dev.yml`

## Connections

- React/Vite app → Protected routes: renders
- Protected routes → Auth state: gates with
- React/Vite app → Lesson planner: provides
- React/Vite app → Quiz generator: provides
- Lesson planner → Central API client: requests generation
- Quiz generator → Central API client: requests generation
- Central API client → Spring Boot API: JWT REST
- Spring Boot API → JWT security: enforces
- JWT security → Workspace authorization: authorizes tenant actions
- Workspace authorization → Document management: scopes
- Document management → MinIO: stores originals
- Document management → PostgreSQL + pgvector: stores metadata
- Document management → FastAPI AI service: submits ingestion
- Spring Boot API → Generation integration: routes requests
- Generation integration → FastAPI AI service: internal call
- FastAPI AI service → Ingestion pipeline: runs
- Ingestion pipeline → MinIO: reads uploads
- Ingestion pipeline → PostgreSQL + pgvector: persists chunks/vectors
- FastAPI AI service → Scoped retrieval: runs
- Scoped retrieval → PostgreSQL + pgvector: semantic search
- FastAPI AI service → Grounded generation: runs
- Grounded generation → Scoped retrieval: gets scoped sources
- Grounded generation → LLM providers: uses
- Docker Compose → React/Vite app: deploys
- Docker Compose → Spring Boot API: deploys
- Docker Compose → FastAPI AI service: deploys
