Files
Memoh/internal/conversation/flow/resolver_compaction.go
T
Acbox 0e646625bf feat: add compaction ratio setting to control partial context compaction
Allow users to configure what percentage of older messages to compact,
keeping the most recent portion intact. Default ratio is 80%, meaning
the oldest 80% of uncompacted messages are summarized while the newest
20% remain as-is for full-fidelity context.
2026-03-29 19:14:43 +08:00

71 lines
1.9 KiB
Go

package flow
import (
"context"
"log/slog"
"github.com/memohai/memoh/internal/compaction"
"github.com/memohai/memoh/internal/conversation"
"github.com/memohai/memoh/internal/models"
"github.com/memohai/memoh/internal/providers"
)
func (r *Resolver) maybeCompact(ctx context.Context, req conversation.ChatRequest, rc resolvedContext, inputTokens int) {
if r.compactionService == nil || r.settingsService == nil {
return
}
settings, err := r.settingsService.GetBot(ctx, req.BotID)
if err != nil {
r.logger.Warn("compaction: failed to load settings", slog.Any("error", err))
return
}
if !settings.CompactionEnabled || settings.CompactionThreshold <= 0 {
return
}
if !compaction.ShouldCompact(inputTokens, settings.CompactionThreshold) {
return
}
modelID := settings.CompactionModelID
if modelID == "" {
modelID = rc.model.ID
}
ratio := settings.CompactionRatio
if ratio <= 0 || ratio > 100 {
ratio = 80
}
cfg := compaction.TriggerConfig{
BotID: req.BotID,
SessionID: req.SessionID,
Ratio: ratio,
TotalInputTokens: inputTokens,
}
model, err := r.modelsService.GetByID(ctx, modelID)
if err != nil {
r.logger.Warn("compaction: failed to resolve model", slog.Any("error", err))
return
}
cfg.ModelID = model.ModelID
provider, err := models.FetchProviderByID(ctx, r.queries, model.LlmProviderID)
if err != nil {
r.logger.Warn("compaction: failed to fetch provider", slog.Any("error", err))
return
}
authResolver := providers.NewService(nil, r.queries, "")
creds, err := authResolver.ResolveModelCredentials(ctx, provider)
if err != nil {
r.logger.Warn("compaction: failed to resolve provider credentials", slog.Any("error", err))
return
}
cfg.ClientType = provider.ClientType
cfg.APIKey = creds.APIKey
cfg.CodexAccountID = creds.CodexAccountID
cfg.BaseURL = provider.BaseUrl
r.compactionService.TriggerCompaction(ctx, cfg)
}