@credo-ts/core
Version:
<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1
1 lines • 11.3 kB
Source Map (JSON)
{"version":3,"file":"DependencyManager.mjs","names":["rootContainer","container"],"sources":["../../src/plugins/DependencyManager.ts"],"sourcesContent":["import type { DependencyContainer } from 'tsyringe'\nimport { type InjectionToken, Lifecycle, container as rootContainer } from 'tsyringe'\nimport type { AgentContext } from '../agent'\nimport type { ModulesMap } from '../agent/AgentModules'\nimport { CredoError } from '../error'\nimport type { Constructor } from '../utils/mixins'\n\nexport type { InjectionToken }\n\nexport class DependencyManager {\n public readonly container: DependencyContainer\n public readonly registeredModules: ModulesMap\n\n /**\n * @internal\n */\n public constructor(\n container: DependencyContainer = rootContainer.createChildContainer(),\n registeredModules: ModulesMap = {}\n ) {\n this.container = container\n this.registeredModules = registeredModules\n }\n\n /**\n * @internal\n */\n public registerModules(modules: ModulesMap) {\n for (const [moduleKey, module] of Object.entries(modules)) {\n if (this.registeredModules[moduleKey]) {\n throw new CredoError(\n `Module with key ${moduleKey} has already been registered. Only a single module can be registered with the same key.`\n )\n }\n\n this.registeredModules[moduleKey] = module\n if (module.api) {\n this.registerContextScoped(module.api)\n }\n try {\n module.register(this)\n } catch (error) {\n throw new CredoError(`Cannot register ${moduleKey}: ${error}`)\n }\n }\n }\n\n /**\n * @internal\n */\n public async initializeModules(agentContext: AgentContext) {\n if (agentContext.dependencyManager.container !== this.container) {\n throw new CredoError(\n `Method 'initializeModule' called on DependencyManager different from the agent context for which 'initializeModule' is called. Make sure to call 'initializeModule' on the DependencyManager associated with the agent context.`\n )\n }\n\n for (const [moduleName, module] of Object.entries(this.registeredModules)) {\n try {\n await module.initialize?.(agentContext)\n } catch (error) {\n throw new CredoError(\n `Error during call to 'initialize' method in module '${moduleName}' for agent context '${agentContext.contextCorrelationId}'.`,\n { cause: error }\n )\n }\n }\n }\n\n /**\n * @internal\n */\n public async shutdownModules(agentContext: AgentContext) {\n if (agentContext.dependencyManager.container !== this.container) {\n throw new CredoError(\n `Method 'shutdownModules' called on DependencyManager different from the agent context for which 'shutdownModules' is called. Make sure to call 'shutdownModules' on the DependencyManager associated with the agent context.`\n )\n }\n\n for (const [moduleName, module] of Object.entries(this.registeredModules)) {\n try {\n await module.shutdown?.(agentContext)\n } catch (error) {\n throw new CredoError(\n `Error during call to 'shutdown' method in module '${moduleName}' for agent context '${agentContext.contextCorrelationId}'.`,\n { cause: error }\n )\n }\n }\n }\n\n /**\n * @internal\n */\n public async initializeAgentContext(agentContext: AgentContext) {\n if (agentContext.dependencyManager.container !== this.container) {\n throw new CredoError(\n `Method 'initializeAgentContext' called on DependencyManager different from the agent context for which 'initializeAgentContext' is called. Make sure to call 'initializeAgentContext' on the DependencyManager associated with the agent context.`\n )\n }\n\n for (const [moduleName, module] of Object.entries(this.registeredModules)) {\n try {\n await module.onInitializeContext?.(agentContext)\n } catch (error) {\n throw new CredoError(\n `Error during call to 'onInitializeContext' method in module '${moduleName}' for agent context '${agentContext.contextCorrelationId}'.`,\n { cause: error }\n )\n }\n }\n }\n\n /**\n * @internal\n */\n public async deleteAgentContext(agentContext: AgentContext) {\n if (agentContext.dependencyManager.container !== this.container) {\n throw new CredoError(\n `Method 'deleteAgentContext' called on DependencyManager different from the agent context for which 'deleteAgentContext' is called. Make sure to call 'deleteAgentContext' on the DependencyManager associated with the agent context.`\n )\n }\n\n try {\n for (const [moduleName, module] of Object.entries(this.registeredModules)) {\n try {\n await module.onDeleteContext?.(agentContext)\n } catch (error) {\n throw new CredoError(\n `Error during call to 'onDeleteContext' method in module '${moduleName}' for agent context '${agentContext.contextCorrelationId}'.`,\n { cause: error }\n )\n }\n }\n } finally {\n await this.container.dispose()\n }\n }\n\n /**\n * @internal\n */\n public async provisionAgentContext(agentContext: AgentContext) {\n if (agentContext.dependencyManager.container !== this.container) {\n throw new CredoError(\n `Method 'provisionAgentContext' called on DependencyManager different from the agent context for which 'provisionAgentContext' is called. Make sure to call 'provisionAgentContext' on the DependencyManager associated with the agent context.`\n )\n }\n\n for (const [moduleName, module] of Object.entries(this.registeredModules)) {\n try {\n await module.onProvisionContext?.(agentContext)\n } catch (error) {\n throw new CredoError(\n `Error during call to 'onProvisionContext' method in module '${moduleName}' for agent context '${agentContext.contextCorrelationId}'.`,\n { cause: error }\n )\n }\n }\n\n return agentContext\n }\n\n /**\n * @internal\n */\n public async closeAgentContext(agentContext: AgentContext) {\n if (agentContext.dependencyManager.container !== this.container) {\n throw new CredoError(\n `Method 'closeAgentContext' called on DependencyManager different from the agent context for which 'closeAgentContext' is called. Make sure to call 'closeAgentContext' on the DependencyManager associated with the agent context.`\n )\n }\n\n try {\n for (const [moduleName, module] of Object.entries(this.registeredModules)) {\n try {\n await module.onCloseContext?.(agentContext)\n } catch (error) {\n throw new CredoError(\n `Error during call to 'onCloseContext' method in module '${moduleName}' for agent context '${agentContext.contextCorrelationId}'.`,\n { cause: error }\n )\n }\n }\n } finally {\n // NOTE: we support reinitialization of the root agent so we can't dispose of the agent context\n if (!agentContext.isRootAgentContext) {\n await this.container.dispose()\n }\n }\n }\n\n public registerSingleton<T>(from: InjectionToken<T>, to: InjectionToken<T>): void\n public registerSingleton<T>(token: Constructor<T>): void\n // biome-ignore lint/suspicious/noExplicitAny: no explanation\n public registerSingleton<T = any>(fromOrToken: InjectionToken<T> | Constructor<T>, to?: any) {\n this.container.registerSingleton(fromOrToken, to)\n }\n\n public resolve<T>(token: InjectionToken<T>): T {\n return this.container.resolve(token)\n }\n\n public registerInstance<T>(token: InjectionToken<T>, instance: T) {\n this.container.registerInstance(token, instance)\n }\n\n public isRegistered<T>(token: InjectionToken<T>, recursive = false): boolean {\n return this.container.isRegistered(token, recursive)\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: no explanation\n public registerContextScoped<T = any>(token: Constructor<T>): void\n // biome-ignore lint/suspicious/noExplicitAny: no explanation\n public registerContextScoped<T = any>(token: InjectionToken<T>, provider: Constructor<T>): void\n\n // biome-ignore lint/suspicious/noExplicitAny: no explanation\n public registerContextScoped(token: any, provider?: any) {\n if (provider) this.container.register(token, provider, { lifecycle: Lifecycle.ContainerScoped })\n else this.container.register(token, token, { lifecycle: Lifecycle.ContainerScoped })\n }\n\n /**\n * @internal\n */\n public createChild() {\n return new DependencyManager(this.container.createChildContainer(), this.registeredModules)\n }\n}\n"],"mappings":";;;;;;;AASA,IAAa,oBAAb,MAAa,kBAAkB;;;;CAO7B,AAAO,YACL,cAAiCA,UAAc,sBAAsB,EACrE,oBAAgC,EAAE,EAClC;AACA,OAAK,YAAYC;AACjB,OAAK,oBAAoB;;;;;CAM3B,AAAO,gBAAgB,SAAqB;AAC1C,OAAK,MAAM,CAAC,WAAW,WAAW,OAAO,QAAQ,QAAQ,EAAE;AACzD,OAAI,KAAK,kBAAkB,WACzB,OAAM,IAAI,WACR,mBAAmB,UAAU,yFAC9B;AAGH,QAAK,kBAAkB,aAAa;AACpC,OAAI,OAAO,IACT,MAAK,sBAAsB,OAAO,IAAI;AAExC,OAAI;AACF,WAAO,SAAS,KAAK;YACd,OAAO;AACd,UAAM,IAAI,WAAW,mBAAmB,UAAU,IAAI,QAAQ;;;;;;;CAQpE,MAAa,kBAAkB,cAA4B;AACzD,MAAI,aAAa,kBAAkB,cAAc,KAAK,UACpD,OAAM,IAAI,WACR,kOACD;AAGH,OAAK,MAAM,CAAC,YAAY,WAAW,OAAO,QAAQ,KAAK,kBAAkB,CACvE,KAAI;AACF,SAAM,OAAO,aAAa,aAAa;WAChC,OAAO;AACd,SAAM,IAAI,WACR,uDAAuD,WAAW,uBAAuB,aAAa,qBAAqB,KAC3H,EAAE,OAAO,OAAO,CACjB;;;;;;CAQP,MAAa,gBAAgB,cAA4B;AACvD,MAAI,aAAa,kBAAkB,cAAc,KAAK,UACpD,OAAM,IAAI,WACR,+NACD;AAGH,OAAK,MAAM,CAAC,YAAY,WA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