Jido.AI.Keyring (Jido AI v0.5.2)
View SourceA GenServer that manages environment variables and application configuration.
This module serves as the source of truth for configuration values, with a hierarchical loading priority:
- Session values (per-process overrides)
- Environment variables (via Dotenvy)
- Application environment (under :jido_ai, :keyring)
- Default values
The keyring supports both global environment values and process-specific session values, allowing for flexible configuration management in different contexts.
Usage
# Get a value (checks session then environment)
value = Keyring.get(:my_key, "default")
# Set a session-specific override
Keyring.set_session_value(:my_key, "override")
# Clear session values
Keyring.clear_session_value(:my_key)
Keyring.clear_all_session_values()
Summary
Functions
Returns the child specification for starting the keyring under a supervisor.
Clears all session-specific values for the specified process.
Clears a session-specific value for the specified process.
Gets a value from the keyring, checking session values first, then environment values.
Gets a value from the environment-level storage, also checking for LiveBook prefixed keys as fallback.
Gets an environment variable with a default value.
Gets a session-specific value for the specified process.
Checks if a value exists and is non-empty.
Lists all available keys in the keyring.
Sets a session-specific value that will override the environment value for the specified process.
Starts the keyring process.
Functions
@spec child_spec(keyword()) :: Supervisor.child_spec()
Returns the child specification for starting the keyring under a supervisor.
@spec clear_all_session_values(GenServer.server(), pid()) :: :ok
Clears all session-specific values for the specified process.
Parameters
server
- The server to query (default: Elixir.Jido.AI.Keyring)pid
- The process ID to clear all values for (default: current process)
Returns :ok
.
@spec clear_session_value(GenServer.server(), atom(), pid()) :: :ok
Clears a session-specific value for the specified process.
Parameters
server
- The server to query (default: Elixir.Jido.AI.Keyring)key
- The key to clear (as an atom)pid
- The process ID to clear the value for (default: current process)
Returns :ok
.
@spec get(GenServer.server(), atom(), term(), pid()) :: term()
Gets a value from the keyring, checking session values first, then environment values.
Parameters
server
- The server to query (default: Elixir.Jido.AI.Keyring)key
- The key to look up (as an atom)default
- The default value if key is not foundpid
- The process ID to check session values for (default: current process)
Returns the value if found, otherwise returns the default value.
@spec get_env_value(GenServer.server(), atom(), term()) :: term()
Gets a value from the environment-level storage, also checking for LiveBook prefixed keys as fallback.
Parameters
server
- The server to query (default: Elixir.Jido.AI.Keyring)key
- The key to look up (as an atom)default
- The default value if key is not found
Returns the environment value if found, otherwise returns the default value.
Gets an environment variable with a default value.
Parameters
key
- The environment variable namedefault
- The default value if not found
Returns the environment variable value if found, otherwise returns the default value.
@spec get_session_value(GenServer.server(), atom(), pid()) :: term() | nil
Gets a session-specific value for the specified process.
Parameters
server
- The server to query (default: Elixir.Jido.AI.Keyring)key
- The key to look up (as an atom)pid
- The process ID to get the value for (default: current process)
Returns the session value if found, otherwise returns nil
.
Checks if a value exists and is non-empty.
Returns true
if the value is a non-empty string, false
otherwise.
@spec list(GenServer.server()) :: [atom()]
Lists all available keys in the keyring.
Returns a list of atoms representing the available environment-level keys. Does not include session-specific overrides.
@spec set_session_value(GenServer.server(), atom(), term(), pid()) :: :ok
Sets a session-specific value that will override the environment value for the specified process.
Parameters
server
- The server to query (default: Elixir.Jido.AI.Keyring)key
- The key to set (as an atom)value
- The value to storepid
- The process ID to associate with this value (default: current process)
Returns :ok
.
@spec start_link(keyword()) :: GenServer.on_start()
Starts the keyring process.
Options
:name
- The name to register the process under (default: Elixir.Jido.AI.Keyring):registry
- The name for the ETS registry table (default: jido_ai_keyring_sessions)