Skip to content

Commit 0ed45a6

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[Ai Assistance] Fix history delete regression
Use the Id of the conversation as the Agent may not exits. Bug: 399792007 Change-Id: I5f7076f00a9e290ba734090d4ea4eda46849ec92 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6311453 Auto-Submit: Nikolay Vitkov <[email protected]> Commit-Queue: Alex Rudenko <[email protected]> Commit-Queue: Nikolay Vitkov <[email protected]> Reviewed-by: Alex Rudenko <[email protected]>
1 parent 45c6afe commit 0ed45a6

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

front_end/panels/ai_assistance/AiAssistancePanel.test.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
cleanup,
1212
createAiAssistancePanel,
1313
createNetworkRequest,
14-
mockAidaClient
14+
mockAidaClient,
15+
openHistoryContextMenu
1516
} from '../../testing/AiAssistanceHelpers.js';
1617
import {findMenuItemWithLabel, getMenu} from '../../testing/ContextMenuHelpers.js';
1718
import {createTarget, registerNoopActions, updateHostConfig} from '../../testing/EnvironmentHelpers.js';
@@ -634,11 +635,64 @@ describeWithMockConnection('AI Assistance Panel', () => {
634635
addHistoryItemStub, sinon.match({type: 'answer', text: 'partially started and now it\'s finished'}));
635636
sinon.assert.neverCalledWith(addHistoryItemStub, sinon.match({type: 'answer', text: 'partially started'}));
636637
});
638+
639+
it('should switch agents and restore history and allow a single delete', async () => {
640+
updateHostConfig({
641+
devToolsFreestyler: {
642+
enabled: true,
643+
},
644+
});
645+
const {panel, expectViewUpdate} = await createAiAssistancePanel(
646+
{
647+
aidaClient: mockAidaClient(
648+
[
649+
[{explanation: 'test'}],
650+
[{explanation: 'test2'}],
651+
],
652+
),
653+
},
654+
);
655+
const updateViewInput = await expectViewUpdate(() => {
656+
panel.handleAction('freestyler.elements-floating-button');
657+
});
658+
await expectViewUpdate(() => {
659+
updateViewInput.onTextSubmit('User question to Freestyler?');
660+
});
661+
662+
const updatedViewInputAfterSwitchToNetwork = await expectViewUpdate(() => {
663+
panel.handleAction('drjones.network-floating-button');
664+
});
665+
await expectViewUpdate(() => {
666+
updatedViewInputAfterSwitchToNetwork.onTextSubmit('User question to DrJones?');
667+
});
668+
669+
const {contextMenu, id} =
670+
openHistoryContextMenu(updatedViewInputAfterSwitchToNetwork, 'User question to Freestyler?');
671+
assert.isDefined(id);
672+
const updatedViewInput = await expectViewUpdate(() => {
673+
contextMenu.invokeHandler(id);
674+
});
675+
676+
const stub = sinon.createStubInstance(AiAssistance.AiHistoryStorage);
677+
sinon.stub(AiAssistance.AiHistoryStorage, 'instance').returns(stub);
678+
const updateDeleteSingle = await expectViewUpdate(() => {
679+
updatedViewInput.onDeleteClick();
680+
});
681+
682+
assert.deepEqual(updateDeleteSingle.messages, []);
683+
assert.strictEqual(stub.deleteHistoryEntry.callCount, 1);
684+
assert.isString(stub.deleteHistoryEntry.lastCall.args[0]);
685+
686+
const menuAfterDelete =
687+
openHistoryContextMenu(updatedViewInputAfterSwitchToNetwork, 'User question to Freestyler?');
688+
assert.isUndefined(menuAfterDelete.id);
689+
});
637690
});
638691

639692
it('should have empty state after clear chat', async () => {
640-
const {panel, expectViewUpdate} =
641-
await createAiAssistancePanel({aidaClient: mockAidaClient([[{explanation: 'test'}]])});
693+
const {panel, expectViewUpdate} = await createAiAssistancePanel({
694+
aidaClient: mockAidaClient([[{explanation: 'test'}]]),
695+
});
642696

643697
const updatedViewInput = await expectViewUpdate(() => {
644698
panel.handleAction('freestyler.elements-floating-button');

front_end/panels/ai_assistance/AiAssistancePanel.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,13 @@ export class AiAssistancePanel extends UI.Panel.Panel {
608608

609609
if (!agent) {
610610
this.#conversation = undefined;
611+
// We need to run doConversation separately
612+
this.#messages = [];
611613
// If a no new agent is provided
612614
// but conversation is
613615
// update with history conversation
614616
if (conversation) {
615617
this.#conversation = conversation;
616-
this.#messages = [];
617618
}
618619
}
619620

@@ -1091,11 +1092,13 @@ export class AiAssistancePanel extends UI.Panel.Panel {
10911092
}
10921093

10931094
#onDeleteClicked(): void {
1094-
if (this.#conversationAgent) {
1095-
this.#historicalConversations =
1096-
this.#historicalConversations.filter(conversation => conversation !== this.#conversation);
1097-
void AiHistoryStorage.instance().deleteHistoryEntry(this.#conversationAgent.id);
1095+
if (!this.#conversation) {
1096+
return;
10981097
}
1098+
1099+
this.#historicalConversations =
1100+
this.#historicalConversations.filter(conversation => conversation !== this.#conversation);
1101+
void AiHistoryStorage.instance().deleteHistoryEntry(this.#conversation.id);
10991102
this.#updateConversationState();
11001103
UI.ARIAUtils.alert(i18nString(UIStrings.chatDeleted));
11011104
}

front_end/testing/AiAssistanceHelpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Logs from '../models/logs/logs.js';
1111
import type * as Workspace from '../models/workspace/workspace.js';
1212
import * as AiAssistance from '../panels/ai_assistance/ai_assistance.js';
1313

14+
import {findMenuItemWithLabel, getMenu} from './ContextMenuHelpers.js';
1415
import {
1516
createTarget,
1617
} from './EnvironmentHelpers.js';
@@ -263,3 +264,17 @@ export function cleanup() {
263264
}
264265
patchWidgets = [];
265266
}
267+
268+
export function openHistoryContextMenu(
269+
lastUpdate: AiAssistance.ViewInput,
270+
item: string,
271+
) {
272+
const contextMenu = getMenu(() => {
273+
lastUpdate.onHistoryClick(new MouseEvent('click'));
274+
});
275+
const freestylerEntry = findMenuItemWithLabel(contextMenu.defaultSection(), item);
276+
return {
277+
contextMenu,
278+
id: freestylerEntry?.id(),
279+
};
280+
}

0 commit comments

Comments
 (0)