Class: Botiasloop::Commands::Compact

Inherits:
Base
  • Object
show all
Defined in:
lib/botiasloop/commands/compact.rb

Overview

Compact command - compresses conversation by summarizing older messages

Constant Summary collapse

KEEP_RECENT =
5
MIN_MESSAGES =
10

Instance Method Summary collapse

Methods inherited from Base

command, description, inherited

Instance Method Details

#execute(context, _args = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/botiasloop/commands/compact.rb', line 15

def execute(context, _args = nil)
  conversation = context.conversation
  config = Config.instance

  messages = conversation.history

  if messages.length < MIN_MESSAGES
    return "Need at least #{MIN_MESSAGES} messages to compact. Current: #{messages.length}"
  end

  # Split messages: older ones to summarize, recent ones to keep
  older_messages = messages[0...-KEEP_RECENT]
  recent_messages = messages.last(KEEP_RECENT)

  # Generate summary using LLM
  summary = summarize_messages(older_messages, config)

  # Replace conversation history
  conversation.compact!(summary, recent_messages)

  compacted_count = older_messages.length
  summary_preview = (summary.length > 100) ? "#{summary[0..100]}..." : summary
  "Conversation #{conversation.uuid} compacted.\n" \
  "#{compacted_count} messages summarized, #{recent_messages.length} recent messages kept.\n" \
  "Summary: #{summary_preview}"
end