services
Service Container for Dependency Injection
Provides a minimal, type-safe service container for injecting optional services into agents without hardcoding dependencies.
Structs
Section titled “Structs”ServiceContainer
Section titled “ServiceContainer”Minimal service container for dependency injection
Allows agents to accept optional services through clean dependency injection without hardcoding specific service fields in the Agent struct.
Examples
Section titled “Examples”use agent_sdk::services::ServiceContainer;use std::sync::Arc;
#[derive(Clone)]struct MyService { name: String,}
let mut container = ServiceContainer::new();container.register(MyService { name: "test".to_string() });
let service: Option<Arc<MyService>> = container.get();assert!(service.is_some());Methods
fn new() -> SelfCreate a new empty service container
register
Section titled “register”fn register<T>(&mut self, service: T)Register a service in the container
Services are stored by their type and can be retrieved later using the same type signature.
Arguments
Section titled “Arguments”service- The service instance to register
Examples
Section titled “Examples”# use agent_sdk::services::ServiceContainer;# #[derive(Clone)]# struct DatabaseService;let mut container = ServiceContainer::new();container.register(DatabaseService);fn get<T>(&self) -> Option<Arc<T>>Get a service from the container
Returns an Arc-wrapped service if found, or None if the service type is not registered.
Returns
Section titled “Returns”Some(Arc<T>)- The service if foundNone- If no service of type T is registered
Examples
Section titled “Examples”# use agent_sdk::services::ServiceContainer;# use std::sync::Arc;# #[derive(Clone)]# struct DatabaseService { pub name: String }# let mut container = ServiceContainer::new();# container.register(DatabaseService { name: "test".to_string() });let service: Option<Arc<DatabaseService>> = container.get();if let Some(db) = service { println!("Database: {}", db.name);}fn has<T>(&self) -> boolCheck if a service type is registered
Returns
Section titled “Returns”true- If a service of type T is registeredfalse- If no service of type T is found
Examples
Section titled “Examples”# use agent_sdk::services::ServiceContainer;# #[derive(Clone)]# struct CacheService;let container = ServiceContainer::new();assert!(!container.has::<CacheService>());fn len(&self) -> usizeGet the number of registered services
is_empty
Section titled “is_empty”fn is_empty(&self) -> boolCheck if the container is empty
fn clear(&mut self)Remove all services from the container