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: {}) ⇒ 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



37
38
39
40
41
42
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
# File 'lib/agent_harness/provider_runtime.rb', line 37

def initialize(model: nil, base_url: nil, api_provider: nil, env: {}, flags: [], unset_env: [], metadata: {})
  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

  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

#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)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/agent_harness/provider_runtime.rb', line 100

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? ? {} : 
  )
end

.wrap(value) ⇒ ProviderRuntime?

Coerce a value into a ProviderRuntime.

Parameters:

Returns:



123
124
125
126
127
128
129
130
131
# File 'lib/agent_harness/provider_runtime.rb', line 123

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)


136
137
138
139
# File 'lib/agent_harness/provider_runtime.rb', line 136

def empty?
  model.nil? && base_url.nil? && api_provider.nil? &&
    env.empty? && flags.empty? && .empty? && unset_env.empty?
end