Files
heidi/app/api/chat
CatoandClaude Opus 5 19853d7cc0 feat(chat): the explanation arrives as it is written (#67)
* feat(chat): the explanation arrives as it is written

Heidi had no streaming at all: every answer appeared at once, after a silent
wait of up to 25 seconds. ai-kit gained `completeStream` and an `ai-kit/sse`
subpath precisely so nine apps would stop hand-rolling this — four SSE readers
across the fleet, two of them dropping frames on chunk boundaries.

ONLY THE EXPLANATION STREAMS, AND THAT IS A SAFETY PROPERTY. `text` is prose
in the reader's own language and `parse.ts` is explicit that it must not face
the variety gate. Everything the gate DOES judge — the dialect line, the
suggestions — waits for `parseAnswer`, along with the invented-correspondence
strip, the gloss-against-itself drop and the degenerate-loop refusal. A learner
cannot audit dialect (§2), so a form shown before it is checked, even for a
second, is the one failure this product is built to prevent. The prose streams;
the language waits for the checker.

Which needed a new primitive. The answer is a JSON object, so raw tokens would
show `{"mode":"understand","text":"Sie f`. `extractJson` already repairs
truncated JSON and cannot help: it repairs by cutting back to the last COMPLETE
value, so a string still being written is discarded and the explanation would
appear in one jump when its closing quote arrived. `partial.ts` reads a
half-written string instead — a real scan tracking string state, not a regex,
because the one thing people paste into this product is text they did not
write, which is occasionally JSON. Its test asserts the property that matters
under streaming: EVERY prefix of a real answer is showable, never leaking a
quote, a brace or half an escape sequence.

Streaming is an observation on the same turn, not a second path. Pass `onText`
and `respondInThread` calls `completeStream` instead of `complete`; threadkit
still decides whether Heidi speaks, and every guard still runs on the whole
answer. A separate streaming-respond would have been the third copy of that
wiring, and the second one already drifted.

One route, not two. The rate limit, the input ceiling, the picture checks and
the history rebuild are identical and are the part that must not diverge; only
the reply shape differs, at the last possible moment. Nine tests assert the
guards hold through BOTH shapes — a streaming request that skipped the ceiling
would let anyone post a novel and have it forwarded a token at a time. Verified
by hand too: a 2,500-character input is refused 400 with `stream: true`.

`/api/chat` moves to relative `.ts` imports so node's runner can exercise it,
the convention `app/api/check/route.ts` already documents. The wire contract
moved to `lib/domain/chat/events.ts` rather than living in the route: the
client needs it, and a browser module importing a server module for its types
works only until somebody adds a value import beside it.

A terminal event is mandatory. A stream has already sent its headers when
something goes wrong, so it cannot report a status — every turn ends with
exactly one of answer, silent or error, and a stream that closes without one is
a failure, because silence is not a status.

Verified locally: correct SSE headers including the `no-transform` and
`x-accel-buffering: no` that stop a proxy delivering the whole answer in one
piece; the client decodes the stream and produces the specific localised
message rather than a generic failure; the blocking path still answers JSON.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs: the canonical doc records how streaming works, and what it refuses to stream

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 05:08:22 -07:00
..