@@ -2,13 +2,25 @@ import { randomUUID } from "node:crypto";
22import { mkdirSync , rmSync , symlinkSync } from "node:fs" ;
33import { tmpdir } from "node:os" ;
44import { join } from "node:path" ;
5- import { Agent , PermissionMode } from "@posthog/agent" ;
5+ import { Agent , type OnLogCallback , PermissionMode } from "@posthog/agent" ;
66import {
77 app ,
88 type BrowserWindow ,
99 type IpcMainInvokeEvent ,
1010 ipcMain ,
1111} from "electron" ;
12+ import { logger } from "../lib/logger" ;
13+
14+ const log = logger . scope ( "agent" ) ;
15+
16+ const onAgentLog : OnLogCallback = ( level , scope , message , data ) => {
17+ const scopedLog = logger . scope ( scope ) ;
18+ if ( data !== undefined ) {
19+ scopedLog [ level ] ( message , data ) ;
20+ } else {
21+ scopedLog [ level ] ( message ) ;
22+ }
23+ } ;
1224
1325interface AgentStartParams {
1426 taskId : string ;
@@ -96,12 +108,9 @@ export function registerAgentIpc(
96108 process . env . CLAUDE_CONFIG_DIR || join ( app . getPath ( "home" ) , ".claude" ) ;
97109 const statsigPath = join ( claudeConfigDir , "statsig" ) ;
98110 rmSync ( statsigPath , { recursive : true , force : true } ) ;
99- console . log (
100- "[agent] Cleared statsig cache to work around input_examples bug" ,
101- ) ;
111+ log . info ( "Cleared statsig cache to work around input_examples bug" ) ;
102112 } catch ( error ) {
103- // Ignore errors if the folder doesn't exist
104- console . warn ( "[agent] Could not clear statsig cache:" , error ) ;
113+ log . warn ( "Could not clear statsig cache:" , error ) ;
105114 }
106115
107116 const taskId = randomUUID ( ) ;
@@ -123,7 +132,7 @@ export function registerAgentIpc(
123132 if ( stderrBuffer . length > 50 ) {
124133 stderrBuffer . shift ( ) ;
125134 }
126- console . error ( `[agent] [claude-stderr] ${ text } ` ) ;
135+ log . error ( `[claude-stderr] ${ text } ` ) ;
127136 emitToRenderer ( {
128137 type : "status" ,
129138 ts : Date . now ( ) ,
@@ -146,6 +155,7 @@ export function registerAgentIpc(
146155 posthogApiUrl : apiHost ,
147156 posthogProjectId : projectId ,
148157 debug : true ,
158+ onLog : onAgentLog ,
149159 } ) ;
150160
151161 const controllerEntry : TaskController = {
@@ -181,7 +191,7 @@ export function registerAgentIpc(
181191 } ) ;
182192 }
183193 } catch ( err ) {
184- console . warn ( "[agent] failed to fetch task progress" , err ) ;
194+ log . warn ( "Failed to fetch task progress" , err ) ;
185195 }
186196 } ;
187197
@@ -214,7 +224,7 @@ export function registerAgentIpc(
214224 } catch { }
215225 symlinkSync ( process . execPath , nodeSymlinkPath ) ;
216226 } catch ( err ) {
217- console . warn ( "[agent] Failed to setup mock node environment" , err ) ;
227+ log . warn ( "Failed to setup mock node environment" , err ) ;
218228 }
219229
220230 const newPath = `${ mockNodeDir } :${ process . env . PATH || "" } ` ;
@@ -259,7 +269,7 @@ export function registerAgentIpc(
259269
260270 emitToRenderer ( { type : "done" , success : true , ts : Date . now ( ) } ) ;
261271 } catch ( err ) {
262- console . error ( "[agent] task execution failed" , err ) ;
272+ log . error ( "Task execution failed" , err ) ;
263273 let errorMessage = err instanceof Error ? err . message : String ( err ) ;
264274 const cause =
265275 err instanceof Error && "cause" in err && err . cause
0 commit comments