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); }