import { r as writeJsonArray, t as readJsonArray } from "./json-store-WWsFzXub.js";
import { t as hybridSearch } from "./hybrid-search-BmHttLrR.js";
import path from "path";
import { randomBytes } from "crypto";
//#region src/core/memory.ts
function globalPath(dataDir) {
	return path.join(dataDir, ".agentic", "memory", "global.json");
}
function customerPath(dataDir, slug) {
	return path.join(dataDir, "customers", slug, "memory.json");
}
function readFile(p) {
	return readJsonArray(p, "memories");
}
function writeFile(p, memories) {
	writeJsonArray(p, "memories", memories);
}
function addMemory(dataDir, m) {
	const entry = {
		id: `mem_${randomBytes(5).toString("hex")}`,
		scope: m.scope,
		...m.slug ? { slug: m.slug } : {},
		type: m.type,
		text: m.text,
		...typeof m.confidence === "number" ? { confidence: m.confidence } : {},
		createdAt: (/* @__PURE__ */ new Date()).toISOString()
	};
	const p = m.scope === "global" ? globalPath(dataDir) : customerPath(dataDir, m.slug ?? "");
	writeFile(p, [...readFile(p), entry]);
	return entry;
}
/** Load memories: global always; plus the customer's own when a slug is given. */
function loadMemories(dataDir, slug) {
	const global = readFile(globalPath(dataDir));
	if (!slug) return global;
	return [...global, ...readFile(customerPath(dataDir, slug))];
}
/** Search memories by relevance (hybrid keyword ranking over memory texts). */
async function searchMemory(dataDir, query, slug, limit = 5) {
	const memories = loadMemories(dataDir, slug);
	const ranked = hybridSearch(query, memories.map((m) => ({
		id: m.id,
		text: m.text
	})), { limit });
	const byId = new Map(memories.map((m) => [m.id, m]));
	return ranked.map((r) => byId.get(r.id)).filter(Boolean);
}
//#endregion
export { loadMemories as n, searchMemory as r, addMemory as t };

//# sourceMappingURL=memory-Dzr9dXSM.js.map