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 or passes through.

Parameters:

  • tool_name (String)

    registered tool name

  • result (String, Hash)

    raw tool execution result

Returns:

  • (String, Hash)

    decorated result (String) or original error Hash



29
30
31
32
33
34
35
36
# File 'app/decorators/tool_decorator.rb', line 29

def self.call(tool_name, result)
  return result if result.is_a?(Hash) && result.key?(:error)

  klass_name = DECORATOR_MAP[tool_name]
  return result unless klass_name

  klass_name.constantize.new.call(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)


42
43
44
# File 'app/decorators/tool_decorator.rb', line 42

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