import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
//#region src/agent/memory/manager.ts
init_logger();
const log = createLogger("MemoryManager");
var MemoryManager = class {
	providers = [];
	toolToProvider = /* @__PURE__ */ new Map();
	hasExternal = false;
	addProvider(provider) {
		if (!(provider.name === "builtin")) {
			if (this.hasExternal) {
				const existing = this.providers.find((p) => p.name !== "builtin")?.name ?? "unknown";
				log.warn({
					rejected: provider.name,
					existing
				}, "Rejected memory provider — only one external provider allowed");
				return;
			}
			this.hasExternal = true;
		}
		this.providers.push(provider);
		for (const schema of provider.getToolSchemas()) {
			const toolName = schema.name;
			if (!toolName) continue;
			if (!this.toolToProvider.has(toolName)) this.toolToProvider.set(toolName, provider);
			else log.warn({
				toolName,
				existing: this.toolToProvider.get(toolName)?.name,
				ignored: provider.name
			}, "Memory tool name conflict");
		}
		log.info({
			name: provider.name,
			toolCount: provider.getToolSchemas().length
		}, "Memory provider registered");
	}
	get providersList() {
		return [...this.providers];
	}
	/**
	* Non-builtin static instructions (builtin uses curated snapshot in system prompt builder).
	*/
	buildExternalSystemPrompt() {
		const blocks = [];
		for (const p of this.providers) {
			if (p.name === "builtin") continue;
			try {
				const block = p.systemPromptBlock();
				if (block?.trim()) blocks.push(block.trim());
			} catch (err) {
				log.warn({
					err,
					name: p.name
				}, "systemPromptBlock failed");
			}
		}
		return blocks.join("\n\n");
	}
	async prefetchAll(query, options) {
		const parts = [];
		for (const p of this.providers) try {
			const v = await p.prefetch(query, options);
			if (v?.trim()) parts.push(v.trim());
		} catch (err) {
			log.debug({
				err,
				name: p.name
			}, "prefetch failed (non-fatal)");
		}
		return parts.join("\n\n");
	}
	queuePrefetchAll(query, options) {
		for (const p of this.providers) try {
			p.queuePrefetch(query, options);
		} catch (err) {
			log.debug({
				err,
				name: p.name
			}, "queuePrefetch failed (non-fatal)");
		}
	}
	syncAll(userContent, assistantContent, options) {
		for (const p of this.providers) try {
			p.syncTurn(userContent, assistantContent, options);
		} catch (err) {
			log.warn({
				err,
				name: p.name
			}, "syncTurn failed");
		}
	}
	getAdditionalTools() {
		const out = [];
		const seen = /* @__PURE__ */ new Set();
		for (const p of this.providers) {
			if (p.name === "builtin") continue;
			try {
				for (const t of p.getToolSchemas()) if (t.name && !seen.has(t.name)) {
					seen.add(t.name);
					out.push(t);
				}
			} catch (err) {
				log.warn({
					err,
					name: p.name
				}, "getToolSchemas failed");
			}
		}
		return out;
	}
	async handleToolCall(toolName, args) {
		const provider = this.toolToProvider.get(toolName);
		if (!provider) throw new Error(`No memory provider handles tool '${toolName}'`);
		return provider.handleToolCall(toolName, args);
	}
	onMemoryWrite(action, target, content) {
		for (const p of this.providers) {
			if (p.name === "builtin") continue;
			try {
				p.onMemoryWrite?.(action, target, content);
			} catch (err) {
				log.debug({
					err,
					name: p.name
				}, "onMemoryWrite failed");
			}
		}
	}
	async initializeAll(sessionId, options) {
		for (const p of this.providers) try {
			await p.initialize(sessionId, options);
		} catch (err) {
			log.warn({
				err,
				name: p.name
			}, "initialize failed");
		}
	}
	async shutdownAll() {
		for (const p of [...this.providers].reverse()) try {
			await p.shutdown();
		} catch (err) {
			log.warn({
				err,
				name: p.name
			}, "shutdown failed");
		}
	}
};
//#endregion
export { MemoryManager };

//# sourceMappingURL=manager.js.map