Files
llm-bot-framework/plugins/__init__.py
2026-04-29 17:41:10 -05:00

48 lines
1.9 KiB
Python

import os
import importlib.util
import logging
logger = logging.getLogger(__name__)
def load_plugins(app=None):
"""
Automatically discover and load general plugins from plugins/.
Each plugin module can have optional hooks:
- register(app) for API routes
- register_commands() for bot commands
- register_tasks() for background tasks
- PLUGIN_NAME, PLUGIN_VERSION for metadata
"""
plugins_dir = os.path.dirname(__file__)
for filename in os.listdir(plugins_dir):
if filename.endswith('.py') and filename != '__init__.py':
module_name = filename[:-3] # remove .py
module_path = os.path.join(plugins_dir, filename)
try:
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Optional metadata
name = getattr(module, 'PLUGIN_NAME', module_name)
version = getattr(module, 'PLUGIN_VERSION', 'unknown')
# Apply hooks if available
if app and hasattr(module, 'register'):
module.register(app)
logger.info(f"Loaded plugin routes: {name} v{version}")
if hasattr(module, 'register_commands'):
module.register_commands()
logger.info(f"Loaded plugin commands: {name} v{version}")
if hasattr(module, 'register_tasks'):
module.register_tasks()
logger.info(f"Loaded plugin tasks: {name} v{version}")
logger.info(f"Loaded plugin: {name} v{version}")
except Exception as e:
logger.error(f"Failed to load plugin {module_name}: {e}")