mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 09:40:19 +08:00
fix(books): scope pending attachment reads to the active page chat (#3102)
This commit is contained in:
@@ -163,6 +163,7 @@ export default function BookChatPanel({
|
||||
const attachmentLimits = useAttachmentLimits();
|
||||
const [attachmentError, setAttachmentError] = useState<string | null>(null);
|
||||
const sessionIdRef = useRef<string | null>(null);
|
||||
const chatScopeRef = useRef<{ bookId?: string; pageId?: string; open: boolean } | null>(null);
|
||||
const clientRef = useRef<UnifiedTurnClient | null>(null);
|
||||
const turnIdRef = useRef<string | null>(null);
|
||||
const replyCommandRef = useRef<{
|
||||
@@ -216,7 +217,15 @@ export default function BookChatPanel({
|
||||
// The first turn creates its session server-side. Persisting that same id
|
||||
// back onto the book rerenders this component, but it is not a session
|
||||
// switch: resetting here would disconnect the turn before its reply arrives.
|
||||
if (initialSessionId && initialSessionId === sessionIdRef.current) return;
|
||||
const scope = chatScopeRef.current;
|
||||
if (
|
||||
scope &&
|
||||
scope.bookId === book?.id &&
|
||||
scope.pageId === page?.id &&
|
||||
scope.open === open &&
|
||||
initialSessionId && initialSessionId === sessionIdRef.current
|
||||
) return;
|
||||
chatScopeRef.current = { bookId: book?.id, pageId: page?.id, open };
|
||||
retryTimersRef.current.forEach((timer) => clearTimeout(timer));
|
||||
retryTimersRef.current.clear();
|
||||
clientRef.current?.disconnect();
|
||||
@@ -447,6 +456,7 @@ export default function BookChatPanel({
|
||||
async function addFiles(files: File[]) {
|
||||
const accepted = filterFiles(files);
|
||||
if (!accepted.length) return;
|
||||
const scope = chatScopeRef.current;
|
||||
const next = await Promise.all(
|
||||
accepted.map(async (file) => {
|
||||
const dataUrl = await readFileAsDataUrl(file);
|
||||
@@ -459,7 +469,7 @@ export default function BookChatPanel({
|
||||
} satisfies PendingAttachment;
|
||||
}),
|
||||
);
|
||||
setAttachments((prev) => [...prev, ...next]);
|
||||
if (chatScopeRef.current === scope) setAttachments((prev) => [...prev, ...next]);
|
||||
}
|
||||
|
||||
function handleFileInputChange(event: ChangeEvent<HTMLInputElement>) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { beforeEach, expect, it, vi } from "vitest";
|
||||
import BookChatPanel from "@/app/(workspace)/learning/books/components/BookChatPanel";
|
||||
import type { StreamEvent } from "@/features/chat/model/protocol";
|
||||
import type { Book, Page } from "@/lib/book-types";
|
||||
import * as fileAttachments from "@/lib/file-attachments";
|
||||
import { COMMAND_CONFIRMATION_FAILED } from "@/features/chat/transport/command-delivery";
|
||||
import { REPLY_NOT_DELIVERED } from "@/lib/ask-user-state";
|
||||
|
||||
@@ -79,6 +80,33 @@ beforeEach(() => {
|
||||
fixture.getSession.mockReset().mockResolvedValue({ messages: [], active_turns: [] });
|
||||
});
|
||||
|
||||
it.each(["page", "session", "same chat"])("scopes a delayed attachment read across a %s update", async change => {
|
||||
let finishRead!: (url: string) => void;
|
||||
const read = vi.spyOn(fileAttachments, "readFileAsDataUrl")
|
||||
.mockImplementationOnce(() => new Promise(resolve => { finishRead = resolve; }));
|
||||
fixture.getSession.mockResolvedValue({ messages: [{ role: "user", content: "Existing question" }] });
|
||||
const props = { book, page, open: true, initialSessionId: "session", onClose: () => {} };
|
||||
const view = render(<BookChatPanel {...props} />);
|
||||
await screen.findByText("Existing question");
|
||||
fireEvent.change(view.container.querySelector('input[type="file"]')!, {
|
||||
target: { files: [new File(["hello"], "notes.pdf", { type: "application/pdf" })] },
|
||||
});
|
||||
expect(read).toHaveBeenCalledTimes(1);
|
||||
const next = {
|
||||
...props,
|
||||
page: change === "page" ? { ...page, id: "next-page" } : page,
|
||||
initialSessionId: change === "session" ? "next-session" : "session",
|
||||
};
|
||||
view.rerender(<BookChatPanel {...next} />);
|
||||
await act(async () => finishRead("data:application/pdf;base64,aGVsbG8="));
|
||||
sendQuestion();
|
||||
expect(fixture.send).toHaveBeenCalledWith(expect.objectContaining({
|
||||
session_id: next.initialSessionId,
|
||||
book_references: [{ book_id: book.id, page_ids: [next.page.id] }],
|
||||
attachments: change === "same chat" ? [expect.objectContaining({ filename: "notes.pdf" })] : [],
|
||||
}));
|
||||
});
|
||||
|
||||
it("renders a live question and submits answers to the same turn before continuing below it", async () => {
|
||||
await start();
|
||||
emit(event("content", "Before the question"));
|
||||
|
||||
Reference in New Issue
Block a user