openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
343 lines (342 loc) • 12.6 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { i as getPluginRegistryForContext } from "./gateway-request-scope-BvvDhs5D.mjs";
import { c as getPluginCacheRoot, l as getPluginCacheSource } from "./plugin-cache-B_eRuqTM.mjs";
import { c as readPluginCacheFile, n as parsePluginCacheJson } from "./plugin-cache-files-e1aGp2KM.mjs";
import { s as resolveBundledPluginsDir, t as areBundledPluginsDisabled } from "./bundled-dir-BKvdAqkP.mjs";
import { t as normalizeManifestPlatforms } from "./manifest-platforms-UY_COXnx.mjs";
import { t as shouldRejectHardlinkedPluginFiles } from "./hardlink-policy-Kq1Lxk7_.mjs";
import { r as loadPluginPublicSurfaceModuleSync, t as getCachedPluginModuleLoader } from "./plugin-module-loader-cache-DgNX_BTY.mjs";
import { l as getPluginValueInstance, r as getPluginInstance } from "./plugin-instance-scope-oezVdCr2.mjs";
import { s as resolveLoaderPackageRoot } from "./sdk-alias-ByYgWl8I.mjs";
import { n as resolveBundledPluginSourcePublicSurfacePath, r as resolvePluginRootPublicSurfaceLocation, t as resolveBundledPluginPublicSurfacePath } from "./public-surface-runtime-JyTrzr-T.mjs";
import { fileURLToPath } from "node:url";
import path from "node:path";
//#region src/plugin-sdk/facade-resolution-shared.ts
/**
* Shared resolver for bundled plugin facade module paths and registry fallbacks.
*/
const RUNTIME_FACADE_MATCH_TIERS = [
"id",
"folder",
"channel"
];
/** An executing instance wins over metadata and bundled fallbacks, including a missing surface. */
function resolveRuntimeFacadeModuleLocation(e) {
if (e.env !== void 0 && e.env !== process.env) return;
const t = getPluginRegistryForContext()?.plugins;
if (!t) return;
let n;
for (const r of RUNTIME_FACADE_MATCH_TIERS) {
for (const i of t) {
if (i.status !== "loaded" || !i.rootDir) continue;
if (!(r === "id" ? i.id === e.dirName : r === "folder" ? path.basename(i.rootDir) === e.dirName : i.channelIds.includes(e.dirName))) continue;
if (n) throw new Error(`Plugin public surface ${e.dirName} has ambiguous runtime ownership; use its plugin id.`);
n = i;
}
if (n) break;
}
if (!n) return;
const r = resolvePluginRootPublicSurfaceLocation({
pluginRoot: n.rootDir,
pluginId: n.id,
artifactBasename: e.artifactBasename
});
return r ? {
...r,
pluginId: n.id,
origin: n.origin
} : null;
}
function readBundledPluginManifestRecordFromDir(e) {
const t = readPluginCacheFile({
rootDir: e,
relativePath: "openclaw.plugin.json",
rejectHardlinks: false
});
if (!t.ok) return null;
try {
const n = parsePluginCacheJson(t, { json5: true });
if (!n.ok || !isRecord(n.value)) return null;
const r = n.value;
const i = normalizeOptionalString(r.id);
if (!i) return null;
return {
id: i,
origin: "bundled",
enabledByDefault: r.enabledByDefault === true,
enabledByDefaultOnPlatforms: normalizeManifestPlatforms(r.enabledByDefaultOnPlatforms),
rootDir: e,
channels: normalizeTrimmedStringList(r.channels)
};
} catch {
return null;
}
}
/** Resolve bundled facade metadata without importing activation or registry runtime. */
function resolveBundledMetadataManifestRecord(e) {
if (!e.location) return null;
let t = e.sourceExtensionsRoot;
if (!e.location.modulePath.startsWith(`${t}${path.sep}`)) {
const n = resolveBundledPluginsDir(e.env ?? process.env);
if (!n) return null;
t = path.resolve(n);
if (!e.location.modulePath.startsWith(`${t}${path.sep}`)) return null;
}
const n = path.relative(t, e.location.modulePath).split(path.sep)[0];
return n ? readBundledPluginManifestRecordFromDir(path.join(t, n)) : null;
}
/** Builds the cache key for one facade lookup under the current bundled-plugin mode. */
function createFacadeResolutionKey(e) {
const t = areBundledPluginsDisabled(e.env ?? process.env) ? "disabled" : "enabled";
return `${e.dirName}::${e.artifactBasename}::${e.bundledPluginsDir ? path.resolve(e.bundledPluginsDir) : "<default>"}::${t}`;
}
/** Chooses the boundary root that should constrain a resolved facade module. */
function resolveFacadeBoundaryRoot(e) {
if (!e.bundledPluginsDir) return e.packageRoot;
const t = path.resolve(e.bundledPluginsDir);
return e.modulePath.startsWith(`${t}${path.sep}`) ? t : e.packageRoot;
}
/** Resolves a bundled facade from source in dev and built artifacts in dist installs. */
function resolveBundledFacadeModuleLocation(e) {
const t = e.env ?? process.env;
if (areBundledPluginsDisabled(t)) return null;
const n = e.preferSource ?? !e.currentModulePath.includes(`${path.sep}dist${path.sep}`);
const r = path.resolve(e.packageRoot, "extensions");
const i = {
rootDir: e.packageRoot,
env: e.env,
...e.bundledPluginsDir ? {
bundledPluginsDir: e.bundledPluginsDir,
bundledPluginsDirMode: "explicit"
} : {},
dirName: e.dirName,
artifactBasename: e.artifactBasename
};
const a = n ? resolveBundledPluginSourcePublicSurfacePath({
dirName: e.dirName,
artifactBasename: e.artifactBasename,
sourceRoot: e.bundledPluginsDir ?? r
}) ?? (e.bundledPluginsDir && !areBundledPluginsDisabled(t) ? resolveBundledPluginSourcePublicSurfacePath({
dirName: e.dirName,
artifactBasename: e.artifactBasename,
sourceRoot: r
}) : null) ?? resolveBundledPluginPublicSurfacePath(i) : resolveBundledPluginPublicSurfacePath(i);
return a ? {
origin: "bundled",
modulePath: a,
boundaryRoot: resolveFacadeBoundaryRoot({
modulePath: a,
bundledPluginsDir: e.bundledPluginsDir,
packageRoot: e.packageRoot
})
} : null;
}
/** Resolves a facade path from manifest registry records using id, folder, then channel matches. */
function resolveRegistryPluginModuleLocationFromRecords(e) {
const t = [
(t) => t.id === e.dirName,
(t) => path.basename(t.rootDir) === e.dirName,
(t) => t.channels.includes(e.dirName)
];
for (const n of t) for (const t of e.registry.filter(n)) {
const n = path.resolve(t.rootDir);
const r = resolvePluginRootPublicSurfaceLocation({
pluginRoot: n,
pluginId: t.id,
artifactBasename: e.artifactBasename
});
if (r) return {
...r,
...t.origin ? { origin: t.origin } : {}
};
}
return null;
}
//#endregion
//#region src/plugin-sdk/facade-loader.ts
/** Error thrown when a bundled plugin public surface artifact cannot be resolved. */
var MissingPublicSurfaceError = class extends Error {
constructor(e, t) {
super(e, t);
this.name = "MissingPublicSurfaceError";
}
};
const CURRENT_MODULE_PATH = fileURLToPath(import.meta.url);
const loadedFacadePluginIds = /* @__PURE__ */ new Set();
function getOpenClawPackageRoot() {
return resolveLoaderPackageRoot({
modulePath: fileURLToPath(import.meta.url),
moduleUrl: import.meta.url
}) ?? fileURLToPath(new URL("../..", import.meta.url));
}
function resolveBundledPublicSurfaceLocation(e) {
const t = resolveRuntimeFacadeModuleLocation(e);
if (t !== void 0) return t;
const n = resolveBundledPluginsDir(e.env ?? process.env);
const r = `facade:${e.preferSource ?? "auto"}:${createFacadeResolutionKey({
...e,
bundledPluginsDir: n
})}`;
const i = getPluginCacheRoot(getOpenClawPackageRoot()).artifacts;
const a = i.get(r);
if (a !== void 0) return a;
const o = resolveBundledFacadeModuleLocation({
...e,
currentModulePath: CURRENT_MODULE_PATH,
packageRoot: getOpenClawPackageRoot(),
bundledPluginsDir: n
});
i.set(r, o);
return o;
}
/** Create a lazy object view over a library or the current managed facade. */
function createLazyFacadeObjectValue(e) {
let t;
let n;
const resolve = () => {
if (n) {
const e = getPluginRegistryForContext()?.plugins.find((e) => e.id === n);
if (e?.status !== "loaded" || !getPluginInstance(e)) throw new Error(`Plugin ${n} was disabled or uninstalled; its facade is unavailable.`);
}
const r = t ?? e();
n ??= getPluginValueInstance(r)?.pluginId;
if (!n) t = r;
return r;
};
const r = {};
const syncProperty = (e) => {
const t = Reflect.getOwnPropertyDescriptor(resolve(), e);
if (t) {
if (n) t.configurable = true;
Object.defineProperty(r, e, t);
} else Reflect.deleteProperty(r, e);
return t;
};
const syncTarget = () => {
const e = resolve();
const t = Object.getOwnPropertyDescriptors(e);
if (n) for (const e of Reflect.ownKeys(t)) Reflect.set(Reflect.get(t, e), "configurable", true);
for (const e of Reflect.ownKeys(r)) if (!Object.hasOwn(t, e)) Reflect.deleteProperty(r, e);
Object.defineProperties(r, t);
Reflect.setPrototypeOf(r, Reflect.getPrototypeOf(e));
if (!Reflect.isExtensible(e)) Reflect.preventExtensions(r);
};
return new Proxy(r, {
defineProperty(e, t, r) {
const i = resolve();
if (n && (r.configurable === false || r.configurable !== true && !Object.hasOwn(i, t))) return false;
const a = Reflect.defineProperty(i, t, r);
if (a) syncProperty(t);
return a;
},
deleteProperty(e, t) {
return Reflect.deleteProperty(resolve(), t) && Reflect.deleteProperty(r, t);
},
get(e, t, n) {
return Reflect.get(resolve(), t, n);
},
getOwnPropertyDescriptor(e, t) {
return syncProperty(t);
},
getPrototypeOf() {
return Reflect.getPrototypeOf(resolve());
},
has(e, t) {
const n = Reflect.has(resolve(), t);
if (!n) Reflect.deleteProperty(r, t);
return n;
},
isExtensible() {
const e = Reflect.isExtensible(resolve());
if (!e) syncTarget();
return e;
},
ownKeys() {
syncTarget();
return Reflect.ownKeys(resolve());
},
preventExtensions() {
const e = resolve();
if (n || !Reflect.preventExtensions(e)) return false;
syncTarget();
return true;
},
set(e, t, n, r) {
return Reflect.set(resolve(), t, n, r);
},
setPrototypeOf(e, t) {
return Reflect.setPrototypeOf(resolve(), t) && Reflect.setPrototypeOf(r, t);
}
});
}
function trackFacadeModule(e, t) {
const n = getPluginCacheSource(e);
if (n.facadeTracked) return;
n.facadeTracked = true;
try {
loadedFacadePluginIds.add(typeof t === "function" ? t() : t);
} catch (e) {
delete n.facadeTracked;
throw e;
}
}
function isPathAtOrInside(e, t) {
const n = path.resolve(t);
const r = path.resolve(e);
return r === n || r.startsWith(n + path.sep);
}
function resolveFacadeBoundaryOpenParams(e) {
const t = getPluginCacheRoot(e).publicSurfaceBoundary;
if (t) return t;
if (isPathAtOrInside(e, getOpenClawPackageRoot())) return {
boundaryLabel: "OpenClaw package root",
rejectHardlinks: false
};
const n = resolveBundledPluginsDir();
if (n && isPathAtOrInside(e, n)) return {
boundaryLabel: "bundled plugin directory",
rejectHardlinks: false
};
return {
boundaryLabel: "plugin root",
rejectHardlinks: shouldRejectHardlinkedPluginFiles({
origin: "global",
rootDir: e
})
};
}
/** Load and cache a facade module after verifying it is inside its declared boundary root. */
function loadFacadeModuleAtLocationSync(e) {
const t = e.location;
const n = loadPluginPublicSurfaceModuleSync({
...t,
...e.boundary ?? resolveFacadeBoundaryOpenParams(t.boundaryRoot),
surfaceLabel: e.surfaceLabel ?? `bundled plugin public surface ${t.modulePath}`,
pluginId: e.pluginId ?? t.pluginId,
loadModule: e.loadModule ?? ((e) => getCachedPluginModuleLoader({
modulePath: e,
importerUrl: import.meta.url,
preferBuiltDist: true,
loaderFilename: import.meta.url
})(e))
});
if (e.trackedPluginId !== void 0) trackFacadeModule(t.modulePath, e.trackedPluginId);
return n;
}
/** Resolve and synchronously load a bundled plugin public surface by plugin dir and artifact name. */
function loadBundledPluginPublicSurfaceModuleSyncCore(e) {
const t = resolveBundledPublicSurfaceLocation(e);
if (!t) throw new MissingPublicSurfaceError(`Unable to resolve bundled plugin public surface ${e.dirName}/${e.artifactBasename}`);
return loadFacadeModuleAtLocationSync({
location: t,
trackedPluginId: e.trackedPluginId ?? e.dirName
});
}
/** List plugin ids whose public facades have been loaded in this process. */
function listImportedBundledPluginFacadeIds() {
return [...loadedFacadePluginIds].toSorted((e, t) => e.localeCompare(t));
}
//#endregion
export { loadFacadeModuleAtLocationSync as a, resolveBundledMetadataManifestRecord as c, loadBundledPluginPublicSurfaceModuleSyncCore as i, resolveRegistryPluginModuleLocationFromRecords as l, createLazyFacadeObjectValue as n, resolveBundledPublicSurfaceLocation as o, listImportedBundledPluginFacadeIds as r, createFacadeResolutionKey as s, MissingPublicSurfaceError as t, resolveRuntimeFacadeModuleLocation as u };