openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
123 lines (122 loc) • 5.26 kB
JavaScript
import { o as getPluginCacheRoot, s as getPluginCacheSource } from "./plugin-cache-DGWspMEc.js";
import "./src-vebZIeLe.js";
import { t as expectDefined } from "./expect-CyE8FADM.js";
import { n as getRuntimeConfig } from "./io.runtime-B9iJRs3w.js";
import { r as pluginCacheExistsSync } from "./plugin-cache-files-DLPF_Tw2.js";
import { n as getCachedPluginSourceModuleLoader, o as recordPluginModuleRoot } from "./plugin-module-loader-cache-lf3kAaBw.js";
import { f as isJavaScriptModulePath, m as tryNativeRequireJavaScriptModule } from "./sdk-alias-BMTmaiMP.js";
import { n as loadPluginManifestRegistryCore } from "./manifest-registry-DCCgYk7q.js";
import "./config-Cs0XXL3x.js";
import path from "node:path";
//#region src/plugins/runtime/runtime-plugin-boundary.ts
function readPluginBoundaryConfigSafely() {
try {
return getRuntimeConfig();
} catch {
return {};
}
}
function resolvePluginRuntimeRecordByEntryBaseNames(entryBaseNames, onMissing) {
const matches = loadPluginManifestRegistryCore({ config: readPluginBoundaryConfigSafely() }).plugins.filter((plugin) => {
if (!plugin?.source) return false;
const record = {
rootDir: plugin.rootDir,
source: plugin.source
};
return entryBaseNames.every((entryBaseName) => resolvePluginRuntimeModulePath(record, entryBaseName) !== null);
});
if (matches.length === 0) {
if (onMissing) onMissing();
return null;
}
if (matches.length > 1) {
const pluginIds = matches.map((plugin) => plugin.id).join(", ");
throw new Error(`plugin runtime boundary is ambiguous for entries [${entryBaseNames.join(", ")}]: ${pluginIds}`);
}
const record = expectDefined(matches[0], "matches capture group 0");
return {
...record.origin ? { origin: record.origin } : {},
rootDir: record.rootDir,
source: record.source
};
}
function resolvePluginRuntimeModulePath(record, entryBaseName, onMissing) {
const rootDir = record.rootDir ?? path.dirname(record.source);
const artifacts = getPluginCacheRoot(rootDir).artifacts;
const key = `runtime-boundary:${record.source}:${entryBaseName}`;
const cached = artifacts.get(key);
if (cached !== void 0) {
if (!cached && onMissing) onMissing();
return cached?.modulePath ?? null;
}
const candidates = [
path.join(path.dirname(record.source), `${entryBaseName}.js`),
path.join(path.dirname(record.source), `${entryBaseName}.ts`),
...record.rootDir ? [path.join(record.rootDir, `${entryBaseName}.js`), path.join(record.rootDir, `${entryBaseName}.ts`)] : []
];
for (const candidate of candidates) if (pluginCacheExistsSync(candidate)) {
artifacts.set(key, {
modulePath: candidate,
boundaryRoot: rootDir
});
return candidate;
}
artifacts.set(key, null);
if (onMissing) onMissing();
return null;
}
function loadPluginBoundaryModule(modulePath, options = {}) {
const source = getPluginCacheSource(modulePath);
const key = options.origin === "bundled" ? "bundled-runtime-boundary" : "runtime-boundary";
const cached = source.variants.get(key)?.exports;
if (cached) return cached.value;
recordPluginModuleRoot(modulePath, options.rootDir ?? path.dirname(modulePath));
if (isJavaScriptModulePath(modulePath)) {
const native = tryNativeRequireJavaScriptModule(modulePath, {
allowWindows: true,
fallbackOnNativeError: options.origin !== "bundled"
});
if (native.ok) {
source.variants.set(key, { exports: { value: native.moduleExport } });
return native.moduleExport;
}
if (options.origin === "bundled") throw new Error(`bundled plugin runtime module must load natively: ${modulePath}`);
} else if (options.origin === "bundled") throw new Error(`bundled plugin runtime module must be built JavaScript: ${modulePath}`);
return getCachedPluginSourceModuleLoader({
modulePath,
rootDir: options.rootDir,
importerUrl: import.meta.url,
loaderFilename: import.meta.url
})(modulePath);
}
//#endregion
//#region src/plugins/runtime/runtime-web-channel-plugin.ts
/** Resolves the active web-channel plugin record that provides runtime APIs. */
function resolveWebChannelPluginRecord() {
return resolvePluginRuntimeRecordByEntryBaseNames(["light-runtime-api", "runtime-api"], () => {
throw new Error("web channel plugin runtime is unavailable: missing plugin that provides light-runtime-api and runtime-api");
});
}
function resolveWebChannelRuntimeModulePath(record, entryBaseName) {
const modulePath = resolvePluginRuntimeModulePath(record, entryBaseName, () => {
throw new Error(`web channel plugin runtime is unavailable: missing ${entryBaseName}`);
});
if (!modulePath) throw new Error(`web channel plugin runtime is unavailable: missing ${entryBaseName}`);
return modulePath;
}
function loadWebChannelHeavyModuleSync() {
const record = resolveWebChannelPluginRecord();
return loadPluginBoundaryModule(resolveWebChannelRuntimeModulePath(record, "runtime-api"), {
origin: record.origin,
rootDir: record.rootDir ?? path.dirname(record.source)
});
}
async function loadWebChannelHeavyModule() {
return loadWebChannelHeavyModuleSync();
}
/** Starts web-channel monitoring through the heavy runtime API. */
function monitorWebChannel(...args) {
return loadWebChannelHeavyModule().then((loaded) => loaded.monitorWebChannel(...args));
}
//#endregion
export { monitorWebChannel };