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



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

attr_writer :content

#group_uuidObject



44
45
46
# File 'lib/ollama_chat/message.rb', line 44

def group_uuid
  @group_uuid
end

#imagesObject



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

attr_writer :images

#sender_nameObject



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

def sender_name
  @sender_name
end

#thinkingObject



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

attr_writer :thinking

#tool_callsObject



50
51
52
# File 'lib/ollama_chat/message.rb', line 50

def tool_calls
  @tool_calls
end

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.



93
94
95
# File 'lib/ollama_chat/message.rb', line 93

def as_json(*a)
  { sender_name:, group_uuid:, tool_calls:, }.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.



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

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
20
21
22
# 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
  if tool_calls = attributes[:tool_calls]
    self.tool_calls = tool_calls
  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:



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

def initialize_group_uuid
  self.group_uuid ||= OllamaChat::UUIDV7.generate
  self
end

#token_estimate(strip_thinking: false) ⇒ OllamaChat::TokenEstimator::Estimate

Estimates the token count for this message's text content.

Parameters:

  • strip_thinking (Boolean) (defaults to: false)

    if true, exclude the thinking content from the byte count.

Returns:



75
76
77
78
79
# File 'lib/ollama_chat/message.rb', line 75

def token_estimate(strip_thinking: false)
  bytes  = content.to_s.bytesize
  bytes += thinking.to_s.bytesize unless strip_thinking
  OllamaChat::TokenEstimator.estimate(bytes)
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.



85
86
87
# File 'lib/ollama_chat/message.rb', line 85

def tool?
  tool_name.present?
end