feat: add streaming responses for AI assistant (resolves #56)#78
Merged
Atharv777 merged 5 commits intokentuckyfriedcode:mainfrom Apr 20, 2026
Merged
Conversation
…dcode#56) The backend agent (server/agent.py) already emits Server-Sent Events (SSE) via text/event-stream. This PR wires up the frontend to consume the stream incrementally so users see tokens as they arrive rather than waiting for the full response. Changes ------- * hooks/useStream.ts (new) - Custom React hook that opens a fetch() connection and reads ReadableStream chunks in real-time. - Parses SSE frames: 'output', 'input_required', and 'error'. - Exposes { lines, isStreaming, inputRequired, error, startStream, stopStream, reset }. - Supports both GET (no body) and POST (with context_payload body). - Uses AbortController for clean teardown on unmount or stopStream(). * components/StreamingMessage.tsx (new) - Renders accumulated stream lines as incremental paragraphs. - Shows a blinking cursor (▋) while isStreaming === true. - Displays an accessible error badge (role='alert') on stream failure. - aria-live='polite' so screen readers announce new tokens. * tests/components/streaming.test.tsx (new) - 30 tests covering useStream hook and StreamingMessage component. - Uses jsdom-compatible fake ReadableStream mocks (no native canvas / fetch dependency). - Covers: initial state, output frames, input_required, error frames, HTTP errors, network errors, null body, stopStream, reset, POST vs GET, multiple consecutive streams, malformed JSON skip. - StreamingMessage: cursor visibility, error badge, aria attributes, custom className, long responses, streaming → complete transition. * tests/components/chat.test.tsx (modified — additive only) - Added 'Chat Interface — Streaming message types' describe block with 8 new tests modelling SSE frame shapes and streaming message state. - All 5 original tests left untouched. * tests/setup.ts (modified) - Polyfills global TextEncoder / TextDecoder for jsdom compatibility. All 52 tests pass (npm test).
Collaborator
|
Hey @lohit-40, can you please share screenshot/video of the updated UI of the streaming tokens? The code looks good to me. |
Contributor
Author
Collaborator
|
Hey @lohit-40, as a brownie point, can you fix the line-break issue too? As of now, each word appears in a new line. You can use a formatting library if you want, and if you make it robustly formatted output, we'll give you double the points as a single PR. |
Contributor
Author
|
can u check rn if anything else needed |
Collaborator
|
Perfect, thanks @lohit-40 |
Collaborator
|
Hey, the frontend tests are failing |
Contributor
Author
|
leeme check give 5 min |
Contributor
Author
|
can u check rn i feel there shouldn't any more hiccups! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Resolves #56
The backend (server/agent.py) already emits text/event-stream Server-Sent Events (SSE). This PR wires up the frontend to consume the stream incrementally so users see tokens as they arrive rather than waiting for the full response.
Changes
hooks/useStream.ts (new)
components/StreamingMessage.tsx (new)
tests/components/streaming.test.tsx (new)
tests/components/chat.test.tsx (modified -- additive only)
tests/setup.ts (modified)
Test Results
Test Suites: 6 passed, 6 total
Tests: 52 passed, 52 total
Acceptance Criteria