Skip to content

Fix agentbot embedded chat streaming envelope - #17420

Merged
wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix/agentbot-embedded-chat-sse
Jul 27, 2026
Merged

wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix/agentbot-embedded-chat-sse

Conversation

@euvre

@euvre euvre commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

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>/completions and parses the SSE stream with the same use-send-message.ts parser as the in-app agent chat, which 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 legacy chatbot envelope ({code, data:{answer}}) and dropped node_*/message_end events, so the widget never received message/message_end frames.

Fix: AgentbotCompletion now forwards canvas.RunEvent frames verbatim via WriteChatbotRunEvent — the same writer the in-app agent chat completions path uses — emits data:[DONE] at stream end, and removes the now-unused WriteAgentbotFrame/AgentbotSSEFrame alias. This matches the Python canvas_service.completion wire shape byte-for-byte. Handler tests updated to assert the agent envelope and to accept both data: and data: SSE prefixes.

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.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Jul 27, 2026
@euvre euvre added the ci Continue Integration label Jul 27, 2026
@coderabbitai

coderabbitai Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Agent completion SSE handling now forwards canvas events directly, preserves telemetry, guarantees a terminal done event, and updates tests for the shared agent-page envelope and flexible data: parsing.

Changes

Agent SSE forwarding

Layer / File(s) Summary
Direct canvas event forwarding
internal/handler/bot.go, internal/service/bot_completion.go
Agent completion forwards raw canvas events, emits a synthetic done event when necessary, documents the wire format, and removes the agent-specific SSE adapter.
SSE contract and parser validation
internal/handler/bot_test.go
Tests validate the canvas event envelope, message_end, [DONE], and optional spacing after data:.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: jinhai-cn

Poem

I’m a rabbit watching events stream,
Canvas frames hop by like a dream.
“Done” is ensured at journey’s end,
Tests check each message as they bend.
Sniff-sniff—SSE now speaks clear!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change to the agentbot streaming envelope.
Description check ✅ Passed The description matches the required Summary template and explains the problem, root cause, fix, and test updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@euvre
euvre requested review from wangq8 and yuzhichang and removed request for yuzhichang July 27, 2026 07:19

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
internal/handler/bot_test.go (1)

424-451: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover node_* forwarding.

The fixture emits only message and message_end, so it cannot catch telemetry filtering—the stated regression this path must prevent. Add a node_* 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

📥 Commits

Reviewing files that changed from the base of the PR and between 3065a29 and c4c9972.

📒 Files selected for processing (3)
  • internal/handler/bot.go
  • internal/handler/bot_test.go
  • internal/service/bot_completion.go
💤 Files with no reviewable changes (1)
  • internal/service/bot_completion.go

Comment thread internal/handler/bot.go
Comment on lines +126 to +134
// 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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 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 and data:[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

Comment thread internal/handler/bot.go
if ev.Type == "done" {
doneSent = true
}
if err := service.WriteChatbotRunEvent(c.Writer, ev); err != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 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 wangq8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.RunEvent frames directly via WriteChatbotRunEvent, the same writer used by the in-app agent chat completions path — ensures byte-for-byte compatibility with the Python canvas_service.completion output.
  • WriteChatbotRunEvent already handles done events by emitting data:[DONE]\n\n, so the explicit WriteDoneFrame call is correctly removed.
  • Defensive synthetic done event emission when the stream ends without one — prevents hanging clients.
  • Clean removal of the now-unused WriteAgentbotFrame / AgentbotSSEFrame aliases.
  • Tests properly updated to assert the agent canvas envelope ({event, message_id, session_id, data}) instead of the legacy chatbot envelope.
  • parseBotSSEFrames correctly handles both data: and data: SSE prefix formats.

No regressions, no new issues introduced. ✅

@wangq8
wangq8 merged commit 30b40ca into infiniflow:main Jul 27, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants