feat: implement dynamic context compression for efficient memory usage

- Added Summary field to Session struct
- Implemented background summarization when history > 20 messages
- Included conversation summary in system prompt for long-term context
- Added thread-safety for concurrent summarization per session
This commit is contained in:
Danieldd28
2026-02-10 01:25:46 +07:00
parent f7d6a9ca43
commit 07e624c8da
3 changed files with 120 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ import (
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"`
}
@@ -92,6 +93,45 @@ func (sm *SessionManager) GetHistory(key string) []providers.Message {
return history
}
func (sm *SessionManager) GetSummary(key string) string {
sm.mu.RLock()
defer sm.mu.RUnlock()
session, ok := sm.sessions[key]
if !ok {
return ""
}
return session.Summary
}
func (sm *SessionManager) SetSummary(key string, summary string) {
sm.mu.Lock()
defer sm.mu.Unlock()
session, ok := sm.sessions[key]
if ok {
session.Summary = summary
session.Updated = time.Now()
}
}
func (sm *SessionManager) TruncateHistory(key string, keepLast int) {
sm.mu.Lock()
defer sm.mu.Unlock()
session, ok := sm.sessions[key]
if !ok {
return
}
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