mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
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
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
-- name: CreateCompactionLog :one
|
||||
INSERT INTO bot_history_message_compacts (bot_id, session_id)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at;
|
||||
|
||||
-- name: CompleteCompactionLog :one
|
||||
UPDATE bot_history_message_compacts
|
||||
SET status = $2,
|
||||
summary = $3,
|
||||
message_count = $4,
|
||||
error_message = $5,
|
||||
usage = $6,
|
||||
model_id = $7,
|
||||
completed_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at;
|
||||
|
||||
-- name: GetCompactionLogByID :one
|
||||
SELECT id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at
|
||||
FROM bot_history_message_compacts
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: ListCompactionLogsByBot :many
|
||||
SELECT id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at
|
||||
FROM bot_history_message_compacts
|
||||
WHERE bot_id = $1
|
||||
AND ($2::timestamptz IS NULL OR started_at < $2)
|
||||
ORDER BY started_at DESC
|
||||
LIMIT $3;
|
||||
|
||||
-- name: ListCompactionLogsBySession :many
|
||||
SELECT id, bot_id, session_id, status, summary, message_count, error_message, usage, model_id, started_at, completed_at
|
||||
FROM bot_history_message_compacts
|
||||
WHERE session_id = $1
|
||||
ORDER BY started_at ASC;
|
||||
|
||||
-- name: DeleteCompactionLogsByBot :exec
|
||||
DELETE FROM bot_history_message_compacts WHERE bot_id = $1;
|
||||
Reference in New Issue
Block a user