

const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
const require_constants = require('../constants.js');
const require_AgentConfig = require('./AgentConfig.js');
const require_AgentContext = require('./context/AgentContext.js');
const require_CredoError = require('../error/CredoError.js');
require('../error/index.js');
const require_DependencyManager = require('../plugins/DependencyManager.js');
require('../plugins/index.js');
const require_DefaultAgentContextProvider = require('./context/DefaultAgentContextProvider.js');
require('./context/index.js');
const require_JwsService = require('../crypto/JwsService.js');
const require_EventEmitter = require('./EventEmitter.js');
const require_StorageVersionRepository = require('../storage/migration/repository/StorageVersionRepository.js');
const require_StorageUpdateService = require('../storage/migration/StorageUpdateService.js');
const require_UpdateAssistant = require('../storage/migration/UpdateAssistant.js');
require('../storage/index.js');
const require_AgentModules = require('./AgentModules.js');
const require_BaseAgent = require('./BaseAgent.js');
let rxjs = require("rxjs");

//#region src/agent/Agent.ts
var Agent = class extends require_BaseAgent.BaseAgent {
	constructor(options, dependencyManager = new require_DependencyManager.DependencyManager()) {
		const agentConfig = new require_AgentConfig.AgentConfig(options.config ?? {}, options.dependencies);
		const modulesWithDefaultModules = require_AgentModules.extendModulesWithDefaultModules(options.modules);
		dependencyManager.registerSingleton(require_EventEmitter.EventEmitter);
		dependencyManager.registerSingleton(require_JwsService.JwsService);
		dependencyManager.registerSingleton(require_StorageVersionRepository.StorageVersionRepository);
		dependencyManager.registerSingleton(require_StorageUpdateService.StorageUpdateService);
		dependencyManager.registerInstance(require_AgentConfig.AgentConfig, agentConfig);
		dependencyManager.registerInstance(require_constants.InjectionSymbols.AgentDependencies, agentConfig.agentDependencies);
		dependencyManager.registerInstance(require_constants.InjectionSymbols.Stop$, new rxjs.Subject());
		dependencyManager.registerInstance(require_constants.InjectionSymbols.FileSystem, new agentConfig.agentDependencies.FileSystem());
		dependencyManager.registerModules(modulesWithDefaultModules);
		if (!dependencyManager.isRegistered(require_constants.InjectionSymbols.Logger)) dependencyManager.registerInstance(require_constants.InjectionSymbols.Logger, agentConfig.logger);
		if (!dependencyManager.isRegistered(require_constants.InjectionSymbols.StorageService)) throw new require_CredoError.CredoError("Missing required dependency: 'StorageService'. You can register it using the AskarModule, DrizzleStorageModule, or implement your own.");
		dependencyManager.registerInstance(require_AgentContext.AgentContext, new require_AgentContext.AgentContext({
			dependencyManager,
			contextCorrelationId: "default",
			isRootAgentContext: true
		}));
		if (!dependencyManager.isRegistered(require_constants.InjectionSymbols.AgentContextProvider)) dependencyManager.registerSingleton(require_constants.InjectionSymbols.AgentContextProvider, require_DefaultAgentContextProvider.DefaultAgentContextProvider);
		super(agentConfig, dependencyManager);
	}
	get events() {
		return this.eventEmitter;
	}
	async initialize() {
		if (this._isInitialized) throw new require_CredoError.CredoError("Agent already initialized. Currently it is not supported to re-initialize an already initialized agent.");
		await this.dependencyManager.initializeModules(this.agentContext);
		await this.dependencyManager.initializeAgentContext(this.agentContext);
		const storageUpdateService = this.dependencyManager.resolve(require_StorageUpdateService.StorageUpdateService);
		const isStorageUpToDate = await storageUpdateService.isUpToDate(this.agentContext);
		this.logger.info(`Agent storage is ${isStorageUpToDate ? "" : "not "}up to date.`);
		if (!isStorageUpToDate && this.agentConfig.autoUpdateStorageOnStartup) {
			const updateAssistant = new require_UpdateAssistant.UpdateAssistant(this);
			await updateAssistant.initialize();
			await updateAssistant.update();
		} else if (!isStorageUpToDate) {
			const currentVersion = await storageUpdateService.getCurrentStorageVersion(this.agentContext);
			await this.dependencyManager.closeAgentContext(this.agentContext);
			throw new require_CredoError.CredoError(`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 (${require_UpdateAssistant.UpdateAssistant.frameworkStorageVersion}). You can also downgrade your version of Credo.`);
		}
		this._isInitialized = true;
	}
	async shutdown() {
		this.dependencyManager.resolve(require_constants.InjectionSymbols.Stop$).next(true);
		await this.dependencyManager.shutdownModules(this.agentContext);
		await this.dependencyManager.closeAgentContext(this.agentContext);
		this._isInitialized = false;
	}
};

//#endregion
exports.Agent = Agent;