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:



94
95
96
# File 'lib/ollama_chat/message_format.rb', line 94

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



70
71
72
73
74
# File 'lib/ollama_chat/message_format.rb', line 70

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



82
83
84
# File 'lib/ollama_chat/message_format.rb', line 82

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



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

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



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

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ollama_chat/message_format.rb', line 53

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(&block) ⇒ String?

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

Parameters:

  • block (Proc)

    a block that returns a string to be processed

Returns:

  • (String, nil)

    the annotated string if it has content, otherwise nil



118
119
120
121
122
123
124
125
126
# File 'lib/ollama_chat/message_format.rb', line 118

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

#think_annotate(&block) ⇒ String?

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

Parameters:

  • block (Proc)

    a block that returns a string to be processed

Returns:

  • (String, nil)

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



104
105
106
107
108
109
110
# File 'lib/ollama_chat/message_format.rb', line 104

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