openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
75 lines (74 loc) • 2.7 kB
JavaScript
import { st as supportsNodeSqliteExtensionLoading } from "../../sqlite-database-admission-DQIqro3d.mjs";
import { i as ensureSqliteLibrarySelected } from "../../bun-sqlite-library-BvzuPYCx.mjs";
import { n as openNodeSqliteDatabase } from "../../node-sqlite-CU4puofF.mjs";
import { n as runSqliteVectorQuery } from "../../sqlite-vector-D8zMkMR6.mjs";
import { t as configureSqliteMaintenanceCache } from "../../sqlite-maintenance-cache-E5FQa1nK.mjs";
import { t as loadSqliteVecExtension } from "../../sqlite-vec-2t4h9sP9.mjs";
import { deserialize } from "node:v8";
//#region src/infra/database-engine/sqlite-vector.child.ts
const MAX_STDIN_BYTES = 1048576;
const MAX_STDOUT_BYTES = 2097152;
function isChildInput(e) {
if (!e || typeof e !== "object") return false;
const t = e;
return typeof t.databasePath === "string" && t.databasePath.length > 0 && (t.sqliteLibraryPath === void 0 || typeof t.sqliteLibraryPath === "string" && t.sqliteLibraryPath.trim().length > 0) && typeof t.request === "string";
}
async function run(e) {
ensureSqliteLibrarySelected({ explicitPath: e.sqliteLibraryPath });
const t = supportsNodeSqliteExtensionLoading();
const n = openNodeSqliteDatabase(e.databasePath, {
allowExtension: t,
readOnly: true
});
try {
n.exec("PRAGMA query_only = ON; PRAGMA busy_timeout = 5000");
configureSqliteMaintenanceCache(n);
if (t) {
if ((await loadSqliteVecExtension({
db: n,
extensionPath: e.extensionPath
})).ok) return runSqliteVectorQuery(n, deserialize(Buffer.from(e.request, "base64")));
}
return {
rows: [],
fallbackScanRequired: true
};
} finally {
n.close();
}
}
function writeResult(e) {
let t = Buffer.from(JSON.stringify(e), "utf8");
if (t.byteLength > MAX_STDOUT_BYTES) t = Buffer.from(JSON.stringify({
status: "failed",
error: "memory vector KNN child result is too large"
}), "utf8");
process.stdout.write(Buffer.concat([t, Buffer.from("\n")]));
}
const chunks = [];
let inputBytes = 0;
for await (const e of process.stdin) {
inputBytes += e.byteLength;
const t = e.indexOf(10);
if (inputBytes > MAX_STDIN_BYTES + (t >= 0 ? 1 : 0)) throw new Error("memory vector KNN child input is too large");
chunks.push(e);
if (t < 0) continue;
if (t !== e.length - 1) throw new Error("invalid memory vector KNN child framing");
const n = JSON.parse(Buffer.concat(chunks).toString("utf8"));
chunks.length = 0;
inputBytes = 0;
if (!isChildInput(n)) throw new Error("invalid memory vector KNN child input");
try {
writeResult({
status: "ok",
value: await run(n)
});
} catch (e) {
writeResult({
status: "failed",
error: e instanceof Error ? e.message : String(e)
});
}
}
//#endregion
export {};