- react_reason_node: 直接发送自定义推理事件 - web_search_node: 添加开始/完成/错误事件 - rag_retrieve_node: 添加开始/完成/重试/错误事件 - 子图包装器: 添加子图开始/完成/错误事件
This commit is contained in:
@@ -55,7 +55,26 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
|
||||
|
||||
Returns: 包装后的节点函数
|
||||
"""
|
||||
def wrapped_node(state: MainGraphState) -> MainGraphState:
|
||||
async def wrapped_node(state: MainGraphState, config: Optional[Dict[str, Any]] = 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:
|
||||
# 调用子图
|
||||
result = subgraph.invoke(state)
|
||||
@@ -72,7 +91,7 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
|
||||
state.news_result = result
|
||||
subgraph_result = result.get("final_result", "")
|
||||
|
||||
# 关键:设置最终结果,这样就不需要再回到 react_reason 了
|
||||
# 关键:设置最终结果
|
||||
if subgraph_result:
|
||||
state.final_result = subgraph_result
|
||||
else:
|
||||
@@ -89,6 +108,26 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
|
||||
"reasoning": f"{name}子图执行完成",
|
||||
"timestamp": datetime.now().isoformat()
|
||||
})
|
||||
|
||||
# 发送子图完成事件
|
||||
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}")
|
||||
|
||||
return state
|
||||
|
||||
except Exception as e:
|
||||
@@ -111,6 +150,25 @@ def wrap_subgraph_for_error_handling(subgraph, name: str):
|
||||
state.current_phase = "error_handling"
|
||||
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}")
|
||||
|
||||
return state
|
||||
|
||||
return wrapped_node
|
||||
|
||||
Reference in New Issue
Block a user