Skip to content

Commit 2e60d25

Browse files
xingyaowwenyst
andauthored
[Agent, LLM] Make sure codeact agent produce message in u/a/u/a order (#3193)
* make sure codeact agent produce message in u/a/u/a order * integration tests * sync message changes to codeact swe * fix integration tests --------- Co-authored-by: Engel Nyst <[email protected]>
1 parent 0627af8 commit 2e60d25

34 files changed

+43
-95
lines changed

agenthub/codeact_agent/codeact_agent.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,13 @@ def _get_messages(self, state: State) -> list[dict[str, str]]:
222222

223223
# add regular message
224224
if message:
225-
messages.append(message)
225+
# handle error if the message is the SAME role as the previous message
226+
# litellm.exceptions.BadRequestError: litellm.BadRequestError: OpenAIException - Error code: 400 - {'detail': 'Only supports u/a/u/a/u...'}
227+
# there should not have two consecutive messages from the same role
228+
if messages and messages[-1]['role'] == message['role']:
229+
messages[-1]['content'] += '\n\n' + message['content']
230+
else:
231+
messages.append(message)
226232

227233
# the latest user message is important:
228234
# we want to remind the agent of the environment constraints

agenthub/codeact_swe_agent/codeact_swe_agent.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ def _get_messages(self, state: State) -> list[dict[str, str]]:
173173

174174
# add regular message
175175
if message:
176-
messages.append(message)
176+
# handle error if the message is the SAME role as the previous message
177+
# litellm.exceptions.BadRequestError: litellm.BadRequestError: OpenAIException - Error code: 400 - {'detail': 'Only supports u/a/u/a/u...'}
178+
# there should not have two consecutive messages from the same role
179+
if messages and messages[-1]['role'] == message['role']:
180+
messages[-1]['content'] += '\n\n' + message['content']
181+
else:
182+
messages.append(message)
177183

178184
# the latest user message is important:
179185
# we want to remind the agent of the environment constraints

tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Browse localhost:8000, and tell me the ultimate answer to life. Do not ask me for confirmation at any point.
402400

403401
ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>

tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Browse localhost:8000, and tell me the ultimate answer to life. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_edits/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Fix typos in bad.txt. Do not ask me for confirmation at any point.
402400

403401
ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>

tests/integration/mock/CodeActAgent/test_edits/prompt_002.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Fix typos in bad.txt. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_edits/prompt_003.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Fix typos in bad.txt. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_edits/prompt_004.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Fix typos in bad.txt. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_edits/prompt_005.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Fix typos in bad.txt. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_ipython/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.
402400

403401
ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>

tests/integration/mock/CodeActAgent/test_ipython/prompt_002.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_ipython/prompt_003.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
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.
402400

403401
ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>

tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
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.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
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.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
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.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.
402400

403401
ENVIRONMENT REMINDER: You have 14 turns left to complete the task. When finished reply with <finish></finish>

tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.log

-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,6 @@ The server is running on port 5000 with PID 126. You can access the list of numb
396396

397397
NOW, LET'S START!
398398

399-
----------
400-
401399
Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.
402400

403401
----------

tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Fix typos in bad.txt. Do not ask me for confirmation at any point.
593591

594592
ENVIRONMENT REMINDER: You have 14 turns left to complete the task.

tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Fix typos in bad.txt. Do not ask me for confirmation at any point.
593591

594592
----------

tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Fix typos in bad.txt. Do not ask me for confirmation at any point.
593591

594592
----------

tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Fix typos in bad.txt. Do not ask me for confirmation at any point.
593591

594592
----------

tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.log

+29-31
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Fix typos in bad.txt. Do not ask me for confirmation at any point.
593591

594592
----------
@@ -601,13 +599,13 @@ open_file("bad.txt")
601599
----------
602600

603601
OBSERVATION:
604-
[File: /workspace/bad.txt (4 lines total)]
605-
(this is the beginning of the file)
606-
1|This is a stupid typoo.
607-
2|Really?
608-
3|No mor typos!
609-
4|Enjoy!
610-
(this is the end of the file)
602+
[File: /workspace/bad.txt (4 lines total)]
603+
(this is the beginning of the file)
604+
1|This is a stupid typoo.
605+
2|Really?
606+
3|No mor typos!
607+
4|Enjoy!
608+
(this is the end of the file)
611609

612610

613611
----------
@@ -629,12 +627,12 @@ edit_file_by_replace(
629627
----------
630628

631629
OBSERVATION:
632-
[No exact match found in bad.txt for
633-
```
634-
This is a stupid typoo.
635-
No mor typos!
636-
```
637-
]
630+
[No exact match found in bad.txt for
631+
```
632+
This is a stupid typoo.
633+
No mor typos!
634+
```
635+
]
638636

639637

640638
----------
@@ -656,14 +654,14 @@ edit_file_by_replace(
656654
----------
657655

658656
OBSERVATION:
659-
[File: /workspace/bad.txt (4 lines total after edit)]
660-
(this is the beginning of the file)
661-
1|This is a stupid typo.
662-
2|Really?
663-
3|No mor typos!
664-
4|Enjoy!
665-
(this is the end of the file)
666-
[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.]
657+
[File: /workspace/bad.txt (4 lines total after edit)]
658+
(this is the beginning of the file)
659+
1|This is a stupid typo.
660+
2|Really?
661+
3|No mor typos!
662+
4|Enjoy!
663+
(this is the end of the file)
664+
[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.]
667665

668666

669667
----------
@@ -680,14 +678,14 @@ edit_file_by_replace(
680678
----------
681679

682680
OBSERVATION:
683-
[File: /workspace/bad.txt (4 lines total after edit)]
684-
(this is the beginning of the file)
685-
1|This is a stupid typo.
686-
2|Really?
687-
3|No more typos!
688-
4|Enjoy!
689-
(this is the end of the file)
690-
[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.]
681+
[File: /workspace/bad.txt (4 lines total after edit)]
682+
(this is the beginning of the file)
683+
1|This is a stupid typo.
684+
2|Really?
685+
3|No more typos!
686+
4|Enjoy!
687+
(this is the end of the file)
688+
[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.]
691689

692690

693691
ENVIRONMENT REMINDER: You have 10 turns left to complete the task.

tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.
593591

594592
ENVIRONMENT REMINDER: You have 14 turns left to complete the task.

tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Use Jupyter IPython to write a text file containing 'hello world' to '/workspace/test.txt'. Do not ask me for confirmation at any point.
593591

594592
----------

tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
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.
593591

594592
ENVIRONMENT REMINDER: You have 14 turns left to complete the task.

tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
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.
593591

594592
----------

tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
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.
593591

594592
----------

tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.
593591

594592
ENVIRONMENT REMINDER: You have 14 turns left to complete the task.

tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.
593591

594592
----------

tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.log

-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,6 @@ exit
587587

588588
NOW, LET'S START!
589589

590-
----------
591-
592590
Write a shell script 'hello.sh' that prints 'hello'. Do not ask me for confirmation at any point.
593591

594592
----------

0 commit comments

Comments
 (0)