fix: agent chat completions can not use (#16570)

### Summary

As title
<img width="2370" height="2039" alt="image"
src="https://github.com/user-attachments/assets/4cccf543-3908-49ee-8101-c5068fbf53ec"
/>
This commit is contained in:
Haruko386
2026-07-03 13:25:14 +08:00
committed by GitHub
parent e65bac238e
commit 383d059969
7 changed files with 353 additions and 71 deletions

View File

@@ -1094,19 +1094,14 @@ func (h *AgentHandler) AgentChatCompletions(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
// SSE wire format is the unified python envelope used by both
// /api/v1/agents/chat/completions and /api/v1/agentbots/<id>/completions.
// One frame per canvas event, all routed through
// service.WriteChatbotRunEvent so the two paths share one writer
// and one shape — see internal/service/bot_completion.go for the
// frame definition. The same unified envelope is used by the
// /api/v1/agents/{canvas_id}/run and /api/v1/agentbots/<id>/completions
// endpoints, all going through service.WriteChatbotRunEvent. The
// channel close is signalled by `data: [DONE]\n\n`. We do NOT emit
// an SSE `event:` line — the front-end's `use-send-message.ts`
// SSE wire format is the flat Python agent-canvas envelope:
// {event,message_id,task_id,session_id,created_at,data}. One
// frame is emitted per canvas event through service.WriteChatbotRunEvent.
// The channel close is signalled by `data: [DONE]\n\n`. We do NOT
// emit an SSE `event:` line — the front-end's use-send-message.ts
// parser feeds each `data:` line directly into JSON.parse and
// breaks on the `e` of `event:` (browser console: "SyntaxError:
// Unexpected token 'e', \"event: mes\"…").
// expects the event type in the JSON object's top-level `event`
// field.
emitted := false
for ev := range events {
emitted = true