Download core/tools/sb_files_tool.py from Kyrlos11/nniback: direct link, hf CLI and curl.
- Browser
- Download file 36 kB
-
https://huggingface.co/spaces/Kyrlos11/nniback/resolve/main/core/tools/sb_files_tool.py
- Command line
-
hf download hf://spaces/Kyrlos11/nniback/core/tools/sb_files_tool.py
-
curl -L -o sb_files_tool.py https://huggingface.co/spaces/Kyrlos11/nniback/resolve/main/core/tools/sb_files_tool.py
36 kB
| from core.agentpress.tool import ToolResult, openapi_schema, tool_metadata | |
| from core.sandbox.tool_base import SandboxToolsBase | |
| from core.utils.files_utils import should_exclude_file, clean_path | |
| from core.agentpress.thread_manager import ThreadManager | |
| from core.utils.logger import logger | |
| from core.utils.config import config | |
| import os | |
| import json | |
| import litellm | |
| import openai | |
| import asyncio | |
| import re | |
| from typing import Optional | |
| 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 | |
| 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 _get_full_path(self, path: str) -> str: | |
| """Get full absolute path relative to /workspace""" | |
| cleaned = self.clean_path(path) | |
| return f"{self.workspace_path}/{cleaned}" | |
| 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) | |
| async def _file_exists(self, path: str) -> bool: | |
| """Check if a file exists in the sandbox""" | |
| try: | |
| await 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 = await 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 = self._get_full_path(rel_path) | |
| content = (await 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 | |
| async def create_file(self, file_path: str, file_contents: str, permissions: str = "644") -> ToolResult: | |
| try: | |
| # Ensure sandbox is initialized | |
| await self._ensure_sandbox() | |
| full_path = self._get_full_path(file_path) | |
| if await 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: | |
| await self.sandbox.fs.create_folder(parent_dir, "755") | |
| # convert to json string if file_contents is a dict | |
| if isinstance(file_contents, dict): | |
| file_contents = json.dumps(file_contents, indent=4) | |
| # Write the file content | |
| await self.sandbox.fs.upload_file(file_contents.encode(), full_path) | |
| await self.sandbox.fs.set_file_permissions(full_path, permissions) | |
| message = f"File '{file_path}' created successfully." | |
| if file_path.lower().endswith('.html'): | |
| try: | |
| website_link = await self.sandbox.get_preview_link(8080) | |
| website_url = website_link.url if hasattr(website_link, 'url') else str(website_link).split("url='")[1].split("'")[0] | |
| if not website_url.endswith('/'): | |
| website_url += '/' | |
| full_preview_url = f"{website_url}{file_path}" | |
| message += f"\n\n✓ HTML file preview available at: {full_preview_url}" | |
| message += "\n[Note: Port 8080 is auto-exposed. Just share this URL with the user - no need to start servers or expose ports manually]" | |
| except Exception as e: | |
| logger.warning(f"Failed to get preview URL for HTML file: {str(e)}") | |
| return self.success_response(message) | |
| except Exception as e: | |
| return self.fail_response(f"Error creating file: {str(e)}") | |
| async def str_replace(self, file_path: str, old_str: str, new_str: str, replace_all: bool = False) -> ToolResult: | |
| try: | |
| # Ensure sandbox is initialized | |
| await self._ensure_sandbox() | |
| full_path = self._get_full_path(file_path) | |
| if not await self._file_exists(full_path): | |
| return self.fail_response(f"File '{file_path}' does not exist") | |
| content = (await 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 and not replace_all: | |
| 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}. Use replace_all=true or provide more context to make it unique") | |
| new_content = content.replace(old_str, new_str) | |
| await self.sandbox.fs.upload_file(new_content.encode(), full_path) | |
| return ToolResult(success=True, output=json.dumps({ | |
| "message": f"Replacement successful.", | |
| "file_path": file_path, | |
| "original_content": content, | |
| "updated_content": new_content | |
| })) | |
| except Exception as e: | |
| return self.fail_response(f"Error replacing string: {str(e)}") | |
| 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() | |
| full_path = self._get_full_path(file_path) | |
| if not await self._file_exists(full_path): | |
| return self.fail_response(f"File '{file_path}' does not exist. Use create_file to create a new file.") | |
| await self.sandbox.fs.upload_file(file_contents.encode(), full_path) | |
| await self.sandbox.fs.set_file_permissions(full_path, permissions) | |
| message = f"File '{file_path}' completely rewritten successfully." | |
| if file_path.lower().endswith('.html'): | |
| try: | |
| website_link = await self.sandbox.get_preview_link(8080) | |
| website_url = website_link.url if hasattr(website_link, 'url') else str(website_link).split("url='")[1].split("'")[0] | |
| if not website_url.endswith('/'): | |
| website_url += '/' | |
| full_preview_url = f"{website_url}{file_path}" | |
| message += f"\n\n✓ HTML file preview available at: {full_preview_url}" | |
| message += "\n[Note: Port 8080 is auto-exposed. Just share this URL with the user - no need to start servers or expose ports manually]" | |
| except Exception as e: | |
| logger.warning(f"Failed to get preview URL for HTML file: {str(e)}") | |
| return self.success_response(message) | |
| except Exception as e: | |
| return self.fail_response(f"Error rewriting file: {str(e)}") | |
| async def delete_file(self, file_path: str) -> ToolResult: | |
| try: | |
| await self._ensure_sandbox() | |
| full_path = self._get_full_path(file_path) | |
| logger.debug(f"Attempting to delete file: '{file_path}' (full path: '{full_path}')") | |
| if not await self._file_exists(full_path): | |
| return self.fail_response(f"File '{file_path}' does not exist") | |
| # Delete the file | |
| await self.sandbox.fs.delete_file(full_path) | |
| # Verify the file was actually deleted | |
| await asyncio.sleep(0.1) # Small delay to ensure deletion is processed | |
| if await self._file_exists(full_path): | |
| logger.warning(f"File '{file_path}' still exists after delete_file call. Attempting alternative deletion method.") | |
| # Try alternative: use shell command as fallback | |
| try: | |
| result = await self.sandbox.process.exec("rm", "-f", full_path) | |
| if result.exit_code != 0: | |
| return self.fail_response(f"Failed to delete file '{file_path}'. File still exists after deletion attempt. Exit code: {result.exit_code}, stderr: {result.stderr}") | |
| # Verify again | |
| await asyncio.sleep(0.1) | |
| if await self._file_exists(full_path): | |
| return self.fail_response(f"Failed to delete file '{file_path}'. File still exists after deletion attempt.") | |
| except Exception as shell_error: | |
| return self.fail_response(f"Failed to delete file '{file_path}'. File still exists and shell deletion also failed: {str(shell_error)}") | |
| logger.debug(f"Successfully deleted file: '{file_path}'") | |
| return self.success_response(f"File '{file_path}' deleted successfully.") | |
| except Exception as e: | |
| logger.error(f"Error deleting file '{file_path}': {str(e)}", exc_info=True) | |
| return self.fail_response(f"Error deleting file: {str(e)}") | |
| async def _call_morph_api(self, file_content: str, code_edit: str, instructions: str, file_path: str) -> tuple[Optional[str], Optional[str]]: | |
| """ | |
| Call Morph API to apply edits to file content. | |
| Returns a tuple (new_content, error_message). | |
| On success, error_message is None. | |
| On failure, new_content is None. | |
| """ | |
| try: | |
| morph_api_key = getattr(config, 'MORPH_API_KEY', None) or os.getenv('MORPH_API_KEY') | |
| openrouter_key = getattr(config, 'OPENROUTER_API_KEY', None) or os.getenv('OPENROUTER_API_KEY') | |
| messages = [{ | |
| "role": "user", | |
| "content": f"<instruction>{instructions}</instruction>\n<code>{file_content}</code>\n<update>{code_edit}</update>" | |
| }] | |
| response = None | |
| if morph_api_key: | |
| logger.debug("Using direct Morph API for file editing.") | |
| client = openai.AsyncOpenAI( | |
| api_key=morph_api_key, | |
| base_url="https://api.morphllm.com/v1" | |
| ) | |
| response = await client.chat.completions.create( | |
| model="morph-v3-large", | |
| messages=messages, | |
| temperature=0.0, | |
| timeout=30.0 | |
| ) | |
| elif openrouter_key: | |
| logger.debug("Morph API key not set, falling back to OpenRouter for file editing via litellm.") | |
| response = await litellm.acompletion( | |
| model="openrouter/morph/morph-v3-large", | |
| messages=messages, | |
| api_key=openrouter_key, | |
| api_base="https://openrouter.ai/api/v1", | |
| temperature=0.0, | |
| timeout=30.0, | |
| extra_body={"app": "Kortix.com"} | |
| ) | |
| else: | |
| error_msg = "No Morph or OpenRouter API key found, cannot perform AI edit." | |
| logger.warning(error_msg) | |
| return None, error_msg | |
| if response and response.choices and len(response.choices) > 0: | |
| content = response.choices[0].message.content.strip() | |
| # Extract code block if wrapped in markdown | |
| if content.startswith("```") and content.endswith("```"): | |
| lines = content.split('\n') | |
| if len(lines) > 2: | |
| content = '\n'.join(lines[1:-1]) | |
| return content, None | |
| else: | |
| error_msg = f"Invalid response from Morph/OpenRouter API: {response}" | |
| logger.error(error_msg) | |
| return None, error_msg | |
| except Exception as e: | |
| error_message = f"AI model call for file edit failed. Exception: {str(e)}" | |
| # Try to get more details from the exception if it's an API error | |
| if hasattr(e, 'response') and hasattr(e.response, 'text'): | |
| error_message += f"\n\nAPI Response Body:\n{e.response.text}" | |
| elif hasattr(e, 'body'): # litellm sometimes puts it in body | |
| error_message += f"\n\nAPI Response Body:\n{e.body}" | |
| logger.error(f"Error calling Morph/OpenRouter API: {error_message}", exc_info=True) | |
| return None, error_message | |
| async def edit_file(self, target_file: str, instructions: str, code_edit: str) -> ToolResult: | |
| try: | |
| await self._ensure_sandbox() | |
| target_file = self.clean_path(target_file) | |
| full_path = self._get_full_path(target_file) | |
| if not await self._file_exists(full_path): | |
| return self.fail_response(f"File '{target_file}' does not exist") | |
| original_content = (await self.sandbox.fs.download_file(full_path)).decode() | |
| is_tiptap_doc = False | |
| original_wrapper = None | |
| if target_file.startswith("docs/") and target_file.endswith(".doc"): | |
| try: | |
| original_wrapper = json.loads(original_content) | |
| if original_wrapper.get("type") == "tiptap_document": | |
| is_tiptap_doc = True | |
| except json.JSONDecodeError: | |
| pass | |
| logger.debug(f"Attempting AI-powered edit for file '{target_file}' with instructions: {instructions[:100]}...") | |
| new_content, error_message = await self._call_morph_api(original_content, code_edit, instructions, target_file) | |
| if error_message: | |
| if is_tiptap_doc and original_wrapper: | |
| logger.debug(f"Morph AI edit failed for TipTap doc: {error_message}, attempting fallback manual update") | |
| if "title" in instructions: | |
| import re | |
| title_match = re.search(r'"title"\s*field\s*to\s*"([^"]+)"', instructions) | |
| if title_match: | |
| original_wrapper["title"] = title_match.group(1) | |
| if "content" in instructions and "content" in code_edit: | |
| content_match = re.search(r'"content":\s*([^,}]+)', code_edit) | |
| if content_match: | |
| try: | |
| new_html_content = json.loads(content_match.group(1).strip()) | |
| original_wrapper["content"] = new_html_content | |
| except: | |
| pass | |
| if "metadata" in instructions: | |
| metadata_match = re.search(r'"metadata":\s*({[^}]+})', code_edit) | |
| if metadata_match: | |
| try: | |
| new_metadata = json.loads(metadata_match.group(1)) | |
| original_wrapper["metadata"] = new_metadata | |
| except: | |
| pass | |
| if "updated_at" in instructions: | |
| from datetime import datetime | |
| original_wrapper["updated_at"] = datetime.now().isoformat() | |
| new_content = json.dumps(original_wrapper, indent=2) | |
| else: | |
| return ToolResult(success=False, output=json.dumps({ | |
| "message": f"AI editing failed: {error_message}", | |
| "file_path": target_file, | |
| "original_content": original_content, | |
| "updated_content": None | |
| })) | |
| if new_content is None: | |
| return ToolResult(success=False, output=json.dumps({ | |
| "message": "AI editing failed for an unknown reason. The model returned no content.", | |
| "file_path": target_file, | |
| "original_content": original_content, | |
| "updated_content": None | |
| })) | |
| if new_content == original_content: | |
| return ToolResult(success=True, output=json.dumps({ | |
| "message": f"AI editing resulted in no changes to the file '{target_file}'.", | |
| "file_path": target_file, | |
| "original_content": original_content, | |
| "updated_content": original_content | |
| })) | |
| await self.sandbox.fs.upload_file(new_content.encode(), full_path) | |
| return ToolResult(success=True, output=json.dumps({ | |
| "message": f"File '{target_file}' edited successfully.", | |
| "file_path": target_file, | |
| "original_content": original_content, | |
| "updated_content": new_content | |
| })) | |
| except Exception as e: | |
| logger.error(f"Unhandled error in edit_file: {str(e)}", exc_info=True) | |
| # Try to get original_content if possible | |
| original_content_on_error = None | |
| try: | |
| full_path_on_error = f"{self.workspace_path}/{self.clean_path(target_file)}" | |
| if await self._file_exists(full_path_on_error): | |
| original_content_on_error = (await self.sandbox.fs.download_file(full_path_on_error)).decode() | |
| except: | |
| pass | |
| return ToolResult(success=False, output=json.dumps({ | |
| "message": f"Error editing file: {str(e)}", | |
| "file_path": target_file, | |
| "original_content": original_content_on_error, | |
| "updated_content": None | |
| })) | |
| async def export_to_pdf(self, file_path: str) -> ToolResult: | |
| """Export an HTML file to PDF using sandbox Playwright rendering.""" | |
| try: | |
| await self._ensure_sandbox() | |
| cleaned_path = self.clean_path(file_path) | |
| full_path = self._get_full_path(file_path) | |
| # Check file exists | |
| if not await self._file_exists(full_path): | |
| return self.fail_response(f"File '{file_path}' does not exist") | |
| # Check it's an HTML file | |
| if not cleaned_path.lower().endswith(('.html', '.htm')): | |
| return self.fail_response(f"File '{file_path}' is not an HTML file. Only .html/.htm files can be exported to PDF.") | |
| # Determine output filename | |
| base_name = cleaned_path.rsplit('.', 1)[0] if '.' in cleaned_path else cleaned_path | |
| output_filename = f"{base_name.replace('/', '_')}.pdf" | |
| logger.info(f"[export_to_pdf] Exporting {cleaned_path} to PDF") | |
| # Call sandbox html-to-pdf endpoint | |
| from core.services.http_client import get_http_client | |
| async with get_http_client() as client: | |
| response = await client.post( | |
| f"{self.sandbox_url}/presentation/html-to-pdf", | |
| json={ | |
| "file_path": cleaned_path, | |
| "file_name": base_name.replace('/', '_') | |
| }, | |
| timeout=120.0 | |
| ) | |
| if response.status_code == 200: | |
| pdf_bytes = response.content | |
| # Save to downloads folder | |
| downloads_path = f"{self.workspace_path}/downloads" | |
| output_path = f"{downloads_path}/{output_filename}" | |
| # Ensure downloads directory exists | |
| try: | |
| await self.sandbox.fs.upload_file(pdf_bytes, output_path) | |
| except Exception: | |
| # Directory might not exist, create it | |
| await self.sandbox.process.start(f"mkdir -p {downloads_path}") | |
| await self.sandbox.fs.upload_file(pdf_bytes, output_path) | |
| logger.info(f"[export_to_pdf] Success: {output_path}") | |
| return self.success_response({ | |
| "message": f"Successfully exported '{file_path}' to PDF", | |
| "output_file": f"downloads/{output_filename}", | |
| "format": "pdf" | |
| }) | |
| else: | |
| error_text = response.text | |
| logger.error(f"[export_to_pdf] Failed: {response.status_code} - {error_text}") | |
| return self.fail_response(f"PDF export failed: {error_text}") | |
| except Exception as e: | |
| logger.error(f"[export_to_pdf] Error: {str(e)}") | |
| return self.fail_response(f"PDF export failed: {str(e)}") | |