# System Architecture ## Architecture Flow Diagram Below is the architectural flow diagram representing the Customer Support Email Agent system, mapping how the Frontend, Backend, AI Pipeline, and External Services interact. ```mermaid graph TD %% Subsystems definition subgraph Frontend ["Frontend (React / Vite) - Hosted on Vercel"] UI_Compose["Compose UI (Compose.tsx)"] UI_Dash["Dashboard UI (Dashboard.tsx)"] UI_Ticket["Detailed View (DetailedView.tsx)"] State["Zustand Global Store"] end subgraph Backend ["Backend (FastAPI) - Hosted on Hugging Face Spaces"] API["FastAPI Routing (/api/v1)"] subgraph Pipeline ["LangGraph Agent Pipeline (SupportPipeline)"] Node1["1. classify_email"] Node2["2. retrieve_context"] Node3["3. generate_response"] Node4["4. check_escalation"] Node5["5. send_email_reply"] Node6["6. check_follow_up"] end FAISS[("FAISS Vector Store\n(docs/knowledge base)")] end subgraph Services ["External Services"] LLM["OpenAI Inference"] Database[("Supabase (PostgreSQL)")] SMTP["SMTP Server (Gmail/Resend)"] end %% --- Core Frontend to Backend APIs --- UI_Compose -- "POST /process-email\n(X-API-Token)" --> API UI_Dash -- "GET /kpis\nGET /emails/recent" --> API UI_Ticket -- "GET /emails/{ticket_id}" --> API %% --- Backend Database Operations --- API -- "Write/Read Tickets Data\n(supabase-py)" --> Database %% --- Pipeline Execution Flow --- API -- "initiates workflow" --> Node1 Node1 --> Node2 Node2 --> Node3 Node3 --> Node4 Node4 -- "if Confidence < 0.7 or Sensitive\n(Escalated)" --> PipelineEnd((End Pipeline)) Node4 -- "if Confident\n(Auto-reply)" --> Node5 Node5 --> Node6 Node6 --> PipelineEnd PipelineEnd -. "return result to API router" .-> API %% --- LLM and Service Interactions --- Node1 -. "LLM Prompt (JSON)" .-> LLM Node2 -. "fetch similar docs" .-> FAISS Node2 -. "embed query" .-> LLM Node3 -. "draft reasoning" .-> LLM Node4 -. "evaluate draft" .-> LLM Node5 -. "dispatch SMTP" .-> SMTP Node6 -. "LLM Prompt" .-> LLM ``` ## Key Components 1. **Frontend**: A React SPA built with Vite. It interacts with the backend strictly through REST APIs and manages state via `zustand`. It leverages `recharts` for the dashboard and `reactflow` for pipeline visualization. 2. **Backend**: A FastAPI Python backend acting as the orchestrator. It manages the authentication (`X-API-Token`) and database persistence (`supabase-py`). 3. **LangGraph Pipeline**: The core AI logic resides here as a Directed Acyclic Graph (DAG) state machine. It handles classification, context retrieval (RAG via FAISS), response generation, autonomous escalation scoring, and conditionally dispatching outbound SMTP emails. 4. **External Services**: - **OpenAI**: Provides the embedding (`text-embedding-3-small`) and LLM text generation capabilities for the intelligent nodes. - **Supabase**: Serves as the primary operational database, storing tickets, statuses, extracted categorization, and execution metadata.