|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import {PerformanceTraceFormatter} from '../../node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/data_formatters/PerformanceTraceFormatter.js'; |
| 8 | +import {PerformanceInsightFormatter} from '../../node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/data_formatters/PerformanceInsightFormatter.js'; |
8 | 9 | import * as TraceEngine from '../../node_modules/chrome-devtools-frontend/front_end/models/trace/trace.js'; |
9 | 10 | import {logger} from '../logger.js'; |
10 | 11 | import {AgentFocus} from '../../node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/performance/AIContext.js'; |
@@ -60,12 +61,51 @@ export async function parseRawTraceBuffer( |
60 | 61 | } |
61 | 62 | } |
62 | 63 |
|
63 | | -// TODO(jactkfranklin): move the formatters from DevTools to use here. |
64 | | -// This is a very temporary helper to output some text from the tool call to aid development. |
65 | | -export function insightOutput(result: TraceResult): string { |
| 64 | +export function getTraceSummary(result: TraceResult): string { |
66 | 65 | const focus = AgentFocus.full(result.parsedTrace); |
67 | 66 | const serializer = new TraceEngine.EventsSerializer.EventsSerializer(); |
68 | 67 | const formatter = new PerformanceTraceFormatter(focus, serializer); |
69 | 68 | const output = formatter.formatTraceSummary(); |
70 | 69 | return output; |
71 | 70 | } |
| 71 | + |
| 72 | +export type InsightName = keyof TraceEngine.Insights.Types.InsightModels; |
| 73 | +export type InsightOutput = {output: string} | {error: string}; |
| 74 | + |
| 75 | +export function getInsightOutput( |
| 76 | + result: TraceResult, |
| 77 | + insightName: InsightName, |
| 78 | +): InsightOutput { |
| 79 | + // Currently, we do not support inspecting traces with multiple navigations. We either: |
| 80 | + // 1. Find Insights from the first navigation (common case: user records a trace with a page reload to test load performance) |
| 81 | + // 2. Fall back to finding Insights not associated with a navigation (common case: user tests an interaction without a page load). |
| 82 | + const mainNavigationId = |
| 83 | + result.parsedTrace.data.Meta.mainFrameNavigations.at(0)?.args.data |
| 84 | + ?.navigationId; |
| 85 | + |
| 86 | + const insightsForNav = result.insights.get( |
| 87 | + mainNavigationId ?? TraceEngine.Types.Events.NO_NAVIGATION, |
| 88 | + ); |
| 89 | + |
| 90 | + if (!insightsForNav) { |
| 91 | + return { |
| 92 | + error: 'No Performance Insights for this trace.', |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + const matchingInsight = |
| 97 | + insightName in insightsForNav.model |
| 98 | + ? insightsForNav.model[insightName] |
| 99 | + : null; |
| 100 | + if (!matchingInsight) { |
| 101 | + return { |
| 102 | + error: `No Insight with the name ${insightName} found. Double check the name you provided is accurate and try again.`, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + const formatter = new PerformanceInsightFormatter( |
| 107 | + result.parsedTrace, |
| 108 | + matchingInsight, |
| 109 | + ); |
| 110 | + return {output: formatter.formatInsight()}; |
| 111 | +} |
0 commit comments