Skip to content

Commit 81e6a23

Browse files
Connor ClarkDevtools-frontend LUCI CQ
authored andcommitted
[AI] Add getFunctionCode to PerformanceAgent
Bug: 463466660 Change-Id: I3d1beec6befcbb7817a2a081fedc0f58485fea2f Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7201966 Auto-Submit: Connor Clark <cjamcl@chromium.org> Commit-Queue: Connor Clark <cjamcl@chromium.org> Reviewed-by: Paul Irish <paulirish@chromium.org>
1 parent 0562110 commit 81e6a23

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

front_end/models/ai_assistance/agents/PerformanceAgent.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Platform from '../../../core/platform/platform.js';
99
import * as Root from '../../../core/root/root.js';
1010
import * as SDK from '../../../core/sdk/sdk.js';
1111
import * as Tracing from '../../../services/tracing/tracing.js';
12+
import * as SourceMapScopes from '../../source_map_scopes/source_map_scopes.js';
1213
import * as Trace from '../../trace/trace.js';
1314
import {
1415
PerformanceInsightFormatter,
@@ -883,12 +884,77 @@ export class PerformanceAgent extends AiAgent<AgentFocus> {
883884

884885
});
885886

887+
this.declareFunction<{scriptUrl: string, line: number, column: number}, {result: string}>('getFunctionCode', {
888+
description: 'Returns the code for a function defined at the given location.',
889+
parameters: {
890+
type: Host.AidaClient.ParametersTypes.OBJECT,
891+
description: '',
892+
nullable: false,
893+
properties: {
894+
scriptUrl: {
895+
type: Host.AidaClient.ParametersTypes.STRING,
896+
description: 'The url of the function.',
897+
nullable: false,
898+
},
899+
line: {
900+
type: Host.AidaClient.ParametersTypes.INTEGER,
901+
description: 'The line number where the function is defined.',
902+
nullable: false,
903+
},
904+
column: {
905+
type: Host.AidaClient.ParametersTypes.INTEGER,
906+
description: 'The column number where the function is defined.',
907+
nullable: false,
908+
},
909+
},
910+
},
911+
displayInfoFromArgs: args => {
912+
return {
913+
title: lockedString('Looking up function code…'),
914+
action: `getFunctionCode('${args.scriptUrl}', ${args.line}, ${args.column})`
915+
};
916+
},
917+
handler: async args => {
918+
debugLog('Function call: getFunctionCode');
919+
920+
if (args.line === undefined) {
921+
return {error: 'Missing arg: line'};
922+
}
923+
924+
if (args.column === undefined) {
925+
return {error: 'Missing arg: column'};
926+
}
927+
928+
if (!this.#formatter) {
929+
throw new Error('missing formatter');
930+
}
931+
932+
const target = SDK.TargetManager.TargetManager.instance().primaryPageTarget();
933+
if (!target) {
934+
throw new Error('missing target');
935+
}
936+
937+
const url = args.scriptUrl as Platform.DevToolsPath.UrlString;
938+
const code = await SourceMapScopes.FunctionCodeResolver.getFunctionCodeFromLocation(
939+
target, url, args.line, args.column, {contextLength: 200, contextLineLength: 5});
940+
if (!code) {
941+
return {error: 'Could not find code'};
942+
}
943+
944+
const result = this.#formatter.formatFunctionCode(code);
945+
946+
const key = `getFunctionCode('${args.scriptUrl}', ${args.line}, ${args.column})`;
947+
this.#cacheFunctionResult(focus, key, result);
948+
return {result: {result}};
949+
},
950+
});
951+
886952
const isFresh = Tracing.FreshRecording.Tracker.instance().recordingIsFresh(parsedTrace);
887953
const isTraceApp = Root.Runtime.Runtime.isTraceApp();
888954

889955
this.declareFunction<{url: string}, {content: string}>('getResourceContent', {
890956
description:
891-
'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.',
957+
'Returns the content of the resource with the given url. Only use this for text resource types. Prefer getFunctionCode when possible.',
892958
parameters: {
893959
type: Host.AidaClient.ParametersTypes.OBJECT,
894960
description: '',

front_end/models/ai_assistance/data_formatters/PerformanceTraceFormatter.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import * as CrUXManager from '../../crux-manager/crux-manager.js';
6+
import type * as SourceMapScopes from '../../source_map_scopes/source_map_scopes.js';
67
import * as Trace from '../../trace/trace.js';
78
import type {AICallTree} from '../performance/AICallTree.js';
89
import type {AgentFocus} from '../performance/AIContext.js';
@@ -813,4 +814,27 @@ The order of headers corresponds to an internal fixed list. If a header is not p
813814
];
814815
return parts.join(';');
815816
}
817+
818+
formatFunctionCode(code: SourceMapScopes.FunctionCodeResolver.FunctionCode): string {
819+
const {startLine, startColumn} = code.range;
820+
const {
821+
startLine: contextStartLine,
822+
startColumn: contextStartColumn,
823+
endLine: contextEndLine,
824+
endColumn: contextEndColumn
825+
} = code.rangeWithContext;
826+
const name = code.functionBounds.name;
827+
const url = code.functionBounds.uiSourceCode.url();
828+
829+
const parts = [];
830+
parts.push(`${name} @ ${url}:${startLine}:${startColumn}. With added context, chunk is from ${contextStartLine}:${
831+
contextStartColumn} to ${contextEndLine}:${contextEndColumn}`);
832+
parts.push(
833+
'\nThe following is a markdown block of JavaScript. <FUNCTION_START> and <FUNCTION_END> marks the exact function declaration, and everything outside that is provided for additional context. Do not show the user the function markers or the additional context.\n');
834+
parts.push('```');
835+
parts.push(code.codeWithContext);
836+
parts.push('```');
837+
838+
return parts.join('\n');
839+
}
816840
}

0 commit comments

Comments
 (0)