Module: Legion::Extensions::Llm::Bedrock

Extended by:
Core, AutoRegistration, Logging::Helper
Defined in:
lib/legion/extensions/llm/bedrock.rb,
lib/legion/extensions/llm/bedrock/version.rb,
lib/legion/extensions/llm/bedrock/provider.rb,
lib/legion/extensions/llm/bedrock/actors/fleet_worker.rb,
lib/legion/extensions/llm/bedrock/runners/fleet_worker.rb

Overview

Amazon Bedrock provider extension namespace.

Defined Under Namespace

Modules: Actor, Runners Classes: Provider

Constant Summary collapse

PROVIDER_FAMILY =
:bedrock
DEFAULT_REGION =
'us-east-2'
VERSION =
'0.3.7'

Class Method Summary collapse

Class Method Details

.broker_aws_credentialsObject

Fetch AWS credentials from the Legion Identity Broker.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/legion/extensions/llm/bedrock.rb', line 143

def self.broker_aws_credentials
  return nil unless defined?(Legion::Identity::Broker)

  creds = Legion::Identity::Broker.credentials_for(:aws)
  return nil unless creds.is_a?(Hash)

  akid = creds[:access_key_id] || creds['access_key_id']
  return nil unless akid

  { api_key: akid, bedrock_access_key_id: akid,
    bedrock_secret_access_key: creds[:secret_access_key] || creds['secret_access_key'],
    bedrock_session_token: creds[:session_token] || creds['session_token'],
    bedrock_region: creds[:region] || creds['region'] || DEFAULT_REGION }.compact
end

.claude_env_pattern_matchObject

Scan Claude config env hash for any key containing all of AWS, BEARER, TOKEN, and BEDROCK fragments (case-insensitive).



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/legion/extensions/llm/bedrock.rb', line 130

def self.claude_env_pattern_match
  env_hash = CredentialSources.claude_config_value(:env)
  return nil unless env_hash.is_a?(Hash)

  fragments = %w[AWS BEARER TOKEN BEDROCK]
  _key, value = env_hash.find do |k, _v|
    upper = k.to_s.upcase
    fragments.all? { |frag| upper.include?(frag) }
  end
  value
end

.dedup_config(config) ⇒ Object



177
178
179
180
# File 'lib/legion/extensions/llm/bedrock.rb', line 177

def self.dedup_config(config)
  key = config[:bedrock_access_key_id]
  key ? config.merge(api_key: key) : config
end

.default_settingsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/extensions/llm/bedrock.rb', line 21

def self.default_settings
  ::Legion::Extensions::Llm.provider_settings(
    family: PROVIDER_FAMILY,
    instance: {
      default_model: 'us.anthropic.claude-sonnet-4-6',
      tier: :frontier,
      transport: :aws_sdk,
      credentials: {
        bearer_token: nil,
        access_key_id: nil,
        secret_access_key: nil,
        session_token: nil,
        profile: nil
      },
      provider: {
        region: DEFAULT_REGION,
        endpoint: nil,
        stub_responses: false
      },
      usage: { inference: true, embedding: true, image: false },
      limits: { concurrency: 4 },
      fleet: {
        enabled: false,
        respond_to_requests: false,
        capabilities: %i[chat stream_chat embed],
        lanes: [],
        concurrency: 4,
        queue_suffix: nil
      }
    }
  )
end

.discover_broker(candidates) ⇒ Object



121
122
123
124
125
126
# File 'lib/legion/extensions/llm/bedrock.rb', line 121

def self.discover_broker(candidates)
  return unless defined?(Legion::Identity::Broker)

  broker_creds = broker_aws_credentials
  candidates[:broker] = broker_creds.merge(tier: :cloud) if broker_creds
end

.discover_claude_bearer(candidates) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/legion/extensions/llm/bedrock.rb', line 83

def self.discover_claude_bearer(candidates)
  claude_bearer = CredentialSources.claude_env_value('AWS_BEARER_TOKEN_BEDROCK')
  claude_bearer ||= claude_env_pattern_match
  return unless claude_bearer

  candidates[:claude] = {
    bearer_token: claude_bearer,
    bedrock_region: CredentialSources.claude_env_value('AWS_DEFAULT_REGION') || DEFAULT_REGION,
    tier: :cloud
  }
end

.discover_env_bearer(candidates) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/legion/extensions/llm/bedrock.rb', line 72

def self.discover_env_bearer(candidates)
  bearer = CredentialSources.env('AWS_BEARER_TOKEN_BEDROCK')
  return unless bearer

  candidates[:env_bearer] = {
    bearer_token: bearer,
    bedrock_region: CredentialSources.env('AWS_DEFAULT_REGION') || DEFAULT_REGION,
    tier: :cloud
  }
end

.discover_env_sigv4(candidates) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/legion/extensions/llm/bedrock.rb', line 95

def self.discover_env_sigv4(candidates)
  akid = CredentialSources.env('AWS_ACCESS_KEY_ID')
  skey = CredentialSources.env('AWS_SECRET_ACCESS_KEY')
  return unless akid && skey

  candidates[:env_sigv4] = {
    api_key: akid, bedrock_access_key_id: akid, bedrock_secret_access_key: skey,
    bedrock_session_token: CredentialSources.env('AWS_SESSION_TOKEN'),
    bedrock_region: CredentialSources.env('AWS_DEFAULT_REGION') || DEFAULT_REGION, tier: :cloud
  }.compact
end

.discover_instancesObject



62
63
64
65
66
67
68
69
70
# File 'lib/legion/extensions/llm/bedrock.rb', line 62

def self.discover_instances
  candidates = {}
  discover_env_bearer(candidates)
  discover_claude_bearer(candidates)
  discover_env_sigv4(candidates)
  discover_settings(candidates)
  discover_broker(candidates)
  CredentialSources.dedup_credentials(candidates).transform_values { |config| sanitize_instance_config(config) }
end

.discover_settings(candidates) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/legion/extensions/llm/bedrock.rb', line 107

def self.discover_settings(candidates)
  settings = CredentialSources.setting(:extensions, :llm, :bedrock)
  return unless settings.is_a?(Hash) && !settings.empty?

  default_config = dedup_config(normalize_instance_config(settings))
  candidates[:settings] = default_config.merge(tier: :cloud) unless default_config.empty?

  settings_instances(settings).each do |name, config|
    next unless config.is_a?(Hash)

    candidates[name.to_sym] = dedup_config(normalize_instance_config(config)).merge(tier: :cloud)
  end
end

.normalize_instance_config(config) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/legion/extensions/llm/bedrock.rb', line 163

def self.normalize_instance_config(config)
  normalized = config.to_h.transform_keys { |key| key.respond_to?(:to_sym) ? key.to_sym : key }
  normalized[:bedrock_region] ||= normalized.delete(:region)
  normalized[:bedrock_endpoint] ||= normalized.delete(:endpoint)
  normalized[:bedrock_endpoint] ||= normalized.delete(:base_url)
  normalized[:bedrock_endpoint] ||= normalized.delete(:api_base)
  normalized[:bedrock_access_key_id] ||= normalized.delete(:api_key) || normalized.delete(:access_key_id)
  normalized[:bedrock_secret_access_key] ||= normalized.delete(:secret_key)
  normalized[:bedrock_secret_access_key] ||= normalized.delete(:secret_access_key)
  normalized[:bedrock_session_token] ||= normalized.delete(:session_token)
  normalized[:bedrock_profile] ||= normalized.delete(:profile)
  normalized.compact.except(:instances)
end

.provider_classObject



54
55
56
# File 'lib/legion/extensions/llm/bedrock.rb', line 54

def self.provider_class
  Provider
end

.registry_publisherObject



58
59
60
# File 'lib/legion/extensions/llm/bedrock.rb', line 58

def self.registry_publisher
  @registry_publisher ||= Legion::Extensions::Llm::RegistryPublisher.new(provider_family: PROVIDER_FAMILY)
end

.sanitize_instance_config(config) ⇒ Object



182
183
184
# File 'lib/legion/extensions/llm/bedrock.rb', line 182

def self.sanitize_instance_config(config)
  config.except(:api_key)
end

.settings_instances(config) ⇒ Object



158
159
160
161
# File 'lib/legion/extensions/llm/bedrock.rb', line 158

def self.settings_instances(config)
  instances = config[:instances] || config['instances']
  instances.is_a?(Hash) ? instances : {}
end