修复子图包装器:使用dispatch_custom_event,用ainvoke
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Has been cancelled

This commit is contained in:
2026-05-06 19:10:20 +08:00
parent 47ccac5b35
commit 47e35bfd00

View File

@@ -9,6 +9,7 @@ from langchain_core.runnables.config import RunnableConfig
from .state import MainGraphState, ErrorRecord, ErrorSeverity from .state import MainGraphState, ErrorRecord, ErrorSeverity
from backend.app.logger import info from backend.app.logger import info
from ._utils import dispatch_custom_event, make_react_event
def wrap_subgraph_for_error_handling(subgraph, name: str): def wrap_subgraph_for_error_handling(subgraph, name: str):
@@ -23,27 +24,23 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
""" """
async def wrapped_node(state: MainGraphState, config: Optional[RunnableConfig] = None) -> MainGraphState: async def wrapped_node(state: MainGraphState, config: Optional[RunnableConfig] = None) -> MainGraphState:
# 发送子图开始事件 # 发送子图开始事件
if config:
try: try:
from langchain_core.callbacks.manager import adispatch_custom_event await dispatch_custom_event(
callbacks = config.get("callbacks")
if callbacks:
await adispatch_custom_event(
"react_reasoning", "react_reasoning",
{ make_react_event(
"step": state.reasoning_step, state.reasoning_step,
"action": f"{name}_subgraph_start", f"{name}_subgraph_start",
"confidence": 1.0, 1.0,
"reasoning": f"开始执行 {name} 子图..." f"开始执行 {name} 子图..."
}, ),
callbacks=callbacks config
) )
except Exception as e: except Exception as e:
info(f"[{name}_subgraph] 无法发送开始事件: {e}") info(f"[{name}_subgraph] 无法发送开始事件: {e}")
try: try:
# 调用子图 # 调用子图(异步,传 config
result = subgraph.invoke(state) result = await subgraph.ainvoke(state, config=config)
# 更新主图状态 # 更新主图状态
subgraph_result = None subgraph_result = None
@@ -75,20 +72,16 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
}) })
# 发送子图完成事件 # 发送子图完成事件
if config:
try: try:
from langchain_core.callbacks.manager import adispatch_custom_event await dispatch_custom_event(
callbacks = config.get("callbacks")
if callbacks:
await adispatch_custom_event(
"react_reasoning", "react_reasoning",
{ make_react_event(
"step": state.reasoning_step, state.reasoning_step,
"action": f"{name}_subgraph_complete", f"{name}_subgraph_complete",
"confidence": 1.0, 1.0,
"reasoning": f"{name} 子图执行完成" f"{name} 子图执行完成"
}, ),
callbacks=callbacks config
) )
except Exception as e: except Exception as e:
info(f"[{name}_subgraph] 无法发送完成事件: {e}") info(f"[{name}_subgraph] 无法发送完成事件: {e}")
@@ -113,20 +106,16 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
state.success = False state.success = False
# 发送子图错误事件 # 发送子图错误事件
if config:
try: try:
from langchain_core.callbacks.manager import adispatch_custom_event await dispatch_custom_event(
callbacks = config.get("callbacks")
if callbacks:
await adispatch_custom_event(
"react_reasoning", "react_reasoning",
{ make_react_event(
"step": state.reasoning_step, state.reasoning_step,
"action": f"{name}_subgraph_error", f"{name}_subgraph_error",
"confidence": 1.0, 1.0,
"reasoning": f"{name} 子图执行失败: {str(e)}" f"{name} 子图执行失败: {str(e)}"
}, ),
callbacks=callbacks config
) )
except Exception as e: except Exception as e:
info(f"[{name}_subgraph] 无法发送错误事件: {e}") info(f"[{name}_subgraph] 无法发送错误事件: {e}")