This is an automated email from the ASF dual-hosted git repository.
lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new 654449d0f fix(ai): parse mixed SSE line endings (#5185)
654449d0f is described below
commit 654449d0fb5cfc3f14aeaa8994b1d8e26677e21f
Author: Loyal-Young <[email protected]>
AuthorDate: Fri Nov 27 11:53:39 2026 +0800
fix(ai): parse mixed SSE line endings (#5185)
---
web/src/api/ai.test.ts | 24 ++++++++++++++++++++++++
web/src/api/ai.ts | 4 +++-
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/web/src/api/ai.test.ts b/web/src/api/ai.test.ts
index 3ddb6838a..207baf7a9 100644
--- a/web/src/api/ai.test.ts
+++ b/web/src/api/ai.test.ts
@@ -313,6 +313,30 @@ describe('AI API', () => {
expect(events).toEqual([{ type: 'text_delta', content: 'hello' }]);
});
+ it.each(['\r\n\n', '\n\r\n', '\r\r\n'])(
+ 'dispatches a frame with mixed SSE line endings %j',
+ async (boundary) => {
+ const frame = `event: agent\ndata: ${JSON.stringify({ type:
'text_delta', content: 'hello' })}${boundary}`;
+ vi.stubGlobal('fetch',
vi.fn().mockResolvedValue(eventStreamResponse([frame + doneFrame()])));
+ const events: ChatSseEvent[] = [];
+
+ await openRunStream(7, body, { onEvent: (event) => events.push(event)
});
+
+ expect(events).toEqual([{ type: 'text_delta', content: 'hello' }]);
+ },
+ );
+
+ it('does not treat one CRLF as a blank line between frames', async () => {
+ const stream =
+ 'event: agent\r\ndata: {"type":"text_delta","content":"hello"}\r\n' +
+ 'event: done\r\ndata: {}\r\n\r\n';
+ vi.stubGlobal('fetch',
vi.fn().mockResolvedValue(eventStreamResponse([stream])));
+
+ await expect(openRunStream(7, body, { onEvent: vi.fn()
})).rejects.toMatchObject({
+ code: 'llm.stream.malformed_event',
+ } satisfies Partial<AiStreamError>);
+ });
+
it('dispatchesMultipleAgentFramesDeliveredInOneChunkTest', async () => {
vi.stubGlobal(
'fetch',
diff --git a/web/src/api/ai.ts b/web/src/api/ai.ts
index 8ea339d8d..63c00ae19 100644
--- a/web/src/api/ai.ts
+++ b/web/src/api/ai.ts
@@ -103,7 +103,9 @@ export class AiStreamError extends Error {
}
function getEventBoundary(buffer: string): { index: number; length: number } |
null {
- const match = /\r\n\r\n|\n\n|\r\r/.exec(buffer);
+ // Each of the two line endings may independently be CRLF, LF, or CR. Do not
split
+ // a single CRLF into two endings when looking for the blank line.
+ const match = /(?:\r\n|\r(?!\n)|\n){2}/.exec(buffer);
return match ? { index: match.index, length: match[0].length } : null;
}