AIbyKaindu commited on
Commit
fd7cc8a
·
verified ·
1 Parent(s): 4f3a1f8

Create Codebase_Inspector.py

Browse files
Files changed (1) hide show
  1. tools/Codebase_Inspector.py +51 -0
tools/Codebase_Inspector.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from smolagents import tool
3
+
4
+ @tool
5
+ def codebase_inspector(action: str, target_path: str, file_content: str = "") -> str:
6
+ """A tool to explore, read, or create/append files in the active agent repository workspace.
7
+
8
+ Args:
9
+ action: The operation to execute. Choose from: 'list_dir', 'read_file', 'write_file', or 'append_file'.
10
+ target_path: The relative file path or directory pathway (e.g., 'tools/my_new_tool.py' or 'tools').
11
+ file_content: Optional string containing python code or text to write/append.
12
+ """
13
+ try:
14
+ # Prevent escaping the root directory for fundamental space isolation
15
+ normalized_path = os.path.normpath(target_path)
16
+ if normalized_path.startswith(".."):
17
+ return "Error: Access denied. Cannot navigate outside the repository root workspace."
18
+
19
+ if action == 'list_dir':
20
+ if os.path.exists(normalized_path) and os.path.isdir(normalized_path):
21
+ files = os.listdir(normalized_path)
22
+ return f"Directory contents of '{normalized_path}': {str(files)}"
23
+ return f"Error: Path '{normalized_path}' does not exist or is not a directory."
24
+
25
+ elif action == 'read_file':
26
+ if os.path.exists(normalized_path) and os.path.isfile(normalized_path):
27
+ with open(normalized_path, 'r', encoding='utf-8') as f:
28
+ content = f.read()
29
+ return f"--- START OF FILE: {normalized_path} ---\n{content}\n--- END OF FILE ---"
30
+ return f"Error: File '{normalized_path}' not found."
31
+
32
+ elif action in ['write_file', 'append_file']:
33
+ if not file_content:
34
+ return "Error: 'file_content' argument cannot be empty when writing or appending."
35
+
36
+ # Ensure directories exist
37
+ dir_name = os.path.dirname(normalized_path)
38
+ if dir_name and not os.path.exists(dir_name):
39
+ os.makedirs(dir_name, exist_ok=True)
40
+
41
+ mode = 'w' if action == 'write_file' else 'a'
42
+ with open(normalized_path, mode, encoding='utf-8') as f:
43
+ f.write(file_content)
44
+
45
+ return f"Successfully executed '{action}' on '{normalized_path}'."
46
+
47
+ else:
48
+ return f"Error: Action '{action}' is invalid. Use list_dir, read_file, write_file, or append_file."
49
+
50
+ except Exception as e:
51
+ return f"An exception occurred while operating on the codebase: {str(e)}"