Skip to content

Commit cc48153

Browse files
OrKoNDevtools-frontend LUCI CQ
authored andcommitted
[AI Assistance] Make sure bindings are not overwritten
This CL fixes the issue of the AI Assistance updating the wrong element on subsequent updates. Fixed: 383979011 Change-Id: I3382a9fc9ad83ed87b2cb1add2895b79589ba389 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6148212 Reviewed-by: Samiya Caur <[email protected]> Commit-Queue: Alex Rudenko <[email protected]>
1 parent 08c452a commit cc48153

File tree

2 files changed

+99
-30
lines changed

2 files changed

+99
-30
lines changed

front_end/panels/ai_assistance/ExtensionScope.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,31 +156,33 @@ export class ExtensionScope {
156156
}
157157
}
158158

159-
const freestylerBinding = `globalThis.freestyler = (args) => {
160-
let resolver;
161-
let rejecter;
162-
const p = new Promise((resolve, reject) => {
163-
resolver = resolve;
164-
rejecter = reject;
165-
});
166-
freestyler.callbacks.set(freestyler.id , {
167-
args: JSON.stringify(args),
168-
callbackId: freestyler.id,
169-
resolver,
170-
rejecter
171-
});
172-
${FREESTYLER_BINDING_NAME}(String(freestyler.id));
173-
freestyler.id++;
174-
return p;
175-
}
176-
freestyler.id = 1;
177-
freestyler.callbacks = new Map();
178-
freestyler.getArgs = (callbackId) => {
179-
return freestyler.callbacks.get(callbackId).args;
180-
}
181-
freestyler.respond = (callbackId) => {
182-
freestyler.callbacks.get(callbackId).resolver();
183-
freestyler.callbacks.delete(callbackId);
159+
const freestylerBinding = `if (!globalThis.freestyler) {
160+
globalThis.freestyler = (args) => {
161+
let resolver;
162+
let rejecter;
163+
const p = new Promise((resolve, reject) => {
164+
resolver = resolve;
165+
rejecter = reject;
166+
});
167+
freestyler.callbacks.set(freestyler.id , {
168+
args: JSON.stringify(args),
169+
callbackId: freestyler.id,
170+
resolver,
171+
rejecter
172+
});
173+
${FREESTYLER_BINDING_NAME}(String(freestyler.id));
174+
freestyler.id++;
175+
return p;
176+
}
177+
freestyler.id = 1;
178+
freestyler.callbacks = new Map();
179+
freestyler.getArgs = (callbackId) => {
180+
return freestyler.callbacks.get(callbackId).args;
181+
}
182+
freestyler.respond = (callbackId) => {
183+
freestyler.callbacks.get(callbackId).resolver();
184+
freestyler.callbacks.delete(callbackId);
185+
}
184186
}`;
185187

186188
const functions = `async function setElementStyles(el, styles) {

test/e2e/ai_assistance/ai_assistance_test.ts

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ describe('AI Assistance', function() {
5757
waitUntil: 'networkidle0',
5858
});
5959

60+
await resetMockMessages(messages);
61+
}
62+
63+
async function resetMockMessages(
64+
messages: string[],
65+
) {
66+
const {frontend} = getBrowserAndPages();
67+
await frontend.bringToFront();
6068
await frontend.evaluate(messages => {
6169
let call = 0;
6270
// @ts-ignore devtools context.
@@ -177,12 +185,30 @@ describe('AI Assistance', function() {
177185
},
178186
},
179187
messages);
180-
await goToResource(resource);
181-
182-
await inspectNode(node, iframeId);
188+
await goToResource(resource, {
189+
waitUntil: 'networkidle0',
190+
});
183191
await openFreestyler();
184192
await turnOnAiAssistance();
185193
await enableDebugModeForFreestyler();
194+
return await sendAiAssistanceMessage({
195+
node,
196+
iframeId,
197+
query,
198+
messages,
199+
});
200+
}
201+
202+
async function sendAiAssistanceMessage(options: {
203+
query: string,
204+
messages: string[],
205+
node?: string,
206+
iframeId?: string,
207+
}) {
208+
const {messages, query, node = 'div', iframeId} = options;
209+
210+
await resetMockMessages(messages);
211+
await inspectNode(node, iframeId);
186212
await typeQuery(query);
187213
return await submitAndWaitTillDone();
188214
}
@@ -205,7 +231,7 @@ STOP`,
205231
result.at(-1)!.request.current_message, {role: 1, parts: [{text: 'OBSERVATION: {"color":"rgb(0, 0, 0)"}'}]});
206232
});
207233

208-
it('gets handles trailing ;', async () => {
234+
it('handles trailing ;', async () => {
209235
const result = await runAiAssistance(
210236
{
211237
query: 'Change the background color for this element to blue',
@@ -232,7 +258,7 @@ STOP`,
232258
result.at(-1)!.request.current_message, {role: 1, parts: [{text: 'OBSERVATION: {"aspectRatio":"auto"}'}]});
233259
});
234260

235-
it('gets handles comments', async () => {
261+
it('handles comments', async () => {
236262
const result = await runAiAssistance({
237263
query: 'Change the background color for this element to blue',
238264
messages: [
@@ -281,6 +307,47 @@ STOP`,
281307
});
282308
});
283309

310+
it('modifies multiple styles', async () => {
311+
await runAiAssistance({
312+
query: 'Change the background color for this element to blue',
313+
messages: [
314+
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
315+
TITLE: changing the property
316+
ACTION
317+
await setElementStyles($0, { 'background-color': 'blue' });
318+
STOP`,
319+
'ANSWER: changed styles',
320+
],
321+
node: 'div',
322+
});
323+
324+
const {target} = getBrowserAndPages();
325+
await target.bringToFront();
326+
await target.waitForFunction(() => {
327+
// @ts-ignore page context.
328+
return window.getComputedStyle(document.querySelector('div')).backgroundColor === 'rgb(0, 0, 255)';
329+
});
330+
331+
await sendAiAssistanceMessage({
332+
query: 'Change the background color for this element to green',
333+
messages: [
334+
`THOUGHT: I can change the background color of an element by setting the background-color CSS property.
335+
TITLE: changing the property
336+
ACTION
337+
await setElementStyles($0, { 'background-color': 'green' });
338+
STOP`,
339+
'ANSWER: changed styles',
340+
],
341+
node: 'button',
342+
});
343+
344+
await target.bringToFront();
345+
await target.waitForFunction(() => {
346+
// @ts-ignore page context.
347+
return window.getComputedStyle(document.querySelector('button')).backgroundColor === 'rgb(0, 128, 0)';
348+
});
349+
});
350+
284351
it('executes in the correct realm', async () => {
285352
const result = await runAiAssistance({
286353
query: 'What is the document title',

0 commit comments

Comments
 (0)