Skip to content

Commit b1bf3be

Browse files
Token efficient directory tree.
1 parent 42db4d1 commit b1bf3be

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

src/smolagents/bp_tools.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,8 @@ def detect_language(filename):
17271727
}
17281728

17291729
@tool
1730-
def list_directory_tree(folder_path: str, max_depth: int = 6, show_files: bool = True, add_function_signatures: bool = False, skip_dirs: object = None) -> str:
1730+
def list_directory_tree(folder_path: str, max_depth: int = 6, show_files: bool = True,
1731+
add_function_signatures: bool = False, skip_dirs: object = None, human: bool = True) -> str:
17311732
"""
17321733
Creates a tree-like view of a directory structure. This is useful for understanding
17331734
project structure without loading all file contents, saving context.
@@ -1753,6 +1754,7 @@ def list_directory_tree(folder_path: str, max_depth: int = 6, show_files: bool =
17531754
show_files: bool Whether to show files or only directories (default True)
17541755
add_function_signatures: bool Whether to extract and display function signatures for source code files (default False)
17551756
skip_dirs: set/list of directory names to show but not inspect (default: build, dist, __pycache__, node_modules, .eggs)
1757+
human: formatted for humans (token expensive)
17561758
17571759
Returns:
17581760
str: A string representation of the directory tree
@@ -1788,7 +1790,10 @@ def add_tree_lines(current_path, prefix="", depth=0):
17881790
item_path = os.path.join(current_path, item)
17891791

17901792
# Choose the appropriate tree characters
1791-
connector = "└── " if is_last else "├── "
1793+
if human:
1794+
connector = "└── " if is_last else "├── "
1795+
else:
1796+
connector = ""
17921797

17931798
# If this is a directory in skip_dirs, show it but don't recurse
17941799
if os.path.isdir(item_path) and item in effective_skip_dirs:
@@ -1832,7 +1837,10 @@ def add_tree_lines(current_path, prefix="", depth=0):
18321837
sig_prefix = prefix + extension
18331838
for sig in signatures.split('\n'):
18341839
if sig.strip(): # Only add non-empty signature lines
1835-
lines.append(f"{sig_prefix} {sig.strip()}")
1840+
if human:
1841+
lines.append(f"{sig_prefix} {sig.strip()}")
1842+
else:
1843+
lines.append(f"{sig.strip()}")
18361844
except (
18371845
UnicodeDecodeError,
18381846
PermissionError,
@@ -1844,9 +1852,12 @@ def add_tree_lines(current_path, prefix="", depth=0):
18441852

18451853
# Recurse into subdirectories
18461854
if os.path.isdir(item_path):
1847-
extension = " " if is_last else "│ "
1848-
add_tree_lines(item_path, prefix + extension, depth + 1)
1849-
1855+
if human:
1856+
extension = " " if is_last else "│ "
1857+
add_tree_lines(item_path, prefix + extension, depth + 1)
1858+
else:
1859+
add_tree_lines(item_path, prefix, depth + 1)
1860+
18501861
# Add root folder
18511862
lines.append(f"{os.path.basename(folder_path)}/")
18521863
add_tree_lines(folder_path, "", 0)
@@ -1865,7 +1876,7 @@ def inject_tree(folder: str) -> str:
18651876
Args:
18661877
folder: path to the folder to generate the tree from.
18671878
"""
1868-
tree = list_directory_tree(folder_path=folder, add_function_signatures=True)
1879+
tree = list_directory_tree(folder_path=folder, add_function_signatures=True, human=False)
18691880
return (
18701881
"\nThis is the result of list_directory_tree:\n<directory_tree>\n"
18711882
+ tree

0 commit comments

Comments
 (0)