Skip to content

fix(agent-routes): return Vercel SSE Data Stream for agent chat#1084

Merged
hotlong merged 2 commits intomainfrom
copilot/fix-agent-chat-sse-format
Apr 8, 2026
Merged

fix(agent-routes): return Vercel SSE Data Stream for agent chat#1084
hotlong merged 2 commits intomainfrom
copilot/fix-agent-chat-sse-format

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

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, but DefaultChatTransport can't parse the response and the chat panel renders nothing.

Backend — agent-routes.ts

  • Default to SSE streaming (stream !== false), matching the general chat route
  • Uses streamChatWithTools() + encodeVercelDataStream() — same codepath as /api/v1/ai/chat
  • JSON fallback only when stream: false is explicitly set
// Before: always JSON
const result = await aiService.chatWithTools(fullMessages, opts);
return { status: 200, body: result };

// After: SSE by default, JSON opt-in
const wantStream = body.stream !== false;
if (wantStream) {
  const events = aiService.streamChatWithTools(fullMessages, opts);
  return {
    status: 200, stream: true, vercelDataStream: true,
    events: encodeVercelDataStream(events),
  };
}

Frontend — AiChatPanel.tsx

  • Enhanced error UI: shows ShieldAlert icon with structured message instead of a flat string
  • Detects SSE/parse-related errors and surfaces a contextual hint about expected wire format

Tests

  • 3 new tests: default SSE mode, explicit stream: true, stream: false JSON fallback
  • Existing agent route tests updated to pass stream: false where they assert on resp.body
  • All 282 tests pass

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 8, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
objectstack-play Ready Ready Preview, Comment Apr 8, 2026 6:53am
spec Ready Ready Preview, Comment Apr 8, 2026 6:53am

Request Review

…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>
Copilot AI changed the title [WIP] Fix Agent Chat to return Vercel SSE format fix(agent-routes): return Vercel SSE Data Stream for agent chat Apr 8, 2026
Copilot AI requested a review from hotlong April 8, 2026 06:52
@hotlong hotlong marked this pull request as ready for review April 8, 2026 07:27
Copilot AI review requested due to automatic review settings April 8, 2026 07:27
@github-actions github-actions bot added documentation Improvements or additions to documentation tests size/m labels Apr 8, 2026
@hotlong hotlong merged commit 7acb6d0 into main Apr 8, 2026
15 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 use streamChatWithTools() + encodeVercelDataStream(), with JSON fallback when stream: 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.

Comment on lines +162 to +173
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),
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +72 to +80
//
// 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)',
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/m tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AI] Studio/Agent聊天未返回Vercel SSE格式,导致AI聊天无内容显示

3 participants