Class: RubyLLM::Contract::Step::AdapterCaller

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/contract/step/adapter_caller.rb

Constant Summary collapse

ADAPTER_ERRORS =

Exceptions treated as :adapter_error (retryable when explicitly opted in). RubyLLM::Error covers provider-semantic errors (auth, bad request, rate limit, server error, context length). Faraday::Error covers transport failures that escape ruby_llm’s Faraday retry middleware after exhaustion (Faraday::TimeoutError, Faraday::ConnectionFailed). Anything else (NoMethodError, programmer ArgumentError from adapter code, etc.) propagates — those are bugs, not retry candidates.

[::RubyLLM::Error, ::Faraday::Error].freeze

Instance Method Summary collapse

Constructor Details

#initialize(adapter:, adapter_options:) ⇒ AdapterCaller

Returns a new instance of AdapterCaller.



18
19
20
21
# File 'lib/ruby_llm/contract/step/adapter_caller.rb', line 18

def initialize(adapter:, adapter_options:)
  @adapter = adapter
  @adapter_options = adapter_options
end

Instance Method Details

#call(messages) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/contract/step/adapter_caller.rb', line 23

def call(messages)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  response = @adapter.call(messages: messages, **@adapter_options)
  latency_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round
  [response, latency_ms]
rescue *ADAPTER_ERRORS => e
  result = Result.new(
    status: :adapter_error,
    raw_output: nil,
    parsed_output: nil,
    validation_errors: [e.message]
  )
  [result, 0]
end