Skip to content

Commit 4200e30

Browse files
Connor ClarkDevtools-frontend LUCI CQ
authored andcommitted
[AI] Fix getResourceContent
* In trace_app, scripts don't show up in ResourceTreeModel, so check the Scripts handler data too * Make sure never to use ResourceTreeModel if attached to a target unrelated to the trace * Provide more guidance to the agent about when to call this function Bug: 452333154 Change-Id: I04e617a4e351570c583b8c062a58d8ccc4616015 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7129331 Commit-Queue: Connor Clark <[email protected]> Auto-Submit: Connor Clark <[email protected]> Reviewed-by: Paul Irish <[email protected]>
1 parent ad5b9a9 commit 4200e30

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

front_end/models/ai_assistance/agents/PerformanceAgent.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -885,50 +885,60 @@ export class PerformanceAgent extends AiAgent<AgentFocus> {
885885
});
886886

887887
const isFresh = Tracing.FreshRecording.Tracker.instance().recordingIsFresh(parsedTrace);
888-
const hasScriptContents =
889-
parsedTrace.metadata.enhancedTraceVersion && parsedTrace.data.Scripts.scripts.some(s => s.content);
888+
const isTraceApp = Root.Runtime.Runtime.isTraceApp();
890889

891-
if (isFresh || hasScriptContents) {
892-
this.declareFunction<{url: string}, {content: string}>('getResourceContent', {
893-
description: 'Returns the content of the resource with the given url. Only use this for text resource types.',
894-
parameters: {
895-
type: Host.AidaClient.ParametersTypes.OBJECT,
896-
description: '',
897-
nullable: false,
898-
properties: {
899-
url: {
900-
type: Host.AidaClient.ParametersTypes.STRING,
901-
description: 'The url for the resource.',
902-
nullable: false,
903-
},
890+
this.declareFunction<{url: string}, {content: string}>('getResourceContent', {
891+
description:
892+
'Returns the content of the resource with the given url. Only use this for text resource types. This function is helpful for getting script contents in order to further analyze main thread activity and suggest code improvements. When analyzing the main thread activity, always call this function to get more detail. Always call this function when asked to provide specifics about what is happening in the code. Never ask permission to call this function, just do it.',
893+
parameters: {
894+
type: Host.AidaClient.ParametersTypes.OBJECT,
895+
description: '',
896+
nullable: false,
897+
properties: {
898+
url: {
899+
type: Host.AidaClient.ParametersTypes.STRING,
900+
description: 'The url for the resource.',
901+
nullable: false,
904902
},
905903
},
906-
displayInfoFromArgs: args => {
907-
return {title: lockedString('Looking at resource content…'), action: `getResourceContent('${args.url}')`};
908-
},
909-
handler: async args => {
910-
debugLog('Function call: getResourceContent');
911-
912-
const url = args.url as Platform.DevToolsPath.UrlString;
904+
},
905+
displayInfoFromArgs: args => {
906+
return {title: lockedString('Looking at resource content…'), action: `getResourceContent('${args.url}')`};
907+
},
908+
handler: async args => {
909+
debugLog('Function call: getResourceContent');
910+
911+
const url = args.url as Platform.DevToolsPath.UrlString;
912+
let content: string|undefined;
913+
914+
// First check parsedTrace.data.Scripts.
915+
// Then, check ResourceTreeModel, but only when it is valid. Don't want to
916+
// use if viewing a loaded trace from DevTools attached to an unrelated
917+
// page.
918+
const script = parsedTrace.data.Scripts.scripts.find(script => script.url === url);
919+
if (script?.content !== undefined) {
920+
content = script.content;
921+
} else if (isFresh || isTraceApp) {
913922
const resource = SDK.ResourceTreeModel.ResourceTreeModel.resourceForURL(url);
914923
if (!resource) {
915-
if (!resource) {
916-
return {error: 'Resource not found'};
917-
}
924+
return {error: 'Resource not found'};
918925
}
919926

920-
const content = await resource.requestContentData();
921-
if ('error' in content) {
922-
return {error: `Could not get resource content: ${content.error}`};
927+
const data = await resource.requestContentData();
928+
if ('error' in data) {
929+
return {error: `Could not get resource content: ${data.error}`};
923930
}
924931

925-
const key = `getResourceContent(${args.url})`;
926-
this.#cacheFunctionResult(focus, key, content.text);
927-
return {result: {content: content.text}};
928-
},
932+
content = data.text;
933+
} else {
934+
return {error: 'Resource not found'};
935+
}
929936

930-
});
931-
}
937+
const key = `getResourceContent(${args.url})`;
938+
this.#cacheFunctionResult(focus, key, content);
939+
return {result: {content}};
940+
},
941+
});
932942

933943
if (!context.external) {
934944
this.declareFunction<{eventKey: string}, {success: boolean}>('selectEventByKey', {

0 commit comments

Comments
 (0)