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
- .compact(session, strategy: :auto) ⇒ Object
- .should_auto_compact?(session) ⇒ Boolean
- .stats(session) ⇒ Object
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) = session.chat..map(&:to_h) return { compacted: false, reason: 'too_few_messages' } if .length < 4 case strategy when :auto auto_compact(session, ) when :dedup dedup_only(session, ) when :summarize summarize_compact(session, ) else { compacted: false, reason: 'unknown_strategy' } end end |
.should_auto_compact?(session) ⇒ Boolean
32 33 34 |
# File 'lib/legion/cli/chat/context_manager.rb', line 32 def should_auto_compact?(session) session.chat..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) = session.chat..map(&:to_h) char_count = .sum { |m| m[:content].to_s.length } { message_count: .length, estimated_tokens: char_count / TOKEN_ESTIMATE_RATIO, char_count: char_count, by_role: .group_by { |m| m[:role].to_s }.transform_values(&:size) } end |