update pay function

This commit is contained in:
2025-11-30 17:06:34 +08:00
parent 41368f82a7
commit 25c3d9d828
3 changed files with 66 additions and 7 deletions

View File

@@ -2168,10 +2168,37 @@ class MCPAgentIntegrated:
if not successful_results:
return "很抱歉,所有步骤都执行失败,无法生成分析报告。"
# 构建结果文本(精简版)
def safe_truncate_result(result, max_length=600):
"""安全截取结果,避免截断 JSON 到不完整状态"""
result_str = str(result)
if len(result_str) <= max_length:
return result_str
# 截取到 max_length
truncated = result_str[:max_length]
# 尝试找到最后一个完整的 JSON 边界(}, ] 或 ,
# 从后往前找一个安全的截断点
safe_endings = ['},', '},\n', '}\n', '],', '],\n', ']\n', '",', '",\n', '"\n']
best_pos = -1
for ending in safe_endings:
pos = truncated.rfind(ending)
if pos > best_pos:
best_pos = pos
if best_pos > max_length // 2: # 只有找到的位置超过一半时才使用
truncated = truncated[:best_pos + 1]
# 如果结果看起来像 JSON添加省略提示
if truncated.strip().startswith('{') or truncated.strip().startswith('['):
return truncated + "\n...(数据已截断)"
else:
return truncated + "..."
# 构建结果文本(精简版,安全截取)
results_text = "\n\n".join([
f"**步骤 {r.step_index + 1}: {r.tool}**\n"
f"结果: {str(r.result)[:800]}..."
f"结果: {safe_truncate_result(r.result)}"
for r in successful_results[:3] # 只取前3个避免超长
])