Privacy-friendly analytics only with consent. No ads, no cross-site tracking. Read the privacy policy.
Slipstream components consume a stream-agnostic contract: a growing content string and an optional streamingMessageId. Whether the bytes come from SSE, WebSockets, or LangGraph, the UI side is identical.
// While streaming, exactly one message updates on every chunk:setMessages((prev) =>prev.map((m) => (m.id === replyId ? { ...m, content } : m))));setStreamingId(replyId); // drives the typing cursor + aria-busy// When done:setStreamingId(null); // cursor stops, completion is announced
async function* streamText(url: string, body: unknown, signal: AbortSignal) {const res = await fetch(url, { method: 'POST', body: JSON.stringify(body), signal });if (!res.ok || !res.body) throw new Error('Stream failed: ' + res.status);const reader = res.body.getReader();const decoder = new TextDecoder();let buffer = '';while (true) {const { done, value } = await reader.read();if (done) break;buffer += decoder.decode(value, { stream: true });// SSE lines: "data: {...}": split on newlines, keep the tail.const lines = buffer.split('\n');buffer = lines.pop() ?? '';for (const line of lines) {const payload = line.replace(/^data:\s*/, '');if (payload && payload !== '[DONE]') yield JSON.parse(payload).delta as string;}}}
Keep the AbortController in a ref: PromptInput's stop button just calls it. Abort is not an error: stop writing and leave the partial text in place (users expect their stop to keep what arrived).
const abortRef = useRef<AbortController | null>(null);async function send(text: string) {const controller = new AbortController();abortRef.current = controller;try {for await (const delta of streamText('/api/chat', { text }, controller.signal)) {appendDelta(delta);}} catch (err) {if ((err as Error).name === 'AbortError') return; // user pressed StopsetMessageError(replyId, (err as Error).message); // render retry card} finally {setStreamingId(null);}}<PromptInput onSubmit={send} isLoading={streamingId !== null}onStop={() => abortRef.current?.abort()} />
Streams cut mid-token: **half, unclosed code fences, dangling tables. Response repairs those on every render (parseIncompleteMarkdown, on by default): never show raw asterisks while streaming.
response.body readers on abort: holding them keeps the connection open.