refactor(util): 重构模板映射逻辑并优化回调处理
- 使用 JSON 序列化反序列化替代递归填充空值方法 - 移除 fillEmptyInPlace 函数及相关递归逻辑 - 在回调日志中添加 OSS 文件路径信息 - 优化 JSON 解析错误处理语法 - 添加调试日志用于跟踪请求载荷和消息映射过程
This commit is contained in:
+10
-20
@@ -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
|
||||
}
|
||||
|
||||
// ======================== 系统提示词合并 ========================
|
||||
|
||||
@@ -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) 注入历史
|
||||
|
||||
Reference in New Issue
Block a user