Skip to content

Commit e697df4

Browse files
jackfranklinDevtools-frontend LUCI CQ
authored and
Devtools-frontend LUCI CQ
committed
AiAssistance: support follow-ups in prompt
This CL adds support for examples that contain follow-up prompts in an HTML comment in the format of: ``` FollowUp1: "Summarise the main thread activity that slowed down rendering" FollowUp2: "How can I make the script not render blocking?" FollowUp3: "Is there anything I can do to optimize how quickly the image loads?" ``` R=@chromium.org Bug: 409706097 Change-Id: Ib92fc457c775ddda0f280614a86ac197f56662f5 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6450640 Commit-Queue: Jack Franklin <jacktfranklin@chromium.org> Reviewed-by: Ergün Erdoğmuş <ergunsh@chromium.org> Auto-Submit: Jack Franklin <jacktfranklin@chromium.org>
1 parent 2a3e20a commit e697df4

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

scripts/ai_assistance/auto-run-helpers.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,41 @@ function parseComment(comment) {
7070
}
7171
}
7272

73+
/** @type {Record<string, string>} */
7374
const result = {};
7475
Object.keys(lines).forEach(lineKey => {
7576
result[lineKey] = lines[lineKey].join('\n');
7677
});
7778
return result;
7879
}
7980

81+
/**
82+
* @param {Record<string, string>} comment
83+
* @return {string[]}
84+
*/
85+
function parseFollowUps(comment) {
86+
/** @type {string[]} */
87+
const followUpPrompts = [];
88+
const FOLLOW_UP_PREFIX = 'followup';
89+
Object.entries(comment).forEach(([key, value]) => {
90+
if (key.toLowerCase().startsWith(FOLLOW_UP_PREFIX)) {
91+
if (value.length === 0) {
92+
throw new Error(`Found empty followup value at ${key}`);
93+
}
94+
const indexStr = key.substring(FOLLOW_UP_PREFIX.length);
95+
const index = parseInt(indexStr, 10);
96+
if (Number.isNaN(index)) {
97+
throw new Error(`Found invalid followup prompt: ${key}, ${value}`);
98+
}
99+
followUpPrompts[index - 1] = value;
100+
}
101+
});
102+
// In case the input was "followup1" and "followup3", this removes holes
103+
// from the array.
104+
return followUpPrompts.filter(x => Boolean(x));
105+
}
106+
80107
module.exports = {
108+
parseFollowUps,
81109
parseComment,
82110
};

scripts/ai_assistance/auto-run.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const puppeteer = require('puppeteer-core');
88
const {hideBin} = require('yargs/helpers');
99
const yargs = require('yargs/yargs');
1010

11-
const {parseComment} = require('./auto-run-helpers');
11+
const {parseComment, parseFollowUps} = require('./auto-run-helpers');
1212

1313
const DEFAULT_FOLLOW_UP_QUERY = 'Fix the issue using JavaScript code execution.';
1414

@@ -348,8 +348,13 @@ class Example {
348348
// Only get the first comment for now.
349349
const comment = comments[0];
350350
const queries = [comment.prompt];
351-
if (userArgs.includeFollowUp) {
351+
352+
const followUpPromptsFromExample = parseFollowUps(comment);
353+
354+
if (userArgs.includeFollowUp && followUpPromptsFromExample.length === 0) {
352355
queries.push(DEFAULT_FOLLOW_UP_QUERY);
356+
} else {
357+
queries.push(...followUpPromptsFromExample);
353358
}
354359

355360
return {

scripts/ai_assistance/auto-run.test.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
const {assert} = require('chai');
1212

13-
const {parseComment} = require('./auto-run-helpers.js');
13+
const {parseComment, parseFollowUps} = require('./auto-run-helpers.js');
1414

1515
describe('parsing comments', () => {
1616
it('parses out the prompt and evaluation sections using the "old" syntax', () => {
@@ -86,3 +86,84 @@ describe('parsing comments', () => {
8686
assert.deepEqual(result, {prompt: 'A', explanation: 'B\nC\nD'});
8787
});
8888
});
89+
90+
describe('parseFollowUps', () => {
91+
it('should return an empty array when no followup keys exist', () => {
92+
const comment = {
93+
prompt: 'Test prompt',
94+
explanation: 'Test explanation',
95+
};
96+
assert.deepEqual(parseFollowUps(comment), []);
97+
});
98+
99+
it('should parse a single followup', () => {
100+
const comment = {
101+
prompt: 'Test prompt',
102+
followup1: 'First follow up',
103+
};
104+
assert.deepEqual(parseFollowUps(comment), ['First follow up']);
105+
});
106+
107+
it('should parse multiple sequential followups in order', () => {
108+
const comment = {
109+
followup2: 'Second follow up',
110+
prompt: 'Test prompt',
111+
followup1: 'First follow up',
112+
explanation: 'Test explanation',
113+
followup3: 'Third follow up',
114+
};
115+
assert.deepEqual(parseFollowUps(comment), [
116+
'First follow up',
117+
'Second follow up',
118+
'Third follow up',
119+
]);
120+
});
121+
122+
it('should parse non-sequential followups and filter empty slots', () => {
123+
const comment = {
124+
followup3: 'Third follow up',
125+
prompt: 'Test prompt',
126+
followup1: 'First follow up',
127+
};
128+
assert.deepEqual(parseFollowUps(comment), ['First follow up', 'Third follow up']);
129+
});
130+
131+
it('should throw an error for invalid followup keys (non-numeric index)', () => {
132+
const comment = {
133+
followup1: 'First follow up',
134+
followupX: 'Invalid key',
135+
};
136+
assert.throws(() => parseFollowUps(comment), 'Found invalid followup prompt: followupX, Invalid key');
137+
});
138+
139+
it('should throw an error for invalid followup keys (no index)', () => {
140+
const comment = {
141+
followup: 'Invalid key',
142+
followup1: 'First follow up',
143+
};
144+
assert.throws(() => parseFollowUps(comment), /^Found invalid followup prompt: followup,/);
145+
});
146+
147+
it('should throw an error when encountering empty string values for followups', () => {
148+
const comment = {
149+
followup1: '',
150+
followup2: 'Second follow up',
151+
};
152+
assert.throws(() => {
153+
parseFollowUps(comment);
154+
}, /Found empty followup value at followup1/);
155+
});
156+
157+
it('should correctly parse followup keys with multiple digits', () => {
158+
const comment = {
159+
followup10: 'Tenth follow up',
160+
followup1: 'First follow up',
161+
followup2: 'Second follow up',
162+
};
163+
assert.deepEqual(parseFollowUps(comment), [
164+
'First follow up',
165+
'Second follow up',
166+
'Tenth follow up',
167+
]);
168+
});
169+
});

0 commit comments

Comments
 (0)