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

#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



24
25
26
# File 'lib/ollama_chat/message_format.rb', line 24

def message_type(images)
  images.present? ? ?📸 : ?📨
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



48
49
50
51
52
53
54
55
56
# File 'lib/ollama_chat/message_format.rb', line 48

def talk_annotate(&block)
  string = block.()
  string.to_s.size == 0 and return
  if @chat.think.on?
    "💬\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



34
35
36
37
38
39
40
# File 'lib/ollama_chat/message_format.rb', line 34

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