Making Interrupted AI Streams Visible: A React 19 SSE Implementation
An AI assistant stops mid-sentence. Your UI shows "complete." The user clicks Approve, and the partial instruction deletes the wrong database table. The postmortem reveals a dropped connection two seconds before the actual termination token arrived. This is not hypothetical.
For IDE integrations around agentic systems like JetBrains/thinkrail, the missing distinction between "finished," "cancelled," and "connection lost" creates authorization risks. Partial output cannot safely trigger edits or imply that an agent action succeeded. This article presents a companion chat integration pattern that preserves honest UI state when assistant streams are interrupted.
Protocol Translation Lives on the Server
The request path is direct:
React 19 client → Next.js route → OpenAI-compatible relay → model
The client speaks the Vercel AI SDK's UI message stream protocol. The upstream relay speaks OpenAI-compatible streaming. Those are not interchangeable despite both traveling over SSE. The server performs translation through streamText() and toUIMessageStreamResponse(). Returning raw upstream bytes to useChat() produces parser failures—the client expects SDK-specific message events, not arbitrary delta objects.
The browser owns rendering and user cancellation. The route owns credentials, model selection, protocol translation, and upstream cancellation. Neither component pretends reconnecting resumes an existing generation.
Baseline Implementation
Start inside an existing React 19 Next.js App Router application. This block installs dependencies, writes the route and page, and creates the environment template. Replace both environment values before starting the development server.
npm install ai @ai-sdk/react @ai-sdk/openai-compatible mkdir -p app/api/chat cat > .env.local <<'EOF' BLOST_API_KEY=replace-with-your-key BLOST_MODEL=replace-with-an-enabled-model-id EOF cat > app/api/chat/route.ts <<'EOF' import { convertToModelMessages, streamText, type UIMessage, } from 'ai'; import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; export const runtime = 'nodejs'; export const maxDuration = 60; export async function POST(req: Request) { const apiKey = process.env.BLOST_API_KEY; const model = process.env.BLOST_MODEL; if (!apiKey || !model) { return new Response('Missing model configuration', { status: 500 }); } const { messages }: { messages: UIMessage[] } = await req.json(); const relay = createOpenAICompatible({ name: 'blost', baseURL: 'https://b-lost.com/v1', apiKey, }); const result = streamText({ model: relay(model), messages: await convertToModelMessages(messages), abortSignal: req.signal, maxRetries: 0, }); return result.toUIMessageStreamResponse({ headers: { 'Cache-Control': 'no-cache, no-transform', 'X-Accel-Buffering': 'no', }, onError: () => 'Generation interrupted. Partial output may be incomplete.', }); } EOF cat > app/page.tsx <<'EOF' 'use client'; import { useState } from 'react'; import { useChat } from '@ai-sdk/react'; import { DefaultChatTransport } from 'ai'; const transport = new DefaultChatTransport({ api: '/api/chat' }); export default function ChatPage() { const [input, setInput] = useState(''); const [cancelled, setCancelled] = useState(false); const { messages, sendMessage, status, error, stop } = useChat({ transport, experimental_throttle: 50, }); const busy = status === 'submitted' || status === 'streaming'; return ( <main> {messages.map(message => ( <section key={message.id}> <strong>{message.role}</strong> {message.parts.map((part, index) => part.type === 'text' ? <pre key={index}>{part.text}</pre> : null )} </section> ))} <p role="status"> {error ? 'Connection or generation failed. Partial output is not final.' : cancelled ? 'Stopped locally. Partial output is not final.' : busy ? 'Receiving response…' : 'Ready'} </p> <form onSubmit={event => { event.preventDefault(); if (!input.trim() || busy) return; setCancelled(false); void sendMessage({ text: input }); setInput(''); }}> <input aria-label="Message" value={input} onChange={event => setInput(event.target.value)} /> <button disabled={busy || !input.trim()}>Send</button> <button type="button" disabled={!busy} onClick={() => { setCancelled(true); void stop(); }} > Stop </button> </form> </main> ); } EOF npm run dev This is a runnable transport baseline. Add session validation, request-size limits, and message sanitization before accepting external traffic.
Production Deviations
Clicking Stop cancels the client request. Passing req.signal upstream propagates that cancellation when the hosting adapter exposes disconnects correctly. This is not universal—a reverse proxy may keep the server-side request alive after the browser disconnects. Test cancellation through deployed ingress, not only against localhost.
Similarly, X-Accel-Buffering: no works for compatible Nginx configurations but cannot override every CDN buffering policy. If tokens arrive in one burst, compare upstream timestamps with browser timestamps to separate model latency from intermediary buffering.
The 50-millisecond render throttle reduces React update pressure during token bursts. It does not slow generation or repair blocked streams. Avoid reparsing entire growing Markdown documents on every token—rendering cost can make healthy connections appear frozen.
No Silent Restart After Visible Output
maxRetries: 0 makes the retry policy explicit. After visible output exists, silently launching another generation can duplicate text or produce a different answer. A resumed HTTP connection is not a resumed model execution.
The baseline retains partial output and labels detected failures. It does not provide durable stream resumption. A cleanly closed but semantically incomplete response may escape ordinary transport error detection. Stronger guarantees require persisted generation identifiers and explicit terminal-state reconciliation.
For IDE integrations, this limitation matters: incomplete assistant text must not authorize an edit. Keep action results separate from conversational progress. Require a confirmed terminal result before presenting work as complete.
The hardest operational question: When do you trust a stream enough to execute its instructions? The baseline above preserves failure signals but cannot prove semantic completeness. How do you reconcile streaming UX with the requirement that partial output never triggers destructive actions? What patterns have held up in your production agent integrations?