File size: 19,353 Bytes
a51a15b |
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
from daytona_sdk.process import SessionExecuteRequest
from typing import Optional
from agentpress.tool import ToolResult, openapi_schema, xml_schema
from sandbox.sandbox import SandboxToolsBase, Sandbox, get_or_start_sandbox
from utils.files_utils import EXCLUDED_FILES, EXCLUDED_DIRS, EXCLUDED_EXT, should_exclude_file, clean_path
from agentpress.thread_manager import ThreadManager
from utils.logger import logger
import os
class SandboxFilesTool(SandboxToolsBase):
"""Tool for executing file system operations in a Daytona sandbox. All operations are performed relative to the /workspace directory."""
def __init__(self, project_id: str, thread_manager: ThreadManager):
super().__init__(project_id, thread_manager)
self.SNIPPET_LINES = 4 # Number of context lines to show around edits
self.workspace_path = "/workspace" # Ensure we're always operating in /workspace
def clean_path(self, path: str) -> str:
"""Clean and normalize a path to be relative to /workspace"""
return clean_path(path, self.workspace_path)
def _should_exclude_file(self, rel_path: str) -> bool:
"""Check if a file should be excluded based on path, name, or extension"""
return should_exclude_file(rel_path)
def _file_exists(self, path: str) -> bool:
"""Check if a file exists in the sandbox"""
try:
self.sandbox.fs.get_file_info(path)
return True
except Exception:
return False
async def get_workspace_state(self) -> dict:
"""Get the current workspace state by reading all files"""
files_state = {}
try:
# Ensure sandbox is initialized
await self._ensure_sandbox()
files = self.sandbox.fs.list_files(self.workspace_path)
for file_info in files:
rel_path = file_info.name
# Skip excluded files and directories
if self._should_exclude_file(rel_path) or file_info.is_dir:
continue
try:
full_path = f"{self.workspace_path}/{rel_path}"
content = self.sandbox.fs.download_file(full_path).decode()
files_state[rel_path] = {
"content": content,
"is_dir": file_info.is_dir,
"size": file_info.size,
"modified": file_info.mod_time
}
except Exception as e:
print(f"Error reading file {rel_path}: {e}")
except UnicodeDecodeError:
print(f"Skipping binary file: {rel_path}")
return files_state
except Exception as e:
print(f"Error getting workspace state: {str(e)}")
return {}
# def _get_preview_url(self, file_path: str) -> Optional[str]:
# """Get the preview URL for a file if it's an HTML file."""
# if file_path.lower().endswith('.html') and self._sandbox_url:
# return f"{self._sandbox_url}/{(file_path.replace('/workspace/', ''))}"
# return None
@openapi_schema({
"type": "function",
"function": {
"name": "create_file",
"description": "Create a new file with the provided contents at a given path in the workspace. The path must be relative to /workspace (e.g., 'src/main.py' for /workspace/src/main.py)",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Path to the file to be created, relative to /workspace (e.g., 'src/main.py')"
},
"file_contents": {
"type": "string",
"description": "The content to write to the file"
},
"permissions": {
"type": "string",
"description": "File permissions in octal format (e.g., '644')",
"default": "644"
}
},
"required": ["file_path", "file_contents"]
}
}
})
@xml_schema(
tag_name="create-file",
mappings=[
{"param_name": "file_path", "node_type": "attribute", "path": "."},
{"param_name": "file_contents", "node_type": "content", "path": "."}
],
example='''
<create-file file_path="src/main.py">
File contents go here
</create-file>
'''
)
async def create_file(self, file_path: str, file_contents: str, permissions: str = "644") -> ToolResult:
try:
# Ensure sandbox is initialized
await self._ensure_sandbox()
file_path = self.clean_path(file_path)
full_path = f"{self.workspace_path}/{file_path}"
if self._file_exists(full_path):
return self.fail_response(f"File '{file_path}' already exists. Use update_file to modify existing files.")
# Create parent directories if needed
parent_dir = '/'.join(full_path.split('/')[:-1])
if parent_dir:
self.sandbox.fs.create_folder(parent_dir, "755")
# Write the file content
self.sandbox.fs.upload_file(full_path, file_contents.encode())
self.sandbox.fs.set_file_permissions(full_path, permissions)
# Get preview URL if it's an HTML file
# preview_url = self._get_preview_url(file_path)
message = f"File '{file_path}' created successfully."
# if preview_url:
# message += f"\n\nYou can preview this HTML file at the automatically served HTTP server: {preview_url}"
return self.success_response(message)
except Exception as e:
return self.fail_response(f"Error creating file: {str(e)}")
@openapi_schema({
"type": "function",
"function": {
"name": "str_replace",
"description": "Replace specific text in a file. The file path must be relative to /workspace (e.g., 'src/main.py' for /workspace/src/main.py). Use this when you need to replace a unique string that appears exactly once in the file.",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Path to the target file, relative to /workspace (e.g., 'src/main.py')"
},
"old_str": {
"type": "string",
"description": "Text to be replaced (must appear exactly once)"
},
"new_str": {
"type": "string",
"description": "Replacement text"
}
},
"required": ["file_path", "old_str", "new_str"]
}
}
})
@xml_schema(
tag_name="str-replace",
mappings=[
{"param_name": "file_path", "node_type": "attribute", "path": "."},
{"param_name": "old_str", "node_type": "element", "path": "old_str"},
{"param_name": "new_str", "node_type": "element", "path": "new_str"}
],
example='''
<str-replace file_path="src/main.py">
<old_str>text to replace (must appear exactly once in the file)</old_str>
<new_str>replacement text that will be inserted instead</new_str>
</str-replace>
'''
)
async def str_replace(self, file_path: str, old_str: str, new_str: str) -> ToolResult:
try:
# Ensure sandbox is initialized
await self._ensure_sandbox()
file_path = self.clean_path(file_path)
full_path = f"{self.workspace_path}/{file_path}"
if not self._file_exists(full_path):
return self.fail_response(f"File '{file_path}' does not exist")
content = self.sandbox.fs.download_file(full_path).decode()
old_str = old_str.expandtabs()
new_str = new_str.expandtabs()
occurrences = content.count(old_str)
if occurrences == 0:
return self.fail_response(f"String '{old_str}' not found in file")
if occurrences > 1:
lines = [i+1 for i, line in enumerate(content.split('\n')) if old_str in line]
return self.fail_response(f"Multiple occurrences found in lines {lines}. Please ensure string is unique")
# Perform replacement
new_content = content.replace(old_str, new_str)
self.sandbox.fs.upload_file(full_path, new_content.encode())
# Show snippet around the edit
replacement_line = content.split(old_str)[0].count('\n')
start_line = max(0, replacement_line - self.SNIPPET_LINES)
end_line = replacement_line + self.SNIPPET_LINES + new_str.count('\n')
snippet = '\n'.join(new_content.split('\n')[start_line:end_line + 1])
# Get preview URL if it's an HTML file
# preview_url = self._get_preview_url(file_path)
message = f"Replacement successful."
# if preview_url:
# message += f"\n\nYou can preview this HTML file at: {preview_url}"
return self.success_response(message)
except Exception as e:
return self.fail_response(f"Error replacing string: {str(e)}")
@openapi_schema({
"type": "function",
"function": {
"name": "full_file_rewrite",
"description": "Completely rewrite an existing file with new content. The file path must be relative to /workspace (e.g., 'src/main.py' for /workspace/src/main.py). Use this when you need to replace the entire file content or make extensive changes throughout the file.",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Path to the file to be rewritten, relative to /workspace (e.g., 'src/main.py')"
},
"file_contents": {
"type": "string",
"description": "The new content to write to the file, replacing all existing content"
},
"permissions": {
"type": "string",
"description": "File permissions in octal format (e.g., '644')",
"default": "644"
}
},
"required": ["file_path", "file_contents"]
}
}
})
@xml_schema(
tag_name="full-file-rewrite",
mappings=[
{"param_name": "file_path", "node_type": "attribute", "path": "."},
{"param_name": "file_contents", "node_type": "content", "path": "."}
],
example='''
<full-file-rewrite file_path="src/main.py">
This completely replaces the entire file content.
Use when making major changes to a file or when the changes
are too extensive for str-replace.
All previous content will be lost and replaced with this text.
</full-file-rewrite>
'''
)
async def full_file_rewrite(self, file_path: str, file_contents: str, permissions: str = "644") -> ToolResult:
try:
# Ensure sandbox is initialized
await self._ensure_sandbox()
file_path = self.clean_path(file_path)
full_path = f"{self.workspace_path}/{file_path}"
if not self._file_exists(full_path):
return self.fail_response(f"File '{file_path}' does not exist. Use create_file to create a new file.")
self.sandbox.fs.upload_file(full_path, file_contents.encode())
self.sandbox.fs.set_file_permissions(full_path, permissions)
# Get preview URL if it's an HTML file
# preview_url = self._get_preview_url(file_path)
message = f"File '{file_path}' completely rewritten successfully."
# if preview_url:
# message += f"\n\nYou can preview this HTML file at: {preview_url}"
return self.success_response(message)
except Exception as e:
return self.fail_response(f"Error rewriting file: {str(e)}")
@openapi_schema({
"type": "function",
"function": {
"name": "delete_file",
"description": "Delete a file at the given path. The path must be relative to /workspace (e.g., 'src/main.py' for /workspace/src/main.py)",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Path to the file to be deleted, relative to /workspace (e.g., 'src/main.py')"
}
},
"required": ["file_path"]
}
}
})
@xml_schema(
tag_name="delete-file",
mappings=[
{"param_name": "file_path", "node_type": "attribute", "path": "."}
],
example='''
<delete-file file_path="src/main.py">
</delete-file>
'''
)
async def delete_file(self, file_path: str) -> ToolResult:
try:
# Ensure sandbox is initialized
await self._ensure_sandbox()
file_path = self.clean_path(file_path)
full_path = f"{self.workspace_path}/{file_path}"
if not self._file_exists(full_path):
return self.fail_response(f"File '{file_path}' does not exist")
self.sandbox.fs.delete_file(full_path)
return self.success_response(f"File '{file_path}' deleted successfully.")
except Exception as e:
return self.fail_response(f"Error deleting file: {str(e)}")
# @openapi_schema({
# "type": "function",
# "function": {
# "name": "read_file",
# "description": "Read and return the contents of a file. This tool is essential for verifying data, checking file contents, and analyzing information. Always use this tool to read file contents before processing or analyzing data. The file path must be relative to /workspace.",
# "parameters": {
# "type": "object",
# "properties": {
# "file_path": {
# "type": "string",
# "description": "Path to the file to read, relative to /workspace (e.g., 'src/main.py' for /workspace/src/main.py). Must be a valid file path within the workspace."
# },
# "start_line": {
# "type": "integer",
# "description": "Optional starting line number (1-based). Use this to read specific sections of large files. If not specified, reads from the beginning of the file.",
# "default": 1
# },
# "end_line": {
# "type": "integer",
# "description": "Optional ending line number (inclusive). Use this to read specific sections of large files. If not specified, reads to the end of the file.",
# "default": None
# }
# },
# "required": ["file_path"]
# }
# }
# })
# @xml_schema(
# tag_name="read-file",
# mappings=[
# {"param_name": "file_path", "node_type": "attribute", "path": "."},
# {"param_name": "start_line", "node_type": "attribute", "path": ".", "required": False},
# {"param_name": "end_line", "node_type": "attribute", "path": ".", "required": False}
# ],
# example='''
# <!-- Example 1: Read entire file -->
# <read-file file_path="src/main.py">
# </read-file>
# <!-- Example 2: Read specific lines (lines 10-20) -->
# <read-file file_path="src/main.py" start_line="10" end_line="20">
# </read-file>
# <!-- Example 3: Read from line 5 to end -->
# <read-file file_path="config.json" start_line="5">
# </read-file>
# <!-- Example 4: Read last 10 lines -->
# <read-file file_path="logs/app.log" start_line="-10">
# </read-file>
# '''
# )
# async def read_file(self, file_path: str, start_line: int = 1, end_line: Optional[int] = None) -> ToolResult:
# """Read file content with optional line range specification.
# Args:
# file_path: Path to the file relative to /workspace
# start_line: Starting line number (1-based), defaults to 1
# end_line: Ending line number (inclusive), defaults to None (end of file)
# Returns:
# ToolResult containing:
# - Success: File content and metadata
# - Failure: Error message if file doesn't exist or is binary
# """
# try:
# file_path = self.clean_path(file_path)
# full_path = f"{self.workspace_path}/{file_path}"
# if not self._file_exists(full_path):
# return self.fail_response(f"File '{file_path}' does not exist")
# # Download and decode file content
# content = self.sandbox.fs.download_file(full_path).decode()
# # Split content into lines
# lines = content.split('\n')
# total_lines = len(lines)
# # Handle line range if specified
# if start_line > 1 or end_line is not None:
# # Convert to 0-based indices
# start_idx = max(0, start_line - 1)
# end_idx = end_line if end_line is not None else total_lines
# end_idx = min(end_idx, total_lines) # Ensure we don't exceed file length
# # Extract the requested lines
# content = '\n'.join(lines[start_idx:end_idx])
# return self.success_response({
# "content": content,
# "file_path": file_path,
# "start_line": start_line,
# "end_line": end_line if end_line is not None else total_lines,
# "total_lines": total_lines
# })
# except UnicodeDecodeError:
# return self.fail_response(f"File '{file_path}' appears to be binary and cannot be read as text")
# except Exception as e:
# return self.fail_response(f"Error reading file: {str(e)}")
|