Skip to content

fix(react-headless): robust SSE framing in agUIAdapter#331

Open
boffin-dmytro wants to merge 1 commit intothesysdev:mainfrom
boffin-dmytro:fix/react-headless-agui-sse
Open

fix(react-headless): robust SSE framing in agUIAdapter#331
boffin-dmytro wants to merge 1 commit intothesysdev:mainfrom
boffin-dmytro:fix/react-headless-agui-sse

Conversation

@boffin-dmytro
Copy link

Summary

This PR makes @openuidev/react-headless’s default agUIAdapter() handle real-world SSE streams reliably.

The previous implementation parsed SSE by splitting each network chunk on \n, which can drop events when JSON/lines are split across chunks (common in production), and didn’t fully respect SSE framing rules.

What changed

  • Hardened agUIAdapter() SSE parsing:
    • buffers across chunk boundaries (partial lines / partial JSON)
    • supports CRLF (\r\n)
    • supports blank-line delimited SSE events
    • supports multi-line data: fields (joined with \n per SSE spec)
    • ignores comment/keepalive lines
    • treats [DONE] as a terminator
    • best-effort by default: malformed events are skipped so streaming can continue
  • Added regression tests covering:
    • chunk-splitting
    • CRLF + multi-line data:
    • malformed event skipping + continued parsing
  • Added a small API reference note clarifying the expected SSE framing for agUIAdapter()

Why it matters

SSE chunk boundaries are arbitrary, and many servers emit CRLF and keepalives. With this change, AG-UI streaming is much more resilient under normal network conditions, preventing silent token/event loss and incomplete assistant messages.

Scope note

This PR intentionally focuses on agUIAdapter() (the default) to keep the change tight and low-risk. Similar SSE framing improvements could be applied to the OpenAI SSE adapters in a follow-up if maintainers want to consolidate parsing behavior.

Files changed

  • packages/react-headless/src/stream/adapters/ag-ui.ts
  • packages/react-headless/src/store/__tests__/createChatStore.test.ts
  • docs/content/docs/api-reference/react-headless.mdx

Kinly review this update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant