# yuchenmao-nus/cs5260_project architecture

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

- Interactive diagram: https://gitdiagram.com/yuchenmao-nus/cs5260_project
- Repository: https://github.com/yuchenmao-nus/cs5260_project

## Overview

1. **Repository shape**
   - SmartFlight is a two-tier app: a **React + Vite frontend** in `frontend/` and a **FastAPI backend** in `backend/`.
   - The backend is the real system boundary for sensitive work: NLU, flight search, ranking, formatting, and any external API keys.

2. **User-facing client**
   - The frontend is a single-page chat-style UI.
   - Key UI pieces are `App.tsx`, `ChatMessage`, `FlightCard`, and `FlightFilters`, which suggest the UX combines conversation with structured flight results and filter controls.
   - Frontend code also contains airline metadata generation (`src/generated/airlines.ts`) and route/airline utilities, so it likely renders normalized flight data rather than raw API payloads.

3. **Backend API entry point**
   - `backend/src/smartflight/main.py` is the FastAPI application entry point.
   - `routers/chat.py` is the main public API surface, so the primary interaction pattern is likely a chat request/response flow rather than a broad REST surface.
   - The frontend talks to this API through `frontend/src/api.ts`.

4. **Core backend layering**
   - The backend is organized into three main layers:
     - `routers/` for HTTP endpoints
     - `services/` for business logic and integration orchestration
     - `agent/` for the AI-driven flight discovery pipeline
   - This separation matters: the router should stay thin, while services and agent modules do the work.

5. **Agent orchestration**
   - `smartflight/agent/agent.py` appears to be the orchestrator for the conversational flight-discovery workflow.
   - Supporting modules like `extract_query.py`, `extract_preference.py`, `search_flights.py`, `filter_flights.py`, and `guardrail.py` indicate a pipeline that:
     1. interprets user intent,
     2. extracts structured search criteria,
     3. retrieves flight options,
     4. filters/ranks them,
     5. applies safety or validity checks before responding.

6. **Flight search subsystem**
   - `agent/fast_flights/` is a distinct subsystem for flight retrieval.
   - It includes:
     - `fetcher.py` for data acquisition,
     - `querying.py` for building searches,
     - `parser.py` for turning raw results into structured objects,
     - `model.py` and `types.py` for internal representations.
   - The presence of `browser_capture.py` suggests the backend may capture or interact with browser-rendered content when direct structured APIs are insufficient.

7. **External integration boundary**
   - `agent/fast_flights/integrations/bright_data.py` shows at least one external data source/infrastructure dependency: **Bright Data**.
   - `integrations/base.py` implies an abstraction over providers, so the system can swap or extend search backends without changing the agent logic.
   - This is the main outbound integration boundary for obtaining flight inventory.

8. **Internal data contract**
   - `agent/fast_flights/pb/flights.proto` and generated protobuf files indicate a strongly typed internal schema for flight results.
   - That suggests the system normalizes heterogeneous search results into a stable intermediate contract before presentation.
   - The UI and higher-level services likely consume simplified models derived from this schema.

9. **Services layer responsibilities**
   - `services/flight_search.py` appears to wrap the agent/search pipeline for application use.
   - `services/nlu.py` likely handles model-backed or heuristic language understanding, probably using OpenAI when enabled.
   - `services/chat_formatting.py`, `recommendation_text.py`, and `progress.py` suggest the backend generates:
     - user-facing conversational text,
     - recommendation summaries,
     - progress/status updates during longer searches.
   - `services/booking.py` exists as a separate boundary, implying booking is modeled as a downstream capability distinct from discovery.

10. **State, safety, and control flow**
   - `agent/state.py` implies the agent maintains conversation/search state across turns.
   - `guardrail.py` suggests the system validates outputs or constrains unsafe/invalid requests before acting on them.
   - `search_result_sample.txt` is probably a fixture/reference for expected search output shape.

11. **Runtime, tooling, and deployment**
   - Backend runtime: **Python 3.11+**, FastAPI, with `uvicorn` for local serving.
   - Frontend runtime: **Node 20+**, React, TypeScript, Vite.
   - The repo includes a `.devcontainer/` and `Dockerfile`, so development and deployment are container-friendly.
   - `backend/.env.example` and the README make it clear that secrets such as `OPENAI_API_KEY` must stay server-side.

12. **Testing and verification**
   - Backend tests live in `backend/tests/`, including `test_chat.py` and `test_fast_flights_querying.py`.
   - This indicates two important test seams: the chat/API contract and the flight-querying engine.
   - There are no obvious frontend tests in the tree, so validation appears backend-heavy.

13. **High-level data flow**
   - The typical flow is:
     - user chats in the frontend,
     - frontend sends the message to the backend chat endpoint,
     - backend agent extracts intent/preferences,
     - flight search service queries external sources,
     - results are normalized, filtered, and formatted,
     - backend returns both conversational text and structured flight cards for the UI.

14. **What to show in an architecture diagram**
   - Draw a clear boundary between **browser UI** and **FastAPI backend**.
   - Inside the backend, separate **router**, **services**, and **agent/search/integration** layers.
   - Show Bright Data and any LLM/OpenAI dependency as external services behind backend-only secrets.
   - Represent protobuf/normalized flight models as the internal contract between search and presentation layers.

Main entry points: [`backend/src/smartflight/agent/agent.py`](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/agent.py) (Agent), [`backend/src/smartflight/routers/chat.py`](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/routers/chat.py) (Chat Router), [`frontend/src/App.tsx`](https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/App.tsx) (App), [`backend/src/smartflight/services/flight_search.py`](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/flight_search.py) (Flight Search)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_ui_app["App<br/>spa shell<br/>[App.tsx]"]
  node_ui_message["Chat Message<br/>chat view<br/>[ChatMessage.tsx]"]
  node_ui_card["Flight Card<br/>results view<br/>[FlightCard.tsx]"]
  node_ui_filters["Flight Filters<br/>controls<br/>[FlightFilters.tsx]"]
  node_ui_api["API Client<br/>http client<br/>[api.ts]"]
  node_ui_models["UI Models<br/>types/utils<br/>[types.ts]"]
end

subgraph group_backend["Backend"]
  node_api_main["FastAPI App<br/>application entry<br/>[main.py]"]
  node_api_chat["Chat Router<br/>http router<br/>[chat.py]"]
  node_svc_nlu["NLU<br/>[nlu.py]"]
  node_svc_search["Flight Search<br/>[flight_search.py]"]
  node_svc_format["Chat Formatting<br/>[chat_formatting.py]"]
  node_svc_progress["Progress<br/>[progress.py]"]
  node_svc_reco["Recommendations"]
  node_svc_booking["Booking<br/>service boundary<br/>[booking.py]"]
  node_agent_core{{"Agent<br/>orchestrator<br/>[agent.py]"}}
  node_agent_state["State<br/>[state.py]"]
  node_agent_extract["Query Parsing<br/>nlp pipeline<br/>[extract_query.py]"]
  node_agent_guard["Guardrail<br/>validation<br/>[guardrail.py]"]
  node_agent_filter["Filter Flights<br/>ranking/filter<br/>[filter_flights.py]"]
end

subgraph group_search["Search Core"]
  node_ff_query["Query Builder<br/>search builder<br/>[querying.py]"]
  node_ff_fetch["Fetcher<br/>retrieval<br/>[fetcher.py]"]
  node_ff_parse["Parser<br/>normalization<br/>[parser.py]"]
  node_ff_proto["Flight Proto<br/>internal contract<br/>[flights.proto]"]
  node_ff_bright(("Bright Data<br/>external provider<br/>[bright_data.py]"))
end

node_ui_app -->|"renders"| node_ui_message
node_ui_app -->|"renders"| node_ui_card
node_ui_app -->|"includes"| node_ui_filters
node_ui_app -->|"calls"| node_ui_api
node_ui_card -->|"uses"| node_ui_models
node_ui_filters -->|"uses"| node_ui_models
node_ui_api -->|"HTTP"| node_api_chat
node_api_main -->|"mounts"| node_api_chat
node_api_chat -->|"delegates"| node_svc_nlu
node_api_chat -->|"delegates"| node_svc_search
node_api_chat -->|"formats"| node_svc_format
node_api_chat -->|"reports"| node_svc_progress
node_svc_search -->|"orchestrates"| node_agent_core
node_svc_nlu -->|"feeds"| node_agent_extract
node_agent_core -->|"maintains"| node_agent_state
node_agent_core -->|"checks"| node_agent_guard
node_agent_core -->|"builds"| node_ff_query
node_agent_core -->|"retrieves"| node_ff_fetch
node_agent_core -->|"filters"| node_agent_filter
node_agent_core -->|"normalizes to"| node_ff_proto
node_ff_query -->|"requests"| node_ff_bright
node_ff_fetch -->|"calls"| node_ff_bright
node_ff_fetch -->|"parses"| node_ff_parse
node_ff_parse -->|"emits"| node_ff_proto
node_svc_format -->|"summarizes"| node_svc_reco
node_svc_search -.->|"handoff"| node_svc_booking

click node_ui_app "https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/App.tsx"
click node_ui_message "https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/components/ChatMessage.tsx"
click node_ui_card "https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/components/FlightCard.tsx"
click node_ui_filters "https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/components/FlightFilters.tsx"
click node_ui_api "https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/api.ts"
click node_ui_models "https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/types.ts"
click node_api_main "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/main.py"
click node_api_chat "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/routers/chat.py"
click node_svc_nlu "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/nlu.py"
click node_svc_search "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/flight_search.py"
click node_svc_format "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/chat_formatting.py"
click node_svc_progress "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/progress.py"
click node_svc_reco "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/recommendation_text.py"
click node_svc_booking "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/booking.py"
click node_agent_core "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/agent.py"
click node_agent_state "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/state.py"
click node_agent_extract "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/extract_query.py"
click node_agent_guard "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/guardrail.py"
click node_agent_filter "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/filter_flights.py"
click node_ff_query "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/querying.py"
click node_ff_fetch "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/fetcher.py"
click node_ff_parse "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/parser.py"
click node_ff_proto "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/pb/flights.proto"
click node_ff_bright "https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/integrations/bright_data.py"

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_ui_app,node_ui_message,node_ui_card,node_ui_filters,node_ui_api,node_ui_models toneBlue
class node_api_main,node_api_chat,node_svc_nlu,node_svc_search,node_svc_format,node_svc_progress,node_svc_reco,node_svc_booking,node_agent_core,node_agent_state,node_agent_extract,node_agent_guard,node_agent_filter toneAmber
class node_ff_query,node_ff_fetch,node_ff_parse,node_ff_proto,node_ff_bright toneMint
```

## Components

### Frontend

React/Vite chat UI and presentation layer

- [App](https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/App.tsx): spa shell · Top-level chat experience and UI composition · `frontend/src/App.tsx`
- [Chat Message](https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/components/ChatMessage.tsx): chat view · Renders conversational messages · `frontend/src/components/ChatMessage.tsx`
- [Flight Card](https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/components/FlightCard.tsx): results view · Displays structured flight offers · `frontend/src/components/FlightCard.tsx`
- [Flight Filters](https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/components/FlightFilters.tsx): controls · Filter controls for narrowing results · `frontend/src/components/FlightFilters.tsx`
- [API Client](https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/api.ts): http client · Frontend entry point for backend chat requests · `frontend/src/api.ts`
- [UI Models](https://github.com/yuchenmao-nus/cs5260_project/blob/main/frontend/src/types.ts): types/utils · Frontend flight normalization helpers and airline metadata · `frontend/src/types.ts`

### Backend

FastAPI API, services, and agent pipeline

- [FastAPI App](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/main.py): application entry · Backend startup and app wiring · `backend/src/smartflight/main.py`
- [Chat Router](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/routers/chat.py): http router · Primary public API surface for chat interactions · `backend/src/smartflight/routers/chat.py`
- [NLU](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/nlu.py): service · Interprets user intent and preferences · `backend/src/smartflight/services/nlu.py`
- [Flight Search](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/flight_search.py): service · Application wrapper around flight discovery pipeline · `backend/src/smartflight/services/flight_search.py`
- [Chat Formatting](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/chat_formatting.py): service · Builds user-facing responses and summaries · `backend/src/smartflight/services/chat_formatting.py`
- [Progress](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/progress.py): service · Produces status updates during longer searches · `backend/src/smartflight/services/progress.py`
- [Recommendations](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/recommendation_text.py): service · Generates recommendation text and ranking summaries · `backend/src/smartflight/services/recommendation_text.py`
- [Booking](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/services/booking.py): service boundary · Downstream booking capability separate from discovery · `backend/src/smartflight/services/booking.py`
- [Agent](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/agent.py): orchestrator · Coordinates the conversational flight-discovery workflow · `backend/src/smartflight/agent/agent.py`
- [State](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/state.py): conversation state · Tracks search and conversation context across turns · `backend/src/smartflight/agent/state.py`
- [Query Parsing](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/extract_query.py): nlp pipeline · Extracts search query and preferences from user input · `backend/src/smartflight/agent/extract_query.py`
- [Guardrail](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/guardrail.py): validation · Constrains invalid or unsafe requests and outputs · `backend/src/smartflight/agent/guardrail.py`
- [Filter Flights](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/filter_flights.py): ranking/filter · Applies filtering and ranking to candidate flights · `backend/src/smartflight/agent/filter_flights.py`

### Search Core

Flight discovery, normalization, and external retrieval

- [Query Builder](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/querying.py): search builder · Constructs flight search requests · `backend/src/smartflight/agent/fast_flights/querying.py`
- [Fetcher](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/fetcher.py): retrieval · Acquires flight data from providers · `backend/src/smartflight/agent/fast_flights/fetcher.py`
- [Parser](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/parser.py): normalization · Turns raw results into structured flights · `backend/src/smartflight/agent/fast_flights/parser.py`
- [Flight Proto](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/pb/flights.proto): internal contract · Stable protobuf schema for normalized flight results · `backend/src/smartflight/agent/fast_flights/pb/flights.proto`
- [Bright Data](https://github.com/yuchenmao-nus/cs5260_project/blob/main/backend/src/smartflight/agent/fast_flights/integrations/bright_data.py): external provider · Outbound flight inventory and retrieval dependency · `backend/src/smartflight/agent/fast_flights/integrations/bright_data.py`

## Connections

- App → Chat Message: renders · Chat UI composes message timeline
- App → Flight Card: renders · Displays flight results as cards
- App → Flight Filters: includes · Provides result filtering controls
- App → API Client: calls · Sends chat requests to backend
- Flight Card → UI Models: uses · Renders normalized flight data
- Flight Filters → UI Models: uses · Operates on structured flight fields
- API Client → Chat Router: HTTP · Frontend talks to chat endpoint
- FastAPI App → Chat Router: mounts · Registers chat router on app startup
- Chat Router → NLU: delegates · Parses and understands user intent
- Chat Router → Flight Search: delegates · Triggers flight discovery workflow
- Chat Router → Chat Formatting: formats · Builds conversational response payloads
- Chat Router → Progress: reports · Streams or emits progress updates
- Flight Search → Agent: orchestrates · Wraps the agent pipeline for app use
- NLU → Query Parsing: feeds · Provides structured intent and preferences
- Agent → State: maintains · Keeps conversation context across turns
- Agent → Guardrail: checks · Validates requests and outputs
- Agent → Query Builder: builds · Creates search queries from parsed intent
- Agent → Fetcher: retrieves · Fetches flight inventory
- Agent → Filter Flights: filters · Ranks and prunes candidate flights
- Agent → Flight Proto: normalizes to · Uses stable internal flight schema
- Query Builder → Bright Data: requests · Sends provider-specific search requests
- Fetcher → Bright Data: calls · Outbound integration for inventory access
- Fetcher → Parser: parses · Converts raw provider responses
- Parser → Flight Proto: emits · Produces normalized protobuf-backed flights
- Chat Formatting → Recommendations: summarizes · Generates recommendation text from results
- Flight Search → Booking: handoff · Keeps booking separate from discovery
