Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions shell/src/app/chat/state-sync/state-sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions shell/src/app/chat/state-sync/state-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('');
/**
Expand Down Expand Up @@ -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);
}

Expand Down
Loading