Commit 6d7282f9 by sunqi02

增加Agent skills系统必备技能

read_file
1 parent 6ce9de3b
......@@ -99,6 +99,14 @@ class AgentHub:
group_name="basic",
)
# Register sandboxed read_file tool for reading project files
read_file = self._make_read_file_tool()
self.toolkit.register_tool_function(
read_file,
func_name="read_file",
group_name="basic",
)
# 构建每个 agent 的专属 toolkit(按 skills 字段过滤)
self.agent_toolkits: Dict[str, Toolkit] = {}
for agent_code, agent_config in self.agents.items():
......@@ -348,6 +356,76 @@ class AgentHub:
response = await sub_agent.reply(Msg("user", instruction, "user"))
return response.get_text_content()
def _make_read_file_tool(self):
"""Create a sandboxed read_file tool function.
The sandbox root is the backend project directory (parent of app/).
"""
root_dir = Path(__file__).parent.parent.parent.resolve()
async def read_file(file_path: str) -> ToolResponse:
"""Read the content of a local file. Path is relative to the project root.
Use this to read SKILL.md files, MEMORY.md, configuration files, etc.
Example: read_file('skills/get_weather/SKILL.md')
Args:
file_path: File path relative to the project root,
e.g. 'skills/meeting_minutes/SKILL.md' or 'config/agents.json'
Returns:
File content as text, truncated to 10000 characters if too large.
"""
try:
# Normalize: convert backslashes, strip single leading ./
normalized = file_path.replace("\\", "/")
if normalized.startswith("./"):
normalized = normalized[2:]
full_path = (root_dir / normalized).resolve()
# Sandbox check: prevent path traversal (must be BEFORE exists check)
if not str(full_path).startswith(str(root_dir)):
return ToolResponse(
content=[TextBlock(
type="text",
text="❌ Access denied: path escapes project root"
)]
)
if not full_path.exists():
return ToolResponse(
content=[TextBlock(
type="text",
text=f"❌ File not found: {file_path}"
)]
)
if not full_path.is_file():
return ToolResponse(
content=[TextBlock(
type="text",
text=f"❌ Not a file: {file_path}"
)]
)
content = full_path.read_text(encoding="utf-8")
if len(content) > 10000:
content = content[:10000] + "\n...[truncated]"
return ToolResponse(
content=[TextBlock(type="text", text=content)]
)
except Exception as e:
return ToolResponse(
content=[TextBlock(
type="text",
text=f"❌ Error reading file: {str(e)}"
)]
)
return read_file
def _make_call_sub_agent_tool(self):
"""Create closure tool function for registration to Toolkit."""
async def call_sub_agent(agent_code: str, instruction: str) -> ToolResponse:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!