File size: 7,442 Bytes
2531078
 
 
 
b64b6b0
2531078
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""GlobTool - File pattern matching for Stack 2.9"""

import fnmatch
import os
import re
from pathlib import Path
from typing import Any, Dict, List, Optional

from .base import BaseTool, ToolResult
from .registry import tool_registry

# Default exclusions
DEFAULT_EXCLUDES = {
    '.git', '.svn', '.hg', '__pycache__', 'node_modules', '.venv', 'venv',
    'env', '.idea', '.vscode', '.DS_Store', '*.pyc', '*.pyo', '*.so',
    '*.dylib', '.cache', '.pytest_cache', '.mypy_cache', 'dist', 'build',
    '*.egg-info', '.tox', '.nox'
}


def _should_exclude(path: Path, exclude_patterns: List[str]) -> bool:
    """Check if path should be excluded."""
    name = path.name

    # Check default exclusions
    if name in DEFAULT_EXCLUDES:
        return True

    # Check custom patterns
    for pattern in exclude_patterns:
        if fnmatch.fnmatch(name, pattern) or fnmatch.fnmatch(str(path), pattern):
            return True

    return False


def _glob_pattern_to_regex(pattern: str) -> str:
    """Convert glob pattern to regex."""
    # Handle ** for recursive matching
    regex_parts = []
    i = 0
    while i < len(pattern):
        c = pattern[i]
        if c == '*':
            if i + 1 < len(pattern) and pattern[i + 1] == '*':
                # ** matches everything including /
                regex_parts.append('.*')
                i += 2
            else:
                # * matches everything except /
                regex_parts.append('[^/]*')
                i += 1
        elif c == '?':
            regex_parts.append('.')
            i += 1
        elif c == '[':
            # Character class
            j = i + 1
            if j < len(pattern) and pattern[j] == '!':
                regex_parts.append('[^')
                j += 1
            else:
                regex_parts.append('[')
            while j < len(pattern) and pattern[j] != ']':
                regex_parts.append(re.escape(pattern[j]))
                j += 1
            regex_parts.append(']')
            i = j + 1
        else:
            regex_parts.append(re.escape(c))
            i += 1

    return ''.join(regex_parts)


def _match_glob(path: Path, pattern: str) -> bool:
    """Check if path matches glob pattern."""
    import re

    # Normalize pattern - handle **/*.py style patterns
    if pattern.startswith('**/'):
        # Recursive pattern
        regex_pattern = _glob_pattern_to_regex(pattern)
        regex = re.compile(regex_pattern)
        return bool(regex.match(str(path))) or bool(regex.match(path.name))
    elif '**' in pattern:
        regex_pattern = _glob_pattern_to_regex(pattern)
        regex = re.compile(regex_pattern)
        return bool(regex.match(str(path)))
    else:
        # Simple pattern
        return fnmatch.fnmatch(path.name, pattern) or fnmatch.fnmatch(str(path), pattern)


class GlobTool(BaseTool):
    """Find files matching glob patterns."""

    name = "glob"
    description = "Find files matching glob patterns"

    input_schema = {
        "type": "object",
        "properties": {
            "pattern": {"type": "string", "description": "Glob pattern (e.g., **/*.py, *.js)"},
            "base_path": {"type": "string", "description": "Base directory to search"},
            "exclude": {"type": "array", "items": {"type": "string"}, "description": "Patterns to exclude"},
            "max_results": {"type": "number", "default": 1000, "description": "Maximum results"},
            "files_only": {"type": "boolean", "default": True, "description": "Only return files"}
        },
        "required": ["pattern"]
    }

    async def execute(self, pattern: str, base_path: Optional[str] = None, exclude: Optional[List[str]] = None, max_results: int = 1000, files_only: bool = True) -> ToolResult:
        """Find files matching pattern."""
        if base_path:
            search_path = Path(base_path)
        else:
            search_path = Path.cwd()

        if not search_path.exists():
            return ToolResult(success=False, error=f"Path not found: {search_path}")

        exclude_patterns = exclude or []
        matches = []
        visited_dirs = set()

        def search_dir(dir_path: Path, depth: int = 0):
            """Recursively search directory."""
            if str(dir_path) in visited_dirs:
                return
            visited_dirs.add(str(dir_path))

            try:
                for item in dir_path.iterdir():
                    if _should_exclude(item, exclude_patterns):
                        continue

                    if item.is_file():
                        if _match_glob(item, pattern):
                            matches.append(str(item))
                            if len(matches) >= max_results:
                                return True
                    elif item.is_dir():
                        # Handle ** pattern
                        if '**' in pattern:
                            search_dir(item, depth + 1)
                        elif depth < 20:  # Limit depth for non-** patterns
                            search_dir(item, depth + 1)
            except PermissionError:
                pass

            return False

        search_dir(search_path)

        return ToolResult(success=True, data={
            "pattern": pattern,
            "base_path": str(search_path),
            "matches": matches,
            "count": len(matches),
            "truncated": len(matches) >= max_results
        })


class GlobListTool(BaseTool):
    """List all files in directory with optional filtering."""

    name = "glob_list"
    description = "List files in directory with optional pattern filter"

    input_schema = {
        "type": "object",
        "properties": {
            "path": {"type": "string", "description": "Directory path"},
            "pattern": {"type": "string", "description": "Optional pattern filter"},
            "recursive": {"type": "boolean", "default": False, "description": "Recursive listing"},
            "max_results": {"type": "number", "default": 500}
        },
        "required": ["path"]
    }

    async def execute(self, path: str, pattern: Optional[str] = None, recursive: bool = False, max_results: int = 500) -> ToolResult:
        """List directory contents."""
        dir_path = Path(path)
        if not dir_path.exists():
            return ToolResult(success=False, error=f"Path not found: {path}")

        matches = []

        def search_dir(d: Path, depth: int = 0):
            if len(matches) >= max_results:
                return

            try:
                for item in d.iterdir():
                    if item.name.startswith('.'):
                        continue

                    if pattern:
                        if _match_glob(item, pattern):
                            matches.append(str(item))
                    else:
                        matches.append(str(item))

                    if item.is_dir() and recursive and depth < 10:
                        search_dir(item, depth + 1)

                    if len(matches) >= max_results:
                        return
            except PermissionError:
                pass

        search_dir(dir_path)

        return ToolResult(success=True, data={
            "path": str(dir_path),
            "files": matches,
            "count": len(matches)
        })


# Register tools
tool_registry.register(GlobTool())
tool_registry.register(GlobListTool())