diff --git a/mini_agent/__init__.py b/mini_agent/__init__.py index 3c8742a..1d7b1c5 100644 --- a/mini_agent/__init__.py +++ b/mini_agent/__init__.py @@ -3,18 +3,20 @@ """ from .agent import MiniAgent -from .llm import SimpleLLM +from .llm import SimpleLLM, LLMResponse from .tools import ToolCollection, PythonExecutor, FileEditor, BashExecutor -from .schema import Message, Memory, AgentState +from .schema import Message, Memory, AgentState, Role __all__ = [ "MiniAgent", - "SimpleLLM", + "SimpleLLM", + "LLMResponse", "ToolCollection", "PythonExecutor", "FileEditor", "BashExecutor", "Message", "Memory", - "AgentState" + "AgentState", + "Role", ] \ No newline at end of file diff --git a/mini_agent/agent.py b/mini_agent/agent.py index e59d534..373eb83 100644 --- a/mini_agent/agent.py +++ b/mini_agent/agent.py @@ -19,6 +19,8 @@ def __init__( system_prompt: Optional[str] = None, max_steps: int = 10 ): + if max_steps < 1: + raise ValueError(f"max_steps must be >= 1, got {max_steps}") self.name = name self.llm = llm self.tools = ToolCollection() @@ -128,7 +130,8 @@ async def act(self) -> None: # 准备结果消息 if result.success: result_content = result.output - print(f"✅ 工具执行成功: {result_content[:100]}...") + preview = result_content[:100] if result_content else "(empty)" + print(f"✅ 工具执行成功: {preview}...") else: result_content = f"错误: {result.error}" print(f"❌ 工具执行失败: {result.error}") diff --git a/mini_agent/schema.py b/mini_agent/schema.py index 150415a..a1d10eb 100644 --- a/mini_agent/schema.py +++ b/mini_agent/schema.py @@ -3,7 +3,7 @@ """ from enum import Enum from typing import List, Optional, Dict, Any -from pydantic import BaseModel +from pydantic import BaseModel, Field class Role(str, Enum): @@ -39,7 +39,7 @@ def tool_message(cls, content: str, tool_call_id: str) -> "Message": class Memory(BaseModel): - messages: List[Message] = [] + messages: List[Message] = Field(default_factory=list) def add_message(self, message: Message): self.messages.append(message) diff --git a/mini_agent/tools.py b/mini_agent/tools.py index 0aae704..fb08e2d 100644 --- a/mini_agent/tools.py +++ b/mini_agent/tools.py @@ -141,6 +141,12 @@ class BashExecutor(BaseTool): async def execute(self, command: str, **kwargs) -> ToolResult: try: + import warnings + warnings.warn( + "BashExecutor uses shell=True which is a security risk. " + "Only use with trusted input.", + stacklevel=2, + ) result = subprocess.run( command, shell=True,