initial file_str_replace implementation
This commit is contained in:
parent
47284931d4
commit
ca5f28f4b3
|
|
@ -15,7 +15,8 @@ from ra_aid.tools import (
|
||||||
emit_research_notes, emit_plan, emit_related_files, emit_task,
|
emit_research_notes, emit_plan, emit_related_files, emit_task,
|
||||||
emit_expert_context, get_memory_value, emit_key_facts, delete_key_facts,
|
emit_expert_context, get_memory_value, emit_key_facts, delete_key_facts,
|
||||||
emit_key_snippets, delete_key_snippets,
|
emit_key_snippets, delete_key_snippets,
|
||||||
emit_research_subtask, request_implementation, read_file_tool, fuzzy_find_project_files, ripgrep_search, list_directory_tree
|
emit_research_subtask, request_implementation, read_file_tool, fuzzy_find_project_files, ripgrep_search, list_directory_tree,
|
||||||
|
file_str_replace
|
||||||
)
|
)
|
||||||
from ra_aid.tools.memory import _global_memory, get_related_files
|
from ra_aid.tools.memory import _global_memory, get_related_files
|
||||||
from ra_aid import print_agent_output, print_stage_header, print_task_header, print_error
|
from ra_aid import print_agent_output, print_stage_header, print_task_header, print_error
|
||||||
|
|
@ -38,7 +39,8 @@ COMMON_TOOLS = [
|
||||||
delete_key_snippets,
|
delete_key_snippets,
|
||||||
read_file_tool,
|
read_file_tool,
|
||||||
fuzzy_find_project_files,
|
fuzzy_find_project_files,
|
||||||
ripgrep_search
|
ripgrep_search,
|
||||||
|
file_str_replace
|
||||||
]
|
]
|
||||||
|
|
||||||
# Expert-specific tools
|
# Expert-specific tools
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from .shell import run_shell_command
|
||||||
from .programmer import run_programming_task
|
from .programmer import run_programming_task
|
||||||
from .expert import ask_expert, emit_expert_context
|
from .expert import ask_expert, emit_expert_context
|
||||||
from .read_file import read_file_tool
|
from .read_file import read_file_tool
|
||||||
|
from .file_str_replace import file_str_replace
|
||||||
from .fuzzy_find import fuzzy_find_project_files
|
from .fuzzy_find import fuzzy_find_project_files
|
||||||
from .list_directory import list_directory_tree
|
from .list_directory import list_directory_tree
|
||||||
from .ripgrep import ripgrep_search
|
from .ripgrep import ripgrep_search
|
||||||
|
|
@ -31,5 +32,6 @@ __all__ = [
|
||||||
'run_shell_command',
|
'run_shell_command',
|
||||||
'skip_implementation',
|
'skip_implementation',
|
||||||
'emit_research_subtask',
|
'emit_research_subtask',
|
||||||
'ripgrep_search'
|
'ripgrep_search',
|
||||||
|
'file_str_replace'
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
from langchain_core.tools import tool
|
||||||
|
from typing import Dict
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
@tool
|
||||||
|
def file_str_replace(
|
||||||
|
filepath: str,
|
||||||
|
old_str: str,
|
||||||
|
new_str: str
|
||||||
|
) -> Dict[str, any]:
|
||||||
|
"""Replace an exact string match in a file with a new string.
|
||||||
|
Only performs replacement if the old string appears exactly once.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filepath: Path to the file to modify
|
||||||
|
old_str: Exact string to replace
|
||||||
|
new_str: String to replace with
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict containing:
|
||||||
|
- success: Whether the operation succeeded
|
||||||
|
- message: Success confirmation or error details
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
path = Path(filepath)
|
||||||
|
if not path.exists():
|
||||||
|
return {"success": False, "message": f"File not found: {filepath}"}
|
||||||
|
|
||||||
|
content = path.read_text()
|
||||||
|
count = content.count(old_str)
|
||||||
|
|
||||||
|
if count == 0:
|
||||||
|
return {"success": False, "message": f"String not found: {old_str}"}
|
||||||
|
elif count > 1:
|
||||||
|
return {"success": False, "message": f"String appears {count} times - must be unique"}
|
||||||
|
|
||||||
|
new_content = content.replace(old_str, new_str)
|
||||||
|
path.write_text(new_content)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"success": True,
|
||||||
|
"message": f"Successfully replaced '{old_str}' with '{new_str}' in {filepath}"
|
||||||
|
}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return {"success": False, "message": f"Error: {str(e)}"}
|
||||||
Loading…
Reference in New Issue