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,28 @@
|
||||
-- 0040_compaction
|
||||
-- Add context compaction support: compaction logs table, compact_id on messages, bot settings columns.
|
||||
|
||||
-- bot_history_message_compacts: stores compaction records and summaries.
|
||||
CREATE TABLE IF NOT EXISTS bot_history_message_compacts (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
session_id UUID REFERENCES bot_sessions(id) ON DELETE SET NULL,
|
||||
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'ok', 'error')),
|
||||
summary TEXT NOT NULL DEFAULT '',
|
||||
message_count INTEGER NOT NULL DEFAULT 0,
|
||||
error_message TEXT NOT NULL DEFAULT '',
|
||||
usage JSONB,
|
||||
model_id UUID REFERENCES models(id) ON DELETE SET NULL,
|
||||
started_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
completed_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_compacts_bot_session ON bot_history_message_compacts(bot_id, session_id, started_at DESC);
|
||||
|
||||
-- Add compact_id to messages to track which compaction a message belongs to.
|
||||
ALTER TABLE bot_history_messages ADD COLUMN IF NOT EXISTS compact_id UUID REFERENCES bot_history_message_compacts(id) ON DELETE SET NULL;
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_history_messages_compact ON bot_history_messages(compact_id);
|
||||
|
||||
-- Bot-level compaction settings.
|
||||
ALTER TABLE bots ADD COLUMN IF NOT EXISTS compaction_enabled BOOLEAN NOT NULL DEFAULT false;
|
||||
ALTER TABLE bots ADD COLUMN IF NOT EXISTS compaction_threshold INTEGER NOT NULL DEFAULT 100000;
|
||||
ALTER TABLE bots ADD COLUMN IF NOT EXISTS compaction_model_id UUID REFERENCES models(id) ON DELETE SET NULL;
|
||||
Reference in New Issue
Block a user