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/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, Translator

Constant Summary collapse

PROVIDER_FAMILY =
:anthropic
DEFAULT_MODEL =

Provider’s preferred default when the operator configures none. Used only as a fallback and only when the configured model policy permits it (see resolve_default_model) — a whitelist/blacklist is never overridden.

'claude-sonnet-4-6'
VERSION =
'0.2.26'

Class Method Summary collapse

Class Method Details

.default_settingsObject



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

def self.default_settings
  ::Legion::Extensions::Llm.provider_settings(
    family:   PROVIDER_FAMILY,
    instance: {
      default_model:      DEFAULT_MODEL,
      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 },
      prompt_caching:     {},
      fleet:              {
        enabled:             false,
        respond_to_requests: false,
        capabilities:        %i[chat stream_chat]
      }
    }
  )
end

.discover_instancesObject



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

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] = resolve_default_model(sanitized)
    sanitized
  end
end

.normalize_instance_config(config) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/legion/extensions/llm/anthropic.rb', line 144

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



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

def self.provider_aliases
  []
end

.provider_classObject



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

def self.provider_class
  Provider
end

.resolve_default_model(config) ⇒ Object

Resolve a default_model that never violates the configured model policy (whitelist/blacklist stays authoritative over the DEFAULT_MODEL fallback).



132
133
134
135
136
137
# File 'lib/legion/extensions/llm/anthropic.rb', line 132

def self.resolve_default_model(config)
  provider_class.policy_safe_default_model(
    configured: config[:default_model], fallback: DEFAULT_MODEL,
    **provider_class.model_policy(config, PROVIDER_FAMILY)
  )
end

.sanitize_instance_config(config) ⇒ Object



154
155
156
# File 'lib/legion/extensions/llm/anthropic.rb', line 154

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

.settings_instances(config) ⇒ Object



139
140
141
142
# File 'lib/legion/extensions/llm/anthropic.rb', line 139

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