This commit is contained in:
@@ -70,7 +70,7 @@ class AIAgentService:
|
||||
raise RuntimeError("没有可用的模型")
|
||||
return self
|
||||
|
||||
async def process_message(self, message: str, thread_id: str, model: str = "zhipu", user_id: str = "default_user") -> dict:
|
||||
async def process_message(self, message: str, thread_id: str, model: str = "local", user_id: str = "default_user") -> dict:
|
||||
"""处理用户消息,返回包含回复、token统计和耗时的字典"""
|
||||
if model not in self.graphs:
|
||||
# 回退到第一个可用模型
|
||||
@@ -175,6 +175,8 @@ class AIAgentService:
|
||||
try:
|
||||
info(f"📡 开始调用 graph.astream()...")
|
||||
chunk_count = 0
|
||||
full_message_content = "" # 收集完整消息内容
|
||||
|
||||
async for chunk in graph.astream(
|
||||
input_state,
|
||||
config=config,
|
||||
@@ -184,21 +186,11 @@ class AIAgentService:
|
||||
):
|
||||
chunk_count += 1
|
||||
chunk_type = chunk["type"]
|
||||
info(f"📦 收到第 {chunk_count} 个chunk, type: {chunk_type}")
|
||||
processed_event = {}
|
||||
|
||||
if chunk_type == "messages":
|
||||
message_chunk, metadata = chunk["data"]
|
||||
node_name = metadata.get("langgraph_node", "unknown")
|
||||
info(f"📨 处理消息chunk, node: {node_name}")
|
||||
# 详细记录消息内容,看看这些 chunk 到底是什么
|
||||
if hasattr(message_chunk, "content"):
|
||||
content_preview = str(message_chunk.content)[:200]
|
||||
info(f"📄 消息内容预览 ({len(content_preview)} chars): {repr(content_preview)}")
|
||||
if hasattr(message_chunk, "type"):
|
||||
info(f"📋 消息类型: {message_chunk.type}")
|
||||
if hasattr(message_chunk, "tool_calls"):
|
||||
info(f"🔧 包含工具调用: {message_chunk.tool_calls}")
|
||||
|
||||
# 检测节点变化,发送节点开始事件
|
||||
if node_name != current_node:
|
||||
@@ -218,8 +210,6 @@ class AIAgentService:
|
||||
reasoning_token = ""
|
||||
if hasattr(message_chunk, 'additional_kwargs'):
|
||||
reasoning_token = message_chunk.additional_kwargs.get("reasoning_content", "")
|
||||
|
||||
info(f"💬 消息token: token_content='{repr(token_content[:50])}', reasoning_token='{repr(reasoning_token[:50])}', node_name='{node_name}'")
|
||||
|
||||
# 处理思考过程
|
||||
if reasoning_token:
|
||||
@@ -228,7 +218,6 @@ class AIAgentService:
|
||||
"node": node_name,
|
||||
"reasoning_token": reasoning_token
|
||||
}
|
||||
info(f"✅ 生成 reasoning_token 事件: {processed_event}")
|
||||
# 处理工具调用
|
||||
elif hasattr(message_chunk, 'tool_calls') and message_chunk.tool_calls:
|
||||
for tool_call in message_chunk.tool_calls:
|
||||
@@ -248,7 +237,7 @@ class AIAgentService:
|
||||
"args": tool_args,
|
||||
"id": tool_call_id
|
||||
}
|
||||
# 处理普通 token
|
||||
# 处理普通 token - 只收集,不打印单个 token
|
||||
elif token_content:
|
||||
processed_event = {
|
||||
"type": "llm_token",
|
||||
@@ -256,18 +245,13 @@ class AIAgentService:
|
||||
"token": token_content,
|
||||
"reasoning_token": reasoning_token
|
||||
}
|
||||
info(f"✅ 生成 llm_token 事件: {processed_event}")
|
||||
else:
|
||||
info(f"⚠️ 没有生成任何事件,token_content='{repr(token_content)}', reasoning_token='{repr(reasoning_token)}'")
|
||||
if node_name == "llm_call":
|
||||
full_message_content += token_content
|
||||
|
||||
elif chunk_type == "updates":
|
||||
info(f"🔄 处理updates chunk")
|
||||
updates_data = chunk["data"]
|
||||
serialized_data = self._serialize_value(updates_data)
|
||||
|
||||
# 关键修复:不再从 updates 中读取 latest_reasoning,避免重复
|
||||
# 因为我们现在直接通过 custom 事件发送推理结果了
|
||||
|
||||
# 检查是否有人工审核请求
|
||||
if "review_pending" in serialized_data and serialized_data["review_pending"]:
|
||||
review_id = serialized_data.get("review_id", "")
|
||||
@@ -302,18 +286,12 @@ class AIAgentService:
|
||||
}
|
||||
|
||||
elif chunk_type == "custom":
|
||||
info(f"🎯 处理custom chunk, 完整数据: {repr(chunk)}")
|
||||
custom_data = chunk["data"]
|
||||
info(f"🎯 custom_data 内容: {repr(custom_data)}")
|
||||
info(f"🎯 custom_data 类型: {type(custom_data)}")
|
||||
|
||||
# 关键修复:处理我们从 react_reason_node 发送的自定义推理事件
|
||||
# LangGraph 的 adispatch_custom_event 发送的事件格式:
|
||||
# chunk["data"] 是我们传的第二个参数(dict)
|
||||
# 处理我们从 react_reason_node 发送的自定义推理事件
|
||||
if isinstance(custom_data, dict):
|
||||
# 检查是否是我们的推理事件
|
||||
if "action" in custom_data and "reasoning" in custom_data:
|
||||
info(f"[Agent Service] 收到自定义推理事件: {custom_data}")
|
||||
yield {
|
||||
"type": "react_reasoning",
|
||||
"step": custom_data.get("step", 1),
|
||||
@@ -339,7 +317,10 @@ class AIAgentService:
|
||||
if processed_event:
|
||||
yield processed_event
|
||||
|
||||
# 完整消息集合完成后,一次性打印
|
||||
info(f"✅ graph.astream() 完成,共 {chunk_count} 个chunks")
|
||||
if full_message_content:
|
||||
info(f"📄 完整消息内容: {repr(full_message_content)}")
|
||||
|
||||
except Exception as e:
|
||||
error(f"❌ 执行 React 图时出错: {e}")
|
||||
|
||||
Reference in New Issue
Block a user