feat: Stream AI responses in real-time via Route Handler#194
Open
feat: Stream AI responses in real-time via Route Handler#194
Conversation
- Add app/lib/ai.ts with generateContentStream() for Gemini/OpenRouter - Add createChatOnly() and addMessagesAndDiffs() to chatHistory.ts - Create app/api/chat/route.ts Route Handler with NDJSON streaming - Create app/(docs)/streamingChatContext.tsx for real-time streaming state - Update layout.tsx to include StreamingChatProvider - Update chatForm.tsx to use fetch() + stream reading instead of server action - Update chatArea.tsx to display streaming content in real-time Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
my-code | 7c16a3e | Commit Preview URL Branch Preview URL |
Mar 18 2026, 12:04 PM |
Copilot
AI
changed the title
[WIP] Add streaming responses for AI chat
feat: Stream AI responses in real-time via Route Handler
Mar 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AI responses previously blocked until fully generated before rendering. This replaces the
askAIServer Action with a streaming Route Handler, navigating to the chat page as soon as the AI returns the targetsectionIdand title, then displaying the response token-by-token.Architecture
Streaming protocol (NDJSON over
POST /api/chat):{"type":"chat","chatId":"...","sectionId":"..."}— emitted once the first two lines (sectionId + title) are parsed; triggers DB chat creation and client navigation{"type":"chunk","text":"..."}— streamed for each AI token{"type":"done"}— emitted after messages/diffs are persisted to DBTwo-phase DB write (
app/lib/chatHistory.ts):createChatOnly()— inserts the chat row early so navigation can happen immediatelyaddMessagesAndDiffs()— persists messages and parsed diffs after streaming completes, then revalidates cacheClient-side streaming state (
app/(docs)/streamingChatContext.tsx):StreamingChatProviderlives in the(docs)layout, so it survives the parallel-route navigation from@docsto@chatchatForm.tsxstarts a backgroundReadableStreamreader; on thechatevent it callsstartStreaming(chatId, userQuestion)and immediately navigateschatArea.tsxrenders from streaming context whileisStreamingThisis true (showing aloading-dotsindicator), then callsrouter.refresh()on completion and clears context once DB data is availableAI streaming utility (
app/lib/ai.ts):generateContentStream()— async generator wrapping GeminigenerateContentStreamand OpenRouter SSE (stream: true), replacing the non-streaminggenerateContentused previously in server actionsWarning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
sparrow.cloudflare.com/opt/hostedtoolcache/node/24.14.0/x64/bin/node /opt/hostedtoolcache/node/24.14.0/x64/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/my-code/my-code/node_modules/wrangler/wrangler-dist/cli.js types --env-interface CloudflareEnv cloudflare-env.d.ts(dns block)workers.cloudflare.com/opt/hostedtoolcache/node/24.14.0/x64/bin/node /opt/hostedtoolcache/node/24.14.0/x64/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/my-code/my-code/node_modules/wrangler/wrangler-dist/cli.js types --env-interface CloudflareEnv cloudflare-env.d.ts(dns block)/home/REDACTED/work/_temp/ghcca-node/node/bin/node node /home/REDACTED/work/my-code/my-code/node_modules/.bin/next build(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.