Class: Gemini::Threads

Inherits:
Object
  • Object
show all
Defined in:
lib/gemini/threads.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Threads

Returns a new instance of Threads.



3
4
5
6
# File 'lib/gemini/threads.rb', line 3

def initialize(client:)
  @client = client
  @threads = {}
end

Instance Method Details

#create(parameters: {}) ⇒ Object

Create a new thread



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gemini/threads.rb', line 22

def create(parameters: {})
  thread_id = SecureRandom.uuid
  created_at = Time.now.to_i
  
  @threads[thread_id] = {
    id: thread_id,
    created_at: created_at,
    metadata: parameters[:metadata] || {},
    model: parameters[:model] || "gemini-2.5-flash"
  }
  
  {
    "id" => thread_id,
    "object" => "thread",
    "created_at" => created_at,
    "metadata" => @threads[thread_id][:metadata]
  }
end

#delete(id:) ⇒ Object

Delete a thread

Raises:



59
60
61
62
63
64
# File 'lib/gemini/threads.rb', line 59

def delete(id:)
  raise Error.new("Thread not found", "thread_not_found") unless @threads[id]
  @threads.delete(id)
  
  { "id" => id, "object" => "thread.deleted", "deleted" => true }
end

#get_model(id:) ⇒ Object

Internal use: Get thread model

Raises:



67
68
69
70
71
72
# File 'lib/gemini/threads.rb', line 67

def get_model(id:)
  thread = @threads[id]
  raise Error.new("Thread not found", "thread_not_found") unless thread
  
  thread[:model]
end

#modify(id:, parameters: {}) ⇒ Object

Modify a thread

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gemini/threads.rb', line 42

def modify(id:, parameters: {})
  thread = @threads[id]
  raise Error.new("Thread not found", "thread_not_found") unless thread
  
  # Apply modifiable parameters
  thread[:metadata] = parameters[:metadata] if parameters[:metadata]
  thread[:model] = parameters[:model] if parameters[:model]
  
  {
    "id" => thread[:id],
    "object" => "thread",
    "created_at" => thread[:created_at],
    "metadata" => thread[:metadata]
  }
end

#retrieve(id:) ⇒ Object

Retrieve a thread

Raises:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gemini/threads.rb', line 9

def retrieve(id:)
  thread = @threads[id]
  raise Error.new("Thread not found", "thread_not_found") unless thread
  
  {
    "id" => thread[:id],
    "object" => "thread",
    "created_at" => thread[:created_at],
    "metadata" => thread[:metadata]
  }
end