Class: RubynCode::Tools::OutputCompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/tools/output_compressor.rb

Overview

Compresses tool output before it enters the conversation context. Each tool type has a strategy and threshold — outputs below the threshold pass through unchanged; larger outputs are compressed to keep context lean.

Constant Summary collapse

CHARS_PER_TOKEN =
4
THRESHOLDS =
{
  'run_specs' => { max_tokens: 500, strategy: :spec_summary },
  'bash' => { max_tokens: 1000, strategy: :head_tail },
  'git_log' => { max_tokens: 800, strategy: :head_tail },
  'git_diff' => { max_tokens: 2000, strategy: :relevant_hunks },
  'grep' => { max_tokens: 1000, strategy: :top_matches },
  'glob' => { max_tokens: 500,  strategy: :tree },
  'git_status' => { max_tokens: 500, strategy: :head_tail },
  'read_file' => { max_tokens: 3000, strategy: :head_tail }
}.freeze
DEFAULT_THRESHOLD =
{ max_tokens: 1500, strategy: :head_tail }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOutputCompressor

Returns a new instance of OutputCompressor.



27
28
29
# File 'lib/rubyn_code/tools/output_compressor.rb', line 27

def initialize
  @stats = { calls: 0, compressed: 0, tokens_saved: 0 }
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



25
26
27
# File 'lib/rubyn_code/tools/output_compressor.rb', line 25

def stats
  @stats
end

Instance Method Details

#compress(tool_name, raw_output) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubyn_code/tools/output_compressor.rb', line 31

def compress(tool_name, raw_output)
  return raw_output if raw_output.nil? || raw_output.empty?

  @stats[:calls] += 1
  config = THRESHOLDS.fetch(tool_name.to_s, DEFAULT_THRESHOLD)
  max_chars = config[:max_tokens] * CHARS_PER_TOKEN

  return raw_output if raw_output.length <= max_chars

  compressed = apply_strategy(config[:strategy], raw_output, max_chars)
  record_savings(raw_output, compressed)
  compressed
end