Skip to content

Commit 2ec1a43

Browse files
authored
fix(studio): serialize SSE error events as valid JSON (Fixes agentic-ai-java#192) (agentic-ai-java#219)
1 parent 0db6311 commit 2ec1a43

7 files changed

Lines changed: 426 additions & 107 deletions

File tree

‎argi-studio/agent-chat-ui/src/lib/argi-api.test.mjs‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,117 @@ test("Studio settings are reachable from agent and graph workspaces", () => {
163163
assert.match(threadSource, /StudioSettingsButton/);
164164
assert.match(graphSource, /StudioSettingsButton/);
165165
});
166+
167+
function chunkedStreamResponse(chunks) {
168+
return new Response(
169+
new ReadableStream({
170+
start(controller) {
171+
for (const chunk of chunks) {
172+
controller.enqueue(new TextEncoder().encode(chunk));
173+
}
174+
controller.close();
175+
},
176+
}),
177+
{
178+
status: 200,
179+
headers: { "Content-Type": "text/event-stream" },
180+
},
181+
);
182+
}
183+
184+
const TRICKY_MESSAGE =
185+
'quote " backslash \\ path C:\\temp\\new tab\tcr\rlf\n unicode 中文 ✓';
186+
187+
for (const [label, method] of [
188+
["agent", "_processSSEStream"],
189+
["graph", "_processGraphSSEStream"],
190+
]) {
191+
test(`${label} SSE error with special characters round-trips`, async () => {
192+
installSessionStorage();
193+
const api = loadApiModule();
194+
const client = new api.default("http://studio");
195+
const data = JSON.stringify({
196+
error: true,
197+
errorType: "IllegalStateException",
198+
errorMessage: TRICKY_MESSAGE,
199+
});
200+
const response = streamResponse(`event:error\ndata:${data}\n\n`);
201+
202+
await assert.rejects(
203+
() => drain(client[method](response)),
204+
(error) => error.message === TRICKY_MESSAGE,
205+
);
206+
});
207+
208+
test(`${label} SSE malformed error event fails instead of ending normally`, async () => {
209+
installSessionStorage();
210+
const api = loadApiModule();
211+
const client = new api.default("http://studio");
212+
const response = streamResponse(
213+
'data:{"node":"a"}\n\nevent:error\ndata:{"error":true,"errorMessage":"bad\tjson\n\n',
214+
);
215+
216+
await assert.rejects(
217+
() => drain(client[method](response)),
218+
/malformed error event/,
219+
);
220+
});
221+
222+
test(`${label} SSE skips malformed data but keeps empty and normal events`, async () => {
223+
installSessionStorage();
224+
const api = loadApiModule();
225+
const client = new api.default("http://studio");
226+
const response = streamResponse(
227+
'data:{not json}\n\ndata:{}\n\ndata:{"node":"a"}\n\n',
228+
);
229+
230+
const values = await drain(client[method](response));
231+
assert.deepEqual(JSON.parse(JSON.stringify(values)), [{}, { node: "a" }]);
232+
});
233+
234+
test(`${label} SSE parses events split across network chunks`, async () => {
235+
installSessionStorage();
236+
const api = loadApiModule();
237+
const client = new api.default("http://studio");
238+
const data = JSON.stringify({ error: true, errorMessage: TRICKY_MESSAGE });
239+
const payload = `data:{"node":"a"}\r\n\r\nevent:error\r\ndata:${data}\r\n\r\n`;
240+
const bytes = new TextEncoder().encode(payload);
241+
const chunks = [];
242+
// Split into 3-byte slices to also cut through multi-byte UTF-8 characters.
243+
for (let i = 0; i < bytes.length; i += 3) {
244+
chunks.push(bytes.slice(i, i + 3));
245+
}
246+
const response = new Response(
247+
new ReadableStream({
248+
start(controller) {
249+
for (const chunk of chunks) {
250+
controller.enqueue(chunk);
251+
}
252+
controller.close();
253+
},
254+
}),
255+
{ status: 200, headers: { "Content-Type": "text/event-stream" } },
256+
);
257+
258+
const seen = [];
259+
await assert.rejects(
260+
async () => {
261+
for await (const value of client[method](response)) {
262+
seen.push(value);
263+
}
264+
},
265+
(error) => error.message === TRICKY_MESSAGE,
266+
);
267+
assert.deepEqual(JSON.parse(JSON.stringify(seen)), [{ node: "a" }]);
268+
});
269+
270+
test(`${label} SSE yields a final event without trailing newline`, async () => {
271+
installSessionStorage();
272+
const api = loadApiModule();
273+
const client = new api.default("http://studio");
274+
const response = chunkedStreamResponse(['data:{"node"', ':"last"}']);
275+
276+
const values = await drain(client[method](response));
277+
assert.deepEqual(JSON.parse(JSON.stringify(values)), [{ node: "last" }]);
278+
});
279+
}

‎argi-studio/agent-chat-ui/src/lib/argi-api.ts‎

Lines changed: 88 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,92 @@ function toStreamError(payload: StudioStreamError): Error | null {
205205
return new Error(message);
206206
}
207207

208+
/**
209+
* Reads a Studio SSE response and yields each parsed data payload. Error payloads,
210+
* and `error` events whose data cannot be parsed, are thrown so a failed stream is
211+
* never mistaken for a normal end. Malformed non-error data lines are skipped.
212+
*/
213+
async function* readStudioSSE<T>(
214+
response: Response,
215+
): AsyncGenerator<T, void, unknown> {
216+
const reader = response.body?.getReader();
217+
if (!reader) {
218+
throw new Error("Response body is not readable");
219+
}
220+
const decoder = new TextDecoder();
221+
let buffer = "";
222+
let eventName = "";
223+
224+
const parseLine = (rawLine: string): T | null => {
225+
const line = (
226+
rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine
227+
).trimStart();
228+
if (line === "") {
229+
// Blank line terminates the current event.
230+
eventName = "";
231+
return null;
232+
}
233+
if (line.startsWith("event:")) {
234+
eventName = line.slice(6).trim();
235+
return null;
236+
}
237+
if (!line.startsWith("data:")) {
238+
return null;
239+
}
240+
const data = line.slice(5).trim();
241+
if (!data) {
242+
return null;
243+
}
244+
let parsed: T & StudioStreamError;
245+
try {
246+
parsed = JSON.parse(data);
247+
} catch (e) {
248+
if (eventName === "error") {
249+
throw new Error(
250+
`Studio stream failed with a malformed error event: ${data}`,
251+
);
252+
}
253+
console.warn("Skipping malformed Studio SSE data line", e);
254+
return null;
255+
}
256+
const streamError = toStreamError(parsed);
257+
if (streamError) {
258+
throw streamError;
259+
}
260+
if (eventName === "error") {
261+
throw new Error("Studio stream failed");
262+
}
263+
return parsed;
264+
};
265+
266+
try {
267+
while (true) {
268+
const { done, value } = await reader.read();
269+
if (done) {
270+
buffer += decoder.decode();
271+
break;
272+
}
273+
buffer += decoder.decode(value, { stream: true });
274+
const lines = buffer.split("\n");
275+
buffer = lines.pop() || "";
276+
for (const line of lines) {
277+
const parsed = parseLine(line);
278+
if (parsed !== null) {
279+
yield parsed;
280+
}
281+
}
282+
}
283+
if (buffer) {
284+
const parsed = parseLine(buffer);
285+
if (parsed !== null) {
286+
yield parsed;
287+
}
288+
}
289+
} finally {
290+
reader.releaseLock();
291+
}
292+
}
293+
208294
class ArgiApiClient {
209295
private baseUrl: string;
210296

@@ -392,44 +478,7 @@ class ArgiApiClient {
392478
private async *_processGraphSSEStream(
393479
response: Response,
394480
): AsyncGenerator<GraphRunResponse, void, unknown> {
395-
const reader = response.body?.getReader();
396-
if (!reader) {
397-
throw new Error("Response body is not readable");
398-
}
399-
const decoder = new TextDecoder();
400-
let buffer = "";
401-
try {
402-
while (true) {
403-
const { done, value } = await reader.read();
404-
if (done) break;
405-
buffer += decoder.decode(value, { stream: true });
406-
const lines = buffer.split("\n");
407-
buffer = lines.pop() || "";
408-
for (const line of lines) {
409-
if (line.trim().startsWith("data:")) {
410-
const data = line.slice(5).trim();
411-
if (data) {
412-
try {
413-
const parsed = JSON.parse(data) as GraphRunResponse &
414-
StudioStreamError;
415-
const streamError = toStreamError(parsed);
416-
if (streamError) {
417-
throw streamError;
418-
}
419-
yield parsed as GraphRunResponse;
420-
} catch (e) {
421-
if (e instanceof SyntaxError) {
422-
continue;
423-
}
424-
throw e;
425-
}
426-
}
427-
}
428-
}
429-
}
430-
} finally {
431-
reader.releaseLock();
432-
}
481+
yield* readStudioSSE<GraphRunResponse>(response);
433482
}
434483

435484
// 获取会话列表
@@ -615,50 +664,7 @@ class ArgiApiClient {
615664
private async *_processSSEStream(
616665
response: Response,
617666
): AsyncGenerator<AgentRunResponse, void, unknown> {
618-
const reader = response.body?.getReader();
619-
if (!reader) {
620-
throw new Error("Response body is not readable");
621-
}
622-
623-
const decoder = new TextDecoder();
624-
let buffer = "";
625-
626-
try {
627-
while (true) {
628-
const { done, value } = await reader.read();
629-
if (done) {
630-
break;
631-
}
632-
633-
buffer += decoder.decode(value, { stream: true });
634-
const lines = buffer.split("\n");
635-
buffer = lines.pop() || "";
636-
637-
for (const line of lines) {
638-
if (line.trim().startsWith("data:")) {
639-
const data = line.slice(5).trim();
640-
if (data) {
641-
try {
642-
const agentResponse = JSON.parse(data) as AgentRunResponse &
643-
StudioStreamError;
644-
const streamError = toStreamError(agentResponse);
645-
if (streamError) {
646-
throw streamError;
647-
}
648-
yield agentResponse;
649-
} catch (e) {
650-
if (e instanceof SyntaxError) {
651-
continue;
652-
}
653-
throw e;
654-
}
655-
}
656-
}
657-
}
658-
}
659-
} finally {
660-
reader.releaseLock();
661-
}
667+
yield* readStudioSSE<AgentRunResponse>(response);
662668
}
663669
}
664670

‎argi-studio/src/main/java/io/github/agentic/ai/agent/studio/controller/ExecutionController.java‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -350,26 +350,7 @@ else if (nodeOutput instanceof InterruptionMetadata interruptionMetadata) {
350350
.onErrorResume(error -> {
351351
// Handle errors from the agent stream and convert to SSE error event
352352
log.error("Error occurred during agent stream execution", error);
353-
354-
// Create error response
355-
String errorMessage = error.getMessage() != null ? error.getMessage() : "Unknown error occurred";
356-
String errorType = error.getClass().getSimpleName();
357-
358-
try {
359-
// Create a structured error response
360-
String errorJson = String.format("{\"error\":true,\"errorType\":\"%s\",\"errorMessage\":\"%s\"}",
361-
errorType.replace("\"", "\\\""), errorMessage.replace("\"", "\\\"").replace("\n", "\\n"));
362-
363-
// Return the error as an SSE event and complete the stream
364-
return Flux.just(ServerSentEvent.<String>builder().event("error").data(errorJson).build());
365-
}
366-
catch (Exception e) {
367-
log.error("Failed to create error SSE event", e);
368-
return Flux.just(ServerSentEvent.<String>builder()
369-
.event("error")
370-
.data("{\"error\":true,\"errorMessage\":\"Internal error occurred\"}")
371-
.build());
372-
}
353+
return Flux.just(StreamErrorEvents.of(mapper, error));
373354
});
374355
}
375356

‎argi-studio/src/main/java/io/github/agentic/ai/agent/studio/controller/GraphExecutionController.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,7 @@ else if (nodeOutput instanceof InterruptionMetadata interruptionMetadata) {
179179
})
180180
.onErrorResume(error -> {
181181
log.error("Error occurred during graph stream execution", error);
182-
String errorMessage = error.getMessage() != null ? error.getMessage() : "Unknown error occurred";
183-
String errorType = error.getClass().getSimpleName();
184-
String errorJson = String.format("{\"error\":true,\"errorType\":\"%s\",\"errorMessage\":\"%s\"}",
185-
errorType.replace("\"", "\\\""), errorMessage.replace("\"", "\\\"").replace("\n", "\\n"));
186-
return Flux.just(ServerSentEvent.<String>builder().event("error").data(errorJson).build());
182+
return Flux.just(StreamErrorEvents.of(mapper, error));
187183
});
188184
}
189185

0 commit comments

Comments
 (0)