| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package service
-
- import (
- "context"
- "sync"
-
- "git.x2erp.com/qdy/go-base/logger"
- "git.x2erp.com/qdy/go-svc-code/internal/opencode"
- )
-
- // SessionInfo 会话信息
- type SessionInfo struct {
- SessionID string
- Title string
- UserID string
- CreatedAt string
- }
-
- // SessionManager 会话管理器
- type SessionManager struct {
- mu sync.RWMutex
- userSessions map[string][]SessionInfo // userID -> sessions
- client opencode.OpenCodeClient
- }
-
- // NewSessionManager 创建新的会话管理器
- func NewSessionManager(client opencode.OpenCodeClient) *SessionManager {
- return &SessionManager{
- userSessions: make(map[string][]SessionInfo),
- client: client,
- }
- }
-
- // CreateSession 为用户创建新会话
- func (sm *SessionManager) CreateSession(ctx context.Context, userID, title string) (*SessionInfo, error) {
- // 调用 opencode 创建会话
- session, err := sm.client.CreateSession(ctx, title)
- if err != nil {
- return nil, err
- }
-
- sessionInfo := &SessionInfo{
- SessionID: session.ID,
- Title: session.Title,
- UserID: userID,
- CreatedAt: session.CreatedAt,
- }
-
- sm.mu.Lock()
- sm.userSessions[userID] = append(sm.userSessions[userID], *sessionInfo)
- sm.mu.Unlock()
-
- logger.Debug("创建会话", "userID", userID, "sessionID", session.ID)
- return sessionInfo, nil
- }
-
- // GetUserSessions 获取用户的所有会话
- func (sm *SessionManager) GetUserSessions(userID string) []SessionInfo {
- sm.mu.RLock()
- defer sm.mu.RUnlock()
-
- sessions, exists := sm.userSessions[userID]
- if !exists {
- return []SessionInfo{}
- }
-
- // 返回副本
- result := make([]SessionInfo, len(sessions))
- copy(result, sessions)
- return result
- }
-
- // GetSession 获取特定会话(检查用户权限)
- func (sm *SessionManager) GetSession(userID, sessionID string) (*SessionInfo, bool) {
- sm.mu.RLock()
- defer sm.mu.RUnlock()
-
- sessions, exists := sm.userSessions[userID]
- if !exists {
- return nil, false
- }
-
- for _, session := range sessions {
- if session.SessionID == sessionID {
- return &session, true
- }
- }
-
- return nil, false
- }
-
- // DeleteSession 删除用户会话
- func (sm *SessionManager) DeleteSession(userID, sessionID string) bool {
- sm.mu.Lock()
- defer sm.mu.Unlock()
-
- sessions, exists := sm.userSessions[userID]
- if !exists {
- return false
- }
-
- for i, session := range sessions {
- if session.SessionID == sessionID {
- // 从切片中删除
- sm.userSessions[userID] = append(sessions[:i], sessions[i+1:]...)
-
- // 如果用户没有会话了,删除用户条目
- if len(sm.userSessions[userID]) == 0 {
- delete(sm.userSessions, userID)
- }
-
- logger.Debug("删除会话", "userID", userID, "sessionID", sessionID)
- return true
- }
- }
-
- return false
- }
-
- // GetAllSessions 获取所有会话(仅用于调试)
- func (sm *SessionManager) GetAllSessions() map[string][]SessionInfo {
- sm.mu.RLock()
- defer sm.mu.RUnlock()
-
- // 返回深拷贝
- result := make(map[string][]SessionInfo)
- for userID, sessions := range sm.userSessions {
- userSessions := make([]SessionInfo, len(sessions))
- copy(userSessions, sessions)
- result[userID] = userSessions
- }
- return result
- }
|