perf(webui): reduce repeated work and bound history rendering (#5703)

* perf(webui): reduce repeated work and bound history rendering

* fix(webui): keep continuation reasoning with its answer

* refactor(webui): reuse answer buffers and prepared copy text

* perf(webui): restore conditional replay buffer initialization

* fix(webui): remove code block pagination
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-09 14:02:26 +08:00
1 parent 1c451b5b5f
commit 1469d4811c
32 files changed
+1156 -361

No files matched your search

+66 -6
View File
@@ -3,6 +3,7 @@ import type { ReactNode } from "react";
import { describe, expect, it, vi } from "vitest";
import { useNanobotStream } from "@/hooks/useNanobotStream";
import { ThreadVisibilityContext } from "@/hooks/useThreadVisibility";
import { normalizeActivityTimeline } from "@/lib/activity-timeline";
import type { StreamError } from "@/lib/nanobot-client";
import type {
@@ -344,6 +345,30 @@ describe("useNanobotStream", () => {
}
});
it("buffers a hidden chat view and flushes its ordered deltas on return", () => {
vi.useFakeTimers();
const fake = fakeClient();
const Client = wrap(fake.client);
let visible = false;
const { result, rerender, unmount } = renderHook(() => useNanobotStream("hidden-view", EMPTY_MESSAGES), {
wrapper: ({ children }) => <Client><ThreadVisibilityContext.Provider value={visible}>{children}</ThreadVisibilityContext.Provider></Client>,
});
try {
act(() => {
for (let seq = 1; seq <= 100; seq++) fake.emit("hidden-view", {
event: "delta", chat_id: "hidden-view", text: "x", turn_id: "turn", turn_seq: seq,
});
});
expect(result.current.messages).toHaveLength(0);
visible = true;
rerender();
expect(result.current.messages[0]).toMatchObject({ content: "x".repeat(100), turnSeq: 100 });
} finally {
unmount();
vi.useRealTimers();
}
});
it("keeps the turn pending on disconnect without breaking a resumed stream", async () => {
const fake = fakeClient();
const { result } = renderHook(
@@ -3409,18 +3434,53 @@ describe("useNanobotStream", () => {
describe("live/replay projection before canonical-event revision migration", () => {
it.each(PROJECTION_FIXTURE_CASES)("matches the shared $name fixture", (fixtureCase) => {
// Keep client-only elapsed-time estimates out of the transport contract.
const clock = vi.spyOn(Date, "now").mockReturnValue(0);
try {
const fake = fakeClient();
const { result } = renderHook(
() => useNanobotStream(fixtureCase.chat_id, fixtureCase.initial_messages),
{ wrapper: wrap(fake.client) },
);
for (const event of fixtureCase.live_events) {
act(() => {
fake.emit(fixtureCase.chat_id, event);
});
}
expect(normalizeProjection(result.current.messages)).toEqual(fixtureCase.expected);
} finally {
clock.mockRestore();
}
});
it.each([true, false])("keeps continuation reasoning before its answer across frames (text snapshots: %s)", async (snapshots) => {
const fixture = PROJECTION_FIXTURE_CASES.find(
(candidate) => candidate.name === "length_continuation_reasoning_then_new_recovery_turn",
)!;
const fake = fakeClient();
const { result } = renderHook(
() => useNanobotStream(fixtureCase.chat_id, fixtureCase.initial_messages),
() => useNanobotStream(fixture.chat_id, fixture.initial_messages),
{ wrapper: wrap(fake.client) },
);
for (const event of fixtureCase.live_events) {
for (const event of fixture.live_events) {
act(() => {
fake.emit(fixtureCase.chat_id, event);
fake.emit(fixture.chat_id, event.event === "stream_end" && !snapshots
? { ...event, text: undefined }
: event);
});
await flushStreamFrame();
}
expect(normalizeProjection(result.current.messages)).toEqual(fixtureCase.expected);
const units = normalizeActivityTimeline(result.current.messages);
expect(units.map((unit) => unit.type === "activity"
? unit.messages.map((message) => message.reasoning).join("")
: unit.message.content)).toEqual([
"Write long code.",
"Plan.\n\nContinue code.\n\nFinish code.",
"```python\nfirst\nsecond",
"Recover.\n\nContinue recovery.",
"Recovered answer.",
]);
});
});