Skip to content

Commit 187041c

Browse files
committed
it works
Change-Id: I02b74ae07dde27aca0c4c44266b54734bc955d89
1 parent 27fb813 commit 187041c

File tree

2 files changed

+240
-314
lines changed

2 files changed

+240
-314
lines changed

src/tools/performance.ts

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import {logger} from '../logger.js';
88
import {zod} from '../third_party/index.js';
99
import type {Page} from '../third_party/index.js';
1010
import type {InsightName} from '../trace-processing/parse.js';
11-
import {
12-
getInsightOutput,
13-
getTraceSummary,
14-
parseRawTraceBuffer,
15-
traceResultIsSuccess,
16-
} from '../trace-processing/parse.js';
11+
import {getInsightOutput, getTraceSummary, parseRawTraceBuffer, traceResultIsSuccess} from '../trace-processing/parse.js';
1712

1813
import {ToolCategory} from './categories.js';
1914
import type {Context, Response} from './ToolDefinition.js';
@@ -28,21 +23,13 @@ export const startTrace = defineTool({
2823
readOnlyHint: true,
2924
},
3025
schema: {
31-
reload: zod
32-
.boolean()
33-
.describe(
34-
'Determines if, once tracing has started, the page should be automatically reloaded.',
35-
),
36-
autoStop: zod
37-
.boolean()
38-
.describe(
39-
'Determines if the trace recording should be automatically stopped.',
40-
),
26+
reload: zod.boolean().describe('Determines if, once tracing has started, the page should be automatically reloaded.'),
27+
autoStop: zod.boolean().describe('Determines if the trace recording should be automatically stopped.'),
4128
},
4229
handler: async (request, response, context) => {
4330
if (context.isRunningPerformanceTrace()) {
4431
response.appendResponseLine(
45-
'Error: a performance trace is already running. Use performance_stop_trace to stop it. Only one trace can be running at any given time.',
32+
'Error: a performance trace is already running. Use performance_stop_trace to stop it. Only one trace can be running at any given time.'
4633
);
4734
return;
4835
}
@@ -93,17 +80,14 @@ export const startTrace = defineTool({
9380
await new Promise(resolve => setTimeout(resolve, 5_000));
9481
await stopTracingAndAppendOutput(page, response, context);
9582
} else {
96-
response.appendResponseLine(
97-
`The performance trace is being recorded. Use performance_stop_trace to stop it.`,
98-
);
83+
response.appendResponseLine(`The performance trace is being recorded. Use performance_stop_trace to stop it.`);
9984
}
10085
},
10186
});
10287

10388
export const stopTrace = defineTool({
10489
name: 'performance_stop_trace',
105-
description:
106-
'Stops the active performance trace recording on the selected page.',
90+
description: 'Stops the active performance trace recording on the selected page.',
10791
annotations: {
10892
category: ToolCategory.PERFORMANCE,
10993
readOnlyHint: true,
@@ -120,32 +104,22 @@ export const stopTrace = defineTool({
120104

121105
export const analyzeInsight = defineTool({
122106
name: 'performance_analyze_insight',
123-
description:
124-
'Provides more detailed information on a specific Performance Insight that was highlighted in the results of a trace recording.',
107+
description: 'Provides more detailed information on a specific Performance Insight that was highlighted in the results of a trace recording.',
125108
annotations: {
126109
category: ToolCategory.PERFORMANCE,
127110
readOnlyHint: true,
128111
},
129112
schema: {
130-
insightName: zod
131-
.string()
132-
.describe(
133-
'The name of the Insight you want more information on. For example: "DocumentLatency" or "LCPBreakdown"',
134-
),
113+
insightName: zod.string().describe('The name of the Insight you want more information on. For example: "DocumentLatency" or "LCPBreakdown"'),
135114
},
136115
handler: async (request, response, context) => {
137116
const lastRecording = context.recordedTraces().at(-1);
138117
if (!lastRecording) {
139-
response.appendResponseLine(
140-
'No recorded traces found. Record a performance trace so you have Insights to analyze.',
141-
);
118+
response.appendResponseLine('No recorded traces found. Record a performance trace so you have Insights to analyze.');
142119
return;
143120
}
144121

145-
const insightOutput = getInsightOutput(
146-
lastRecording,
147-
request.params.insightName as InsightName,
148-
);
122+
const insightOutput = getInsightOutput(lastRecording, request.params.insightName as InsightName);
149123
if ('error' in insightOutput) {
150124
response.appendResponseLine(insightOutput.error);
151125
return;
@@ -155,11 +129,7 @@ export const analyzeInsight = defineTool({
155129
},
156130
});
157131

158-
async function stopTracingAndAppendOutput(
159-
page: Page,
160-
response: Response,
161-
context: Context,
162-
): Promise<void> {
132+
async function stopTracingAndAppendOutput(page: Page, response: Response, context: Context): Promise<void> {
163133
try {
164134
const traceEventsBuffer = await page.tracing.stop();
165135
const result = await parseRawTraceBuffer(traceEventsBuffer);
@@ -169,26 +139,21 @@ async function stopTracingAndAppendOutput(
169139
const traceSummaryText = getTraceSummary(result);
170140
response.appendResponseLine(traceSummaryText);
171141
} else {
172-
response.appendResponseLine(
173-
'There was an unexpected error parsing the trace:',
174-
);
142+
response.appendResponseLine('There was an unexpected error parsing the trace:');
175143
response.appendResponseLine(result.error);
176144
}
177145
} catch (e) {
178146
const errorText = e instanceof Error ? e.message : JSON.stringify(e);
179147
logger(`Error stopping performance trace: ${errorText}`);
180-
response.appendResponseLine(
181-
'An error occurred generating the response for this trace:',
182-
);
148+
response.appendResponseLine('An error occurred generating the response for this trace:');
183149
response.appendResponseLine(errorText);
184150
} finally {
185151
context.setIsRunningPerformanceTrace(false);
186152
}
187153
}
188154

189-
// This key is expected to be visible.
190-
// b/349721878
191-
const CRUX_API_KEY = 'AIzaSyCCSOx25vrb5z0tbedCB3_JRzzbVW6Uwgw';
155+
// This key is expected to be visible. b/349721878
156+
const CRUX_API_KEY = 'AIzaSyBn5gimNjhiEyA_euicSKko6IlD3HdgUfk';
192157
const CRUX_ENDPOINT = `https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=${CRUX_API_KEY}`;
193158

194159
export const queryChromeUXReport = defineTool({
@@ -200,53 +165,35 @@ export const queryChromeUXReport = defineTool({
200165
readOnlyHint: true,
201166
},
202167
schema: {
203-
origin: zod
204-
.string()
205-
.describe(
206-
'The origin to query, e.g., "https://www.google.com". Do not provide this if "url" is specified.',
207-
)
208-
.optional(),
168+
origin: zod.string().describe('The origin to query, e.g., "https://www.google.com". Do not provide this if "url" is specified.').optional(),
209169
url: zod
210170
.string()
211-
.describe(
212-
'The specific page URL to query, e.g., "https://www.google.com/search?q=puppies". Do not provide this if "origin" is specified.',
213-
)
171+
.describe('The specific page URL to query, e.g., "https://www.google.com/search?q=puppies". Do not provide this if "origin" is specified.')
214172
.optional(),
215173
formFactor: zod
216174
.enum(['DESKTOP', 'PHONE', 'TABLET'])
217-
.describe(
218-
'The form factor to filter by. If omitted, data for all form factors is aggregated.',
219-
)
175+
.describe('The form factor to filter by. If omitted, data for all form factors is aggregated.')
220176
.optional(),
221177
},
222178
handler: async (request, response) => {
223179
const {origin, url, formFactor} = request.params;
224180

225181
if ((!origin && !url) || (origin && url)) {
226-
response.appendResponseLine(
227-
'Error: you must provide either "origin" or "url", but not both.',
228-
);
182+
response.appendResponseLine('Error: you must provide either "origin" or "url", but not both.');
229183
return;
230184
}
231185

232186
const body = JSON.stringify({
233187
origin,
234188
url,
235189
formFactor,
236-
metrics: [
237-
'first_contentful_paint',
238-
'largest_contentful_paint',
239-
'cumulative_layout_shift',
240-
'interaction_to_next_paint',
241-
],
190+
metrics: ['first_contentful_paint', 'largest_contentful_paint', 'cumulative_layout_shift', 'interaction_to_next_paint'],
242191
});
243192

244193
try {
245194
const cruxResponse = await fetch(CRUX_ENDPOINT, {
246195
method: 'POST',
247-
headers: {
248-
'Content-Type': 'application/json',
249-
},
196+
headers: {'Content-Type': 'application/json', referer: 'devtools://mcp'},
250197
body,
251198
});
252199

0 commit comments

Comments
 (0)