UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

4 lines • 2.35 kB
import{ht as supportsNodeSqliteExtensionLoading}from"../../sqlite-database-admission-B3KDkUt-.mjs";import{i as ensureSqliteLibrarySelected}from"../../bun-sqlite-library-YO4xsZt9.mjs";import{n as openNodeSqliteDatabase}from"../../node-sqlite-DXyF1fBG.mjs";import{n as runSqliteVectorQuery}from"../../sqlite-vector-zlSCsLxB.mjs";import{t as configureSqliteMaintenanceCache}from"../../sqlite-maintenance-cache-C2QXuBdj.mjs";import{t as loadSqliteVecExtension}from"../../sqlite-vec-D-Kq5nmv.mjs";import{deserialize}from"node:v8"; //!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(` `)]))}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)})}}export{};