Skip to content

Commit af527fd

Browse files
authored
Merge branch 'main' into feat/add-remove-assistant-agent-tools
2 parents 8c25461 + e1d5833 commit af527fd

File tree

4 files changed

+586
-2
lines changed

4 files changed

+586
-2
lines changed

python/packages/autogen-agentchat/src/autogen_agentchat/agents/_code_executor_agent.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,23 @@ async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token:
8181
if code_blocks:
8282
# Execute the code blocks.
8383
result = await self._code_executor.execute_code_blocks(code_blocks, cancellation_token=cancellation_token)
84-
return Response(chat_message=TextMessage(content=result.output, source=self.name))
84+
85+
code_output = result.output
86+
if code_output.strip() == "":
87+
# No output
88+
code_output = f"The script ran but produced no output to console. The POSIX exit code was: {result.exit_code}. If you were expecting output, consider revising the script to ensure content is printed to stdout."
89+
elif result.exit_code != 0:
90+
# Error
91+
code_output = f"The script ran, then exited with an error (POSIX exit code: {result.exit_code})\nIts output was:\n{result.output}"
92+
93+
return Response(chat_message=TextMessage(content=code_output, source=self.name))
8594
else:
86-
return Response(chat_message=TextMessage(content="No code blocks found in the thread.", source=self.name))
95+
return Response(
96+
chat_message=TextMessage(
97+
content="No code blocks found in the thread. Please provide at least one markdown-encoded code block to execute (i.e., quoting code in ```python or ```sh code blocks).",
98+
source=self.name,
99+
)
100+
)
87101

88102
async def on_reset(self, cancellation_token: CancellationToken) -> None:
89103
"""It it's a no-op as the code executor agent has no mutable state."""
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import pytest
2+
from autogen_agentchat.agents import CodeExecutorAgent
3+
from autogen_agentchat.base import Response
4+
from autogen_agentchat.messages import TextMessage
5+
from autogen_core import CancellationToken
6+
from autogen_core.components.code_executor import LocalCommandLineCodeExecutor
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_basic_code_execution() -> None:
11+
"""Test basic code execution"""
12+
13+
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
14+
15+
messages = [
16+
TextMessage(
17+
content="""
18+
```python
19+
import math
20+
21+
number = 42
22+
square_root = math.sqrt(number)
23+
print("%0.3f" % (square_root,))
24+
```
25+
""".strip(),
26+
source="assistant",
27+
)
28+
]
29+
response = await agent.on_messages(messages, CancellationToken())
30+
31+
assert isinstance(response, Response)
32+
assert isinstance(response.chat_message, TextMessage)
33+
assert response.chat_message.content.strip() == "6.481"
34+
assert response.chat_message.source == "code_executor"
35+
36+
37+
@pytest.mark.asyncio
38+
async def test_code_execution_error() -> None:
39+
"""Test basic code execution"""
40+
41+
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
42+
43+
messages = [
44+
TextMessage(
45+
content="""
46+
```python
47+
import math
48+
49+
number = -1.0
50+
square_root = math.sqrt(number)
51+
print("%0.3f" % (square_root,))
52+
```
53+
""".strip(),
54+
source="assistant",
55+
)
56+
]
57+
response = await agent.on_messages(messages, CancellationToken())
58+
59+
assert isinstance(response, Response)
60+
assert isinstance(response.chat_message, TextMessage)
61+
assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content
62+
assert "ValueError: math domain error" in response.chat_message.content
63+
64+
65+
@pytest.mark.asyncio
66+
async def test_code_execution_no_output() -> None:
67+
"""Test basic code execution"""
68+
69+
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
70+
71+
messages = [
72+
TextMessage(
73+
content="""
74+
```python
75+
import math
76+
77+
number = 42
78+
square_root = math.sqrt(number)
79+
```
80+
""".strip(),
81+
source="assistant",
82+
)
83+
]
84+
response = await agent.on_messages(messages, CancellationToken())
85+
86+
assert isinstance(response, Response)
87+
assert isinstance(response.chat_message, TextMessage)
88+
assert (
89+
"The script ran but produced no output to console. The POSIX exit code was: 0. If you were expecting output, consider revising the script to ensure content is printed to stdout."
90+
in response.chat_message.content
91+
)
92+
93+
94+
@pytest.mark.asyncio
95+
async def test_code_execution_no_block() -> None:
96+
"""Test basic code execution"""
97+
98+
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
99+
100+
messages = [
101+
TextMessage(
102+
content="""
103+
import math
104+
105+
number = 42
106+
square_root = math.sqrt(number)
107+
""".strip(),
108+
source="assistant",
109+
)
110+
]
111+
response = await agent.on_messages(messages, CancellationToken())
112+
113+
assert isinstance(response, Response)
114+
assert isinstance(response.chat_message, TextMessage)
115+
assert (
116+
"No code blocks found in the thread. Please provide at least one markdown-encoded code block"
117+
in response.chat_message.content
118+
)
119+
120+
121+
@pytest.mark.asyncio
122+
async def test_code_execution_multiple_blocks() -> None:
123+
"""Test basic code execution"""
124+
125+
agent = CodeExecutorAgent(name="code_executor", code_executor=LocalCommandLineCodeExecutor())
126+
127+
messages = [
128+
TextMessage(
129+
content="""
130+
```python
131+
import math
132+
133+
number = 42
134+
square_root = math.sqrt(number)
135+
print("%0.3f" % (square_root,))
136+
```
137+
138+
And also:
139+
140+
```python
141+
import time
142+
print(f"The current time is: {time.time()}")
143+
144+
```
145+
146+
And this should result in an error:
147+
```python
148+
import math
149+
150+
number = -1.0
151+
square_root = math.sqrt(number)
152+
print("%0.3f" % (square_root,))
153+
```
154+
155+
""".strip(),
156+
source="assistant",
157+
)
158+
]
159+
response = await agent.on_messages(messages, CancellationToken())
160+
161+
assert isinstance(response, Response)
162+
assert isinstance(response.chat_message, TextMessage)
163+
assert "6.481" in response.chat_message.content
164+
assert "The current time is:" in response.chat_message.content
165+
assert "The script ran, then exited with an error (POSIX exit code: 1)" in response.chat_message.content
166+
assert "ValueError: math domain error" in response.chat_message.content

0 commit comments

Comments
 (0)