fix: preserve rewritten stream snapshots in webchat (#58641) (thanks @neeravmakwana)

This commit is contained in:
Neerav Makwana
2026-04-01 01:39:19 -04:00
committed by GitHub
parent e1d963ed2e
commit 26a891aaeb
7 changed files with 45 additions and 11 deletions

View File

@@ -134,6 +134,25 @@ describe("handleChatEvent", () => {
expect(state.chatMessages).toEqual([]);
});
it("replaces the stream when a delta snapshot gets shorter", () => {
const state = createState({
sessionKey: "main",
chatRunId: "run-1",
chatStream: "Alpha beta",
});
const payload: ChatEventPayload = {
runId: "run-1",
sessionKey: "main",
state: "delta",
message: {
role: "assistant",
content: [{ type: "text", text: "Alpha" }],
},
};
expect(handleChatEvent(state, payload)).toBe("delta");
expect(state.chatStream).toBe("Alpha");
});
it("returns final for another run when payload has no message", () => {
const state = createActiveStreamingState();
const payload: ChatEventPayload = {

View File

@@ -295,10 +295,7 @@ export function handleChatEvent(state: ChatState, payload?: ChatEventPayload) {
if (payload.state === "delta") {
const next = extractText(payload.message);
if (typeof next === "string" && !isSilentReplyStream(next)) {
const current = state.chatStream ?? "";
if (!current || next.length >= current.length) {
state.chatStream = next;
}
state.chatStream = next;
}
} else if (payload.state === "final") {
const finalMessage = normalizeFinalAssistantMessage(payload.message);