53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
# core/registry.py
|
|
import importlib
|
|
from django.apps import apps
|
|
from core.base import TaskModule
|
|
|
|
|
|
class TaskRegistry:
|
|
MODULES = {}
|
|
|
|
@classmethod
|
|
def discover_modules(cls):
|
|
for app_config in apps.get_app_configs():
|
|
try:
|
|
module_path = f"{app_config.name}.tasks"
|
|
importlib.import_module(module_path)
|
|
|
|
for obj_name in dir(apps.get_app_config(app_config.name).module):
|
|
obj = getattr(apps.get_app_config(app_config.name).module, obj_name)
|
|
if (isinstance(obj, type) and
|
|
issubclass(obj, TaskModule) and
|
|
obj.__name__ != 'TaskModule'):
|
|
cls.MODULES[obj.MODULE_NAME] = obj
|
|
except Exception:
|
|
continue
|
|
|
|
@classmethod
|
|
def get_all_tools(cls) -> list:
|
|
tools = []
|
|
for module in cls.MODULES.values():
|
|
tools.extend(module.get_tools())
|
|
return tools
|
|
|
|
@classmethod
|
|
def execute(cls, tool_name: str, arguments: dict) -> dict:
|
|
for name, module in cls.MODULES.items():
|
|
available_tools = [t["function"]["name"] for t in module.get_tools()]
|
|
if tool_name in available_tools:
|
|
return module.execute_tool(tool_name, arguments)
|
|
return {"status": "error", "message": f"Unknown tool: {tool_name}"}
|
|
|
|
@classmethod
|
|
def build_system_prompt(cls) -> str:
|
|
fragments = [
|
|
"You are an AI assistant with access to the following modules:",
|
|
*(f"- **{name.upper()}**: {mod.DESCRIPTION}" for name, mod in cls.MODULES.items()),
|
|
"\nOnly call tools that exist in your available tool list."
|
|
]
|
|
return "\n".join(fragments)
|
|
|
|
|
|
# Auto-discover on first import
|
|
TaskRegistry.discover_modules()
|