@@ -16,6 +16,7 @@ interface ToolResult {
1616 role : 'tool' ;
1717 tool_call_id : string ;
1818 content : any ;
19+ userMessage ?: any ;
1920}
2021
2122interface ToolDetails {
@@ -31,6 +32,7 @@ class Agent {
3132 private maxRun : number ;
3233 private systemPrompt : SystemPrompt ;
3334 private userMessage : Message [ ] ;
35+ private nextUserMessage :any ;
3436
3537
3638 constructor ( tools : any = [ ] , systemPrompt : SystemPrompt , maxRun : number = 0 , subAgents : any [ ] = [ ] ) {
@@ -47,6 +49,7 @@ class Agent {
4749 this . tools = this . tools . concat ( subAgents . map ( subagent => ( {
4850 ...subagent
4951 } ) ) ) ;
52+
5053
5154
5255 }
@@ -114,26 +117,64 @@ class Agent {
114117 console . log ( "got sub agent resonse result" , agentResponse ) ;
115118 const [ didUserReject , result ] = [ false , "tool result is successful" ] ;
116119 console . log ( "got sub agent resonse result" , didUserReject , result ) ;
117-
118- toolResults . push ( this . getToolResult ( toolUseId , result ) ) ;
120+ let toolResult = this . getToolResult ( toolUseId , result )
121+ toolResults . push ( {
122+ role : "tool" ,
123+ tool_call_id : toolResult . tool_call_id ,
124+ content : toolResult . content ,
125+
126+ } ) ;
127+ if ( toolResult . userMessage ) {
128+ this . nextUserMessage = {
129+ role : "user" ,
130+ content : toolResult . userMessage
131+ }
132+ }
119133 if ( didUserReject ) {
120134 userRejectedToolUse = true ;
121135 }
136+
122137 }
123138 else {
124139 console . log ( "calling tool with params" , toolName , toolInput ) ;
125140 const [ didUserReject , result ] = await this . executeTool ( toolName , toolInput ) ;
126141 console . log ( "tool result" , result ) ;
127- toolResults . push ( this . getToolResult ( toolUseId , result ) ) ;
128-
142+ // toolResults.push(this.getToolResult(toolUseId, result));
143+ let toolResult = this . getToolResult ( toolUseId , result )
144+ toolResults . push ( {
145+ role : "tool" ,
146+ tool_call_id : toolResult . tool_call_id ,
147+ content : toolResult . content ,
148+
149+ } ) ;
150+ if ( toolResult . userMessage ) {
151+ this . nextUserMessage = {
152+ role : "user" ,
153+ content : toolResult . userMessage
154+ }
155+ }
129156 if ( didUserReject ) {
130157 userRejectedToolUse = true ;
131158 }
132159 }
133160
134161 }
135162 } else {
136- toolResults . push ( this . getToolResult ( toolUseId , "Skipping tool execution due to previous tool user rejection." ) ) ;
163+ let toolResult = this . getToolResult ( toolUseId , "Skipping tool execution due to previous tool user rejection." )
164+
165+ toolResults . push ( {
166+ role : "tool" ,
167+ tool_call_id : toolResult . tool_call_id ,
168+ content : toolResult . content ,
169+
170+ } ) ;
171+ if ( toolResult . userMessage ) {
172+ this . nextUserMessage = {
173+ role : "user" ,
174+ content : toolResult . userMessage
175+ }
176+ }
177+
137178 }
138179 }
139180 }
@@ -148,10 +189,26 @@ class Agent {
148189 completed = true ;
149190 result = "The user is satisfied with the result." ;
150191 }
151- toolResults . push ( this . getToolResult ( taskCompletedBlock . id , result ) ) ;
192+ let toolResult = this . getToolResult ( taskCompletedBlock . id , result )
193+ toolResults . push ( {
194+ role : "tool" ,
195+ tool_call_id : toolResult . tool_call_id ,
196+ content : toolResult . content ,
197+
198+ } ) ;
199+ if ( toolResult . userMessage ) {
200+ this . nextUserMessage = {
201+ role : "user" ,
202+ content : toolResult . userMessage
203+ }
204+ }
205+
152206 }
153207
154208 this . apiConversationHistory . push ( ...toolResults ) ;
209+ if ( this . nextUserMessage ) {
210+ this . apiConversationHistory . push ( this . nextUserMessage ) ;
211+ }
155212 let nextUserMessage : Message [ ] = toolResults ;
156213
157214 if ( toolResults . length === 0 ) {
@@ -228,11 +285,24 @@ class Agent {
228285 } ;
229286 }
230287
231- private getToolResult ( tool_call_id : string , content : any ) : ToolResult {
288+ private getToolResult ( tool_call_id : string , content : string ) : ToolResult {
289+ let userMessage = undefined
290+ try {
291+ let parsed = JSON . parse ( content ) ;
292+
293+ if ( parsed . payload && parsed . payload . content ) {
294+ content = `The browser action has been executed. The screenshot have been captured for your analysis. The tool response is provided in the next user message`
295+ // this.apiConversationHistory.push()
296+ userMessage = parsed . payload . content
297+ }
298+ } catch ( error ) {
299+
300+ }
232301 return {
233302 role : "tool" ,
234303 tool_call_id,
235304 content,
305+ userMessage
236306 } ;
237307 }
238308
0 commit comments