77import * as fs from 'node:fs' ;
88import * as path from 'node:path' ;
99
10- import type { ParsedArguments } from '../build/src/bin/chrome-devtools-mcp-cli-options.js' ;
1110import {
11+ cliOptions ,
12+ parseArguments ,
13+ } from '../build/src/bin/chrome-devtools-mcp-cli-options.js' ;
14+ import {
15+ getPossibleFlagMetrics ,
16+ type FlagMetric ,
17+ } from '../build/src/telemetry/flagUtils.js' ;
18+ import {
19+ applyToExisting ,
1220 applyToExistingMetrics ,
1321 generateToolMetrics ,
1422 type ToolMetric ,
1523} from '../build/src/telemetry/toolMetricsUtils.js' ;
16- import type { ToolDefinition } from '../build/src/tools/ToolDefinition.js' ;
1724import { createTools } from '../build/src/tools/tools.js' ;
1825
19- export function HaveUniqueNames ( tools : ToolDefinition [ ] ) : boolean {
26+ export function HaveUniqueNames ( tools : Array < { name : string } > ) : boolean {
2027 const toolNames = tools . map ( tool => tool . name ) ;
2128 const toolNamesSet = new Set ( toolNames ) ;
2229 return toolNamesSet . size === toolNames . length ;
@@ -30,8 +37,9 @@ function writeToolCallMetricsConfig() {
3037 throw new Error ( `Error: Directory ${ dir } does not exist.` ) ;
3138 }
3239
33- const fullTools = createTools ( { slim : false } as ParsedArguments ) ;
34- const slimTools = createTools ( { slim : true } as ParsedArguments ) ;
40+ // Avoid 'as ParsedArguments' by using parseArguments
41+ const fullTools = createTools ( parseArguments ( '0.0.0' , [ '' , '' ] ) ) ;
42+ const slimTools = createTools ( parseArguments ( '0.0.0' , [ '' , '' , '--slim' ] ) ) ;
3543
3644 const allTools = [ ...fullTools , ...slimTools ] ;
3745
@@ -62,4 +70,43 @@ function writeToolCallMetricsConfig() {
6270 ) ;
6371}
6472
65- writeToolCallMetricsConfig ( ) ;
73+ function writeFlagUsageMetrics ( ) {
74+ const outputPath = path . resolve ( 'src/telemetry/flag_usage_metrics.json' ) ;
75+
76+ const dir = path . dirname ( outputPath ) ;
77+ if ( ! fs . existsSync ( dir ) ) {
78+ throw new Error ( `Error: Directory ${ dir } does not exist.` ) ;
79+ }
80+
81+ let existingMetrics : FlagMetric [ ] = [ ] ;
82+ if ( fs . existsSync ( outputPath ) ) {
83+ try {
84+ existingMetrics = JSON . parse (
85+ fs . readFileSync ( outputPath , 'utf8' ) ,
86+ ) as FlagMetric [ ] ;
87+ } catch {
88+ console . warn (
89+ `Warning: Failed to parse existing metrics from ${ outputPath } . Starting fresh.` ,
90+ ) ;
91+ }
92+ }
93+
94+ const newMetrics = getPossibleFlagMetrics ( cliOptions ) ;
95+ const mergedMetrics = applyToExisting < FlagMetric > (
96+ existingMetrics ,
97+ newMetrics ,
98+ ) ;
99+
100+ fs . writeFileSync ( outputPath , JSON . stringify ( mergedMetrics , null , 2 ) + '\n' ) ;
101+
102+ console . log (
103+ `Successfully wrote ${ mergedMetrics . length } flag usage metrics to ${ outputPath } ` ,
104+ ) ;
105+ }
106+
107+ function main ( ) {
108+ writeToolCallMetricsConfig ( ) ;
109+ writeFlagUsageMetrics ( ) ;
110+ }
111+
112+ main ( ) ;
0 commit comments