-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathline_based_edit_example.py
More file actions
executable file
·155 lines (121 loc) · 5.17 KB
/
line_based_edit_example.py
File metadata and controls
executable file
·155 lines (121 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
import os
import sys
import json
import logging
import asyncio
from pathlib import Path
# Add the parent directory to the path to import the agent module
script_dir = Path(__file__).parent
parent_dir = script_dir.parent
sys.path.append(str(parent_dir))
from cursor_agent_tools import create_agent
from cursor_agent_tools import BaseAgent
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Set cursor_agent_tools.tools.file_tools logger to DEBUG
file_tools_logger = logging.getLogger("cursor_agent_tools.tools.file_tools")
file_tools_logger.setLevel(logging.DEBUG)
# Create a temporary directory for testing
temp_dir = Path("./temp_line_edit_test").resolve() # Get absolute path
temp_dir.mkdir(exist_ok=True)
# Create a test file with absolute path
test_file = temp_dir / "test_file.py"
test_file = test_file.resolve() # Get absolute path
test_file_content = """#!/usr/bin/env python3
def hello_world():
print("Hello, World!")
def add(a, b):
# Add two numbers
return a + b
def multiply(a, b):
# Multiply two numbers
return a * b
def main():
hello_world()
result1 = add(5, 3)
result2 = multiply(4, 2)
print(f"Addition result: {result1}")
print(f"Multiplication result: {result2}")
if __name__ == "__main__":
main()
"""
async def main():
"""
Demonstrate the line-based editing feature using a real agent implementation.
Line-based editing allows precise modifications to specific line ranges
using a JSON dictionary where keys are line ranges (e.g., "1-5", "7-7")
and values are the new content for those ranges.
"""
# Create a test file
with open(test_file, "w") as f:
f.write(test_file_content)
logger.info(f"Created test file at {test_file}")
# Define a system prompt that emphasizes line-based editing with JSON examples
system_prompt = """You are an expert coding assistant that specializes in precise line-based editing of files.
IMPORTANT: When using the edit_file tool, you MUST provide the code_edit parameter as a proper JSON dictionary string.
The keys should be line ranges (e.g., "6-8") and the values should be the new content for those ranges.
For example, to edit lines 6-8 of a file, you would use:
```
{
"6-8": "def add(a, b):\\n # Add two numbers with validation\\n return a + b"
}
```
Another example for multiple edits:
```
{
"6-8": "def add(a, b):\\n # Add two numbers with validation\\n return a + b",
"10-12": "def multiply(a, b):\\n # Multiply two numbers with validation\\n return a * b"
}
```
To add a new function after line 12, you would use:
```
{
"12-12": "\\n\\ndef divide(a, b):\\n # Divide two numbers\\n if b == 0:\\n raise ZeroDivisionError(\\"Division by zero\\")\\n return a / b"
}
```
For complete file replacement, use the code_replace parameter instead.
Always read the file first to determine the correct line numbers."""
# Initialize a real agent with the custom system prompt
agent = create_agent(model="gpt-4o", system_prompt=system_prompt)
# Register default tools (including file operations)
agent.register_default_tools()
# Example: Single prompt to edit multiply function
logger.info("Testing line-based editing with a single function")
prompt = f"""
Read {test_file} and then update the multiply function to add input validation and better documentation.
You need to find the exact line numbers for the multiply function first.
After finding the line numbers, use the edit_file tool with the following parameters:
1. target_file: The absolute path to the file
2. instructions: A brief description of what you're changing
3. code_edit: A JSON dictionary with line ranges as keys and new content as values
Example of how to use edit_file:
```python
edit_file(
target_file="/path/to/file.py",
instructions="Update multiply function with validation",
code_edit={{"10-12": "def multiply(a, b):\\n # Multiply two numbers with validation\\n # Parameters: a, b - numeric values to multiply\\n # Returns: The product of a and b\\n if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):\\n raise TypeError(\\"Both arguments must be numbers\\")\\n return a * b"}}
)
```
Do NOT use code_replace as that would replace the entire file.
"""
response = await agent.chat(prompt)
logger.info(f"Agent response: {response}")
# Display the edited file
with open(test_file, "r") as f:
edited_content = f.read()
logger.info("File after edit:")
logger.info(edited_content)
# Verify changes were made correctly
with open(test_file, "r") as f:
final_content = f.read()
multiply_improved = "def multiply" in final_content and any(term in final_content for term in ["validation", "parameter", "input"])
logger.info(f"Multiply function was improved: {multiply_improved}")
# Clean up
if input("Do you want to clean up the temporary directory? (y/n): ").lower() == "y":
import shutil
shutil.rmtree(temp_dir)
logger.info(f"Removed temporary directory {temp_dir}")
if __name__ == "__main__":
asyncio.run(main())