Services¶
The services layer provides infrastructure services for the CLI. These are leaf services with no business logic — they handle external interactions such as authentication, credential storage, Docker operations, and API communication.
Services are instantiated lazily via the dependency injection container
(kelvin.sdk.container.Services).
API Client¶
Factory for creating authenticated Kelvin API client instances.
API Client factory - provides authenticated Kelvin API client.
This module provides a factory for creating authenticated API client instances. It handles session validation, token refresh, and client instantiation.
- exception kelvin.sdk.services.api_client.APIClientError(message=None, exit_code=None)[source]¶
Bases:
AuthenticationErrorBase exception for API client factory errors.
Inherits from AuthenticationError (exit_code=77) so that
except AuthenticationErrorcatches login-related errors like NotLoggedInError and CredentialsExpiredError.
- exception kelvin.sdk.services.api_client.NotLoggedInError(message=None)[source]¶
Bases:
APIClientErrorRaised when no active session exists.
- Parameters:
message (str)
- exception kelvin.sdk.services.api_client.CredentialsExpiredError(message=None)[source]¶
Bases:
APIClientErrorRaised when credentials are expired and cannot be refreshed.
- Parameters:
message (str)
- class kelvin.sdk.services.api_client.APIClientFactory(session, credentials, auth)[source]¶
Bases:
objectFactory for creating authenticated Kelvin API clients.
This factory handles: - Session validation (ensures user is logged in) - Token expiration checks - Automatic token refresh when possible - Client instantiation with valid credentials
The factory does NOT cache clients - each call creates a new client. Caching should be done at the container level if needed.
Example¶
>>> factory = APIClientFactory(session, credentials, auth) >>> client = factory.create() # Returns authenticated Client >>> workloads = client.app_workloads.list()
- __init__(session, credentials, auth)[source]¶
Initialize API client factory.
Parameters¶
- session
Session service for getting current session.
- credentials
Credential store for retrieving tokens.
- auth
Auth service for refreshing tokens.
- Parameters:
session (SessionService)
credentials (CredentialStore)
auth (AuthService)
- Return type:
None
- create(verbose=False)[source]¶
Create an authenticated API client.
- Return type:
Client- Parameters:
verbose (bool)
Returns¶
- Client
Authenticated Kelvin API client.
Raises¶
- NotLoggedInError
If no active session exists or no credentials found.
- CredentialsExpiredError
If credentials are expired and cannot be refreshed.
- Parameters:
session (SessionService)
credentials (CredentialStore)
auth (AuthService)
Configuration¶
Manages CLI configuration settings persisted to a local YAML file.
ConfigService - Manages CLI configuration file storage.
This service manages the CLI configuration settings by persisting to a local YAML file in the user’s config directory.
- exception kelvin.sdk.services.config.ConfigServiceError(message=None, exit_code=None)[source]¶
Bases:
CLIErrorBase exception for ConfigService errors.
- exception kelvin.sdk.services.config.InvalidConfigKeyError(key, valid_keys)[source]¶
Bases:
ConfigServiceErrorRaised when an invalid configuration key is provided.
- exception kelvin.sdk.services.config.InvalidConfigValueError(key, value, expected_type)[source]¶
Bases:
ConfigServiceErrorRaised when an invalid configuration value is provided.
- class kelvin.sdk.services.config.CLIConfig(**data)[source]¶
Bases:
BaseModelCLI configuration settings.
This is the single source of truth for configuration defaults. Field defaults here are used everywhere in the CLI.
- Parameters:
- kelvin.sdk.services.config.get_config_keys()[source]¶
Get configuration keys with metadata from the Pydantic model.
- class kelvin.sdk.services.config.ConfigService[source]¶
Bases:
objectManages CLI configuration file storage.
This is a leaf service with no dependencies. It persists configuration settings to a YAML file in the user’s config directory.
- get_config()[source]¶
Get the current configuration.
- Return type:
- Returns:
CLIConfig with current settings.
- set_value(key, value)[source]¶
Set a configuration value.
- Parameters:
- Return type:
- Returns:
Updated CLIConfig.
- Raises:
InvalidConfigKeyError – If key is not valid.
InvalidConfigValueError – If value cannot be parsed.
- unset_value(key)[source]¶
Reset a configuration value to its default.
- Parameters:
key (
str) – The configuration key.- Return type:
- Returns:
Updated CLIConfig.
- Raises:
InvalidConfigKeyError – If key is not valid.
Credential Store¶
Secure OAuth token storage and retrieval via keyring.
Credential store service for secure OAuth token storage.
This service manages secure storage and retrieval of OAuth tokens using keyring. It is a leaf service with no dependencies.
- exception kelvin.sdk.services.credential_store.CredentialStoreError(message=None, exit_code=None)[source]¶
Bases:
CLIErrorBase exception for credential store operations.
- exception kelvin.sdk.services.credential_store.CredentialStorageError(message=None, exit_code=None)[source]¶
Bases:
CredentialStoreErrorFailed to store credentials.
- exception kelvin.sdk.services.credential_store.CredentialRetrievalError(message=None, exit_code=None)[source]¶
Bases:
CredentialStoreErrorFailed to retrieve credentials.
- class kelvin.sdk.services.credential_store.TokenCredentials(access_token, access_expires_at, refresh_token=None, refresh_expires_at=None, oauth_client_id=None)[source]¶
Bases:
objectOAuth token credentials.
- Parameters:
- class kelvin.sdk.services.credential_store.CredentialStore[source]¶
Bases:
objectSecure credential storage using keyring.
This service handles storage and retrieval of OAuth tokens in the system’s secure credential store (keyring). It does not perform authentication - that responsibility belongs to AuthService.
Example¶
>>> store = CredentialStore() >>> creds = store.retrieve("https://api.example.com") >>> if creds and not store.is_token_expired(creds): ... print("Valid credentials found")
- store(url, access_token, expires_in, refresh_token=None, refresh_expires_in=None, oauth_client_id=None)[source]¶
Store OAuth tokens in the secure keyring.
- Return type:
- Parameters:
Parameters¶
- url
The platform URL to associate with these credentials.
- access_token
The OAuth access token.
- expires_in
Number of seconds until the access token expires.
- refresh_token
Optional OAuth refresh token.
- refresh_expires_in
Number of seconds until the refresh token expires.
Returns¶
- TokenCredentials
The stored credentials with calculated expiration timestamps.
Raises¶
- CredentialStorageError
If storage fails.
- store_token_response(url, tokens, oauth_client_id=None)[source]¶
Store OAuth tokens from a TokenResponse.
- Return type:
- Parameters:
url (str)
tokens (TokenResponse)
oauth_client_id (str | None)
Parameters¶
- url
The platform URL to associate with these credentials.
- tokens
The TokenResponse from AuthService.
- oauth_client_id
The Keycloak OAuth client ID used to obtain these tokens.
Returns¶
- TokenCredentials
The stored credentials with calculated expiration timestamps.
Raises¶
- CredentialStorageError
If storage fails.
- retrieve(url)[source]¶
Retrieve OAuth tokens from the secure keyring.
- Return type:
- Parameters:
url (str)
Parameters¶
- url
The platform URL to retrieve credentials for.
Returns¶
- Optional[TokenCredentials]
The stored credentials if found, None otherwise.
- clear(url)[source]¶
Clear stored credentials for a specific URL.
Parameters¶
- url
The platform URL to clear credentials for.
Returns¶
- bool
True if credentials were cleared, False if none existed.
- is_token_expired(credentials)[source]¶
Check if the access token is expired.
- Return type:
- Parameters:
credentials (TokenCredentials)
Parameters¶
- credentials
The credentials to check.
Returns¶
- bool
True if the access token is expired, False otherwise.
Docker¶
Docker operations for building and managing application images.
DockerService - Docker operations using buildx for all builds.
This service manages Docker operations for building and managing application images. Uses buildx for all builds (both single and multi-arch) for consistency. It is a leaf service with no dependencies.
- exception kelvin.sdk.services.docker.DockerServiceError(message=None, exit_code=None)[source]¶
Bases:
CLIErrorBase exception for Docker service operations.
- exception kelvin.sdk.services.docker.DockerNotRunningError(message=None)[source]¶
Bases:
DockerServiceErrorDocker daemon is not running.
- Parameters:
message (str)
- exception kelvin.sdk.services.docker.DockerVersionError(message=None, exit_code=None)[source]¶
Bases:
DockerServiceErrorDocker version does not meet requirements.
- exception kelvin.sdk.services.docker.DockerBuildError(message=None, exit_code=None)[source]¶
Bases:
DockerServiceErrorDocker build operation failed.
- exception kelvin.sdk.services.docker.DockerImageNotFoundError(message=None, exit_code=None)[source]¶
Bases:
DockerServiceErrorDocker image not found.
- exception kelvin.sdk.services.docker.DockerPlatformError(message=None, exit_code=None)[source]¶
Bases:
DockerServiceErrorRequested platform is not supported.
- exception kelvin.sdk.services.docker.DockerRegistryError(message=None, exit_code=None)[source]¶
Bases:
DockerServiceErrorDocker registry operation failed.
- class kelvin.sdk.services.docker.DockerImage(id, name, tags, size, created_at=None, labels=<factory>)[source]¶
Bases:
objectDocker image information.
- Parameters:
- class kelvin.sdk.services.docker.BuildConfig(context_path, dockerfile_path, image_name, build_args=None, platforms=None, labels=None, fresh_build=False)[source]¶
Bases:
objectConfiguration for a Docker build operation.
- Parameters:
- class kelvin.sdk.services.docker.BuildResult(success, image_name, logs=<factory>)[source]¶
Bases:
objectResult of a Docker build operation.
- class kelvin.sdk.services.docker.DockerService[source]¶
Bases:
objectDocker operations using buildx for all builds.
This is a leaf service with no dependencies. It manages: - Docker daemon status checks - Registry authentication - Image builds (via buildx) - Image management (list, get, remove, tag, pull) - File extraction from images
All builds use buildx for consistency, even for single-architecture builds.
Example¶
>>> docker = DockerService() >>> if docker.is_running(): ... result = docker.build(config, registry_url="registry.example.com")
- is_running()[source]¶
Check if Docker daemon is running.
- Return type:
Returns¶
- bool
True if Docker is running, False otherwise.
- get_version()[source]¶
Get Docker version string.
Returns¶
- Optional[str]
Docker version string, or None if unavailable.
- validate_version(minimum_version)[source]¶
Check if Docker version meets minimum requirement.
Parameters¶
- minimum_version
Minimum required Docker version.
Returns¶
- bool
True if current version meets requirement.
Raises¶
- DockerVersionError
If version check fails or version is insufficient.
- get_supported_platforms()[source]¶
Get platforms supported by current buildx builder.
Returns¶
- List[str]
List of supported platforms (e.g., [“linux/amd64”, “linux/arm64”]).
- validate_platforms(platforms)[source]¶
Validate that requested platforms are supported by buildx.
Parameters¶
- platforms
List of platforms to validate.
Raises¶
- DockerPlatformError
If any platform is not supported.
- login(registry_url, username, password)[source]¶
Login to Docker registry (both docker and buildx).
Parameters¶
- registry_url
The registry URL to authenticate with.
- username
Registry username.
- password
Registry password or token.
Raises¶
- DockerRegistryError
If login fails.
- build(config, registry_url=None)[source]¶
Build image locally using buildx.
Single platform: loads image to local daemon (–load)
Multi platform: builds but does NOT load (multi-arch can’t be loaded locally)
If registry_url provided, image name is prefixed with it
Parameters¶
- config
Build configuration.
- registry_url
Optional registry URL to prefix image name.
Returns¶
- BuildResult
Result of the build operation.
Raises¶
- DockerBuildError
If build fails.
- rtype:
- Parameters:
config (BuildConfig)
registry_url (str | None)
- Return type:
- build_and_push(config, registry_url)[source]¶
Build and push image to registry using buildx –push.
Supports multi-arch builds
Image name is prefixed with registry_url
Image is NOT loaded locally (pushed directly to registry)
Parameters¶
- config
Build configuration.
- registry_url
Registry URL to push to.
Returns¶
- BuildResult
Result of the build operation.
Raises¶
- DockerBuildError
If build or push fails.
- rtype:
- Parameters:
config (BuildConfig)
registry_url (str)
- Return type:
- list_images(labels=None)[source]¶
List local images, optionally filtered by labels.
- Return type:
- Parameters:
Parameters¶
- labels
Optional labels to filter images.
Returns¶
- List[DockerImage]
List of matching Docker images.
- get_image(name)[source]¶
Get a specific image by name/tag.
- Return type:
- Parameters:
name (str)
Parameters¶
- name
Image name or tag.
Returns¶
- Optional[DockerImage]
The image if found, None otherwise.
- image_exists(name)[source]¶
Check if image exists locally.
Parameters¶
- name
Image name or tag.
Returns¶
- bool
True if image exists, False otherwise.
- remove_image(name)[source]¶
Remove a local image.
Parameters¶
- name
Image name or tag to remove.
Raises¶
- DockerImageNotFoundError
If image does not exist.
- tag_image(source, target)[source]¶
Tag an existing image with a new name.
Parameters¶
- source
Source image name.
- target
Target tag name.
Raises¶
- DockerImageNotFoundError
If source image does not exist.
- pull_image(name)[source]¶
Pull image from registry.
- Return type:
- Parameters:
name (str)
Parameters¶
- name
Image name with optional tag.
Returns¶
- DockerImage
The pulled image.
Raises¶
- DockerImageNotFoundError
If image not found in registry.
- DockerRegistryError
If pull fails.
- extract_from_image(image_name, container_path, output_path)[source]¶
Extract files from an image to local path.
Parameters¶
- image_name
Name of the image to extract from.
- container_path
Path inside the container to extract.
- output_path
Local path to extract files to.
Raises¶
- DockerImageNotFoundError
If image does not exist.
- DockerServiceError
If extraction fails.
MLflow¶
MLflow registry operations for model listing, retrieval, and artifact downloading.
MLflowService - MLflow registry operations.
This is a leaf service that wraps MLflow operations for interacting with MLflow model registries. It handles model listing, version retrieval, and model artifact downloading.
MLflow is an optional dependency. If not installed, operations will raise MLflowNotInstalledError with instructions on how to install it.
- exception kelvin.sdk.services.mlflow.MLflowServiceError(message=None, exit_code=None)[source]¶
Bases:
CLIErrorBase exception for MLflow service errors.
- exception kelvin.sdk.services.mlflow.MLflowNotInstalledError[source]¶
Bases:
MLflowServiceErrorRaised when MLflow is not installed.
- Return type:
None
- exception kelvin.sdk.services.mlflow.RegistryConnectionError(registry_uri, reason)[source]¶
Bases:
MLflowServiceErrorRaised when cannot connect to MLflow registry.
- exception kelvin.sdk.services.mlflow.ModelNotFoundError(model_name)[source]¶
Bases:
MLflowServiceErrorRaised when a model is not found in the registry.
- Parameters:
model_name (str)
- Return type:
None
- exception kelvin.sdk.services.mlflow.ModelVersionNotFoundError(model_name, version)[source]¶
Bases:
MLflowServiceErrorRaised when a model version is not found.
- exception kelvin.sdk.services.mlflow.ModelDownloadError(model_name, version, reason)[source]¶
Bases:
MLflowServiceErrorRaised when model download fails.
- class kelvin.sdk.services.mlflow.ModelIO(name, data_type)[source]¶
Bases:
objectModel output specification.
- class kelvin.sdk.services.mlflow.DownloadedModel(model_path, inputs=<factory>, outputs=<factory>, description=None)[source]¶
Bases:
objectInformation about a downloaded model.
- Parameters:
- class kelvin.sdk.services.mlflow.MLflowService[source]¶
Bases:
objectMLflow registry operations.
This is a leaf service with no dependencies on other services. It wraps MLflow client operations and handles the optional MLflow dependency gracefully.
- list_models(registry_uri, filter_string=None, max_results=1000)[source]¶
List registered models in the MLflow registry.
- Parameters:
- Return type:
list[RegisteredModel]- Returns:
List of RegisteredModel objects from MLflow.
- Raises:
RegistryConnectionError – If cannot connect to registry.
- get_model_versions(registry_uri, model_name)[source]¶
Get all versions of a model.
- Parameters:
- Return type:
list[ModelVersion]- Returns:
List of ModelVersion objects from MLflow.
- Raises:
RegistryConnectionError – If cannot connect to registry.
ModelNotFoundError – If model doesn’t exist.
- download_model(registry_uri, model_name, model_version, dest_path)[source]¶
Download model artifacts and extract signature information.
- Parameters:
- Return type:
- Returns:
DownloadedModel with path and signature information.
- Raises:
RegistryConnectionError – If cannot connect to registry.
ModelVersionNotFoundError – If model version doesn’t exist.
ModelDownloadError – If download fails.
Schema¶
JSON Schema validation for app configuration files.
SchemaService - JSON Schema validation for app configurations.
This service validates app configuration files against JSON schemas. Schemas are fetched from a remote service based on the spec_version defined in the app configuration.
- exception kelvin.sdk.services.schema.SchemaServiceError(message=None, exit_code=None)[source]¶
Bases:
CLIErrorBase exception for SchemaService errors.
- exception kelvin.sdk.services.schema.SchemaFetchError(message=None, exit_code=None)[source]¶
Bases:
SchemaServiceErrorRaised when schema cannot be fetched from remote service.
- exception kelvin.sdk.services.schema.SchemaValidationError(message, errors)[source]¶
Bases:
SchemaServiceErrorRaised when app configuration fails schema validation.
- class kelvin.sdk.services.schema.SchemaValidationResult(valid, errors=<factory>, warnings=<factory>)[source]¶
Bases:
objectResult of schema validation.
- class kelvin.sdk.services.schema.SchemaService[source]¶
Bases:
objectSchema management and validation.
This is a leaf service with no dependencies. It validates app configurations against JSON schemas fetched from a remote service.
The schema URL is determined by the spec_version key in the app configuration. Schemas are available at
https://apps.kelvininc.com/schemas/kelvin/{version}/app/app.json- validate_config_dict(config)[source]¶
Validate an app configuration dictionary against its schema.
The schema version is determined by the ‘spec_version’ key in the config.
- Parameters:
config (
dict[str,object]) – The configuration dictionary to validate.- Return type:
- Returns:
SchemaValidationResult with validation outcome.
- Raises:
SchemaFetchError – If the schema cannot be fetched.
- get_schema(version)[source]¶
Get the schema for a specific version.
- Parameters:
version (
str) – The schema version to retrieve.- Return type:
- Returns:
The schema as a dictionary.
- Raises:
SchemaFetchError – If the schema cannot be fetched.
Session¶
Session state management and platform metadata.
SessionService - Manages session state and platform metadata file storage.
This service manages the current session state, including the platform URL, Docker registry information, and platform metadata by persisting to local files.
- exception kelvin.sdk.services.session.SessionServiceError(message=None, exit_code=None)[source]¶
Bases:
CLIErrorBase exception for SessionService errors.
- exception kelvin.sdk.services.session.SessionNotFoundError(message=None, exit_code=None)[source]¶
Bases:
SessionServiceErrorRaised when no session is found.
- exception kelvin.sdk.services.session.SessionStorageError(message=None, exit_code=None)[source]¶
Bases:
SessionServiceErrorRaised when session storage operations fail.
- class kelvin.sdk.services.session.SessionService[source]¶
Bases:
objectManages session state and platform metadata file storage.
This is a leaf service with no dependencies. It simply persists session and metadata information to local files.
Note: Login state checking and token refresh are NOT handled here. Those responsibilities belong to the command layer (AuthCommands) which orchestrates CredentialStore and AuthService.
- get_current_session()[source]¶
Get the current session information.
- Return type:
- Returns:
SessionInfo if a session exists, None otherwise.
- set_current_session(url, metadata=None)[source]¶
Set the current session and optionally store metadata.
- Parameters:
url (
str) – The platform URL.metadata (
Optional[PlatformMetadata]) – Optional platform metadata. If provided, will also be stored.
- Return type:
- Returns:
The created SessionInfo.
- Raises:
SessionStorageError – If saving the session fails.
- clear_session()[source]¶
Clear the current session and metadata files.
- Raises:
SessionStorageError – If clearing the session fails.
- Return type:
- store_metadata(metadata)[source]¶
Store platform metadata.
- Parameters:
metadata (
PlatformMetadata) – The metadata to store.- Raises:
SessionStorageError – If storing fails.
- Return type:
- get_metadata()[source]¶
Get stored platform metadata.
- Return type:
- Returns:
PlatformMetadata if available, None otherwise.
- get_docker_registry_url()[source]¶
Get the Docker registry URL including optional path.
Used for image name prefixes (e.g.,
registry.example.com:5000/kelvin).
- get_docker_login_url()[source]¶
Get the Docker registry URL for
docker login(host:port only, no path).docker loginonly accepts host:port, not a full image path prefix.
Session Models¶
Data models for session state and platform metadata.
Session and Platform Metadata models.
Pydantic models for session state and platform metadata.
- class kelvin.sdk.services.session_models.SessionInfo(**data)[source]¶
Bases:
BaseModelCurrent session information.
- Parameters:
- class kelvin.sdk.services.session_models.ApiDocumentation(**data)[source]¶
Bases:
BaseModelDocumentation section of the API metadata response.
- Parameters:
url (str | None)
- class kelvin.sdk.services.session_models.ApiDocker(**data)[source]¶
Bases:
BaseModelDocker section of the API metadata response.
- class kelvin.sdk.services.session_models.ApiSdk(**data)[source]¶
Bases:
BaseModelSDK section of the API metadata response.
- Parameters:
- class kelvin.sdk.services.session_models.ApiSchema(**data)[source]¶
Bases:
BaseModelSchema section of the API metadata response.
- class kelvin.sdk.services.session_models.ApiMetadataResponse(**data)[source]¶
Bases:
BaseModelPydantic model for parsing the platform metadata API response.
- Parameters:
documentation (ApiDocumentation | None)
docker (ApiDocker | None)
sdk (ApiSdk | None)
schema (ApiSchema | None)
- model_config: ClassVar[ConfigDict] = {'populate_by_name': True, 'validate_by_alias': True, 'validate_by_name': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- documentation: ApiDocumentation | None¶
Template¶
Application template management and project scaffolding.
TemplateService - Application template management and project scaffolding.
This service manages application templates for creating new Kelvin applications. Templates are defined declaratively using template.yaml files that specify: - Static files to copy - Jinja2 templates to render - Shared files from _common directory - Symlinks to create
Template structure:
templates/
├── _common/ # Shared files across templates
│ ├── .dockerignore.jinja2
│ └── ui_schemas/
├── app/
│ ├── template.yaml # Template definition
│ ├── app.yaml.jinja2
│ └── ...
└── importer/
├── template.yaml
└── ...
- exception kelvin.sdk.services.template.TemplateServiceError(message=None, exit_code=None)[source]¶
Bases:
CLIErrorBase exception for TemplateService errors.
- exception kelvin.sdk.services.template.TemplateNotFoundError(template_name)[source]¶
Bases:
TemplateServiceErrorRaised when a requested template does not exist.
- Parameters:
template_name (str)
- Return type:
None
- exception kelvin.sdk.services.template.ManifestError(template_name, reason)[source]¶
Bases:
TemplateServiceErrorRaised when a manifest file is invalid or missing.
- exception kelvin.sdk.services.template.TemplateRenderError(file_path, reason)[source]¶
Bases:
TemplateServiceErrorRaised when a template fails to render.
- exception kelvin.sdk.services.template.FileCreationError(file_path, reason)[source]¶
Bases:
TemplateServiceErrorRaised when file creation fails.
- class kelvin.sdk.services.template.FileMapping(**data)[source]¶
Bases:
BaseModelFile mapping in template.yaml.
- class kelvin.sdk.services.template.SymlinkMapping(**data)[source]¶
Bases:
BaseModelSymlink mapping in template.yaml.
- class kelvin.sdk.services.template.TemplateManifest(**data)[source]¶
Bases:
BaseModelParsed template.yaml manifest file.
- Parameters:
name (str)
description (str)
app_type (str)
files (list[FileMapping])
symlinks (list[SymlinkMapping])
- files: list[FileMapping]¶
- symlinks: list[SymlinkMapping]¶
- classmethod from_dict(data, template_name)[source]¶
Create a TemplateManifest from a dictionary.
- Parameters:
- Return type:
- Returns:
Parsed TemplateManifest.
- Raises:
ManifestError – If validation fails.
- class kelvin.sdk.services.template.TemplateInfo(name, description, app_type)[source]¶
Bases:
objectInformation about an available template.
- class kelvin.sdk.services.template.TemplateContext(app_name, app_version, app_description='', title='', app_type='', spec_version='5.0.0', inputs=<factory>, outputs=<factory>)[source]¶
Bases:
objectContext for rendering an application template.
This contains all the variables available to Jinja2 templates.
- Parameters:
- class kelvin.sdk.services.template.CreateAppResult(app_path, files_created=<factory>, symlinks_created=<factory>)[source]¶
Bases:
objectResult of app creation from template.
- class kelvin.sdk.services.template.SyncAgentResult(agents_md_path, symlinks_created=<factory>)[source]¶
Bases:
objectResult of syncing agent files.
- class kelvin.sdk.services.template.TemplateService(templates_dir=None)[source]¶
Bases:
objectApplication template management and project scaffolding.
This is a leaf service with no dependencies. It manages templates stored in the templates/ directory and creates new applications from those templates.
Templates are defined using manifest.yaml files that specify what files to copy/render and what symlinks to create.
- Parameters:
templates_dir (Optional[Path])
- list_templates()[source]¶
List all available application templates.
- Return type:
- Returns:
List of TemplateInfo objects.
- get_template(name)[source]¶
Get template info by name.
- Parameters:
name (
str) – Template name.- Return type:
- Returns:
TemplateInfo for the template.
- Raises:
TemplateNotFoundError – If template doesn’t exist.
ManifestError – If manifest is invalid.
- create_app(template_name, target_dir, context)[source]¶
Create an application from template.
Creates target_dir/<context.app_name>/ with all template files.
- Parameters:
template_name (
str) – Name of the template to use.target_dir (
Path) – Parent directory where app folder will be created.context (
TemplateContext) – Template context with variables for rendering.
- Return type:
- Returns:
CreateAppResult with paths to created files.
- Raises:
TemplateNotFoundError – If template doesn’t exist.
ManifestError – If manifest is invalid.
TemplateRenderError – If template rendering fails.
FileCreationError – If file creation fails.
- preview_files(template_name, context)[source]¶
Preview file paths that would be created.
- Parameters:
template_name (
str) – Name of the template to use.context (
TemplateContext) – Application context (only app_name is used).
- Return type:
- Returns:
List of relative file paths that would be created.
- Raises:
TemplateNotFoundError – If template doesn’t exist.
ManifestError – If manifest is invalid.
- sync_agent_file(target_dir, template_name='app')[source]¶
Sync AGENTS.md, .skills directory, and related symlinks to an existing app directory.
Reads the template manifest to find the AGENTS.md file, .skills directory, and related symlinks, then copies/creates them. This avoids duplicating the file/symlink definitions — template.yaml is the single source of truth.
- Parameters:
- Return type:
- Returns:
SyncAgentResult with paths to created files and symlinks.
- Raises:
TemplateServiceError – If the target directory is not a Kelvin app (no app.yaml), or if AGENTS.md is not found in the template manifest.
Authentication¶
OAuth/PKCE authentication service package.
Auth Service¶
Core authentication service handling OAuth flows and token operations.
AuthService - Pure authentication service for OAuth flows.
This service handles OAuth flows and token operations but does NOT store credentials. The calling code (Commands layer) is responsible for deciding when/if to persist tokens using CredentialStore.
- class kelvin.sdk.services.auth.auth_service.AuthService[source]¶
Bases:
objectPure authentication service - returns tokens, no storage.
This service handles OAuth flows and token operations but does NOT store credentials. The calling code (Commands layer) is responsible for deciding when/if to persist tokens using CredentialStore.
- login_browser(url, open_browser=True, on_auth_url=None)[source]¶
Start browser-based OAuth flow, return tokens.
- Parameters:
- Return type:
- Returns:
TokenResponse containing access and refresh tokens.
- Raises:
OAuthFlowError – If the OAuth flow fails.
- get_authorization_url(url)[source]¶
Get the authorization URL for manual browser-based login.
This is useful when automatic browser opening is not desired.
- Parameters:
url (
str) – The base URL of the Kelvin platform.- Return type:
- Returns:
The authorization URL to open in a browser.
- Raises:
OAuthFlowError – If fetching the authorization endpoint fails.
- login_password(url, username, password, totp=None)[source]¶
Authenticate with username/password, return tokens.
- Parameters:
- Return type:
- Returns:
TokenResponse containing access and refresh tokens.
- Raises:
OAuthFlowError – If authentication fails.
- login_client_credentials(url, client_id, client_secret)[source]¶
Authenticate with client credentials, return tokens.
- Parameters:
- Return type:
- Returns:
TokenResponse containing access token (no refresh token for this grant).
- Raises:
OAuthFlowError – If authentication fails.
- refresh_tokens(url, refresh_token, oauth_client_id=None)[source]¶
Refresh expired tokens, return new tokens.
- Parameters:
- Return type:
- Returns:
TokenResponse containing new tokens.
- Raises:
OAuthFlowError – If token refresh fails.
- logout(url, refresh_token, oauth_client_id=None)[source]¶
Revoke tokens on the server.
- Parameters:
- Raises:
OAuthFlowError – If logout fails.
- Return type:
- fetch_platform_metadata(url)[source]¶
Fetch platform metadata from the /metadata endpoint.
- Parameters:
url (
str) – The base URL of the Kelvin platform.- Return type:
- Returns:
PlatformMetadata containing docker registry info and other platform details.
- Raises:
OAuthFlowError – If fetching metadata fails.
Callback Server¶
Local HTTP server for handling OAuth redirect callbacks.
Local HTTP server for handling OAuth callbacks.
- class kelvin.sdk.services.auth.callback_server.CallbackResult(code=None, state=None)[source]¶
Bases:
objectResult from OAuth callback.
- class kelvin.sdk.services.auth.callback_server.CallbackHandler(request, client_address, server)[source]¶
Bases:
BaseHTTPRequestHandlerHTTP request handler for OAuth callbacks.
- class kelvin.sdk.services.auth.callback_server.CallbackServer(port=8080)[source]¶
Bases:
objectLocal HTTP server for handling OAuth callbacks.
- Parameters:
port (int)
- __init__(port=8080)[source]¶
Initialize the callback server.
- Parameters:
port (
int) – The port to listen on.- Return type:
None
- server: HTTPServer | None¶
- get_callback_url()[source]¶
Get the callback URL for the OAuth flow.
- Return type:
- Returns:
The callback URL.
- start()[source]¶
Start the callback server.
- Raises:
OAuthFlowError – If the server fails to start.
- Return type:
- wait_for_callback(timeout=120.0)[source]¶
Wait for the OAuth callback.
- Parameters:
timeout (
float) – Maximum time to wait in seconds.- Return type:
- Returns:
CallbackResult with the authorization code and state.
- Raises:
OAuthFlowError – If timeout or error occurs.
Auth Errors¶
Authentication-specific exception hierarchy.
Authentication service errors.
- exception kelvin.sdk.services.auth.errors.AuthServiceError(message=None, exit_code=None)[source]¶
Bases:
AuthenticationErrorBase exception for AuthService errors.
- exception kelvin.sdk.services.auth.errors.KeycloakError(message=None, exit_code=None)[source]¶
Bases:
AuthServiceErrorRaised when there is an error related to Keycloak operations.
- exception kelvin.sdk.services.auth.errors.OAuthFlowError(message=None, exit_code=None)[source]¶
Bases:
AuthServiceErrorRaised when there is an error during the OAuth flow.
Keycloak Client¶
Keycloak OAuth/OIDC client implementation.
Keycloak OAuth/OIDC client implementation.
- class kelvin.sdk.services.auth.keycloak_client.KeycloakClient(base_url, realm, client_id, client_secret=None)[source]¶
Bases:
objectClient for interacting with Keycloak OAuth/OIDC endpoints.
- get_oidc_configuration()[source]¶
Fetch the OIDC configuration from Keycloak’s well-known endpoint.
- Return type:
- Returns:
Dictionary containing OIDC configuration.
- Raises:
KeycloakError – If the request fails.
- get_authorization_endpoint()[source]¶
Get the authorization endpoint URL.
- Return type:
- Returns:
Authorization endpoint URL.
- Raises:
KeycloakError – If endpoint is not found.
- get_token_endpoint()[source]¶
Get the token endpoint URL.
- Return type:
- Returns:
Token endpoint URL.
- Raises:
KeycloakError – If endpoint is not found.
- get_userinfo_endpoint()[source]¶
Get the userinfo endpoint URL.
- Return type:
- Returns:
Userinfo endpoint URL.
- Raises:
KeycloakError – If endpoint is not found.
- get_logout_endpoint()[source]¶
Get the logout endpoint URL.
- Return type:
- Returns:
Logout endpoint URL.
- Raises:
KeycloakError – If endpoint is not found.
- get_revocation_endpoint()[source]¶
Get the token revocation endpoint URL (RFC 7009).
Tries
revocation_endpointfrom OIDC config first, then falls back to the standard Keycloak path.- Return type:
- Returns:
Revocation endpoint URL.
- exchange_code_for_tokens(code, redirect_uri, code_verifier)[source]¶
Exchange an authorization code for access and refresh tokens.
- Parameters:
- Return type:
- Returns:
TokenResponse containing access and refresh tokens.
- Raises:
KeycloakError – If the token exchange fails.
- authenticate_with_password(username, password, totp=None)[source]¶
Authenticate using Resource Owner Password Credentials Grant.
- Parameters:
- Return type:
- Returns:
TokenResponse containing access and refresh tokens.
- Raises:
KeycloakError – If the authentication fails.
- authenticate_with_client_credentials(client_id, client_secret)[source]¶
Authenticate using Client Credentials Grant.
- Parameters:
- Return type:
- Returns:
TokenResponse containing access token (no refresh token for this grant).
- Raises:
KeycloakError – If the authentication fails.
- refresh_access_token(refresh_token)[source]¶
Refresh the access token using a refresh token.
- Parameters:
refresh_token (
str) – The refresh token.- Return type:
- Returns:
TokenResponse containing new tokens.
- Raises:
KeycloakError – If the token refresh fails.
- get_user_info(access_token)[source]¶
Fetch user information from Keycloak’s userinfo endpoint.
- Parameters:
access_token (
str) – The access token.- Return type:
- Returns:
Dictionary containing user information.
- Raises:
KeycloakError – If the request fails.
- logout(refresh_token)[source]¶
Logout by revoking the refresh token (RFC 7009).
- Parameters:
refresh_token (
str) – The refresh token to revoke.- Raises:
KeycloakError – If the revocation fails.
- Return type:
Auth Models¶
Authentication data models.
Authentication models.
PKCE Utilities¶
PKCE (Proof Key for Code Exchange) utilities for OAuth 2.0.
PKCE (Proof Key for Code Exchange) utilities for OAuth 2.0.
- kelvin.sdk.services.auth.pkce.generate_code_verifier()[source]¶
Generate a cryptographically random code verifier for PKCE.
- Return type:
- Returns:
A URL-safe base64 encoded random string (43 characters).
- kelvin.sdk.services.auth.pkce.generate_code_challenge(code_verifier)[source]¶
Generate a code challenge from a code verifier for PKCE.
- kelvin.sdk.services.auth.pkce.generate_state()[source]¶
Generate a random state parameter for CSRF protection.
- Return type:
- Returns:
A random URL-safe string.
- kelvin.sdk.services.auth.pkce.build_authorization_url(authorization_endpoint, client_id, redirect_uri, code_challenge, state, scope='openid profile email')[source]¶
Build the OAuth authorization URL with PKCE parameters.
- Parameters:
authorization_endpoint (
str) – The Keycloak authorization endpoint.client_id (
str) – The OAuth client ID.redirect_uri (
str) – The redirect URI (callback URL).code_challenge (
str) – The PKCE code challenge.state (
str) – The state parameter for CSRF protection.scope (
str) – The OAuth scopes to request.
- Return type:
- Returns:
The complete authorization URL.