mirror of
https://github.com/coder/coder.git
synced 2026-11-12 12:45:36 +08:00
## Problem When sending a message in the agent detail chat, the text lingered in the input textarea while the HTTP POST round-tripped to the server. Only after the server responded did the input clear and the message appear in the timeline (via WebSocket). This created a noticeable delay where the user couldn't start typing their next message. ## Solution **Optimistic input clear** (`AgentChatInput.tsx`): - Clear the textarea and editing state *immediately* on submit, before awaiting the network call. - Capture the input text beforehand so it can be restored in the `catch` block if the request fails. **Optimistic user bubble** (`AgentDetail.tsx`): - Inject a temporary `ChatMessage` (with a negative ID) into the chat store so the user's message bubble appears in the timeline instantly. - Set chat status to `pending` and clear stream state, mirroring the existing edit-message path. - On error, roll back: remove the optimistic message and restore the previous chat status. The real message arrives via the WebSocket stream and `upsertDurableMessage` replaces the optimistic entry naturally (the server message has a positive ID, so it's inserted alongside; the optimistic negative-ID message gets cleaned up when `replaceMessages` is called with the authoritative message list from the next query invalidation). ## Testing - Type a message and press Enter — input clears and bubble appears immediately. - Simulate a network error — input text is restored, optimistic bubble is removed. - Edit an existing message — unchanged behavior (already had optimistic updates). - Queue a message while streaming — unchanged behavior.