Files
clawgo/pkg/session/manager.go
2026-02-12 03:04:08 +00:00

198 lines
3.5 KiB
Go

package session
import (
"encoding/json"
"os"
"path/filepath"
"sync"
"time"
"gitea.kkkk.dev/DBT/clawgo/pkg/providers"
)
type Session struct {
Key string `json:"key"`
Messages []providers.Message `json:"messages"`
Summary string `json:"summary,omitempty"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
mu sync.RWMutex
}
type SessionManager struct {
sessions map[string]*Session
mu sync.RWMutex
storage string
}
func NewSessionManager(storage string) *SessionManager {
sm := &SessionManager{
sessions: make(map[string]*Session),
storage: storage,
}
if storage != "" {
os.MkdirAll(storage, 0755)
sm.loadSessions()
}
return sm
}
func (sm *SessionManager) GetOrCreate(key string) *Session {
sm.mu.RLock()
session, ok := sm.sessions[key]
sm.mu.RUnlock()
if ok {
return session
}
sm.mu.Lock()
defer sm.mu.Unlock()
// Re-check existence after acquiring Write lock
if session, ok = sm.sessions[key]; ok {
return session
}
session = &Session{
Key: key,
Messages: []providers.Message{},
Created: time.Now(),
Updated: time.Now(),
}
sm.sessions[key] = session
return session
}
func (sm *SessionManager) AddMessage(sessionKey, role, content string) {
session := sm.GetOrCreate(sessionKey)
session.mu.Lock()
defer session.mu.Unlock()
session.Messages = append(session.Messages, providers.Message{
Role: role,
Content: content,
})
session.Updated = time.Now()
}
func (sm *SessionManager) GetHistory(key string) []providers.Message {
sm.mu.RLock()
session, ok := sm.sessions[key]
sm.mu.RUnlock()
if !ok {
return []providers.Message{}
}
session.mu.RLock()
defer session.mu.RUnlock()
history := make([]providers.Message, len(session.Messages))
copy(history, session.Messages)
return history
}
func (sm *SessionManager) GetSummary(key string) string {
sm.mu.RLock()
session, ok := sm.sessions[key]
sm.mu.RUnlock()
if !ok {
return ""
}
session.mu.RLock()
defer session.mu.RUnlock()
return session.Summary
}
func (sm *SessionManager) SetSummary(key string, summary string) {
sm.mu.RLock()
session, ok := sm.sessions[key]
sm.mu.RUnlock()
if ok {
session.mu.Lock()
defer session.mu.Unlock()
session.Summary = summary
session.Updated = time.Now()
}
}
func (sm *SessionManager) TruncateHistory(key string, keepLast int) {
sm.mu.RLock()
session, ok := sm.sessions[key]
sm.mu.RUnlock()
if !ok {
return
}
session.mu.Lock()
defer session.mu.Unlock()
if len(session.Messages) <= keepLast {
return
}
session.Messages = session.Messages[len(session.Messages)-keepLast:]
session.Updated = time.Now()
}
func (sm *SessionManager) Save(session *Session) error {
if sm.storage == "" {
return nil
}
session.mu.RLock()
defer session.mu.RUnlock()
sessionPath := filepath.Join(sm.storage, session.Key+".json")
data, err := json.MarshalIndent(session, "", " ")
if err != nil {
return err
}
return os.WriteFile(sessionPath, data, 0644)
}
func (sm *SessionManager) loadSessions() error {
files, err := os.ReadDir(sm.storage)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
continue
}
if filepath.Ext(file.Name()) != ".json" {
continue
}
sessionPath := filepath.Join(sm.storage, file.Name())
data, err := os.ReadFile(sessionPath)
if err != nil {
continue
}
var session Session
if err := json.Unmarshal(data, &session); err != nil {
continue
}
sm.sessions[session.Key] = &session
}
return nil
}