# models | Marsho AI
Kawaii, Intelligent and Easy to Extend

Skip to content

# Module `nonebot_plugin_marshoai.models` ​

### class `Cache` ​

#### func `__init__(self)` ​

Source code or View on GitHub
python

    def __init__(self):
        self.cache = {}

#### func `get(self, key)` ​

Source code or View on GitHub
python

    def get(self, key):
        if key in self.cache:
            return self.cache[key]
        else:
            self.cache[key] = None
            return None

#### func `set(self, key, value)` ​

Source code or View on GitHub
python

    def set(self, key, value):
        self.cache[key] = value

### class `MarshoContext` ​

#### func `__init__(self)` ​

Source code or View on GitHub
python

    def __init__(self):
        self.contents = {'private': {}, 'non-private': {}}

#### func `append(self, content, target_id: str, is_private: bool)` ​

Description: 往上下文中添加消息

Source code or View on GitHub
python

    def append(self, content, target_id: str, is_private: bool):
        target_dict = self._get_target_dict(is_private)
        target_dict.setdefault(target_id, []).append(content)

#### func `set_context(self, contexts, target_id: str, is_private: bool)` ​

Description: 设置上下文

Source code or View on GitHub
python

    def set_context(self, contexts, target_id: str, is_private: bool):
        self._get_target_dict(is_private)[target_id] = contexts

#### func `reset(self, target_id: str, is_private: bool)` ​

Description: 重置上下文

Source code or View on GitHub
python

    def reset(self, target_id: str, is_private: bool):
        self._get_target_dict(is_private).pop(target_id, None)

#### func `reset_all(self)` ​

Description: 重置所有上下文

Source code or View on GitHub
python

    def reset_all(self):
        self.contents = {'private': {}, 'non-private': {}}

#### func `build(self, target_id: str, is_private: bool) -> list` ​

Description: 构建返回的上下文，不包括系统消息

Source code or View on GitHub
python

    def build(self, target_id: str, is_private: bool) -> list:
        return self._get_target_dict(is_private).setdefault(target_id, [])

### class `MarshoTools` ​

#### func `__init__(self)` ​

Source code or View on GitHub
python

    def __init__(self):
        self.tools_list = []
        self.imported_packages = {}

#### func `load_tools(self, tools_dir)` ​

Description: 从指定路径加载工具包

Source code or View on GitHub
python

    def load_tools(self, tools_dir):
        if not os.path.exists(tools_dir):
            logger.error(f'工具集目录 {tools_dir} 不存在。')
            return
        for package_name in os.listdir(tools_dir):
            package_path = os.path.join(tools_dir, package_name)
            if package_name in config.marshoai_disabled_toolkits:
                logger.info(f'工具包 {package_name} 已被禁用。')
                continue
            if os.path.isdir(package_path) and os.path.exists(os.path.join(package_path, '__init__.py')):
                self._load_package(package_name, package_path)
            else:
                logger.warning(f'{package_path} 不是有效的工具包路径，跳过加载。')

#### async func `call(self, full_function_name: str, args: dict)` ​

Description: 调用指定的函数

Source code or View on GitHub
python

    async def call(self, full_function_name: str, args: dict):
        parts = full_function_name.split('__')
        if len(parts) != 2:
            logger.error('函数名无效')
            return
        package_name, function_name = parts
        if package_name in self.imported_packages:
            package = self.imported_packages[package_name]
            try:
                function = getattr(package, function_name)
                return await function(**args)
            except Exception as e:
                errinfo = f"调用函数 '{function_name}'时发生错误:{e}"
                logger.error(errinfo)
                return errinfo
        else:
            logger.error(f"工具包 '{package_name}' 未导入")

#### func `has_function(self, full_function_name: str) -> bool` ​

Description: 检查是否存在指定的函数

Source code or View on GitHub
python

    def has_function(self, full_function_name: str) -> bool:
        try:
            return any((t['function']['name'].replace('-', '_') == full_function_name.replace('-', '_') for t in self.tools_list))
        except Exception as e:
            logger.error(f"检查函数 '{full_function_name}' 时发生错误:{e}")
            return False

#### func `get_tools_list(self)` ​

Source code or View on GitHub
python

    def get_tools_list(self):
        if not self.tools_list or not config.marshoai_enable_tools:
            return None
        return self.tools_list