-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathupdate_metrics.ts
More file actions
112 lines (92 loc) · 3.02 KB
/
update_metrics.ts
File metadata and controls
112 lines (92 loc) · 3.02 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import {
cliOptions,
parseArguments,
} from '../build/src/bin/chrome-devtools-mcp-cli-options.js';
import {
getPossibleFlagMetrics,
type FlagMetric,
} from '../build/src/telemetry/flagUtils.js';
import {
applyToExisting,
applyToExistingMetrics,
generateToolMetrics,
type ToolMetric,
} from '../build/src/telemetry/toolMetricsUtils.js';
import {createTools} from '../build/src/tools/tools.js';
export function HaveUniqueNames(tools: Array<{name: string}>): boolean {
const toolNames = tools.map(tool => tool.name);
const toolNamesSet = new Set(toolNames);
return toolNamesSet.size === toolNames.length;
}
function writeToolCallMetricsConfig() {
const outputPath = path.resolve('src/telemetry/tool_call_metrics.json');
const dir = path.dirname(outputPath);
if (!fs.existsSync(dir)) {
throw new Error(`Error: Directory ${dir} does not exist.`);
}
// Avoid 'as ParsedArguments' by using parseArguments
const fullTools = createTools(parseArguments('0.0.0', ['', '']));
const slimTools = createTools(parseArguments('0.0.0', ['', '', '--slim']));
const allTools = [...fullTools, ...slimTools];
if (!HaveUniqueNames(allTools)) {
throw new Error('Error: Duplicate tool names found.');
}
let existingMetrics: ToolMetric[] = [];
if (fs.existsSync(outputPath)) {
try {
existingMetrics = JSON.parse(
fs.readFileSync(outputPath, 'utf8'),
) as ToolMetric[];
} catch {
console.warn(
`Warning: Failed to parse existing metrics from ${outputPath}. Starting fresh.`,
);
}
}
const newMetrics = generateToolMetrics(allTools);
const mergedMetrics = applyToExistingMetrics(existingMetrics, newMetrics);
fs.writeFileSync(outputPath, JSON.stringify(mergedMetrics, null, 2) + '\n');
console.log(
`Successfully wrote ${mergedMetrics.length} total tool metrics (including deprecated ones) to ${outputPath}`,
);
}
function writeFlagUsageMetrics() {
const outputPath = path.resolve('src/telemetry/flag_usage_metrics.json');
const dir = path.dirname(outputPath);
if (!fs.existsSync(dir)) {
throw new Error(`Error: Directory ${dir} does not exist.`);
}
let existingMetrics: FlagMetric[] = [];
if (fs.existsSync(outputPath)) {
try {
existingMetrics = JSON.parse(
fs.readFileSync(outputPath, 'utf8'),
) as FlagMetric[];
} catch {
console.warn(
`Warning: Failed to parse existing metrics from ${outputPath}. Starting fresh.`,
);
}
}
const newMetrics = getPossibleFlagMetrics(cliOptions);
const mergedMetrics = applyToExisting<FlagMetric>(
existingMetrics,
newMetrics,
);
fs.writeFileSync(outputPath, JSON.stringify(mergedMetrics, null, 2) + '\n');
console.log(
`Successfully wrote ${mergedMetrics.length} flag usage metrics to ${outputPath}`,
);
}
function main() {
writeToolCallMetricsConfig();
writeFlagUsageMetrics();
}
main();