Class: Kward::ToolOutputCompactor
- Inherits:
-
Object
- Object
- Kward::ToolOutputCompactor
- Defined in:
- lib/kward/tool_output_compactor.rb
Overview
Deterministically trims large tool outputs before they are appended to the model-facing transcript.
The original output is still handed to session/tool-execution persistence by ToolRegistry; this object only decides what the next model call sees. Keep it conservative: small outputs and short errors are more valuable verbatim than compacted.
Constant Summary collapse
- MIN_BYTES =
12 * 1024
- ERROR_OUTPUT_MAX_BYTES =
8 * 1024
- HEAD_LINES =
40- TAIL_LINES =
40- ERROR_CONTEXT_LINES =
2- ERROR_PATTERN =
/\b(error|fatal|failed|failure|exception|traceback|panic|segmentation fault|assertion)\b/i.freeze
- TEST_PATTERN =
/(^\s*\d+\)\s|\b(\d+\s+(tests?|examples?|runs?|assertions?|failures?|errors?|skips?)|finished in|failures?:|seed\s+\d+)\b)/i.freeze
- SEARCH_PATTERN =
/(^\#{1,6}\s+\S+|^[-*]\s+\S+|\S+:\d+:|https?:\/\/\S+)/.freeze
Instance Method Summary collapse
Instance Method Details
#compact(tool_name, content, artifact_id: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kward/tool_output_compactor.rb', line 21 def compact(tool_name, content, artifact_id: nil) text = normalize(content) return text unless text.bytesize > MIN_BYTES return text if error_output?(text) && text.bytesize <= ERROR_OUTPUT_MAX_BYTES compacted = tool_name.to_s == "run_shell_command" ? compact_shell_output(text) : compact_lines(text) return text if compacted == text return text if compacted.bytesize >= text.bytesize artifact_id = yield if artifact_id.nil? && block_given? header = compacted_header(tool_name, text, compacted, artifact_id: artifact_id) candidate = "#{header}\n\n#{compacted}" candidate.bytesize < text.bytesize ? candidate : text end |