Skip to content

Support Console/Agent "try it" page for SSE streaming - #1499

Open
RavinduWeerakoon wants to merge 2 commits into
wso2:mainfrom
RavinduWeerakoon:console-stream-fix
Open

Support Console/Agent "try it" page for SSE streaming#1499
RavinduWeerakoon wants to merge 2 commits into
wso2:mainfrom
RavinduWeerakoon:console-stream-fix

Conversation

@RavinduWeerakoon

@RavinduWeerakoon RavinduWeerakoon commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Purpose

The current console "Try it" page does not support interaction with SSE streaming agents. Although the response content is received successfully by the frontend the UI does not display or render the streaming output correctly. This PR tries to resolve that issue

Goals

Approach

Screen.Recording.2026-08-05.at.10.42.06.mov

when recieved response is different from the expected one

image

User stories

Summary of user stories addressed by this change>

Release note

Brief description of the new feature or bug fix as it will appear in the release notes

Documentation

Link(s) to product documentation that addresses the changes of this PR. If no doc impact, enter �N/A� plus brief explanation of why there�s no doc impact

Training

Link to the PR for changes to the training content in https://github.com/wso2/WSO2-Training, if applicable

Certification

Type �Sent� when you have provided new/updated certification questions, plus four answers for each question (correct answer highlighted in bold), based on this change. Certification questions/answers should be sent to certification@wso2.com and NOT pasted in this PR. If there is no impact on certification exams, type �N/A� and explain why.

Marketing

Link to drafts of marketing content that will describe and promote this feature, including product page changes, technical articles, blog posts, videos, etc., if applicable

Automation tests

  • Unit tests

    Code coverage information

  • Integration tests

    Details about the test cases and coverage

Security checks

Samples

Provide high-level details about the samples related to this feature

Related PRs

List any other related PRs

Migrations (if applicable)

Describe migration steps and platforms on which migration has been tested

Test environment

List all JDK versions, operating systems, databases, and browser/versions on which this feature/fix was tested

Learning

Describe the research phase and any blog posts, patterns, libraries, or add-ons you used to solve the problem.

Fixes #1446

Summary by CodeRabbit

  • New Features

    • Agent Chat now displays responses as they stream in real time.
    • Added a Stop button to cancel an in-progress response while preserving partial output.
    • Added clearer handling for incomplete, malformed, or unsupported responses, including helpful hints and notifications.
  • Bug Fixes

    • Improved chat behavior for interrupted streams and invalid response formats.
    • Enhanced loading and message display states during streamed replies.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@RavinduWeerakoon, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 49 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d309a751-249a-40c1-85a4-d6ab77892c81

📥 Commits

Reviewing files that changed from the base of the PR and between 25d3297 and ef7902c.

📒 Files selected for processing (2)
  • console/workspaces/pages/test/src/AgentTest/AgentChat.test.tsx
  • console/workspaces/pages/test/src/AgentTest/AgentChat.tsx
📝 Walkthrough

Walkthrough

AgentChat now supports SSE streaming, incremental assistant-message updates, request cancellation, and response-format validation. New SSE utilities parse and validate stream payloads. Tests cover buffered responses, streaming, aborts, malformed data, hints, and snackbar notifications.

Changes

Agent streaming chat

Layer / File(s) Summary
SSE parsing utilities
console/workspaces/pages/test/src/AgentTest/utils/sse.ts, console/workspaces/pages/test/src/AgentTest/utils/sse.test.ts
Added typed stream chunks, SSE event buffering, multiline data handling, sentinel handling, payload validation, and utility tests.
AgentChat streaming flow
console/workspaces/pages/test/src/AgentTest/AgentChat.tsx
Added SSE response handling, incremental message updates, abort control, format validation, snackbar notifications, and Send/Stop control switching.
AgentChat behavior coverage
console/workspaces/pages/test/src/AgentTest/AgentChat.test.tsx
Added tests for buffered replies, streaming accumulation, stream cancellation, malformed responses, fallback hints, and snackbar notifications.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AgentChat
  participant fetch
  participant readSSEStream
  participant parseStreamChunk
  AgentChat->>fetch: Send request with abort signal
  fetch-->>AgentChat: Return SSE response
  AgentChat->>readSSEStream: Read response body
  readSSEStream-->>AgentChat: Yield event payload
  AgentChat->>parseStreamChunk: Parse payload
  parseStreamChunk-->>AgentChat: Return validated chunk
  AgentChat->>AgentChat: Append text to assistant message
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description states the purpose but leaves most required sections incomplete, including goals, approach details, testing, security checks, and release information. Complete the required template sections with implementation details, user stories, release notes, documentation impact, tests, security checks, and test environments.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding SSE streaming support to the Console/Agent Try It page.
Linked Issues check ✅ Passed The implementation adds SSE parsing, streamed message rendering, cancellation, error handling, and tests that directly address issue #1446.
Out of Scope Changes check ✅ Passed The changes are limited to AgentChat SSE support, related stream utilities, and focused tests, all of which align with issue #1446.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@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 (4)
console/workspaces/pages/test/src/AgentTest/utils/sse.ts (2)

44-67: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Handle CRLF event delimiters.

The event boundary search matches only "\n\n". The SSE specification also allows "\r\n\r\n" and "\r\r". If the agent gateway emits CRLF line endings, no boundary is found during streaming. All events are then flushed as one merged payload at line 61, and parseStreamChunk returns null for it. Normalize line endings before the boundary search.

♻️ Proposed fix to normalize line endings
-      buffer += decoder.decode(value, { stream: true });
+      buffer += decoder.decode(value, { stream: true }).replace(/\r\n?/g, "\n");
       let boundary = buffer.indexOf("\n\n");
🤖 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 `@console/workspaces/pages/test/src/AgentTest/utils/sse.ts` around lines 44 -
67, Update the SSE parsing loop around buffer and boundary to normalize CRLF and
CR line endings to LF before searching for event delimiters. Ensure "\r\n\r\n"
and "\r\r" are recognized as "\n\n", while preserving the existing
extractDataField and trailing-event handling.

89-106: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Validate the elements of content.

parseStreamChunk checks only that content is an array. A payload such as {"node":"answer","content":[null]} passes validation. The consumer in AgentChat.tsx then reads part.type, which throws a TypeError. The catch block converts that into an error alert instead of the format hint. Validate that each element is an object.

♻️ Proposed fix to validate content elements
     if (
       parsed &&
       typeof parsed === "object" &&
       typeof parsed.node === "string" &&
-      Array.isArray(parsed.content)
+      Array.isArray(parsed.content) &&
+      parsed.content.every(
+        (part: unknown) => typeof part === "object" && part !== null,
+      )
     ) {
🤖 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 `@console/workspaces/pages/test/src/AgentTest/utils/sse.ts` around lines 89 -
106, Update parseStreamChunk to validate every element of parsed.content as a
non-null object before returning the StreamChunk; reject the payload with null
or non-object elements by returning null, while preserving the existing node and
array checks.
console/workspaces/pages/test/src/AgentTest/AgentChat.tsx (1)

85-86: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Abort the active request on unmount.

abortControllerRef is aborted only by the Stop button. If the user navigates away during a stream, the fetch and the reader loop continue, and handleStreamingResponse keeps calling setMessages on an unmounted component. Add a cleanup effect that aborts the controller.

♻️ Proposed cleanup effect
   const [isStreaming, setIsStreaming] = useState(false);
   const abortControllerRef = useRef<AbortController | null>(null);
+
+  useEffect(() => {
+    return () => abortControllerRef.current?.abort();
+  }, []);
🤖 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 `@console/workspaces/pages/test/src/AgentTest/AgentChat.tsx` around lines 85 -
86, In the AgentChat component, add an unmount cleanup effect that checks
abortControllerRef and aborts the active AbortController when the component is
removed. Keep the existing Stop-button behavior unchanged and ensure the cleanup
prevents the streaming request and reader loop from continuing after unmount.
console/workspaces/pages/test/src/AgentTest/AgentChat.test.tsx (1)

88-91: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Restore stubbed globals after each test.

Each test replaces the global fetch with vi.stubGlobal. No test restores it. Add afterEach(() => vi.unstubAllGlobals()) so a stub cannot leak into later tests in this file.

🤖 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 `@console/workspaces/pages/test/src/AgentTest/AgentChat.test.tsx` around lines
88 - 91, Add an afterEach cleanup alongside the existing beforeEach in AgentChat
tests to call vi.unstubAllGlobals(), ensuring fetch and any other stubbed
globals are restored after every test while preserving the current mock reset
behavior.
🤖 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 `@console/workspaces/pages/test/src/AgentTest/AgentChat.test.tsx`:
- Around line 256-258: Update the hint assertion in the AgentChat test to match
the actual streaming text configured by AgentChat.tsx, including the SSE data
structure after “Expected an SSE data:”. Remove the incorrect “line” substring
while preserving the case-insensitive waitFor assertion.

In `@console/workspaces/pages/test/src/AgentTest/AgentChat.tsx`:
- Around line 329-332: Update the fallback branch of the responseText assignment
in AgentChat so it serializes the full responseData object rather than
responseData.result, preserving the existing formatting and hint suffix. Keep
the response string path unchanged.

---

Nitpick comments:
In `@console/workspaces/pages/test/src/AgentTest/AgentChat.test.tsx`:
- Around line 88-91: Add an afterEach cleanup alongside the existing beforeEach
in AgentChat tests to call vi.unstubAllGlobals(), ensuring fetch and any other
stubbed globals are restored after every test while preserving the current mock
reset behavior.

In `@console/workspaces/pages/test/src/AgentTest/AgentChat.tsx`:
- Around line 85-86: In the AgentChat component, add an unmount cleanup effect
that checks abortControllerRef and aborts the active AbortController when the
component is removed. Keep the existing Stop-button behavior unchanged and
ensure the cleanup prevents the streaming request and reader loop from
continuing after unmount.

In `@console/workspaces/pages/test/src/AgentTest/utils/sse.ts`:
- Around line 44-67: Update the SSE parsing loop around buffer and boundary to
normalize CRLF and CR line endings to LF before searching for event delimiters.
Ensure "\r\n\r\n" and "\r\r" are recognized as "\n\n", while preserving the
existing extractDataField and trailing-event handling.
- Around line 89-106: Update parseStreamChunk to validate every element of
parsed.content as a non-null object before returning the StreamChunk; reject the
payload with null or non-object elements by returning null, while preserving the
existing node and array checks.
🪄 Autofix

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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e3ad5d5d-bca6-4f3a-a15f-492994320e1a

📥 Commits

Reviewing files that changed from the base of the PR and between 892f7b9 and 25d3297.

📒 Files selected for processing (4)
  • console/workspaces/pages/test/src/AgentTest/AgentChat.test.tsx
  • console/workspaces/pages/test/src/AgentTest/AgentChat.tsx
  • console/workspaces/pages/test/src/AgentTest/utils/sse.test.ts
  • console/workspaces/pages/test/src/AgentTest/utils/sse.ts

Comment thread console/workspaces/pages/test/src/AgentTest/AgentChat.test.tsx
Comment thread console/workspaces/pages/test/src/AgentTest/AgentChat.tsx Outdated
@@ -0,0 +1,263 @@
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).

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.

Update license header year

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.

Fix in the other files as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Improvement]: Support Try it for streaming agents

2 participants