Class: Legion::Extensions::Llm::Vertex::Helpers::Callable

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/vertex/helpers/callable.rb

Overview

Callable wrapper for a Vertex AI provider instance. Delegates the fleet dispatch operations to a per-instance Vertex::Provider (real HTTP dispatch; provider/Faraday errors propagate so normalize_dispatch_error classifies them) and implements the disconnect and normalize_dispatch_error(error:) contracts required by Inventory::CallableHandle and Routing::ProviderOutcome. 0.8.0 callable contract: chat/stream_chat take the rehydrated message array positionally (WorkerExecution dispatch shape) and the Selection-derived model as a bare String; folded wire params become Canonical::Params at this boundary (05 O4).

Constant Summary collapse

COMPLETION_NAMED_KEYS =

Keys the base Provider exposes as named kwargs for the completion operations. Anything else the fleet passes (sampling scalars, temperature — a Canonical::Params member, 05 O4) is folded into Canonical::Params at the dispatch boundary.

%i[tools schema thinking tool_prefs headers].freeze
EMBED_NAMED_KEYS =

Named kwargs of Vertex's own embed dialect (task_type/title are Vertex wire spellings); anything else folds into params.

%i[dimensions task_type title headers].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_cfg:, logger:) ⇒ Callable

Returns a new instance of Callable.



34
35
36
37
38
39
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 34

def initialize(instance_cfg:, logger:)
  @instance_cfg = instance_cfg
  @logger = logger
  @provider = Provider.new(instance_cfg)
  @disconnected = false
end

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



32
33
34
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 32

def provider
  @provider
end

Instance Method Details

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

--- Fleet dispatch operations (Fleet::WorkerExecution contract) --



53
54
55
56
57
58
59
60
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 53

def chat(messages, model:, **rest)
  # Canonical boundary (N x N law): pipeline dispatch delivers
  # Canonical::Message objects only. Hash/legacy shapes are the
  # bypass class — reject loudly, never coerce.
  provider.enforce_canonical_messages!(messages)
  named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS)
  provider.chat(messages, model: model, params: canonical_params(params), **named)
end

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



73
74
75
76
77
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 73

def count_tokens(messages:, model:, **rest)
  provider.enforce_canonical_messages!(messages)
  _named, params = split_fleet_kwargs(rest, [])
  provider.count_tokens(messages: messages, model: model, params: params)
end

#disconnectObject



45
46
47
48
49
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 45

def disconnect
  @disconnected = true
  @provider.disconnect
  @logger.debug { '[vertex][callable] disconnected' }
end

#disconnected?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 41

def disconnected?
  @disconnected
end

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



68
69
70
71
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 68

def embed(text:, model:, **rest)
  named, params = split_fleet_kwargs(rest, EMBED_NAMED_KEYS)
  provider.embed(text: text, model: model, params: params, **named)
end

#normalize_dispatch_error(error:) ⇒ Object

--- Error normalization ------------------------------------------



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
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 81

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
           # ServiceUnavailableError and all other errors map to provider_error.
           # Never escalate to instance_unavailable from a typed error alone.
           :provider_error
         end

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

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



62
63
64
65
66
# File 'lib/legion/extensions/llm/vertex/helpers/callable.rb', line 62

def stream_chat(messages, model:, **rest, &)
  provider.enforce_canonical_messages!(messages)
  named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS)
  provider.stream_chat(messages, model: model, params: canonical_params(params), **named, &)
end