Class: TUI::Decorators::BaseDecorator
- Inherits:
-
Object
- Object
- TUI::Decorators::BaseDecorator
- Includes:
- Formatting
- Defined in:
- lib/tui/decorators/base_decorator.rb
Overview
Client-side decorator layer for per-tool TUI rendering.
Mirrors the server’s Draper architecture but with a different specialization axis: server decorators are uniform per EVENT TYPE (tool_call, tool_result, message), while client decorators are unique per TOOL NAME (bash, read_file, web_get) — determining how each tool looks on screen.
The factory dispatches on the tool field in the structured data hash received from the server. Unknown tools fall back to generic rendering provided by this base class.
Direct Known Subclasses
BashDecorator, EditDecorator, ReadDecorator, ThinkDecorator, WebGetDecorator, WriteDecorator
Constant Summary collapse
- ICON =
wrench
"\u{1F527}"- CHECKMARK =
"\u2713"- RETURN_ARROW =
"\u21A9"- ERROR_ICON =
"\u274C"
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.for(data) ⇒ BaseDecorator
Factory returning the per-tool decorator for the given data hash.
Instance Method Summary collapse
-
#color ⇒ String
Color for tool call headers.
-
#icon ⇒ String
Icon for this tool type.
-
#initialize(data) ⇒ BaseDecorator
constructor
A new instance of BaseDecorator.
-
#render(tui) ⇒ Array<RatatuiRuby::Widgets::Line>
Renders the event, dispatching by role to the appropriate method.
-
#render_call(tui) ⇒ Array<RatatuiRuby::Widgets::Line>
Generic tool call rendering — icon, tool name, and indented input.
-
#render_response(tui) ⇒ Array<RatatuiRuby::Widgets::Line>
Generic tool response rendering — success/failure indicator and content.
-
#render_think(tui) ⇒ Array<RatatuiRuby::Widgets::Line>
Think rendering — delegated to ThinkDecorator, but base provides a fallback that renders as a generic tool call.
-
#response_color ⇒ String
Color for tool response content.
Methods included from Formatting
#format_ns_timestamp, #format_token_label
Constructor Details
#initialize(data) ⇒ BaseDecorator
Returns a new instance of BaseDecorator.
32 33 34 |
# File 'lib/tui/decorators/base_decorator.rb', line 32 def initialize(data) @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
30 31 32 |
# File 'lib/tui/decorators/base_decorator.rb', line 30 def data @data end |
Class Method Details
.for(data) ⇒ BaseDecorator
Factory returning the per-tool decorator for the given data hash.
41 42 43 44 |
# File 'lib/tui/decorators/base_decorator.rb', line 41 def self.for(data) tool = resolve_tool(data) decorator_for(tool).new(data) end |
Instance Method Details
#color ⇒ String
Color for tool call headers. Subclasses override for tool-specific colors.
113 114 115 |
# File 'lib/tui/decorators/base_decorator.rb', line 113 def color "white" end |
#icon ⇒ String
Icon for this tool type. Subclasses override with tool-specific icons.
107 108 109 |
# File 'lib/tui/decorators/base_decorator.rb', line 107 def icon ICON end |
#render(tui) ⇒ Array<RatatuiRuby::Widgets::Line>
Renders the event, dispatching by role to the appropriate method.
50 51 52 53 54 55 56 |
# File 'lib/tui/decorators/base_decorator.rb', line 50 def render(tui) case data["role"].to_s when "tool_call" then render_call(tui) when "tool_response" then render_response(tui) when "think" then render_think(tui) end end |
#render_call(tui) ⇒ Array<RatatuiRuby::Widgets::Line>
Generic tool call rendering — icon, tool name, and indented input. Subclasses override for tool-specific presentation.
63 64 65 66 67 68 69 70 71 |
# File 'lib/tui/decorators/base_decorator.rb', line 63 def render_call(tui) style = tui.style(fg: color) header = build_call_header lines = [tui.line(spans: [tui.span(content: header, style: style)])] data["input"].to_s.split("\n", -1).each do |line| lines << tui.line(spans: [tui.span(content: " #{line}", style: style)]) end lines end |
#render_response(tui) ⇒ Array<RatatuiRuby::Widgets::Line>
Generic tool response rendering — success/failure indicator and content. Subclasses override for tool-specific presentation.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/tui/decorators/base_decorator.rb', line 78 def render_response(tui) indicator = (data["success"] == false) ? ERROR_ICON : CHECKMARK tool_id = data["tool_use_id"] tokens = data["tokens"] = [] << "[#{tool_id}]" if tool_id << indicator << format_token_label(tokens, data["estimated"]) if tokens prefix = " #{RETURN_ARROW} #{.join(" ")} " content_lines = data["content"].to_s.split("\n", -1) style = tui.style(fg: response_color) lines = [tui.line(spans: [tui.span(content: "#{prefix}#{content_lines.first}", style: style)])] content_lines.drop(1).each { |line| lines << tui.line(spans: [tui.span(content: " #{line}", style: style)]) } lines end |
#render_think(tui) ⇒ Array<RatatuiRuby::Widgets::Line>
Think rendering — delegated to ThinkDecorator, but base provides a fallback that renders as a generic tool call.
101 102 103 |
# File 'lib/tui/decorators/base_decorator.rb', line 101 def render_think(tui) render_call(tui) end |
#response_color ⇒ String
Color for tool response content. Subclasses override for tool-specific colors.
119 120 121 |
# File 'lib/tui/decorators/base_decorator.rb', line 119 def response_color "white" end |