feat(plugin): implement caching for plugin agent tools registry

This commit is contained in:
jxxghp
2026-06-19 20:50:35 +08:00
parent 38c3dcc76b
commit 7f1cb40421
3 changed files with 290 additions and 89 deletions

View File

@@ -55,6 +55,9 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
self._stop_monitor_event = threading.Event()
# 本地插件同步写入运行目录后的短时忽略窗口
self._recent_local_sync: Dict[str, float] = {}
# 插件智能体工具注册表缓存,插件启停或配置生效时主动失效。
self._plugin_agent_tools_cache: Dict[str, List[Dict[str, Any]]] = {}
self._plugin_agent_tools_cache_lock = threading.Lock()
# 开发者模式监测插件修改
if settings.DEV or settings.PLUGIN_AUTO_RELOAD:
self.__start_monitor()
@@ -112,6 +115,7 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
eventmanager.disable_event_handler(plugin)
except Exception as err:
logger.error(f"加载插件 {plugin_id} 出错:{str(err)} - {traceback.format_exc()}")
self.clear_plugin_agent_tools_cache()
def init_plugin(self, plugin_id: str, conf: dict):
"""
@@ -131,6 +135,14 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
else:
# 禁用插件类的事件处理器
eventmanager.disable_event_handler(type(plugin))
self.clear_plugin_agent_tools_cache()
def clear_plugin_agent_tools_cache(self) -> None:
"""
清空插件智能体工具注册表缓存。
"""
with self._plugin_agent_tools_cache_lock:
self._plugin_agent_tools_cache.clear()
def stop(self, pid: Optional[str] = None):
"""
@@ -167,6 +179,7 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
self._running_plugins = {}
# 清除所有插件模块缓存
self._clear_plugin_modules()
self.clear_plugin_agent_tools_cache()
logger.info("插件停止完成")
@staticmethod
@@ -882,6 +895,21 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
logger.error(f"获取插件 {plugin_id} 动作出错:{str(e)}")
return ret_actions
@staticmethod
def _copy_plugin_agent_tools(
tools_info: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
"""
复制插件智能体工具注册信息,避免调用方修改缓存内容。
"""
return [
{
**plugin_info,
"tools": list(plugin_info.get("tools", [])),
}
for plugin_info in tools_info
]
def get_plugin_agent_tools(self, pid: Optional[str] = None) -> List[Dict[str, Any]]:
"""
获取插件智能体工具
@@ -891,6 +919,12 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
"tools": [ToolClass1, ToolClass2, ...]
}]
"""
cache_key = pid or "__all__"
with self._plugin_agent_tools_cache_lock:
cached_tools = self._plugin_agent_tools_cache.get(cache_key)
if cached_tools is not None:
return self._copy_plugin_agent_tools(cached_tools)
ret_tools = []
# 创建字典快照避免并发修改
running_plugins_snapshot = dict(self._running_plugins)
@@ -910,6 +944,10 @@ class PluginManager(ConfigReloadMixin, metaclass=Singleton):
})
except Exception as e:
logger.error(f"获取插件 {plugin_id} 智能体工具出错:{str(e)}")
with self._plugin_agent_tools_cache_lock:
self._plugin_agent_tools_cache[cache_key] = self._copy_plugin_agent_tools(
ret_tools
)
return ret_tools
@staticmethod