Repository navigation
Fix agentbot embedded chat streaming envelope - #17420
Conversation
The shared/embedded agent chat page (web/src/pages/agent/share) parses /api/v1/agentbots/<id>/completions with the same use-send-message.ts parser as the in-app agent chat and expects the agent canvas event envelope ({event, message_id, session_id, task_id, data}), terminated by data:[DONE].
The Go handler instead re-wrapped canvas events into the chatbot {code, data:{answer}} envelope and dropped node_*/message_end events, so the widget never received message/message_end frames and stayed stuck on "Thinking"/"searching" with an empty answer.
Forward canvas.RunEvent frames verbatim via WriteChatbotRunEvent — the same writer the in-app agent chat completions path uses — emit data:[DONE] at stream end, and remove the now-unused WriteAgentbotFrame/AgentbotSSEFrame alias. Update the handler tests to assert the agent envelope and accept both "data:" and "data: " SSE prefixes.
📝 WalkthroughWalkthroughAgent completion SSE handling now forwards canvas events directly, preserves telemetry, guarantees a terminal ChangesAgent SSE forwarding
Estimated code review effort: 3 (Moderate) | ~20 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
internal/handler/bot_test.go (1)
424-451: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCover
node_*forwarding.The fixture emits only
messageandmessage_end, so it cannot catch telemetry filtering—the stated regression this path must prevent. Add anode_*event and assert its canvas envelope before the terminal frame. As per coding guidelines, “Preserve valid behavior with focused tests.”🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/handler/bot_test.go` around lines 424 - 451, Extend the fixture used by the forwarding test around the frame assertions to emit a node_* event in addition to message and message_end. Assert that the node event is forwarded as a valid canvas envelope with its expected event and payload, and keep the terminal [DONE] assertion intact.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/handler/bot.go`:
- Around line 126-134: Remove compatibility, migration, and
alternate-implementation wording from the comments at internal/handler/bot.go
lines 126-134 and internal/handler/bot_test.go lines 401-405. In the bot.go
comment, retain only the current canvas envelope fields and data:[DONE]
termination requirements; in the bot_test.go comment, retain only the rationale
for asserting the expected canvas envelope. Make no code or stream-contract
changes.
- Line 172: Update the all-event forwarding path around WriteChatbotRunEvent so
events with ev.Type == "error" retain the canvas envelope, including event
metadata, IDs, and timestamps, instead of being converted to the reduced
{code,message,data:false} shape. Adjust the shared serialization logic to
preserve this envelope and add an assertion covering the serialized error frame.
---
Nitpick comments:
In `@internal/handler/bot_test.go`:
- Around line 424-451: Extend the fixture used by the forwarding test around the
frame assertions to emit a node_* event in addition to message and message_end.
Assert that the node event is forwarded as a valid canvas envelope with its
expected event and payload, and keep the terminal [DONE] assertion intact.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 3fd03fc3-7db3-4fbc-bedb-de896fdb1d2b
📒 Files selected for processing (3)
internal/handler/bot.gointernal/handler/bot_test.gointernal/service/bot_completion.go
💤 Files with no reviewable changes (1)
- internal/service/bot_completion.go
| // The shared/embedded agent chat page (web/src/pages/agent/share) | ||
| // parses the stream with the same use-send-message.ts parser as the | ||
| // in-app agent chat, so frames must carry the agent canvas envelope | ||
| // — {event, message_id, session_id, task_id, created_at, data} — | ||
| // terminated by `data:[DONE]`. Forwarding raw canvas.RunEvent via | ||
| // WriteChatbotRunEvent keeps this endpoint byte-compatible with the | ||
| // python canvas_service.completion output (which yields every | ||
| // canvas event, including node_* telemetry used by the "Thinking" | ||
| // log panel). |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Remove compatibility/migration framing from Go comments.
Keep the current stream contract, but describe it directly without byte-compatibility, old-envelope, or alternate-implementation comparisons.
internal/handler/bot.go#L126-L134: retain only the current canvas-envelope anddata:[DONE]requirements.internal/handler/bot_test.go#L401-L405: retain only the expected canvas-envelope assertion rationale.
As per coding guidelines, “Do not add new compatibility wording in comments or documentation.”
📍 Affects 2 files
internal/handler/bot.go#L126-L134(this comment)internal/handler/bot_test.go#L401-L405
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/handler/bot.go` around lines 126 - 134, Remove compatibility,
migration, and alternate-implementation wording from the comments at
internal/handler/bot.go lines 126-134 and internal/handler/bot_test.go lines
401-405. In the bot.go comment, retain only the current canvas envelope fields
and data:[DONE] termination requirements; in the bot_test.go comment, retain
only the rationale for asserting the expected canvas envelope. Make no code or
stream-contract changes.
Source: Coding guidelines
| if ev.Type == "done" { | ||
| doneSent = true | ||
| } | ||
| if err := service.WriteChatbotRunEvent(c.Writer, ev); err != nil { |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Preserve the canvas envelope for error events.
WriteChatbotRunEvent special-cases ev.Type == "error" into {code,message,data:false}, dropping event, IDs, and timestamps. Thus this new all-event forwarding path still sends error frames in a different protocol than the shared agent parser expects. Serialize errors through the canvas envelope too, and add an error-frame assertion.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/handler/bot.go` at line 172, Update the all-event forwarding path
around WriteChatbotRunEvent so events with ev.Type == "error" retain the canvas
envelope, including event metadata, IDs, and timestamps, instead of being
converted to the reduced {code,message,data:false} shape. Adjust the shared
serialization logic to preserve this envelope and add an assertion covering the
serialized error frame.
wangq8
left a comment
There was a problem hiding this comment.
LGTM. This is a clean fix that aligns the agentbot SSE stream with the canvas event envelope expected by the shared agent chat widget.
What works well:
- Forwards raw
canvas.RunEventframes directly viaWriteChatbotRunEvent, the same writer used by the in-app agent chat completions path — ensures byte-for-byte compatibility with the Pythoncanvas_service.completionoutput. WriteChatbotRunEventalready handlesdoneevents by emittingdata:[DONE]\n\n, so the explicitWriteDoneFramecall is correctly removed.- Defensive synthetic
doneevent emission when the stream ends without one — prevents hanging clients. - Clean removal of the now-unused
WriteAgentbotFrame/AgentbotSSEFramealiases. - Tests properly updated to assert the agent canvas envelope (
{event, message_id, session_id, data}) instead of the legacy chatbot envelope. parseBotSSEFramescorrectly handles bothdata:anddata:SSE prefix formats.
No regressions, no new issues introduced. ✅
Summary
The embedded/shared agent chat widget (agent "Embed into website") could not hold a conversation: messages stayed stuck on "Thinking"/"searching" with an empty answer.
Root cause: the embedded agent chat page (
web/src/pages/agent/share) posts to/api/v1/agentbots/<id>/completionsand parses the SSE stream with the sameuse-send-message.tsparser as the in-app agent chat, which expects the agent canvas event envelope ({event, message_id, session_id, task_id, data}) terminated bydata:[DONE]. The Go handler instead re-wrapped canvas events into the legacy chatbot envelope ({code, data:{answer}}) and droppednode_*/message_endevents, so the widget never receivedmessage/message_endframes.Fix:
AgentbotCompletionnow forwardscanvas.RunEventframes verbatim viaWriteChatbotRunEvent— the same writer the in-app agent chat completions path uses — emitsdata:[DONE]at stream end, and removes the now-unusedWriteAgentbotFrame/AgentbotSSEFramealias. This matches the Pythoncanvas_service.completionwire shape byte-for-byte. Handler tests updated to assert the agent envelope and to accept bothdata:anddata:SSE prefixes.