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:
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
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
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
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():
Call
initialize()
to load existing state and start background processesContainer begins observing DataSource for new interaction events
Learning engine processes events and updates model incrementally
Call
predict()
to get AI-generated suggestions and insightsCall
saveState()
periodically or before shutdownCall
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
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
Validates configuration and constructs a new KarlContainer instance.
Configures the coroutine scope that manages KARL's asynchronous operations.
Configures the data source that feeds user interaction events into the learning system.
Configures the data storage implementation for persistent state and interaction history.
Configures optional user-defined instructions that influence learning behavior.
Configures the learning engine implementation that will power the AI capabilities.