Class: SmartPrompt::SlidingWindowStrategy
- Inherits:
-
Object
- Object
- SmartPrompt::SlidingWindowStrategy
- Includes:
- ContextStrategy
- Defined in:
- lib/smart_prompt/sliding_window_strategy.rb
Overview
SlidingWindowStrategy implements a simple context selection strategy that keeps the most recent N messages (sliding window approach)
This strategy:
-
Preserves system messages regardless of window size
-
Keeps the most recent N non-system messages
-
Trims messages to fit within token limits if specified
-
Is efficient and predictable for simple conversation flows
Instance Method Summary collapse
-
#initialize(config = {}) ⇒ SlidingWindowStrategy
constructor
Initialize the sliding window strategy.
-
#select_messages(messages, max_tokens, current_message = nil) ⇒ Array<Message>
Select messages using sliding window approach.
-
#should_compress?(session) ⇒ Boolean
Determine if compression should be triggered Recommends compression when message count exceeds 2x window size.
Constructor Details
#initialize(config = {}) ⇒ SlidingWindowStrategy
Initialize the sliding window strategy
19 20 21 22 |
# File 'lib/smart_prompt/sliding_window_strategy.rb', line 19 def initialize(config = {}) @window_size = config[:window_size] || 10 @preserve_system = config[:preserve_system] != false end |
Instance Method Details
#select_messages(messages, max_tokens, current_message = nil) ⇒ Array<Message>
Select messages using sliding window approach
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/smart_prompt/sliding_window_strategy.rb', line 29 def (, max_tokens, = nil) return [] if .nil? || .empty? # Separate system and non-system messages = @preserve_system ? .select(&:system_message?) : [] = .reject(&:system_message?) # Get the most recent messages within window size = .last(@window_size) # Combine system messages (at the beginning) with recent messages selected = + # Log selection decision log_debug "SlidingWindowStrategy: selected #{selected.count}/#{.count} messages (window_size=#{@window_size}, system=#{.count}, recent=#{.count})" # Trim to token limit if specified result = max_tokens ? trim_to_token_limit(selected, max_tokens) : selected if max_tokens && result.count < selected.count tokens_before = selected.sum { |m| m.token_count || 0 } tokens_after = result.sum { |m| m.token_count || 0 } log_debug "SlidingWindowStrategy: trimmed to token limit #{max_tokens}: #{selected.count} -> #{result.count} messages, #{tokens_before} -> #{tokens_after} tokens" end result end |
#should_compress?(session) ⇒ Boolean
Determine if compression should be triggered Recommends compression when message count exceeds 2x window size
61 62 63 |
# File 'lib/smart_prompt/sliding_window_strategy.rb', line 61 def should_compress?(session) session. > @window_size * 2 end |