mirror of https://github.com/kortix-ai/suna.git
wip
This commit is contained in:
parent
ff7d499d05
commit
804ccbff41
|
@ -298,100 +298,107 @@ class SandboxFilesTool(SandboxToolsBase):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self.fail_response(f"Error deleting file: {str(e)}")
|
return self.fail_response(f"Error deleting file: {str(e)}")
|
||||||
|
|
||||||
@openapi_schema({
|
# @openapi_schema({
|
||||||
"type": "function",
|
# "type": "function",
|
||||||
"function": {
|
# "function": {
|
||||||
"name": "read_file",
|
# "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.",
|
# "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": {
|
# "parameters": {
|
||||||
"type": "object",
|
# "type": "object",
|
||||||
"properties": {
|
# "properties": {
|
||||||
"file_path": {
|
# "file_path": {
|
||||||
"type": "string",
|
# "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."
|
# "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": {
|
# "start_line": {
|
||||||
"type": "integer",
|
# "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.",
|
# "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
|
# "default": 1
|
||||||
},
|
# },
|
||||||
"end_line": {
|
# "end_line": {
|
||||||
"type": "integer",
|
# "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.",
|
# "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
|
# "default": None
|
||||||
}
|
# }
|
||||||
},
|
# },
|
||||||
"required": ["file_path"]
|
# "required": ["file_path"]
|
||||||
}
|
# }
|
||||||
}
|
# }
|
||||||
})
|
# })
|
||||||
@xml_schema(
|
# @xml_schema(
|
||||||
tag_name="read-file",
|
# tag_name="read-file",
|
||||||
mappings=[
|
# mappings=[
|
||||||
{"param_name": "file_path", "node_type": "attribute", "path": "."},
|
# {"param_name": "file_path", "node_type": "attribute", "path": "."},
|
||||||
{"param_name": "start_line", "node_type": "attribute", "path": ".", "required": False},
|
# {"param_name": "start_line", "node_type": "attribute", "path": ".", "required": False},
|
||||||
{"param_name": "end_line", "node_type": "attribute", "path": ".", "required": False}
|
# {"param_name": "end_line", "node_type": "attribute", "path": ".", "required": False}
|
||||||
],
|
# ],
|
||||||
example='''
|
# example='''
|
||||||
<!-- Example 1: Read entire file -->
|
# <!-- Example 1: Read entire file -->
|
||||||
<read-file file_path="src/main.py">
|
# <read-file file_path="src/main.py">
|
||||||
</read-file>
|
# </read-file>
|
||||||
|
|
||||||
<!-- Example 2: Read specific lines (lines 10-20) -->
|
# <!-- Example 2: Read specific lines (lines 10-20) -->
|
||||||
<read-file file_path="src/main.py" start_line="10" end_line="20">
|
# <read-file file_path="src/main.py" start_line="10" end_line="20">
|
||||||
</read-file>
|
# </read-file>
|
||||||
|
|
||||||
<!-- Example 3: Read from line 5 to end -->
|
# <!-- Example 3: Read from line 5 to end -->
|
||||||
<read-file file_path="config.json" start_line="5">
|
# <read-file file_path="config.json" start_line="5">
|
||||||
</read-file>
|
# </read-file>
|
||||||
|
|
||||||
<!-- Example 4: Read last 10 lines -->
|
# <!-- Example 4: Read last 10 lines -->
|
||||||
<read-file file_path="logs/app.log" start_line="-10">
|
# <read-file file_path="logs/app.log" start_line="-10">
|
||||||
</read-file>
|
# </read-file>
|
||||||
'''
|
# '''
|
||||||
)
|
# )
|
||||||
async def read_file(self, file_path: str, start_line: int = 1, end_line: Optional[int] = None) -> ToolResult:
|
# 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.
|
# """Read file content with optional line range specification.
|
||||||
|
|
||||||
Args:
|
# Args:
|
||||||
file_path: Path to the file relative to /workspace
|
# file_path: Path to the file relative to /workspace
|
||||||
start_line: Starting line number (1-based), defaults to 1
|
# start_line: Starting line number (1-based), defaults to 1
|
||||||
end_line: Ending line number (inclusive), defaults to None (end of file)
|
# end_line: Ending line number (inclusive), defaults to None (end of file)
|
||||||
|
|
||||||
Returns:
|
# Returns:
|
||||||
ToolResult containing:
|
# ToolResult containing:
|
||||||
- Success: File content and metadata
|
# - Success: File content and metadata
|
||||||
- Failure: Error message if file doesn't exist or is binary
|
# - Failure: Error message if file doesn't exist or is binary
|
||||||
"""
|
# """
|
||||||
try:
|
# try:
|
||||||
file_path = self.clean_path(file_path)
|
# file_path = self.clean_path(file_path)
|
||||||
full_path = f"{self.workspace_path}/{file_path}"
|
# full_path = f"{self.workspace_path}/{file_path}"
|
||||||
|
|
||||||
if not self._file_exists(full_path):
|
# if not self._file_exists(full_path):
|
||||||
return self.fail_response(f"File '{file_path}' does not exist")
|
# return self.fail_response(f"File '{file_path}' does not exist")
|
||||||
|
|
||||||
# Download and decode file content
|
# # Download and decode file content
|
||||||
content = self.sandbox.fs.download_file(full_path).decode()
|
# content = self.sandbox.fs.download_file(full_path).decode()
|
||||||
|
|
||||||
# Handle line range if specified
|
# # Split content into lines
|
||||||
if start_line > 1 or end_line is not None:
|
# lines = content.split('\n')
|
||||||
lines = content.split('\n')
|
# total_lines = len(lines)
|
||||||
start_idx = max(0, start_line - 1) # Convert to 0-based index
|
|
||||||
end_idx = end_line if end_line is not None else len(lines)
|
|
||||||
content = '\n'.join(lines[start_idx:end_idx])
|
|
||||||
|
|
||||||
return self.success_response({
|
# # Handle line range if specified
|
||||||
"content": content,
|
# if start_line > 1 or end_line is not None:
|
||||||
"file_path": file_path,
|
# # Convert to 0-based indices
|
||||||
"start_line": start_line,
|
# start_idx = max(0, start_line - 1)
|
||||||
"end_line": end_line if end_line is not None else len(content.split('\n')),
|
# end_idx = end_line if end_line is not None else total_lines
|
||||||
"total_lines": len(content.split('\n'))
|
# end_idx = min(end_idx, total_lines) # Ensure we don't exceed file length
|
||||||
})
|
|
||||||
|
|
||||||
except UnicodeDecodeError:
|
# # Extract the requested lines
|
||||||
return self.fail_response(f"File '{file_path}' appears to be binary and cannot be read as text")
|
# content = '\n'.join(lines[start_idx:end_idx])
|
||||||
except Exception as e:
|
|
||||||
return self.fail_response(f"Error reading file: {str(e)}")
|
# 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)}")
|
||||||
|
|
||||||
|
|
||||||
async def test_files_tool():
|
async def test_files_tool():
|
||||||
|
|
Loading…
Reference in New Issue