fix: 修复 parseWorkflowResponse 处理包含额外文本的 JSON 输出

Coze 工作流返回的 output 字段包含:
1. 双编码的 JSON 字符串
2. JSON 对象后面跟着额外的注释/说明文本

修复方案:
- 从字符串中提取 JSON 对象部分(从 { 开始到 } 结束)
- 仅解析 JSON 部分,忽略后面的注释文本

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
panchengyong
2026-03-09 14:37:49 +08:00
parent 9f64233cae
commit 7b51a1bcf7

View File

@@ -720,18 +720,28 @@ public class ToolCommunityServiceImpl implements ToolCommunityService {
if (dataMap == null) return null; if (dataMap == null) return null;
// 处理 Coze 工作流返回的 output 字段(可能是双编码的 JSON 字符串) // 处理 Coze 工作流返回的 output 字段(可能是双编码的 JSON 字符串,可能包含额外的文本
Object outputField = dataMap.get("output"); Object outputField = dataMap.get("output");
if (outputField != null) { if (outputField != null) {
try { try {
// 第一次解析:从字符串去掉外层引号(因为 output 本身是 JSON 字符串)
String outputStr = outputField instanceof String String outputStr = outputField instanceof String
? (String) outputField ? (String) outputField
: outputField.toString(); : outputField.toString();
// 第二次解析:解析内部的 JSON 对象 // 提取 JSON 对象部分(从 { 开始到 } 结束)
int jsonStart = outputStr.indexOf('{');
int jsonEnd = outputStr.lastIndexOf('}');
if (jsonStart >= 0 && jsonEnd > jsonStart) {
String jsonPart = outputStr.substring(jsonStart, jsonEnd + 1);
// 解析 JSON 对象
Map<String, Object> output = JSON.parseObject(jsonPart, Map.class);
result.put("output", output);
} else {
// 如果找不到有效的 JSON尝试直接解析
Map<String, Object> output = JSON.parseObject(outputStr, Map.class); Map<String, Object> output = JSON.parseObject(outputStr, Map.class);
result.put("output", output); result.put("output", output);
}
} catch (Exception e) { } catch (Exception e) {
log.warn("Failed to parse workflow output field: {}", e.getMessage()); log.warn("Failed to parse workflow output field: {}", e.getMessage());
result.put("output", outputField); result.put("output", outputField);