Skip to content

Commit 9748d8e

Browse files
OrKoNDevtools-frontend LUCI CQ
authored andcommitted
[AI Assistance] Add better integration with Workspace
Bug: 393268664 Change-Id: I1a1acc1e1b2db365cc94c8f23cebb0913edfd259 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6280747 Reviewed-by: Ergün Erdoğmuş <[email protected]> Commit-Queue: Alex Rudenko <[email protected]>
1 parent f9f582e commit 9748d8e

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

front_end/panels/ai_assistance/AiAssistancePanel.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {createTarget, registerNoopActions, updateHostConfig} from '../../testing
1818
import {expectCall} from '../../testing/ExpectStubCall.js';
1919
import {describeWithMockConnection} from '../../testing/MockConnection.js';
2020
import {createNetworkPanelForMockConnection} from '../../testing/NetworkHelpers.js';
21+
import {createFileSystemUISourceCode} from '../../testing/UISourceCodeHelpers.js';
2122
import * as UI from '../../ui/legacy/legacy.js';
2223
import * as Elements from '../elements/elements.js';
2324
import * as Network from '../network/network.js';
@@ -1116,4 +1117,62 @@ describeWithMockConnection('AI Assistance Panel', () => {
11161117
]);
11171118
});
11181119
});
1120+
1121+
describe('workspace', () => {
1122+
function createTestFilesystem(fileSystemPath: string) {
1123+
const {project, uiSourceCode} = createFileSystemUISourceCode({
1124+
url: Platform.DevToolsPath.urlString`file:///example.html`,
1125+
mimeType: 'text/html',
1126+
content: 'content',
1127+
fileSystemPath,
1128+
});
1129+
return {project, uiSourceCode};
1130+
}
1131+
1132+
it('does not report a workspace project if disabled', async () => {
1133+
createTestFilesystem('file://test');
1134+
updateHostConfig({
1135+
devToolsFreestyler: {
1136+
enabled: true,
1137+
patching: false,
1138+
},
1139+
});
1140+
const {
1141+
initialViewInput,
1142+
} = await createAiAssistancePanel();
1143+
assert.strictEqual(initialViewInput.projectName, '');
1144+
});
1145+
1146+
it('reports a current workspace project', async () => {
1147+
createTestFilesystem('file://test');
1148+
updateHostConfig({
1149+
devToolsFreestyler: {
1150+
enabled: true,
1151+
patching: true,
1152+
},
1153+
});
1154+
const {
1155+
initialViewInput,
1156+
} = await createAiAssistancePanel();
1157+
assert.strictEqual(initialViewInput.projectName, 'test');
1158+
});
1159+
1160+
it('reports an updated project', async () => {
1161+
const {project} = createTestFilesystem('file://test');
1162+
updateHostConfig({
1163+
devToolsFreestyler: {
1164+
enabled: true,
1165+
patching: true,
1166+
},
1167+
});
1168+
const {initialViewInput, expectViewUpdate} = await createAiAssistancePanel();
1169+
assert.strictEqual(initialViewInput.projectName, 'test');
1170+
1171+
const updatedViewInput = await expectViewUpdate(() => {
1172+
Workspace.Workspace.WorkspaceImpl.instance().removeProject(project);
1173+
createTestFilesystem('file://test2');
1174+
});
1175+
assert.strictEqual(updatedViewInput.projectName, 'test2');
1176+
});
1177+
});
11191178
});

front_end/panels/ai_assistance/AiAssistancePanel.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ export class AiAssistancePanel extends UI.Panel.Panel {
322322
#patchSuggestion?: string;
323323
#patchSuggestionLoading?: boolean;
324324
#imageInput: string = '';
325+
#workspace = Workspace.Workspace.WorkspaceImpl.instance();
325326

326327
constructor(private view: View = defaultView, {aidaClient, aidaAvailability, syncInfo}: {
327328
aidaClient: Host.AidaClient.AidaClient,
@@ -345,6 +346,10 @@ export class AiAssistancePanel extends UI.Panel.Panel {
345346
return new Conversation(item.type, item.history, item.id, true);
346347
});
347348

349+
this.#selectProject();
350+
}
351+
352+
#selectProject(): void {
348353
if (isAiAssistancePatchingEnabled()) {
349354
// TODO: this is temporary code that should be replaced with workflow selection flow.
350355
// For now it picks the first Workspace project that is not Snippets.
@@ -356,11 +361,16 @@ export class AiAssistancePanel extends UI.Panel.Panel {
356361
continue;
357362
}
358363
this.#project = project;
364+
this.requestUpdate();
359365
break;
360366
}
361367
}
362368
}
363369

370+
#onProjectAddedOrRemoved(): void {
371+
this.#selectProject();
372+
}
373+
364374
#getChatUiState(): ChatViewState {
365375
const blockedByAge = Root.Runtime.hostConfig.aidaAvailability?.blockedByAge === true;
366376
return (this.#aiAssistanceEnabledSetting?.getIfNotDisabled() && !blockedByAge) ? ChatViewState.CHAT_VIEW :
@@ -536,6 +546,11 @@ export class AiAssistancePanel extends UI.Panel.Panel {
536546
SDK.TargetManager.TargetManager.instance().addModelListener(
537547
SDK.DOMModel.DOMModel, SDK.DOMModel.Events.AttrRemoved, this.#handleDOMNodeAttrChange, this);
538548
Host.userMetrics.actionTaken(Host.UserMetrics.Action.AiAssistancePanelOpened);
549+
550+
if (isAiAssistancePatchingEnabled()) {
551+
this.#workspace.addEventListener(Workspace.Workspace.Events.ProjectAdded, this.#onProjectAddedOrRemoved, this);
552+
this.#workspace.addEventListener(Workspace.Workspace.Events.ProjectRemoved, this.#onProjectAddedOrRemoved, this);
553+
}
539554
}
540555

541556
override willHide(): void {
@@ -572,6 +587,12 @@ export class AiAssistancePanel extends UI.Panel.Panel {
572587
this.#handleDOMNodeAttrChange,
573588
this,
574589
);
590+
591+
if (isAiAssistancePatchingEnabled()) {
592+
this.#workspace.removeEventListener(Workspace.Workspace.Events.ProjectAdded, this.#onProjectAddedOrRemoved, this);
593+
this.#workspace.removeEventListener(
594+
Workspace.Workspace.Events.ProjectRemoved, this.#onProjectAddedOrRemoved, this);
595+
}
575596
}
576597

577598
#handleAidaAvailabilityChange = async(): Promise<void> => {
@@ -1001,6 +1022,9 @@ export class AiAssistancePanel extends UI.Panel.Panel {
10011022
}
10021023

10031024
async #onApplyToWorkspace(): Promise<void> {
1025+
if (!isAiAssistancePatchingEnabled()) {
1026+
return;
1027+
}
10041028
const changeSummary = this.#getChangeSummary();
10051029
if (!changeSummary) {
10061030
throw new Error('Change summary does not exist');

0 commit comments

Comments
 (0)