Class: Legion::Extensions::Llm::Anthropic::Actor::AnthropicCallable

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb

Overview

Callable wrapper for an Anthropic provider instance. Implements the fleet dispatch operations (chat, stream_chat, embed, count_tokens) by delegating to a per-instance Anthropic::Provider, plus the disconnect and normalize_dispatch_error(error:) contracts required by Inventory::CallableHandle and Routing::ProviderOutcome.

Dispatch errors propagate unmodified so the coordinator can classify them through normalize_dispatch_error.

CRITICAL: Anthropic 529 overloaded_error is ALWAYS :overloaded. It is NEVER :instance_unavailable. Only explicit connection failures at the transport layer map to :connection_failure (which the actor's harness may escalate to :instance_unavailable).

Constant Summary collapse

CHAT_PROVIDER_KEYS =

Provider dispatch kwargs the base Provider accepts by name. Any other fleet param is merged into the provider params: passthrough, which the base Provider deep-merges into the rendered payload.

%i[tools temperature params headers schema thinking tool_prefs].freeze
EMBED_PROVIDER_KEYS =
%i[dimensions params headers].freeze

Instance Method Summary collapse

Constructor Details

#initialize(instance_cfg:, logger:) ⇒ AnthropicCallable

Returns a new instance of AnthropicCallable.



908
909
910
911
912
913
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 908

def initialize(instance_cfg:, logger:)
  @instance_cfg = instance_cfg
  @logger = logger
  @provider_mutex = Mutex.new
  @disconnected = false
end

Instance Method Details

#chat(messages:, model:, **rest) ⇒ Object



926
927
928
929
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 926

def chat(messages:, model:, **rest)
  provider.chat(messages: messages, model: normalize_model(model),
                                **dispatch_kwargs(rest, known: CHAT_PROVIDER_KEYS))
end

#count_tokens(messages:, model:, **rest) ⇒ Object



941
942
943
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 941

def count_tokens(messages:, model:, **rest)
  provider.count_tokens(messages: messages, model: normalize_model(model), params: rest)
end

#disconnectObject



945
946
947
948
949
950
951
952
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 945

def disconnect
  @provider_mutex.synchronize do
    @disconnected = true
    @provider&.disconnect
    @provider = nil
  end
  @logger.debug { '[anthropic][callable] disconnected' }
end

#disconnected?Boolean

Returns:

  • (Boolean)


922
923
924
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 922

def disconnected?
  @disconnected
end

#embed(text:, model:, **rest) ⇒ Object



936
937
938
939
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 936

def embed(text:, model:, **rest)
  provider.embed(text: text, model: normalize_model(model),
                           **dispatch_kwargs(rest, known: EMBED_PROVIDER_KEYS))
end

#normalize_dispatch_error(error:) ⇒ Object



954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 954

def normalize_dispatch_error(error:)
  reason = error.message.to_s[0, 512]

  kind = case error
         when Faraday::ConnectionFailed
           :connection_failure
         when Faraday::TimeoutError
           :timeout
         when Faraday::ClientError
           classify_client_error(error: error)
         when Faraday::ServerError
           classify_server_error(error: error)
         when Legion::Extensions::Llm::OverloadedError
           :overloaded
         else
           :provider_error
         end

  Legion::Extensions::Llm::Routing::ProviderOutcome.new(
    kind:   kind,
    reason: reason.empty? ? 'unknown dispatch error' : reason
  )
end

#providerObject

The wrapped per-instance provider (built lazily on first dispatch).



916
917
918
919
920
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 916

def provider
  @provider_mutex.synchronize do
    @provider ||= build_provider
  end
end

#stream_chat(messages:, model:, **rest) ⇒ Object



931
932
933
934
# File 'lib/legion/extensions/llm/anthropic/actors/discovery_refresh.rb', line 931

def stream_chat(messages:, model:, **rest, &)
  provider.stream_chat(messages: messages, model: normalize_model(model),
                                  **dispatch_kwargs(rest, known: CHAT_PROVIDER_KEYS), &)
end