mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-17 01:30:45 +00:00
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import type { DatabaseSync } from "node:sqlite";
|
|
import { formatErrorMessage } from "../../../../src/infra/errors.js";
|
|
import { normalizeOptionalString } from "../../../../src/shared/string-coerce.js";
|
|
|
|
type SqliteVecModule = {
|
|
getLoadablePath: () => string;
|
|
load: (db: DatabaseSync) => void;
|
|
};
|
|
|
|
const SQLITE_VEC_MODULE_ID = "sqlite-vec";
|
|
|
|
async function loadSqliteVecModule(): Promise<SqliteVecModule> {
|
|
return import(SQLITE_VEC_MODULE_ID) as Promise<SqliteVecModule>;
|
|
}
|
|
|
|
export async function loadSqliteVecExtension(params: {
|
|
db: DatabaseSync;
|
|
extensionPath?: string;
|
|
}): Promise<{ ok: boolean; extensionPath?: string; error?: string }> {
|
|
try {
|
|
const sqliteVec = await loadSqliteVecModule();
|
|
const resolvedPath = normalizeOptionalString(params.extensionPath);
|
|
const extensionPath = resolvedPath ?? sqliteVec.getLoadablePath();
|
|
|
|
params.db.enableLoadExtension(true);
|
|
if (resolvedPath) {
|
|
params.db.loadExtension(extensionPath);
|
|
} else {
|
|
sqliteVec.load(params.db);
|
|
}
|
|
|
|
return { ok: true, extensionPath };
|
|
} catch (err) {
|
|
const message = formatErrorMessage(err);
|
|
return { ok: false, error: message };
|
|
}
|
|
}
|