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(&block) ⇒ String?
The talk_annotate method processes a string output by a block and conditionally adds annotation.
-
#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.
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.
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.
70 71 72 73 74 |
# File 'lib/ollama_chat/message_format.rb', line 70 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.
82 83 84 |
# File 'lib/ollama_chat/message_format.rb', line 82 def (images) images.present? ? ?📸 : ?📨 end |
#role_color(message) ⇒ Integer
Returns the terminal color code associated with the message’s role.
22 23 24 25 26 27 28 29 |
# File 'lib/ollama_chat/message_format.rb', line 22 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.
40 41 42 |
# File 'lib/ollama_chat/message_format.rb', line 40 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.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ollama_chat/message_format.rb', line 53 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(&block) ⇒ String?
The talk_annotate method processes a string output by a block and conditionally adds annotation.
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.
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 |