Class: LLM::Compactor::Truncate

Inherits:
LLM::Compactor show all
Defined in:
lib/llm/compactor/truncate.rb

Overview

An LLM::Compactor::Truncate drops the oldest messages when the conversation grows beyond a configured size, keeping only the N most recent messages.

No LLM call is made but this strategy is purely lossy. It also fast - no network required and operates purely on memory.

Instance Attribute Summary

Attributes inherited from LLM::Compactor

#ctx

Instance Method Summary collapse

Methods inherited from LLM::Compactor

#initialize

Constructor Details

This class inherits a constructor from LLM::Compactor

Instance Method Details

#call(keep: 64) ⇒ Array<LLM::Message>?

Parameters:

  • keep (String, Integer) (defaults to: 64)

    The last (approx) n number of messages to keep. This parameter can also be a percentage: eg "80%" to keep 80% of the most recent messages.

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/llm/compactor/truncate.rb', line 20

def call(keep: 64)
  keep = parse(keep)
  if keep <= 0 || keep > messages.reject(&:system?).size
    nil
  else
    stream.on_compaction(self)
    kept = take(messages, keep)
    messages.replace([messages.select(&:system?).first, *kept].compact)
    ctx.compacted = true
    stream.on_compaction_finish(self)
    kept
  end
end