π OpenHands: AI-Driven Development
[Agent, LLM] Make sure codeact agent produce message in u/a/u/a order (#3193)
Xingyao Wang2e60d25eae16
diff --git a/agenthub/codeact_agent/codeact_agent.py b/agenthub/codeact_agent/codeact_agent.pyindex 319b01e292..08af8ca95a 100644--- a/agenthub/codeact_agent/codeact_agent.py+++ b/agenthub/codeact_agent/codeact_agent.py@@ -222,7 +222,13 @@ class CodeActAgent(Agent):# add regular messageif message:- messages.append(message)+ # handle error if the message is the SAME role as the previous message+ # litellm.exceptions.BadRequestError: litellm.BadRequestError: OpenAIException - Error code: 400 - {'detail': 'Only supports u/a/u/a/u...'}+ # there should not have two consecutive messages from the same role+ if messages and messages[-1]['role'] == message['role']:+ messages[-1]['content'] += '\n\n' + message['content']+ else:+ messages.append(message)# the latest user message is important:# we want to remind the agent of the environment constraintsdiff --git a/agenthub/codeact_swe_agent/codeact_swe_agent.py b/agenthub/codeact_swe_agent/codeact_swe_agent.pyindex d150af8405..67ed646ee2 100644--- a/agenthub/codeact_swe_agent/codeact_swe_agent.py+++ b/agenthub/codeact_swe_agent/codeact_swe_agent.py@@ -173,7 +173,13 @@ class CodeActSWEAgent(Agent):# add regular messageif message:- messages.append(message)+ # handle error if the message is the SAME role as the previous message+ # litellm.exceptions.BadRequestError: litellm.BadRequestError: OpenAIException - Error code: 400 - {'detail': 'Only supports u/a/u/a/u...'}+ # there should not have two consecutive messages from the same role+ if messages and messages[-1]['role'] == message['role']:+ messages[-1]['content'] += '\n\n' + message['content']+ else:+ messages.append(message)# the latest user message is important:# we want to remind the agent of the environment constraintsdiff --git a/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.log b/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.logindex 0fdee5cca1..69b4fa0e96 100644--- a/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.log+++ b/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Browse localhost:8000, and tell me the ultimate answer to life. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>diff --git a/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.log b/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.logindex 645ac5d1ef..0f8e9046da 100644--- a/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.log+++ b/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Browse localhost:8000, and tell me the ultimate answer to life. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_001.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_001.logindex 83437bb6c1..2d38aad365 100644--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_001.log+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_001.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_002.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_002.logindex 14ca3b1ee8..7dc980e2cf 100644--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_002.log+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_002.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_003.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_003.logindex c154368638..7886233808 100644--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_003.log+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_003.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_004.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_004.logindex 5234b65b2a..3c0b337c13 100644--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_004.log+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_004.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_005.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_005.logindex 9d3c125ec7..42b28cbdd6 100644--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_005.log+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_005.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_ipython/prompt_001.log b/tests/integration/mock/CodeActAgent/test_ipython/prompt_001.logindex c0b99ce0e6..2ad15757b9 100644--- a/tests/integration/mock/CodeActAgent/test_ipython/prompt_001.log+++ b/tests/integration/mock/CodeActAgent/test_ipython/prompt_001.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>diff --git a/tests/integration/mock/CodeActAgent/test_ipython/prompt_002.log b/tests/integration/mock/CodeActAgent/test_ipython/prompt_002.logindex 544403208a..42feaa6dca 100644--- a/tests/integration/mock/CodeActAgent/test_ipython/prompt_002.log+++ b/tests/integration/mock/CodeActAgent/test_ipython/prompt_002.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_ipython/prompt_003.log b/tests/integration/mock/CodeActAgent/test_ipython/prompt_003.logindex 1eb6cc6274..152fce2f92 100644--- a/tests/integration/mock/CodeActAgent/test_ipython/prompt_003.log+++ b/tests/integration/mock/CodeActAgent/test_ipython/prompt_003.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.log b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.logindex ed28f3b2ad..d67b661233 100644--- a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.log+++ b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>diff --git a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.log b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.logindex 5b67d25233..19cfefab7e 100644--- a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.log+++ b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.log b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.logindex a5e6549bf3..e0b458c592 100644--- a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.log+++ b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.log b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.logindex c4c3175080..b2ccaff503 100644--- a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.log+++ b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.log b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.logindex 312e366130..1a08637d5d 100644--- a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.log+++ b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>diff --git a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.log b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.logindex ca4f9fe02e..2514d84339 100644--- a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.log+++ b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.log b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.logindex 08061310e5..3efd232d5a 100644--- a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.log+++ b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.log b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.logindex af83dd9d13..a3a1652887 100644--- a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.log+++ b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.log@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numbNOW, LET'S START!------------Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.logindex 0d198ce1f0..a94ddeb77c 100644--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.log+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task.diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.logindex 4f046938ce..f1df309355 100644--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.log+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.logindex 0ddb465d3a..5244d8f09e 100644--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.log+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.logindex 831b0fe98d..5f49f00dc9 100644--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.log+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.logindex ec72733678..bf576286c4 100644--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.log+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Fix typos in bad.txt. Do not ask me for confirmation at any point.----------@@ -601,13 +599,13 @@ open_file("bad.txt")----------OBSERVATION:-[File: /workspace/bad.txt (4 lines total)]-(this is the beginning of the file)-1|This is a stupid typoo.-2|Really?-3|No mor typos!-4|Enjoy!-(this is the end of the file)+[File: /workspace/bad.txt (4 lines total)]+(this is the beginning of the file)+1|This is a stupid typoo.+2|Really?+3|No mor typos!+4|Enjoy!+(this is the end of the file)----------@@ -629,12 +627,12 @@ edit_file_by_replace(----------OBSERVATION:-[No exact match found in bad.txt for-```-This is a stupid typoo.-No mor typos!-```-]+[No exact match found in bad.txt for+```+This is a stupid typoo.+No mor typos!+```+]----------@@ -656,14 +654,14 @@ edit_file_by_replace(----------OBSERVATION:-[File: /workspace/bad.txt (4 lines total after edit)]-(this is the beginning of the file)-1|This is a stupid typo.-2|Really?-3|No mor typos!-4|Enjoy!-(this is the end of the file)-[File updated (edited at line 1). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]+[File: /workspace/bad.txt (4 lines total after edit)]+(this is the beginning of the file)+1|This is a stupid typo.+2|Really?+3|No mor typos!+4|Enjoy!+(this is the end of the file)+[File updated (edited at line 1). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]----------@@ -680,14 +678,14 @@ edit_file_by_replace(----------OBSERVATION:-[File: /workspace/bad.txt (4 lines total after edit)]-(this is the beginning of the file)-1|This is a stupid typo.-2|Really?-3|No more typos!-4|Enjoy!-(this is the end of the file)-[File updated (edited at line 3). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]+[File: /workspace/bad.txt (4 lines total after edit)]+(this is the beginning of the file)+1|This is a stupid typo.+2|Really?+3|No more typos!+4|Enjoy!+(this is the end of the file)+[File updated (edited at line 3). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]ENVIRONMENT REMINDER: You have 10 turns left to complete the task.diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.log b/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.logindex 69a60d8dbb..66c5aae295 100644--- a/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.log+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task.diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.log b/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.logindex c64727d5e1..7075c75a0a 100644--- a/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.log+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.log b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.logindex 65bbb95fc3..c226985b13 100644--- a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.log+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task.diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.log b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.logindex 22fe91d275..c9c294b01b 100644--- a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.log+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.log b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.logindex 1542f5f3e8..91e38ac6d3 100644--- a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.log+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Install and import pymsgbox==1.0.9 and print it's version in /workspace/test.txt. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.log b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.logindex 1fcc8aae38..e94a1922c5 100644--- a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.log+++ b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.ENVIRONMENT REMINDER: You have 14 turns left to complete the task.diff --git a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.log b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.logindex e06f9bd513..ef9bb1469b 100644--- a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.log+++ b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.log b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.logindex 0c54161524..7186f107d9 100644--- a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.log+++ b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.----------diff --git a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_004.log b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_004.logindex d8a0cfb83a..79be0c2340 100644--- a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_004.log+++ b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_004.log@@ -587,8 +587,6 @@ exitNOW, LET'S START!------------Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.----------