Module: LLM::Tool::Utils
- Included in:
- ReadFile
- Defined in:
- lib/llm/tools/utils.rb,
lib/llm/tools/utils/process.rb
Overview
Tool utils.
Defined Under Namespace
Modules: Process
Instance Method Summary collapse
-
#truncate(content, max_bytes:) ⇒ String
Truncates a string so a tool return stays bounded.
-
#truncate!(content, max_bytes:) ⇒ [String, Boolean]
Performs the truncation and returns a tuple where the first element is the content, which can be either truncated or left intact.
Instance Method Details
#truncate(content, max_bytes:) ⇒ String
Truncates a string so a tool return stays bounded. Appends a marker when truncated so the model knows more content was available. Bounds each individual component (string field) a tool returns: a tool that returns multiple strings (eg stdout and stderr) caps each separately.
18 19 20 21 |
# File 'lib/llm/tools/utils.rb', line 18 def truncate(content, max_bytes:) body, truncated = truncate!(content, max_bytes:) truncated ? "#{body}\n...\n[truncated: more than #{max_bytes} bytes]" : body end |
#truncate!(content, max_bytes:) ⇒ [String, Boolean]
Performs the truncation and returns a tuple where the first element is the content, which can be either truncated or left intact. The second element is a boolean that indicates whether truncation took place.
The difference between truncate and this method is that truncate appends a marker to the truncated content that indicates truncation took place, while this method leaves the content bare so a caller can structure it itself (eg ReadFile) without parsing the marker back out.
41 42 43 44 45 |
# File 'lib/llm/tools/utils.rb', line 41 def truncate!(content, max_bytes:) s = content.to_s return [s, false] if s.bytesize <= max_bytes [s.byteslice(0, max_bytes), true] end |