FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/cecli/tools/explore_code.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
13
(13)
Pull requests
7
(7)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
cecli
/
tools
/
explore_code.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
387 lines (330 loc) · 15.6 KB
Breadcrumbs
cecli
/
cecli
/
tools
/
explore_code.py
Copy path
File metadata and controls
387 lines (330 loc) · 15.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import
os
from
cecli
.
tools
.
utils
.
base_tool
import
BaseTool
from
cecli
.
tools
.
utils
.
helpers
import
ToolError
from
cecli
.
tools
.
utils
.
output
import
color_markers
,
tool_footer
,
tool_header
from
cecli
.
tools
.
utils
.
responses
import
ToolResponse
from
cecli
.
tools
.
validations
import
ToolValidations
cwd
=
os
.
getcwd
()
try
:
import
cymbal
CYMBAL_AVAILABLE
=
True
except
ImportError
:
CYMBAL_AVAILABLE
=
False
finally
:
os
.
chdir
(
cwd
)
class
Tool
(
BaseTool
):
NORM_NAME
=
"explorecode"
VALIDATIONS
=
{
"queries"
: [
"coerce_list"
],
"queries[]"
: [
"coerce_dict"
],
}
RESULT_TYPE
=
"list"
SCHEMA
=
{
"type"
:
"function"
,
"function"
: {
"name"
:
"ExploreCode"
,
"description"
: (
"Search, investigate, and find references to symbols using the Cymbal code indexing"
" library. This is the preferred tool for analyzing code structure."
),
"parameters"
: {
"type"
:
"object"
,
"properties"
: {
"queries"
: {
"type"
:
"array"
,
"items"
: {
"type"
:
"object"
,
"properties"
: {
"symbol"
: {
"type"
:
"string"
,
"description"
: (
"The symbol name to search for, investigate, or find"
" references to. This should be a single symbol"
" (e.g. method, function, or class name)."
),
},
"action"
: {
"type"
:
"string"
,
"enum"
: [
"search"
,
"investigate"
,
"find_references"
],
"description"
: (
"Action to perform: 'search', 'investigate', or"
" 'find_references'.
\n
\n
Note: For the 'investigate' action,"
" you can use filename-based disambiguation:
\n
- '{file"
" name}:{symbol}' to specify a symbol in a particular file"
" (e.g., 'config.go:Config')
\n
"
),
},
"limit"
: {
"type"
:
"integer"
,
"description"
: (
"Maximum number of results to return. Defaults to 15."
),
"default"
:
15
,
},
},
"required"
: [
"symbol"
,
"action"
],
},
"description"
: (
"Array of exploration queries. Maximum of 5 queries at a time."
),
}
},
"required"
: [
"queries"
],
},
},
}
@
classmethod
def
execute
(
cls
,
coder
,
queries
,
**
kwargs
):
"""
Search, investigate, or find references to symbols using the Cymbal code indexing library.
Args:
coder: The Coder instance.
queries (list): Array of exploration queries {symbol, action, limit}.
Returns:
str: Formatted results from the Cymbal operations.
"""
response
=
ToolResponse
(
cls
.
NORM_NAME
,
result_type
=
"list"
)
try
:
# Check if cymbal is available
if
not
CYMBAL_AVAILABLE
:
coder
.
io
.
tool_error
(
"Cymbal library is not available. Please install it with: pip install py-cymbal"
)
response
.
append_error
(
"Cymbal library is not available"
)
return
response
# Initialize Cymbal and index if necessary
c
=
cymbal
.
Cymbal
()
repo_path
=
getattr
(
coder
,
"root"
,
"."
)
try
:
# Always index to ensure we have the latest data
c
.
index
(
repo_path
)
except
Exception
as
e
:
error_msg
=
f"Failed to index repository:
{
str
(
e
)
}
"
coder
.
io
.
tool_error
(
error_msg
)
response
.
append_error
(
error_msg
)
return
response
all_failed_queries
=
[]
total_successful_queries
=
0
for
query
in
queries
:
symbol
=
query
.
get
(
"symbol"
)
action
=
query
.
get
(
"action"
)
limit
=
query
.
get
(
"limit"
,
15
)
try
:
if
action
==
"search"
:
# Sanitize symbol: Cymbal's CLI interprets hyphens as SQL operators.
# Replace hyphens with underscores (common in code) and strip special chars.
safe_symbol
=
symbol
.
replace
(
"-"
,
"_"
)
if
symbol
else
symbol
results
=
c
.
search
(
safe_symbol
,
limit
=
limit
)
results
=
cls
.
_filter_gitignored
(
results
,
coder
)
response
.
append_result
(
content
=
cls
.
_format_search_results
(
results
,
symbol
))
elif
action
==
"investigate"
:
symbol_name
=
symbol
file_hint
=
""
if
":"
in
symbol
:
parts
=
symbol
.
split
(
":"
,
1
)
if
len
(
parts
)
==
2
:
file_hint
=
parts
[
0
]
symbol_name
=
parts
[
1
]
# Sanitize for Cymbal search
safe_name
=
symbol_name
.
replace
(
"-"
,
"_"
)
if
symbol_name
else
symbol_name
try
:
investigation
=
c
.
investigate
(
safe_name
,
file_hint
)
investigation
=
cls
.
_filter_investigation_gitignored
(
investigation
,
coder
)
response
.
append_result
(
content
=
cls
.
_format_investigation_results
(
investigation
,
symbol
)
)
except
Exception
as
e
:
if
"multiple matches"
in
str
(
e
).
lower
():
results
=
c
.
search
(
symbol_name
,
limit
=
10
)
response
.
append_result
(
content
=
(
f"Error: Multiple matches found for '
{
symbol
}
'.
\n
"
f"Please use a more specific name or check the locations"
f" below:
\n
"
+
"
\n
"
.
join
(
f"-
{
r
.
get
(
'rel_path'
)
or
r
.
get
(
'file'
,
''
)
}
:
{
r
.
get
(
'start_line'
,
0
)
}
"
for
r
in
results
)
)
)
else
:
raise
e
elif
action
==
"find_references"
:
safe_symbol
=
symbol
.
replace
(
"-"
,
"_"
)
if
symbol
else
symbol
references
=
c
.
find_references
(
safe_symbol
,
limit
=
limit
)
references
=
cls
.
_filter_gitignored
(
references
,
coder
)
response
.
append_result
(
content
=
cls
.
_format_reference_results
(
references
,
symbol
)
)
else
:
all_failed_queries
.
append
(
f"Error for symbol '
{
symbol
}
': Unknown action '
{
action
}
'"
)
continue
total_successful_queries
+=
1
except
Exception
as
e
:
all_failed_queries
.
append
(
f"Error for symbol '
{
symbol
}
':
{
str
(
e
)
}
"
)
if
total_successful_queries
==
0
:
error_msg
=
"No queries were successfully executed:
\n
"
+
"
\n
"
.
join
(
all_failed_queries
)
response
.
append_error
(
error_msg
)
return
response
if
all_failed_queries
:
for
failed_msg
in
all_failed_queries
:
coder
.
io
.
tool_error
(
failed_msg
)
else
:
coder
.
io
.
tool_output
(
"
\u2713
All queries successful."
,
type
=
"tool-result"
)
return
response
except
Exception
as
e
:
coder
.
io
.
tool_error
(
f"Error in ExploreCode:
{
str
(
e
)
}
"
)
response
.
append_error
(
str
(
e
))
return
response
finally
:
if
"c"
in
locals
():
c
.
close
()
@
classmethod
def
_filter_gitignored
(
cls
,
results
,
coder
):
"""Filter out results whose file path is git-ignored."""
if
not
results
or
not
hasattr
(
coder
,
"repo"
)
or
not
coder
.
repo
:
return
results
filtered
=
[]
for
r
in
results
:
file_path
=
r
.
get
(
"rel_path"
)
or
r
.
get
(
"file"
,
""
)
if
not
file_path
:
filtered
.
append
(
r
)
continue
if
not
coder
.
repo
.
git_ignored_file
(
file_path
):
filtered
.
append
(
r
)
return
filtered
@
classmethod
def
_filter_investigation_gitignored
(
cls
,
investigation
,
coder
):
"""Filter git-ignored entries from an investigation result."""
if
not
investigation
or
not
hasattr
(
coder
,
"repo"
)
or
not
coder
.
repo
:
return
investigation
# Filter references
if
"refs"
in
investigation
:
investigation
[
"refs"
]
=
cls
.
_filter_gitignored
(
investigation
[
"refs"
],
coder
)
# Filter impact/callers
if
"impact"
in
investigation
:
investigation
[
"impact"
]
=
cls
.
_filter_gitignored
(
investigation
[
"impact"
],
coder
)
return
investigation
@
classmethod
def
_format_search_results
(
cls
,
results
,
symbol
):
"""Format search results for display."""
if
not
results
:
return
f"No symbols found matching '
{
symbol
}
'"
formatted
=
[
f"Found
{
len
(
results
)
}
symbols matching '
{
symbol
}
':"
]
for
i
,
result
in
enumerate
(
results
[:
15
],
1
):
name
=
result
.
get
(
"name"
,
"Unknown"
)
kind
=
result
.
get
(
"kind"
,
"unknown"
)
file
=
result
.
get
(
"rel_path"
)
or
result
.
get
(
"file"
,
"Unknown"
)
start_line
=
result
.
get
(
"start_line"
,
0
)
signature
=
result
.
get
(
"signature"
,
""
)
parent
=
result
.
get
(
"parent"
)
location
=
f"
{
file
}
:
{
start_line
}
"
if
parent
:
location
=
f"
{
location
}
(in
{
parent
}
)"
formatted
.
append
(
f"
{
i
}
.
{
name
}
{
signature
}
(
{
kind
}
) at
{
location
}
"
)
if
len
(
results
)
>
15
:
formatted
.
append
(
f"... and
{
len
(
results
)
-
15
}
more results"
)
return
"
\n
"
.
join
(
formatted
)
@
classmethod
def
_format_investigation_results
(
cls
,
investigation
,
symbol
):
"""Format investigation results for display."""
if
not
investigation
:
return
f"No information found for symbol '
{
symbol
}
'"
# Handle nested structure if present
if
"results"
in
investigation
and
"result"
in
investigation
[
"results"
]:
investigation
=
investigation
[
"results"
][
"result"
]
formatted
=
[
f"Investigation of symbol '
{
symbol
}
':"
]
# Extract definition information
definition
=
investigation
.
get
(
"symbol"
)
if
definition
:
def_name
=
definition
.
get
(
"name"
,
symbol
)
def_file
=
definition
.
get
(
"rel_path"
)
or
definition
.
get
(
"file"
,
"Unknown"
)
def_line
=
definition
.
get
(
"start_line"
,
0
)
def_kind
=
definition
.
get
(
"kind"
,
"unknown"
)
def_sig
=
definition
.
get
(
"signature"
,
""
)
formatted
.
append
(
f"Definition:
{
def_name
}
{
def_sig
}
(
{
def_kind
}
) at
{
def_file
}
:
{
def_line
}
"
)
# Source code snippet
source
=
investigation
.
get
(
"source"
)
if
source
:
formatted
.
append
(
"
\n
Source Code:"
)
formatted
.
append
(
"```python"
)
formatted
.
append
(
source
.
strip
())
formatted
.
append
(
"```"
)
# References
references
=
investigation
.
get
(
"refs"
, [])
ref_count
=
len
(
references
)
if
references
else
0
formatted
.
append
(
f"
\n
References found:
{
ref_count
}
"
)
if
references
and
ref_count
>
0
:
formatted
.
append
(
"Top references:"
)
for
i
,
ref
in
enumerate
(
references
[:
10
],
1
):
ref_file
=
ref
.
get
(
"rel_path"
)
or
ref
.
get
(
"file"
,
"Unknown"
)
ref_line
=
ref
.
get
(
"line"
,
0
)
formatted
.
append
(
f"
{
i
}
.
{
ref_file
}
:
{
ref_line
}
"
)
if
ref_count
>
10
:
formatted
.
append
(
f"... and
{
ref_count
-
10
}
more references"
)
# Impact / Callers
impact
=
investigation
.
get
(
"impact"
, [])
if
impact
:
formatted
.
append
(
"
\n
Impact (Callers):"
)
for
i
,
imp
in
enumerate
(
impact
[:
10
],
1
):
imp_file
=
imp
.
get
(
"rel_path"
)
or
imp
.
get
(
"file"
,
"Unknown"
)
imp_line
=
imp
.
get
(
"line"
,
0
)
imp_caller
=
imp
.
get
(
"caller"
,
"unknown"
)
formatted
.
append
(
f"
{
i
}
.
{
imp_caller
}
at
{
imp_file
}
:
{
imp_line
}
"
)
if
len
(
impact
)
>
10
:
formatted
.
append
(
f"... and
{
len
(
impact
)
-
10
}
more callers"
)
return
"
\n
"
.
join
(
formatted
)
@
classmethod
def
_format_reference_results
(
cls
,
references
,
symbol
):
"""Format reference finding results for display."""
if
not
references
:
return
f"No references found for symbol '
{
symbol
}
'"
formatted
=
[
f"Found
{
len
(
references
)
}
references to '
{
symbol
}
':"
]
for
i
,
ref
in
enumerate
(
references
[:
15
],
1
):
file
=
ref
.
get
(
"rel_path"
)
or
ref
.
get
(
"file"
,
"Unknown"
)
line
=
ref
.
get
(
"line"
,
0
)
context
=
ref
.
get
(
"context"
, [])
formatted
.
append
(
f"
{
i
}
.
{
file
}
:
{
line
}
"
)
if
context
:
formatted
.
append
(
" Context:"
)
for
line_text
in
context
:
formatted
.
append
(
f"
{
line_text
.
strip
()
}
"
)
if
len
(
references
)
>
15
:
formatted
.
append
(
f"... and
{
len
(
references
)
-
15
}
more references"
)
return
"
\n
"
.
join
(
formatted
)
@
classmethod
def
format_output
(
cls
,
coder
,
mcp_server
,
tool_response
):
"""Format output for ExploreCode tool."""
color_start
,
color_end
=
color_markers
(
coder
)
# Output header
tool_header
(
coder
=
coder
,
mcp_server
=
mcp_server
,
tool_response
=
tool_response
)
try
:
params
=
ToolValidations
.
validate_params
(
tool_response
.
function
.
arguments
,
cls
.
VALIDATIONS
,
cls
.
SCHEMA
)
except
ToolError
:
coder
.
io
.
tool_error
(
"Invalid Tool JSON"
)
return
queries
=
params
.
get
(
"queries"
, [])
if
queries
:
coder
.
io
.
tool_output
(
""
)
for
i
,
query
in
enumerate
(
queries
):
symbol
=
query
.
get
(
"symbol"
,
""
)
action
=
query
.
get
(
"action"
,
""
)
limit
=
query
.
get
(
"limit"
,
15
)
# Format as "{action}: • {symbol} • {limit}" with action wrapped in color markers
# Capitalize action and replace underscores with spaces
formatted_action
=
action
formatted_query
=
f"
{
color_start
}
{
formatted_action
}
:
{
color_end
}
{
symbol
}
•
{
limit
}
"
coder
.
io
.
tool_output
(
formatted_query
)
coder
.
io
.
tool_output
(
""
)
# Output footer
tool_footer
(
coder
=
coder
,
tool_response
=
tool_response
,
params
=
params
)
Back
|
FazBrowse Home
|
New Git URL