Module: Smith::Tool::BoundedCompletionContext

Includes:
BoundedCompletionControls, FailFastCompletion, GracefulCompletion
Defined in:
lib/smith/tool/bounded_completion_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install(chat) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/smith/tool/bounded_completion_context.rb', line 18

def self.install(chat)
  version = Gem::Version.new(RubyLLM::VERSION)
  raise Error, "bounded completion requires RubyLLM 1.16.0" unless SUPPORTED_RUBY_LLM.satisfied_by?(version)

  missing = REQUIRED_HOOKS.reject { |hook| chat.respond_to?(hook, true) }
  valid = missing.empty? && chat.respond_to?(:tool_prefs) && chat.tool_prefs.respond_to?(:replace)
  raise Error, "unsupported RubyLLM tool-loop interface for bounded completion" unless valid

  chat.extend(self) unless chat.singleton_class < self
  chat.__send__(:install_bounded_completion_context)
  chat
end

Instance Method Details

#complete(&stream) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/smith/tool/bounded_completion_context.rb', line 31

def complete(&stream)
  allowance = Tool.current_tool_call_allowance
  owner = [Thread.current, Fiber.current]
  iterative = allowance.is_a?(CallAllowance)

  bounded_completion_guard.around_completion(owner, reentrant: !iterative) do
    return super(&stream) unless iterative

    validate_reserved_params!
    validate_supported_tools! if allowance.complete_on_exhaustion?
    state = bounded_completion_guard.state_for(allowance)
    loop do
      result = complete_with_budget_policy(state) { super(&stream) }
      return result unless state.consume_continuation!
    end
  end
end