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 and analytical brain. Each event type has a dedicated subclass that implements rendering methods for each view mode:

  • basic / verbose / debug — TUI display modes returning structured hashes

  • brain — analytical brain transcript returning plain strings (or nil to skip)

TUI 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.

Brain mode returns condensed single-line strings for the analytical brain’s event transcript. Returns nil to exclude an event from the brain’s view.

Subclasses must override #render_basic. Verbose, debug, and brain 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"
MIDDLE_TRUNCATION_MARKER =
"\n[...truncated...]\n"

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:



76
77
78
79
80
81
82
# File 'app/decorators/event_decorator.rb', line 76

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”, “brain”

Returns:

  • (Hash, String, nil)

    structured event data (basic/verbose/debug), plain string (brain), or nil to hide the event

Raises:

  • (ArgumentError)

    if the mode is not a valid view mode



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

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)


107
108
109
# File 'app/decorators/event_decorator.rb', line 107

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

#render_brainString?

Analytical brain view — condensed single-line string for the brain’s event transcript. Returns nil to exclude from the brain’s context. Subclasses override to provide event-type-specific formatting.

Returns:

  • (String, nil)

    formatted transcript line, or nil to skip



129
130
131
# File 'app/decorators/event_decorator.rb', line 129

def render_brain
  nil
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



121
122
123
# File 'app/decorators/event_decorator.rb', line 121

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



114
115
116
# File 'app/decorators/event_decorator.rb', line 114

def render_verbose
  render_basic
end