Class: ToolDecorator
- Inherits:
-
Object
- Object
- ToolDecorator
- Defined in:
- app/decorators/tool_decorator.rb
Overview
Base class for server-side tool response decoration. Transforms raw tool results into LLM-optimized formats before they enter the event stream.
This is a separate decorator type from EventDecorator: EventDecorator formats events for clients (TUI/web), while ToolDecorator formats tool responses for the LLM. They sit at different points in the pipeline:
Tool executes → ToolDecorator transforms → event stream → EventDecorator renders
Subclasses implement #call to transform a tool’s raw result into an LLM-friendly string. Each tool can have its own ToolDecorator subclass (e.g. WebGetToolDecorator) registered in DECORATOR_MAP.
Direct Known Subclasses
Constant Summary collapse
- DECORATOR_MAP =
{ "web_get" => "WebGetToolDecorator" }.freeze
Class Method Summary collapse
-
.call(tool_name, result) ⇒ String, Hash
Factory: dispatches to the tool-specific decorator, then sanitizes the result for safe LLM consumption.
Instance Method Summary collapse
-
#call(result) ⇒ String
Subclasses override to transform the raw tool result.
Class Method Details
.call(tool_name, result) ⇒ String, Hash
Factory: dispatches to the tool-specific decorator, then sanitizes the result for safe LLM consumption.
Sanitization guarantees the final string is UTF-8 encoded, free of ANSI escape codes, and stripped of control characters that carry no meaning for an LLM. This is the single gate — no tool or decorator subclass needs to think about encoding or terminal noise.
35 36 37 38 39 40 41 42 |
# File 'app/decorators/tool_decorator.rb', line 35 def self.call(tool_name, result) return result if result.is_a?(Hash) && result.key?(:error) klass_name = DECORATOR_MAP[tool_name] result = klass_name.constantize.new.call(result) if klass_name sanitize_for_llm(result) end |
Instance Method Details
#call(result) ⇒ String
Subclasses override to transform the raw tool result.
97 98 99 |
# File 'app/decorators/tool_decorator.rb', line 97 def call(result) raise NotImplementedError, "#{self.class} must implement #call" end |