-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
66 lines (52 loc) · 2.32 KB
/
Copy pathtools.py
File metadata and controls
66 lines (52 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
============================================================================
TOOLS MODULE — Agent Capabilities
============================================================================
Tools that agents can use during swarm execution.
- File read/write
- Command execution
============================================================================
"""
from pathlib import Path
from command_safety import CommandExecutor
class AgentTools:
def __init__(self, workspace_path: Path):
self.workspace = workspace_path
# Auto-approve safe and moderate commands. Dangerous ones will need approval.
self.executor = CommandExecutor(cwd=str(self.workspace))
def write_file(self, rel_path: str, content: str) -> str:
"""Write a file inside the workspace."""
path = self.workspace / rel_path
# Security: Prevent writing outside workspace
try:
path.resolve().relative_to(self.workspace.resolve())
except ValueError:
return f"Error: Cannot write outside workspace ({rel_path})"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
return f"File written: {rel_path}"
def read_file(self, rel_path: str) -> str:
"""Read a file from the workspace."""
path = self.workspace / rel_path
try:
path.resolve().relative_to(self.workspace.resolve())
except ValueError:
return f"Error: Cannot read outside workspace ({rel_path})"
if not path.exists():
return f"Error: File not found ({rel_path})"
try:
return path.read_text(encoding="utf-8")
except Exception as e:
return f"Error reading file: {e}"
def run_command(self, command: str) -> str:
"""Run a shell command inside the workspace."""
result = self.executor.execute(command, timeout=300)
out = []
out.append(f"Command: {command}")
out.append(f"Exit code: {result.returncode}")
out.append(f"Safety: {result.safety} (Approved: {result.approved})")
if result.stdout:
out.append(f"STDOUT:\n{result.stdout}")
if result.stderr:
out.append(f"STDERR:\n{result.stderr}")
return "\n".join(out)