Class: LlmCassette::Cassette

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cassette/cassette.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, record: nil) ⇒ Cassette

Returns a new instance of Cassette.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/llm_cassette/cassette.rb', line 11

def initialize(name, record: nil)
  @name = name.to_s
  @record_mode = (record || LlmCassette.configuration.record).to_sym
  @interactions = []
  @replay_index = 0

  return if record?
  unless file_exists?
    raise CassetteNotFoundError, "Cassette '#{name}' not found at #{path}. " \
                                 "Re-run with record: :all to record it."
  end
  load!
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/llm_cassette/cassette.rb', line 9

def name
  @name
end

Instance Method Details

#eject!Object



46
47
48
# File 'lib/llm_cassette/cassette.rb', line 46

def eject!
  save! if record?
end

#next_interactionObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/llm_cassette/cassette.rb', line 29

def next_interaction
  interaction = @interactions[@replay_index]
  @replay_index += 1

  unless interaction
    raise NoMoreInteractionsError,
          "No more interactions in cassette '#{name}'. " \
          "Expected interaction ##{@replay_index} but cassette has #{@interactions.size}."
  end

  interaction
end

#record?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/llm_cassette/cassette.rb', line 25

def record?
  @record_mode == :all
end

#record_interaction(interaction) ⇒ Object



42
43
44
# File 'lib/llm_cassette/cassette.rb', line 42

def record_interaction(interaction)
  @interactions << interaction
end

#sizeObject



50
51
52
# File 'lib/llm_cassette/cassette.rb', line 50

def size
  @interactions.size
end