Minimal Server-Sent Events reader for fetch responses.
EventSource only does GET without custom headers, so streamed POST endpoints (the assistant's
messages) are read straight off response.body. Works wherever fetch returns a WHATWG
ReadableStream: every modern browser and Node 18+.
Follows the parts of the SSE format the API uses: data: lines (several in one event are
joined with a newline), events separated by a blank line, : comment lines ignored, and
\n, \r\n or \r as line endings. A chunk may end anywhere, including mid-line or in the
middle of a multi-byte character, so text is decoded in streaming mode and only complete lines
are parsed. Whatever is left when the stream ends is flushed as a last event.
Minimal Server-Sent Events reader for
fetchresponses.EventSource only does GET without custom headers, so streamed POST endpoints (the assistant's messages) are read straight off
response.body. Works whereverfetchreturns a WHATWG ReadableStream: every modern browser and Node 18+.Follows the parts of the SSE format the API uses:
data:lines (several in one event are joined with a newline), events separated by a blank line,:comment lines ignored, and\n,\r\nor\ras line endings. A chunk may end anywhere, including mid-line or in the middle of a multi-byte character, so text is decoded in streaming mode and only complete lines are parsed. Whatever is left when the stream ends is flushed as a last event.