Files
Memoh/internal/conversation/flow/resolver_compaction.go
T
Acbox Liu de62f94315 feat: add context compaction to automatically summarize old messages (#compaction) (#276)
When input tokens exceed a configurable threshold after a conversation round,
the system asynchronously compacts older messages into a summary. Cascading
compactions reference prior summaries via <prior_context> tags to maintain
conversational continuity without duplicating content.

- Add bot_history_message_compacts table and compact_id on messages
- Add compaction_enabled, compaction_threshold, compaction_model_id to bots
- Implement compaction service (internal/compaction) with LLM summarization
- Integrate into conversation flow: replace compacted messages with summaries
  wrapped in <summary> tags during context loading
- Add REST API endpoints (GET/DELETE /bots/:bot_id/compaction/logs)
- Add frontend Compaction tab with settings and log viewer
- Wire compaction service into both dev (cmd/agent) and prod (cmd/memoh) entry points
- Update test mocks to include new GetBotByID columns
2026-03-22 14:26:00 +08:00

56 lines
1.4 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"
)
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
}
cfg := compaction.TriggerConfig{
BotID: req.BotID,
SessionID: req.SessionID,
}
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
cfg.ClientType = string(model.ClientType)
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
}
cfg.APIKey = provider.ApiKey
cfg.BaseURL = provider.BaseUrl
r.compactionService.TriggerCompaction(ctx, cfg)
}