@@ -73,33 +73,36 @@ function writeLog(
7373
7474function logJson ( value : unknown , options : AgentRunOptions | undefined ) : void {
7575 try {
76- writeLog ( process . stdout , JSON . stringify ( value ) , options ?. logPrefix ) ;
76+ const message = JSON . stringify ( value ) ;
77+ writeLog ( process . stdout , message , options ?. logPrefix ) ;
7778 } catch ( error ) {
7879 const reason = error instanceof Error ? error . message : String ( error ) ;
79- writeLog (
80- process . stdout ,
81- JSON . stringify ( { error : "serialization_failed" , reason } ) ,
82- options ?. logPrefix ,
83- ) ;
80+ const errorMessage = JSON . stringify ( {
81+ error : "serialization_failed" ,
82+ reason,
83+ } ) ;
84+ writeLog ( process . stdout , errorMessage , options ?. logPrefix ) ;
8485 }
8586}
8687
8788function logError ( value : unknown , options : AgentRunOptions | undefined ) : void {
8889 try {
89- writeLog ( process . stderr , JSON . stringify ( value ) , options ?. logPrefix ) ;
90+ const message = JSON . stringify ( value ) ;
91+ writeLog ( process . stderr , message , options ?. logPrefix ) ;
9092 } catch ( error ) {
9193 const reason = error instanceof Error ? error . message : String ( error ) ;
92- writeLog (
93- process . stderr ,
94- JSON . stringify ( { error : "serialization_failed" , reason } ) ,
95- options ?. logPrefix ,
96- ) ;
94+ const errorMessage = JSON . stringify ( {
95+ error : "serialization_failed" ,
96+ reason,
97+ } ) ;
98+ writeLog ( process . stderr , errorMessage , options ?. logPrefix ) ;
9799 }
98100}
99101
100102function logPromptResult (
101103 result : { info : AssistantMessage ; parts : Part [ ] } ,
102104 options : AgentRunOptions | undefined ,
105+ logs ?: string [ ] ,
103106) : void {
104107 logJson ( { info : result . info } , options ) ;
105108 if ( Array . isArray ( result . parts ) ) {
@@ -167,6 +170,11 @@ const opencodeAgent: AgentDefinition = {
167170 sessionCache . set ( cacheKey , sessionID ) ;
168171 }
169172
173+ const actions : string [ ] = [ ] ;
174+ const usage = {
175+ input : 0 ,
176+ output : 0 ,
177+ } ;
170178 try {
171179 const [ providerID , modelID ] = model . split ( "/" ) ;
172180 const { data } = await opencode . client . session . prompt ( {
@@ -182,6 +190,14 @@ const opencodeAgent: AgentDefinition = {
182190 throwOnError : true ,
183191 } ) ;
184192
193+ usage . input = data . info . tokens . input ;
194+ usage . output = data . info . tokens . output ;
195+
196+ actions . push ( JSON . stringify ( data . info ) ) ;
197+ if ( Array . isArray ( data . parts ) ) {
198+ data . parts . forEach ( ( part ) => actions . push ( JSON . stringify ( part ) ) ) ;
199+ }
200+
185201 logPromptResult ( data , options ) ;
186202 } catch ( error ) {
187203 sessionCache . delete ( cacheKey ) ;
@@ -195,7 +211,7 @@ const opencodeAgent: AgentDefinition = {
195211 throw error ;
196212 }
197213
198- return { command : displayCommand } ;
214+ return { command : displayCommand , actions , usage } ;
199215 } ,
200216} ;
201217
0 commit comments