Skip to content

Commit 338737d

Browse files
committed
Added an optional sources parameter to CodeExecutorAgent for more control on which agents can contribute code. Also moved the _extract_markdown_code_blocks to an member method so that it can be overriden by subclasses.
1 parent 403844e commit 338737d

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,6 @@
99
from ._base_chat_agent import BaseChatAgent
1010

1111

12-
def _extract_markdown_code_blocks(markdown_text: str) -> List[CodeBlock]:
13-
pattern = re.compile(r"```(?:\s*([\w\+\-]+))?\n([\s\S]*?)```")
14-
matches = pattern.findall(markdown_text)
15-
code_blocks: List[CodeBlock] = []
16-
for match in matches:
17-
language = match[0].strip() if match[0] else ""
18-
code_content = match[1]
19-
code_blocks.append(CodeBlock(code=code_content, language=language))
20-
return code_blocks
21-
22-
2312
class CodeExecutorAgent(BaseChatAgent):
2413
"""An agent that extracts and executes code snippets found in received messages and returns the output.
2514
@@ -75,9 +64,11 @@ def __init__(
7564
code_executor: CodeExecutor,
7665
*,
7766
description: str = "A computer terminal that performs no other action than running Python scripts (provided to it quoted in ```python code blocks), or sh shell scripts (provided to it quoted in ```sh code blocks).",
67+
sources: Sequence[str] | None = None,
7868
) -> None:
7969
super().__init__(name=name, description=description)
8070
self._code_executor = code_executor
71+
self._sources = sources
8172

8273
@property
8374
def produced_message_types(self) -> Sequence[type[ChatMessage]]:
@@ -89,7 +80,9 @@ async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token:
8980
code_blocks: List[CodeBlock] = []
9081
for msg in messages:
9182
if isinstance(msg, TextMessage):
92-
code_blocks.extend(_extract_markdown_code_blocks(msg.content))
83+
if self._sources is not None and msg.source not in self._sources:
84+
continue
85+
code_blocks.extend(self._extract_markdown_code_blocks(msg.content))
9386
if code_blocks:
9487
# Execute the code blocks.
9588
result = await self._code_executor.execute_code_blocks(code_blocks, cancellation_token=cancellation_token)
@@ -114,3 +107,13 @@ async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token:
114107
async def on_reset(self, cancellation_token: CancellationToken) -> None:
115108
"""It it's a no-op as the code executor agent has no mutable state."""
116109
pass
110+
111+
def _extract_markdown_code_blocks(self, markdown_text: str) -> List[CodeBlock]:
112+
pattern = re.compile(r"```(?:\s*([\w\+\-]+))?\n([\s\S]*?)```")
113+
matches = pattern.findall(markdown_text)
114+
code_blocks: List[CodeBlock] = []
115+
for match in matches:
116+
language = match[0].strip() if match[0] else ""
117+
code_content = match[1]
118+
code_blocks.append(CodeBlock(code=code_content, language=language))
119+
return code_blocks

0 commit comments

Comments
 (0)