@@ -71,51 +71,31 @@ function writeLog(
7171 }
7272}
7373
74- function logJson (
75- value : unknown ,
76- options : AgentRunOptions | undefined ,
77- logs ?: string [ ] ,
78- ) : void {
74+ function logJson ( value : unknown , options : AgentRunOptions | undefined ) : void {
7975 try {
8076 const message = JSON . stringify ( value ) ;
8177 writeLog ( process . stdout , message , options ?. logPrefix ) ;
82- if ( options ?. captureLogs && logs ) {
83- logs . push ( message ) ;
84- }
8578 } catch ( error ) {
8679 const reason = error instanceof Error ? error . message : String ( error ) ;
8780 const errorMessage = JSON . stringify ( {
8881 error : "serialization_failed" ,
8982 reason,
9083 } ) ;
9184 writeLog ( process . stdout , errorMessage , options ?. logPrefix ) ;
92- if ( options ?. captureLogs && logs ) {
93- logs . push ( errorMessage ) ;
94- }
9585 }
9686}
9787
98- function logError (
99- value : unknown ,
100- options : AgentRunOptions | undefined ,
101- logs ?: string [ ] ,
102- ) : void {
88+ function logError ( value : unknown , options : AgentRunOptions | undefined ) : void {
10389 try {
10490 const message = JSON . stringify ( value ) ;
10591 writeLog ( process . stderr , message , options ?. logPrefix ) ;
106- if ( options ?. captureLogs && logs ) {
107- logs . push ( `ERROR: ${ message } ` ) ;
108- }
10992 } catch ( error ) {
11093 const reason = error instanceof Error ? error . message : String ( error ) ;
11194 const errorMessage = JSON . stringify ( {
11295 error : "serialization_failed" ,
11396 reason,
11497 } ) ;
11598 writeLog ( process . stderr , errorMessage , options ?. logPrefix ) ;
116- if ( options ?. captureLogs && logs ) {
117- logs . push ( `ERROR: ${ errorMessage } ` ) ;
118- }
11999 }
120100}
121101
@@ -179,7 +159,6 @@ const opencodeAgent: AgentDefinition = {
179159 options ?. onStart ?.( displayCommand ) ;
180160
181161 const cacheKey = sessionKey ( cwd , model ) ;
182- const logs : string [ ] = options ?. captureLogs ? [ ] : undefined as any ;
183162
184163 let sessionID = sessionCache . get ( cacheKey ) ;
185164 if ( ! sessionID ) {
@@ -191,6 +170,10 @@ const opencodeAgent: AgentDefinition = {
191170 sessionCache . set ( cacheKey , sessionID ) ;
192171 }
193172
173+ const usage = {
174+ input : 0 ,
175+ output : 0 ,
176+ } ;
194177 try {
195178 const [ providerID , modelID ] = model . split ( "/" ) ;
196179 const { data } = await opencode . client . session . prompt ( {
@@ -206,7 +189,10 @@ const opencodeAgent: AgentDefinition = {
206189 throwOnError : true ,
207190 } ) ;
208191
209- logPromptResult ( data , options , logs ) ;
192+ usage . input = data . info . tokens . input ;
193+ usage . output = data . info . tokens . output ;
194+
195+ logPromptResult ( data , options ) ;
210196 } catch ( error ) {
211197 sessionCache . delete ( cacheKey ) ;
212198 logError (
@@ -215,12 +201,11 @@ const opencodeAgent: AgentDefinition = {
215201 details : serializeError ( error ) ,
216202 } ,
217203 options ,
218- logs ,
219204 ) ;
220205 throw error ;
221206 }
222207
223- return { command : displayCommand , sessionID , logs } ;
208+ return { command : displayCommand , usage } ;
224209 } ,
225210} ;
226211
0 commit comments