From d1ee1594f125a23ee2cb0fcdb57592d23ca55c46 Mon Sep 17 00:00:00 2001 From: Dayuan Jiang <34411969+DayuanJiang@users.noreply.github.com> Date: Sat, 10 Oct 2026 23:30:51 +0900 Subject: [PATCH] fix(chat): the AI reads and draws the page the user is viewing (#974) * fix(chat): the AI reads and draws the page the user is viewing The model always got the first page and display_diagram replaced the whole file with one page, so drawing on page 3 of a multi-page file deleted the other pages (#241), and edits went to page 1 while the user looked at another page. Now a turn records the page the user is viewing when the message is sent. That page's model goes to the model (also as previousXml, on regenerate and in edit_diagram error messages), selected shapes are sent on any page, display_diagram and append_diagram replace only that page (a drawing that brings several pages still replaces the file), edit_diagram targets it, and the streaming preview draws on it. Compact cells and default styles take the layer and the edges of that page, in the preview and in editDiagram. The editor bridge replaces the current page in place when the other pages are unchanged, so Ctrl+Z undoes an AI change on a multi-page file in one step; when a full load is needed, the user's page is shown again afterwards. Version summaries and the compare dialog count the page the change was made on. * test: read the failed edit's output without unsafe optional chaining --- components/canvas/canvas-stage.tsx | 7 +- components/chat/chat-engine.tsx | 49 ++++--- components/chat/compare-dialog.tsx | 6 +- components/chat/use-diagram-streaming.ts | 28 +++- components/chat/use-versions.ts | 4 +- contexts/diagram-context.tsx | 31 +++- hooks/use-diagram-tool-handlers.ts | 52 +++++-- lib/diagram-diff.ts | 43 +++--- lib/diagram-pages.ts | 138 ++++++++++++++++++ lib/drawio/editor-bridge.ts | 124 ++++++++++++---- lib/utils.ts | 40 +++-- packages/mcp-server/src/edit-diagram.ts | 2 +- stores/versions-store.ts | 2 + tests/e2e/diagram-content.spec.ts | 8 +- tests/e2e/workspace.spec.ts | 63 +++++++- tests/unit/diagram-diff.test.ts | 12 +- tests/unit/diagram-pages.test.ts | 125 ++++++++++++++++ tests/unit/mcp-core.test.ts | 47 ++++++ tests/unit/use-diagram-streaming.test.tsx | 84 ++++++++++- tests/unit/use-diagram-tool-handlers.test.tsx | 99 +++++++++++++ tests/unit/utils.test.ts | 63 ++++++++ 21 files changed, 907 insertions(+), 120 deletions(-) create mode 100644 lib/diagram-pages.ts create mode 100644 tests/unit/diagram-pages.test.ts diff --git a/components/canvas/canvas-stage.tsx b/components/canvas/canvas-stage.tsx index e6dc25e0..2cb1617e 100644 --- a/components/canvas/canvas-stage.tsx +++ b/components/canvas/canvas-stage.tsx @@ -46,11 +46,6 @@ export function CanvasStage({ const engine = useChatEngine() const isDark = useSettingsStore((s) => s.isDark) const hasEditor = useCanvasStore((s) => s.hasEditor) - // The model sees the first page only, so asking about shapes elsewhere - // would change the wrong page - const onFirstPage = useCanvasStore( - (s) => s.pages.length === 0 || s.pages[0]?.id === s.currentPageId, - ) // Canvas width when the chat panel starts sliding in or out const slideStartWidthRef = useRef(0) @@ -168,7 +163,7 @@ export function CanvasStage({