Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions backend/python/app/agents/tools/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ def import_modules(self, module_paths: List[str]) -> Dict[str, Any]:
"imported": self.imported_modules,
"failed": self.failed_imports,
"success_rate": (
len(self.imported_modules) / len(module_paths)
if module_paths else 0
)
len(self.imported_modules) / len(module_paths) if module_paths else 0
),
}


Expand Down Expand Up @@ -100,14 +99,25 @@ def discover(self, base_dir: Path, importer: ModuleImporter) -> List[str]:

# Import main module if exists
main_module = app_dir / f"{self.app_name}.py"
if main_module.exists():
modules.append(f"app.agents.actions.{self.app_name}.{self.app_name}")

# Import other Python files
for py_file in app_dir.glob("*.py"):
if py_file.name not in ToolDiscoveryConfig.SKIP_FILES:
module_name = py_file.stem
modules.append(f"app.agents.actions.{self.app_name}.{module_name}")
main_exists = main_module.exists()
# Precompute module prefix for efficiency
module_prefix = f"app.agents.actions.{self.app_name}."
skip_files_set = ToolDiscoveryConfig.SKIP_FILES

# Use list comprehension for faster iteration and avoid repetitive string formatting
py_files = [py_file for py_file in app_dir.glob("*.py")]
# We can pre-filter for non-skipped files
# Still preserve main module logic and order
if main_exists:
modules.append(f"{module_prefix}{self.app_name}")

# Avoid re-including main module (which can be included by glob)
main_module_name = f"{self.app_name}.py"
# Convert skip_files to a set for O(1) lookup
for py_file in py_files:
name = py_file.name
if name not in skip_files_set and name != main_module_name:
modules.append(f"{module_prefix}{py_file.stem}")

return modules

Expand Down Expand Up @@ -204,18 +214,15 @@ def _log_results(self) -> None:
self.logger.info(f"Modules imported: {len(self.importer.imported_modules)}")

if self.importer.failed_imports:
self.logger.warning(
f"Failed imports: {len(self.importer.failed_imports)}"
)
self.logger.warning(f"Failed imports: {len(self.importer.failed_imports)}")
for failure in self.importer.failed_imports[:5]: # Show first 5
self.logger.warning(f" - {failure}")

def _get_discovery_results(self) -> Dict[str, Any]:
"""Get discovery results"""
registered_tools = _global_tools_registry.list_tools()
total_attempts = (
len(self.importer.imported_modules) +
len(self.importer.failed_imports)
total_attempts = len(self.importer.imported_modules) + len(
self.importer.failed_imports
)

return {
Expand All @@ -225,8 +232,9 @@ def _get_discovery_results(self) -> Dict[str, Any]:
"total_tools": len(registered_tools),
"success_rate": (
len(self.importer.imported_modules) / total_attempts
if total_attempts > 0 else 0
)
if total_attempts > 0
else 0
),
}


Expand Down