diff --git a/common/util/mapping.go b/common/util/mapping.go index d91d22a..11be623 100644 --- a/common/util/mapping.go +++ b/common/util/mapping.go @@ -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 中