import { n as logger } from "./logger-Dyl4VcLO.js";
import path from "path";
import os from "os";
import * as lancedb from "@lancedb/lancedb";
import { Index, makeArrowTable } from "@lancedb/lancedb";
import { Field, FixedSizeList, Float32, Schema, Utf8 } from "apache-arrow";
import { env, pipeline } from "@huggingface/transformers";
//#region src/core/embedder.ts
env.cacheDir = process.env["HF_CACHE_DIR"] ?? path.join(os.homedir(), ".cache", "datasynx-opencrm", "models");
var EmbeddingPipeline = class {
	static instance = null;
	static get() {
		if (!this.instance) {
			console.error("Loading embedding model (first time, ~25MB)...");
			this.instance = pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
		}
		return this.instance;
	}
	static reset() {
		this.instance = null;
	}
};
async function embedText(text) {
	return (await (await EmbeddingPipeline.get())(text, {
		pooling: "mean",
		normalize: true
	}))[0]?.data ?? new Float32Array(384);
}
//#endregion
//#region src/core/lancedb.ts
let _db = null;
async function getDb(dataDir) {
	if (!_db) {
		const dbPath = path.join(dataDir, ".agentic", "lancedb");
		_db = await lancedb.connect(dbPath);
	}
	return _db;
}
const CUSTOMER_TABLE_SCHEMA = new Schema([
	new Field("source_ref", new Utf8(), false),
	new Field("text", new Utf8(), false),
	new Field("date", new Utf8(), false),
	new Field("type", new Utf8(), false),
	new Field("vector", new FixedSizeList(384, new Field("item", new Float32(), true)), false)
]);
async function getOrCreateCustomerTable(db, tableName) {
	if (!(await db.tableNames()).includes(tableName)) {
		const table = await db.createEmptyTable(tableName, CUSTOMER_TABLE_SCHEMA);
		await table.createIndex("source_ref", { config: Index.btree() });
		return table;
	}
	return db.openTable(tableName);
}
async function indexInLanceDB(dataDir, slug, text, sourceRef, meta) {
	try {
		const vectorFloat32 = await embedText(text);
		const table = await getOrCreateCustomerTable(await getDb(dataDir), `docs_${slug.replace(/[^a-z0-9]/gi, "_")}`);
		const date = meta?.date ?? (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
		const type = meta?.type ?? "unknown";
		const data = makeArrowTable([{
			source_ref: sourceRef,
			text: text.slice(0, 2e3),
			date,
			type,
			vector: Array.from(vectorFloat32)
		}]);
		await table.mergeInsert("source_ref").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(data);
	} catch (err) {
		logger.error("lancedb", "indexInLanceDB failed", { error: err.message });
	}
}
async function dropCustomerTable(dataDir, slug) {
	try {
		const db = await getDb(dataDir);
		const tableName = `docs_${slug.replace(/[^a-z0-9]/gi, "_")}`;
		if ((await db.tableNames()).includes(tableName)) await db.dropTable(tableName);
	} catch (err) {
		logger.error("lancedb", "dropCustomerTable failed", { error: err.message });
	}
}
async function searchKnowledge(dataDir, slug, query, limit) {
	try {
		const vectorFloat32 = await embedText(query);
		const db = await getDb(dataDir);
		const tableName = `docs_${slug.replace(/[^a-z0-9]/gi, "_")}`;
		if (!(await db.tableNames()).includes(tableName)) return [];
		return (await (await db.openTable(tableName)).search(Array.from(vectorFloat32)).limit(limit).toArray()).map((r) => ({
			content: String(r["text"] ?? ""),
			score: typeof r["_distance"] === "number" ? 1 - r["_distance"] : 1,
			source: String(r["source_ref"] ?? "")
		}));
	} catch {
		return [];
	}
}
//#endregion
export { indexInLanceDB as n, searchKnowledge as r, dropCustomerTable as t };

//# sourceMappingURL=lancedb-CuHKNsNZ.js.map