fix(webui): cancel stale history reads on local navigation
This commit is contained in:
1 parent
27ef6fe24f
commit
6baf7178a8
4 files changed
+43
-8
No files matched your search
@@ -530,6 +530,9 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
}, []);
|
||||
|
||||
const jumpToUserPrompt = useCallback((promptId: string) => {
|
||||
// A local destination also supersedes an in-flight remote jump.
|
||||
navigation?.cancel();
|
||||
pendingPromptJumpRef.current = null;
|
||||
if (navigateToVisiblePrompt(promptId)) return;
|
||||
if (navigation?.prompts.some((prompt) => prompt.id === promptId && prompt.cursor)) {
|
||||
threadMotionRef.current?.takeUserControl();
|
||||
@@ -1146,10 +1149,10 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => {
|
||||
navigation?.latest();
|
||||
if (historyWindow) {
|
||||
pendingConversationScrollRef.current = true;
|
||||
setVisibleMessageCount(INITIAL_HISTORY_WINDOW);
|
||||
navigation?.latest();
|
||||
} else scrollToBottom(true, { force: true });
|
||||
}}
|
||||
className={cn(
|
||||
|
||||
@@ -28,6 +28,7 @@ export interface PromptHistoryNavigation {
|
||||
jump: (id: string) => void;
|
||||
older: () => void;
|
||||
newer: () => void;
|
||||
cancel: () => void;
|
||||
latest: () => void;
|
||||
retry: () => void;
|
||||
updateMessages: (update: (messages: UIMessage[]) => UIMessage[]) => void;
|
||||
@@ -51,15 +52,19 @@ export function usePromptHistory(key: string | null, version: number, messages:
|
||||
const scope = useRef({ key, client });
|
||||
const currentScope = scope.current.key === key && scope.current.client === client;
|
||||
|
||||
const latest = useCallback(() => {
|
||||
const cancel = useCallback(() => {
|
||||
request.current?.abort();
|
||||
request.current = null;
|
||||
setPages([]);
|
||||
setTarget(null);
|
||||
setLoading(false);
|
||||
setError(null);
|
||||
}, []);
|
||||
|
||||
const latest = useCallback(() => {
|
||||
cancel();
|
||||
setPages([]);
|
||||
}, [cancel]);
|
||||
|
||||
useEffect(() => {
|
||||
scope.current = { key, client };
|
||||
latest();
|
||||
@@ -161,7 +166,7 @@ export function usePromptHistory(key: string | null, version: number, messages:
|
||||
}, [currentScope, pages, first, last]);
|
||||
|
||||
return { prompts, window, target: currentScope ? target : null, loading: currentScope && loading,
|
||||
error: currentScope ? error ?? indexError : null, latest, updateMessages,
|
||||
error: currentScope ? error ?? indexError : null, cancel, latest, updateMessages,
|
||||
retry: () => error ? retry.current() : setRefresh((value) => value + 1),
|
||||
jump: (id) => {
|
||||
const prompt = prompts.find((item) => item.id === id);
|
||||
|
||||
@@ -1995,7 +1995,7 @@ describe("ThreadViewport", () => {
|
||||
const navigation = { prompts: [{ id: "u1", label: "hello", preview: "hello", answerPreview: "", createdAt: 1, index: 0 }],
|
||||
window: { messages: [{ id: "old", role: "user" as const, content: "old question", createdAt: 0 }],
|
||||
userMessageOffset: 0, hasMoreBefore: false, hasMoreAfter: true, forkBoundaryMessageCount: null },
|
||||
target: null, loading: false, error: null, jump: vi.fn(), older: vi.fn(), newer: vi.fn(), latest, retry: vi.fn(), updateMessages: vi.fn() };
|
||||
target: null, loading: false, error: null, jump: vi.fn(), older: vi.fn(), newer: vi.fn(), cancel: vi.fn(), latest, retry: vi.fn(), updateMessages: vi.fn() };
|
||||
const view = render(<ThreadViewport ref={viewportRef} messages={messages} isStreaming={false} composer={<div />} navigation={navigation} />);
|
||||
act(() => viewportRef.current?.jumpToUserPrompt("u1"));
|
||||
expect(latest).toHaveBeenCalledOnce();
|
||||
@@ -2012,7 +2012,7 @@ describe("ThreadViewport", () => {
|
||||
navigation={{ prompts: [], window: { messages: history, userMessageOffset: 0,
|
||||
hasMoreBefore: false, hasMoreAfter: true, forkBoundaryMessageCount: null },
|
||||
target: { id: "old-user", sequence: 1 }, loading: false, error: null,
|
||||
jump: vi.fn(), older: vi.fn(), newer: vi.fn(), latest: vi.fn(), retry: vi.fn(), updateMessages: vi.fn() }} />);
|
||||
jump: vi.fn(), older: vi.fn(), newer: vi.fn(), cancel: vi.fn(), latest: vi.fn(), retry: vi.fn(), updateMessages: vi.fn() }} />);
|
||||
await waitFor(() => expect(screen.getByText("Large old turn")).toBeInTheDocument());
|
||||
});
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { act, renderHook, waitFor } from "@testing-library/react";
|
||||
import { type ReactNode } from "react";
|
||||
import { act, render, renderHook, screen, waitFor, within } from "@testing-library/react";
|
||||
import { createRef, type ReactNode } from "react";
|
||||
import { beforeEach, expect, it, vi } from "vitest";
|
||||
import { usePromptHistory } from "@/hooks/usePromptHistory";
|
||||
import * as api from "@/lib/api";
|
||||
import { NanobotClient } from "@/lib/nanobot-client";
|
||||
import { ClientProvider } from "@/providers/ClientProvider";
|
||||
import type { UIMessage, WebuiThreadPersistedPayload } from "@/lib/types";
|
||||
import { ThreadViewport, type ThreadViewportHandle } from "@/components/thread/ThreadViewport";
|
||||
|
||||
vi.mock("@/lib/api", async (original) => ({ ...await original<typeof api>(), fetchPromptIndex: vi.fn(), fetchWebuiThread: vi.fn() }));
|
||||
const index = Array.from({ length: 2000 }, (_, index) => ({ id: `u${index}`, preview: `question ${index}`,
|
||||
@@ -87,6 +88,32 @@ it("does not request new endpoints from an older compatible host", () => {
|
||||
expect(api.fetchWebuiThread).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([false, true])("a local jump supersedes a pending history read without discarding the current window (history=%s)", async (history) => {
|
||||
const viewport = createRef<ThreadViewportHandle>();
|
||||
function Conversation() {
|
||||
const navigation = usePromptHistory("a", 1, latest);
|
||||
return <ThreadViewport ref={viewport} messages={latest} navigation={navigation} isStreaming={false} />;
|
||||
}
|
||||
render(<Conversation />, { wrapper: wrapper() });
|
||||
await waitFor(() => expect(screen.getByTestId("prompt-rail-scroll")).toBeInTheDocument());
|
||||
if (history) {
|
||||
await act(async () => viewport.current?.jumpToUserPrompt("u10"));
|
||||
}
|
||||
const visible = history ? 10 : 1999;
|
||||
const body = within(screen.getByTestId("thread-message-region"));
|
||||
expect(body.getByText(`question ${visible}`)).toBeInTheDocument();
|
||||
let finish!: (body: WebuiThreadPersistedPayload) => void;
|
||||
vi.mocked(api.fetchWebuiThread).mockImplementationOnce(() => new Promise((resolve) => { finish = resolve; }));
|
||||
act(() => viewport.current?.jumpToUserPrompt("u20"));
|
||||
const options = vi.mocked(api.fetchWebuiThread).mock.lastCall?.[2];
|
||||
if (typeof options !== "object") throw new Error("expected history read");
|
||||
act(() => viewport.current?.jumpToUserPrompt(`u${visible}`));
|
||||
await act(async () => finish(page(20)));
|
||||
expect(body.getByText(`question ${visible}`)).toBeInTheDocument();
|
||||
expect(body.queryByText("question 20")).not.toBeInTheDocument();
|
||||
expect(options.signal?.aborted).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps legacy backfilled and newly sent prompts navigable without a transcript cursor", async () => {
|
||||
vi.mocked(api.fetchPromptIndex).mockResolvedValue({ prompts: [] });
|
||||
const { result, rerender } = renderHook(({ messages }) => usePromptHistory("a", 1, messages), {
|
||||
|
||||
Reference in new issue
Block a user