/** * Service Container - Lightweight Dependency Injection (D-006) * * A simple service registry that enables: * - Type-safe service registration and retrieval * - Factory-based lazy initialization * - Service lifecycle management (singleton vs transient) * - Test isolation via reset mechanism * * Usage: * ```ts * // Register a singleton service * container.registerSingleton('logger', () => createLogger()); * * // Register a transient service (new instance each time) * container.registerTransient('cache', () => new Cache()); * * // Get a service * const logger = container.get('logger'); * * // Reset for testing * container.reset(); * ``` */ /** * Service lifetime options */ export type ServiceLifetime = 'singleton' | 'transient'; /** * Service factory function type */ export type ServiceFactory = () => T; /** * Async service factory function type */ export type AsyncServiceFactory = () => Promise; /** * Service container configuration */ export interface ServiceContainerConfig { /** Enable debug logging */ debug?: boolean; /** Name for this container (useful for debugging multiple containers) */ name?: string; } /** * A lightweight service container for dependency injection. * * Features: * - Type-safe service registration and retrieval * - Singleton and transient lifetimes * - Lazy initialization via factory functions * - Test-friendly reset mechanism * - Tag-based service grouping * * @example * ```ts * const container = new ServiceContainer(); * * // Register services * container.registerSingleton('db', () => new Database()); * container.registerTransient('request', () => new Request()); * * // Get services * const db = container.get('db'); * ``` */ export declare class ServiceContainer { private readonly services; private readonly config; private initializationOrder; constructor(config?: ServiceContainerConfig); /** * Register a singleton service. * The factory is called once on first access, then cached. */ registerSingleton(name: string, factory: ServiceFactory, tags?: string[]): this; /** * Register an async singleton service. * The factory is called once on first access, then cached. */ registerSingletonAsync(name: string, factory: AsyncServiceFactory, tags?: string[]): this; /** * Register a transient service. * The factory is called on every access. */ registerTransient(name: string, factory: ServiceFactory, tags?: string[]): this; /** * Register an existing instance as a singleton. * Useful for externally created services. */ registerInstance(name: string, instance: T, tags?: string[]): this; /** * Get a service by name. * @throws Error if service is not found or is async */ get(name: string): T; /** * Get a service by name, or undefined if not found. */ getOptional(name: string): T | undefined; /** * Get an async service by name. */ getAsync(name: string): Promise; /** * Check if a service is registered. */ has(name: string): boolean; /** * Get all services with a specific tag. */ getByTag(tag: string): T[]; /** * Get all services with a specific tag (async). */ getByTagAsync(tag: string): Promise; /** * Unregister a service. */ unregister(name: string): boolean; /** * Reset all singleton instances. * Services remain registered but will be recreated on next access. * Useful for test isolation. */ resetInstances(): void; /** * Reset the entire container. * All registrations and instances are cleared. */ reset(): void; /** * Get the list of registered service names. */ getRegisteredServices(): string[]; /** * Get the initialization order of singleton services. */ getInitializationOrder(): string[]; /** * Get container statistics. */ getStats(): { totalServices: number; singletons: number; transients: number; initializedSingletons: number; asyncServices: number; }; /** * Resolve a synchronous service. */ private resolveSync; /** * Resolve an asynchronous service. */ private resolveAsync; /** * Validate service name. */ private validateName; } /** * Get the global service container. * Creates one if it doesn't exist. */ export declare function getServiceContainer(): ServiceContainer; /** * Set the global service container. * Useful for testing or custom configurations. */ export declare function setServiceContainer(container: ServiceContainer): void; /** * Reset the global service container. * Creates a fresh container instance. */ export declare function resetServiceContainer(): void; /** * Reset only the singleton instances in the global container. * Keeps service registrations intact. */ export declare function resetServiceInstances(): void; /** * Well-known service names for type-safe service access. * Use these instead of string literals. */ export declare const ServiceTokens: { readonly Logger: "logger"; readonly BrowserManager: "browserManager"; readonly SessionManager: "sessionManager"; readonly ContentExtractor: "contentExtractor"; readonly ApiAnalyzer: "apiAnalyzer"; readonly SmartBrowser: "smartBrowser"; readonly LearningEngine: "learningEngine"; readonly ProceduralMemory: "proceduralMemory"; readonly VectorStore: "vectorStore"; readonly EmbeddingProvider: "embeddingProvider"; readonly RateLimiter: "rateLimiter"; readonly PageCache: "pageCache"; readonly ApiCache: "apiCache"; readonly PerformanceTracker: "performanceTracker"; readonly HttpClient: "httpClient"; readonly TieredFetcher: "tieredFetcher"; readonly ContentIntelligence: "contentIntelligence"; readonly LightweightRenderer: "lightweightRenderer"; readonly FeedbackService: "feedbackService"; readonly WebhookService: "webhookService"; readonly VerificationEngine: "verificationEngine"; readonly DebugTraceRecorder: "debugTraceRecorder"; readonly WorkflowRecorder: "workflowRecorder"; readonly Config: "config"; }; export type ServiceToken = typeof ServiceTokens[keyof typeof ServiceTokens]; /** * Type-safe helper to get a service from the global container. */ export declare function getService(token: ServiceToken): T; /** * Type-safe helper to get an optional service from the global container. */ export declare function getOptionalService(token: ServiceToken): T | undefined; /** * Type-safe helper to get an async service from the global container. */ export declare function getServiceAsync(token: ServiceToken): Promise; /** * Type-safe helper to check if a service is registered. */ export declare function hasService(token: ServiceToken): boolean; //# sourceMappingURL=service-container.d.ts.map