Files
deer-flow/backend/src/sandbox/local/local_sandbox.py

78 lines
2.6 KiB
Python
Raw Normal View History

2026-01-14 07:19:34 +08:00
import os
import subprocess
from pathlib import Path
2026-01-14 07:19:34 +08:00
from src.sandbox.local.list_dir import list_dir
from src.sandbox.sandbox import Sandbox
class LocalSandbox(Sandbox):
def __init__(self, id: str, path_mappings: dict[str, str] | None = None):
"""
Initialize local sandbox with optional path mappings.
Args:
id: Sandbox identifier
path_mappings: Dictionary mapping container paths to local paths
Example: {"/mnt/skills": "/absolute/path/to/skills"}
"""
2026-01-14 07:19:34 +08:00
super().__init__(id)
self.path_mappings = path_mappings or {}
def _resolve_path(self, path: str) -> str:
"""
Resolve container path to actual local path using mappings.
Args:
path: Path that might be a container path
Returns:
Resolved local path
"""
path_str = str(path)
# Try each mapping (longest prefix first for more specific matches)
for container_path, local_path in sorted(self.path_mappings.items(), key=lambda x: len(x[0]), reverse=True):
if path_str.startswith(container_path):
# Replace the container path prefix with local path
relative = path_str[len(container_path) :].lstrip("/")
resolved = str(Path(local_path) / relative) if relative else local_path
return resolved
# No mapping found, return original path
return path_str
2026-01-14 07:19:34 +08:00
def execute_command(self, command: str) -> str:
result = subprocess.run(
command,
executable="/bin/zsh",
shell=True,
capture_output=True,
text=True,
timeout=30,
)
output = result.stdout
if result.stderr:
output += f"\nStd Error:\n{result.stderr}" if output else result.stderr
if result.returncode != 0:
output += f"\nExit Code: {result.returncode}"
return output if output else "(no output)"
def list_dir(self, path: str, max_depth=2) -> list[str]:
resolved_path = self._resolve_path(path)
return list_dir(resolved_path, max_depth)
2026-01-14 07:19:34 +08:00
def read_file(self, path: str) -> str:
resolved_path = self._resolve_path(path)
with open(resolved_path) as f:
2026-01-14 07:19:34 +08:00
return f.read()
def write_file(self, path: str, content: str, append: bool = False) -> None:
resolved_path = self._resolve_path(path)
dir_path = os.path.dirname(resolved_path)
2026-01-14 07:19:34 +08:00
if dir_path:
os.makedirs(dir_path, exist_ok=True)
mode = "a" if append else "w"
with open(resolved_path, mode) as f:
2026-01-14 07:19:34 +08:00
f.write(content)