Class: Smith::Tool::CallAllowance

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/tool/call_allowance.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remaining) ⇒ CallAllowance

Returns a new instance of CallAllowance.



32
33
34
35
36
37
38
39
# File 'lib/smith/tool/call_allowance.rb', line 32

def initialize(remaining)
  unless remaining.is_a?(Integer) && remaining >= 0
    raise ArgumentError, "tool call allowance must be a non-negative integer"
  end

  @remaining = remaining
  @mutex = Mutex.new
end

Class Method Details

.charge_legacy!(allowance) ⇒ Object



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

def self.charge_legacy!(allowance)
  legacy_allowance_mutex(allowance).synchronize do
    Thread.handle_interrupt(Object => :never) do
      remaining = allowance[:remaining]
      unless remaining.is_a?(Integer) && remaining.positive?
        raise BudgetExceeded, "agent tool_calls budget exceeded"
      end

      yield if block_given?
      allowance[:remaining] = remaining - 1
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



56
57
58
# File 'lib/smith/tool/call_allowance.rb', line 56

def [](key)
  remaining if key == :remaining
end

#charge!Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/smith/tool/call_allowance.rb', line 41

def charge!
  @mutex.synchronize do
    Thread.handle_interrupt(Object => :never) do
      raise BudgetExceeded, "agent tool_calls budget exceeded" unless @remaining.positive?

      yield if block_given?
      @remaining -= 1
    end
  end
end

#remainingObject



52
53
54
# File 'lib/smith/tool/call_allowance.rb', line 52

def remaining
  @mutex.synchronize { @remaining }
end