Class: MessageDecorator

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

Overview

Base decorator for Message records, providing multi-resolution rendering for the TUI and analytical brain. Each message 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 message transcript. Returns nil to exclude a message 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 a Message AR model

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

Render for a specific view mode

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

Decorate a raw payload hash (from EventBus)

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

Defined Under Namespace

Classes: MessagePayload

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(message) ⇒ MessageDecorator?

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

Parameters:

  • message (Message, Hash)

    a Message AR model or a raw payload hash

Returns:



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

def self.for(message)
  source = wrap_source(message)
  klass_name = DECORATOR_MAP[source.message_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 message data (basic/verbose/debug), plain string (brain), or nil to hide the message

Raises:

  • (ArgumentError)

    if the mode is not a valid view mode



98
99
100
101
102
103
# File 'app/decorators/message_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 message for basic view mode.

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

Returns:

  • (Hash, nil)

    structured message data, or nil to hide the message

Raises:

  • (NotImplementedError)


107
108
109
# File 'app/decorators/message_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 message transcript. Returns nil to exclude from the brain’s context. Subclasses override to provide message-type-specific formatting.

Returns:

  • (String, nil)

    formatted transcript line, or nil to skip



129
130
131
# File 'app/decorators/message_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 message data, or nil to hide the message



121
122
123
# File 'app/decorators/message_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 message data, or nil to hide the message



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

def render_verbose
  render_basic
end