Class: AgentHarness::ProviderRuntime

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_harness/provider_runtime.rb

Overview

Normalized runtime configuration for per-request provider overrides.

ProviderRuntime lets callers pass a single, provider-agnostic payload into send_message that each provider materializes into CLI args, env vars, or config files as needed.

Examples:

Routing OpenCode through OpenRouter with a specific model

runtime = AgentHarness::ProviderRuntime.new(
  model: "anthropic/claude-opus-4.1",
  base_url: "https://openrouter.ai/api/v1",
  api_provider: "openrouter",
  env: { "OPENROUTER_API_KEY" => "sk-..." }
)
provider.send_message(prompt: "Hello", provider_runtime: runtime)

Passing a Hash (auto-coerced by Base#send_message)

provider.send_message(
  prompt: "Hello",
  provider_runtime: {
    model: "openai/gpt-5.3-codex",
    base_url: "https://openrouter.ai/api/v1"
  }
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil, base_url: nil, api_provider: nil, env: {}, flags: [], unset_env: [], metadata: {}, chat_base_url: nil, chat_model: nil, chat_api_key: nil, chat_max_tokens: nil, chat_tools: nil) ⇒ ProviderRuntime

Returns a new instance of ProviderRuntime.

Parameters:

  • model (String, nil) (defaults to: nil)

    model identifier override

  • base_url (String, nil) (defaults to: nil)

    upstream API base URL override

  • api_provider (String, nil) (defaults to: nil)

    API-compatible backend name

  • env (Hash<String,String>) (defaults to: {})

    extra environment variables for the subprocess

  • flags (Array<String>) (defaults to: [])

    extra CLI flags to append

  • unset_env (Array<String>) (defaults to: [])

    environment variable names to remove from inherited env

  • metadata (Hash) (defaults to: {})

    arbitrary provider-specific data

  • chat_base_url (String, nil) (defaults to: nil)

    override transport base URL for chat

  • chat_model (String, nil) (defaults to: nil)

    override model for chat

  • chat_api_key (String, nil) (defaults to: nil)

    override API key for chat

  • chat_max_tokens (Integer, nil) (defaults to: nil)

    max tokens for chat response

  • chat_tools (Array<Hash>, nil) (defaults to: nil)

    default tool definitions for chat requests



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/agent_harness/provider_runtime.rb', line 43

def initialize(model: nil, base_url: nil, api_provider: nil, env: {}, flags: [], unset_env: [], metadata: {},
  chat_base_url: nil, chat_model: nil, chat_api_key: nil, chat_max_tokens: nil, chat_tools: nil)
  validate_optional_string!(:model, model)
  validate_optional_string!(:base_url, base_url)
  validate_optional_string!(:api_provider, api_provider)

  @model = model
  @base_url = base_url
  @api_provider = api_provider

  env_hash = env.nil? ? {} : env
  unless env_hash.is_a?(Hash)
    raise ArgumentError, "env must be a Hash (got #{env_hash.class})"
  end
  normalized_env = env_hash.each_with_object({}) do |(key, value), acc|
    string_key = key.to_s
    unless value.is_a?(String)
      raise ArgumentError, "env value for #{string_key.inspect} must be a String (got #{value.class})"
    end
    acc[string_key] = value
  end
  @env = normalized_env.freeze

  normalized_flags = flags.nil? ? [] : flags
  unless normalized_flags.is_a?(Array)
    raise ArgumentError, "flags must be an Array (got #{normalized_flags.class})"
  end
  normalized_flags = normalized_flags.dup
  normalized_flags.each_with_index do |flag, index|
    unless flag.is_a?(String)
      raise ArgumentError,
        "flags must be an Array of Strings; invalid element at index #{index}: #{flag.inspect} (#{flag.class})"
    end
  end
  @flags = normalized_flags.freeze

   = .nil? ? {} : 
  unless .is_a?(Hash)
    raise ArgumentError, "metadata must be a Hash (got #{.class})"
  end
  @metadata = .dup.freeze

  # Unset environment variables for the request. These are variable names that
  # should be removed from the inherited environment before the provider
  # command runs.
  unset_array = unset_env.nil? ? [] : unset_env
  unless unset_array.is_a?(Array)
    raise ArgumentError, "unset_env must be an Array (got #{unset_array.class})"
  end
  normalized_unset_env = unset_array.map.with_index do |key, index|
    key.to_s
  rescue NoMethodError
    raise ArgumentError,
      "unset_env must contain values convertible to String; invalid element at index #{index}: #{key.inspect} (#{key.class})"
  end
  @unset_env = normalized_unset_env.freeze

  validate_optional_string!(:chat_base_url, chat_base_url)
  validate_optional_string!(:chat_model, chat_model)
  validate_optional_string!(:chat_api_key, chat_api_key)
  unless chat_max_tokens.nil? || chat_max_tokens.is_a?(Integer)
    raise ArgumentError, "chat_max_tokens must be an Integer or nil (got #{chat_max_tokens.class})"
  end
  unless chat_tools.nil? || chat_tools.is_a?(Array)
    raise ArgumentError, "chat_tools must be an Array or nil (got #{chat_tools.class})"
  end
  normalized_chat_tools = chat_tools&.dup
  normalized_chat_tools&.each_with_index do |tool, index|
    unless tool.is_a?(Hash)
      raise ArgumentError,
        "chat_tools must be an Array of Hashes; invalid element at index #{index}: #{tool.inspect} (#{tool.class})"
    end
  end
  @chat_base_url = chat_base_url
  @chat_model = chat_model
  @chat_api_key = chat_api_key
  @chat_max_tokens = chat_max_tokens
  @chat_tools = normalized_chat_tools&.freeze

  freeze
end

Instance Attribute Details

#api_providerObject (readonly)

Returns the value of attribute api_provider.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def api_provider
  @api_provider
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def base_url
  @base_url
end

#chat_api_keyObject (readonly)

Returns the value of attribute chat_api_key.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def chat_api_key
  @chat_api_key
end

#chat_base_urlObject (readonly)

Returns the value of attribute chat_base_url.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def chat_base_url
  @chat_base_url
end

#chat_max_tokensObject (readonly)

Returns the value of attribute chat_max_tokens.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def chat_max_tokens
  @chat_max_tokens
end

#chat_modelObject (readonly)

Returns the value of attribute chat_model.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def chat_model
  @chat_model
end

#chat_toolsObject (readonly)

Returns the value of attribute chat_tools.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def chat_tools
  @chat_tools
end

#envObject (readonly)

Returns the value of attribute env.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def env
  @env
end

#flagsObject (readonly)

Returns the value of attribute flags.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def flags
  @flags
end

#metadataObject (readonly)

Returns the value of attribute metadata.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def 
  @metadata
end

#modelObject (readonly)

Returns the value of attribute model.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def model
  @model
end

#unset_envObject (readonly)

Returns the value of attribute unset_env.



28
29
30
# File 'lib/agent_harness/provider_runtime.rb', line 28

def unset_env
  @unset_env
end

Class Method Details

.from_hash(hash) ⇒ ProviderRuntime

Build a ProviderRuntime from a Hash.

Parameters:

  • hash (Hash)

    runtime attributes

Returns:

Raises:

  • (ArgumentError)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/agent_harness/provider_runtime.rb', line 129

def self.from_hash(hash)
  raise ArgumentError, "expected a Hash, got #{hash.class}" unless hash.is_a?(Hash)

  env_val = hash_value(hash, :env)
  flags_val = hash_value(hash, :flags)
  unset_env_val = hash_value(hash, :unset_env)
   = hash_value(hash, :metadata)

  new(
    model: hash_value(hash, :model),
    base_url: hash_value(hash, :base_url),
    api_provider: hash_value(hash, :api_provider),
    env: env_val.nil? ? {} : env_val,
    flags: flags_val.nil? ? [] : flags_val,
    unset_env: unset_env_val.nil? ? [] : unset_env_val,
    metadata: .nil? ? {} : ,
    chat_base_url: hash_value(hash, :chat_base_url),
    chat_model: hash_value(hash, :chat_model),
    chat_api_key: hash_value(hash, :chat_api_key),
    chat_max_tokens: hash_value(hash, :chat_max_tokens),
    chat_tools: hash_value(hash, :chat_tools)
  )
end

.wrap(value) ⇒ ProviderRuntime?

Coerce a value into a ProviderRuntime.

Parameters:

Returns:



157
158
159
160
161
162
163
164
165
# File 'lib/agent_harness/provider_runtime.rb', line 157

def self.wrap(value)
  case value
  when ProviderRuntime then value
  when Hash then from_hash(value)
  when nil then nil
  else
    raise ArgumentError, "Cannot coerce #{value.class} into ProviderRuntime"
  end
end

Instance Method Details

#empty?Boolean

Whether any meaningful overrides are present.

Returns:

  • (Boolean)


170
171
172
173
174
175
# File 'lib/agent_harness/provider_runtime.rb', line 170

def empty?
  model.nil? && base_url.nil? && api_provider.nil? &&
    env.empty? && flags.empty? && .empty? && unset_env.empty? &&
    chat_base_url.nil? && chat_model.nil? && chat_api_key.nil? && chat_max_tokens.nil? &&
    chat_tools.nil?
end