Module: Legion::Extensions::Llm::Anthropic

Extended by:
Core, AutoRegistration, Logging::Helper
Defined in:
lib/legion/extensions/llm/anthropic.rb,
lib/legion/extensions/llm/anthropic/version.rb,
lib/legion/extensions/llm/anthropic/provider.rb,
lib/legion/extensions/llm/anthropic/registry_publisher.rb,
lib/legion/extensions/llm/anthropic/actors/fleet_worker.rb,
lib/legion/extensions/llm/anthropic/runners/fleet_worker.rb,
lib/legion/extensions/llm/anthropic/registry_event_builder.rb,
lib/legion/extensions/llm/anthropic/transport/exchanges/llm_registry.rb,
lib/legion/extensions/llm/anthropic/transport/messages/registry_event.rb

Overview

Anthropic provider extension namespace.

Defined Under Namespace

Modules: Actor, Runners, Transport Classes: Provider, RegistryEventBuilder, RegistryPublisher

Constant Summary collapse

PROVIDER_FAMILY =
:anthropic
VERSION =
'0.2.7'

Class Method Summary collapse

Class Method Details

.default_settingsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/llm/anthropic.rb', line 21

def self.default_settings
  ::Legion::Extensions::Llm.provider_settings(
    family: PROVIDER_FAMILY,
    instance: {
      endpoint: 'https://api.anthropic.com',
      tier: :frontier,
      transport: :http,
      credentials: { api_key: 'env://ANTHROPIC_API_KEY' },
      usage: { inference: true, embedding: false, image: false },
      limits: { concurrency: 4 },
      fleet: {
        enabled: false,
        respond_to_requests: false,
        capabilities: %i[chat stream_chat],
        lanes: [],
        concurrency: 4,
        queue_suffix: nil
      }
    }
  )
end

.discover_instancesObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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
# File 'lib/legion/extensions/llm/anthropic.rb', line 51

def self.discover_instances # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  candidates = {}

  env_key = CredentialSources.env('ANTHROPIC_API_KEY')
  if env_key
    candidates[:env] = {
      api_key: env_key,
      anthropic_api_key: env_key,
      tier: :frontier
    }
  end

  claude_key = CredentialSources.claude_config_value(:anthropicApiKey)
  if claude_key
    candidates[:claude] = {
      api_key: claude_key,
      anthropic_api_key: claude_key,
      tier: :frontier
    }
  end

  settings_config = CredentialSources.setting(:extensions, :llm, :anthropic)
  if settings_config.is_a?(Hash)
    settings_key = settings_config[:api_key] || settings_config['api_key']
    if settings_key
      candidates[:settings] = normalize_instance_config(settings_config).merge(
        api_key: settings_key,
        anthropic_api_key: settings_key,
        tier: :frontier
      )
    end

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

      normalized = normalize_instance_config(config)
      next unless normalized[:anthropic_api_key]

      normalized[:api_key] = normalized[:anthropic_api_key]
      candidates[name.to_sym] = normalized.merge(tier: :frontier)
    end
  end

  if defined?(Legion::Identity::Broker)
    broker_cred = Legion::Identity::Broker.credential_for(:anthropic)
    if broker_cred
      candidates[:broker] = {
        api_key: broker_cred,
        anthropic_api_key: broker_cred,
        tier: :frontier
      }
    end
  end

  CredentialSources.dedup_credentials(candidates).transform_values { |config| sanitize_instance_config(config) }
end

.normalize_instance_config(config) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity



113
114
115
116
117
118
119
120
121
# File 'lib/legion/extensions/llm/anthropic.rb', line 113

def self.normalize_instance_config(config) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
  normalized = config.to_h.transform_keys { |key| key.respond_to?(:to_sym) ? key.to_sym : key }
  normalized[:anthropic_api_key] ||= normalized.delete(:api_key)
  normalized[:anthropic_api_base] ||= normalized.delete(:base_url)
  normalized[:anthropic_api_base] ||= normalized.delete(:api_base)
  normalized[:anthropic_api_base] ||= normalized.delete(:endpoint)
  normalized[:anthropic_version] ||= normalized.delete(:version)
  normalized.compact.except(:instances)
end

.provider_aliasesObject



47
48
49
# File 'lib/legion/extensions/llm/anthropic.rb', line 47

def self.provider_aliases
  [:claude]
end

.provider_classObject



43
44
45
# File 'lib/legion/extensions/llm/anthropic.rb', line 43

def self.provider_class
  Provider
end

.sanitize_instance_config(config) ⇒ Object



123
124
125
# File 'lib/legion/extensions/llm/anthropic.rb', line 123

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

.settings_instances(config) ⇒ Object



108
109
110
111
# File 'lib/legion/extensions/llm/anthropic.rb', line 108

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