adamdottv 9 месяцев назад
Родитель
Сommit
bab17d7520

+ 3 - 0
internal/app/app.go

@@ -42,6 +42,9 @@ func New(ctx context.Context, conn *sql.DB) (*App, error) {
 	messages := message.NewService(q)
 	files := history.NewService(q, conn)
 
+	// Initialize session manager
+	session.InitManager(sessions)
+
 	app := &App{
 		Sessions:    sessions,
 		Messages:    messages,

+ 88 - 0
internal/session/manager.go

@@ -0,0 +1,88 @@
+package session
+
+import (
+	"context"
+	"sync"
+
+	"github.com/opencode-ai/opencode/internal/logging"
+	"github.com/opencode-ai/opencode/internal/pubsub"
+)
+
+// Manager handles session management, tracking the currently active session.
+type Manager struct {
+	currentSessionID string
+	service          Service
+	mu               sync.RWMutex
+}
+
+// Global instance of the session manager
+var globalManager *Manager
+
+// InitManager initializes the global session manager with the provided service.
+func InitManager(service Service) {
+	globalManager = &Manager{
+		currentSessionID: "",
+		service:          service,
+	}
+
+	// Subscribe to session events to handle session deletions
+	go func() {
+		ctx := context.Background()
+		eventCh := service.Subscribe(ctx)
+		for event := range eventCh {
+			if event.Type == pubsub.DeletedEvent && event.Payload.ID == CurrentSessionID() {
+				// If the current session is deleted, clear the current session
+				SetCurrentSession("")
+			}
+		}
+	}()
+}
+
+// SetCurrentSession changes the active session to the one with the specified ID.
+func SetCurrentSession(sessionID string) {
+	if globalManager == nil {
+		logging.Warn("Session manager not initialized")
+		return
+	}
+
+	globalManager.mu.Lock()
+	defer globalManager.mu.Unlock()
+
+	globalManager.currentSessionID = sessionID
+	logging.Debug("Current session changed", "sessionID", sessionID)
+}
+
+// CurrentSessionID returns the ID of the currently active session.
+func CurrentSessionID() string {
+	if globalManager == nil {
+		logging.Warn("Session manager not initialized")
+		return ""
+	}
+
+	globalManager.mu.RLock()
+	defer globalManager.mu.RUnlock()
+
+	return globalManager.currentSessionID
+}
+
+// CurrentSession returns the currently active session.
+// If no session is set or the session cannot be found, it returns nil.
+func CurrentSession() *Session {
+	if globalManager == nil {
+		logging.Warn("Session manager not initialized")
+		return nil
+	}
+
+	sessionID := CurrentSessionID()
+	if sessionID == "" {
+		return nil
+	}
+
+	session, err := globalManager.service.Get(context.Background(), sessionID)
+	if err != nil {
+		logging.Warn("Failed to get current session", "err", err)
+		return nil
+	}
+
+	return &session
+}

+ 4 - 1
internal/tui/components/dialog/session.go

@@ -91,8 +91,11 @@ func (s *sessionDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 			return s, nil
 		case key.Matches(msg, sessionKeys.Enter):
 			if len(s.sessions) > 0 {
+				selectedSession := s.sessions[s.selectedIdx]
+				// Update the session manager with the selected session
+				session.SetCurrentSession(selectedSession.ID)
 				return s, util.CmdHandler(SessionSelectedMsg{
-					Session: s.sessions[s.selectedIdx],
+					Session: selectedSession,
 				})
 			}
 		case key.Matches(msg, sessionKeys.Escape):

+ 6 - 3
internal/tui/page/chat.go

@@ -128,17 +128,20 @@ func (p *chatPage) clearSidebar() tea.Cmd {
 func (p *chatPage) sendMessage(text string, attachments []message.Attachment) tea.Cmd {
 	var cmds []tea.Cmd
 	if p.session.ID == "" {
-		session, err := p.app.Sessions.Create(context.Background(), "New Session")
+		newSession, err := p.app.Sessions.Create(context.Background(), "New Session")
 		if err != nil {
 			return util.ReportError(err)
 		}
 
-		p.session = session
+		p.session = newSession
+		// Update the current session in the session manager
+		session.SetCurrentSession(newSession.ID)
+		
 		cmd := p.setSidebar()
 		if cmd != nil {
 			cmds = append(cmds, cmd)
 		}
-		cmds = append(cmds, util.CmdHandler(chat.SessionSelectedMsg(session)))
+		cmds = append(cmds, util.CmdHandler(chat.SessionSelectedMsg(newSession)))
 	}
 
 	_, err := p.app.CoderAgent.Run(context.Background(), p.session.ID, text, attachments...)