Files
ai-kit/test
CatoandClaude Opus 5 5119b59eb5 feat(stream): the chain can stream, so apps stop going around it (#62)
This package could not stream. `complete()` is request/response only, so every
app that wanted tokens to appear as they arrived used ai-kit to pick the model,
classify the 429 and track health — and then threw all of it away for the one
call that matters. Measured across the fleet on 2026-09-15: six hand-rolled
provider clients, four SSE readers, four emitters with divergent headers, under
~20,800 lines of chat UI in nine apps.

The duplication was real and it was in the wrong place. The apps were not
reinventing streaming because streaming is hard; they were reinventing it
because the package that owned the chain had no door for it.

ONE WALK, NOT TWO

`complete()` and `completeStream()` now share `walkChain`, extracted from
complete() unchanged. They must agree about what a dead vendor is, or the fleet
gets two answers to "is this provider out of budget" and the streaming path —
the one every chat surface uses — is the one that gets it wrong. complete()'s
existing suite is what proves the extraction was faithful; all 278 tests pass.

WHERE FALLBACK STOPS, AND WHY

A non-streaming walk can try every link because nothing has been shown yet. A
streaming walk cannot: once the first token is out, falling back would replay
the answer from the beginning into somebody already reading it. So the boundary
is explicit. Fails before the first delta -> LinkFailure, the walk continues.
Fails after -> StreamInterrupted, carrying the partial text, no silent recovery,
because there is no honest one.

That is also why a link is "served" only once a first token is really out. A 200
that then says nothing demotes, which is the same judgement complete() already
makes about an empty completion.

THE BUG THIS ENDS

ai-kit/sse carries both ends of the wire, lifted from loki, which had them
right. The reader KEEPS THE REMAINDER between chunks. A ReadableStream chunk is
a TCP-sized slice ending wherever the network put the boundary — routinely
mid-frame. Two apps split each chunk on newlines and dropped whatever straddled
it: a word vanishes from a long answer under load, nobody reports it, nobody can
reproduce it, and every short test passes.

12 new tests, choosing their own chunk boundaries because the boundaries ARE the
bug — including a frame cut mid-JSON and a chunk cut inside a two-byte "ü".
Proven by mutation: reintroducing the shipped `split` with no remainder turns
the suite red.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 02:31:37 -07:00
..