From 5781db29dac3a0a509c8b4a5c1ab3503950662b3 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Mon, 5 May 2025 20:50:35 -0400 Subject: [PATCH] Update [ghstack-poisoned] --- codemcp/main.py | 30 ++++++---------------------- codemcp/tools/chmod.py | 44 ++++++++++++++---------------------------- codemcp/tools/glob.py | 26 ++++++------------------- codemcp/tools/grep.py | 28 +++++++-------------------- 4 files changed, 34 insertions(+), 94 deletions(-) diff --git a/codemcp/main.py b/codemcp/main.py index 9ca9b34..556c70c 100644 --- a/codemcp/main.py +++ b/codemcp/main.py @@ -267,17 +267,12 @@ async def codemcp( raise ValueError("path is required for Grep subtool") try: - grep_result = await grep_files( + result_string = await grep_files( pattern, path, include, chat_id, commit_hash ) - result_string = grep_result.get( - "resultForAssistant", - f"Found {grep_result.get('numFiles', 0)} file(s)", - ) return result_string except Exception as e: - # Log the error but don't suppress it - let it propagate - logging.error(f"Exception in grep subtool: {e!s}", exc_info=True) + logging.error(f"Error in Grep subtool: {e}", exc_info=True) raise if subtool == "Glob": @@ -287,22 +282,12 @@ async def codemcp( raise ValueError("path is required for Glob subtool") try: - glob_result = await glob_files( - pattern, - path, - limit, - offset, - chat_id, - commit_hash, - ) - result_string = glob_result.get( - "resultForAssistant", - f"Found {glob_result.get('numFiles', 0)} file(s)", + result_string = await glob_files( + pattern, path, limit, offset, chat_id, commit_hash ) return result_string except Exception as e: - # Log the error but don't suppress it - let it propagate - logging.error(f"Exception in glob subtool: {e!s}", exc_info=True) + logging.error(f"Error in Glob subtool: {e}", exc_info=True) raise if subtool == "RM": @@ -350,10 +335,7 @@ async def codemcp( if chat_id is None: raise ValueError("chat_id is required for Chmod subtool") - chmod_result = await chmod(path, mode, chat_id, commit_hash) - result_string = chmod_result.get( - "resultForAssistant", "Chmod operation completed" - ) + result_string = await chmod(path, mode, chat_id, commit_hash) return result_string except Exception: diff --git a/codemcp/tools/chmod.py b/codemcp/tools/chmod.py index cd6d5b6..6a5b5e5 100644 --- a/codemcp/tools/chmod.py +++ b/codemcp/tools/chmod.py @@ -30,20 +30,20 @@ async def chmod( path: str, - mode: str, # Changed from Literal["a+x", "a-x"] to str to handle validation internally + mode: str, chat_id: str | None = None, commit_hash: str | None = None, -) -> dict[str, Any]: +) -> str: """Change file permissions using chmod. Args: - path: The absolute path to the file to modify + path: The path to the file to change permissions for mode: The chmod mode to apply, only "a+x" and "a-x" are supported chat_id: The unique ID of the current chat session commit_hash: Optional Git commit hash for version tracking Returns: - A dictionary with chmod output + A formatted string with the chmod operation result """ # Set default values chat_id = "" if chat_id is None else chat_id @@ -73,26 +73,14 @@ async def chmod( if mode == "a+x" and is_executable: message = f"File '{path}' is already executable" - result = { - "output": message, - "resultForAssistant": message, - } # Append commit hash - result["resultForAssistant"], _ = await append_commit_hash( - result["resultForAssistant"], directory - ) - return result + message, _ = await append_commit_hash(message, directory, commit_hash) + return message elif mode == "a-x" and not is_executable: message = f"File '{path}' is already non-executable" - result = { - "output": message, - "resultForAssistant": message, - } # Append commit hash - result["resultForAssistant"], _ = await append_commit_hash( - result["resultForAssistant"], directory - ) - return result + message, _ = await append_commit_hash(message, directory, commit_hash) + return message # Execute chmod command cmd = ["chmod", mode, absolute_path] @@ -124,20 +112,18 @@ async def chmod( if not success: raise RuntimeError(f"Failed to commit chmod changes: {commit_message}") - # Prepare output - output = { - "output": f"{action_msg} and committed changes", - } + # Prepare result string + result_string = f"{action_msg} and committed changes" - # Add formatted result for assistant - output["resultForAssistant"] = render_result_for_assistant(output) + # Format the result for assistant + formatted_result = render_result_for_assistant({"output": result_string}) # Append commit hash - output["resultForAssistant"], _ = await append_commit_hash( - output["resultForAssistant"], directory + formatted_result, _ = await append_commit_hash( + formatted_result, directory, commit_hash ) - return output + return formatted_result def render_result_for_assistant(output: dict[str, Any]) -> str: diff --git a/codemcp/tools/glob.py b/codemcp/tools/glob.py index 5b6c644..5f8a7d9 100644 --- a/codemcp/tools/glob.py +++ b/codemcp/tools/glob.py @@ -150,7 +150,7 @@ async def glob_files( offset: int | None = None, chat_id: str | None = None, commit_hash: str | None = None, -) -> Dict[str, Any]: +) -> str: """Search for files matching a glob pattern. Args: @@ -162,7 +162,7 @@ async def glob_files( commit_hash: Optional Git commit hash for version tracking Returns: - A dictionary with matched files + A formatted string with the search results """ try: @@ -185,26 +185,12 @@ async def glob_files( formatted_result, _ = await append_commit_hash( formatted_result, normalized_path, commit_hash ) - result["resultForAssistant"] = formatted_result - return result + return formatted_result except Exception as e: # Log the error logging.error(f"Error in glob_files: {e}", exc_info=True) - # Prepare error output - error_output = { - "files": [], - "numFiles": 0, - "totalFiles": 0, - "pattern": pattern, - "path": path, - "limit": limit, - "offset": offset, - "error": str(e), - } - - # Add formatted result for assistant - error_output["resultForAssistant"] = f"Error searching for files: {e}" - - return error_output + # Return error message + error_message = f"Error searching for files: {e}" + return error_message diff --git a/codemcp/tools/grep.py b/codemcp/tools/grep.py index 4050fa9..a6b73dd 100644 --- a/codemcp/tools/grep.py +++ b/codemcp/tools/grep.py @@ -107,7 +107,7 @@ async def git_grep( cwd=absolute_path, capture_output=True, text=True, - check=False, # Don't raise exception if git grep doesn't find matches + check=False, ) # git grep returns exit code 1 when no matches are found, which is normal @@ -162,7 +162,7 @@ async def grep_files( include: str | None = None, chat_id: str | None = None, commit_hash: str | None = None, -) -> Dict[str, Any]: +) -> str: """Search for a pattern in files within a directory or in a specific file. Args: @@ -173,7 +173,7 @@ async def grep_files( commit_hash: Optional Git commit hash for version tracking Returns: - A dictionary with matched files + A formatted string with the search results """ try: @@ -212,25 +212,11 @@ async def grep_files( result_for_assistant, normalized_path, commit_hash ) - output["resultForAssistant"] = result_for_assistant - - return output + return result_for_assistant except Exception as e: # Log the error logging.error(f"Error in grep_files: {e}", exc_info=True) - # Prepare error output - error_output = { - "numFiles": 0, - "matchedFiles": [], - "truncated": False, - "pattern": pattern, - "path": path, - "include": include, - "error": str(e), - } - - # Add formatted result for assistant - error_output["resultForAssistant"] = f"Error searching for pattern: {e}" - - return error_output + # Return error message + error_message = f"Error searching for pattern: {e}" + return error_message