Skip to content

Commit 66bddca

Browse files
jackfranklinDevtools-frontend LUCI CQ
authored andcommitted
AI: update autofreestyler to support Insights AI example
Fixed: 400349823 Change-Id: I7ea9605ea96a3dcf05a63102930fd1cb3aa999e7 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6321991 Auto-Submit: Jack Franklin <[email protected]> Commit-Queue: Ergün Erdoğmuş <[email protected]> Commit-Queue: Jack Franklin <[email protected]> Reviewed-by: Ergün Erdoğmuş <[email protected]>
1 parent e0032e4 commit 66bddca

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

scripts/ai_assistance/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ takes these results and presents them in a UI for evaluation.
2121

2222
3. Close the DevTools window for the initial `about:blank` page but keep the tab open.
2323

24-
4. Run the following command. `--test-target` can be one of `elements` or `performance` to determine which mode the tool is run in.
24+
4. Run the following command. See below for the list of values `--test-target` supports. This flag is used to determine which AI experience is evaluated.
2525
```
2626
node scripts/ai_assistance/auto-run.js --test-target elements --example-urls <example-url-1> <example-url-2>
2727
```
@@ -33,7 +33,14 @@ Tip: You can add a `--label <label>` argument to the run to label the dataset. F
3333
node scripts/ai_assistance/auto-run.js --label title-change --example-urls <example-url-1> <example-url-2>
3434
```
3535

36+
## `--test-target` values
37+
38+
* `elements`: tests the entrypoint via right clicking on an element in the Elements panel.
39+
* `performance-main-thread`: tests the entrypoint via right clicking on an event in the Performance panel main thread.
40+
* `performance-insights`: tests the entrypoint via the "Ask AI" button shown on an individual Insight in the Performance panel sidebar.
41+
3642
## Evaluating the results
43+
3744
**Steps**
3845
1. Serve the `scripts/ai_assistance` folder by using a simple file server. For example:
3946
```

scripts/ai_assistance/auto-run.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const yargsInput = yargs(hideBin(process.argv))
3838
})
3939
.option('test-target', {
4040
describe: 'Which panel do you want to run the examples against?',
41-
choices: ['elements', 'performance'],
41+
choices: ['elements', 'performance-main-thread', 'performance-insights'],
4242
demandOption: true,
4343
})
4444
.parseSync();
@@ -314,12 +314,12 @@ class Example {
314314
* and explanation.
315315
* @param {puppeteer.Page} page - the target page
316316
* @param {puppeteer.Page} devtoolsPage - the DevTools page
317-
* @returns {Promise<{idx: number, queries: string[], explanation: string, performanceAnnotation?: puppeteer.ElementHandle<HTMLElement>}>}
317+
* @returns {Promise<{idx: number, queries: string[], explanation: string, performanceAnnotation?: puppeteer.ElementHandle<HTMLElement>, rawComment: Record<string, string>}>}
318318
*/
319319
async #generateMetadata(page, devtoolsPage) {
320320
/** @type {puppeteer.ElementHandle<HTMLElement>|undefined} */
321321
let annotation = undefined;
322-
if (userArgs.testTarget === 'performance') {
322+
if (userArgs.testTarget === 'performance-main-thread') {
323323
annotation = await this.#lookForAnnotatedPerformanceEvent(devtoolsPage);
324324
}
325325

@@ -339,6 +339,7 @@ class Example {
339339
queries,
340340
explanation: comment.explanation,
341341
performanceAnnotation: annotation,
342+
rawComment: comment,
342343
};
343344
}
344345

@@ -372,12 +373,13 @@ class Example {
372373
const devtoolsPage = await devtoolsTarget.asPage();
373374
this.log('[Info]: Got devtools page');
374375

375-
if (userArgs.testTarget === 'performance') {
376+
if (userArgs.testTarget === 'performance-main-thread' || userArgs.testTarget === 'performance-insights') {
376377
const fileName = await TraceDownloader.run(this, page);
377378
await this.#loadPerformanceTrace(devtoolsPage, fileName);
378379
}
379380

380-
const {idx, queries, explanation, performanceAnnotation} = await this.#generateMetadata(page, devtoolsPage);
381+
const {idx, queries, explanation, performanceAnnotation, rawComment} =
382+
await this.#generateMetadata(page, devtoolsPage);
381383
this.log('[Info]: Running...');
382384
// Strip comments to prevent LLM from seeing it.
383385
await page.evaluate(() => {
@@ -419,7 +421,7 @@ class Example {
419421
await devtoolsPage.locator(':scope >>> #tab-elements').click();
420422
}
421423

422-
if (userArgs.testTarget === 'performance' && performanceAnnotation) {
424+
if (userArgs.testTarget === 'performance-main-thread' && performanceAnnotation) {
423425
this.log('[Info]: selecting event in the performance timeline');
424426
await devtoolsPage.locator(':scope >>> #tab-timeline').setTimeout(5000).click();
425427
// Clicking on the annotation will also have the side-effect of selecting the event
@@ -430,10 +432,42 @@ class Example {
430432
);
431433
}
432434

435+
if (userArgs.testTarget === 'performance-insights') {
436+
const insight = rawComment.insight;
437+
if (!insight) {
438+
throw new Error('Cannot execute performance-insights example without "Insight:" in example comment metadata.');
439+
}
440+
441+
this.log('[Info]: selecting insight in the performance panel');
442+
await devtoolsPage.locator(':scope >>> #tab-timeline').setTimeout(5000).click();
443+
444+
// Ensure the sidebar is open. If this selector is not found then we assume the sidebar is already open.
445+
const sidebarButton = await devtoolsPage.$('aria/Show sidebar');
446+
if (sidebarButton) {
447+
await sidebarButton.click();
448+
}
449+
450+
this.log(`[Info]: expanding Insight "${insight}"`);
451+
// Now find the header for the right insight, and click to expand it.
452+
await devtoolsPage.locator(`aria/View details for ${insight}`).setTimeout(5000).click();
453+
}
454+
433455
this.log('[Info]: Locating AI assistance tab');
434-
await devtoolsPage.locator('aria/Customize and control DevTools').click();
435-
await devtoolsPage.locator('aria/More tools').click();
436-
await devtoolsPage.locator('aria/AI assistance').click();
456+
// Because there are two Performance AI flows, to ensure we activate the
457+
// right one for Insights, we go via the "Ask AI" button in the Insights
458+
// sidebar directly to select the right insight.
459+
// For the other flows, we select the right context item in the right
460+
// panel, and then open the AI Assistance panel.
461+
if (userArgs.testTarget === 'performance-insights') {
462+
// Now click the "Ask AI" button to open up the AI assistance panel
463+
await devtoolsPage.locator(':scope >>> devtools-button[data-ask-ai]').setTimeout(5000).click();
464+
this.log('[Info]: Opened AI assistance tab');
465+
} else {
466+
await devtoolsPage.locator('aria/Customize and control DevTools').click();
467+
await devtoolsPage.locator('aria/More tools').click();
468+
await devtoolsPage.locator('aria/AI assistance').click();
469+
}
470+
437471
this.log('[Info]: Opened AI assistance tab');
438472

439473
this.#page = page;
@@ -463,10 +497,10 @@ class Example {
463497
switch (userArgs.testTarget) {
464498
case 'elements':
465499
return 'aria/Ask a question about the selected element';
466-
case 'performance':
500+
case 'performance-main-thread':
467501
return 'aria/Ask a question about the selected item and its call tree';
468-
default:
469-
throw new Error(`Unknown --test-target: ${userArgs.testTarget}`);
502+
case 'performance-insights':
503+
return 'aria/Ask a question about the selected performance insight';
470504
}
471505
}
472506

scripts/ai_assistance/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ export interface YargsInput {
4747
label: string;
4848
parallel: boolean;
4949
includeFollowUp: boolean;
50-
testTarget: 'elements'|'performance';
50+
testTarget: TestTarget;
5151
}
52+
export type TestTarget = 'elements'|'performance-main-thread'|'performance-insights';
5253

5354
// Clang cannot handle the Record<> syntax over multiple lines, it seems.
5455
/* clang-format off */

0 commit comments

Comments
 (0)