Class: Smith::Agent::ModelReference

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/smith/agent/model_reference.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ModelReference

Returns a new instance of ModelReference.



24
25
26
27
# File 'lib/smith/agent/model_reference.rb', line 24

def initialize(attributes)
  super
  freeze
end

Class Method Details

.coerce(value, provider: nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/smith/agent/model_reference.rb', line 29

def self.coerce(value, provider: nil)
  return value if value.is_a?(self)
  return from_hash(value) if value.is_a?(Hash)
  return parse(value) if provider.nil? && value.is_a?(String) && value.include?("/")

  new(model_id: value, provider:)
end

.parse(value) ⇒ Object

Inverse of #to_s: the first slash separates provider from model, so a reference whose model id itself contains slashes round-trips ("openrouter/openai/gpt-5" -> provider :openrouter, model "openai/gpt-5"). An explicit provider: keyword keeps the string literal and is never re-split.

Raises:

  • (ArgumentError)


48
49
50
51
52
53
# File 'lib/smith/agent/model_reference.rb', line 48

def self.parse(value)
  provider, model_id = value.split("/", 2)
  raise ArgumentError, "provider segment in #{value.inspect} must not be blank" if provider.empty?

  new(model_id:, provider:)
end

Instance Method Details

#chat_optionsObject



67
68
69
# File 'lib/smith/agent/model_reference.rb', line 67

def chat_options
  { model: model_id, provider: }.compact.freeze
end

#keyObject



55
56
57
# File 'lib/smith/agent/model_reference.rb', line 55

def key
  [provider, model_id].freeze
end

#same_candidate?(other) ⇒ Boolean

True when the two references can address the same physical model: an unqualified reference delegates provider selection to RubyLLM, so it can resolve to any provider serving the same model id.

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/smith/agent/model_reference.rb', line 62

def same_candidate?(other)
  model_id == other.model_id &&
    (provider.nil? || other.provider.nil? || provider == other.provider)
end

#to_sObject



71
72
73
# File 'lib/smith/agent/model_reference.rb', line 71

def to_s
  provider ? "#{provider}/#{model_id}" : model_id
end