WebSocket Architecture DocumentationΒΆ
Welcome to the OpenContracts WebSocket architecture documentation! This directory contains comprehensive documentation for the real-time chat system that powers document and corpus conversations.
π OverviewΒΆ
OpenContracts uses WebSocket technology to provide real-time, streaming conversations with AI agents. The system enables users to ask questions about documents and corpuses, receiving answers that stream progressively with source citations, reasoning timelines, and tool approval workflows.
π Documentation StructureΒΆ
π Protocol OverviewΒΆ
Essential reading for understanding the WebSocket communication protocol
- Message types and data structures
- Connection lifecycle and authentication
- State management patterns
- Error handling strategies
- Security considerations
- Performance characteristics
π₯οΈ Backend ImplementationΒΆ
Deep dive into the Django Channels WebSocket consumers
UnifiedAgentConsumer: All agent chat contexts (corpus, document, standalone)ThreadUpdatesConsumer: Real-time thread/conversation updatesNotificationUpdatesConsumer: Real-time user notifications- Agent integration and lifecycle management
- Event processing and streaming
- Approval workflow implementation
- Error handling and logging
- Performance optimizations
π¨ Frontend ImplementationΒΆ
Comprehensive guide to the React WebSocket components
ChatTray: Document sidebar chat interfaceCorpusChat: Full-screen corpus conversations- State management with React hooks and Jotai
- Message processing and UI updates
- Source citation integration
- Mobile responsiveness
- Testing strategies
π Quick StartΒΆ
For Backend DevelopersΒΆ
- Read the Protocol Overview to understand message flow
- Study the Backend Implementation for consumer details
- Key files to examine:
config/websocket/consumers/unified_agent_conversation.pyconfig/websocket/consumers/thread_updates.pyconfig/websocket/consumers/notification_updates.pyopencontractserver/llms/agents/
For Frontend DevelopersΒΆ
- Start with the Protocol Overview for context
- Dive into the Frontend Implementation for component details
- Key files to examine:
frontend/src/components/knowledge_base/document/right_tray/ChatTray.tsxfrontend/src/components/corpuses/CorpusChat.tsxfrontend/src/components/widgets/chat/ChatMessage.tsx
For Product/QA TeamsΒΆ
- The Protocol Overview provides the best high-level understanding
- Focus on the "Message Types" and "Connection Lifecycle" sections
- Use the error handling sections for troubleshooting guidance
ποΈ System ArchitectureΒΆ
```mermaid graph TB subgraph "Frontend (React)" CT[ChatTray Component] CC[CorpusChat Component] CM[ChatMessage Component] SA[Source Atom State] end
subgraph "WebSocket Layer"
WS[WebSocket Connection]
MSG[Message Protocol]
end
subgraph "Backend (Django Channels)"
UAC[UnifiedAgentConsumer]
TUC[ThreadUpdatesConsumer]
NUC[NotificationUpdatesConsumer]
AGENT[LLM Agents]
end
subgraph "Data Layer"
DB[(Database)]
CONV[Conversations]
MSGS[Messages]
end
CT --> WS
CC --> WS
WS --> MSG
MSG --> UAC
MSG --> TUC
MSG --> NUC
UAC --> AGENT
AGENT --> DB
DB --> CONV
DB --> MSGS
SA --> CT
SA --> CC
CM --> CT
CM --> CC
```
π Message Flow ExampleΒΆ
Here's a typical conversation flow:
```mermaid sequenceDiagram participant User participant Frontend participant WebSocket participant Backend participant Agent participant DB
User->>Frontend: Types question
Frontend->>WebSocket: {"query": "What is...?"}
WebSocket->>Backend: Forward message
Backend->>Agent: Process query
Backend->>DB: Store user message
Backend->>WebSocket: ASYNC_START
WebSocket->>Frontend: Message begins
Frontend->>User: Show processing state
Agent->>Backend: Content chunks
Backend->>WebSocket: ASYNC_CONTENT
WebSocket->>Frontend: Stream content
Frontend->>User: Display progressive text
Agent->>Backend: Source citations
Backend->>WebSocket: ASYNC_SOURCES
WebSocket->>Frontend: Citation data
Frontend->>User: Show source pins
Agent->>Backend: Final response
Backend->>DB: Store assistant message
Backend->>WebSocket: ASYNC_FINISH
WebSocket->>Frontend: Mark complete
Frontend->>User: Enable new input
```
π― Key FeaturesΒΆ
Real-Time StreamingΒΆ
- Progressive content display as the AI generates responses
- No waiting for complete responses
- Immediate visual feedback
Source CitationsΒΆ
- Live source references during streaming
- Clickable annotations that highlight document sections
- Integration with document viewer
Tool Approval WorkflowΒΆ
- User authorization for sensitive operations
- Modal dialogs for approval decisions
- Graceful handling of approvals/rejections
Conversation PersistenceΒΆ
- Automatic saving of conversation history
- Resume functionality for long conversations
- Cross-session conversation access
Mobile ResponsivenessΒΆ
- Adaptive layouts for mobile devices
- Touch-friendly interfaces
- Optimized performance on mobile networks
π οΈ Development WorkflowΒΆ
Adding New Message TypesΒΆ
- Backend: Add new event type to
opencontractserver/llms/agents/core_agents.py - Consumer: Handle new event in both consumers' message processing
- Frontend: Add case to message processing switch statements
- Types: Update TypeScript interfaces in frontend
- Documentation: Update this documentation
Debugging TipsΒΆ
- Use Session IDs: All backend logs include session IDs for correlation
- Browser DevTools: Monitor WebSocket frames in Network tab
- Console Logging: Frontend logs all WebSocket events
- Database Queries: Check conversation and message records for persistence issues
Testing StrategiesΒΆ
- Unit Tests: Test individual message processing functions
- Integration Tests: Use Django Channels testing for full flow
- E2E Tests: Browser automation for complete user workflows
- Manual Testing: Use browser DevTools to simulate edge cases
π TroubleshootingΒΆ
Common IssuesΒΆ
Connection Failures - Check authentication status - Verify corpus/document permissions - Review Django Channels configuration
Message Processing Errors - Validate JSON message format - Check message type spelling - Review browser console for errors
Performance Issues - Monitor WebSocket frame sizes - Check for memory leaks in long conversations - Review database query performance
UI State Issues - Verify state synchronization between server and client - Check message deduplication logic - Review React component lifecycle
Debug CommandsΒΆ
# Check WebSocket connections
docker logs opencontracts_django_1 | grep "Session"
# Monitor database conversations
docker exec -it opencontracts_postgres_1 psql -U postgres -d opencontracts -c "SELECT * FROM conversations_conversation ORDER BY created_at DESC LIMIT 10;"
# View WebSocket routing
docker exec -it opencontracts_django_1 python manage.py shell -c "from config.routing import websocket_urlpatterns; print(websocket_urlpatterns)"
π Additional ResourcesΒΆ
π€ ContributingΒΆ
When contributing to the WebSocket system:
- Read all documentation in this directory first
- Follow established patterns for message handling
- Add appropriate logging with session IDs
- Update documentation for any protocol changes
- Test thoroughly across both components
- Consider mobile UX for all UI changes
π Related DocumentationΒΆ
This documentation is maintained by the OpenContracts development team. For questions or improvements, please create an issue or submit a pull request.