mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-18 02:30:08 +08:00
♻️ refactor(core): 重构 AI 编排框架与记忆及 RAG 子系统 (#2149)
* ♻️ refactor(core): 重构 AI 编排框架与记忆及 RAG 子系统 - 【重构】重构 `BaseRunnable` 并引入统一的 `RunIntent` 意图载体,规范 Agent、Team 和 Workflow 的执行流 - 【解耦】将中期记忆槽和长期向量记忆从 `MemoryConfig` 中解耦,转为独立的能力组件与工具箱进行管理 - 【记忆】移除 `MemoryReader` 和 `MemoryWriter`,统一封装为 `SessionMemoryContext` 会话记忆门面 - 【RAG】重构检索器与存储后端接口,统一采用 `QueryRequest` 进行多维度联合检索,并引入 `InMemoryScorer` 提升打分性能 - 【事件】优化 `EventBus` 异步事件分发机制,引入队列机制确保事件按序处理,避免并发竞态问题 - 【依赖注入】移除 `memory` 注入项,优化 `DependencyInjector` 的签名解析缓存以提升性能 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
webjoin111
pre-commit-ci[bot]
parent
922d092650
commit
52f7dbdedf
@@ -1,13 +1,11 @@
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
import inspect
|
||||
from typing import Any, cast
|
||||
|
||||
from nonebot.utils import is_coroutine_callable
|
||||
|
||||
from zhenxun.services.ai.capabilities import AbstractCapability
|
||||
from zhenxun.services.ai.flow.core.base import BaseRunnable
|
||||
from zhenxun.services.ai.run import RunContext
|
||||
from zhenxun.services.ai.run.di import DependencyInjector
|
||||
from zhenxun.services.ai.tools.bridges.handoff import HandoffTool
|
||||
from zhenxun.services.ai.tools.core.tool import BaseTool
|
||||
|
||||
from .models import Transition
|
||||
|
||||
@@ -18,8 +16,8 @@ class TeamRoutingCapability(AbstractCapability):
|
||||
def __init__(
|
||||
self,
|
||||
team_name: str,
|
||||
members: list[Any],
|
||||
state_flow: Mapping[str, Sequence[Any]] | Callable | None = None,
|
||||
members: list[BaseRunnable],
|
||||
state_flow: Mapping[str, Sequence[Transition | str]] | Callable | None = None,
|
||||
max_handoffs: int = 3,
|
||||
):
|
||||
self.team_name = team_name
|
||||
@@ -27,7 +25,9 @@ class TeamRoutingCapability(AbstractCapability):
|
||||
self.state_flow = state_flow
|
||||
self.max_handoffs = max_handoffs
|
||||
|
||||
async def _get_allowed_transitions(self, context: RunContext) -> list[Any] | None:
|
||||
async def _get_allowed_transitions(
|
||||
self, context: RunContext
|
||||
) -> list[Transition] | None:
|
||||
"""核心FSM解析:解析静态字典或动态执行函数获取允许的 Transition 列表"""
|
||||
if self.state_flow is None:
|
||||
return None
|
||||
@@ -45,16 +45,10 @@ class TeamRoutingCapability(AbstractCapability):
|
||||
]
|
||||
|
||||
if callable(self.state_flow):
|
||||
sig = inspect.signature(self.state_flow)
|
||||
kwargs = await DependencyInjector.resolve_all(
|
||||
sig, call_kwargs={}, context=context
|
||||
result = await DependencyInjector.invoke(
|
||||
self.state_flow, call_kwargs={}, context=context
|
||||
)
|
||||
|
||||
if is_coroutine_callable(self.state_flow):
|
||||
result = await cast(Callable, self.state_flow)(**kwargs)
|
||||
else:
|
||||
result = cast(Callable, self.state_flow)(**kwargs)
|
||||
|
||||
if result is None:
|
||||
return None
|
||||
|
||||
@@ -62,7 +56,7 @@ class TeamRoutingCapability(AbstractCapability):
|
||||
|
||||
return None
|
||||
|
||||
async def get_tools(self, context: RunContext) -> list[Any]:
|
||||
async def get_tools(self, context: RunContext) -> list[BaseTool]:
|
||||
tools = []
|
||||
allowed_transitions = await self._get_allowed_transitions(context)
|
||||
|
||||
@@ -81,10 +75,7 @@ class TeamRoutingCapability(AbstractCapability):
|
||||
if transition is None:
|
||||
continue
|
||||
|
||||
if getattr(m, "persona", None):
|
||||
desc = f"角色:{m.persona.role},目标:{m.persona.goal}"
|
||||
else:
|
||||
desc = getattr(m, "description", "") or "处理节点"
|
||||
desc = m.profile_summary
|
||||
|
||||
if transition and getattr(transition, "description", ""):
|
||||
desc += f" 【移交条件】:{transition.description}"
|
||||
|
||||
Reference in New Issue
Block a user