- react_reason_node: 直接发送自定义推理事件 - web_search_node: 添加开始/完成/错误事件 - rag_retrieve_node: 添加开始/完成/重试/错误事件 - 子图包装器: 添加子图开始/完成/错误事件
This commit is contained in:
@@ -121,12 +121,31 @@ async def react_reason_node(state: MainGraphState, config: Optional[Dict[str, An
|
||||
|
||||
# ========== 2. 联网搜索节点 ==========
|
||||
|
||||
def web_search_node(state: MainGraphState) -> MainGraphState:
|
||||
async def web_search_node(state: MainGraphState, config: Optional[Dict[str, Any]] = None) -> MainGraphState:
|
||||
"""
|
||||
联网搜索节点:执行搜索并将结果保存到状态
|
||||
"""
|
||||
state.current_phase = "web_searching"
|
||||
|
||||
# 发送开始事件
|
||||
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": "web_search_start",
|
||||
"confidence": 1.0,
|
||||
"reasoning": "开始执行联网搜索..."
|
||||
},
|
||||
callbacks=callbacks
|
||||
)
|
||||
except Exception as e:
|
||||
info(f"[web_search_node] 无法发送开始事件: {e}")
|
||||
|
||||
# 获取搜索查询
|
||||
reasoning_result = state.debug_info.get("reasoning_result")
|
||||
search_query = reasoning_result.metadata.get("search_query", state.user_query) if reasoning_result else state.user_query
|
||||
@@ -151,6 +170,25 @@ def web_search_node(state: MainGraphState) -> MainGraphState:
|
||||
state.success = True
|
||||
print(f"[WebSearch] 搜索完成")
|
||||
|
||||
# 发送完成事件
|
||||
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": "web_search_complete",
|
||||
"confidence": 1.0,
|
||||
"reasoning": f"联网搜索完成,找到 {len(search_result) if isinstance(search_result, list) else 1} 条结果"
|
||||
},
|
||||
callbacks=callbacks
|
||||
)
|
||||
except Exception as e:
|
||||
info(f"[web_search_node] 无法发送完成事件: {e}")
|
||||
|
||||
except Exception as e:
|
||||
from app.main_graph.state import ErrorRecord, ErrorSeverity
|
||||
from datetime import datetime
|
||||
@@ -169,6 +207,25 @@ def web_search_node(state: MainGraphState) -> MainGraphState:
|
||||
state.current_error = error_record
|
||||
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": "web_search_error",
|
||||
"confidence": 1.0,
|
||||
"reasoning": f"联网搜索失败: {str(e)}"
|
||||
},
|
||||
callbacks=callbacks
|
||||
)
|
||||
except Exception as e:
|
||||
info(f"[web_search_node] 无法发送错误事件: {e}")
|
||||
|
||||
return state
|
||||
|
||||
|
||||
Reference in New Issue
Block a user