fix(agent-routes): return Vercel SSE Data Stream for agent chat#1084
fix(agent-routes): return Vercel SSE Data Stream for agent chat#1084
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…efault - agent-routes.ts: use streamChatWithTools + encodeVercelDataStream when stream !== false (default), matching general chat route behaviour - AiChatPanel.tsx: enhance error UI to surface SSE parse failures clearly - chatbot-features.test.ts: add SSE round-trip tests, update existing tests to use stream: false for JSON-only assertions - CHANGELOG.md: document the fix Agent-Logs-Url: https://github.com/objectstack-ai/framework/sessions/f011c46d-3fda-4917-9a19-c28e1ca50ba2 Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes the agent-specific chat route (/api/v1/ai/agents/:agentName/chat) so it returns a Vercel AI SDK v6-compatible streaming response by default (instead of always returning JSON), matching what the Studio chat UI transport expects.
Changes:
- Update agent chat route to default to streaming (
stream !== false) and usestreamChatWithTools()+encodeVercelDataStream(), with JSON fallback whenstream: false. - Update/extend route tests to cover default streaming, explicit streaming, and explicit JSON fallback.
- Improve Studio chat panel error UI messaging for stream/parse-related failures, and document the fix in the changelog.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
packages/services/service-ai/src/routes/agent-routes.ts |
Switch agent chat endpoint to streaming-by-default and encode stream in Vercel-compatible format, with JSON fallback. |
packages/services/service-ai/src/__tests__/chatbot-features.test.ts |
Update existing assertions to use stream: false where JSON is expected; add new streaming behavior tests. |
apps/studio/src/components/AiChatPanel.tsx |
Improve error rendering and add a hint for SSE/stream parsing failures. |
CHANGELOG.md |
Document the agent chat streaming fix and related Studio UI behavior improvement. |
| return { | ||
| status: 200, | ||
| stream: true, | ||
| vercelDataStream: true, | ||
| contentType: 'text/event-stream', | ||
| headers: { | ||
| 'Content-Type': 'text/event-stream', | ||
| 'Cache-Control': 'no-cache', | ||
| 'Connection': 'keep-alive', | ||
| 'x-vercel-ai-ui-message-stream': 'v1', | ||
| }, | ||
| events: encodeVercelDataStream(events), |
There was a problem hiding this comment.
The streaming response is returning contentType and headers, but RouteResponse (from routes/ai-routes.ts) doesn’t define those fields and the runtime HttpDispatcher.handleAI() streaming path ignores route-provided headers/contentType entirely (it constructs its own headers from vercelDataStream). Either remove these fields here to avoid dead/misleading data, or extend RouteResponse + update HttpDispatcher.handleAI() to propagate streaming headers (e.g., the x-vercel-ai-ui-message-stream header) when present.
| // | ||
| // Dual-mode endpoint matching the general chat route behaviour: | ||
| // • `stream !== false` → Vercel Data Stream Protocol (SSE) | ||
| // • `stream === false` → JSON response (legacy) | ||
| // | ||
| { | ||
| method: 'POST', | ||
| path: '/api/v1/ai/agents/:agentName/chat', | ||
| description: 'Chat with a specific AI agent', | ||
| description: 'Chat with a specific AI agent (supports Vercel AI Data Stream Protocol)', |
There was a problem hiding this comment.
The endpoint comments/description call this the “Vercel Data Stream Protocol (SSE)”, but the encoder being used (stream/vercel-stream-encoder.ts) explicitly implements the Vercel AI SDK v6 UI Message Stream Protocol (SSE data: frames with JSON payloads). Please align the wording here (and/or naming like vercelDataStream) to the actual wire protocol to avoid confusion when debugging transports.
Agent chat (
/api/v1/ai/agents/:agentName/chat) returned plain JSON while the Studio frontend expects Vercel AI SDK v6 UI Message Stream Protocol (SSE). Result: API responds correctly, butDefaultChatTransportcan't parse the response and the chat panel renders nothing.Backend —
agent-routes.tsstream !== false), matching the general chat routestreamChatWithTools()+encodeVercelDataStream()— same codepath as/api/v1/ai/chatstream: falseis explicitly setFrontend —
AiChatPanel.tsxShieldAlerticon with structured message instead of a flat stringTests
stream: true,stream: falseJSON fallbackstream: falsewhere they assert onresp.body