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



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

attr_writer :content

#group_uuidObject



41
42
43
# File 'lib/ollama_chat/message.rb', line 41

def group_uuid
  @group_uuid
end

#imagesObject



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

attr_writer :images

#sender_nameObject



36
37
38
# File 'lib/ollama_chat/message.rb', line 36

def sender_name
  @sender_name
end

#thinkingObject



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

attr_writer :thinking

Instance Method Details

#as_json(*a) ⇒ Hash

Converts the message to a JSON-compatible hash, including the sender name and group UUID.

Parameters:

  • a (Array)

    optional arguments for JSON conversion.

Returns:

  • (Hash)

    a hash representation of the message.



72
73
74
# File 'lib/ollama_chat/message.rb', line 72

def as_json(*a)
  { sender_name:, group_uuid: }.compact | super
end

#group_timeTime

Extracts the timestamp embedded within the UUIDv7 group identifier.

Returns:

  • (Time)

    The time the group was created, derived from the UUIDv7's time-ordered bits.



56
57
58
# File 'lib/ollama_chat/message.rb', line 56

def group_time
  Time.at((group_uuid.delete(?-)[0, 16].to_i(16) >> 16) / 1000.0) if group_uuid
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
17
18
19
# File 'lib/ollama_chat/message.rb', line 11

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

#initialize_group_uuidOllamaChat::Message

Ensures that the message has a group_uuid by generating a UUIDv7 if missing. Returns self to allow for method chaining.

Returns:



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

def initialize_group_uuid
  self.group_uuid ||= OllamaChat::UUIDV7.generate
  self
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.



64
65
66
# File 'lib/ollama_chat/message.rb', line 64

def tool?
  tool_name.present?
end