♻️ 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:
Rumio
2026-07-14 16:48:33 +08:00
committed by GitHub
co-authored by webjoin111 pre-commit-ci[bot]
parent 922d092650
commit 52f7dbdedf
66 changed files with 2131 additions and 2353 deletions
+23 -15
View File
@@ -1,13 +1,14 @@
from abc import ABC, abstractmethod
import asyncio
from collections.abc import AsyncIterator
from typing import Any
from typing import cast
from zhenxun.services.ai.core.exceptions import (
AbortException,
ControlFlowExit,
ToolFatalError,
)
from zhenxun.services.ai.core.stream_events import AgentStreamEvent
from zhenxun.services.ai.run import RunContext
from zhenxun.services.ai.utils.logger import log_flow as logger
@@ -23,6 +24,21 @@ from .types import (
)
class StreamCapturer:
"""内部辅助类:透传工作流节点的内部流事件,并捕获最终的 StepOutput 产出值"""
def __init__(self, stream: AsyncIterator[AgentStreamEvent | StepOutput]):
self._stream = stream
self.output: StepOutput | None = None
async def __aiter__(self) -> AsyncIterator[AgentStreamEvent]:
async for event in self._stream:
if isinstance(event, StepOutput):
self.output = event
else:
yield event
class BaseNode(ABC):
"""工作流节点统一抽象基类"""
@@ -50,7 +66,7 @@ class BaseNode(ABC):
async def _handle_execution_failure(
self, e: BaseException, step_input: StepInput, context: RunContext, attempt: int
) -> tuple[str, StepOutput | None, StepInput | None, Any]:
) -> tuple[str, StepOutput | None, StepInput | None, "BaseNode | None"]:
"""
解析执行异常并应用容错策略
"""
@@ -128,19 +144,10 @@ class BaseNode(ABC):
@abstractmethod
async def run_stream(
self, step_input: StepInput, context: RunContext
) -> AsyncIterator[Any]:
) -> AsyncIterator[AgentStreamEvent | StepOutput]:
"""子类必须实现的核心流式执行逻辑"""
yield None
async def _forward_stream(
self, stream: AsyncIterator[Any], output_box: list[StepOutput]
) -> AsyncIterator[Any]:
"""辅助方法:转发内部流事件,并将最终的 StepOutput 拦截放入 output_box 列表中"""
async for event in stream:
if isinstance(event, StepOutput):
output_box.append(event)
else:
yield event
if False:
yield cast(StepOutput, None)
async def aexecute(self, step_input: StepInput, context: RunContext) -> StepOutput:
"""非流式执行(聚合流并返回最终结果),子类无需重写"""
@@ -160,7 +167,7 @@ class BaseNode(ABC):
async def aexecute_stream(
self, step_input: StepInput, context: RunContext
) -> AsyncIterator[Any]:
) -> AsyncIterator[AgentStreamEvent | StepOutput]:
"""标准化模板方法:处理缓存快进、授权挂起、异常熔断与生命周期事件分发"""
logger.debug(f" ⚙️ [节点] `{self.name}` 开始执行...")
@@ -222,4 +229,5 @@ class BaseNode(ABC):
yield evt
break
assert output is not None
yield output