Class: ToolDecorator

Inherits:
Object
  • Object
show all
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.

Examples:

Decorating a tool result

ToolDecorator.call("web_get", {body: html, content_type: "text/html"})
#=> "[Converted: HTML → Markdown]\n\n# Page Title\n..."

Direct Known Subclasses

WebGetToolDecorator

Constant Summary collapse

DECORATOR_MAP =
{
  "web_get" => "WebGetToolDecorator"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • tool_name (String)

    registered tool name

  • result (String, Hash)

    raw tool execution result

Returns:

  • (String, Hash)

    sanitized result (String) or original error Hash



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.

Parameters:

  • result (String, Hash)

    raw tool execution result

Returns:

  • (String)

    LLM-optimized content

Raises:

  • (NotImplementedError)


97
98
99
# File 'app/decorators/tool_decorator.rb', line 97

def call(result)
  raise NotImplementedError, "#{self.class} must implement #call"
end