{"version":3,"file":"Agent.mjs","names":[],"sources":["../../src/agent/Agent.ts"],"sourcesContent":["import { Subject } from 'rxjs'\nimport { InjectionSymbols } from '../constants'\nimport { JwsService } from '../crypto/JwsService'\nimport { CredoError } from '../error'\nimport { DependencyManager } from '../plugins'\nimport { StorageUpdateService, StorageVersionRepository, UpdateAssistant } from '../storage'\nimport type { InitConfig } from '../types'\nimport { AgentConfig } from './AgentConfig'\nimport type { AgentDependencies } from './AgentDependencies'\nimport type { AgentModulesInput } from './AgentModules'\nimport { extendModulesWithDefaultModules } from './AgentModules'\nimport { BaseAgent } from './BaseAgent'\nimport { AgentContext, DefaultAgentContextProvider } from './context'\nimport { EventEmitter } from './EventEmitter'\n\ninterface AgentOptions<AgentModules extends AgentModulesInput> {\n  config?: InitConfig\n  modules?: AgentModules\n  dependencies: AgentDependencies\n}\n\n// Any makes sure you can use Agent as a type without always needing to specify the exact generics for the agent\n// biome-ignore lint/suspicious/noExplicitAny: no explanation\nexport class Agent<AgentModules extends AgentModulesInput = any> extends BaseAgent<AgentModules> {\n  public constructor(options: AgentOptions<AgentModules>, dependencyManager = new DependencyManager()) {\n    const agentConfig = new AgentConfig(options.config ?? {}, options.dependencies)\n    const modulesWithDefaultModules = extendModulesWithDefaultModules(options.modules)\n\n    // Register internal dependencies\n    dependencyManager.registerSingleton(EventEmitter)\n    dependencyManager.registerSingleton(JwsService)\n    dependencyManager.registerSingleton(StorageVersionRepository)\n    dependencyManager.registerSingleton(StorageUpdateService)\n\n    dependencyManager.registerInstance(AgentConfig, agentConfig)\n    dependencyManager.registerInstance(InjectionSymbols.AgentDependencies, agentConfig.agentDependencies)\n    dependencyManager.registerInstance(InjectionSymbols.Stop$, new Subject<boolean>())\n    dependencyManager.registerInstance(InjectionSymbols.FileSystem, new agentConfig.agentDependencies.FileSystem())\n\n    // Register all modules. This will also include the default modules\n    dependencyManager.registerModules(modulesWithDefaultModules)\n\n    if (!dependencyManager.isRegistered(InjectionSymbols.Logger)) {\n      dependencyManager.registerInstance(InjectionSymbols.Logger, agentConfig.logger)\n    }\n    if (!dependencyManager.isRegistered(InjectionSymbols.StorageService)) {\n      throw new CredoError(\n        \"Missing required dependency: 'StorageService'. You can register it using the AskarModule, DrizzleStorageModule, or implement your own.\"\n      )\n    }\n\n    // TODO: contextCorrelationId for base wallet\n    // Bind the default agent context to the container for use in modules etc.\n    dependencyManager.registerInstance(\n      AgentContext,\n      new AgentContext({\n        dependencyManager,\n        contextCorrelationId: 'default',\n        isRootAgentContext: true,\n      })\n    )\n\n    // If no agent context provider has been registered we use the default agent context provider.\n    if (!dependencyManager.isRegistered(InjectionSymbols.AgentContextProvider)) {\n      dependencyManager.registerSingleton(InjectionSymbols.AgentContextProvider, DefaultAgentContextProvider)\n    }\n\n    super(agentConfig, dependencyManager)\n  }\n\n  public get events() {\n    return this.eventEmitter\n  }\n\n  public async initialize() {\n    if (this._isInitialized) {\n      throw new CredoError(\n        'Agent already initialized. Currently it is not supported to re-initialize an already initialized agent.'\n      )\n    }\n\n    // We first initialize all the modules\n    await this.dependencyManager.initializeModules(this.agentContext)\n\n    // Then we initialize the root agent context\n    await this.dependencyManager.initializeAgentContext(this.agentContext)\n\n    // Make sure the storage is up to date\n    const storageUpdateService = this.dependencyManager.resolve(StorageUpdateService)\n    const isStorageUpToDate = await storageUpdateService.isUpToDate(this.agentContext)\n    this.logger.info(`Agent storage is ${isStorageUpToDate ? '' : 'not '}up to date.`)\n\n    if (!isStorageUpToDate && this.agentConfig.autoUpdateStorageOnStartup) {\n      const updateAssistant = new UpdateAssistant(this)\n\n      await updateAssistant.initialize()\n      await updateAssistant.update()\n    } else if (!isStorageUpToDate) {\n      const currentVersion = await storageUpdateService.getCurrentStorageVersion(this.agentContext)\n\n      // Close agent context to prevent un-initialized agent with initialized agent context\n      await this.dependencyManager.closeAgentContext(this.agentContext)\n\n      throw new CredoError(\n        // TODO: add link to where documentation on how to update can be found.\n        `Current agent storage is not up to date. To prevent the framework state from getting corrupted the agent initialization is aborted. Make sure to update the agent storage (currently at ${currentVersion}) to the latest version (${UpdateAssistant.frameworkStorageVersion}). You can also downgrade your version of Credo.`\n      )\n    }\n\n    this._isInitialized = true\n  }\n\n  public async shutdown() {\n    // TODO: replace stop$, should be replaced by module specific lifecycle methods\n    const stop$ = this.dependencyManager.resolve<Subject<boolean>>(InjectionSymbols.Stop$)\n    // All observables use takeUntil with the stop$ observable\n    // this means all observables will stop running if a value is emitted on this observable\n    stop$.next(true)\n\n    await this.dependencyManager.shutdownModules(this.agentContext)\n    await this.dependencyManager.closeAgentContext(this.agentContext)\n\n    this._isInitialized = false\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAuBA,IAAa,QAAb,cAAyE,UAAwB;CAC/F,AAAO,YAAY,SAAqC,oBAAoB,IAAI,mBAAmB,EAAE;EACnG,MAAM,cAAc,IAAI,YAAY,QAAQ,UAAU,EAAE,EAAE,QAAQ,aAAa;EAC/E,MAAM,4BAA4B,gCAAgC,QAAQ,QAAQ;AAGlF,oBAAkB,kBAAkB,aAAa;AACjD,oBAAkB,kBAAkB,WAAW;AAC/C,oBAAkB,kBAAkB,yBAAyB;AAC7D,oBAAkB,kBAAkB,qBAAqB;AAEzD,oBAAkB,iBAAiB,aAAa,YAAY;AAC5D,oBAAkB,iBAAiB,iBAAiB,mBAAmB,YAAY,kBAAkB;AACrG,oBAAkB,iBAAiB,iBAAiB,OAAO,IAAI,SAAkB,CAAC;AAClF,oBAAkB,iBAAiB,iBAAiB,YAAY,IAAI,YAAY,kBAAkB,YAAY,CAAC;AAG/G,oBAAkB,gBAAgB,0BAA0B;AAE5D,MAAI,CAAC,kBAAkB,aAAa,iBAAiB,OAAO,CAC1D,mBAAkB,iBAAiB,iBAAiB,QAAQ,YAAY,OAAO;AAEjF,MAAI,CAAC,kBAAkB,aAAa,iBAAiB,eAAe,CAClE,OAAM,IAAI,WACR,yIACD;AAKH,oBAAkB,iBAChB,cACA,IAAI,aAAa;GACf;GACA,sBAAsB;GACtB,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