refactor(gateway): 重构任务回调和数据结构

This commit is contained in:
WangLiZhao
2026-06-22 15:08:57 +08:00
parent 973085ffc8
commit 733f0429fe
3 changed files with 36 additions and 8 deletions
+6 -2
View File
@@ -16,8 +16,12 @@ type HistoryRound struct {
// SessionCallbackReq 会话回调请求
type SessionCallbackReq struct {
g.Meta `path:"/callback" method:"post" tags:"会话管理" summary:"会话回调"`
Messages map[string]any `json:"messages" v:"required" dc:"消息数组"`
EpicycleId int64 `json:"epicycleId" v:"required" dc:"轮次ID"`
EpicycleId int64 `json:"epicycleId"`
TaskId string `json:"task_id"`
State int `json:"state"`
ErrorMsg string `json:"error_msg"`
OssFile string `json:"oss_file"`
FileType string `json:"file_type"`
}
// SessionCallbackRes 会话回调响应
+4 -2
View File
@@ -2,6 +2,7 @@ package prompt
import (
"context"
"encoding/json"
"errors"
"fmt"
"prompts-core/common/util"
@@ -14,7 +15,6 @@ import (
"gitea.redpowerfuture.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
@@ -154,7 +154,9 @@ func Callback(ctx context.Context, req *dto.CallbackReq) error {
// 3) 解析 OSS 内容为消息
var messages map[string]any
if len(ossContent) > 0 {
messages = gjson.New(ossContent).Map()
if err := json.Unmarshal(ossContent, &messages); err != nil {
g.Log().Warningf(ctx, "[回调处理] 解析OSS内容失败 taskId=%s err=%v", req.TaskId, err)
}
}
// 4) 处理失败
+26 -4
View File
@@ -2,7 +2,9 @@ package session
import (
"context"
"encoding/json"
"fmt"
"prompts-core/service/gateway"
"gitea.redpowerfuture.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/utils"
@@ -21,11 +23,31 @@ import (
// Callback 会话回调
func Callback(ctx context.Context, req *dto.SessionCallbackReq) (*dto.SessionCallbackRes, error) {
req.Messages["role"] = "assistant"
// 1) 读取 OSS 文件内容
var (
ossContent []byte
messages map[string]any
err error
)
if req.OssFile != "" {
ossContent, err = gateway.GetFileBytesFromURL(ctx, req.OssFile)
if err != nil {
g.Log().Infof(ctx, "[会话回调] 读取OSS文件 taskId=%s,state=%v,ossFile=%v", req.TaskId, req.State, req.OssFile)
g.Log().Warningf(ctx, "[会话回调] 读取OSS失败 taskId=%s err=%v", req.TaskId, err)
}
}
// 3) 解析 OSS 内容为消息
if len(ossContent) > 0 {
if err := json.Unmarshal(ossContent, &messages); err != nil {
g.Log().Warningf(ctx, "[回调处理] 解析OSS内容失败 taskId=%s err=%v", req.TaskId, err)
}
}
messages["role"] = "assistant"
// 1) 更新 DB
_, err := dao.ComposeSession.Update(ctx, &entity.ComposeSession{
_, err = dao.ComposeSession.Update(ctx, &entity.ComposeSession{
SQLBaseDO: beans.SQLBaseDO{Id: req.EpicycleId},
ResponseContent: req.Messages,
ResponseContent: messages,
})
if err != nil {
g.Log().Errorf(ctx, "[会话回调] 更新数据库失败 epicycleId=%d err=%v", req.EpicycleId, err)
@@ -42,7 +64,7 @@ func Callback(ctx context.Context, req *dto.SessionCallbackReq) (*dto.SessionCal
// 3) entity → HistoryRound → 写入 Redis
round := entityToHistoryRound(session)
round.Assistant = req.Messages
round.Assistant = messages
if err = SaveToRedis(ctx, session.TenantId, session.SessionId, session.NodeId, round); err != nil {
return nil, fmt.Errorf("redis存储失败: %w", err)
}