Module: LittleGhost::Support::OutputTruncation
- Defined in:
- lib/little_ghost/support/output_truncation.rb
Overview
OutputTruncation keeps large tool results within a predictable context budget without breaking UTF-8. Its byte-to-token estimate is deliberately approximate; use a provider tokenizer when exact accounting is required.
Constant Summary collapse
- APPROX_BYTES_PER_TOKEN =
Byte estimate used when no provider tokenizer is available.
4
Class Method Summary collapse
-
.approx_bytes_for_tokens(tokens) ⇒ Object
Converts a token budget to its approximate byte budget.
-
.approx_token_count(text) ⇒ Object
Estimates tokens from the UTF-8 byte length of
text. -
.approx_tokens_from_byte_count(bytes) ⇒ Object
Converts bytes to an approximate token count, rounded up.
-
.truncate_middle_with_token_budget(text, max_tokens) ⇒ Object
Keeps text within budget or produces a middle-truncated UTF-8 string and the original approximate token count.
Class Method Details
.approx_bytes_for_tokens(tokens) ⇒ Object
Converts a token budget to its approximate byte budget.
20 21 22 |
# File 'lib/little_ghost/support/output_truncation.rb', line 20 def approx_bytes_for_tokens(tokens) Integer(tokens) * APPROX_BYTES_PER_TOKEN end |
.approx_token_count(text) ⇒ Object
Estimates tokens from the UTF-8 byte length of text.
15 16 17 |
# File 'lib/little_ghost/support/output_truncation.rb', line 15 def approx_token_count(text) approx_tokens_from_byte_count(String(text).bytesize) end |
.approx_tokens_from_byte_count(bytes) ⇒ Object
Converts bytes to an approximate token count, rounded up.
25 26 27 |
# File 'lib/little_ghost/support/output_truncation.rb', line 25 def approx_tokens_from_byte_count(bytes) (Integer(bytes) + APPROX_BYTES_PER_TOKEN - 1) / APPROX_BYTES_PER_TOKEN end |
.truncate_middle_with_token_budget(text, max_tokens) ⇒ Object
Keeps text within budget or produces a middle-truncated UTF-8 string and the original approximate token count.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/little_ghost/support/output_truncation.rb', line 31 def truncate_middle_with_token_budget(text, max_tokens) content = utf8_content(text) max_tokens = Integer(max_tokens) max_bytes = approx_bytes_for_tokens(max_tokens) return [content, nil] if max_tokens.positive? && content.bytesize <= max_bytes prefix, suffix = split_string(content, max_bytes / 2, max_bytes - (max_bytes / 2)) removed_tokens = approx_tokens_from_byte_count([content.bytesize - max_bytes, 0].max) truncated = "#{prefix}…#{removed_tokens} tokens truncated…#{suffix}" [truncated, approx_token_count(content)] end |