diff --git a/agent/tools/codemap.py b/agent/tools/codemap.py index 8e0b0aa..88b41e6 100644 --- a/agent/tools/codemap.py +++ b/agent/tools/codemap.py @@ -100,7 +100,7 @@ def get_code_definitions(file_path: str) -> str: return "\n".join(output_lines) @tool(parse_docstring=True) -def get_function_implementation(file_path: str, function_name: str) -> Optional[str]: +def get_function_implementation(file_path: str, function_name: str) -> str: """ Extract the implementation of a specific function or method from a file. @@ -119,7 +119,7 @@ def get_function_implementation(file_path: str, function_name: str) -> Optional[ } lang = lang_map.get(suffix) if not lang: - return None + return f"Unsupported file type: {suffix}" # Initialize parser language = get_language(lang) @@ -178,7 +178,7 @@ def get_function_implementation(file_path: str, function_name: str) -> Optional[ return "\n".join(output_lines) - return None + return f"Function '{function_name}' not found in {file_path}" @tool(parse_docstring=True) def get_code_definitions_multi(file_paths: list[str]) -> str: @@ -202,12 +202,17 @@ def get_code_definitions_multi(file_paths: list[str]) -> str: def get_raw_file_content(file_path: str) -> str: """ Get the raw content of the file. good for a non-code files - + Args: file_path: file path to read """ - with open(file_path, "rb") as f: - return f.read().decode('utf-8') + try: + with open(file_path, "rb") as f: + return f.read().decode('utf-8') + except FileNotFoundError: + return f"Error: File '{file_path}' not found" + except Exception as e: + return f"Error reading file '{file_path}': {str(e)}" # List of available tools codemap_tools = [get_code_definitions, get_function_implementation, get_code_definitions_multi, get_raw_file_content] diff --git a/agent/tools/write.py b/agent/tools/write.py index 91ca99d..eb3f981 100644 --- a/agent/tools/write.py +++ b/agent/tools/write.py @@ -16,9 +16,13 @@ def create_file(path: str, content: str) -> str: str: A success message with the file path, or an error message if creation failed """ try: - # Check if file already exists - # Ensure the directory exists - os.makedirs(os.path.dirname(path), exist_ok=True) + if os.path.exists(path): + return f"Error: File {path} already exists. Use write_to_file to overwrite it." + + # Ensure the directory exists (only if there is a directory component) + dir_name = os.path.dirname(path) + if dir_name: + os.makedirs(dir_name, exist_ok=True) with open(path, "w", encoding="utf-8") as f: f.write(content)