Skip to content

Commit ef4fbdf

Browse files
authored
Feat: tool improvements (#801)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed --------- Co-authored-by: kopekC <28070492+kopekC@users.noreply.github.com>
1 parent 039b68a commit ef4fbdf

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

src/codegen/extensions/langchain/tools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class SearchInput(BaseModel):
119119
description="""The search query to find in the codebase. When ripgrep is available, this will be passed as a ripgrep pattern. For regex searches, set use_regex=True.
120120
Ripgrep is the preferred method.""",
121121
)
122-
target_directories: Optional[list[str]] = Field(default=None, description="Optional list of directories to search in")
123122
file_extensions: Optional[list[str]] = Field(default=None, description="Optional list of file extensions to search (e.g. ['.py', '.ts'])")
124123
page: int = Field(default=1, description="Page number to return (1-based, default: 1)")
125124
files_per_page: int = Field(default=10, description="Number of files to return per page (default: 10)")
@@ -138,7 +137,7 @@ def __init__(self, codebase: Codebase) -> None:
138137
super().__init__(codebase=codebase)
139138

140139
def _run(self, query: str, target_directories: Optional[list[str]] = None, file_extensions: Optional[list[str]] = None, page: int = 1, files_per_page: int = 10, use_regex: bool = False) -> str:
141-
result = search(self.codebase, query, target_directories=target_directories, file_extensions=file_extensions, page=page, files_per_page=files_per_page, use_regex=use_regex)
140+
result = search(self.codebase, query, file_extensions=file_extensions, page=page, files_per_page=files_per_page, use_regex=use_regex)
142141
return result.render()
143142

144143

src/codegen/extensions/tools/relace_edit_prompts.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@
22

33
RELACE_EDIT_PROMPT = """Edit a file using the Relace Instant Apply API.
44
5+
⚠️ IMPORTANT: The 'edit_snippet' parameter must be provided as a direct string, NOT as a dictionary or JSON object.
6+
For example, use: edit_snippet="// ... code here ..." NOT edit_snippet={"code": "// ... code here ..."}
7+
DO NOT pass the edit_snippet as a dictionary or JSON object or dictionary with a 'code' key .
8+
59
The Relace Instant Apply API is a high-speed code generation engine optimized for real-time performance at 2000 tokens/second. It splits code generation into two specialized steps:
610
711
1. Hard Reasoning: Uses SOTA models like Claude for complex code understanding
812
2. Fast Integration: Rapidly merges edits into existing code
913
10-
To use this tool, provide:
11-
1. The path to the file you want to edit
12-
2. An edit snippet that describes the changes you want to make
14+
To use this tool effectively, provide:
15+
1. The path to the file you want to edit (filepath)
16+
2. An edit snippet that describes the changes you want to make (edit_snippet)
1317
14-
The edit snippet should:
18+
The edit snippet is crucial for successful merging and should follow these guidelines:
1519
- Include complete code blocks that will appear in the final output
16-
- Clearly indicate which parts of the code remain unchanged with comments like "// ... rest of code ..."
17-
- Maintain correct indentation and code structure
20+
- Clearly indicate which parts of the code remain unchanged with descriptive comments like:
21+
* "// ... keep existing imports ..."
22+
* "// ... rest of the class definition ..."
23+
* "// ... existing function implementations ..."
24+
- Maintain correct indentation and code structure exactly as it should appear in the final code
25+
- Use comments to indicate the purpose of your changes (e.g., "// Add new function")
26+
- For removing sections, provide sufficient context so it's clear what should be removed
27+
- Focus only on the modifications, not other aspects of the code
28+
- Preserve language-specific syntax (comment style may vary by language)
29+
- Include more context than just the minimal changes - surrounding code helps the API understand where and how to apply the edit
1830
1931
Example edit snippet:
2032
```
@@ -28,7 +40,33 @@
2840
// ... keep existing code ...
2941
```
3042
43+
Another example (Python):
44+
```
45+
# ... existing imports ...
46+
47+
# Add new helper function
48+
def validate_input(data):
49+
'''Validates the input data structure.'''
50+
if not isinstance(data, dict):
51+
raise TypeError("Input must be a dictionary")
52+
if "id" not in data:
53+
raise ValueError("Input must contain an 'id' field")
54+
return True
55+
56+
# ... rest of the file ...
57+
```
58+
3159
The API will merge your edit snippet with the existing code to produce the final result.
60+
The API key will be automatically retrieved from the RELACE_API environment variable.
61+
62+
Common pitfalls to avoid:
63+
- Not providing enough context around changes
64+
- Providing too little surrounding code (include more than just the changed lines)
65+
- Incorrect indentation in the edit snippet
66+
- Forgetting to indicate unchanged sections with comments
67+
- Using inconsistent comment styles
68+
- Being too vague about where changes should be applied
69+
- Passing the edit_snippet as a dictionary or JSON object instead of a direct string
3270
"""
3371

3472
RELACE_EDIT_SYSTEM_PROMPT = """You are an expert at creating edit snippets for the Relace Instant Apply API.

src/codegen/extensions/tools/search.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def render(self) -> str:
114114
def _search_with_ripgrep(
115115
codebase: Codebase,
116116
query: str,
117-
target_directories: Optional[list[str]] = None,
118117
file_extensions: Optional[list[str]] = None,
119118
page: int = 1,
120119
files_per_page: int = 10,
@@ -141,9 +140,6 @@ def _search_with_ripgrep(
141140

142141
# Add target directories if specified
143142
search_path = codebase.repo_path
144-
if target_directories:
145-
# We'll handle target directories by filtering results later
146-
pass
147143

148144
# Add the query and path
149145
cmd.append(f"{query}")
@@ -189,10 +185,6 @@ def _search_with_ripgrep(
189185
# Convert to relative path within the codebase
190186
rel_path = os.path.relpath(filepath, codebase.repo_path)
191187

192-
# Skip if not in target directories
193-
if target_directories and not any(rel_path.startswith(d) for d in target_directories):
194-
continue
195-
196188
try:
197189
line_number = int(line_number_str)
198190

@@ -264,7 +256,6 @@ def _search_with_ripgrep(
264256
def _search_with_python(
265257
codebase: Codebase,
266258
query: str,
267-
target_directories: Optional[list[str]] = None,
268259
file_extensions: Optional[list[str]] = None,
269260
page: int = 1,
270261
files_per_page: int = 10,
@@ -304,10 +295,6 @@ def _search_with_python(
304295

305296
all_results = []
306297
for file in codebase.files(extensions=extensions):
307-
# Skip if file doesn't match target directories
308-
if target_directories and not any(file.filepath.startswith(d) for d in target_directories):
309-
continue
310-
311298
# Skip binary files
312299
try:
313300
content = file.content
@@ -366,7 +353,6 @@ def _search_with_python(
366353
def search(
367354
codebase: Codebase,
368355
query: str,
369-
target_directories: Optional[list[str]] = None,
370356
file_extensions: Optional[list[str]] = None,
371357
page: int = 1,
372358
files_per_page: int = 10,
@@ -383,7 +369,6 @@ def search(
383369
Args:
384370
codebase: The codebase to operate on
385371
query: The text to search for or regex pattern to match
386-
target_directories: Optional list of directories to search in
387372
file_extensions: Optional list of file extensions to search (e.g. ['.py', '.ts']).
388373
If None, searches all files ('*')
389374
page: Page number to return (1-based, default: 1)
@@ -395,7 +380,7 @@ def search(
395380
"""
396381
# Try to use ripgrep first
397382
try:
398-
return _search_with_ripgrep(codebase, query, target_directories, file_extensions, page, files_per_page, use_regex)
383+
return _search_with_ripgrep(codebase, query, file_extensions, page, files_per_page, use_regex)
399384
except (FileNotFoundError, subprocess.SubprocessError):
400385
# Fall back to Python implementation if ripgrep fails or isn't available
401-
return _search_with_python(codebase, query, target_directories, file_extensions, page, files_per_page, use_regex)
386+
return _search_with_python(codebase, query, file_extensions, page, files_per_page, use_regex)

0 commit comments

Comments
 (0)