KarlContainerBuilder

A fluent builder implementation for configuring and instantiating KarlContainer instances.

This builder implements the Builder pattern to provide a type-safe, fluent API for constructing properly configured KarlContainer instances. It enforces dependency injection principles by requiring all essential dependencies before allowing container creation, preventing runtime errors and ensuring proper initialization.

Architecture benefits:

  • Compile-time safety through required dependency validation

  • Immutable configuration once built

  • Clear separation of concerns between configuration and execution

  • Extensible design for future configuration options

Required dependencies:

  1. LearningEngine: Handles AI model training, inference, and state management

    • Typically provided by specialized modules (e.g., karl-kldl for KotlinDL integration)

    • Must support incremental learning and model serialization/deserialization

    • Responsible for feature extraction and prediction generation

  2. DataStorage: Manages persistent storage of container state and interaction data

    • Usually implemented using database abstractions (e.g., karl-room, karl-sqldelight)

    • Handles encryption, data versioning, and migration strategies

    • Provides thread-safe operations for concurrent access scenarios

  3. DataSource: Application-specific component that feeds interaction data to KARL

    • Implemented by the client application to capture user behaviors

    • Converts application events into standardized InteractionData format

    • Manages event filtering and rate limiting to prevent data overflow

  4. CoroutineScope: Manages asynchronous operations and lifecycle

    • Should be tied to appropriate application lifecycle (Activity, ViewModel, etc.)

    • Used for background learning, periodic state saves, and cleanup operations

    • Ensures proper resource management and graceful shutdown

Optional configurations:

  • Instructions: User-defined rules and preferences that influence learning behavior

  • Can be updated dynamically after container creation

  • Used for customizing prediction algorithms and filtering strategies

  • Supports both programmatic and natural language instruction formats

Container lifecycle after build():

  1. Call initialize() to load existing state and start background processes

  2. Container begins observing DataSource for new interaction events

  3. Learning engine processes events and updates model incrementally

  4. Call predict() to get AI-generated suggestions and insights

  5. Call saveState() periodically or before shutdown

  6. Call release() to clean up resources and stop background operations

Thread safety:

  • Builder itself is not thread-safe and should be used from single thread

  • Built container instances are designed for concurrent access

  • All dependency implementations must be thread-safe

Error handling:

  • Build-time validation prevents common configuration errors

  • Runtime errors in dependencies are propagated through container methods

  • Failed initialization attempts can be retried with different configurations

Parameters

userId

The unique user identifier that this builder will configure the container for. This identifier is immutable after builder creation and determines data isolation.

See also

for the recommended way to create builder instances

for detailed container operation documentation

for learning algorithm integration patterns

for persistence strategy implementation

for application event integration patterns

Functions

Link copied to clipboard

Validates configuration and constructs a new KarlContainer instance.

Link copied to clipboard
fun withCoroutineScope(scope: <Error class: unknown class>): KarlContainerBuilder

Configures the coroutine scope that manages KARL's asynchronous operations.

Link copied to clipboard

Configures the data source that feeds user interaction events into the learning system.

Link copied to clipboard

Configures the data storage implementation for persistent state and interaction history.

Link copied to clipboard

Configures optional user-defined instructions that influence learning behavior.

Link copied to clipboard

Configures the learning engine implementation that will power the AI capabilities.