nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] SSE wrapper in createUndiciFetch holds back the last 13 characters of every chunk, delaying each streamed event

Open addyCooks opened this issue 2 weeks ago • 1 comments

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

  1. Start a local OpenAI-compatible server that sends one SSE event with content first, waits 3 seconds, then sends second and finishes.
  2. Call AISDKClient.chat against it with an onToken callback that prints the elapsed time.
  3. 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 repair data: [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.

addyCooks avatar Nov 14 '26 06:11 addyCooks

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.

Purv-Kabaria avatar Nov 14 '26 10:11 Purv-Kabaria