UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

364 lines (363 loc) • 13.1 kB
import { S as resolveGlobalSingleton } from "./number-coercion-CmpLVzZ6.mjs"; import { createRequire } from "node:module"; import { existsSync } from "node:fs"; import { posix } from "node:path"; import { getEnvironmentData, isMainThread, setEnvironmentData } from "node:worker_threads"; //#region node-version.mjs const NODE_RELEASE_VERSION_RE = /^v?((?:0|[1-9]\d*))\.((?:0|[1-9]\d*))\.((?:0|[1-9]\d*))(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u; const NODE_RELEASE_FLOORS = [{ major: 24, minor: 16, patch: 0 }, { major: 26, minor: 1, patch: 0 }]; NODE_RELEASE_FLOORS[NODE_RELEASE_FLOORS.length - 1]; `${NODE_RELEASE_FLOORS.map(({ major, minor, patch }, index) => `>=${major}.${minor}.${patch}${index < NODE_RELEASE_FLOORS.length - 1 ? ` <${major + 1}` : ""}`).join(" || ").replaceAll(" || ", ", ").replace(/, ([^,]+)$/, ", or $1")}`; /** Parses an anchored release SemVer, allowing a leading v and valid build metadata. */ function parseNodeReleaseVersion(value) { if (typeof value !== "string") return null; const match = NODE_RELEASE_VERSION_RE.exec(value.trim()); if (!match) return null; const version = { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]) }; return Object.values(version).every(Number.isSafeInteger) ? version : null; } function isNodeVersionAtLeast(version, minimum) { if (!version) return false; if (version.major !== minimum.major) return version.major > minimum.major; if (version.minor !== minimum.minor) return version.minor > minimum.minor; return version.patch >= minimum.patch; } function renderProcessNodeVersionCheck() { return `((value) => { if (typeof value !== "string") return false; const match = new RegExp(${JSON.stringify(NODE_RELEASE_VERSION_RE.source)}, "u").exec(value.trim()); if (!match) return false; const version = { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]) }; if (!Object.values(version).every(Number.isSafeInteger)) return false; const floors = ${JSON.stringify(NODE_RELEASE_FLOORS)}; const minimum = floors.find((floor) => floor.major === version.major); const atLeast = (floor) => version.major > floor.major || (version.major === floor.major && (version.minor > floor.minor || (version.minor === floor.minor && version.patch >= floor.patch))); return minimum ? atLeast(minimum) : version.major > floors[floors.length - 1].major; })(process.versions.node)`; } renderProcessNodeVersionCheck(); //#endregion //#region src/infra/diagnostic-flags-env.ts function parseDiagnosticEnvFlags(raw) { const trimmed = raw?.trim() ?? ""; const lowered = trimmed.toLowerCase(); if (!lowered) return { flags: [], disablesAll: false }; if ([ "0", "false", "off", "none" ].includes(lowered)) return { flags: [], disablesAll: true }; if ([ "1", "true", "all", "*" ].includes(lowered)) return { flags: ["*"], disablesAll: false }; return { flags: trimmed.split(/[,\s]+/), disablesAll: false }; } //#endregion //#region node-sqlite.mjs function probeSqlite(DatabaseSync) { const result = { available: false, version: null, text: false, blob: false, json: false }; let database; try { database = new DatabaseSync(":memory:"); result.available = true; const version = database.prepare("SELECT sqlite_version() AS version").get()?.version; result.version = typeof version === "string" ? version : null; const text = "a\0b\0"; const bytes = Buffer.from(text, "utf8"); const json = JSON.stringify({ value: text }); database.exec("CREATE TABLE probe (text_value TEXT, blob_value BLOB, json_value TEXT)"); database.prepare("INSERT INTO probe VALUES (?, ?, ?)").run(text, bytes, json); const row = database.prepare("SELECT text_value, blob_value, json_value FROM probe").get(); result.text = typeof row?.text_value === "string" && row.text_value.length === 4 && Buffer.from(row.text_value, "utf8").equals(bytes); result.blob = row?.blob_value instanceof Uint8Array && Buffer.from(row.blob_value).equals(bytes); result.json = row?.json_value === json && JSON.parse(row.json_value).value === text; } catch (error) { result.error = error instanceof Error ? error.message : String(error); } finally { database?.close(); } return result; } function isSqliteWalResetSafeVersion(value) { const match = /^(\d+)\.(\d+)\.(\d+)$/u.exec(value.trim()); if (!match) return false; const [major, minor, patch] = match.slice(1).map(Number); if (![ major, minor, patch ].every(Number.isSafeInteger)) return false; return major > 3 || major === 3 && (minor > 51 || minor === 51 && patch >= 3 || minor === 50 && patch >= 7 || minor === 44 && patch >= 6); } `${probeSqlite.toString()}`; //#endregion //#region src/infra/bun-sqlite-library.ts const WORKER_SELECTION_KEY = "openclaw.bunSqliteLibrarySelection"; const WORKER_CAPABILITIES_KEY = "openclaw.sqliteRuntimeCapabilities"; function createCapabilities(deps) { let decision; let initialization; let earlyTopologyOwners = 0; const pending = Object.freeze({ explicitSqliteCloseReleasesNativeResources: false, decided: false, reason: "SQLite close capability has not been decided" }); function decide(value) { if (!decision) { decision = Object.freeze(value); deps.publish(decision); if (decision.explicitSqliteCloseReleasesNativeResources && earlyTopologyOwners > 0) deps.warn(`SQLite close became capable after ${earlyTopologyOwners} topology owners were created; those owners remain conservative`); } return decision; } function conservative(reason) { return { explicitSqliteCloseReleasesNativeResources: false, decided: true, reason }; } function settled() { if (decision) return decision; if (!deps.isBun) return decide({ explicitSqliteCloseReleasesNativeResources: true, decided: true, reason: "Node runtime" }); if (deps.platform === "win32") return decide(conservative("Bun Windows native-close conformance is not qualified")); if (!deps.isMainThread) return decide({ ...pending, reason: "Parent did not complete SQLite close admission", ...deps.inherited }); } return { get() { return settled() ?? pending; }, capture() { const fact = settled() ?? pending; if (!fact.decided) earlyTopologyOwners += 1; return fact.explicitSqliteCloseReleasesNativeResources; }, initialize() { initialization ??= (async () => { deps.select(); const ready = settled(); if (ready) return ready; try { if (deps.forceConservative) return decide(conservative("SQLite close optimization disabled by internal diagnostic flag")); return decide({ ...await deps.probe(), decided: true }); } catch (error) { return decide(conservative(`SQLite close probe failed: ${String(error)}`)); } })(); return initialization; } }; } function probeLibrary(path) { const { dlopen, FFIType } = createRequire(import.meta.url)("bun:ffi"); const library = dlopen(path, { sqlite3_libversion: { args: [], returns: FFIType.cstring }, sqlite3_compileoption_used: { args: [FFIType.cstring], returns: FFIType.i32 } }); try { return { version: String(library.symbols.sqlite3_libversion()), extensionLoadingSupported: library.symbols.sqlite3_compileoption_used(Buffer.from("OMIT_LOAD_EXTENSION\0")) === 0 }; } finally { library.close(); } } function selectLibrary(path) { const { Database } = createRequire(import.meta.url)("bun:sqlite"); Database.setCustomSQLite(path); } function createSelector(deps) { let selection; let failure; return (options = {}) => { if (failure) throw failure; if (selection) return selection; const override = options.explicitPath ?? (deps.env.OPENCLAW_SQLITE_LIBRARY?.trim() || void 0); if (!deps.isBun || deps.platform !== "darwin") { selection = override ? { source: "runtime", ignoredOverride: "OPENCLAW_SQLITE_LIBRARY requires Bun on macOS" } : { source: "runtime" }; return selection; } const prefix = deps.env.HOMEBREW_PREFIX?.trim(); const candidates = override !== void 0 ? [override] : [.../* @__PURE__ */ new Set([ ...prefix ? [posix.join(prefix, "opt/sqlite/lib/libsqlite3.dylib")] : [], "/opt/homebrew/opt/sqlite/lib/libsqlite3.dylib", "/usr/local/opt/sqlite/lib/libsqlite3.dylib", "/opt/local/lib/libsqlite3.dylib" ])]; for (const path of candidates) { let probe; try { try { probe = deps.probe(path); } catch (error) { throw deps.exists(path) ? error : new Error("missing file", { cause: error }); } if (!isSqliteWalResetSafeVersion(probe.version)) throw new Error(`SQLite version ${probe.version} below the WAL safety floor`); if (!probe.extensionLoadingSupported) throw new Error("built with SQLITE_OMIT_LOAD_EXTENSION"); } catch (error) { if (override === void 0) continue; failure = selectionError(path, error); throw failure; } try { deps.select(path); } catch (error) { failure = selectionError(path, error); throw failure; } selection = { source: override === void 0 ? "discovered" : "env", path, version: probe.version, extensionLoadingSupported: true }; return selection; } selection = { source: "runtime" }; return selection; }; } function selectionError(path, error) { return new Error(`Cannot use SQLite library ${path}: ${error instanceof Error ? error.message : String(error)}. Fix or unset OPENCLAW_SQLITE_LIBRARY; install a supported library with brew install sqlite.`, { cause: error }); } function inheritedSelection() { const value = getEnvironmentData(WORKER_SELECTION_KEY); if (value === void 0) return; if (value !== null && typeof value === "object" && !Array.isArray(value)) { const source = "source" in value ? value.source : void 0; if (source === "runtime") return { source: "runtime" }; const path = "path" in value ? value.path : void 0; const version = "version" in value ? value.version : void 0; const extensionLoadingSupported = "extensionLoadingSupported" in value ? value.extensionLoadingSupported : void 0; if ((source === "env" || source === "discovered") && typeof path === "string" && typeof version === "string" && extensionLoadingSupported === true) return { source, path, version, extensionLoadingSupported: true }; } throw new Error("Invalid inherited SQLite library selection"); } function createRuntimeSelector() { const isBun = Boolean(process.versions.bun); const sharedLibrary = isBun && process.platform === "darwin"; const inherited = sharedLibrary && !isMainThread ? inheritedSelection() : void 0; const select = createSelector({ isBun, platform: process.platform, env: process.env, exists: existsSync, probe: probeLibrary, select: selectLibrary }); let published = false; return (options) => { const selection = inherited ?? select(options); if (sharedLibrary && isMainThread && !published) { setEnvironmentData(WORKER_SELECTION_KEY, Object.freeze({ ...selection })); published = true; } return selection; }; } function runtimeSelector() { return resolveGlobalSingleton(Symbol.for("openclaw.bunSqliteLibrarySelection"), createRuntimeSelector); } function capabilities(options) { const deps = options?.internals; if (deps) return deps.owner ??= createCapabilities(deps); const select = runtimeSelector(); return select.capabilities ??= createCapabilities({ isBun: Boolean(process.versions.bun), platform: process.platform, isMainThread, get inherited() { return getEnvironmentData(WORKER_CAPABILITIES_KEY); }, select, probe: async () => (await import("./bun-sqlite-close-probe-xEVOjuu6.mjs")).probeSqliteNativeClose(), publish: (value) => setEnvironmentData(WORKER_CAPABILITIES_KEY, value), warn: (message) => process.emitWarning(message, { code: "SQLITE_EARLY_TOPOLOGY" }), get forceConservative() { return parseDiagnosticEnvFlags(process.env.OPENCLAW_DIAGNOSTICS).flags.some((flag) => flag.toLowerCase() === "sqlite.close.conservative"); } }); } function getSqliteRuntimeCapabilities(options) { return capabilities(options).get(); } /** A topology owner keeps this snapshot for placement and paired native retirement. */ function captureSqliteWorkerClosePolicy(options) { return capabilities(options).capture(); } /** Retained supervisors forward the caller's current facts when creating each descendant. */ function captureSqliteWorkerEnvironmentData() { return [[WORKER_SELECTION_KEY, ensureSqliteLibrarySelected()], [WORKER_CAPABILITIES_KEY, getSqliteRuntimeCapabilities()]]; } /** Select once, before any SQLite open; shared across CLI and bundled SDK module graphs. */ function ensureSqliteLibrarySelected(options) { const dependencies = options?.internals; return (dependencies ? dependencies.selector ??= createSelector(dependencies) : runtimeSelector())(options); } //#endregion export { isSqliteWalResetSafeVersion as a, getSqliteRuntimeCapabilities as i, captureSqliteWorkerEnvironmentData as n, isNodeVersionAtLeast as o, ensureSqliteLibrarySelected as r, parseNodeReleaseVersion as s, captureSqliteWorkerClosePolicy as t };