[fix] error with tools ,agent no response

This commit is contained in:
Mark
2026-01-12 17:20:39 +08:00
parent f2390412d2
commit 63f965fafe
2 changed files with 51 additions and 24 deletions

View File

@@ -300,6 +300,9 @@ class DraftRunService:
if hasattr(agent_config, 'tools') and agent_config.tools and isinstance(agent_config.tools, list):
if hasattr(agent_config, 'tools') and agent_config.tools:
for tool_config in agent_config.tools:
print("+"*50)
print(f"agent_config:{agent_config}")
print(f"tool_config:{tool_config}")
if tool_config.get("enabled", False):
# 根据工具名称查找工具实例
tool_instance = tool_service._get_tool_instance(tool_config.get("tool_id", ""),
@@ -523,8 +526,11 @@ class DraftRunService:
tool_service = ToolService(self.db)
# 从配置中获取启用的工具
if hasattr(agent_config, 'tools') and agent_config.tools and isinstance(agent_config.tools, dict):
if hasattr(agent_config, 'tools') and agent_config.tools and isinstance(agent_config.tools, list):
for tool_config in agent_config.tools:
print("+"*50)
print(f"agent_config:{agent_config}")
print(f"tool_config:{tool_config}")
if tool_config.get("enabled", False):
# 根据工具名称查找工具实例
tool_instance = tool_service._get_tool_instance(tool_config.get("tool_id", ""),
@@ -677,7 +683,7 @@ class DraftRunService:
)
except Exception as e:
logger.error("流式 Agent 调用失败", extra={"error": str(e)})
logger.error("流式 Agent 调用失败", extra={"error": str(e)}, exc_info=True)
# 发送错误事件
yield self._format_sse_event("error", {
"error": str(e),

View File

@@ -860,17 +860,27 @@ class MultiAgentOrchestrator:
# 3. 智能整合结果
merge_mode = self.config.execution_config.get("result_merge_mode", "smart")
# merge_mode = "master"
# 智能判断是否需要整合
need_merge = self._should_merge_results(results, collaboration_strategy)
if not need_merge:
# 不需要整合:用户已经看到所有内容了
logger.info("跳过整合阶段(用户已看到所有 Agent 输出)")
# 不发送额外的 message 事件,避免重复
# 输出 message 事件,将 sub_agent_message 的内容汇总输出
if results:
all_content = ""
for result in results:
msg = result.get("result", {}).get("message", "")
if msg:
all_content += msg + "\n\n"
if all_content:
yield self._format_sse_event("message", {
"content": all_content.strip()
})
elif merge_mode == "master" and len(results) > 1:
# Master Agent 整合(非流式,避免等待时间)
logger.info("开始 Master Agent 整合")
# Master Agent 流式整合
logger.info("开始 Master Agent 流式整合")
# 发送整合开始提示
yield self._format_sse_event("merge_start", {
@@ -879,20 +889,16 @@ class MultiAgentOrchestrator:
"message": "正在整合多个专家的回答..."
})
# 流式整合(更快)
# 流式整合
try:
final_response = await self._master_merge_results(
async for event in self._master_merge_results_stream(
results,
collaboration_strategy,
message
)
# 发送整合后的结果
yield self._format_sse_event("message", {
"content": final_response
})
):
yield event
except Exception as e:
logger.error(f"Master Agent 整合失败,降级到 smart 模式: {str(e)}")
logger.error(f"Master Agent 流式整合失败,降级到 smart 模式: {str(e)}")
final_response = self._smart_merge_results(results, collaboration_strategy)
yield self._format_sse_event("message", {
"content": final_response
@@ -2332,8 +2338,23 @@ class MultiAgentOrchestrator:
# 多个结果:根据策略智能整合
if strategy == "decomposition":
# 问题拆分:用户已经看到所有子问题的答案
# 返回空字符串,表示不需要额外的整合输出
# 问题拆分:所有子问题的答案合并
# 按顺序组合各个 Agent 的回答
merged_parts = []
for result in results:
if "error" in result:
continue
agent_name = result.get("agent_name", "")
sub_question = result.get("sub_question", "")
message = result.get("result", {}).get("message", "")
if message:
if sub_question:
merged_parts.append(f"**{sub_question}**\n{message}")
else:
merged_parts.append(message)
if merged_parts:
return "\n\n".join(merged_parts)
return ""
elif strategy == "sequential":
@@ -2515,14 +2536,14 @@ class MultiAgentOrchestrator:
from app.models import ModelApiKey, ModelType
# 获取 Master Agent 的模型配置
master_agent_release = self.config.master_agent_release
if not master_agent_release:
default_model_config_id = self.config.default_model_config_id
if not default_model_config_id:
logger.warning("没有配置 Master Agent使用简单整合")
return self._smart_merge_results(results, strategy)
# 获取 API Key 配置
api_key_config = self.db.query(ModelApiKey).filter(
ModelApiKey.model_config_id == master_agent_release.default_model_config_id,
ModelApiKey.model_config_id == default_model_config_id,
ModelApiKey.is_active == True
).first()
@@ -2670,8 +2691,8 @@ class MultiAgentOrchestrator:
from app.models import ModelApiKey, ModelType
# 获取 Master Agent 的模型配置
master_agent_release = self.config.master_agent_release
if not master_agent_release:
default_model_config_id = self.config.default_model_config_id
if not default_model_config_id:
logger.warning("没有配置 Master Agent使用简单整合")
final_response = self._smart_merge_results(results, strategy)
yield self._format_sse_event("message", {"content": final_response})
@@ -2679,7 +2700,7 @@ class MultiAgentOrchestrator:
# 获取 API Key 配置
api_key_config = self.db.query(ModelApiKey).filter(
ModelApiKey.model_config_id == master_agent_release.default_model_config_id,
ModelApiKey.model_config_id == default_model_config_id,
ModelApiKey.is_active == True
).first()
@@ -2779,7 +2800,7 @@ class MultiAgentOrchestrator:
if strategy == "decomposition":
# 问题拆分:每个子问题独立,用户已经看到所有答案
# 通常不需要整合(除非配置要求)
return self.config.execution_config.get("force_merge_decomposition", False)
return self.config.execution_config.get("force_merge_decomposition", True)
if strategy == "hierarchical":
# 层级协作:主 Agent 已经整合了,不需要再整合