Skip to content

Commit 7207311

Browse files
committed
Enhance FindTextTool with recursive search option and improve json_schema specification.
1 parent 5cc287a commit 7207311

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

patchwork/common/tools/grep_tool.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def json_schema(self) -> dict:
115115
"name": "find_text",
116116
"description": f"""\
117117
Tool to find text in a file or files in a directory using a pattern based on the Unix shell style.
118-
The current working directory is always {self.__working_dir}.
118+
The current working directory is always {self.__working_dir}.
119119
The path provided should either be absolute or relative to the current working directory.
120120
121121
This tool will match each line of the file with the provided pattern and prints the line number and the line content.
@@ -135,19 +135,26 @@ def json_schema(self) -> dict:
135135
[!seq] matches any char not in seq
136136
137137
Example:
138-
* '*macs' will match the file '.emacs'
139-
* '*.py' will match all files with the '.py' extension
138+
* 'class T*' will match the line 'class Team:' but not ' class Team:' because the second line is indented.
139+
* '*var1: str' will match the line ' var1: str' but not 'var1: str = "test"'
140+
* '*class Team*' will match both the lines 'class Team:' and 'class TeamMember(BaseTeam):'
141+
* 'TeamMember' will not match 'class TeamMember:' because the pattern should match the entire line
142+
140143
""",
141144
"type": "string",
142145
},
143146
"path": {
144147
"description": """\
145-
The path to the file to find text in.
148+
The path to the file to find text in.
146149
If not given, will search all file content in the current working directory.
147150
If the path is a directory, will search all file content in the directory.
148151
""",
149152
"type": "string",
150153
},
154+
"recursive": {
155+
"description": "Set as False to only search specified file or immediate files in the specified directory. Default is True.",
156+
"type": "boolean",
157+
},
151158
"is_case_sensitive": {
152159
"description": "Whether the pattern should be case-sensitive.",
153160
"type": "boolean",
@@ -162,6 +169,7 @@ def execute(
162169
pattern: Optional[str] = None,
163170
path: Optional[Path] = None,
164171
is_case_sensitive: bool = False,
172+
recursive: bool = True,
165173
) -> str:
166174
if pattern is None:
167175
return "`pattern` argument is required!"
@@ -176,12 +184,14 @@ def execute(
176184
try:
177185
path = Path(path).resolve()
178186
except FileNotFoundError:
179-
return f"`path` does not exist"
187+
return "`path` does not exist"
180188
if not path.is_relative_to(self.__working_dir):
181189
return f"Path must be relative to working dir {self.__working_dir}"
182190

183191
if path.is_file():
184192
paths = [path]
193+
elif recursive:
194+
paths = list(set(p for p in path.rglob("*") if p.is_file()))
185195
else:
186196
paths = [p for p in path.iterdir() if p.is_file()]
187197

@@ -200,17 +210,17 @@ def execute(
200210
content = f"Line {i + 1}: {self.__CHAR_LIMIT_TEXT}"
201211

202212
file_matches[str(path)].append(content)
203-
except Exception as e:
213+
except Exception:
204214
pass
205215

206216
total_file_matches = ""
207217
for path_str, matches in file_matches.items():
208-
total_file_matches += f"\nPattern matches found in '{path}':\n" + "\n".join(matches)
218+
total_file_matches += f"\nPattern matches found in '{path_str}':\n" + "\n".join(matches)
209219

210220
if len(total_file_matches) <= 5000:
211221
return total_file_matches
212222

213223
total_file_matches = ""
214224
for path_str, matches in file_matches.items():
215-
total_file_matches += f"\n {len(matches)} Pattern matches found in '{path}': <TRUNCATED>\n"
225+
total_file_matches += f"\n {len(matches)} Pattern matches found in '{path_str}': <TRUNCATED>\n"
216226
return total_file_matches

0 commit comments

Comments
 (0)