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:
@@ -720,18 +720,28 @@ public class ToolCommunityServiceImpl implements ToolCommunityService {
|
||||
|
||||
if (dataMap == null) return null;
|
||||
|
||||
// 处理 Coze 工作流返回的 output 字段(可能是双编码的 JSON 字符串)
|
||||
// 处理 Coze 工作流返回的 output 字段(可能是双编码的 JSON 字符串,可能包含额外的文本)
|
||||
Object outputField = dataMap.get("output");
|
||||
if (outputField != null) {
|
||||
try {
|
||||
// 第一次解析:从字符串去掉外层引号(因为 output 本身是 JSON 字符串)
|
||||
String outputStr = outputField instanceof String
|
||||
? (String) outputField
|
||||
: outputField.toString();
|
||||
|
||||
// 第二次解析:解析内部的 JSON 对象
|
||||
Map<String, Object> output = JSON.parseObject(outputStr, Map.class);
|
||||
result.put("output", output);
|
||||
// 提取 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);
|
||||
result.put("output", output);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to parse workflow output field: {}", e.getMessage());
|
||||
result.put("output", outputField);
|
||||
|
||||
Reference in New Issue
Block a user