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.
Instance Method Summary collapse
-
#chat ⇒ OllamaChat::Chat
Returns the current chat context.
-
#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.
-
#message_type(images) ⇒ String
The message_type method determines the appropriate message icon based on whether images are present.
-
#role_color(message) ⇒ Integer
Returns the terminal color code associated with the message’s role.
-
#role_template(message) ⇒ String
Returns the formatting template for the message sender’s role.
-
#sender_name_displayed(message, template: true) ⇒ String
Returns the display name for the message sender.
-
#talk_annotate(think_loud: chat.think_loud?, &block) ⇒ String?
The talk_annotate method processes a string output by a block and conditionally adds annotation.
-
#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.
Instance Method Details
#chat ⇒ OllamaChat::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.
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.
73 74 75 76 77 |
# File 'lib/ollama_chat/message_format.rb', line 73 def display_sender() color = role_color() name = sender_name_displayed() (.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.
85 86 87 |
# File 'lib/ollama_chat/message_format.rb', line 85 def (images) images.present? ? ?📸 : ?📨 end |
#role_color(message) ⇒ Integer
Returns the terminal color code associated with the message’s role.
23 24 25 26 27 28 29 30 |
# File 'lib/ollama_chat/message_format.rb', line 23 def role_color() case .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.
41 42 43 |
# File 'lib/ollama_chat/message_format.rb', line 41 def role_template() chat.config.roles[.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.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ollama_chat/message_format.rb', line 56 def sender_name_displayed(, template: true) if sender_name = .ask_and_send(:sender_name).full? if template role_template() % { sender_name: } else sender_name end else .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.
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
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 |