RAG Streaming SSE Protocol
POST /rag/query/stream is a source-only, contract-pinned streaming surface.
Its successful response media type is text/event-stream, not a JSON response
body. The checked site-side protocol fixture is
docs-api/rag-stream-sse-contract.json; its digest is
da4a2ac57441610e0656f929b56c375a3a0f4ba75a564754fd3236e930d14fef.
This page documents the protocol boundary only. retvest-platform owns the
route and stream producer, retvest-sdk owns SDK consumers and distribution
claims, and retvest-app owns browser consumers. A live end-to-end capture,
every cross-repository consumer pin, and portfolio acceptance remain owner
evidence pending.
Event Order
Frames use standard SSE event: and data: fields, with one JSON object in
each event's data field. The declared event order is meta, citations,
token, error, then done:
| Event | Role | Terminal behavior |
|---|---|---|
meta |
Carries contract-owned stream metadata. | Non-terminal; it is the first event. |
citations |
Carries the canonical citations array. |
Non-terminal; it follows meta and precedes tokens. |
token |
Carries one non-empty streamed text fragment. | Non-terminal; a successful stream has at least one. |
error |
Carries a machine-readable failure code. | Terminal alternative to done. |
done |
Marks successful completion. | Terminal success event. |
A successful stream follows meta citations token+ done. A failed stream
follows meta (citations token*)? error. Exactly one terminal event is
allowed: error and done are alternatives, never a pair. Consumers use the
single canonical citations array event; they must not substitute singular
citation events or a terminal source list.
Consumer Safety Rules
- Buffer arbitrarily fragmented UTF-8 SSE input until a blank-line frame terminator arrives; do not parse a partial frame.
- Reject a frame whose
data:is not one JSON object. A malformed payload is a failed stream, not a partial answer that may be continued. - Treat a transport disconnect before
errorordoneas a failed stream. - Reject a second terminal event and every event after the terminal event.
- Treat the
citationsevent as the canonical source publication payload; do not synthesize a singular citation event or infer a source list fromdone.
The fixture does not capture a fresh stream, prove platform/SDK/app runtime behavior, or establish that all portfolio consumers have synchronized the digest. Those remain cross-repository acceptance work.