{"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,YAAmB,SAAqC,oBAAoB,IAAI,kBAAkB,GAAG;EACnG,MAAM,cAAc,IAAI,YAAY,QAAQ,UAAU,CAAC,GAAG,QAAQ,YAAY;EAC9E,MAAM,4BAA4B,gCAAgC,QAAQ,OAAO;EAGjF,kBAAkB,kBAAkB,YAAY;EAChD,kBAAkB,kBAAkB,UAAU;EAC9C,kBAAkB,kBAAkB,wBAAwB;EAC5D,kBAAkB,kBAAkB,oBAAoB;EAExD,kBAAkB,iBAAiB,aAAa,WAAW;EAC3D,kBAAkB,iBAAiB,iBAAiB,mBAAmB,YAAY,iBAAiB;EACpG,kBAAkB,iBAAiB,iBAAiB,OAAO,IAAI,QAAiB,CAAC;EACjF,kBAAkB,iBAAiB,iBAAiB,YAAY,IAAI,YAAY,kBAAkB,WAAW,CAAC;EAG9G,kBAAkB,gBAAgB,yBAAyB;EAE3D,IAAI,CAAC,kBAAkB,aAAa,iBAAiB,MAAM,GACzD,kBAAkB,iBAAiB,iBAAiB,QAAQ,YAAY,MAAM;EAEhF,IAAI,CAAC,kBAAkB,aAAa,iBAAiB,cAAc,GACjE,MAAM,IAAI,WACR,wIACF;EAKF,kBAAkB,iBAChB,cACA,IAAI,aAAa;GACf;GACA,sBAAsB;GACtB,oBAAoB;EACt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