Fix/sse streaming events#203
Open
Antherix wants to merge 4 commits into
Open
Conversation
Owner
|
@Antherix Please resolve the merge conflicts |
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.
Description
This PR fixes the message streaming endpoint to emit properly structured SSE frames, enabling the frontend to reliably distinguish between content chunks, token usage data, completion signals, and errors. Previously, the backend wrote raw text to the response stream with no event typing, making structured client-side parsing impossible and blocking future features like per-message token tracking.
Closes #52
Type of Change
Checklist
Changes Made
SSE Helper (Backend)
writeSSE(res, event, data)helper inchatMessage.controller.jsthat writes valid SSE frames in the formatevent: …\ndata: …\n\nres.write()callsStreaming Loop (Backend)
res.write(content)withwriteSSE(res, "chunk", { content })for each LLM content deltawriteSSE(res, "usage", { inputTokens, outputTokens })when token counts arrive from the modelwriteSSE(res, "done", {})on successful stream completionres.end("Stream ended with error.", ...)withwriteSSE(res, "error", { message })on failureEarly-Exit Error Path (Backend)
res.write()to a properevent: errorSSE frame beforeres.end()SSE Frame Parser (Frontend —
src/lib/api.ts)sendMessageStreamwith a proper SSE frame parser\n\nto extract complete framesevent:anddata:lines from each frame and dispatches by event typechunkevents: extractscontentand callsonChunkerrorevents: extractsmessageand calls bothonErrorandonChunkto surface it in the UIusageanddoneevents: silently consumed, ready for future useJSON.stringify(payload)in the fetch body — previously serialisedonChunk,onError, andsignal(non-serialisable) into the request body; now explicitly serialises onlyuserPrompt,model,provider, andchatIdSignature Updates (Frontend —
src/lib/api.ts)signal?: AbortSignaltosendMessageStreampayload type and wired it to the fetch call, restoring abort support thatChatPage.tsxalready expectedonError?: (message: string) => voidcallback for callers that want typed error handling separate from the chunk streamSSE Contract
chunk{ content: string }usage{ inputTokens: number, outputTokens: number }done{}error{ message: string }Files Updated
Backend
backend/controllers/chatMessage.controller.jsFrontend
src/lib/api.tsHow Has This Been Tested?
event: chunk/event: doneframed responses on/message/sendonChunk,signal) are no longer sent in the request bodyAcceptance Criteria
Technical Notes
event: …\ndata: …\n\n) is the standard specified in the WHATWG EventSource spec. Using named events allows the client to branch on event type without inspecting the data payload, which was the core fragility this PR addresses.reader.read()call returns multiple frames or a partial frame, which is common under network pressure.onChunkcontinues to receive error message text as a fallback so the chat bubble always shows something meaningful even if the caller does not supplyonError.