Skip to content

A programming framework for agentic AI

Code executor cleanup (#37)

Eric Zhua97703e4418c
diff --git a/src/agnext/agent_components/code_executor/__init__.py b/src/agnext/agent_components/code_executor/__init__.py
index 5df725747..adedcb8bb 100644
--- a/src/agnext/agent_components/code_executor/__init__.py
+++ b/src/agnext/agent_components/code_executor/__init__.py
@@ -1,14 +1,11 @@
-from ._base import CodeBlock, CodeExecutor, CodeExtractor, CodeResult
+from ._base import CodeBlock, CodeExecutor, CodeResult
from ._impl.command_line_code_result import CommandLineCodeResult
from ._impl.local_commandline_code_executor import LocalCommandLineCodeExecutor
-from ._impl.markdown_code_extractor import MarkdownCodeExtractor
__all__ = [
"LocalCommandLineCodeExecutor",
- "MarkdownCodeExtractor",
"CommandLineCodeResult",
"CodeBlock",
"CodeResult",
"CodeExecutor",
- "CodeExtractor",
]
diff --git a/src/agnext/agent_components/code_executor/_base.py b/src/agnext/agent_components/code_executor/_base.py
index 46fabb3f3..2d05629bb 100644
--- a/src/agnext/agent_components/code_executor/_base.py
+++ b/src/agnext/agent_components/code_executor/_base.py
@@ -23,22 +23,6 @@ class CodeResult:
output: str
-class CodeExtractor(Protocol):
- """Extracts code blocks from a message."""
-
- # TODO support text or multimodal message directly
- def extract_code_blocks(self, message: str) -> List[CodeBlock]:
- """Extract code blocks from a message.
-
- Args:
- message (str): The message to extract code blocks from.
-
- Returns:
- List[CodeBlock]: The extracted code blocks.
- """
- ...
-
-
@runtime_checkable
class CodeExecutor(Protocol):
"""Executes code blocks and returns the result."""
diff --git a/src/agnext/agent_components/code_executor/_impl/local_commandline_code_executor.py b/src/agnext/agent_components/code_executor/_impl/local_commandline_code_executor.py
index 927395732..fe65a39e6 100644
--- a/src/agnext/agent_components/code_executor/_impl/local_commandline_code_executor.py
+++ b/src/agnext/agent_components/code_executor/_impl/local_commandline_code_executor.py
@@ -2,7 +2,6 @@
# Credit to original authors
import logging
-import re
import subprocess
import sys
import warnings
@@ -19,9 +18,8 @@ from ...func_with_reqs import (
build_python_functions_file,
to_stub,
)
-from .._base import CodeBlock, CodeExecutor, CodeExtractor
+from .._base import CodeBlock, CodeExecutor
from .command_line_code_result import CommandLineCodeResult
-from .markdown_code_extractor import MarkdownCodeExtractor
from .utils import PYTHON_VARIANTS, get_file_name_from_content, lang_to_cmd, silence_pip # type: ignore
__all__ = ("LocalCommandLineCodeExecutor",)
@@ -30,7 +28,15 @@ A = ParamSpec("A")
class LocalCommandLineCodeExecutor(CodeExecutor):
- SUPPORTED_LANGUAGES: ClassVar[List[str]] = ["bash", "shell", "sh", "pwsh", "powershell", "ps1", "python"]
+ SUPPORTED_LANGUAGES: ClassVar[List[str]] = [
+ "bash",
+ "shell",
+ "sh",
+ "pwsh",
+ "powershell",
+ "ps1",
+ "python",
+ ]
FUNCTION_PROMPT_TEMPLATE: ClassVar[
str
] = """You have access to the following user defined functions. They can be accessed from the module called `$module_name` by their function names.
@@ -44,7 +50,11 @@ $functions"""
timeout: int = 60,
work_dir: Union[Path, str] = Path("."),
functions: Sequence[
- Union[FunctionWithRequirements[Any, A], Callable[..., Any], FunctionWithRequirementsStr]
+ Union[
+ FunctionWithRequirements[Any, A],
+ Callable[..., Any],
+ FunctionWithRequirementsStr,
+ ]
] = [],
functions_module: str = "functions",
):
@@ -134,33 +144,6 @@ $functions"""
"""(Experimental) The working directory for the code execution."""
return self._work_dir
- @property
- def code_extractor(self) -> CodeExtractor:
- """(Experimental) Export a code extractor that can be used by an agent."""
- return MarkdownCodeExtractor()
-
- @staticmethod
- def sanitize_command(lang: str, code: str) -> None:
- """
- Sanitize the code block to prevent dangerous commands.
- This approach acknowledges that while Docker or similar
- containerization/sandboxing technologies provide a robust layer of security,
- not all users may have Docker installed or may choose not to use it.
- Therefore, having a baseline level of protection helps mitigate risks for users who,
- either out of choice or necessity, run code outside of a sandboxed environment.
- """
- dangerous_patterns = [
- (r"\brm\s+-rf\b", "Use of 'rm -rf' command is not allowed."),
- (r"\bmv\b.*?\s+/dev/null", "Moving files to /dev/null is not allowed."),
- (r"\bdd\b", "Use of 'dd' command is not allowed."),
- (r">\s*/dev/sd[a-z][1-9]?", "Overwriting disk blocks directly is not allowed."),
- (r":\(\)\{\s*:\|\:&\s*\};:", "Fork bombs are not allowed."),
- ]
- if lang in ["bash", "shell", "sh"]:
- for pattern, message in dangerous_patterns:
- if re.search(pattern, code):
- raise ValueError(f"Potentially dangerous command detected: {message}")
-
def _setup_functions(self) -> None:
func_file_content = build_python_functions_file(self._functions)
func_file = self._work_dir / f"{self._functions_module}.py"
@@ -178,7 +161,11 @@ $functions"""
try:
result = subprocess.run(
- cmd, cwd=self._work_dir, capture_output=True, text=True, timeout=float(self._timeout)
+ cmd,
+ cwd=self._work_dir,
+ capture_output=True,
+ text=True,
+ timeout=float(self._timeout),
)
except subprocess.TimeoutExpired as e:
raise ValueError("Pip install timed out") from e
@@ -216,7 +203,6 @@ $functions"""
lang, code = code_block.language, code_block.code
lang = lang.lower()
- LocalCommandLineCodeExecutor.sanitize_command(lang, code)
code = silence_pip(code, lang)
if lang in PYTHON_VARIANTS:
@@ -232,7 +218,11 @@ $functions"""
# Check if there is a filename comment
filename = get_file_name_from_content(code, self._work_dir)
except ValueError:
- return CommandLineCodeResult(exit_code=1, output="Filename is not in the workspace", code_file=None)
+ return CommandLineCodeResult(
+ exit_code=1,
+ output="Filename is not in the workspace",
+ code_file=None,
+ )
if filename is None:
# create a file with an automatically generated name
@@ -249,7 +239,11 @@ $functions"""
try:
result = subprocess.run(
- cmd, cwd=self._work_dir, capture_output=True, text=True, timeout=float(self._timeout)
+ cmd,
+ cwd=self._work_dir,
+ capture_output=True,
+ text=True,
+ timeout=float(self._timeout),
)
except subprocess.TimeoutExpired:
logs_all += "\n Timeout"
@@ -269,4 +263,7 @@ $functions"""
def restart(self) -> None:
"""(Experimental) Restart the code executor."""
- warnings.warn("Restarting local command line code executor is not supported. No action is taken.", stacklevel=2)
+ warnings.warn(
+ "Restarting local command line code executor is not supported. No action is taken.",
+ stacklevel=2,
+ )
diff --git a/src/agnext/agent_components/code_executor/_impl/markdown_code_extractor.py b/src/agnext/agent_components/code_executor/_impl/markdown_code_extractor.py
deleted file mode 100644
index 18239ecd6..000000000
--- a/src/agnext/agent_components/code_executor/_impl/markdown_code_extractor.py
+++ /dev/null
@@ -1,37 +0,0 @@
-# File based from: https://github.com/microsoft/autogen/blob/main/autogen/coding/markdown_code_extractor.py
-# Credit to original authors
-
-import re
-from typing import List
-
-from .._base import CodeBlock, CodeExtractor
-from .utils import CODE_BLOCK_PATTERN, infer_lang
-
-__all__ = ("MarkdownCodeExtractor",)
-
-
-class MarkdownCodeExtractor(CodeExtractor):
- """(Experimental) A class that extracts code blocks from a message using Markdown syntax."""
-
- def extract_code_blocks(self, message: str) -> List[CodeBlock]:
- """(Experimental) Extract code blocks from a message. If no code blocks are found,
- return an empty list.
-
- Args:
- message (str): The message to extract code blocks from.
-
- Returns:
- List[CodeBlock]: The extracted code blocks or an empty list.
- """
-
- match = re.findall(CODE_BLOCK_PATTERN, message, flags=re.DOTALL)
- if not match:
- return []
- code_blocks: List[CodeBlock] = []
- for lang, code in match:
- if lang == "":
- lang = infer_lang(code)
- if lang == "unknown":
- lang = ""
- code_blocks.append(CodeBlock(code=code, language=lang))
- return code_blocks
diff --git a/src/agnext/chat/agents/oai_assistant.py b/src/agnext/chat/agents/oai_assistant.py
index d4461ef02..7b1cff1f5 100644
--- a/src/agnext/chat/agents/oai_assistant.py
+++ b/src/agnext/chat/agents/oai_assistant.py
@@ -1,4 +1,4 @@
-from typing import Any, Callable, Dict, List, Mapping
+from typing import Any, List, Mapping
import openai
from openai.types.beta import AssistantResponseFormatParam
@@ -18,13 +18,11 @@ class OpenAIAssistantAgent(BaseChatAgent, TypeRoutedAgent):
client: openai.AsyncClient,
assistant_id: str,
thread_id: str,
- tools: Dict[str, Callable[..., str]] | None = None,
) -> None:
super().__init__(name, description, runtime)
self._client = client
self._assistant_id = assistant_id
self._thread_id = thread_id
- self._tools = tools or {}
@message_handler(TextMessage)
async def on_text_message(self, message: TextMessage, cancellation_token: CancellationToken) -> None:
diff --git a/src/agnext/chat/agents/random_agent.py b/src/agnext/chat/agents/random_agent.py
deleted file mode 100644
index afe6bb601..000000000
--- a/src/agnext/chat/agents/random_agent.py
+++ /dev/null
@@ -1,36 +0,0 @@
-import random
-
-from agnext.agent_components.type_routed_agent import TypeRoutedAgent, message_handler
-from agnext.chat.types import RespondNow, TextMessage
-from agnext.core import CancellationToken
-
-from ..agents.base import BaseChatAgent
-
-
-class RandomResponseAgent(BaseChatAgent, TypeRoutedAgent):
- # TODO: use require_response
- @message_handler(RespondNow)
- async def on_chat_message_with_cancellation(
- self, message: RespondNow, cancellation_token: CancellationToken
- ) -> TextMessage:
- # Generate a random response.
- response_body = random.choice(
- [
- "Hello!",
- "Hi!",
- "Hey!",
- "How are you?",
- "What's up?",
- "Good day!",
- "Good morning!",
- "Good evening!",
- "Good afternoon!",
- "Good night!",
- "Good bye!",
- "Bye!",
- "See you later!",
- "See you soon!",
- "See you!",
- ]
- )
- return TextMessage(content=response_body, source=self.name)
diff --git a/tests/execution/test_markdown_code_extractor.py b/tests/execution/test_markdown_code_extractor.py
deleted file mode 100644
index 13894b3b5..000000000
--- a/tests/execution/test_markdown_code_extractor.py
+++ /dev/null
@@ -1,118 +0,0 @@
-# File based from: https://github.com/microsoft/autogen/blob/main/test/coding/test_markdown_code_extractor.py
-# Credit to original authors
-
-from agnext.agent_components.code_executor import MarkdownCodeExtractor
-
-_message_1 = """
-Example:
-```
-print("hello extract code")
-```
-"""
-
-_message_2 = """Example:
-```python
-def scrape(url):
- import requests
- from bs4 import BeautifulSoup
- response = requests.get(url)
- soup = BeautifulSoup(response.text, "html.parser")
- title = soup.find("title").text
- text = soup.find("div", {"id": "bodyContent"}).text
- return title, text
-```
-Test:
-```python
-url = "https://en.wikipedia.org/wiki/Web_scraping"
-title, text = scrape(url)
-print(f"Title: {title}")
-print(f"Text: {text}")
-```
-"""
-
-_message_3 = """
-Example:
- ```python
- def scrape(url):
- import requests
- from bs4 import BeautifulSoup
- response = requests.get(url)
- soup = BeautifulSoup(response.text, "html.parser")
- title = soup.find("title").text
- text = soup.find("div", {"id": "bodyContent"}).text
- return title, text
- ```
-"""
-
-_message_4 = """
-Example:
-``` python
-def scrape(url):
- import requests
- from bs4 import BeautifulSoup
- response = requests.get(url)
- soup = BeautifulSoup(response.text, "html.parser")
- title = soup.find("title").text
- text = soup.find("div", {"id": "bodyContent"}).text
- return title, text
-```
-""".replace(
- "\n", "\r\n"
-)
-
-_message_5 = """
-Test bash script:
-```bash
-echo 'hello world!'
-```
-"""
-
-_message_6 = """
-Test some C# code, expecting ""
-```
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace ConsoleApplication1
-{
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Hello World");
- }
- }
-}
-```
-"""
-
-_message_7 = """
-Test some message that has no code block.
-"""
-
-
-def test_extract_code() -> None:
- extractor = MarkdownCodeExtractor()
-
- code_blocks = extractor.extract_code_blocks(_message_1)
- assert len(code_blocks) == 1 and code_blocks[0].language == "python"
-
- code_blocks = extractor.extract_code_blocks(_message_2)
- assert len(code_blocks) == 2 and code_blocks[0].language == "python" and code_blocks[1].language == "python"
-
- code_blocks = extractor.extract_code_blocks(_message_3)
- assert len(code_blocks) == 1 and code_blocks[0].language == "python"
-
- code_blocks = extractor.extract_code_blocks(_message_4)
- assert len(code_blocks) == 1 and code_blocks[0].language == "python"
-
- code_blocks = extractor.extract_code_blocks(_message_5)
- assert len(code_blocks) == 1 and code_blocks[0].language == "bash"
-
- code_blocks = extractor.extract_code_blocks(_message_6)
- assert len(code_blocks) == 1 and code_blocks[0].language == ""
-
- code_blocks = extractor.extract_code_blocks(_message_7)
- assert len(code_blocks) == 0