Module: Vangrail::Providers

Defined in:
lib/vangrail/providers.rb,
lib/vangrail/providers/gateway.rb,
lib/vangrail/providers/llmlite.rb

Overview

The provider registry, in the order Provider.resolve tries it.

Order is the whole policy: local before shared. A proxy on the loopback costs nothing per call, keeps rail traffic on the machine, and needs no shared credential, so an application with one running should use it without being told to.

Only vendor-neutral entries are built in. A hostname compiled into this gem is an endpoint every installation inherits whether it can reach it or not, and a credential path compiled in is worse, because it publishes where somebody's secrets live. A shared gateway is therefore registered by the application that has one, or described by environment:

Vangrail::Providers.register_gateway(Gateway::Spec.new(name: 'hub', base_url: '...'))
Vangrail::Providers.register_gateway(name: 'hub', base_url: '...')
GUARDRAILS_GATEWAY_API_BASE=...  GUARDRAILS_GATEWAY_API_KEY=...

An endpoint needed for a single run needs no registration at all: GUARDRAILS_API_BASE with GUARDRAILS_API_KEY beats the whole registry.

Defined Under Namespace

Modules: Gateway, Llmlite

Class Method Summary collapse

Class Method Details

.install!(env = ENV) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/vangrail/providers.rb', line 30

def install!(env = ENV)
  Provider.registry.clear
  Provider.register(Llmlite.provider(env))
  register_environment_gateway(env)
  registered_specs.each { |spec| Provider.register(Gateway.provider(spec, env)) }
  Provider.registry
end

.register_environment_gateway(env = ENV) ⇒ Object



54
55
56
57
58
59
# File 'lib/vangrail/providers.rb', line 54

def register_environment_gateway(env = ENV)
  spec = Gateway.from_environment(env)
  return nil unless spec

  Provider.register(Gateway.provider(spec, env))
end

.register_gateway(spec = nil, env: ENV, **kwargs) ⇒ Object

Registers a shared gateway and returns its Provider. Registering a name twice replaces it, so reloading an application is not a duplicate.

Preferred: a Gateway::Spec. The 0.1.0 keywords still work and become one.



47
48
49
50
51
52
# File 'lib/vangrail/providers.rb', line 47

def register_gateway(spec = nil, env: ENV, **kwargs)
  spec = spec.is_a?(Gateway::Spec) ? spec : Gateway::Spec.new(**(spec || kwargs))
  registered_specs.reject! { |s| s.name == spec.name }
  registered_specs << spec
  Provider.register(Gateway.provider(spec, env))
end

.registered_specsObject

Gateways an application asked for, in the order it asked.



39
40
41
# File 'lib/vangrail/providers.rb', line 39

def registered_specs
  @registered_specs ||= []
end

.reset!Object

Forgets registered gateways along with any cached credential, so a test that points a lookup at nothing gets what it asked for.



63
64
65
66
# File 'lib/vangrail/providers.rb', line 63

def reset!
  @registered_specs = []
  install!
end