from abc import ABC
from functools import wraps
import inspect


# 工具类装饰
def tool(name, description):
    def decorator(cls):
        cls._name = name
        cls._description = description
        cls._is_tool = True
        return cls

    return decorator


# 工具方法装饰
def tool_func(param_description):
    def decorator(func):
        func._param_description = param_description

        @wraps(func)
        def wrapper(*args, **kwargs):
            result = func(*args, **kwargs)
            return result

        return wrapper

    return decorator


# 工具基类
class ToolBase(ABC):
    _name = 'tool_base'
    _description = 'tool_description'
    _is_tool = False

    def execute(self, *args, **kwargs):
        methods = inspect.getmembers(self, predicate=inspect.ismethod)
        for name, method in methods:
            func = method.__func__ if hasattr(method, '__func__') else method
            if hasattr(func, '_param_description'):
                result = func(self, *args, **kwargs)
                return result
        raise Exception('没有找到@tool_func装饰的方法')
