1+ import { Message } from 'types/util-types'
12import { publisher } from '../constants'
23import {
34 PLACEHOLDER ,
@@ -121,6 +122,7 @@ ${PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS}`,
121122 - An answer to the user prompt (if they asked a question).
122123 - An explanation of the changes made.
123124 - A note on any checks you ran to verify the changes, such as tests, typechecking, etc.
125+ - Do not include a section on the benefits of the changes, as we're most interested in the changes themselves and what still needs to be done.
124126- Do not write a summary outside of the one that you include in the set_output tool.
125127- As soon as you use set_output, you must end your turn using the end_turn tool.
126128` ,
@@ -129,13 +131,22 @@ ${PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS}`,
129131 const stepLimit = 25
130132 let stepCount = 0
131133 let agentState = initialAgentState
134+ let accumulatedEditToolResults : any [ ] = [ ]
132135
133136 while ( true ) {
134137 stepCount ++
135138
136139 const stepResult = yield 'STEP'
137140 agentState = stepResult . agentState // Capture the latest state
138141
142+ // Accumulate new tool messages from this step
143+ const { messageHistory } = agentState
144+
145+ // Extract and accumulate new edit tool results using helper function
146+ accumulatedEditToolResults . push (
147+ ...getLatestEditToolResults ( messageHistory ) ,
148+ )
149+
139150 if ( stepResult . stepsComplete ) {
140151 break
141152 }
@@ -155,34 +166,51 @@ ${PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS}`,
155166 // One final step to produce the summary
156167 const finalStepResult = yield 'STEP'
157168 agentState = finalStepResult . agentState
169+
170+ // Extract and accumulate final edit tool results using helper function
171+ accumulatedEditToolResults . push (
172+ ...getLatestEditToolResults ( agentState . messageHistory ) ,
173+ )
158174 break
159175 }
160176 }
161177
162- // Collect all the edits from the conversation
163- const { messageHistory, output } = agentState
164- const editToolResults = messageHistory
165- . filter ( ( message ) => message . role === 'tool' )
166- . filter (
167- ( message ) =>
168- message . content . toolName === 'write_file' ||
169- message . content . toolName === 'str_replace' ,
170- )
171- . flatMap ( ( message ) => message . content . output )
172- . filter ( ( output ) => output . type === 'json' )
173- . map ( ( output ) => output . value )
174- // Only successful edits!
175- . filter (
176- ( toolResult ) => toolResult && ! ( 'errorMessage' in ( toolResult as any ) ) ,
177- )
178-
179178 yield {
180179 toolName : 'set_output' ,
181180 input : {
182- ...output ,
183- edits : editToolResults ,
181+ ...agentState . output ,
182+ edits : accumulatedEditToolResults ,
184183 } ,
185184 }
185+
186+ function getLatestEditToolResults ( messageHistory : Message [ ] ) {
187+ const lastAssistantMessageIndex = messageHistory . findLastIndex (
188+ ( message ) => message . role === 'assistant' ,
189+ )
190+
191+ // Get all edit tool messages after the last assistant message
192+ const newToolMessages = messageHistory
193+ . slice ( lastAssistantMessageIndex + 1 )
194+ . filter ( ( message ) => message . role === 'tool' )
195+ . filter (
196+ ( message ) =>
197+ message . content . toolName === 'write_file' ||
198+ message . content . toolName === 'str_replace' ,
199+ )
200+
201+ // Extract and return new edit tool results
202+ return (
203+ newToolMessages
204+ . flatMap ( ( message ) => message . content . output )
205+ . filter ( ( output ) => output . type === 'json' )
206+ . map ( ( output ) => output . value )
207+ // Only successful edits!
208+ . filter (
209+ ( toolResult ) =>
210+ toolResult && ! ( 'errorMessage' in ( toolResult as any ) ) ,
211+ )
212+ )
213+ }
186214 } ,
187215}
188216
0 commit comments