refactor(util): 优化模板映射逻辑

This commit is contained in:
WangLiZhao
2026-06-30 15:58:12 +08:00
parent 4417870b7f
commit a0ab180b2c
+9 -23
View File
@@ -1,6 +1,7 @@
package util
import (
"encoding/json"
"fmt"
"strings"
@@ -114,33 +115,18 @@ func buildAttachment(tmpl map[string]any, url string) map[string]any {
return nil
}
body := gconv.Map(tmpl["body"])
fillEmptyInPlace(body, url)
// 深拷贝模板
b, _ := json.Marshal(tmpl)
str := string(b)
str = strings.ReplaceAll(str, `"{{url}}"`, `"`+url+`"`)
result := map[string]any{
"type": typ,
typ: body,
}
if role, ok := tmpl["role"].(string); ok && role != "" {
result["role"] = role
}
var result map[string]any
_ = json.Unmarshal([]byte(str), &result)
delete(result, "type")
result["type"] = typ
return result
}
// fillEmptyInPlace 递归填充空字符串
func fillEmptyInPlace(m map[string]any, value string) {
for k, v := range m {
switch vv := v.(type) {
case string:
if vv == "" {
m[k] = value
}
case map[string]any:
fillEmptyInPlace(vv, value)
}
}
}
// ======================== 系统提示词合并 ========================
// MergeSystemPrompt 将系统提示词和技能内容拼接到 system role 的 content 中