Class: Clacky::UI2::Components::MessageComponent

Inherits:
BaseComponent show all
Defined in:
lib/clacky/ui2/components/message_component.rb

Overview

MessageComponent renders user and assistant messages

Instance Method Summary collapse

Methods inherited from BaseComponent

#initialize, render

Constructor Details

This class inherits a constructor from Clacky::UI2::Components::BaseComponent

Instance Method Details

#render(data) ⇒ String

Render a message

Parameters:

  • data (Hash)

    Message data

    • :role [String] “user” or “assistant”

    • :content [String] Message content

    • :timestamp [Time, nil] Optional timestamp

    • :files [Array<Hash>] Optional file hashes (for user messages)

    • :prefix_newline [Boolean] Whether to add newline before message (for system messages)

Returns:

  • (String)

    Rendered message



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/clacky/ui2/components/message_component.rb', line 18

def render(data)
  role = data[:role]
  content = data[:content]
  timestamp = data[:timestamp]
  files = data[:files] || []
  prefix_newline = data.fetch(:prefix_newline, true)
  
  case role
  when "user"
    render_user_message(content, timestamp, files)
  when "assistant"
    render_assistant_message(content, timestamp)
  else
    render_system_message(content, timestamp, prefix_newline)
  end
end

#render_assistant_message(content, timestamp = nil) ⇒ String

Render assistant message

Parameters:

  • content (String)

    Message content

  • timestamp (Time, nil) (defaults to: nil)

    Optional timestamp

Returns:

  • (String)

    Rendered message



73
74
75
76
77
78
79
80
81
# File 'lib/clacky/ui2/components/message_component.rb', line 73

def render_assistant_message(content, timestamp = nil)
  return "" if content.nil? || content.empty?

  symbol = format_symbol(:assistant)
  text = format_text(content, :assistant)
  time_str = timestamp ? @pastel.dim("[#{format_timestamp(timestamp)}]") : ""

  "\n#{symbol} #{text} #{time_str}".rstrip
end

#render_user_message(content, timestamp = nil, files = []) ⇒ String

Render user message

Parameters:

  • content (String)

    Message content

  • timestamp (Time, nil) (defaults to: nil)

    Optional timestamp

  • files (Array<Hash>) (defaults to: [])

    Optional file hashes { name:, mime_type:, … }

Returns:

  • (String)

    Rendered message



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/clacky/ui2/components/message_component.rb', line 41

def render_user_message(content, timestamp = nil, files = [])
  symbol = format_symbol(:user)
  text = format_text(content, :user)
  time_str = timestamp ? @pastel.dim("[#{format_timestamp(timestamp)}]") : ""

  result = "\n#{symbol} #{text} #{time_str}".rstrip

  # Append file attachment info if present
  if files && files.any?
    files.each_with_index do |f, idx|
      filename = f[:name] || f["name"] || "file"
      result += "\n" + @pastel.dim("    [File #{idx + 1}] #{filename}")
    end
  end

  result
end