From e57997f51b18ade4aadc100501fe1996c0561c6c Mon Sep 17 00:00:00 2001 From: wolter <11@gmail> Date: Wed, 24 Sep 2025 15:39:20 +0800 Subject: [PATCH] fix:session --- internal/biz/session.go | 9 ++++++--- internal/data/impl/sys_impl.go | 6 +++--- internal/entitys/session.go | 3 ++- internal/services/base.go | 4 ++-- internal/services/session.go | 12 ++++++++++-- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/internal/biz/session.go b/internal/biz/session.go index b2deb46..9c6f951 100644 --- a/internal/biz/session.go +++ b/internal/biz/session.go @@ -2,11 +2,11 @@ package biz import ( "ai_scheduler/internal/data/constants" + errorcode "ai_scheduler/internal/data/error" "ai_scheduler/internal/data/impl" "ai_scheduler/internal/data/model" "ai_scheduler/internal/entitys" "context" - "fmt" "github.com/gofiber/fiber/v2/utils" "time" @@ -21,10 +21,11 @@ type SessionBiz struct { conf *config.Config } -func NewSessionBiz(conf *config.Config, sessionImpl *impl.SessionImpl, sysImpl *impl.SysImpl) *SessionBiz { +func NewSessionBiz(conf *config.Config, sessionImpl *impl.SessionImpl, sysImpl *impl.SysImpl, chatImpl *impl.ChatImpl) *SessionBiz { return &SessionBiz{ sessionRepo: sessionImpl, sysRepo: sysImpl, + chatRepo: chatImpl, conf: conf, } } @@ -37,7 +38,7 @@ func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRe if err != nil { return } else if !has { - err = fmt.Errorf("sys not found") + err = errorcode.SysNotFound return } @@ -72,6 +73,7 @@ func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRe Content: sysConfig.Prologue, } result.Chat = append(result.Chat, chat) + result.SessionId = session.SessionID // 开场白写入会话历史 s.chatRepo.AsyncCreate(ctx, model.AiChatHi{ @@ -81,6 +83,7 @@ func (s *SessionBiz) SessionInit(ctx context.Context, req *entitys.SessionInitRe }) } else { + result.SessionId = session.SessionID // 存在,返回会话历史 var chatList []model.AiChatHi chatList, err = s.chatRepo.FindAll( diff --git a/internal/data/impl/sys_impl.go b/internal/data/impl/sys_impl.go index 7252657..8dc89c6 100644 --- a/internal/data/impl/sys_impl.go +++ b/internal/data/impl/sys_impl.go @@ -9,13 +9,13 @@ import ( type SysImpl struct { dataTemp.DataTemp - BaseModel[model.AiSy] + BaseRepository[model.AiSy] } func NewSysImpl(db *utils.Db) *SysImpl { return &SysImpl{ - DataTemp: *dataTemp.NewDataTemp(db, new(model.AiSy)), - BaseModel: BaseModel[model.AiSy]{}, + DataTemp: *dataTemp.NewDataTemp(db, new(model.AiSy)), + BaseRepository: NewBaseModel[model.AiSy](db.Client), } } diff --git a/internal/entitys/session.go b/internal/entitys/session.go index c18a4a4..62c2f4f 100644 --- a/internal/entitys/session.go +++ b/internal/entitys/session.go @@ -6,7 +6,8 @@ type SessionInitRequest struct { } type SessionInitResponse struct { - Chat []ChatHistory `json:"chat"` + SessionId string `json:"session_id"` + Chat []ChatHistory `json:"chat"` } type SessionListRequest struct { diff --git a/internal/services/base.go b/internal/services/base.go index 804d255..56697da 100644 --- a/internal/services/base.go +++ b/internal/services/base.go @@ -23,11 +23,11 @@ func handRes(c *fiber.Ctx, _err error, rsp interface{}) error { } body := fiber.Map{ - "code": err.Code, + "code": err.Code(), "msg": err.Error(), "data": rsp, } - log.Info(c.UserContext(), c.Path(), "请求参数=", c.BodyRaw(), "响应=", body) + log.Info(c.UserContext(), c.Path(), "请求参数=", string(c.BodyRaw()), "响应=", body) return c.JSON(body) } diff --git a/internal/services/session.go b/internal/services/session.go index c2657f1..ea4f464 100644 --- a/internal/services/session.go +++ b/internal/services/session.go @@ -27,7 +27,11 @@ func (s *SessionService) SessionInit(c *fiber.Ctx) error { result, err := s.sessionBiz.SessionInit(c.Context(), req) - return handRes(c, err, result) + if err != nil { + return err + } + + return c.JSON(result) } // SessionList 获取会话列表 @@ -39,7 +43,11 @@ func (s *SessionService) SessionList(c *fiber.Ctx) error { sessionList, err := s.sessionBiz.SessionList(c.Context(), req) - return handRes(c, err, fiber.Map{ + if err != nil { + return err + } + + return c.JSON(fiber.Map{ "session_list": sessionList, }) }