From c5d7284f915d7a3daeb906fb75b1f95c4b1684da Mon Sep 17 00:00:00 2001 From: Jerel John Velarde Date: Sun, 18 Oct 2026 22:56:09 -0700 Subject: [PATCH] fix(chat): don't repeat the model's canvas in the next prompt Since #215, ChatCoordinator.submitPrompt calls StateSync.syncActiveDraftToHistory so each request includes the latest canvas. But commitLayoutFromLlm cleared lastSynchronizedLayout when it applied a reply, so the next prompt always appended a user turn holding the canvas the model had just returned. The model received its own canvas twice in every follow-up request, and both chat panels showed it twice: the plain panel as a "Canvas Revision Snapshot" after the Gemini AI reply, the CopilotKit panel as a second "N components in this canvas" card. commitLayoutFromLlm now records the model's canvas as the last synchronized layout, sanitized like every layout syncLayoutToHistory compares. A canvas the user changes after the reply, including one restored to the previous draft, differs from it and is still recorded. --- .../app/chat/state-sync/state-sync.spec.ts | 19 +++++++++++++++++++ shell/src/app/chat/state-sync/state-sync.ts | 10 ++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/shell/src/app/chat/state-sync/state-sync.spec.ts b/shell/src/app/chat/state-sync/state-sync.spec.ts index f38504d41..a0dfde76d 100644 --- a/shell/src/app/chat/state-sync/state-sync.spec.ts +++ b/shell/src/app/chat/state-sync/state-sync.spec.ts @@ -157,6 +157,25 @@ describe('StateSync Autosave Draft Integrations', () => { expect(chatStateMock.chatHistory().at(-1)?.content).toContain('original flight'); }); + it('does not record the model canvas again before the next prompt', () => { + const generated = formatJson([{version: 'v0.9', text: 'generated flight'}]); + // The calls ChatCoordinator makes for one prompt and a canvas reply. + service.injectExternalDraft('[{"version":"v0.9","text":"original flight"}]'); + service.syncActiveDraftToHistory(); + chatStateMock.updateChatHistory(history => [ + ...history, + {role: MessageRole.USER, content: 'Change the flight'}, + {role: MessageRole.MODEL, content: generated, isSnapshot: true}, + ]); + service.commitLayoutFromLlm(generated); + const historyAfterReply = [...chatStateMock.chatHistory()]; + + // The next prompt, with the canvas unchanged since the reply. + service.syncActiveDraftToHistory(); + + expect(chatStateMock.chatHistory()).toEqual(historyAfterReply); + }); + it('syncs an injected draft before its initial observable emission', () => { const draft = '[{"version":"v0.9","text":"edited gallery draft","mockRules":[]} ]'; service.injectExternalDraft(draft); diff --git a/shell/src/app/chat/state-sync/state-sync.ts b/shell/src/app/chat/state-sync/state-sync.ts index 7c0ac8899..83adfde91 100644 --- a/shell/src/app/chat/state-sync/state-sync.ts +++ b/shell/src/app/chat/state-sync/state-sync.ts @@ -68,6 +68,10 @@ export class StateSync { // `_activeDraft` directly, bypassing history sync. private previousCatalogId: string | null = null; private isDraftEdited = false; + /** + * The sanitized canvas the chat history last recorded, in a canvas snapshot + * or in the model's reply. An unchanged canvas isn't recorded again. + */ private lastSynchronizedLayout: string | null = null; private readonly _activeDraft = signal(''); /** @@ -183,8 +187,10 @@ export class StateSync { commitLayoutFromLlm(value: string): void { this.cancelPendingDraftSync$.next(); this.isDraftEdited = true; - // Restoring a previous draft after this response must update context again. - this.lastSynchronizedLayout = null; + // The model's reply in the chat history already holds this canvas. Mark it + // as recorded, so it is added to the history again only after it changes, + // for example when the user edits it or restores the previous draft. + this.lastSynchronizedLayout = this.sanitizeLayout(value); this._activeDraft.set(value); }