Module: OllamaChat::MessageFormat

Included in:
Chat, FollowChat, MessageList
Defined in:
lib/ollama_chat/message_format.rb

Overview

A module that provides formatting functionality for chat messages.

The MessageFormat module encapsulates methods for determining message icons based on whether images are present, and for conditionally annotating content with thinking or talk indicators. It supports customizable formatting of message text for display in terminal interfaces.

Examples:

Using message_type to determine icon based on images

message_type([])        # => "📨"
message_type(["image"]) # => "📸"

Annotating content with thinking indicator

think_annotate { "Thinking..." } # => "💭\nThinking...\n" (when think is enabled)

Annotating content with talk indicator

talk_annotate { "Speaking..." } # => "💬\nSpeaking...\n" (when think is enabled)

Instance Method Summary collapse

Instance Method Details

#chatOllamaChat::Chat

Returns the current chat context.

This method ensures that the formatting logic has access to the chat’s configuration (e.g., whether ‘think_loud’ is enabled). It returns ‘self` if the object is already a `OllamaChat::Chat` instance, otherwise it returns the `@chat` instance variable.

Returns:



97
98
99
# File 'lib/ollama_chat/message_format.rb', line 97

def chat
  self.is_a?(OllamaChat::Chat) ? self : @chat
end

#display_sender(message) ⇒ String

Formats the sender’s identity for display in the terminal, including the message icon and the sender’s name or role with appropriate coloring.

Parameters:

Returns:

  • (String)

    the formatted string representing the sender



73
74
75
76
77
# File 'lib/ollama_chat/message_format.rb', line 73

def display_sender(message)
  color = role_color(message)
  name  = sender_name_displayed(message)
  message_type(message.images) + " " + bold { color(color) { name } }
end

#message_type(images) ⇒ String

The message_type method determines the appropriate message icon based on whether images are present.

Parameters:

  • images (Array)

    an array of images

Returns:

  • (String)

    returns 📸 if images are present, 📨 otherwise



85
86
87
# File 'lib/ollama_chat/message_format.rb', line 85

def message_type(images)
  images.present? ? ?📸 : ?📨
end

#role_color(message) ⇒ Integer

Returns the terminal color code associated with the message’s role.

Parameters:

Returns:

  • (Integer)

    the color code corresponding to the role



23
24
25
26
27
28
29
30
# File 'lib/ollama_chat/message_format.rb', line 23

def role_color(message)
  case message.role
  when 'user'      then 172
  when 'assistant' then 111
  when 'system'    then 213
  else                  210
  end
end

#role_template(message) ⇒ String

Returns the formatting template for the message sender’s role.

The template is retrieved from the chat configuration based on the message’s role. If no specific template is found for the role, it falls back to the default role template.

Parameters:

Returns:

  • (String)

    the formatting template string



41
42
43
# File 'lib/ollama_chat/message_format.rb', line 41

def role_template(message)
  chat.config.roles[message.role] || chat.config.roles.default
end

#sender_name_displayed(message, template: true) ⇒ String

Returns the display name for the message sender.

If a full sender name is available, it returns either the formatted template or the raw name based on the ‘template` parameter. Otherwise, it returns the message role.

Parameters:

  • message (OllamaChat::Message)

    the message object

  • template (Boolean) (defaults to: true)

    whether to apply the role formatting template (default: true)

Returns:

  • (String)

    the formatted sender name, the raw name, or the role



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ollama_chat/message_format.rb', line 56

def sender_name_displayed(message, template: true)
  if sender_name = message.ask_and_send(:sender_name).full?
    if template
      role_template(message) % { sender_name: }
    else
      sender_name
    end
  else
    message.role
  end
end

#talk_annotate(think_loud: chat.think_loud?, &block) ⇒ String?

The talk_annotate method processes a string output by a block and conditionally adds annotation.

Parameters:

  • think_loud (Boolean) (defaults to: chat.think_loud?)

    whether to annotate the content (defaults to current chat setting)

  • block (Proc)

    a block that returns a string to be processed

Returns:

  • (String, nil)

    the annotated string if it has content, otherwise nil



126
127
128
129
130
131
132
133
134
# File 'lib/ollama_chat/message_format.rb', line 126

def talk_annotate(think_loud: chat.think_loud?, &block)
  string = block.()
  string.to_s.size == 0 and return
  if think_loud
    "💬\n#{string}\n"
  else
    string
  end
end

#think_annotate(think_loud: chat.think_loud?, &block) ⇒ String?

The think_annotate method processes a string and conditionally annotates it with a thinking emoji if the think feature is enabled.

to be processed

Parameters:

  • think_loud (Boolean) (defaults to: chat.think_loud?)

    whether to annotate the content (defaults to current chat setting) @param block [ Proc ] a block that returns a string

Returns:

  • (String, nil)

    the annotated string with a thinking emoji if enabled, otherwise nil



110
111
112
113
114
115
116
# File 'lib/ollama_chat/message_format.rb', line 110

def think_annotate(think_loud: chat.think_loud?, &block)
  string = block.()
  string.to_s.size == 0 and return
  if think_loud
    "💭\n#{string}\n"
  end
end