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
-
#as_json(*a) ⇒ Hash
Converts the message to a JSON-compatible hash, including the sender name and group UUID.
-
#group_time ⇒ Time
Extracts the timestamp embedded within the UUIDv7 group identifier.
-
#initialize(**attributes) ⇒ Object
Initializes a new message, ensuring the sender_name is set if provided.
-
#initialize_group_uuid ⇒ OllamaChat::Message
Ensures that the message has a
group_uuidby generating a UUIDv7 if missing. -
#tool? ⇒ Boolean
Returns true if the message is a tool message.
Instance Attribute Details
#content ⇒ Object
23 |
# File 'lib/ollama_chat/message.rb', line 23 attr_writer :content |
#group_uuid ⇒ Object
41 42 43 |
# File 'lib/ollama_chat/message.rb', line 41 def group_uuid @group_uuid end |
#images ⇒ Object
31 |
# File 'lib/ollama_chat/message.rb', line 31 attr_writer :images |
#sender_name ⇒ Object
36 37 38 |
# File 'lib/ollama_chat/message.rb', line 36 def sender_name @sender_name end |
#thinking ⇒ Object
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.
72 73 74 |
# File 'lib/ollama_chat/message.rb', line 72 def as_json(*a) { sender_name:, group_uuid: }.compact | super end |
#group_time ⇒ Time
Extracts the timestamp embedded within the UUIDv7 group identifier.
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.
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_uuid ⇒ OllamaChat::Message
Ensures that the message has a group_uuid by generating a UUIDv7 if
missing. Returns self to allow for method chaining.
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.
64 65 66 |
# File 'lib/ollama_chat/message.rb', line 64 def tool? tool_name.present? end |