Skip to content

Commit

Permalink
fix(agent): Fix scan_plugins function
Browse files Browse the repository at this point in the history
- Improve error logging in scan_plugins
- Fix logic to determine qualified module name
- Use importlib in scan_plugins rather than __import__ magic function
  • Loading branch information
Pwuts committed Mar 20, 2024
1 parent b7a3cbf commit 57bf4e2
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions autogpts/autogpt/autogpt/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import json
import logging
import os
import sys
import zipfile
from pathlib import Path
from typing import TYPE_CHECKING, List
Expand Down Expand Up @@ -220,21 +219,21 @@ def scan_plugins(config: Config) -> List[AutoGPTPluginTemplate]:

plugins_config = config.plugins_config
# Directory-based plugins
for plugin_path in [f.path for f in os.scandir(config.plugins_dir) if f.is_dir()]:
for plugin_path in [f for f in Path(config.plugins_dir).iterdir() if f.is_dir()]:
# Avoid going into __pycache__ or other hidden directories
if plugin_path.startswith("__"):
if plugin_path.name.startswith("__"):
continue

plugin_module_path = plugin_path.split(os.path.sep)
plugin_module_name = plugin_module_path[-1]
qualified_module_name = ".".join(plugin_module_path)
plugin_module_name = plugin_path.name
qualified_module_name = ".".join(plugin_path.parts)

try:
__import__(qualified_module_name)
except ImportError:
logger.error(f"Failed to load {qualified_module_name}")
plugin = importlib.import_module(qualified_module_name)
except ImportError as e:
logger.error(

Check warning on line 233 in autogpts/autogpt/autogpt/plugins/__init__.py

View check run for this annotation

Codecov / codecov/patch

autogpts/autogpt/autogpt/plugins/__init__.py#L232-L233

Added lines #L232 - L233 were not covered by tests
f"Failed to load {qualified_module_name} from {plugin_path}: {e}"
)
continue
plugin = sys.modules[qualified_module_name]

if not plugins_config.is_enabled(plugin_module_name):
logger.warning(
Expand All @@ -261,8 +260,8 @@ def scan_plugins(config: Config) -> List[AutoGPTPluginTemplate]:
zipped_package = zipimporter(str(plugin))
try:
zipped_module = zipped_package.load_module(str(module.parent))
except ZipImportError:
logger.error(f"Failed to load {str(module.parent)}")
except ZipImportError as e:
logger.error(f"Failed to load {module.parent} from {plugin}: {e}")

Check warning on line 264 in autogpts/autogpt/autogpt/plugins/__init__.py

View check run for this annotation

Codecov / codecov/patch

autogpts/autogpt/autogpt/plugins/__init__.py#L263-L264

Added lines #L263 - L264 were not covered by tests
continue

for key in dir(zipped_module):
Expand Down

0 comments on commit 57bf4e2

Please # to comment.