import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { resolveBuiltinMemoryStoreConfig } from "../memory/memory-config.js";
import { BuiltinMemoryStore } from "../memory/builtin-memory-store.js";
import { createMemoryManagerFromConfig } from "../memory/create-memory-manager.js";
import { SkillManager } from "../skills/skill-manager.js";
import { SystemPromptBuilder } from "../prompt/service-prompt-builder.js";
//#region src/agent/workspace-runtime/registry.ts
init_logger();
const log = createLogger("WorkspaceRuntimeRegistry");
var WorkspaceRuntimeRegistry = class {
	runtimes = /* @__PURE__ */ new Map();
	getConfig;
	bundledSkillsDir;
	constructor(opts) {
		this.getConfig = opts.getConfig;
		this.bundledSkillsDir = opts.bundledSkillsDir;
	}
	/** Lazily construct (and cache) the runtime for a resolved workspace path. */
	getOrCreate(resolvedPath) {
		const existing = this.runtimes.get(resolvedPath);
		if (existing) return existing;
		const cfg = this.getConfig();
		const builtinMemoryStore = new BuiltinMemoryStore(resolveBuiltinMemoryStoreConfig(resolvedPath, cfg));
		const memoryManager = createMemoryManagerFromConfig(resolvedPath, builtinMemoryStore, cfg);
		const skillManager = new SkillManager(resolvedPath, this.bundledSkillsDir);
		const rt = {
			skillManager,
			systemPromptBuilder: new SystemPromptBuilder({
				workspace: resolvedPath,
				config: cfg,
				skillManager
			}),
			builtinMemoryStore,
			memoryManager
		};
		this.runtimes.set(resolvedPath, rt);
		return rt;
	}
	/** Iterate every live workspace runtime (used by skill / config hot-reload). */
	values() {
		return this.runtimes.values();
	}
	/**
	* Tear down every runtime (shutdown memory providers) and forget the cache.
	* Used by `AgentManager.updateAgentDefaults` and `AgentManager.dispose`.
	*/
	async clearAll() {
		const toShutdown = [...this.runtimes.values()];
		this.runtimes.clear();
		await Promise.allSettled(toShutdown.map((rt) => rt.memoryManager.shutdownAll().catch((err) => {
			log.warn({ err }, "memoryManager.shutdownAll failed");
		})));
	}
};
//#endregion
export { WorkspaceRuntimeRegistry };

//# sourceMappingURL=registry.js.map