Skip to content

Commit 4a23e06

Browse files
jackfranklinDevtools-frontend LUCI CQ
authored andcommitted
Add some types to AI Assistance script
I was making them to aid my debugging as I was getting familiar with the code, and thought we might as well land some to help others too. Bug: 704639063 Change-Id: Ia4678cc136e6414438d19c7652fd9f5b390c0cae Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6085698 Commit-Queue: Alex Rudenko <[email protected]> Auto-Submit: Jack Franklin <[email protected]> Reviewed-by: Alex Rudenko <[email protected]>
1 parent b3042fb commit 4a23e06

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

scripts/ai_assistance/auto-run.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function formatElapsedTime() {
1717
}
1818

1919
/* clang-format off */
20+
/** @type {import('./types').YargsInput} */
2021
const yargsObject = yargs
2122
.option('example-urls', {
2223
string: true,
@@ -42,6 +43,7 @@ const OUTPUT_DIR = path.resolve(__dirname, 'data');
4243
const {exampleUrls, label} = yargsObject;
4344

4445
class Logger {
46+
/** @type {import('./types').Logs} */
4547
#logs = {};
4648
#updateElapsedTimeInterval = 0;
4749
constructor() {
@@ -72,6 +74,11 @@ class Logger {
7274
this.log('head', -1, `${text}\n`);
7375
}
7476

77+
/**
78+
* @param {string} id
79+
* @param {number} index
80+
* @param {string} text
81+
*/
7582
log(id, index, text) {
7683
this.#updateElapsedTime();
7784
this.#logs[id] = {index, text};
@@ -259,6 +266,10 @@ class Example {
259266
});
260267

261268
const results = [];
269+
270+
/**
271+
* @returns {Promise<import('./types').ExecutedExample>}
272+
*/
262273
const prompt = async query => {
263274
await this.#devtoolsPage.locator('aria/Ask a question about the selected element').click();
264275

@@ -291,11 +302,12 @@ class Example {
291302
await done;
292303
abort.abort();
293304

294-
const result = JSON.parse(await this.#devtoolsPage.evaluate(() => {
305+
/** @type {import('./types').IndividualPromptRequestResponse[]} */
306+
const results = JSON.parse(await this.#devtoolsPage.evaluate(() => {
295307
return localStorage.getItem('freestylerStructuredLog');
296308
}));
297309

298-
return result.map(r => ({...r, exampleId: this.id()}));
310+
return results.map(r => ({...r, exampleId: this.id()}));
299311
};
300312

301313
for (const query of this.#queries) {
@@ -307,6 +319,8 @@ class Example {
307319
await this.#page.close();
308320
const elapsedTime = numberFormatter.format((performance.now() - executionStartTime) / 1000);
309321
this.log(`Finished (${elapsedTime}s)`);
322+
323+
/** @type {import('./types').ExecutedExample} */
310324
return {results, metadata: {exampleId: this.id(), explanation: this.#explanation}};
311325
}
312326

@@ -326,6 +340,11 @@ class Example {
326340
}
327341

328342
const logger = new Logger();
343+
344+
/**
345+
* @param {Example[]} examples
346+
* @return {Promise<RunResult>}
347+
*/
329348
async function runInParallel(examples) {
330349
logger.head('Preparing examples...');
331350
for (const example of examples) {
@@ -348,8 +367,15 @@ async function runInParallel(examples) {
348367
return {allExampleResults, metadata};
349368
}
350369

370+
/**
371+
* @param {Example[]} examples
372+
* @return {Promise<import('./types').RunResult>}
373+
*/
351374
async function runSequentially(examples) {
375+
/** @type {import('./types').ExecutedExample[]} */
352376
const allExampleResults = [];
377+
378+
/** @type {import('./types').ExampleMetadata} */
353379
const metadata = [];
354380
logger.head('Running examples sequentially...');
355381
for (const example of examples) {
@@ -401,8 +427,11 @@ async function main() {
401427

402428
logger.head('Preparing examples...');
403429
const examples = exampleUrls.map(exampleUrl => new Example(exampleUrl, browser));
430+
/** @type {import('./types').IndividualPromptRequestResponse[]} */
404431
let allExampleResults = [];
432+
/** @type {import('./types').ExampleMetadata[]} */
405433
let metadata = [];
434+
406435
if (yargsObject.parallel) {
407436
({allExampleResults, metadata} = await runInParallel(examples));
408437
} else {

scripts/ai_assistance/types.d.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {AidaRequest} from '../../front_end/core/host/AidaClient.ts'
2+
3+
/**
4+
* Some types used in auto-run.js. They only exist here because it's
5+
* nicer to define these types in a .d.ts file than JSDOc syntax.
6+
*/
7+
8+
/**
9+
* The result of running auto_freestyler against all the provided examples.
10+
*/
11+
export interface RunResult {
12+
allExampleResults: ExecutedExample[];
13+
metadata: ExampleMetadata[]
14+
}
15+
16+
/**
17+
* The result of running a single example.
18+
*/
19+
export interface ExecutedExample {
20+
results: IndividualPromptRequestResponse[];
21+
metadata: ExampleMetadata;
22+
}
23+
24+
/**
25+
* The result of making a single request to Aida.
26+
*/
27+
export interface IndividualPromptRequestResponse {
28+
request: AidaRequest;
29+
response: string;
30+
exampleId: string;
31+
}
32+
33+
export interface ExampleMetadata {
34+
exampleId: string;
35+
explanation: string;
36+
}
37+
38+
/**
39+
* The CLI arguments people can use to configure the run.
40+
*/
41+
export interface YargsInput {
42+
exampleUrls: string[];
43+
label: string;
44+
parallel: boolean;
45+
includeFollowUp: boolean;
46+
}
47+
48+
// Clang cannot handle the Record<> syntax over multiple lines, it seems.
49+
/* clang-format off */
50+
export type Logs = Record<string, {
51+
index: number;
52+
text: string;
53+
}> ;
54+
/* clang-format on */

0 commit comments

Comments
 (0)