No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

session_service.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "git.x2erp.com/qdy/go-base/logger"
  6. "git.x2erp.com/qdy/go-svc-code/internal/opencode"
  7. )
  8. // SessionInfo 会话信息
  9. type SessionInfo struct {
  10. SessionID string
  11. Title string
  12. UserID string
  13. CreatedAt string
  14. }
  15. // SessionManager 会话管理器
  16. type SessionManager struct {
  17. mu sync.RWMutex
  18. userSessions map[string][]SessionInfo // userID -> sessions
  19. client opencode.OpenCodeClient
  20. }
  21. // NewSessionManager 创建新的会话管理器
  22. func NewSessionManager(client opencode.OpenCodeClient) *SessionManager {
  23. return &SessionManager{
  24. userSessions: make(map[string][]SessionInfo),
  25. client: client,
  26. }
  27. }
  28. // CreateSession 为用户创建新会话
  29. func (sm *SessionManager) CreateSession(ctx context.Context, userID, title string) (*SessionInfo, error) {
  30. // 调用 opencode 创建会话
  31. session, err := sm.client.CreateSession(ctx, title)
  32. if err != nil {
  33. return nil, err
  34. }
  35. sessionInfo := &SessionInfo{
  36. SessionID: session.ID,
  37. Title: session.Title,
  38. UserID: userID,
  39. CreatedAt: session.CreatedAt,
  40. }
  41. sm.mu.Lock()
  42. sm.userSessions[userID] = append(sm.userSessions[userID], *sessionInfo)
  43. sm.mu.Unlock()
  44. logger.Debug("创建会话", "userID", userID, "sessionID", session.ID)
  45. return sessionInfo, nil
  46. }
  47. // GetUserSessions 获取用户的所有会话
  48. func (sm *SessionManager) GetUserSessions(userID string) []SessionInfo {
  49. sm.mu.RLock()
  50. defer sm.mu.RUnlock()
  51. sessions, exists := sm.userSessions[userID]
  52. if !exists {
  53. return []SessionInfo{}
  54. }
  55. // 返回副本
  56. result := make([]SessionInfo, len(sessions))
  57. copy(result, sessions)
  58. return result
  59. }
  60. // GetSession 获取特定会话(检查用户权限)
  61. func (sm *SessionManager) GetSession(userID, sessionID string) (*SessionInfo, bool) {
  62. sm.mu.RLock()
  63. defer sm.mu.RUnlock()
  64. sessions, exists := sm.userSessions[userID]
  65. if !exists {
  66. return nil, false
  67. }
  68. for _, session := range sessions {
  69. if session.SessionID == sessionID {
  70. return &session, true
  71. }
  72. }
  73. return nil, false
  74. }
  75. // DeleteSession 删除用户会话
  76. func (sm *SessionManager) DeleteSession(userID, sessionID string) bool {
  77. sm.mu.Lock()
  78. defer sm.mu.Unlock()
  79. sessions, exists := sm.userSessions[userID]
  80. if !exists {
  81. return false
  82. }
  83. for i, session := range sessions {
  84. if session.SessionID == sessionID {
  85. // 从切片中删除
  86. sm.userSessions[userID] = append(sessions[:i], sessions[i+1:]...)
  87. // 如果用户没有会话了,删除用户条目
  88. if len(sm.userSessions[userID]) == 0 {
  89. delete(sm.userSessions, userID)
  90. }
  91. logger.Debug("删除会话", "userID", userID, "sessionID", sessionID)
  92. return true
  93. }
  94. }
  95. return false
  96. }
  97. // GetAllSessions 获取所有会话(仅用于调试)
  98. func (sm *SessionManager) GetAllSessions() map[string][]SessionInfo {
  99. sm.mu.RLock()
  100. defer sm.mu.RUnlock()
  101. // 返回深拷贝
  102. result := make(map[string][]SessionInfo)
  103. for userID, sessions := range sm.userSessions {
  104. userSessions := make([]SessionInfo, len(sessions))
  105. copy(userSessions, sessions)
  106. result[userID] = userSessions
  107. }
  108. return result
  109. }