sequenceDiagram
participant User
participant Frontend as Frontend<br/>(script.js)
participant API as FastAPI<br/>(app.py)
participant RAG as RAGSystem<br/>(rag_system.py)
participant Session as SessionManager<br/>(session_manager.py)
participant AI as AIGenerator<br/>(ai_generator.py)
participant Tools as SearchTools<br/>(search_tools.py)
participant Vector as VectorStore<br/>(vector_store.py)
User->>Frontend: Types query & clicks send
Note over Frontend: sendMessage() at line 45
Frontend->>Frontend: Disable input & show loading
Note over Frontend: Lines 49-60
Frontend->>API: POST /api/query
Note over Frontend: Fetch request at lines 63-72
API->>API: Validate QueryRequest
Note over API: Pydantic model at lines 38-41
API->>RAG: query(query, session_id)
Note over API: Line 66
RAG->>Session: get_conversation_history()
Note over RAG: Lines 118-119
Session-->>RAG: Return chat history
RAG->>AI: generate_response(query, history, tools)
Note over RAG: Lines 122-127
AI->>AI: Call Claude API with tools
Note over AI: Line 80
alt Claude decides to use search tool
AI->>Tools: execute_tool("search_course_content")
Note over AI: Lines 83-84
Tools->>Vector: search(query, filters)
Note over Tools: Lines 66-70
Vector-->>Tools: Return search results
Tools->>Tools: Format results & track sources
Note over Tools: Lines 88-114
Tools-->>AI: Formatted search results
AI->>AI: Generate final response with context
Note over AI: Lines 134-135
end
AI-->>RAG: Generated response
RAG->>Tools: get_last_sources()
Note over RAG: Line 130
Tools-->>RAG: Source list
RAG->>Session: add_exchange(session_id, query, response)
Note over RAG: Line 137
RAG-->>API: (response, sources)
API->>API: Build QueryResponse
Note over API: Lines 68-72
API-->>Frontend: JSON response
Frontend->>Frontend: Remove loading message
Note over Frontend: Line 84
Frontend->>Frontend: Convert markdown & display
Note over Frontend: Lines 120, 133-135
Frontend->>Frontend: Show sources in collapsible
Note over Frontend: Lines 124-131
Frontend->>Frontend: Re-enable input
Note over Frontend: Lines 92-94
Frontend-->>User: Display response with sources