fix(webui): cache and defer thread history
This commit is contained in:
1 parent
7ba60c2ef0
commit
a490cd629b
30 files changed
+1306
-145
No files matched your search
@@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { sessionTitle, useSessionHistory, useSessions } from "@/hooks/useSessions";
|
||||
import * as api from "@/lib/api";
|
||||
import { webuiThreadCache } from "@/lib/webui-thread-cache";
|
||||
import { ClientProvider } from "@/providers/ClientProvider";
|
||||
|
||||
vi.mock("@/lib/api", async (importOriginal) => {
|
||||
@@ -93,6 +94,7 @@ describe("useSessions", () => {
|
||||
vi.mocked(api.listSessions).mockReset();
|
||||
vi.mocked(api.deleteSession).mockReset();
|
||||
vi.mocked(api.fetchWebuiThread).mockReset();
|
||||
webuiThreadCache.clear();
|
||||
});
|
||||
|
||||
it("does not use low-information greetings as fallback session titles", () => {
|
||||
@@ -566,6 +568,70 @@ describe("useSessions", () => {
|
||||
expect(result.current.messages[2]!.content).toBe("summary");
|
||||
});
|
||||
|
||||
it("shows a cached transcript immediately while revalidating it", async () => {
|
||||
const cached = {
|
||||
schemaVersion: 3,
|
||||
revision: "rev-cached",
|
||||
messages: [
|
||||
{ id: "a1", role: "assistant" as const, content: "cached answer", createdAt: 1 },
|
||||
],
|
||||
};
|
||||
vi.mocked(api.fetchWebuiThread).mockResolvedValueOnce(cached);
|
||||
|
||||
const first = renderHook(() => useSessionHistory("websocket:cached"), {
|
||||
wrapper: wrap(fakeClient()),
|
||||
});
|
||||
await waitFor(() => expect(first.result.current.loading).toBe(false));
|
||||
first.unmount();
|
||||
|
||||
vi.mocked(api.fetchWebuiThread).mockImplementationOnce(() => new Promise(() => {}));
|
||||
const second = renderHook(() => useSessionHistory("websocket:cached"), {
|
||||
wrapper: wrap(fakeClient()),
|
||||
});
|
||||
|
||||
expect(second.result.current.loading).toBe(false);
|
||||
expect(second.result.current.messages[0]?.content).toBe("cached answer");
|
||||
expect(api.fetchWebuiThread).toHaveBeenLastCalledWith(
|
||||
"tok",
|
||||
"websocket:cached",
|
||||
expect.objectContaining({
|
||||
limit: 40,
|
||||
direction: "latest",
|
||||
revision: "rev-cached",
|
||||
cached,
|
||||
}),
|
||||
);
|
||||
second.unmount();
|
||||
});
|
||||
|
||||
it("keeps rendered history visible when its LRU entry was evicted before refresh", async () => {
|
||||
const loaded = {
|
||||
schemaVersion: 3,
|
||||
revision: "rev-loaded",
|
||||
messages: [
|
||||
{ id: "a1", role: "assistant" as const, content: "still visible", createdAt: 1 },
|
||||
],
|
||||
};
|
||||
vi.mocked(api.fetchWebuiThread)
|
||||
.mockResolvedValueOnce(loaded)
|
||||
.mockImplementationOnce(() => new Promise(() => {}));
|
||||
|
||||
const { result, unmount } = renderHook(
|
||||
() => useSessionHistory("websocket:evicted"),
|
||||
{ wrapper: wrap(fakeClient()) },
|
||||
);
|
||||
await waitFor(() => expect(result.current.loading).toBe(false));
|
||||
expect(result.current.messages[0]?.content).toBe("still visible");
|
||||
|
||||
webuiThreadCache.delete("websocket:evicted");
|
||||
act(() => result.current.refresh());
|
||||
await waitFor(() => expect(api.fetchWebuiThread).toHaveBeenCalledTimes(2));
|
||||
|
||||
expect(result.current.loading).toBe(false);
|
||||
expect(result.current.messages[0]?.content).toBe("still visible");
|
||||
unmount();
|
||||
});
|
||||
|
||||
it("flags transcript ending with a trace row as pending", async () => {
|
||||
vi.mocked(api.fetchWebuiThread).mockResolvedValue({
|
||||
schemaVersion: 3,
|
||||
@@ -785,7 +851,7 @@ describe("useSessions", () => {
|
||||
"tok",
|
||||
"websocket:paged",
|
||||
expect.objectContaining({
|
||||
limit: 80,
|
||||
limit: 40,
|
||||
direction: "latest",
|
||||
signal: expect.any(AbortSignal),
|
||||
}),
|
||||
|
||||
Reference in new issue
Block a user