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/translator.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/actors/discovery_refresh.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, Translator

Constant Summary collapse

PROVIDER_FAMILY =
:anthropic
VERSION =
'0.2.17'

Class Method Summary collapse

Class Method Details

.default_settingsObject



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

def self.default_settings
  ::Legion::Extensions::Llm.provider_settings(
    family:   PROVIDER_FAMILY,
    instance: {
      default_model:      'claude-sonnet-4-6',
      endpoint:           'https://api.anthropic.com',
      api_version:        '2023-10-16',
      default_max_tokens: 4096,
      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



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
124
125
126
127
# File 'lib/legion/extensions/llm/anthropic.rb', line 55

def self.discover_instances
  candidates = {}

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

  claude_key = CredentialSources.claude_config_value(:anthropicApiKey)
  if claude_key
    candidates[:claude] = {
      api_key:                claude_key,
      anthropic_api_key:      claude_key,
      tier:                   :frontier,
      source:                 CredentialSources.source_tag(:file, '~/.claude/settings.json', 'anthropicApiKey'),
      credential_fingerprint: CredentialSources.credential_fingerprint(claude_key)
    }
  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,
        source:                 CredentialSources.source_tag(:settings, 'extensions.llm.anthropic'),
        credential_fingerprint: CredentialSources.credential_fingerprint(settings_key)
      )
    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]
      normalized[:source] =
        CredentialSources.source_tag(:settings, "extensions.llm.anthropic.instances.#{name}")
      normalized[:credential_fingerprint] =
        CredentialSources.credential_fingerprint(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,
        source:                 CredentialSources.source_tag(:broker, 'identity', 'anthropic'),
        credential_fingerprint: CredentialSources.credential_fingerprint(broker_cred)
      }
    end
  end

  CredentialSources.dedup_credentials(candidates).transform_values do |config|
    sanitized = sanitize_instance_config(config)
    sanitized[:capabilities] ||= %i[completion streaming vision tools].freeze
    sanitized[:default_model] ||= 'claude-sonnet-4-6'
    sanitized
  end
end

.normalize_instance_config(config) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/legion/extensions/llm/anthropic.rb', line 134

def self.normalize_instance_config(config)
  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



51
52
53
# File 'lib/legion/extensions/llm/anthropic.rb', line 51

def self.provider_aliases
  []
end

.provider_classObject



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

def self.provider_class
  Provider
end

.sanitize_instance_config(config) ⇒ Object



144
145
146
# File 'lib/legion/extensions/llm/anthropic.rb', line 144

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

.settings_instances(config) ⇒ Object



129
130
131
132
# File 'lib/legion/extensions/llm/anthropic.rb', line 129

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