aiChatRepo.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package redis
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fwh-toolbox-server/cache"
  6. "time"
  7. "go.uber.org/zap"
  8. )
  9. var (
  10. aiChatCtx = context.Background()
  11. aiChatSessionKey = "fwhtoolbox:aiChat:session:" // {prefix}{userId} → sessionId
  12. aiChatSessionListKey = "fwhtoolbox:aiChat:sessionList:" // {prefix}{userId} → JSON array
  13. aiChatTTL = 7 * 24 * time.Hour
  14. )
  15. // AiSessionItem 会话列表项(Redis 存储结构)
  16. type AiSessionItem struct {
  17. SessionId string `json:"sessionId"`
  18. Title string `json:"title"`
  19. CreatedAt int64 `json:"createdAt"`
  20. LastActive int64 `json:"lastActive"`
  21. }
  22. // ============ 当前活跃会话 ============
  23. // AiActiveSession 当前活跃会话信息(Redis 存储结构)
  24. type AiActiveSession struct {
  25. SessionId string `json:"sessionId"`
  26. Title string `json:"title"`
  27. CreatedAt int64 `json:"createdAt"`
  28. LastActive int64 `json:"lastActive"`
  29. }
  30. // SaveAiSession 保存用户当前活跃会话完整信息
  31. func SaveAiSession(userId string, sess AiActiveSession) error {
  32. key := aiChatSessionKey + userId
  33. data, err := json.Marshal(sess)
  34. if err != nil {
  35. return err
  36. }
  37. return cache.Set(aiChatCtx, key, string(data), aiChatTTL)
  38. }
  39. // GetAiSession 获取用户保存的活跃会话信息
  40. func GetAiSession(userId string) (*AiActiveSession, error) {
  41. key := aiChatSessionKey + userId
  42. raw, err := cache.Get(aiChatCtx, key)
  43. if err != nil {
  44. return nil, err
  45. }
  46. var sess AiActiveSession
  47. if err := json.Unmarshal([]byte(raw), &sess); err != nil {
  48. return nil, err
  49. }
  50. return &sess, nil
  51. }
  52. // DeleteAiSession 删除用户保存的活跃会话信息
  53. func DeleteAiSession(userId string) {
  54. key := aiChatSessionKey + userId
  55. cache.Delete(aiChatCtx, key)
  56. }
  57. // ============ 会话历史列表 ============
  58. // SaveAiSessionList 保存用户完整会话历史列表
  59. func SaveAiSessionList(userId string, list []AiSessionItem) error {
  60. key := aiChatSessionListKey + userId
  61. data, err := json.Marshal(list)
  62. if err != nil {
  63. return err
  64. }
  65. return cache.Set(aiChatCtx, key, string(data), aiChatTTL)
  66. }
  67. // GetAiSessionList 获取用户完整会话历史列表
  68. func GetAiSessionList(userId string) ([]AiSessionItem, error) {
  69. key := aiChatSessionListKey + userId
  70. raw, err := cache.Get(aiChatCtx, key)
  71. if err != nil {
  72. return nil, err
  73. }
  74. var list []AiSessionItem
  75. if err := json.Unmarshal([]byte(raw), &list); err != nil {
  76. return nil, err
  77. }
  78. return list, nil
  79. }
  80. // DeleteAiSessionList 删除用户会话历史列表
  81. func DeleteAiSessionList(userId string) {
  82. key := aiChatSessionListKey + userId
  83. cache.Delete(aiChatCtx, key)
  84. }
  85. // UpdateAiSessionTitle 更新列表中指定会话的标题
  86. func UpdateAiSessionTitle(userId string, sessionId string, title string) {
  87. list, err := GetAiSessionList(userId)
  88. if err != nil {
  89. return
  90. }
  91. for i := range list {
  92. if list[i].SessionId == sessionId {
  93. list[i].Title = title
  94. break
  95. }
  96. }
  97. if err := SaveAiSessionList(userId, list); err != nil {
  98. zap.L().Warn("[AIChat] 更新会话标题到Redis失败", zap.Error(err))
  99. }
  100. }