class Discover

Discovers models from an OpenAI-compatible provider and caches its catalog. Subclasses can filter or enrich entries by overriding #include_model? and #normalize_model.

Nested Classes and Modules

class Error

Raised when an upstream model catalog is unavailable or invalid.

Definitions

def initialize(provider, ttl: DEFAULT_TTL)

Initialize a cached model source for an upstream provider.

Signature

parameter provider Interface(:models)

The provider to query.

option :ttl Numeric

How long to cache the model list, in seconds.

Implementation

def initialize(provider, ttl: DEFAULT_TTL)
	@provider = provider
	@ttl = ttl
	@lock = Async::Semaphore.new(1)
	@models = nil
	@expires_at = 0
end

def each(&block)

Enumerate the normalized, cached model list.

Signature

yields {|model| ...}

Each discovered model.

parameter model Hash

A normalized model entry.

returns Enumerator | Nil

An enumerator without a block, otherwise nil.

Implementation

def each(&block)
	return enum_for(__method__) unless block
	
	self.all.each(&block)
end

def find(id)

Find one model by its upstream ID.

Signature

parameter id String

The model ID.

returns Hash | Nil

The matching model entry, if present.

Implementation

def find(id)
	self.all.find{|model| model["id"] == id}
end

def all

Fetch and cache the provider's normalized model catalog.

Signature

returns Array(Hash)

The cached model entries.

Implementation

def all
	if @models && Process.clock_gettime(Process::CLOCK_MONOTONIC) < @expires_at
		return @models
	end
	
	@lock.acquire do
		now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
		next @models if @models && now < @expires_at
		
		@models = fetch_models.freeze
		@expires_at = now + @ttl
	end
	
	return @models
end