修复子图包装器:使用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 backend.app.logger import info
from ._utils import dispatch_custom_event, make_react_event
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:
# 发送子图开始事件
if config:
try:
from langchain_core.callbacks.manager import adispatch_custom_event
callbacks = config.get("callbacks")
if callbacks:
await adispatch_custom_event(
"react_reasoning",
{
"step": state.reasoning_step,
"action": f"{name}_subgraph_start",
"confidence": 1.0,
"reasoning": f"开始执行 {name} 子图..."
},
callbacks=callbacks
)
except Exception as e:
info(f"[{name}_subgraph] 无法发送开始事件: {e}")
try:
await dispatch_custom_event(
"react_reasoning",
make_react_event(
state.reasoning_step,
f"{name}_subgraph_start",
1.0,
f"开始执行 {name} 子图..."
),
config
)
except Exception as e:
info(f"[{name}_subgraph] 无法发送开始事件: {e}")
try:
# 调用子图
result = subgraph.invoke(state)
# 调用子图(异步,传 config
result = await subgraph.ainvoke(state, config=config)
# 更新主图状态
subgraph_result = None
@@ -75,23 +72,19 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
})
# 发送子图完成事件
if config:
try:
from langchain_core.callbacks.manager import adispatch_custom_event
callbacks = config.get("callbacks")
if callbacks:
await adispatch_custom_event(
"react_reasoning",
{
"step": state.reasoning_step,
"action": f"{name}_subgraph_complete",
"confidence": 1.0,
"reasoning": f"{name} 子图执行完成"
},
callbacks=callbacks
)
except Exception as e:
info(f"[{name}_subgraph] 无法发送完成事件: {e}")
try:
await dispatch_custom_event(
"react_reasoning",
make_react_event(
state.reasoning_step,
f"{name}_subgraph_complete",
1.0,
f"{name} 子图执行完成"
),
config
)
except Exception as e:
info(f"[{name}_subgraph] 无法发送完成事件: {e}")
return state
@@ -113,23 +106,19 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
state.success = False
# 发送子图错误事件
if config:
try:
from langchain_core.callbacks.manager import adispatch_custom_event
callbacks = config.get("callbacks")
if callbacks:
await adispatch_custom_event(
"react_reasoning",
{
"step": state.reasoning_step,
"action": f"{name}_subgraph_error",
"confidence": 1.0,
"reasoning": f"{name} 子图执行失败: {str(e)}"
},
callbacks=callbacks
)
except Exception as e:
info(f"[{name}_subgraph] 无法发送错误事件: {e}")
try:
await dispatch_custom_event(
"react_reasoning",
make_react_event(
state.reasoning_step,
f"{name}_subgraph_error",
1.0,
f"{name} 子图执行失败: {str(e)}"
),
config
)
except Exception as e:
info(f"[{name}_subgraph] 无法发送错误事件: {e}")
return state