mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 13:10:43 +00:00
fix: load staged dist-runtime plugins in docker
This commit is contained in:
@@ -437,7 +437,8 @@ function toSafeImportPath(specifier: string): string {
|
||||
function createPluginJitiLoader(options: Pick<PluginLoadOptions, "pluginSdkResolution">) {
|
||||
const jitiLoaders: PluginJitiLoaderCache = new Map();
|
||||
return (modulePath: string) => {
|
||||
const tryNative = shouldPreferNativeJiti(modulePath);
|
||||
const tryNative =
|
||||
shouldPreferNativeJiti(modulePath) && !isBundledRuntimeDependencyMirrorPath(modulePath);
|
||||
return getCachedPluginJitiLoader({
|
||||
cache: jitiLoaders,
|
||||
modulePath,
|
||||
@@ -453,8 +454,32 @@ function createPluginJitiLoader(options: Pick<PluginLoadOptions, "pluginSdkResol
|
||||
};
|
||||
}
|
||||
|
||||
function resolveCanonicalDistRuntimeSource(source: string): string {
|
||||
const marker = `${path.sep}dist-runtime${path.sep}extensions${path.sep}`;
|
||||
const index = source.indexOf(marker);
|
||||
if (index === -1) {
|
||||
return source;
|
||||
}
|
||||
const candidate = `${source.slice(0, index)}${path.sep}dist${path.sep}extensions${path.sep}${source.slice(index + marker.length)}`;
|
||||
return fs.existsSync(candidate) ? candidate : source;
|
||||
}
|
||||
|
||||
const registeredBundledRuntimeDepNodePaths = new Set<string>();
|
||||
|
||||
function isBundledRuntimeDependencyMirrorPath(modulePath: string): boolean {
|
||||
const resolvedModulePath = path.resolve(modulePath);
|
||||
for (const nodeModulesDir of registeredBundledRuntimeDepNodePaths) {
|
||||
const installRoot = path.dirname(nodeModulesDir);
|
||||
if (
|
||||
resolvedModulePath === installRoot ||
|
||||
resolvedModulePath.startsWith(`${installRoot}${path.sep}`)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function registerBundledRuntimeDependencyNodePath(installRoot: string): void {
|
||||
const nodeModulesDir = path.join(installRoot, "node_modules");
|
||||
if (registeredBundledRuntimeDepNodePaths.has(nodeModulesDir) || !fs.existsSync(nodeModulesDir)) {
|
||||
@@ -529,7 +554,8 @@ function prepareBundledPluginRuntimeDistMirror(params: {
|
||||
}): string {
|
||||
const sourceExtensionsRoot = path.dirname(params.pluginRoot);
|
||||
const sourceDistRoot = path.dirname(sourceExtensionsRoot);
|
||||
const mirrorDistRoot = path.join(params.installRoot, "dist");
|
||||
const sourceDistRootName = path.basename(sourceDistRoot);
|
||||
const mirrorDistRoot = path.join(params.installRoot, sourceDistRootName);
|
||||
const mirrorExtensionsRoot = path.join(mirrorDistRoot, "extensions");
|
||||
fs.mkdirSync(mirrorExtensionsRoot, { recursive: true, mode: 0o755 });
|
||||
for (const entry of fs.readdirSync(sourceDistRoot, { withFileTypes: true })) {
|
||||
@@ -551,6 +577,24 @@ function prepareBundledPluginRuntimeDistMirror(params: {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sourceDistRootName === "dist-runtime") {
|
||||
const sourceCanonicalDistRoot = path.join(path.dirname(sourceDistRoot), "dist");
|
||||
const targetCanonicalDistRoot = path.join(params.installRoot, "dist");
|
||||
if (fs.existsSync(sourceCanonicalDistRoot)) {
|
||||
const targetMatchesSource =
|
||||
fs.existsSync(targetCanonicalDistRoot) &&
|
||||
safeRealpathOrResolve(targetCanonicalDistRoot) ===
|
||||
safeRealpathOrResolve(sourceCanonicalDistRoot);
|
||||
if (!targetMatchesSource) {
|
||||
fs.rmSync(targetCanonicalDistRoot, { recursive: true, force: true });
|
||||
try {
|
||||
fs.symlinkSync(sourceCanonicalDistRoot, targetCanonicalDistRoot, "junction");
|
||||
} catch {
|
||||
copyBundledPluginRuntimeRoot(sourceCanonicalDistRoot, targetCanonicalDistRoot);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return mirrorExtensionsRoot;
|
||||
}
|
||||
|
||||
@@ -938,7 +982,33 @@ function resolvePluginModuleExport(moduleExport: unknown): {
|
||||
definition?: OpenClawPluginDefinition;
|
||||
register?: OpenClawPluginDefinition["register"];
|
||||
} {
|
||||
const resolved = unwrapDefaultModuleExport(moduleExport);
|
||||
const seen = new Set<unknown>();
|
||||
const candidates: unknown[] = [unwrapDefaultModuleExport(moduleExport), moduleExport];
|
||||
for (let index = 0; index < candidates.length && index < 12; index += 1) {
|
||||
const resolved = candidates[index];
|
||||
if (seen.has(resolved)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(resolved);
|
||||
if (typeof resolved === "function") {
|
||||
return {
|
||||
register: resolved as OpenClawPluginDefinition["register"],
|
||||
};
|
||||
}
|
||||
if (resolved && typeof resolved === "object") {
|
||||
const def = resolved as OpenClawPluginDefinition;
|
||||
const register = def.register ?? def.activate;
|
||||
if (typeof register === "function") {
|
||||
return { definition: def, register };
|
||||
}
|
||||
for (const key of ["default", "module"]) {
|
||||
if (key in def) {
|
||||
candidates.push((def as Record<string, unknown>)[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const resolved = candidates[0];
|
||||
if (typeof resolved === "function") {
|
||||
return {
|
||||
register: resolved as OpenClawPluginDefinition["register"],
|
||||
@@ -2132,9 +2202,11 @@ export function loadOpenClawPlugins(options: PluginLoadOptions = {}): PluginRegi
|
||||
runtimeSetupSource
|
||||
? runtimeSetupSource
|
||||
: runtimeCandidateSource;
|
||||
const moduleLoadSource = resolveCanonicalDistRuntimeSource(loadSource);
|
||||
const moduleRoot = resolveCanonicalDistRuntimeSource(runtimePluginRoot);
|
||||
const opened = openBoundaryFileSync({
|
||||
absolutePath: loadSource,
|
||||
rootPath: runtimePluginRoot,
|
||||
absolutePath: moduleLoadSource,
|
||||
rootPath: moduleRoot,
|
||||
boundaryLabel: "plugin root",
|
||||
rejectHardlinks: candidate.origin !== "bundled",
|
||||
skipLexicalRootCheck: true,
|
||||
|
||||
Reference in New Issue
Block a user