Module: RubyLLM::Modes::Truncation

Defined in:
lib/ruby_llm/modes/truncation.rb

Overview

Cuts text down to a character limit before it reaches a classifier. Every backend has an input limit (Jev rejects a request over roughly 170k characters with a 400), and one oversized entry would otherwise cost the whole turn its routing.

Class Method Summary collapse

Class Method Details

.head(text, limit) ⇒ Object

The first limit characters of text, with a marker for the rest. A history entry is cut this way: its opening says what the turn was about.



15
16
17
18
19
# File 'lib/ruby_llm/modes/truncation.rb', line 15

def head(text, limit)
  return text if limit.nil? || text.size <= limit

  text[0, limit] + marker(text.size - limit)
end

.head_and_tail(text, limit) ⇒ Object

The first and the last limit / 2 characters of text, with a marker between them. The routed message is cut this way: the intent of a long paste ("make cards from this article: ..." or "... summarise the above") sits at one end or the other, never in the middle.



26
27
28
29
30
31
32
# File 'lib/ruby_llm/modes/truncation.rb', line 26

def head_and_tail(text, limit)
  return text if limit.nil? || text.size <= limit

  head_size = limit / 2
  tail_size = limit - head_size
  text[0, head_size] + marker(text.size - limit) + text[-tail_size, tail_size]
end