from __future__ import annotations
from pydantic import BaseModel
from typing import ClassVar, Callable
        
class Callback(BaseModel):
    """
    回调

    定义回调结构时：
        class A(Callback):
            attr: type

    触发回调时：
        A(attr=value)

    为函数注册回调时：
        @A
        def func(cb:A):
            pass
    """
    name: ClassVar[str]
    """回调名称"""
    functions: ClassVar[list[Callable]] = []
    """注册的函数列表"""

    def __init__(self, *args, **kwargs):
        if len(args) == 1 and not kwargs and callable(args[0]):
            func = args[0]

            # 注册函数
            self.__class__.functions.append(func)
        else:
            super().__init__(*args, **kwargs)
            
            # 触发回调
            for func in self.functions:
                func(self)

class LoadMCPTools(Callback):
    """加载 MCP 工具"""
    name: str = "load_mcp_tools"

__all__ = [
    "Callback",
    "LoadMCPTools",
]