openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
140 lines (139 loc) • 5.79 kB
JavaScript
import { t as parseJsonWithJson5Fallback } from "./parse-json-compat-DoWUfD79.mjs";
import { m as resolveConfigPath } from "./paths-DAcAg5aV.mjs";
import { o as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BvvDhs5D.mjs";
import { n as getPluginRegistryState } from "./runtime-state-DVrhXeqI.mjs";
import { t as isPluginEnabledByDefaultForPlatform } from "./default-enablement-DLXVIwwO.mjs";
import { p as resolveEffectivePluginActivationState, r as createPluginActivationSource, u as normalizePluginsConfig } from "./config-state-B1JlHgba.mjs";
import { a as getRuntimeConfigSnapshot, c as getRuntimeConfigSourceSnapshot } from "./runtime-snapshot-WbwbY5b7.mjs";
import { p as resolvePluginMetadataSnapshot } from "./plugin-metadata-snapshot-By9ySblt.mjs";
import { c as resolveBundledMetadataManifestRecord, l as resolveRegistryPluginModuleLocationFromRecords } from "./facade-loader-08DBXZOS.mjs";
import { i as configMayNeedPluginAutoEnable, t as applyPluginAutoEnable } from "./plugin-auto-enable-CPcwS2II.mjs";
import fs from "node:fs";
import path from "node:path";
//#region src/plugin-sdk/facade-activation-contract.ts
const ALWAYS_ALLOWED_RUNTIME_DIR_NAMES = ["image-generation-core"];
//#endregion
//#region src/plugin-sdk/facade-activation-check.runtime.ts
/**
* Runtime boundary checks for bundled plugin public-surface facade imports.
*/
const ALWAYS_ALLOWED_RUNTIME_DIR_NAME_SET = new Set(ALWAYS_ALLOWED_RUNTIME_DIR_NAMES);
const EMPTY_FACADE_BOUNDARY_CONFIG = {};
function readFacadeBoundaryConfigSafely() {
try {
const e = getRuntimeConfigSourceSnapshot();
if (e) return e;
const t = getRuntimeConfigSnapshot();
if (t) return t;
const n = resolveConfigPath();
if (!fs.existsSync(n)) return EMPTY_FACADE_BOUNDARY_CONFIG;
const r = fs.readFileSync(n, "utf8");
const i = parseJsonWithJson5Fallback(r);
return i && typeof i === "object" ? i : EMPTY_FACADE_BOUNDARY_CONFIG;
} catch {
return EMPTY_FACADE_BOUNDARY_CONFIG;
}
}
function getFacadeBoundaryResolvedConfig() {
const e = readFacadeBoundaryConfigSafely();
const t = configMayNeedPluginAutoEnable(e, process.env) ? applyPluginAutoEnable({
config: e,
env: process.env
}) : {
config: e,
autoEnabledReasons: {}
};
const n = t.config;
return {
rawConfig: e,
config: n,
normalizedPluginsConfig: normalizePluginsConfig(n?.plugins),
activationSource: createPluginActivationSource({ config: e }),
autoEnabledReasons: t.autoEnabledReasons
};
}
function getFacadeManifestRegistry(e) {
const t = e.env ? { env: e.env } : {};
const n = getFacadeBoundaryResolvedConfig();
return resolvePluginMetadataSnapshot({
config: n.config,
...t,
allowWorkspaceScopedCurrent: true
}).manifestRegistry.plugins;
}
/** Resolves the concrete plugin module location recorded in the manifest registry. */
function resolveRegistryPluginModuleLocation(e) {
const t = getFacadeManifestRegistry(e);
return resolveRegistryPluginModuleLocationFromRecords({
registry: t,
dirName: e.dirName,
artifactBasename: e.artifactBasename
});
}
function resolveBundledPluginManifestRecord(e) {
const t = resolveBundledMetadataManifestRecord(e);
if (t) return t;
const n = getFacadeManifestRegistry(e);
return (e.location ? n.find((t) => {
const n = path.resolve(t.rootDir);
const r = path.resolve(e.location.modulePath);
return r === n || r.startsWith(`${n}${path.sep}`);
}) : null) ?? n.find((t) => t.id === e.dirName) ?? n.find((t) => path.basename(t.rootDir) === e.dirName) ?? n.find((t) => t.channels.includes(e.dirName)) ?? null;
}
/** Resolves the stable plugin id used for telemetry and error reporting. */
function resolveTrackedFacadePluginId(e) {
return resolveBundledPluginManifestRecord(e)?.id ?? e.dirName;
}
/** Evaluates whether a bundled plugin's api/runtime-api facade is currently enabled. */
function resolveBundledPluginPublicSurfaceAccess(e) {
if (e.artifactBasename === "runtime-api.js" && ALWAYS_ALLOWED_RUNTIME_DIR_NAME_SET.has(e.dirName)) return {
allowed: true,
pluginId: e.dirName
};
const t = resolveBundledPluginManifestRecord(e);
if (!t) return {
allowed: false,
reason: `no bundled plugin manifest found for ${e.dirName}`
};
const n = getPluginRegistryState();
const r = getPluginRuntimeGatewayRequestScope()?.pluginRegistry ?? (n?.runtimeSubagentMode === "gateway-bindable" ? n.activeRegistry : void 0);
if (r) {
const e = r.plugins.find((e) => e.id === t.id);
return {
allowed: e?.status === "loaded",
pluginId: t.id,
...e?.status === "loaded" ? {} : { reason: "plugin runtime is not active" }
};
}
const { config: i, normalizedPluginsConfig: a, activationSource: o, autoEnabledReasons: s } = getFacadeBoundaryResolvedConfig();
const c = resolveEffectivePluginActivationState({
id: t.id,
origin: t.origin,
channelIds: t.channels,
config: a,
rootConfig: i,
enabledByDefault: isPluginEnabledByDefaultForPlatform(t),
activationSource: o,
autoEnabledReason: s[t.id]?.[0]
});
if (c.enabled) return {
allowed: true,
pluginId: t.id
};
return {
allowed: false,
pluginId: t.id,
reason: c.reason ?? "plugin runtime is not activated"
};
}
/** Resolves bundled facade access and throws unless the facade is allowed to load. */
function resolveActivatedBundledPluginPublicSurfaceAccessOrThrow(e) {
const t = resolveBundledPluginPublicSurfaceAccess(e);
if (!t.allowed) {
const n = t.pluginId ?? e.dirName;
throw new Error(`Bundled plugin public surface access blocked for "${n}" via ${e.dirName}/${e.artifactBasename}: ${t.reason ?? "plugin runtime is not activated"}`);
}
return t;
}
//#endregion
export { resolveActivatedBundledPluginPublicSurfaceAccessOrThrow, resolveBundledPluginPublicSurfaceAccess, resolveRegistryPluginModuleLocation, resolveTrackedFacadePluginId };