44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ import process from 'node:process' ;
8+
79import { logger } from '../logger.js' ;
810
9- import { ClearcutSender } from './clearcut-sender.js' ;
1011import type { LocalState , Persistence } from './persistence.js' ;
1112import { FilePersistence } from './persistence.js' ;
12- import type { FlagUsage } from './types.js' ;
13+ import { type FlagUsage , WatchdogMessageType , OsType } from './types.js' ;
14+ import { WatchdogClient } from './watchdog-client.js' ;
1315
1416const MS_PER_DAY = 24 * 60 * 60 * 1000 ;
1517
18+ function detectOsType ( ) : OsType {
19+ switch ( process . platform ) {
20+ case 'win32' :
21+ return OsType . OS_TYPE_WINDOWS ;
22+ case 'darwin' :
23+ return OsType . OS_TYPE_MACOS ;
24+ case 'linux' :
25+ return OsType . OS_TYPE_LINUX ;
26+ default :
27+ return OsType . OS_TYPE_UNSPECIFIED ;
28+ }
29+ }
30+
1631export class ClearcutLogger {
1732 #persistence: Persistence ;
18- #sender: ClearcutSender ;
33+ #watchdog: WatchdogClient ;
1934
20- constructor ( options ?: { persistence ?: Persistence ; sender ?: ClearcutSender } ) {
21- this . #persistence = options ?. persistence ?? new FilePersistence ( ) ;
22- this . #sender = options ?. sender ?? new ClearcutSender ( ) ;
35+ constructor ( options : {
36+ appVersion : string ;
37+ logFile ?: string ;
38+ persistence ?: Persistence ;
39+ watchdogClient ?: WatchdogClient ;
40+ } ) {
41+ this . #persistence = options . persistence ?? new FilePersistence ( ) ;
42+ this . #watchdog =
43+ options . watchdogClient ??
44+ new WatchdogClient ( {
45+ parentPid : process . pid ,
46+ appVersion : options . appVersion ,
47+ osType : detectOsType ( ) ,
48+ logFile : options . logFile ,
49+ } ) ;
2350 }
2451
2552 async logToolInvocation ( args : {
2653 toolName : string ;
2754 success : boolean ;
2855 latencyMs : number ;
2956 } ) : Promise < void > {
30- await this . #sender. send ( {
31- tool_invocation : {
32- tool_name : args . toolName ,
33- success : args . success ,
34- latency_ms : args . latencyMs ,
57+ this . #watchdog. send ( {
58+ type : WatchdogMessageType . LOG_EVENT ,
59+ payload : {
60+ tool_invocation : {
61+ tool_name : args . toolName ,
62+ success : args . success ,
63+ latency_ms : args . latencyMs ,
64+ } ,
3565 } ,
3666 } ) ;
3767 }
3868
3969 async logServerStart ( flagUsage : FlagUsage ) : Promise < void > {
40- await this . #sender. send ( {
41- server_start : {
42- flag_usage : flagUsage ,
70+ this . #watchdog. send ( {
71+ type : WatchdogMessageType . LOG_EVENT ,
72+ payload : {
73+ server_start : {
74+ flag_usage : flagUsage ,
75+ } ,
4376 } ,
4477 } ) ;
4578 }
@@ -57,13 +90,15 @@ export class ClearcutLogger {
5790 daysSince = Math . ceil ( diffTime / MS_PER_DAY ) ;
5891 }
5992
60- await this . #sender. send ( {
61- daily_active : {
62- days_since_last_active : daysSince ,
93+ this . #watchdog. send ( {
94+ type : WatchdogMessageType . LOG_EVENT ,
95+ payload : {
96+ daily_active : {
97+ days_since_last_active : daysSince ,
98+ } ,
6399 } ,
64100 } ) ;
65101
66- // Update persistence
67102 state . lastActive = new Date ( ) . toISOString ( ) ;
68103 await this . #persistence. saveState ( state ) ;
69104 }
0 commit comments