[Bug] SSE wrapper in createUndiciFetch holds back the last 13 characters of every chunk, delaying each streamed event
Description
createUndiciFetch rewrites the event stream and keeps the last 13 characters of its buffer until the next chunk arrives. The held characters include the end of the event (\n\n), so each event is parsed one chunk late. When the model pauses, the last text shows up seconds later.
Environment
- OS: Windows 11 Home 10.0.26300
- Node version: v22.22.3
- Nanocoder version: 1.32.0 (built from main at f92cce9c)
- Provider: Local mock OpenAI-compatible server
- Model: Mock model
Steps to Reproduce
- Start a local OpenAI-compatible server that sends one SSE event with content
first, waits 3 seconds, then sendssecondand finishes. - Call
AISDKClient.chatagainst it with anonTokencallback that prints the elapsed time. - Compare with the time the first bytes reach a raw undici fetch (about 175 ms).
Expected Behavior
first reaches onToken about 0.2 to 0.3 seconds after the request.
Actual Behavior
The first token arrives only when the next chunk does.
Logs/Screenshots
[3280ms] onToken "first second"
[3352ms] onFinish
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] I can reproduce this issue consistently
- [x] This issue occurs with the latest version of nanocoder (main)
Suggested fix:
source/ai-sdk-client/providers/provider-factory.ts:134-170: the 13-character tail only exists to repairdata: [DONE]split across chunks. Hold back only when the buffer ends with a prefix of that marker, or work per line/event instead.- Add a test that a single event is forwarded immediately.
I will pick this up, following the suggested stream-buffer fix. I will retain only a suffix that can be the start of the malformed DONE marker, so complete SSE events are forwarded immediately. Regression tests will cover a paused stream and marker splits across chunk boundaries.