mirror of
https://github.com/Mail-0/Zero.git
synced 2026-06-28 23:06:54 +00:00
# feat: sync threadId to ZeroAgent via WebSocket for multi-tab support
## Summary
This PR implements real-time threadId synchronization between the frontend and ZeroAgent backend to provide better context to the AI assistant. When users navigate between email threads in the frontend (via `useQueryState('threadId')`), the system now automatically sends WebSocket updates to the ZeroAgent so the AI knows which thread the user is currently viewing.
**Key changes:**
- Added `ThreadIdUpdate` WebSocket message type for bidirectional communication
- Created `ThreadIdSyncProvider` component that monitors threadId changes and sends updates
- Modified `ZeroAgent` to store threadId per connection for multi-tab support
- Updated AI system prompt generation to include current threadId context
- Maintains connection isolation so multiple tabs can have different active threads
## Review & Testing Checklist for Human
- [ ] **Verify frontend loads properly and threadId sync works** - There was a CSS import error during development that prevented browser testing. Confirm the app loads and threadId changes trigger WebSocket messages in DevTools.
- [ ] **Test multi-tab support** - Open multiple browser tabs, navigate to different threads in each tab, and verify the AI chat in each tab has context for the correct thread.
- [ ] **Confirm AI system prompt includes threadId** - Start a chat conversation and verify the AI assistant has proper context about the current thread the user is viewing.
- [ ] **Check for regressions** - Verify existing email navigation, AI chat functionality, and WebSocket notifications still work as expected.
- [ ] **Test edge cases** - Try rapid thread navigation, WebSocket reconnections, and tab closing/opening to ensure the system handles these scenarios gracefully.
---
### Diagram
```mermaid
%%{ init : { "theme" : "default" }}%%
graph TD
Frontend["apps/mail/components/party.tsx<br/>NotificationProvider"]:::major-edit
ThreadSync["ThreadIdSyncProvider<br/>(new component)"]:::major-edit
QueryState["useQueryState('threadId')<br/>(existing hook)"]:::context
Backend["apps/server/src/routes/agent/index.ts<br/>ZeroAgent class"]:::major-edit
Types["apps/server/src/routes/agent/types.ts<br/>Message Types"]:::minor-edit
Prompt["apps/server/src/lib/prompts.ts<br/>AiChatPrompt"]:::context
QueryState --> ThreadSync
ThreadSync --> Frontend
Frontend -->|"WebSocket ThreadIdUpdate"| Backend
Backend --> Prompt
Types -.->|"defines message structure"| Frontend
Types -.->|"defines message structure"| Backend
subgraph Legend
L1[Major Edit]:::major-edit
L2[Minor Edit]:::minor-edit
L3[Context/No Edit]:::context
end
classDef major-edit fill:#90EE90
classDef minor-edit fill:#87CEEB
classDef context fill:#FFFFFF
```
### Notes
- **Session Info**: Requested by @MrgSub, implemented in Devin session: https://app.devin.ai/sessions/f2c92b4c778d4314a4c9947ca2022667
- **Environment Issue**: Frontend had CSS import errors during development preventing full browser testing. The implementation follows existing WebSocket patterns but needs verification in a working environment.
- **Architecture**: Uses existing connection management system where each browser tab gets a unique connectionId, enabling independent threadId tracking per tab.
- **Backward Compatibility**: Changes are additive and shouldn't break existing functionality, but the system prompt generation flow has been modified to use stored threadId instead of request-time threadId.
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Enabled real-time syncing of the current threadId to ZeroAgent over WebSocket, allowing each browser tab to maintain its own thread context for multi-tab support.
- **New Features**
- Added a ThreadIdSyncProvider component to detect thread changes and send updates to ZeroAgent.
- ZeroAgent now tracks threadId per connection, so each tab can have independent context.
- System prompt generation now uses the correct threadId for each connection.
<!-- End of auto-generated description by cubic. -->