Files
Memoh/internal/compaction/prompt.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

40 lines
1.5 KiB
Go

package compaction
import (
"fmt"
"strings"
)
const systemPrompt = `You are a conversation summarizer. Given a conversation history, produce a concise summary that preserves:
- Key facts, decisions, and agreements
- User preferences and requests
- Important context needed for continuing the conversation
- Names, dates, numbers, and specific details
- Tool usage outcomes and their results
If <prior_context> is provided, it contains summaries of earlier conversation segments. Use them ONLY to understand the conversation flow and maintain continuity. Do NOT include, repeat, or rephrase any content from <prior_context> in your output.
Output ONLY the summary of the new conversation segment. No preamble, no headers.`
type messageEntry struct {
Role string
Content string
}
func buildUserPrompt(priorSummaries []string, messages []messageEntry) string {
var sb strings.Builder
if len(priorSummaries) > 0 {
sb.WriteString("<prior_context>\n")
sb.WriteString("The following are summaries of earlier parts of this conversation. They are provided ONLY as reference context to help you understand the conversation flow. Do NOT include or repeat any of this content in your output summary.\n\n")
sb.WriteString(strings.Join(priorSummaries, "\n---\n"))
sb.WriteString("\n</prior_context>\n\n")
sb.WriteString("Now summarize the following conversation segment:\n")
} else {
sb.WriteString("Summarize the following conversation:\n")
}
for _, m := range messages {
fmt.Fprintf(&sb, "%s: %s\n", m.Role, m.Content)
}
return sb.String()
}