-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathcodeindex.py
167 lines (122 loc) · 5.12 KB
/
codeindex.py
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
import os
from devon_agent.retrieval.code_index import CodeIndex
from devon_agent.tool import Tool
from dataclasses import dataclass
def setup_code_index(ctx, **kwargs):
if ctx["state"].code_index:
return ctx["state"].code_index
else:
if "cache_path" in kwargs and os.path.exists(kwargs["cache_path"]):
return CodeIndex.load_from_json(kwargs["cache_path"])
else:
codebase_path = None
if "codebase_path" in kwargs:
codebase_path = kwargs["codebase_path"]
else:
codebase_path = ctx["session"].base_path
if codebase_path is None:
raise ValueError("Codebase path is required")
code_index = CodeIndex(codebase_path)
code_index.initialize()
if "cache_path" in kwargs:
code_index.save_as_json(kwargs["cache_path"])
return code_index
def cleanup_code_index(ctx, code_index, **kwargs):
if "cache_path" in kwargs:
if not os.path.exists(kwargs["cache_path"]):
code_index.save_as_json(kwargs["cache_path"])
class FindFunctionTool(Tool):
@property
def name(self):
return "find_function"
def setup(self, ctx, **kwargs):
self.code_index = setup_code_index(ctx, **kwargs)
def cleanup(self, ctx):
cleanup_code_index(ctx, self.code_index, **self.kwargs)
def supported_formats(self):
return ["docstring", "manpage"]
def documentation(self, format="docstring"):
match format:
case "docstring":
return self.function.__doc__
case "manpage":
return """NAME
find_function - get location of function or method in the codebase
SYNOPSIS
find_function [FUNCTION_NAME]
DESCRIPTION
The find_function command searches the codebase for a function with the given name and returns its location.
OPTIONS
FUNCTION_NAME
The name of the function to search for. Only function name. For methods specify the class name and the method name separated by a dot.
RETURN VALUE
The location of the function in the codebase. A dictionary containing the following keys:
- file_path: The path to the file containing the function.
- line_number: The line number in the file where the function is defined.
EXAMPLES
To find the location of a function named "my_function", run the following command:
find_function "my_function"
The command will return a dictionary containing the file path and line number of the function:
{
"file_path": "/path/to/file.py",
"line_number": 10
}
To find the location of a function named "my_function" in class "MyClass", run the following command:
find_function "MyClass.my_function"
The command will return a dictionary containing the file path and line number of the function:
{
"file_path": "/path/to/file.py",
"line_number": 10
}
"""
case _:
raise ValueError(f"Unsupported format: {format}")
def function(self, ctx, function_name: str, **kwargs):
"""
find_function [function_name] - Find the location of a function in the codebase.
"""
return self.code_index.function_table.get_function_with_location(function_name)
class FindClassTool(Tool):
@property
def name(self):
return "find_class"
def setup(self, ctx, **kwargs):
self.code_index = setup_code_index(ctx, **kwargs)
def cleanup(self, ctx):
cleanup_code_index(ctx, self.code_index, **self.kwargs)
def supported_formats(self):
return ["docstring", "manpage"]
def documentation(self, format="docstring"):
match format:
case "docstring":
return self.function.__doc__
case "manpage":
return """NAME
find_class - get location of class in the codebase
SYNOPSIS
find_class [CLASS_NAME]
DESCRIPTION
The find_class command searches the codebase for a class with the given name and returns its location.
OPTIONS
CLASS_NAME
The name of the class to search for.
RETURN VALUE
The location of the class in the codebase. A dictionary containing the following keys:
- file_path: The path to the file containing the class.
- line_number: The line number in the file where the class is defined.
EXAMPLES
To find the location of a class named "MyClass", run the following command:
find_class "MyClass"
The command will return a dictionary containing the file path and line number of the class:
{
"file_path": "/path/to/file.py",
"line_number": 10
}
"""
case _:
raise ValueError(f"Unsupported format: {format}")
def function(self, ctx, class_name: str, **kwargs):
"""
find_class [class_name] - Find the location of a class in the codebase.
"""
return self.code_index.class_table.get_class_with_location(class_name)