LemonAi.ProviderRegistry (lemon_ai v0.1.0)

View Source

Registry for LLM provider implementations.

Providers are registered by their API identifier and can be looked up at runtime. This module uses :persistent_term for crash-resilient storage - the registry survives process restarts without re-registration.

Design

Unlike a GenServer-based registry, this implementation stores provider mappings in :persistent_term, which:

  • Survives process crashes and restarts
  • Provides O(1) read access with no message passing
  • Is optimized for read-heavy, write-rarely patterns (perfect for registries)

Usage

# Registration (typically during application startup)
LemonAi.ProviderRegistry.register(:anthropic_messages, LemonAi.Providers.Anthropic)

# Lookup (fast, no GenServer call)
{:ok, module} = LemonAi.ProviderRegistry.get(:anthropic_messages)

Summary

Functions

Clear all providers. Primarily useful for testing.

Get the provider module for an API.

Get the provider module for an API, raising if not found.

Initialize the registry with default providers.

Check if the registry has been initialized.

List all registered API identifiers.

Register a provider module for an API.

Check if a provider is registered for an API.

Unregister a provider. Primarily useful for testing.

Functions

clear()

@spec clear() :: :ok

Clear all providers. Primarily useful for testing.

get(api_id)

@spec get(atom()) :: {:ok, module()} | {:error, :not_found}

Get the provider module for an API.

This is a fast O(1) read operation with no message passing.

get!(api_id)

@spec get!(atom()) :: module()

Get the provider module for an API, raising if not found.

init()

@spec init() :: :ok

Initialize the registry with default providers.

Called during application startup. Safe to call multiple times.

initialized?()

@spec initialized?() :: boolean()

Check if the registry has been initialized.

list()

@spec list() :: [atom()]

List all registered API identifiers.

register(api_id, module)

@spec register(atom(), module()) :: :ok

Register a provider module for an API.

This is a write operation that updates :persistent_term. While safe, it should be called sparingly (ideally only during application startup) as :persistent_term writes trigger a global GC of the term.

registered?(api_id)

@spec registered?(atom()) :: boolean()

Check if a provider is registered for an API.

unregister(api_id)

@spec unregister(atom()) :: :ok

Unregister a provider. Primarily useful for testing.