agentscope-ai / agentscope-ai/agentscope

[Feature]: Add string replacement tool for file editing

Đang mở
#1,323 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
stale-issue
Ngôn ngữ chính
Python
Star
31.5k
Fork
3.5k
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
95

Mô tả

**Is your feature request related to a problem? Please describe.**

Currently, AgentScope's `_text_file` module provides three file editing tools:
- `view_text_file` - View file content
- `insert_text_file` - Insert content at a specified line number
- `write_text_file` - Write content to file (supports line range replacement)

However, there is no tool for **string-based replacement** in files. This makes it difficult for coding agents to perform precise code modifications. The current workflow requires:
1. Read the entire file with `view_text_file`
2. Calculate exact line numbers for the target content
3. Use `write_text_file` with `ranges` parameter

This approach has several limitations:
- Requires precise line number calculation, which is error-prone
- Cannot handle cases where the same content appears multiple times
- Not intuitive for LLM-based agents that think in terms of code patterns

**Describe the solution you'd like**

Add a new `replace_text_file` tool (or `edit_text_file`) that supports string-based replacement:

```python
async def replace_text_file(
file_path: str,
old_string: str,
new_string: str,
replace_all: bool = False,
) -> ToolResponse:
"""Replace text in a file using string matching.

Args:
file_path: The target file path.
old_string: The text to be replaced.
new_string: The new text to replace with.
replace_all: If True, replace all occurrences. Default is False.

Returns:
ToolResponse with the result of the operation.
"""
```
Key Features:
1. Exact string matching - Find and replace exact text
2. Multiple occurrence handling - Support replace_all parameter for global replacement
3. Error handling - Return error if old_string not found or found multiple times (without replace_all)
4. Line ending normalization - Handle different line endings gracefully

Describe alternatives you've considered

Alternative 1: Use execute_python_code to run Python string operations
- Not ideal as it's indirect and less safe

Alternative 2: Use execute_shell_command with sed
- Platform-dependent and harder for LLMs to use correctly

Additional context

This feature is commonly found in coding agent frameworks:

1. SWE-Agent uses str_replace_editor for precise code modifications
2. Cursor/Cline also implement similar string replacement tools

Proposed Implementation

A minimal implementation could be added to src/agentscope/tool/_text_file/_replace_text_file.py:
```py
async def replace_text_file(
file_path: str,
old_string: str,
new_string: str,
replace_all: bool = False,
) -> ToolResponse:
file_path = os.path.expanduser(file_path)

if not os.path.exists(file_path):
return ToolResponse(
content=[TextBlock(type="text", text=f"Error: File {file_path} does not exist.")],
)

with open(file_path, "r", encoding="utf-8") as f:
content = f.read()

if old_string not in content:
return ToolResponse(
content=[TextBlock(type="text", text=f"Error: old_string not found in file.")],
)

if not replace_all:
count = content.count(old_string)
if count > 1:
return ToolResponse(
content=[TextBlock(
type="text",
text=f"Error: Found {count} occurrences. Use replace_all=True or provide more context.",
)],
)

new_content = content.replace(old_string, new_string) if replace_all else content.replace(old_string, new_string, 1)

with open(file_path, "w", encoding="utf-8") as f:
f.write(new_content)

return ToolResponse(
content=[TextBlock(type="text", text=f"Successfully replaced text in {file_path}.")],
)
```
Would you be willing to submit a PR for this feature?

Yes, I can submit a PR with tests and documentation if this feature is approved.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.