Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 49 additions & 47 deletions corecoder/tools/glob_tool.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
"""File pattern matching."""

from pathlib import Path
from .base import Tool


class GlobTool(Tool):
name = "glob"
description = (
"Find files matching a glob pattern. "
"Supports ** for recursive matching (e.g. '**/*.py')."
)
parameters = {
"type": "object",
"properties": {
"pattern": {
"type": "string",
"description": "Glob pattern, e.g. '**/*.py' or 'src/**/*.ts'",
},
"path": {
"type": "string",
"description": "Directory to search in (default: cwd)",
},
},
"required": ["pattern"],
}

def execute(self, pattern: str, path: str = ".") -> str:
try:
base = Path(path).expanduser().resolve()
if not base.is_dir():
return f"Error: {path} is not a directory"

hits = list(base.glob(pattern))
# sort by mtime, newest first
hits.sort(key=lambda p: p.stat().st_mtime if p.exists() else 0, reverse=True)

total = len(hits)
shown = hits[:100]
lines = [str(h) for h in shown]
result = "\n".join(lines)

if total > 100:
result += f"\n... ({total} matches, showing first 100)"
return result or "No files matched."
except Exception as e:
return f"Error: {e}"
"""File pattern matching."""

from pathlib import Path
from .base import Tool


class GlobTool(Tool):
name = "glob"
description = (
"Find files matching a glob pattern. "
"Supports ** for recursive matching (e.g. '**/*.py')."
)
parameters = {
"type": "object",
"properties": {
"pattern": {
"type": "string",
"description": "Glob pattern, e.g. '**/*.py' or 'src/**/*.ts'",
},
"path": {
"type": "string",
"description": "Directory to search in (default: cwd)",
},
},
"required": ["pattern"],
}

def execute(self, pattern: str, path: str = ".") -> str:
try:
base = Path(path).expanduser().resolve()
if not base.exists():
return f"Error: {path} not found"
if not base.is_dir():
return f"Error: {path} is not a directory"

hits = list(base.glob(pattern))
# sort by mtime, newest first
hits.sort(key=lambda p: p.stat().st_mtime if p.exists() else 0, reverse=True)

total = len(hits)
shown = hits[:100]
lines = [str(h) for h in shown]
result = "\n".join(lines)

if total > 100:
result += f"\n... ({total} matches, showing first 100)"
return result or "No files matched."
except Exception as e:
return f"Error: {e}"
Loading