Skip to content

Commit b3742b9

Browse files
committed
fix(ui): stream chat deltas incrementally
1 parent bcaf326 commit b3742b9

2 files changed

Lines changed: 152 additions & 1 deletion

File tree

‎ui/src/ui/controllers/chat.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,129 @@ describe("handleChatEvent", () => {
224224
expect(state.chatRunId).toBe("run-1");
225225
});
226226

227+
it("appends gateway deltaText when the cumulative snapshot matches the current prefix", () => {
228+
const state = createState({
229+
sessionKey: "main",
230+
chatRunId: "run-1",
231+
chatStream: "Live",
232+
});
233+
const payload: ChatEventPayload = {
234+
runId: "run-1",
235+
sessionKey: "main",
236+
state: "delta",
237+
deltaText: " reply",
238+
message: {
239+
role: "assistant",
240+
content: [{ type: "text", text: "Live reply" }],
241+
},
242+
};
243+
244+
expect(handleChatEvent(state, payload)).toBe("delta");
245+
expect(state.chatStream).toBe("Live reply");
246+
});
247+
248+
it("uses the cumulative snapshot when the first observed delta joins mid-stream", () => {
249+
const state = createState({
250+
sessionKey: "main",
251+
chatRunId: "run-1",
252+
chatStream: null,
253+
});
254+
const payload: ChatEventPayload = {
255+
runId: "run-1",
256+
sessionKey: "main",
257+
state: "delta",
258+
deltaText: " reply",
259+
message: {
260+
role: "assistant",
261+
content: [{ type: "text", text: "Live reply" }],
262+
},
263+
};
264+
265+
expect(handleChatEvent(state, payload)).toBe("delta");
266+
expect(state.chatStream).toBe("Live reply");
267+
});
268+
269+
it("appends gateway deltaText when no full message snapshot is present", () => {
270+
const state = createState({
271+
sessionKey: "main",
272+
chatRunId: "run-1",
273+
chatStream: "Live",
274+
});
275+
const payload: ChatEventPayload = {
276+
runId: "run-1",
277+
sessionKey: "main",
278+
state: "delta",
279+
deltaText: " reply",
280+
};
281+
282+
expect(handleChatEvent(state, payload)).toBe("delta");
283+
expect(state.chatStream).toBe("Live reply");
284+
});
285+
286+
it("uses the cumulative snapshot when a missed delta would make append stale", () => {
287+
const state = createState({
288+
sessionKey: "main",
289+
chatRunId: "run-1",
290+
chatStream: "Hello",
291+
});
292+
const payload: ChatEventPayload = {
293+
runId: "run-1",
294+
sessionKey: "main",
295+
state: "delta",
296+
deltaText: "!",
297+
message: {
298+
role: "assistant",
299+
content: [{ type: "text", text: "Hello world!" }],
300+
},
301+
};
302+
303+
expect(handleChatEvent(state, payload)).toBe("delta");
304+
expect(state.chatStream).toBe("Hello world!");
305+
});
306+
307+
it("uses the cumulative snapshot when a same-length missed replacement changes the prefix", () => {
308+
const state = createState({
309+
sessionKey: "main",
310+
chatRunId: "run-1",
311+
chatStream: "AB",
312+
});
313+
const payload: ChatEventPayload = {
314+
runId: "run-1",
315+
sessionKey: "main",
316+
state: "delta",
317+
deltaText: "E",
318+
message: {
319+
role: "assistant",
320+
content: [{ type: "text", text: "CDE" }],
321+
},
322+
};
323+
324+
expect(handleChatEvent(state, payload)).toBe("delta");
325+
expect(state.chatStream).toBe("CDE");
326+
});
327+
328+
it("replaces the stream when gateway deltaText marks a replacement", () => {
329+
const state = createState({
330+
sessionKey: "main",
331+
chatRunId: "run-1",
332+
chatStream: "Alpha beta",
333+
});
334+
const payload: ChatEventPayload = {
335+
runId: "run-1",
336+
sessionKey: "main",
337+
state: "delta",
338+
deltaText: "Alpha",
339+
replace: true,
340+
message: {
341+
role: "assistant",
342+
content: [{ type: "text", text: "ignored snapshot" }],
343+
},
344+
};
345+
346+
expect(handleChatEvent(state, payload)).toBe("delta");
347+
expect(state.chatStream).toBe("Alpha");
348+
});
349+
227350
it("adopts the run id for selected-session live deltas observed from another channel", () => {
228351
const state = createState({
229352
sessionKey: "agent:main:feishu:direct:peer-1",

‎ui/src/ui/controllers/chat.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ export type ChatEventPayload = {
310310
agentId?: string;
311311
state: "delta" | "final" | "aborted" | "error";
312312
message?: unknown;
313+
deltaText?: string;
314+
replace?: boolean;
313315
errorMessage?: string;
314316
};
315317

@@ -410,6 +412,32 @@ function maybeResetToolStream(state: ChatState) {
410412
}
411413
}
412414

415+
function resolveDeltaChatStreamText(
416+
currentStream: string | null,
417+
payload: ChatEventPayload,
418+
): string | null {
419+
const snapshot = payload.message == null ? null : extractText(payload.message);
420+
if (typeof payload.deltaText === "string") {
421+
if (payload.replace === true) {
422+
return payload.deltaText;
423+
}
424+
if (currentStream === null) {
425+
return typeof snapshot === "string" ? snapshot : payload.deltaText;
426+
}
427+
if (typeof snapshot === "string") {
428+
const prefixLength = snapshot.length - payload.deltaText.length;
429+
if (
430+
prefixLength !== currentStream.length ||
431+
snapshot.slice(0, prefixLength) !== currentStream
432+
) {
433+
return snapshot;
434+
}
435+
}
436+
return `${currentStream}${payload.deltaText}`;
437+
}
438+
return typeof snapshot === "string" ? snapshot : null;
439+
}
440+
413441
type InFlightChatHistoryRequest = {
414442
client: NonNullable<ChatState["client"]>;
415443
key: string;
@@ -942,7 +970,7 @@ export function handleChatEvent(state: ChatState, payload?: ChatEventPayload) {
942970
});
943971

944972
if (payload.state === "delta") {
945-
const next = extractText(payload.message);
973+
const next = resolveDeltaChatStreamText(state.chatStream, payload);
946974
if (
947975
typeof next === "string" &&
948976
!isSilentReplyStream(next) &&

0 commit comments

Comments
 (0)