openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
377 lines (376 loc) • 14.8 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { n as isTruthyEnvValue, s as isVitestRuntimeEnv } from "./env-HehwidXj.mjs";
import { n as tryProcessCwd } from "./safe-cwd-CZqaxroH.mjs";
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs";
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs";
import "./utils-C8YECn2Q.mjs";
import { a as getPluginCache, c as getPluginCacheRoot } from "./plugin-cache-B_eRuqTM.mjs";
import { r as resolveOpenClawPackageRootSync } from "./openclaw-root-C-ayo09z.mjs";
import { a as pluginCacheRealpathSync, c as readPluginCacheFile, n as parsePluginCacheJson, r as pluginCacheExistsSync, s as readPluginCacheDirectory, u as refreshPluginCacheStat } from "./plugin-cache-files-e1aGp2KM.mjs";
import { n as isTypeScriptPackageEntry, r as listBuiltRuntimeEntryCandidates } from "./package-entrypoints-Do32y0v-.mjs";
import { n as getPackageManifestMetadata } from "./package-manifest-CC8p56hY.mjs";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";
import os from "node:os";
//#region src/plugins/plugin-runtime-artifact-selection.ts
/** Selects built plugin artifacts without importing active runtime state. */
/** Canonical packaged runtime replaces staging-only dist-runtime artifacts. */
function resolveCanonicalDistRuntimeSource(e) {
const t = `${path.sep}dist-runtime${path.sep}extensions${path.sep}`;
const n = e.indexOf(t);
if (n === -1) return e;
const r = `${e.slice(0, n)}${path.sep}dist${path.sep}extensions${path.sep}${e.slice(n + t.length)}`;
return pluginCacheExistsSync(r) ? r : e;
}
/** Finalize once at execution; discovery and identity retain the selected artifact. */
function resolvePluginRuntimeExecutionArtifact(e) {
const t = resolveCanonicalDistRuntimeSource(e.source);
const n = resolveCanonicalDistRuntimeSource(e.rootDir);
return {
source: t,
rootDir: n !== e.rootDir && !isPathInside(pluginCacheRealpathSync(n) ?? n, pluginCacheRealpathSync(t) ?? t) ? e.rootDir : n
};
}
/** Selects from lifecycle-owned package facts without pinning a runtime registry. */
function resolvePluginRuntimeArtifactSelection(e) {
const { source: t, rootDir: n } = resolvePluginRuntimeExecutionArtifact({
source: pluginCacheRealpathSync(e.source) ?? path.resolve(e.source),
rootDir: pluginCacheRealpathSync(e.rootDir) ?? path.resolve(e.rootDir)
});
const r = getPluginCacheRoot(n).runtimeArtifacts;
const i = JSON.stringify([
e.source,
e.entryKind,
e.origin,
e.preferBuiltPluginArtifacts,
e.sourcePreferred,
e.packageManifest?.build?.bundledDist
]);
let a = r.get(i);
if (!a) {
a = resolvePluginRuntimeExecutionArtifact(resolvePreferredBuiltRuntimeArtifact({
...e,
source: t,
rootDir: n
}));
r.set(i, a);
}
return a;
}
/** Built hosts default only checkout plugins to compiled execution, not installed packages. */
function resolvePluginRuntimeArtifactPreference(e) {
if (e !== void 0) return e ? "all" : "source";
return /\.[cm]?js$/.test(new URL(import.meta.url).pathname) ? "bundled" : "source";
}
function prefersBuiltPluginArtifacts(e, t) {
return e === "all" || e === "bundled" && t === "bundled";
}
function resolveBundledArtifactRelativePath(e, t) {
const n = readPluginCacheFile({
rootDir: e,
relativePath: "package.json",
rejectHardlinks: false
});
const r = n.ok ? parsePluginCacheJson(n) : void 0;
const i = r?.ok && isRecord(r.value) ? getPackageManifestMetadata(r.value) : void 0;
const a = [...i?.runtimeExtensions?.length ? i.runtimeExtensions : i?.extensions ?? [], i?.runtimeSetupEntry ?? i?.setupEntry].filter((e) => typeof e === "string");
const o = t.replace(/\.[^.]+$/u, "");
const s = a.find((e) => path.normalize(e).replace(/\.[^.]+$/u, "") === o);
if (s) return /\.[cm]?js$/.test(s) ? s : null;
const c = new Set(a.map((e) => path.extname(e)));
const l = c.size === 1 ? [...c][0] : void 0;
if (!l || ![
".js",
".mjs",
".cjs"
].includes(l) || a.some((e) => path.normalize(e).startsWith(`dist${path.sep}`))) return null;
return t.replace(/\.[^.]+$/u, l);
}
function resolvePackageLocalDistRuntimeArtifact(e) {
const t = path.relative(e.rootDir, e.source);
if (!isTypeScriptPackageEntry(t) || t === "" || t.startsWith("..") || path.isAbsolute(t)) return null;
for (const n of listBuiltRuntimeEntryCandidates(t)) {
if (e.origin === "bundled" && !n.startsWith("./dist/")) continue;
const t = path.resolve(e.rootDir, n);
if (pluginCacheExistsSync(t)) return pluginCacheRealpathSync(t) ?? path.resolve(t);
}
return null;
}
function resolvePreferredBundledRootArtifactFromCanonicalPaths(e) {
const { rootDir: t, source: n } = e;
const r = e.packageManifest?.build?.bundledDist === false;
const i = path.dirname(t);
if (path.basename(i) !== "extensions") return {
source: n,
rootDir: t
};
const a = path.dirname(i);
if (path.basename(a) === "dist" || path.basename(a) === "dist-runtime") return {
source: n,
rootDir: t
};
const o = path.relative(t, n);
if (o === "" || o.startsWith("..") || path.isAbsolute(o)) return {
source: n,
rootDir: t
};
for (const e of r ? ["dist"] : ["dist-runtime", "dist"]) {
const n = path.join(a, e, "extensions", path.basename(t));
const r = resolveBundledArtifactRelativePath(n, o);
if (!r) continue;
const i = path.join(n, r);
if (pluginCacheExistsSync(i)) return {
source: pluginCacheRealpathSync(i) ?? path.resolve(i),
rootDir: pluginCacheRealpathSync(n) ?? path.resolve(n)
};
}
return {
source: n,
rootDir: t
};
}
/** Selects the lifecycle-owned root build for one bundled source artifact. */
function resolvePreferredBundledRootArtifact(e) {
const t = getPluginCacheRoot(e.rootDir).runtimeArtifacts;
const n = JSON.stringify([
"bundled-root",
e.source,
e.packageManifest?.build?.bundledDist
]);
const r = t.get(n);
if (r) return r;
const i = resolvePreferredBundledRootArtifactFromCanonicalPaths({
source: pluginCacheRealpathSync(e.source) ?? path.resolve(e.source),
rootDir: pluginCacheRealpathSync(e.rootDir) ?? path.resolve(e.rootDir),
packageManifest: e.packageManifest
});
t.set(n, i);
return i;
}
/** Applies source, package-local, and root-build preference without runtime memo state. */
function resolvePreferredBuiltRuntimeArtifact(e) {
const { rootDir: t, source: n } = e;
if (!e.preferBuiltPluginArtifacts || e.sourcePreferred) return {
source: n,
rootDir: t
};
if (e.origin !== "bundled") {
const r = resolvePackageLocalDistRuntimeArtifact({
...e,
source: n,
rootDir: t
});
return r ? {
source: r,
rootDir: t
} : {
source: n,
rootDir: t
};
}
const r = e.packageManifest?.build?.bundledDist === false ? null : resolvePackageLocalDistRuntimeArtifact({
...e,
source: n,
rootDir: t
});
if (r) return {
source: r,
rootDir: t
};
return resolvePreferredBundledRootArtifactFromCanonicalPaths({
source: n,
rootDir: t,
packageManifest: e.packageManifest
});
}
//#endregion
//#region src/plugins/bundled-dir.ts
/** Resolves the bundled plugin directory for source checkouts, dist builds, and tests. */
const DISABLED_BUNDLED_PLUGINS_DIR = path.join(os.tmpdir(), "openclaw-empty-bundled-plugins");
const TEST_TRUST_BUNDLED_PLUGINS_DIR_ENV = "OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR";
/** Returns true when env disables bundled plugin discovery. */
function areBundledPluginsDisabled(e = process.env) {
const t = normalizeOptionalLowercaseString(e.OPENCLAW_DISABLE_BUNDLED_PLUGINS);
return t === "1" || t === "true";
}
function resolveDisabledBundledPluginsDir() {
if (!pluginCacheExistsSync(DISABLED_BUNDLED_PLUGINS_DIR)) {
fs.mkdirSync(DISABLED_BUNDLED_PLUGINS_DIR, { recursive: true });
refreshPluginCacheStat(DISABLED_BUNDLED_PLUGINS_DIR);
}
return DISABLED_BUNDLED_PLUGINS_DIR;
}
function isSourceCheckoutRoot(e) {
return pluginCacheExistsSync(path.join(e, "pnpm-workspace.yaml")) && pluginCacheExistsSync(path.join(e, "src")) && pluginCacheExistsSync(path.join(e, "extensions"));
}
function shouldTrustTestBundledPluginsDirOverride(e) {
const t = e !== process.env;
if (!isTruthyEnvValue(e[TEST_TRUST_BUNDLED_PLUGINS_DIR_ENV]) && !(t && isTruthyEnvValue(process.env[TEST_TRUST_BUNDLED_PLUGINS_DIR_ENV]))) return false;
return isVitestRuntimeEnv(e) || t && isVitestRuntimeEnv(process.env);
}
function hasUsableBundledPluginTree(e) {
if (!pluginCacheExistsSync(e)) return false;
try {
return readPluginCacheDirectory(e).some((t) => {
if (!t.isDirectory()) return false;
const n = path.join(e, t.name);
return pluginCacheExistsSync(path.join(n, "package.json")) || pluginCacheExistsSync(path.join(n, "openclaw.plugin.json"));
});
} catch {
return false;
}
}
function safeRealpathSync(e) {
return pluginCacheRealpathSync(e, true);
}
function trustedBundledPluginRootsForPackageRoot(e) {
const t = [path.join(e, "dist", "extensions"), path.join(e, "dist-runtime", "extensions")];
if (isSourceCheckoutRoot(e)) t.push(path.join(e, "extensions"));
return t;
}
function resolvePackageRootsForBundledPlugins(e) {
const t = resolveOpenClawPackageRootSync({ argv1: process.argv[1] });
const n = t && e && isPluginInPackageBundledRoots({
rootDir: e,
packageRoot: t
}) ? null : t;
const r = resolveOpenClawPackageRootSync({ moduleUrl: import.meta.url });
return uniqueStrings([n, r].filter((e) => Boolean(e)));
}
function resolveSourceCheckoutDependencyDiagnostic(e = process.env) {
if (areBundledPluginsDisabled(e)) return null;
for (const e of resolvePackageRootsForBundledPlugins()) {
if (!isSourceCheckoutRoot(e)) continue;
const t = path.join(e, "extensions");
if (!isPluginInPackageBundledRoots({
rootDir: t,
packageRoot: e
}) || !hasUsableBundledPluginTree(t)) continue;
if (pluginCacheExistsSync(path.join(e, "node_modules", ".pnpm"))) continue;
return {
source: e,
message: "OpenClaw source checkout detected without pnpm workspace dependencies; run `pnpm install` from the repo root so bundled plugins can load package-local dependencies."
};
}
return null;
}
function resolveTrustedExistingOverride(e) {
const t = safeRealpathSync(e);
if (!t) return null;
const n = resolveOpenClawPackageRootSync({ moduleUrl: import.meta.url });
if (!n || !isPluginInPackageBundledRoots({
rootDir: t,
packageRoot: n
})) return null;
if (!hasUsableBundledPluginTree(t)) return null;
return t;
}
/** Checks physical containment in a package's source or compiled plugin trees. */
function isPluginInPackageBundledRoots(e) {
const t = safeRealpathSync(e.rootDir);
const n = safeRealpathSync(e.packageRoot);
if (!t || !n) return false;
return trustedBundledPluginRootsForPackageRoot(e.packageRoot).map((e) => safeRealpathSync(e)).some((e) => e !== null && isPathInside(n, e) && isPathInside(e, t));
}
/** Recognizes compiled bundle ownership only; this never confers plugin trust. */
function isForeignBundledPluginRoot(e, t = process.env) {
const n = safeRealpathSync(e);
if (!n) return false;
const r = path.dirname(n);
const i = path.dirname(r);
if (path.basename(r) !== "extensions" || !["dist", "dist-runtime"].includes(path.basename(i))) return false;
const a = path.dirname(i);
if (resolveOpenClawPackageRootSync({ cwd: a }) !== a || !isPluginInPackageBundledRoots({
rootDir: n,
packageRoot: a
})) return false;
const o = resolveBundledPluginsDir(t);
const s = o ? safeRealpathSync(o) : null;
if (s && isPathInside(s, n)) return false;
const c = resolvePackageRootsForBundledPlugins();
return c.length > 0 && !c.some((e) => isPluginInPackageBundledRoots({
rootDir: n,
packageRoot: e
}));
}
function resolveBundledDirFromPackageRoot(e, t = "bundled") {
const n = path.join(e, "dist", "extensions");
const r = path.join(e, "dist-runtime", "extensions");
if (isSourceCheckoutRoot(e)) {
const i = path.join(e, "extensions");
return (t === "source" ? [i] : [
n,
r,
i
]).find((t) => isPluginInPackageBundledRoots({
rootDir: t,
packageRoot: e
}) && hasUsableBundledPluginTree(t));
}
return pluginCacheExistsSync(n) ? [r, n].find((t) => isPluginInPackageBundledRoots({
rootDir: t,
packageRoot: e
})) : void 0;
}
function resolveBundledPluginsDirUncached(e) {
if (areBundledPluginsDisabled(e)) return resolveDisabledBundledPluginsDir();
const t = e.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim();
let n = null;
if (t) {
const r = resolveUserPath(t, e);
if (pluginCacheExistsSync(r)) {
if (shouldTrustTestBundledPluginsDirOverride(e)) return path.resolve(r);
const t = resolveTrustedExistingOverride(r);
if (t) return t;
n = r;
}
}
for (const e of resolvePackageRootsForBundledPlugins(n)) {
const t = resolveBundledDirFromPackageRoot(e, resolvePluginRuntimeArtifactPreference());
if (t) return t;
}
const r = path.dirname(process.execPath);
const i = path.join(r, "dist", "extensions");
if (pluginCacheExistsSync(i)) return i;
const a = path.join(r, "extensions");
if (pluginCacheExistsSync(a)) return a;
try {
let e = path.dirname(fileURLToPath(import.meta.url));
for (let t = 0; t < 6; t += 1) {
const t = path.join(e, "extensions");
if (pluginCacheExistsSync(t)) return t;
const n = path.dirname(e);
if (n === e) break;
e = n;
}
} catch {}
}
function resolveBundledPluginsDir(e = process.env) {
const t = areBundledPluginsDisabled(e);
const n = t ? void 0 : e.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim();
const r = n ? resolveUserPath(n, e) : void 0;
const i = shouldTrustTestBundledPluginsDirOverride(e);
const a = process.argv[1];
const o = process.execPath;
const s = tryProcessCwd();
const c = getPluginCache().metadata;
const l = c.bundledPluginsDir;
if (l && l.moduleUrl === import.meta.url && l.disabled === t && l.resolvedOverride === r && l.trustOverride === i && l.argv1 === a && l.execPath === o && l.cwd === s) return l.value;
const u = resolveBundledPluginsDirUncached(e);
c.bundledPluginsDir = {
moduleUrl: import.meta.url,
disabled: t,
resolvedOverride: r,
trustOverride: i,
argv1: a,
execPath: o,
cwd: s,
value: u
};
return u;
}
//#endregion
export { isSourceCheckoutRoot as a, resolveSourceCheckoutDependencyDiagnostic as c, resolveCanonicalDistRuntimeSource as d, resolvePluginRuntimeArtifactPreference as f, resolvePreferredBundledRootArtifact as h, isPluginInPackageBundledRoots as i, shouldTrustTestBundledPluginsDirOverride as l, resolvePluginRuntimeExecutionArtifact as m, hasUsableBundledPluginTree as n, resolveBundledDirFromPackageRoot as o, resolvePluginRuntimeArtifactSelection as p, isForeignBundledPluginRoot as r, resolveBundledPluginsDir as s, areBundledPluginsDisabled as t, prefersBuiltPluginArtifacts as u };