Module: Legion::CLI::Chat::ContextManager

Defined in:
lib/legion/cli/chat/context_manager.rb

Overview

Manages conversation context window size through deduplication, stopword compression, and LLM-based summarization. Integrates with Legion::LLM::Compressor when available.

Constant Summary collapse

COMPACT_THRESHOLD =
40
TOKEN_ESTIMATE_RATIO =

~4 chars per token

4

Class Method Summary collapse

Class Method Details

.compact(session, strategy: :auto) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/cli/chat/context_manager.rb', line 16

def compact(session, strategy: :auto)
  messages = session.chat.messages.map(&:to_h)
  return { compacted: false, reason: 'too_few_messages' } if messages.length < 4

  case strategy
  when :auto
    auto_compact(session, messages)
  when :dedup
    dedup_only(session, messages)
  when :summarize
    summarize_compact(session, messages)
  else
    { compacted: false, reason: 'unknown_strategy' }
  end
end

.should_auto_compact?(session) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/legion/cli/chat/context_manager.rb', line 32

def should_auto_compact?(session)
  session.chat.messages.length >= COMPACT_THRESHOLD
end

.stats(session) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/cli/chat/context_manager.rb', line 36

def stats(session)
  messages = session.chat.messages.map(&:to_h)
  char_count = messages.sum { |m| m[:content].to_s.length }
  {
    message_count:    messages.length,
    estimated_tokens: char_count / TOKEN_ESTIMATE_RATIO,
    char_count:       char_count,
    by_role:          messages.group_by { |m| m[:role].to_s }.transform_values(&:size)
  }
end