From c5e3450c79b12427e8d81325a0dfe2b94d8fee18 Mon Sep 17 00:00:00 2001 From: WangLiZhao <1838393649@qq.com> Date: Mon, 6 Jul 2026 15:12:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(gateway):=20=E4=BF=AE=E6=94=B9=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E5=8E=86=E5=8F=B2=E6=8E=A5=E5=8F=A3=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=8A=A0=E7=A7=9F=E6=88=B7=E9=9A=94=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/prompt_session_controller.go | 5 +++ model/dto/prompt_session_dto.go | 16 ++++++++ service/session/prompt_session_service.go | 45 ++++++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/controller/prompt_session_controller.go b/controller/prompt_session_controller.go index 99bbb4c..d3f375b 100644 --- a/controller/prompt_session_controller.go +++ b/controller/prompt_session_controller.go @@ -25,6 +25,11 @@ func (c *session) GetHistoryList(ctx context.Context, req *dto.GetHistoryListReq return sessionService.GetHistoryList(ctx, req) } +// GetSessionHistory 获取会话历史(服务调用) +func (c *session) GetSessionHistory(ctx context.Context, req *dto.GetSessionHistoryReq) (res *dto.GetSessionHistoryRes, err error) { + return sessionService.GetSessionHistory(ctx, req) +} + // DeleteMessages 批量删除消息 func (c *session) DeleteMessages(ctx context.Context, req *dto.DeleteMessagesReq) (res *dto.DeleteMessagesRes, err error) { return sessionService.DeleteMessages(ctx, req) diff --git a/model/dto/prompt_session_dto.go b/model/dto/prompt_session_dto.go index 6b585a7..2b8d565 100644 --- a/model/dto/prompt_session_dto.go +++ b/model/dto/prompt_session_dto.go @@ -43,6 +43,22 @@ type GetHistoryListRes struct { Total int `json:"total" dc:"总数"` } +type GetSessionHistoryReq struct { + g.Meta `path:"/sessionHistory" method:"get" tags:"会话管理" summary:"获取会话历史"` + SessionId string `json:"sessionId" v:"required#sessionId不能为空" dc:"会话ID"` + NodeId string `json:"nodeId" dc:"节点ID"` + Size int `json:"size" d:"50" dc:"条数"` +} + +type GetSessionHistoryRes struct { + List []SessionHistoryItem `json:"list" dc:"历史消息列表"` +} + +type SessionHistoryItem struct { + Role string `json:"role" dc:"角色:user/assistant"` + Content string `json:"content" dc:"消息内容"` +} + // GetHistoryMessagesReq 获取历史消息请求(提示词拼接) type GetHistoryMessagesReq struct { g.Meta `path:"/historyMessages" method:"get" tags:"会话管理" summary:"获取历史消息"` diff --git a/service/session/prompt_session_service.go b/service/session/prompt_session_service.go index 8c3de77..5d1b53f 100644 --- a/service/session/prompt_session_service.go +++ b/service/session/prompt_session_service.go @@ -85,7 +85,10 @@ func GetHistoryList(ctx context.Context, req *dto.GetHistoryListReq) (*dto.GetHi return nil, err } sessions, total, err := dao.ComposeSession.List(ctx, &entity.ComposeSession{ - SQLBaseDO: beans.SQLBaseDO{Creator: user.UserName}, + SQLBaseDO: beans.SQLBaseDO{ + TenantId: user.TenantId, + Creator: user.UserName, + }, }, req.Page, req.Size) if err != nil { return nil, fmt.Errorf("DB获取历史列表失败: %w", err) @@ -94,6 +97,46 @@ func GetHistoryList(ctx context.Context, req *dto.GetHistoryListReq) (*dto.GetHi return &dto.GetHistoryListRes{List: rounds, Total: total}, nil } +// GetSessionHistory 获取会话历史 +func GetSessionHistory(ctx context.Context, req *dto.GetSessionHistoryReq) (*dto.GetSessionHistoryRes, error) { + user, err := utils.GetUserInfo(ctx) + if err != nil { + return nil, err + } + + sessions, _, err := dao.ComposeSession.List(ctx, &entity.ComposeSession{ + SQLBaseDO: beans.SQLBaseDO{ + TenantId: user.TenantId, + Creator: user.UserName, + }, + SessionId: req.SessionId, + NodeId: req.NodeId, + }, 1, req.Size) + if err != nil { + return nil, fmt.Errorf("DB获取历史失败: %w", err) + } + + var items []dto.SessionHistoryItem + for _, s := range sessions { + // 提取 user 消息 + if userMsg := util.ExtractUserText(s.RequestContent); userMsg != nil { + items = append(items, dto.SessionHistoryItem{ + Role: "user", + Content: gconv.String(userMsg["content"]), + }) + } + // 提取 assistant 消息 + if s.ResponseContent != nil { + items = append(items, dto.SessionHistoryItem{ + Role: "assistant", + Content: gconv.String(s.ResponseContent["content"]), + }) + } + } + + return &dto.GetSessionHistoryRes{List: items}, nil +} + // ============================================ // 场景2:提示词拼接(按 sessionId + nodeId) // ============================================