fix(webui): admit history without dropping live output (NAN-311)

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-22 01:22:49 +08:00
1 parent df63526ddf
commit b37a23698d
21 files changed
+3026 -1607

No files matched your search

+14 -13
View File
@@ -1,26 +1,27 @@
import { expect, it } from "vitest";
import { ThreadMessageCache } from "@/lib/thread-message-cache";
import { ThreadTranscript } from "@/lib/thread-transcript";
import type { UIMessage } from "@/lib/types";
import { createThreadCheckpoint } from "@/lib/thread-checkpoint";
const messages: UIMessage[] = [{ id: "u", role: "user", content: "hello", createdAt: 1 }];
const rows = createThreadCheckpoint(messages);
const rows: UIMessage[] = [{ id: "u", role: "user", content: "hello", createdAt: 1 }];
const snapshot = new ThreadTranscript(rows).save();
it("keeps recently visited chats within the entry budget", () => {
const cache = new ThreadMessageCache(() => false, 10000, 2);
cache.set("a", rows); cache.set("b", rows);
expect(cache.get("a")).toBe(rows);
cache.set("c", rows);
expect([cache.get("a"), cache.get("b"), cache.get("c")]).toEqual([rows, undefined, rows]);
cache.set("a", snapshot); cache.set("b", snapshot);
expect(cache.get("a")?.projection.messages).toBe(rows);
cache.set("c", snapshot);
expect([cache.get("a")?.projection.messages, cache.get("b"), cache.get("c")?.projection.messages])
.toEqual([rows, undefined, rows]);
});
it("bounds large replay snapshots while retaining temporary chats", () => {
const cache = new ThreadMessageCache((key) => key === "temporary", 1000, 2);
cache.set("temporary", rows);
cache.set("large", createThreadCheckpoint([{ ...messages[0], content: "x".repeat(1000) }]));
const cache = new ThreadMessageCache((key) => key === "temporary", 4096, 2);
cache.set("temporary", snapshot);
cache.set("large", new ThreadTranscript([{ ...rows[0], content: "x".repeat(4096) }]).save());
expect(cache.get("large")).toBeUndefined();
expect(cache.get("temporary")).toBe(rows);
expect(cache.get("temporary")?.projection.messages).toBe(rows);
cache.delete("temporary");
cache.set("small", rows);
expect(cache.get("small")).toBe(rows);
cache.set("small", snapshot);
expect(cache.get("small")?.projection.messages).toBe(rows);
});