From f52d87e3fada17dc691c81d2a6d0b3ec6f614758 Mon Sep 17 00:00:00 2001 From: WangLiZhao <1838393649@qq.com> Date: Tue, 30 Jun 2026 13:17:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(util):=20=E9=87=8D=E6=9E=84=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E6=98=A0=E5=B0=84=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=9B=9E=E8=B0=83=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 JSON 序列化反序列化替代递归填充空值方法 - 移除 fillEmptyInPlace 函数及相关递归逻辑 - 在回调日志中添加 OSS 文件路径信息 - 优化 JSON 解析错误处理语法 - 添加调试日志用于跟踪请求载荷和消息映射过程 --- common/util/mapping.go | 30 ++++++++---------------- service/prompt/prompt_compose_service.go | 8 ++++--- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/common/util/mapping.go b/common/util/mapping.go index 821e183..11be623 100644 --- a/common/util/mapping.go +++ b/common/util/mapping.go @@ -1,6 +1,7 @@ package util import ( + "encoding/json" "fmt" "strings" @@ -114,27 +115,16 @@ 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+`"`) - return map[string]any{ - "type": typ, - typ: body, - } -} - -// 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) - } - } + var result map[string]any + _ = json.Unmarshal([]byte(str), &result) + delete(result, "type") + result["type"] = typ + return result } // ======================== 系统提示词合并 ======================== diff --git a/service/prompt/prompt_compose_service.go b/service/prompt/prompt_compose_service.go index 2cc196e..6a43411 100644 --- a/service/prompt/prompt_compose_service.go +++ b/service/prompt/prompt_compose_service.go @@ -129,8 +129,7 @@ func handleBuild(ctx context.Context, req *dto.ComposeMessagesReq, chatModel, ai // Callback 回调处理 func Callback(ctx context.Context, req *dto.CallbackReq) error { - g.Log().Infof(ctx, "[开始回调处理] taskId=%s state=%d", req.TaskId, req.State) - + g.Log().Infof(ctx, "[开始回调处理] taskId=%s state=%d ossFile=%s", req.TaskId, req.State, req.OssFile) // 1) 查询任务 composeTask, err := dao.ComposeTask.Get(ctx, &entity.ComposeTask{TaskId: req.TaskId}) if err != nil { @@ -154,7 +153,7 @@ func Callback(ctx context.Context, req *dto.CallbackReq) error { // 3) 解析 OSS 内容为消息 var messages map[string]any if len(ossContent) > 0 { - if err := json.Unmarshal(ossContent, &messages); err != nil { + if err = json.Unmarshal(ossContent, &messages); err != nil { g.Log().Warningf(ctx, "[回调处理] 解析OSS内容失败 taskId=%s err=%v", req.TaskId, err) } } @@ -240,6 +239,9 @@ func handleCallbackSuccess(ctx context.Context, req *dto.CallbackReq, composeTas userPrompt := composeTask.RequestPayload["userPrompt"].(string) messages = util.MergeSystemPrompt(messages, systemPrompt, skillContent, userPrompt, model.RequestMapping) + fmt.Println("打印1", composeTask.RequestPayload) + fmt.Println("打印2", messages) + fmt.Println("打印3", model.ExtendMapping) // 5) 合并附加结构 messages = util.MergeConsult(composeTask.RequestPayload, messages, model.ExtendMapping) // 6) 注入历史