summaryrefslogtreecommitdiffhomepage
path: root/internal/session
diff options
context:
space:
mode:
authoradamdottv <[email protected]>2025-05-08 07:43:31 -0500
committeradamdottv <[email protected]>2025-05-08 07:58:37 -0500
commitbab17d75208ffca043ff85c258ec42507d955a1e (patch)
tree7a497b108ecba5a4a8a82b7ddc727fc2c240c435 /internal/session
parent051d7d7936abbb20a2d165d5a356fc6fe0199a27 (diff)
downloadopencode-bab17d75208ffca043ff85c258ec42507d955a1e.tar.gz
opencode-bab17d75208ffca043ff85c258ec42507d955a1e.zip
feat: session manager
Diffstat (limited to 'internal/session')
-rw-r--r--internal/session/manager.go88
1 files changed, 88 insertions, 0 deletions
diff --git a/internal/session/manager.go b/internal/session/manager.go
new file mode 100644
index 000000000..8df421bd8
--- /dev/null
+++ b/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
+} \ No newline at end of file