import { i as readInteractions } from "./interactions-writer-B8XAzdqR.js";
import { t as readPipeline } from "./pipeline-writer-CIllfnZl.js";
import { t as hybridSearch } from "./hybrid-search-BmHttLrR.js";
import { n as loadMemories } from "./memory-Dzr9dXSM.js";
import { r as loadSops } from "./sop-BV7ICAFR.js";
//#region src/core/ask.ts
async function gatherCorpus(dataDir, slug) {
	const docs = [];
	for (const m of loadMemories(dataDir, slug)) docs.push({
		id: `mem:${m.id}`,
		text: m.text
	});
	for (const s of loadSops(dataDir, slug)) docs.push({
		id: `sop:${s.id}`,
		text: `${s.title} ${s.triggers.join(" ")} ${s.body}`
	});
	if (slug) {
		(await readInteractions(dataDir, slug).catch(() => "")).split(/(?=^## )/m).map((e) => e.trim()).filter((e) => e && !e.startsWith("# ")).forEach((e, i) => docs.push({
			id: `int:${slug}:${i}`,
			text: e
		}));
		const deals = await readPipeline(dataDir, slug).catch(() => []);
		for (const d of deals) docs.push({
			id: `deal:${d.name}`,
			text: `${d.name} stage ${d.stage} value ${d.value ?? ""} ${d.notes ?? ""}`
		});
	}
	return docs;
}
async function askCrm(dataDir, question, slug) {
	const corpus = await gatherCorpus(dataDir, slug);
	const ranked = hybridSearch(question, corpus, { limit: 6 });
	const byId = new Map(corpus.map((d) => [d.id, d]));
	const sources = ranked.map((r) => byId.get(r.id)).filter(Boolean);
	if (sources.length === 0) return { sources: [] };
	try {
		const { callLlm } = await import("./llm-DSX1-wFu.js");
		return {
			answer: await callLlm(`Answer the question using ONLY the context. Cite snippet numbers. If unknown, say so.\n\nQuestion: ${question}\n\nContext:\n${sources.map((s, i) => `[${i + 1}] ${s.text}`).join("\n")}`, {
				tool: "ask_crm",
				...slug ? { slug } : {}
			}),
			sources
		};
	} catch {
		return { sources };
	}
}
//#endregion
export { askCrm };

//# sourceMappingURL=ask-CDysGnRg.js.map