Skip to content

Commit 907f113

Browse files
committed
Add sort_by and max_result options in search_files + default path to first allowed dir (project dir)
1 parent 25d5345 commit 907f113

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

llms/extensions/computer/filesystem.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,15 +496,20 @@ def move_file(source: Annotated[str, "Source path"], destination: Annotated[str,
496496

497497

498498
def search_files(
499-
path: Annotated[str, "Path to search in"],
500499
pattern: Annotated[str, "Glob pattern to match"],
500+
path: Annotated[str, "Path to search in"] = None,
501501
exclude_patterns: Annotated[List[str], "Glob patterns to exclude"] = None,
502+
sort_by: Annotated[Literal["path", "modified", "size"], "Sort by path, modified or size"] = "path",
503+
max_results: int = 200,
502504
) -> str:
503505
"""
504506
Recursively search for files and directories matching a pattern. The patterns should be glob-style patterns that match paths relative to the working directory.
505507
Use pattern like '.ext' to match files in current directory, and '**/.ext' to match files in all subdirectories.
506508
Returns full paths to all matching items. Great for finding files when you don't know their exact location. Only searches within allowed directories.
509+
If no path is provided, searches in the first allowed directory.
507510
"""
511+
if not path:
512+
path = get_allowed_directories()[0]
508513
valid_path = _validate_path(path)
509514
results = []
510515
if exclude_patterns is None:
@@ -533,6 +538,16 @@ def search_files(
533538
except Exception as e:
534539
raise RuntimeError(f"Error searching files in {valid_path}: {e}") from e
535540

541+
if sort_by == "size":
542+
results.sort(key=lambda p: os.path.getsize(p) if os.path.exists(p) else 0, reverse=True)
543+
elif sort_by == "modified":
544+
results.sort(key=lambda p: os.path.getmtime(p) if os.path.exists(p) else 0, reverse=True)
545+
else: # path
546+
results.sort()
547+
548+
if max_results > 0:
549+
results = results[:max_results]
550+
536551
if not results:
537552
return "No matches found"
538553

0 commit comments

Comments
 (0)