WebSocket Chat¶
Overview¶
The WebSocket chat implementation provides real-time, bi-directional communication between users and the AI assistant through a web interface. It's currently the only active communication channel in the system.
Implementation Details¶
System Flow¶
```mermaid sequenceDiagram participant User participant Frontend participant Backend participant LLM Service participant Firestore
User->>Frontend: Connect to WebSocket
Frontend->>Backend: Send auth message
Backend->>Firestore: Verify user
Backend-->>Frontend: Accept connection
User->>Frontend: Send message
Frontend->>Backend: Forward message
Backend->>LLM Service: Generate response
LLM Service-->>Backend: Stream response
Backend-->>Frontend: Stream chunks
Frontend-->>User: Display response
```
Code Flow¶
```mermaid graph TD subgraph Frontend [DEPRECATED - Will be removed] A[ChatComponent] → B[WebSocket Connection] B → C[Message Handling] C → D[UI Updates] end
subgraph Backend
E[WebSocket Endpoint] --> F[Auth Middleware]
F --> G[verify_firebase_token]
subgraph Application Layer
G --> H[chatbot_websocket]
H --> I[LLMService]
end
subgraph Domain Layer
I --> J[Response Generation]
J --> K[Streaming]
end
subgraph Infrastructure Layer
K --> L[WebSocket]
L --> M[Client]
end
end
style Frontend fill:#f9f,stroke:#333,stroke-width:2px
style Backend fill:#bbf,stroke:#333,stroke-width:2px
style Application Layer fill:#dfd,stroke:#333,stroke-width:2px
style Domain Layer fill:#fdd,stroke:#333,stroke-width:2px
style Infrastructure Layer fill:#ddf,stroke:#333,stroke-width:2px
```
Components¶
Frontend [DEPRECATED - Will be removed]¶
ChatComponent.tsx: Main chat interface componentMessageList.tsx: Handles message displayChatMessageItem.tsx: Individual message rendering
Backend¶
chatbot.py: WebSocket endpoint handlerllm_service.py: AI response generationapp_user_repository.py: User management
Features¶
- Real-time Communication
- Bi-directional WebSocket connection
- Streaming responses
-
Instant message delivery
-
Session Management
- Session ID generation
- Local storage persistence
-
Automatic reconnection
-
Message Handling
- Markdown formatting
- Code block support
-
Error message display
-
UI Features [DEPRECATED - Will be removed]
- Message history
- Connection status
- Expandable chat window
- Message timestamps
API Endpoints¶
GET /api/v1/async/chatbot: WebSocket connection endpoint
Error Handling¶
- Connection Errors
- Automatic reconnection
- User notification
-
Error state display
-
Authentication Errors
- Token validation
- Session verification
-
Graceful disconnection
-
Message Errors
- Error message display
- Retry mechanism
- State recovery
Monitoring¶
- Connection Status
- WebSocket state
- Connection duration
-
Reconnection attempts
-
Performance Metrics
- Response time
- Message throughput
- Error rates
Areas for Improvement¶
High Priority¶
- Frontend Migration
- Remove deprecated frontend components
- Update documentation
-
Clean up unused code
-
Session Management
- Implement server-side session storage
- Add session timeout handling
-
Improve reconnection logic
-
Error Handling
- Add specific error types
- Implement retry strategies
- Enhance error messages
Medium Priority¶
- Features
- Add message editing
- Implement message reactions
-
Add file sharing
-
Security
- Add message encryption
- Implement rate limiting
-
Add IP blocking
-
Performance
- Optimize message streaming
- Implement message batching
- Add rate limiting
Low Priority¶
- Documentation
- Add API documentation
- Create troubleshooting guide
-
Document best practices
-
Testing
- Add integration tests
- Implement load testing
-
Add UI tests
-
Maintenance
- Update dependencies
- Optimize code
- Clean up technical debt