-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathparse.ts
More file actions
71 lines (63 loc) · 2.17 KB
/
parse.ts
File metadata and controls
71 lines (63 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {PerformanceTraceFormatter} from '../../node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/data_formatters/PerformanceTraceFormatter.js';
import * as TraceEngine from '../../node_modules/chrome-devtools-frontend/front_end/models/trace/trace.js';
import {logger} from '../logger.js';
import {AgentFocus} from '../../node_modules/chrome-devtools-frontend/front_end/models/ai_assistance/performance/AIContext.js';
const engine = TraceEngine.TraceModel.Model.createWithAllHandlers();
export interface TraceResult {
parsedTrace: TraceEngine.TraceModel.ParsedTrace;
insights: TraceEngine.Insights.Types.TraceInsightSets;
}
export async function parseRawTraceBuffer(
buffer: Uint8Array<ArrayBufferLike> | undefined,
): Promise<TraceResult | null> {
engine.resetProcessor();
if (!buffer) {
return null;
}
const asString = new TextDecoder().decode(buffer);
if (!asString) {
return null;
}
try {
const data = JSON.parse(asString) as
| {
traceEvents: TraceEngine.Types.Events.Event[];
}
| TraceEngine.Types.Events.Event[];
const events = Array.isArray(data) ? data : data.traceEvents;
await engine.parse(events);
const parsedTrace = engine.parsedTrace();
if (!parsedTrace) {
return null;
}
const insights = parsedTrace?.insights;
if (!insights) {
return null;
}
return {
parsedTrace,
insights,
};
} catch (e) {
if (e instanceof Error) {
logger(`Error parsing trace: ${e.message}`);
} else {
logger(`Error parsing trace: ${JSON.stringify(e)}`);
}
return null;
}
}
// TODO(jactkfranklin): move the formatters from DevTools to use here.
// This is a very temporary helper to output some text from the tool call to aid development.
export function insightOutput(result: TraceResult): string {
const focus = AgentFocus.full(result.parsedTrace);
const serializer = new TraceEngine.EventsSerializer.EventsSerializer();
const formatter = new PerformanceTraceFormatter(focus, serializer);
const output = formatter.formatTraceSummary();
return output;
}