@@ -70,38 +70,61 @@ function writeLog(
7070 }
7171}
7272
73- function logJson ( value : unknown , options : AgentRunOptions | undefined ) : void {
73+ function logJson (
74+ value : unknown ,
75+ options : AgentRunOptions | undefined ,
76+ logs ?: string [ ] ,
77+ ) : void {
7478 try {
75- writeLog ( process . stdout , JSON . stringify ( value ) , options ?. logPrefix ) ;
79+ const message = JSON . stringify ( value ) ;
80+ writeLog ( process . stdout , message , options ?. logPrefix ) ;
81+ if ( options ?. captureLogs && logs ) {
82+ logs . push ( message ) ;
83+ }
7684 } catch ( error ) {
7785 const reason = error instanceof Error ? error . message : String ( error ) ;
78- writeLog (
79- process . stdout ,
80- JSON . stringify ( { error : "serialization_failed" , reason } ) ,
81- options ?. logPrefix ,
82- ) ;
86+ const errorMessage = JSON . stringify ( {
87+ error : "serialization_failed" ,
88+ reason,
89+ } ) ;
90+ writeLog ( process . stdout , errorMessage , options ?. logPrefix ) ;
91+ if ( options ?. captureLogs && logs ) {
92+ logs . push ( errorMessage ) ;
93+ }
8394 }
8495}
8596
86- function logError ( value : unknown , options : AgentRunOptions | undefined ) : void {
97+ function logError (
98+ value : unknown ,
99+ options : AgentRunOptions | undefined ,
100+ logs ?: string [ ] ,
101+ ) : void {
87102 try {
88- writeLog ( process . stderr , JSON . stringify ( value ) , options ?. logPrefix ) ;
103+ const message = JSON . stringify ( value ) ;
104+ writeLog ( process . stderr , message , options ?. logPrefix ) ;
105+ if ( options ?. captureLogs && logs ) {
106+ logs . push ( `ERROR: ${ message } ` ) ;
107+ }
89108 } catch ( error ) {
90109 const reason = error instanceof Error ? error . message : String ( error ) ;
91- writeLog (
92- process . stderr ,
93- JSON . stringify ( { error : "serialization_failed" , reason } ) ,
94- options ?. logPrefix ,
95- ) ;
110+ const errorMessage = JSON . stringify ( {
111+ error : "serialization_failed" ,
112+ reason,
113+ } ) ;
114+ writeLog ( process . stderr , errorMessage , options ?. logPrefix ) ;
115+ if ( options ?. captureLogs && logs ) {
116+ logs . push ( `ERROR: ${ errorMessage } ` ) ;
117+ }
96118 }
97119}
98120
99121function logPromptResult (
100122 result : { info : AssistantMessage ; parts : Part [ ] } ,
101123 options : AgentRunOptions | undefined ,
124+ logs ?: string [ ] ,
102125) : void {
103- logJson ( { info : result . info } , options ) ;
104- result . parts . forEach ( ( part ) => logJson ( part , options ) ) ;
126+ logJson ( { info : result . info } , options , logs ) ;
127+ result . parts . forEach ( ( part ) => logJson ( part , options , logs ) ) ;
105128}
106129
107130function serializeError ( error : unknown ) : Record < string , unknown > {
@@ -144,6 +167,7 @@ const opencodeAgent: AgentDefinition = {
144167 options ?. onStart ?.( displayCommand ) ;
145168
146169 const cacheKey = sessionKey ( cwd , model ) ;
170+ const logs : string [ ] = options ?. captureLogs ? [ ] : undefined as any ;
147171
148172 let sessionID = sessionCache . get ( cacheKey ) ;
149173 if ( ! sessionID ) {
@@ -170,7 +194,7 @@ const opencodeAgent: AgentDefinition = {
170194 throwOnError : true ,
171195 } ) ;
172196
173- logPromptResult ( data , options ) ;
197+ logPromptResult ( data , options , logs ) ;
174198 } catch ( error ) {
175199 sessionCache . delete ( cacheKey ) ;
176200 logError (
@@ -179,11 +203,12 @@ const opencodeAgent: AgentDefinition = {
179203 details : serializeError ( error ) ,
180204 } ,
181205 options ,
206+ logs ,
182207 ) ;
183208 throw error ;
184209 }
185210
186- return { command : displayCommand } ;
211+ return { command : displayCommand , sessionID , logs } ;
187212 } ,
188213} ;
189214
0 commit comments