♻️ 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
@@ -1,3 +1,4 @@
import json
import types
from typing import Any, Generic, Union, cast, get_origin
@@ -6,14 +7,9 @@ from nonebot.compat import type_validate_json
from pydantic import BaseModel, Field, ValidationError, create_model
from zhenxun.services.ai.core.exceptions import (
ControlFlowExit,
ModelRetry,
SchemaParseError,
)
from zhenxun.services.ai.core.models import ToolDefinition
from zhenxun.services.ai.run.models import OutputDataT
from zhenxun.services.ai.tools.core.tool import BaseTool
from zhenxun.services.ai.tools.models import StructuredSubmissionResult, ToolResult
from zhenxun.services.ai.core.messages.types import OutputDataT
from zhenxun.services.ai.utils.logger import log_core as logger
from zhenxun.utils.pydantic_compat import model_json_schema, model_validate
@@ -96,8 +92,6 @@ class BaseOutputProcessor(Generic[OutputDataT]):
def _parse_and_validate(self, text: str) -> Any:
"""[私有方法] 执行带有容错修复的 JSON 解析与模型验证"""
if self.raw_schema is not None:
import json
try:
return json.loads(text)
except Exception:
@@ -152,74 +146,3 @@ class BaseOutputProcessor(Generic[OutputDataT]):
return final_obj
except Exception as e:
raise e
class SubmitFinalResultExecutable(BaseTool):
"""
动态生成的提交最终结果工具。
用于将大模型的结构化输出拦截并终止 AgentExecutor 的循环。
"""
def __init__(
self,
output_processor: BaseOutputProcessor,
guardrails: list[Any] | None = None,
):
"""
初始化提交最终结果的动态执行工具。
参数:
output_processor: 绑定的结构化输出处理器,用于验证提交的最终结果。
guardrails: 用于在结果输出前进行安全合规拦截的护栏中间件列表,默认 None。
"""
super().__init__(
name="submit_final_result",
description=(
"当你完成所有必要的调查 and 思考后,"
"必须且只能调用此工具来提交最终的结构化结果。"
"提交后任务将立刻结束。"
),
)
self.output_processor = output_processor
self.guardrails = guardrails or []
async def get_definition(self, context: Any | None = None) -> ToolDefinition | None:
if getattr(self, "_dynamic_def", None) is not None:
return self._dynamic_def
schema = self.output_processor.get_json_schema()
return ToolDefinition(
name=self.name,
description=self.description,
parameters=schema,
)
async def execute(self, context: Any | None = None, **kwargs) -> ToolResult:
parse_target = kwargs
if isinstance(kwargs, dict):
if "kwargs" in kwargs and len(kwargs) == 1:
parse_target = kwargs["kwargs"]
elif "result" in kwargs and len(kwargs) == 1:
parse_target = kwargs["result"]
try:
json_str = __import__("json").dumps(parse_target, ensure_ascii=False)
final_obj = await self.output_processor.validate_and_parse(
json_str, context=context
)
from zhenxun.services.ai.guardrails import GuardrailPipeline
pipeline = GuardrailPipeline(self.guardrails)
json_str, final_obj = await pipeline.run_output_pipeline(
json_str, final_obj, context
)
return StructuredSubmissionResult(
output="结构化数据已成功提交", parsed_obj=final_obj
)
except ControlFlowExit as e:
raise e
except ModelRetry as e:
raise e
except Exception as e:
error_msg = f"系统捕获到解析异常:\n{e}"
raise SchemaParseError(error_msg)
@@ -25,6 +25,8 @@ RoleT = TypeVar("RoleT", default=str, covariant=True)
ContentT = TypeVar("ContentT", default=LLMContentPart, covariant=True)
"""泛型:多模态片段数组的元素内容类型变量"""
OutputDataT = TypeVar("OutputDataT", default=str)
from .context_events import AgentEvent
from .models import (
@@ -53,6 +55,7 @@ __all__ = [
"AnyLLMMessage",
"ContentT",
"LLMContentPart",
"OutputDataT",
"PromptInput",
"RoleT",
"UserContentUnion",
+40 -13
View File
@@ -106,6 +106,7 @@ class EventBus:
defaultdict(list)
)
self._background_tasks = set()
self._async_handlers_queues: dict[Callable, asyncio.Queue] = {}
def subscribe(
self, event_type: type[T_Event], handler: Callable[[T_Event], Any]
@@ -119,6 +120,33 @@ class EventBus:
"""
self._subscribers[event_type].append(handler)
if (
is_coroutine_callable(handler)
and handler not in self._async_handlers_queues
):
q = asyncio.Queue(maxsize=1000)
self._async_handlers_queues[handler] = q
async def _worker(h=handler, queue=q):
while True:
evt = await queue.get()
if evt is None:
queue.task_done()
break
try:
await h(evt)
except asyncio.CancelledError:
queue.task_done()
break
except Exception as err:
logger.error(f"EventBus 订阅者执行异常: {err}")
finally:
queue.task_done()
task = asyncio.create_task(_worker())
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)
async def emit(self, event: AgentStreamEvent) -> None:
"""
发布事件,触发所有匹配的订阅者,并将事件放入迭代队列中。
@@ -133,21 +161,14 @@ class EventBus:
for handler in handlers:
if is_coroutine_callable(handler):
async def _run_handler(h=handler, e=event):
try:
await h(e)
except Exception as err:
logger.error(f"EventBus 订阅者执行异常: {err}")
task = asyncio.create_task(_run_handler())
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)
q = self._async_handlers_queues.get(handler)
if q:
await q.put(event)
else:
try:
handler(event)
except Exception as err:
logger.error(f"EventBus 订阅者执行异常: {err}")
logger.error(f"EventBus 同步订阅者执行异常: {err}")
if not self._finished:
await self._queue.put(event)
@@ -156,11 +177,17 @@ class EventBus:
"""
结束事件总线。等待所有后台任务执行完毕,并向队列中投放结束标记以终止异步迭代。
"""
self._finished = True
await self._queue.put(None)
for q in self._async_handlers_queues.values():
await q.put(None)
if self._background_tasks:
tasks = list(self._background_tasks)
await asyncio.gather(*tasks, return_exceptions=True)
self._finished = True
await self._queue.put(None)
self._background_tasks.clear()
self._async_handlers_queues.clear()
async def __aiter__(self):
"""