Class: Geminize::Models::Conversation

Inherits:
Object
  • Object
show all
Defined in:
lib/geminize/models/conversation.rb

Overview

Represents a conversation with message history

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, title = nil, messages = nil, created_at = nil, system_instruction = nil) ⇒ Conversation

Initialize a new conversation



35
36
37
38
39
40
41
42
# File 'lib/geminize/models/conversation.rb', line 35

def initialize(id = nil, title = nil, messages = nil, created_at = nil, system_instruction = nil)
  @id = id || SecureRandom.uuid
  @title = title
  @messages = messages || []
  @created_at = created_at || Time.now
  @updated_at = @created_at
  @system_instruction = system_instruction
end

Instance Attribute Details

#created_atTime (readonly)



18
19
20
# File 'lib/geminize/models/conversation.rb', line 18

def created_at
  @created_at
end

#idString (readonly)



12
13
14
# File 'lib/geminize/models/conversation.rb', line 12

def id
  @id
end

#messagesArray<Message> (readonly)



24
25
26
# File 'lib/geminize/models/conversation.rb', line 24

def messages
  @messages
end

#system_instructionString?



27
28
29
# File 'lib/geminize/models/conversation.rb', line 27

def system_instruction
  @system_instruction
end

#titleString



15
16
17
# File 'lib/geminize/models/conversation.rb', line 15

def title
  @title
end

#updated_atTime (readonly)



21
22
23
# File 'lib/geminize/models/conversation.rb', line 21

def updated_at
  @updated_at
end

Class Method Details

.from_hash(hash) ⇒ Conversation

Create a conversation from a hash



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/geminize/models/conversation.rb', line 133

def self.from_hash(hash)
  id = hash["id"]
  title = hash["title"]
  created_at = hash["created_at"] ? Time.parse(hash["created_at"]) : Time.now
  system_instruction = hash["system_instruction"]

  messages = []
  if hash["messages"]&.is_a?(Array)
    messages = hash["messages"].map { |msg_hash| Message.from_hash(msg_hash) }
  end

  new(id, title, messages, created_at, system_instruction)
end

.from_json(json) ⇒ Conversation

Create a conversation from a JSON string



150
151
152
153
# File 'lib/geminize/models/conversation.rb', line 150

def self.from_json(json)
  hash = JSON.parse(json)
  from_hash(hash)
end

Instance Method Details

#add_message(message) ⇒ Message

Add a message to the conversation



67
68
69
70
71
# File 'lib/geminize/models/conversation.rb', line 67

def add_message(message)
  @messages << message
  @updated_at = Time.now
  message
end

#add_model_message(content) ⇒ ModelMessage

Add a model message to the conversation



57
58
59
60
61
62
# File 'lib/geminize/models/conversation.rb', line 57

def add_model_message(content)
  message = ModelMessage.new(content)
  add_message(message)
  @updated_at = Time.now
  message
end

#add_user_message(content) ⇒ UserMessage

Add a user message to the conversation



47
48
49
50
51
52
# File 'lib/geminize/models/conversation.rb', line 47

def add_user_message(content)
  message = UserMessage.new(content)
  add_message(message)
  @updated_at = Time.now
  message
end

#clearself

Clear all messages from the conversation



99
100
101
102
103
# File 'lib/geminize/models/conversation.rb', line 99

def clear
  @messages = []
  @updated_at = Time.now
  self
end

#has_messages?Boolean

Check if the conversation has any messages



87
88
89
# File 'lib/geminize/models/conversation.rb', line 87

def has_messages?
  !@messages.empty?
end

#last_messageMessage?

Get the last message in the conversation



81
82
83
# File 'lib/geminize/models/conversation.rb', line 81

def last_message
  @messages.last
end

#message_countInteger

Get the number of messages in the conversation



93
94
95
# File 'lib/geminize/models/conversation.rb', line 93

def message_count
  @messages.size
end

#messages_as_hashesArray<Hash>

Get the messages as an array of hashes for the API



75
76
77
# File 'lib/geminize/models/conversation.rb', line 75

def messages_as_hashes
  @messages.map(&:to_hash)
end

#to_hHash

Alias for to_hash for consistency with Ruby conventions



120
121
122
# File 'lib/geminize/models/conversation.rb', line 120

def to_h
  to_hash
end

#to_hashHash

Convert the conversation to a hash



107
108
109
110
111
112
113
114
115
116
# File 'lib/geminize/models/conversation.rb', line 107

def to_hash
  {
    id: @id,
    title: @title,
    created_at: @created_at.iso8601,
    updated_at: @updated_at.iso8601,
    messages: @messages.map(&:to_hash),
    system_instruction: @system_instruction
  }
end

#to_json(*args) ⇒ String

Serialize the conversation to a JSON string



126
127
128
# File 'lib/geminize/models/conversation.rb', line 126

def to_json(*args)
  to_h.to_json(*args)
end