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 , IpcMessageType , OsType } from './types.js' ;
14+ import { WatchdogClient } from './watchdog-client.js' ;
1315
1416const MS_PER_DAY = 24 * 60 * 60 * 1000 ;
1517
1618export class ClearcutLogger {
1719 #persistence: Persistence ;
18- #sender: ClearcutSender ;
20+ #watchdog: WatchdogClient ;
1921
20- constructor ( options ?: { persistence ?: Persistence ; sender ?: ClearcutSender } ) {
21- this . #persistence = options ?. persistence ?? new FilePersistence ( ) ;
22- this . #sender = options ?. sender ?? new ClearcutSender ( ) ;
22+ constructor ( options : {
23+ persistence ?: Persistence ;
24+ logFile ?: string ;
25+ appVersion : string ;
26+ } ) {
27+ this . #persistence = options . persistence ?? new FilePersistence ( ) ;
28+ this . #watchdog = new WatchdogClient ( {
29+ parentPid : process . pid ,
30+ appVersion : options . appVersion ,
31+ osType : this . #detectOsType( ) ,
32+ logFile : options . logFile ,
33+ } ) ;
34+ }
35+
36+ #detectOsType( ) : OsType {
37+ switch ( process . platform ) {
38+ case 'win32' :
39+ return OsType . OS_TYPE_WINDOWS ;
40+ case 'darwin' :
41+ return OsType . OS_TYPE_MACOS ;
42+ case 'linux' :
43+ return OsType . OS_TYPE_LINUX ;
44+ default :
45+ return OsType . OS_TYPE_UNSPECIFIED ;
46+ }
2347 }
2448
2549 async logToolInvocation ( args : {
2650 toolName : string ;
2751 success : boolean ;
2852 latencyMs : number ;
2953 } ) : Promise < void > {
30- await this . #sender. send ( {
31- tool_invocation : {
32- tool_name : args . toolName ,
33- success : args . success ,
34- latency_ms : args . latencyMs ,
54+ this . #watchdog. send ( {
55+ type : IpcMessageType . EVENT ,
56+ payload : {
57+ tool_invocation : {
58+ tool_name : args . toolName ,
59+ success : args . success ,
60+ latency_ms : args . latencyMs ,
61+ } ,
3562 } ,
3663 } ) ;
3764 }
3865
3966 async logServerStart ( flagUsage : FlagUsage ) : Promise < void > {
40- await this . #sender. send ( {
41- server_start : {
42- flag_usage : flagUsage ,
67+ this . #watchdog. send ( {
68+ type : IpcMessageType . EVENT ,
69+ payload : {
70+ server_start : {
71+ flag_usage : flagUsage ,
72+ } ,
4373 } ,
4474 } ) ;
4575 }
@@ -57,9 +87,12 @@ export class ClearcutLogger {
5787 daysSince = Math . ceil ( diffTime / MS_PER_DAY ) ;
5888 }
5989
60- await this . #sender. send ( {
61- daily_active : {
62- days_since_last_active : daysSince ,
90+ this . #watchdog. send ( {
91+ type : IpcMessageType . EVENT ,
92+ payload : {
93+ daily_active : {
94+ days_since_last_active : daysSince ,
95+ } ,
6396 } ,
6497 } ) ;
6598
0 commit comments