{"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    agentContext.isInitialized = true\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      agentContext.isInitialized = false\n\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,YACE,cAAiCA,UAAc,qBAAqB,GACpE,oBAAgC,CAAC,GACjC;EACA,KAAK,YAAYC;EACjB,KAAK,oBAAoB;CAC3B;;;;CAKA,gBAAuB,SAAqB;EAC1C,KAAK,MAAM,CAAC,WAAW,WAAW,OAAO,QAAQ,OAAO,GAAG;GACzD,IAAI,KAAK,kBAAkB,YACzB,MAAM,IAAI,WACR,mBAAmB,UAAU,wFAC/B;GAGF,KAAK,kBAAkB,aAAa;GACpC,IAAI,OAAO,KACT,KAAK,sBAAsB,OAAO,GAAG;GAEvC,IAAI;IACF,OAAO,SAAS,IAAI;GACtB,SAAS,OAAO;IACd,MAAM,IAAI,WAAW,mBAAmB,UAAU,IAAI,OAAO;GAC/D;EACF;CACF;;;;CAKA,MAAa,kBAAkB,cAA4B;EACzD,IAAI,aAAa,kBAAkB,cAAc,KAAK,WACpD,MAAM,IAAI,WACR,iOACF;EAGF,KAAK,MAAM,CAAC,YAAY,WAAW,OAAO,QAAQ,KAAK,iBAAiB,GACtE,IAAI;GACF,MAAM,OAAO,aAAa,YAAY;EACxC,SAAS,OAAO;GACd,MAAM,IAAI,WACR,uDAAuD,WAAW,uBAAuB,aAAa,qBAAqB,KAC3H,EAAE,OAAO,MAAM,CACjB;EACF;CAEJ;;;;CAKA,MAAa,gBA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