Goal: Real-time messaging with latency control. Flow:
- [UI]
ChatInterface.tsx(User Input) (handleSendMessagefunction) ↓ - [Client]
api.ts(sendMessagefunction) ↓ POST /api/chat/send_message/ - [View]
views.py(ChatViewSet) -> Save User Message to DB ├── Step A: Session Handling │ Checkchat_session_id. If null ->ChatSession.objects.create()│ (Initialize with User Persona, World Time, etc.) │ └── Step B: User PersistenceMessage.objects.create(role='user', content=...)[DB Write 1] Save User Message to PostgreSQL. ↓ (Synchronous Call) - [Engine]
tasks.py(generate_ai_response) ↓ - [LLM] Google Gemini API (Context Window Injection) ↓
- [DB]
models.py(Save Assistant Response) ↓ - [Client State]
ChatInterface.tsx(Handle Response) ├── Update Redux:dispatch(addMessage(ai_message))│ └── Bind Session:setChatSessionId(response.chat_session_id)(Crucial: Transitions state from "New Chat" -> "Existing Session") ↓ - [Cross-Component Sync]
onSessionUpdate()callback ↓ Triggerspage.tsx↓ Refreshes Sidebar History List (Shows new title/timestamp)
Goal: Extract structured persona from unstructured files (novels/PDFs). Flow:
- [UI]
CreateCharacterForm.tsx(File Drop) ↓ GraphQL Mutation (GENERATE_DRAFT) - [Schema]
graphql/schema.py↓ - [ETL] Gemini 2.5 Flash (Prompt: "Extract JSON") ↓
- [Parser] JSON Response -> GraphQL Object ↓
- [UI] Auto-fill React Form Fields
Goal: Create and update character definitions. Flow:
- [UI]
CreateCharacterForm.tsx(User clicks Save) ↓ GraphQL Mutation (CREATE_CHARACTERorUPDATE_CHARACTER) - [Client]
apolloClient.ts↓ POST /api/graphql/ - [Schema]
graphql/schema.py(Mutation Resolvers) ↓ - [ORM]
models.py(Charactermodel) ↓ - [DB] PostgreSQL (Persistent Storage)
Goal: Show All Sessions in History Sidebar. Flow:
- [Client]
api.ts(getChatSessions) ↓ GET /api/sessions/ - [View]
views.py->serializers.py(Serialize DB Objects) ↓ JSON Response (Snake_Case) - [Client]
api.ts(normalizeSession) -> Data Normalization ↓ CamelCase Data - [UI] Sidebar Render
Goal: Persist dynamic settings (World Time, User Persona) for the session. Flow:
- [UI]
SessionSettings.tsx(User updates settings) ↓ - [Client]
api.ts(updateChatSession) ↓ PATCH /api/sessions/{id}/ - [View]
views.py(ChatSessionViewSet) ↓ - [Serializer]
serializers.py(Validation) ↓ - [DB] Update
ChatSessionrow in PostgreSQL
Goal: Display paginated, detailed list of all conversations. Flow:
- [UI]
page.tsx(Switch view to 'history_all') ↓ - [Data] Reuses
recentChatsstate (Fetched viaapi.getChatSessions) ↓ - [Render] Map through history items ↓
- [Interaction] Delete Session
↓
api.deleteChatSession(DELETE /api/sessions/{id}/) ↓ Update State (Remove item from list)
Goal: Reload previous messages when entering an old session. Flow:
- [User] Selects a Chat ID ↓
- [UI]
ChatInterface.tsx(useEffect trigger) ↓ Parallel Requests - [Client]
api.ts├──getMessages(id)(GET /api/messages/?chat_session_id=X) └──getChatSession(id)(GET /api/sessions/{id}/) ↓ - [Backend]
views.py(MessageViewSet) filters by Session ID ↓ - [State] Redux Store (
setMessages,setChatSession) ↓ - [UI]
ChatWindow.tsx(Re-renders message bubbles)
Goal: Load character details and restore chat history when entering a room.
Location: frontend/src/components/ChatInterface.tsx
Flow A: Character Context (for NEW chat)
- [Trigger]
useEffect(oncharacterIdchange) ↓ - [Function]
loadCharacter()(Internal Async) ↓ - [Client]
api.ts(getCharacter) -> GET /api/characters/{id}/ ↓ - [State]
dispatch(setCharacter)-> Update Redux
Flow B: History Restoration (for OLD chat)
- [Trigger]
useEffect(oninitialSessionIdchange) ↓ - [Function]
loadChatHistory()(Internal Async) ↓ - [Client] Parallel Fetch via
api.ts: ├──getMessages(id)-> GET /api/messages/ └──getChatSession(id)-> GET /api/sessions/{id}/ ↓ - [State]
dispatch(setMessages)&dispatch(setChatSession)
Goal: Remove specific chat history and cleanup database. Flow:
- [UI]
page.tsx(User clicks Trash icon in Sidebar) ↓ - [Client]
api.ts(deleteChatSession) ↓ DELETE /api/sessions/{id}/ - [View]
views.py(ChatSessionViewSet) ↓ (Inheriteddestroymethod) - [DB] PostgreSQL [Cascade Delete] Removes Session AND all associated Messages automatically. ↓
- [UI] State Update (Filter out item from
recentChatslist)
Goal: Safely remove a character while preserving data integrity. Flow:
- [UI]
CharacterGallery.tsx(User clicks Delete) ↓ GraphQL Mutation (DELETE_CHARACTER) - [Schema]
graphql/schema.py(delete_characterresolver) ↓ [Validation Check]if character.chat_sessions.exists(): return False(Prevents deleting characters that have active history) Referential Integrity ↓ - [DB] PostgreSQL (Delete Row) ↓
- [UI] Refetch List / Remove Card