fix: 修复响应体解析逻辑并统一结构包装
This commit is contained in:
@@ -274,29 +274,23 @@ func ParseStructResult(raw map[string]any, responseBody string) map[string]any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
contentVal := raw[responseBody]
|
contentVal := raw[responseBody]
|
||||||
if contentVal == nil {
|
|
||||||
return map[string]any{
|
|
||||||
"total_rounds": 1,
|
|
||||||
"rounds": []map[string]any{raw},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var rounds []map[string]any
|
||||||
|
|
||||||
|
// 是字符串,尝试解析
|
||||||
contentStr := gconv.String(contentVal)
|
contentStr := gconv.String(contentVal)
|
||||||
if contentStr == "" || contentStr == "0" {
|
if contentStr == "" || contentStr == "0" {
|
||||||
|
rounds = append(rounds, map[string]any{responseBody: raw})
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"total_rounds": 1,
|
"total_rounds": 1,
|
||||||
"rounds": []map[string]any{raw},
|
"rounds": rounds,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 尝试解析为数组
|
// 尝试解析为数组
|
||||||
var arr []map[string]any
|
var arr []any
|
||||||
if err := json.Unmarshal([]byte(contentStr), &arr); err == nil && len(arr) > 0 {
|
if err := json.Unmarshal([]byte(contentStr), &arr); err == nil && len(arr) > 0 {
|
||||||
// 数组的每个元素包一层 content
|
rounds = append(rounds, map[string]any{responseBody: arr})
|
||||||
var rounds []map[string]any
|
|
||||||
for _, item := range arr {
|
|
||||||
rounds = append(rounds, map[string]any{"content": item})
|
|
||||||
}
|
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"total_rounds": len(rounds),
|
"total_rounds": len(rounds),
|
||||||
"rounds": rounds,
|
"rounds": rounds,
|
||||||
@@ -304,15 +298,20 @@ func ParseStructResult(raw map[string]any, responseBody string) map[string]any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 尝试解析为单个对象
|
// 尝试解析为单个对象
|
||||||
var parsed map[string]any
|
var parsed any
|
||||||
if err := json.Unmarshal([]byte(contentStr), &parsed); err == nil {
|
if err := json.Unmarshal([]byte(contentStr), &parsed); err == nil {
|
||||||
raw[responseBody] = parsed
|
rounds = append(rounds, map[string]any{responseBody: parsed})
|
||||||
}
|
|
||||||
|
|
||||||
// 兜底:包标准结构
|
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"total_rounds": 1,
|
"total_rounds": 1,
|
||||||
"rounds": []map[string]any{raw},
|
"rounds": rounds,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 兜底:原始字符串作为内容
|
||||||
|
rounds = append(rounds, map[string]any{responseBody: contentStr})
|
||||||
|
return map[string]any{
|
||||||
|
"total_rounds": 1,
|
||||||
|
"rounds": rounds,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user