Module: OllamaChat::MessageMixin

Included in:
Message
Defined in:
lib/ollama_chat/message.rb

Overview

Mixin to provide write access to attributes that are read-only in the base class and add utility methods for content cleaning.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject



20
# File 'lib/ollama_chat/message.rb', line 20

attr_writer :content

#imagesObject



28
# File 'lib/ollama_chat/message.rb', line 28

attr_writer :images

#sender_nameObject



33
34
35
# File 'lib/ollama_chat/message.rb', line 33

def sender_name
  @sender_name
end

#thinkingObject



24
# File 'lib/ollama_chat/message.rb', line 24

attr_writer :thinking

Instance Method Details

#as_json(*a) ⇒ Hash

Converts the message to a JSON-compatible hash, including the sender name if present.

Parameters:

  • a (Array)

    optional arguments for JSON conversion.

Returns:

  • (Hash)

    a hash representation of the message.



47
48
49
50
51
52
53
# File 'lib/ollama_chat/message.rb', line 47

def as_json(*a)
  if sender_name
    { sender_name: } | super
  else
    super
  end
end

#initialize(**attributes) ⇒ Object

Initializes a new message, ensuring the sender_name is set if provided.

Parameters:

  • attributes (Hash)

    a hash of attributes to initialize the message.



11
12
13
14
15
16
# File 'lib/ollama_chat/message.rb', line 11

def initialize(**attributes)
  super
  if sender_name = attributes[:sender_name]
    self.sender_name = sender_name
  end
end

#tool?Boolean

Returns true if the message is a tool message.

Returns:

  • (Boolean)

    true if the message has a present tool name, false otherwise.



39
40
41
# File 'lib/ollama_chat/message.rb', line 39

def tool?
  tool_name.present?
end