From 80395096aa4390ac6ddfceea63d3c3a46f407776 Mon Sep 17 00:00:00 2001 From: Zhangbo-cn <60600082+Zhangbo-cn@users.noreply.github.com> Date: Sun, 29 Nov 2026 22:27:15 +0800 Subject: [PATCH 1/7] fix(assistant): stop restaging references the composer already holds Clicking a node bumps the assistant selection request, which stages the node again even after its reference chip was inserted. The tray then drew a second copy of the same title under the chip (two boxes for one reference). Filter the staged batch against the references already present in the draft content and attachments, cancel the tray when nothing is left, and keep the insert path on the filtered batch. --- src/canvas/AssistantPanel.tsx | 34 ++++++++++++++++++++++----- src/canvas/pendingCanvasReferences.ts | 15 ++++++++++++ tests/pendingCanvasReferences.test.ts | 23 ++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index 50ff8d5eb..d0283c523 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -31,6 +31,7 @@ import { pendingCanvasReferences, pendingReferenceInsertion, samePendingReferences, + unreferencedPendingItems, } from "./pendingCanvasReferences.ts"; import { assetOptionId, referenceCandidates } from "./referenceCandidates.ts"; import { type MentionOption } from "./referenceMentions"; @@ -130,6 +131,24 @@ export function AssistantPanel({ selectionRequest, ); const pending = pendingReferences.pending; + // Clicking a node on the canvas restages it even when the composer already + // holds that reference; the tray only offers what the prompt does not contain. + const referencedNodeIds = new Set(); + for (const part of value.draftContent ?? []) + if (part.type === "reference" && part.nodeId) referencedNodeIds.add(part.nodeId); + for (const asset of value.assets as ReadonlyArray< + ChatAsset & { nodeId?: string; logicalNodeId?: string } + >) { + const nodeId = asset.logicalNodeId ?? asset.nodeId; + if (nodeId) referencedNodeIds.add(nodeId); + } + const staged = pending + ? { + ...pending, + items: unreferencedPendingItems(pending.items, referencedNodeIds), + } + : null; + const stagedCount = staged?.items.length ?? 0; const [checkingReferences, setCheckingReferences] = useState(false); const confirming = useRef(null); // Keep the canvas defaulting new nodes to the model this dialog is using. @@ -234,9 +253,12 @@ export function AssistantPanel({ log.current.scrollTop = log.current.scrollHeight; }, [value.messages, open]); useEffect(() => () => confirming.current?.abort(), [pending?.id]); + useEffect(() => { + if (pending && !stagedCount) pendingReferences.cancel(); + }, [pending, stagedCount, pendingReferences.cancel]); const confirmPending = async (end: number) => { - if (!pending || referenceDisabled || confirming.current) return; - const batch = pending, + if (!staged || referenceDisabled || confirming.current) return; + const batch = staged, before = latest.current.value; const abort = new AbortController(); confirming.current = abort; @@ -560,7 +582,7 @@ export function AssistantPanel({ void chat.send(); }} > - {pending && ( + {stagedCount > 0 && staged && (
- {t("assistant.panel.pendingCount", { count: pending.items.length })} ·{" "} + {t("assistant.panel.pendingCount", { count: staged.items.length })} ·{" "} {referenceDisabled ? t("assistant.panel.pendingDisabled") : t("assistant.panel.pendingHint")} @@ -583,7 +605,7 @@ export function AssistantPanel({
- {pending.items.map((item) => ( + {staged.items.map((item) => (
void confirmPending(at.end) : undefined + stagedCount > 0 ? (at) => void confirmPending(at.end) : undefined } onChange={chat.setContent} onPasteFiles={(files) => void upload(files)} diff --git a/src/canvas/pendingCanvasReferences.ts b/src/canvas/pendingCanvasReferences.ts index 113e6035b..b1ab86b9b 100644 --- a/src/canvas/pendingCanvasReferences.ts +++ b/src/canvas/pendingCanvasReferences.ts @@ -122,3 +122,18 @@ export function pendingReferenceInsertion( ); return { value: next, start, inserted, caret: start + inserted.length }; } + +/** + * Drop nodes that the composer already references. + * + * Clicking a node on the canvas bumps the selection request, which restages that + * node even after its reference was inserted. Staging an already inserted node + * draws a second copy of the same title beside the chip, so the tray must only + * offer the nodes that are not in the prompt yet. + */ +export function unreferencedPendingItems( + items: readonly PendingCanvasReference[], + referenced: ReadonlySet, +) { + return items.filter((item) => !referenced.has(item.nodeId)); +} diff --git a/tests/pendingCanvasReferences.test.ts b/tests/pendingCanvasReferences.test.ts index 3990ae99e..835f2ced6 100644 --- a/tests/pendingCanvasReferences.test.ts +++ b/tests/pendingCanvasReferences.test.ts @@ -5,6 +5,7 @@ import { pendingCanvasReferences, pendingReferenceInsertion, samePendingReferences, + unreferencedPendingItems, } from "../src/canvas/pendingCanvasReferences.ts"; test("staging keeps blank nodes and media information without treating generation prompts as saved text", () => { @@ -117,3 +118,25 @@ test("subjects stage one node with its actual main image; description-only subje assert.equal(staged.thumbnail, subject.data.subject!.main.src); assert.equal(pendingReferenceInsertion("", 0, [staged]).value, "@角色 "); }); + +test("a node already referenced by the composer is not staged a second time", () => { + const text = createNode("text", undefined, { title: "文本 1", text: "正文" }); + const image = createNode("image", undefined, { + title: "图 1", + src: "data:image/png,fixture", + }); + const staged = pendingCanvasReferences([text, image]); + assert.deepEqual( + staged.map((item) => item.nodeId), + [text.id, image.id], + ); + // The composer holds the text node (chip) and an asset bound to the image node. + const referenced = new Set([text.id, image.id!]); + assert.deepEqual(unreferencedPendingItems(staged, referenced), []); + assert.deepEqual( + unreferencedPendingItems(staged, new Set([text.id])).map((item) => item.nodeId), + [image.id], + ); + // Identity of the staged batch is untouched: dedupe never mutates input. + assert.equal(staged.length, 2); +}); From 46b08b190ae5807e7c93ab39371e2a93cf851260 Mon Sep 17 00:00:00 2001 From: Zhangbo-cn <60600082+Zhangbo-cn@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:17:38 +0800 Subject: [PATCH 2/7] fix(assistant): drop the composer attachment strip The strip repeated every reference the editor already shows as a chip and, after an insert, added a collapsible 'collapsed attachments' block under the input. Keep the chip as the single representation; sent messages keep their own strip. --- src/canvas/AssistantPanel.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index d0283c523..ea2a78f44 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -721,7 +721,6 @@ export function AssistantPanel({ void chat.send(); }} /> -