The TreeAgent provides secure directory navigation and file system exploration capabilities for mindX agents. It enables agents to explore directory structures and find files within a sandboxed root path, ensuring safe file system operations.
File: tools/tree_agent.py
Class: TreeAgent
Version: 1.0.0
Status: ✅ Active
ls and find)class TreeAgent(BaseTool):
- root_path: Path - Sandboxed root directory
- shell: ShellCommandTool - For executing commands
- memory_agent: MemoryAgent - For logging operations
from agents.tree_agent import TreeAgent
agent = TreeAgent(root_path="/home/user/project")
List directory contents
result = await agent.execute("ls -la")
# Find files matching pattern
result = await agent.execute("find . -name '.py'")
Only allows specific safe commands:
ls: List directory contentsfind: Search for filesAll other commands are rejected with an error message.
All commands are executed relative to the specified root path:
Commands are automatically prefixed with the root path:
# User command: "find . -name file.txt"
Executed as: "find /root/path . -name file.txt"
ls and findtree, stat, file commandsAll operations are logged to the memory agent:
await self.memory_agent.log_process(
process_name='tree_agent_execution',
data={'command': full_command, 'success': success, 'result': result},
metadata={'agent_id': self.bdi_agent_ref.agent_id, 'tool_id': 'tree_agent'}
)
Uses ShellCommandTool internally for command execution, inheriting its security and error handling features.
result = await agent.execute("ls")
Returns: List of files and directories
result = await agent.execute("find . -name '.py'")
Returns: List of Python files
result = await agent.execute("find . -type f -name 'test_*.py'")
Returns: Test files matching pattern
tools.shell_command_tool.ShellCommandTool: For command executioncore.bdi_agent.BaseTool: Base tool classagents.memory_agent.MemoryAgent: For operation logging# Validate command
if not command.startswith("ls") and not command.startswith("find"):
return "Error: Only 'ls' and 'find' commands are allowed."
Construct sandboxed command
parts = command.split(" ")
cmd_base = parts[0]
cmd_args = " ".join(parts[1:])
full_command = f"{cmd_base} {self.root_path} {cmd_args}"
Execute via shell tool
success, result = await self.shell.execute(command=full_command)