This document covers the streamlined prompt flow through the system using LangGraph for intelligent backend routing:
- From frontend trigger to Node.js LangGraph handlers
- Single socket event for all operations
- Backend-driven routing and decision making
- Simplified frontend integration
Core Functions
handleSubmitPrompt(refine = false)
Triggers the complete prompt submission lifecycle through single socket event.
Behavior:
- First message: Creates new chat and adds user as member
- All messages: Emits single socket event to backend
- Disables chat input until response received
- No frontend routing decisions required
enterNewPrompt()
Creates message entry in MongoDB before backend submission.
Usage: Must be called before sending to backend
Payload Structure
Single payload format for all AI interactions:Backend Processing
Single Entry Point
All AI requests go through one unified endpoint: Socket Event:ai-query
Processing Flow:
- Receive socket event with payload
- LangGraph analyzes request requirements
- Backend determines operation type:
- Normal chat
- Document-based query
- Agent interaction
- Agent + Document combination
- Tool requirements (search, image)
- Execute appropriate handler
- Stream response back via socket
Backend Operation Functions
Unified Handler:processAIQuery(payload)
- Single entry point for all operations
- Simple chat processing
- Document retrieval and RAG
- Agent prompt integration
- Tool activation (SearxNG, DALL·E)
- Vision processing for images
- Previous architecture had multiple client-side function calls
- Current architecture: backend handles all routing via LangGraph
React Hooks
Core Hooks
useConversation()
- Central conversation management with unified LLM streaminguseThunderBoltPopup()
- File uploads, agent selection, prompt selectionuseMediaUpload()
- Media uploads for conversations
Data Hooks
useCustomGpt()
- Fetch available custom GPT agentsusePrompt()
- Retrieve saved promptsuseBrainDocs()
- Load uploaded documents
Utility Hooks
useIntersectionObserver()
- Infinite scroll for listsuseDebounce()
- Debounce user inputuseServerAction()
- Server-side actions in Next.js
UI State Management
Unified State Updates
handleModelSelectionUrl()
- Sync model state with URLhandleProAgentUrlState()
- Sync Pro Agent state with URLhandleNewChatClick()
- Start new chat session
Backend Intelligence
LangGraph Decision Making
Backend automatically handles: Operation Detection:- Query analysis for intent
- Document requirement identification
- Agent configuration loading
- Tool necessity determination
- Chat history retrieval
- Document vector search (if needed)
- Agent prompt integration (if selected)
- Tool setup (if required)
- Single or dual LLM call based on tools
- Streaming response to frontend
- Cost tracking and logging
UI Components
Lists and Modals
CommonList
- Combined private/shared document listRenderModalList
- Model selection dropdownAgentSelector
- Agent selection interface (no routing logic needed)
Parameters
All operations use the same parameter set:Backend-Only Parameters
These are handled internally by backend:- Tool selection decisions
- RAG vs Simple Chat routing
- Context assembly strategies
- LLM call optimization
Socket Communication
Frontend Socket Events
Emit Events:ai-query
- Single event for all operations
ai-response-stream
- Streamed response chunksai-response-complete
- Final response signalai-error
- Error notifications
Backend Socket Handling
Event Processing:- Receive
ai-query
event - LangGraph processes request
- Stream response via
ai-response-stream
- Emit
ai-response-complete
when done - Handle errors via
ai-error
Troubleshooting
Prompt Not Reaching Backend
Debug Steps:- Verify
handleSubmitPrompt()
triggered - Check socket connection status
- Confirm
enterNewPrompt()
executed - Review browser console for errors
- Validate payload structure
- Socket disconnected
- Payload validation failure
- Network interruption
- Backend service down
Response Not Displaying
Debug Steps:- Check socket event listeners active
- Verify
ai-response-stream
handler - Review UI update logic
- Test with simplified query
- Event listener not attached
- UI state not updating
- Response parsing error
- Stream interruption
Agent or Document Issues
Debug Steps:- Verify agent/document IDs in payload
- Check backend logs for retrieval
- Validate database connections
- Test with simple queries first
- Invalid agent/document ID
- Database connection failure
- Vector search timeout
- Permission issues