CanvasAnvil is an open source AI diagram maker and flowchart generator - an infinite-canvas diagram editor and self-hosted drawio alternative that writes editable .drawio files. Text to diagram, then fix nodes, layout and labels in place. Also AI presentations, interior design CAD/BOM, posters and infographics.
TypeScript ai-agentsai-diagramai-presentationbomcadcanvasdiagramdiagram-generatordrawioflowchartinfographicspptppt-generatorpresentation-generatorreactsvgedittext-to-diagramtypescriptworkflow
fix(chat): stop the two file-sync effects from looping forever
codingFeng10114c4df3af960
diff --git a/client/shared/chat/chat-input.tsx b/client/shared/chat/chat-input.tsxindex de4529e..65235a2 100644--- a/client/shared/chat/chat-input.tsx+++ b/client/shared/chat/chat-input.tsx@@ -35,6 +35,11 @@ interface ChatInputProps {workspaceId?: FileWorkspace;}+/** File objects keep their identity, so a member-wise check is enough. */+function sameFileList(a: File[], b: File[]): boolean {+ return a.length === b.length && a.every((file, i) => file === b[i]);+}+export function ChatInput({input,setInput,@@ -71,11 +76,20 @@ export function ChatInput({const { files, handleFileChange, setFiles, pdfData } = useFileProcessor(workspaceId);const isRich = !!(richSegments && onRichSegmentsChange);- // Sync controlled files if provided+ /**+ * Sync controlled files in, without starting a fight with the effect+ * below that syncs them back out.+ *+ * The two run one step apart: this one writes the parent's array into+ * local state while the next still holds the previous local array and+ * writes that one back up. Two arrays that are equal but not identical --+ * such as the separate empty arrays the two sides start with -- therefore+ * swap places on every render, forever. Keeping the array we already have+ * whenever the contents match is what breaks the cycle.+ */useEffect(() => {- if (controlledFiles) {- setFiles(controlledFiles);- }+ if (!controlledFiles) return;+ setFiles((prev) => (sameFileList(prev, controlledFiles) ? prev : controlledFiles));}, [controlledFiles, setFiles]);// Notify parent when files change@@ -113,7 +127,7 @@ export function ChatInput({if (raf) cancelAnimationFrame(raf);raf = requestAnimationFrame(() => {raf = 0;- autoResizeTextarea();+ autoResizeTextarea();});};diff --git a/tests/chat-storage.test.ts b/tests/chat-storage.test.tsindex 4be05b5..ea96759 100644--- a/tests/chat-storage.test.ts+++ b/tests/chat-storage.test.ts@@ -184,3 +184,36 @@ test("KNOWN ODDITY: over budget, it drops the newest turns, not the oldest", ()assert.ok(context.includes("turn 45"), "keeps the oldest of the window");assert.ok(!context.includes("turn 49"), "and drops the newest turn");});++/**+ * The guard that stops ChatInput's two file-sync effects from swapping arrays+ * forever. It lives in chat-input.tsx alongside the effects it protects; this+ * pins the contract that made the loop possible -- equal-but-distinct arrays+ * must compare as the same.+ */+function sameFileList(a: File[], b: File[]): boolean {+ return a.length === b.length && a.every((file, i) => file === b[i]);+}++const fakeFile = (name: string) => ({ name }) as unknown as File;++test("two separate empty arrays count as the same file list", () => {+ // This is the exact pair that used to ping-pong: the parent and the input+ // each start with their own [], equal in content and distinct by reference.+ assert.equal(sameFileList([], []), true);+});++test("the same files in the same order count as the same list", () => {+ const a = fakeFile("a.png");+ const b = fakeFile("b.png");+ assert.equal(sameFileList([a, b], [a, b]), true);+});++test("a changed list is still detected", () => {+ const a = fakeFile("a.png");+ const b = fakeFile("b.png");+ assert.equal(sameFileList([a], [a, b]), false, "an added file");+ assert.equal(sameFileList([a, b], [a]), false, "a removed file");+ assert.equal(sameFileList([a, b], [b, a]), false, "a reorder");+ assert.equal(sameFileList([a], [fakeFile("a.png")]), false, "a different File with the same name");+});