Skip to content

Commit 41ddba8

Browse files
authored
[Agent] (Potentially) improve Editing using diff (OpenHands#2685)
* add replace-based block edit & preliminary test case fix * further fix the insert behavior * make edit only work on first occurence * bump codeact version since we now use new edit agentskills * update prompt for new agentskills * update integration tests * make run_infer.sh executable * remove code block for edit_file * update integration test for prompt changes * default to not use hint for eval * fix insert emptyfile bug * throw value error when `to_replace` is empty * make `_edit_or_insert_file` return string so we can try to fix some linter errors (best attempt) * add todo * update integration test * fix sandbox test for this PR
1 parent 6a0ffc5 commit 41ddba8

47 files changed

Lines changed: 2689 additions & 1367 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

agenthub/codeact_agent/codeact_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def get_in_context_example() -> str:
9797

9898

9999
class CodeActAgent(Agent):
100-
VERSION = '1.6'
100+
VERSION = '1.7'
101101
"""
102102
The Code Act Agent is a minimalist agent.
103103
The agent works by passing the model a list of action-observation pairs and prompting the model to take the next step.

agenthub/codeact_agent/prompt.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
COMMAND_DOCS = (
66
'\nApart from the standard Python library, the assistant can also use the following functions (already imported) in <execute_ipython> environment:\n'
77
f'{_AGENT_SKILLS_DOCS}'
8-
"Please note that THE `edit_file` and `append_file` FUNCTIONS REQUIRE PROPER INDENTATION. If the assistant would like to add the line ' print(x)', it must fully write that out, with all those spaces before the code! Indentation is important and code that is not indented correctly will fail and require fixing before it can be run."
8+
"Please note that THE `edit_file` and `insert_content_at_line` FUNCTIONS REQUIRE PROPER INDENTATION. If the assistant would like to add the line ' print(x)', it must fully write that out, with all those spaces before the code! Indentation is important and code that is not indented correctly will fail and require fixing before it can be run."
99
)
1010

1111
# ======= SYSTEM MESSAGE =======
@@ -75,7 +75,12 @@ def index():
7575
7676
if __name__ == '__main__':
7777
app.run(port=5000)\"\"\"
78-
edit_file('app.py', start=1, end=1, content=EDITED_CODE)
78+
79+
insert_content_at_line(
80+
'app.py',
81+
1,
82+
EDITED_CODE,
83+
)
7984
</execute_ipython>
8085
8186
USER:
@@ -90,6 +95,7 @@ def index():
9095
8|
9196
9|if __name__ == '__main__':
9297
10| app.run(port=5000)
98+
11|
9399
[File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]
94100
95101
ASSISTANT:
@@ -199,7 +205,7 @@ def index():
199205
</execute_ipython>
200206
201207
USER:
202-
[File: /workspace/app.py (10 lines total)]
208+
[File: /workspace/app.py (11 lines total)]
203209
1|from flask import Flask
204210
2|app = Flask(__name__)
205211
3|
@@ -210,16 +216,21 @@ def index():
210216
8|
211217
9|if __name__ == '__main__':
212218
10| app.run(port=5000)
219+
11|
213220
214221
ASSISTANT:
215222
I should edit the file to display the numbers in a table format. I should include correct indentation. Let me update the file:
216223
<execute_ipython>
217-
edit_file('app.py', start=7, end=7, content=" return '<table>' + ''.join([f'<tr><td>{i}</td></tr>' for i in numbers]) + '</table>'")
224+
edit_file(
225+
'app.py',
226+
to_replace=" return str(numbers)",
227+
new_content=" return '<table>' + ''.join([f'<tr><td>{i}</td></tr>' for i in numbers]) + '</table>'",
228+
)
218229
</execute_ipython>
219230
220231
USER:
221232
Observation:
222-
[File: /workspace/app.py (10 lines total after edit)]
233+
[File: /workspace/app.py (11 lines total after edit)]
223234
1|from flask import Flask
224235
2|app = Flask(__name__)
225236
3|
@@ -230,6 +241,7 @@ def index():
230241
8|
231242
9|if __name__ == '__main__':
232243
10| app.run(port=5000)
244+
11|
233245
[File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]
234246
235247
ASSISTANT:

evaluation/swe_bench/scripts/run_infer.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)