Class: EventDecorator

Inherits:
ApplicationDecorator show all
Defined in:
app/decorators/event_decorator.rb

Overview

Base decorator for Event records, providing multi-resolution rendering for the TUI. Each event type has a dedicated subclass that implements rendering methods for each view mode (basic, verbose, debug).

Decorators return structured hashes (not pre-formatted strings) so that the TUI can style and lay out content based on semantic role, without fragile regex parsing. The TUI receives structured data via ActionCable and formats it for display.

Subclasses must override #render_basic. Verbose and debug modes delegate to basic until subclasses provide their own implementations.

Examples:

Decorate an Event AR model

decorator = EventDecorator.for(event)
decorator.render_basic  #=> {role: :user, content: "hello"} or nil

Render for a specific view mode

decorator = EventDecorator.for(event)
decorator.render("verbose")  #=> {role: :user, content: "hello", timestamp: 1709312325000000000}

Decorate a raw payload hash (from EventBus)

decorator = EventDecorator.for(type: "user_message", content: "hello")
decorator.render_basic  #=> {role: :user, content: "hello"}

Defined Under Namespace

Classes: EventPayload

Constant Summary collapse

TOOL_ICON =
"\u{1F527}"
RETURN_ARROW =
"\u21A9"
ERROR_ICON =
"\u274C"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(event) ⇒ EventDecorator?

Factory returning the appropriate subclass decorator for the given event. Hashes are normalized via EventPayload to provide a uniform interface.

Parameters:

  • event (Event, Hash)

    an Event AR model or a raw payload hash

Returns:



69
70
71
72
73
74
75
# File 'app/decorators/event_decorator.rb', line 69

def self.for(event)
  source = wrap_source(event)
  klass_name = DECORATOR_MAP[source.event_type]
  return nil unless klass_name

  klass_name.constantize.new(source)
end

Instance Method Details

#render(mode) ⇒ Hash?

Dispatches to the render method for the given view mode.

Parameters:

  • mode (String)

    one of “basic”, “verbose”, “debug”

Returns:

  • (Hash, nil)

    structured event data, or nil to hide the event

Raises:

  • (ArgumentError)

    if the mode is not a valid view mode



89
90
91
92
93
94
# File 'app/decorators/event_decorator.rb', line 89

def render(mode)
  method = RENDER_DISPATCH[mode]
  raise ArgumentError, "Invalid view mode: #{mode.inspect}" unless method

  public_send(method)
end

#render_basicHash?

This method is abstract.

Subclasses must implement to render the event for basic view mode.

Returns structured event data, or nil to hide the event.

Returns:

  • (Hash, nil)

    structured event data, or nil to hide the event

Raises:

  • (NotImplementedError)


98
99
100
# File 'app/decorators/event_decorator.rb', line 98

def render_basic
  raise NotImplementedError, "#{self.class} must implement #render_basic"
end

#render_debugHash?

Debug view mode with token counts and system prompts. Delegates to #render_basic until subclasses provide their own implementations.

Returns:

  • (Hash, nil)

    structured event data, or nil to hide the event



112
113
114
# File 'app/decorators/event_decorator.rb', line 112

def render_debug
  render_basic
end

#render_verboseHash?

Verbose view mode with timestamps and tool details. Delegates to #render_basic until subclasses provide their own implementations.

Returns:

  • (Hash, nil)

    structured event data, or nil to hide the event



105
106
107
# File 'app/decorators/event_decorator.rb', line 105

def render_verbose
  render_basic
end