Skip to content

Commit 02a60e0

Browse files
authored
refactor(plugins): unify setup registration and artifact selection (#130474)
1 parent 5153f25 commit 02a60e0

4 files changed

Lines changed: 299 additions & 263 deletions

File tree

‎src/plugins/plugin-runtime-artifact-resolution.ts‎

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "node:path";
44
import { resolveRealpathOrAbsolute } from "../infra/boundary-path.js";
55
import type { OpenClawPackageManifest } from "./manifest.js";
66
import type { PluginOrigin } from "./plugin-origin.types.js";
7+
import { resolvePreferredBuiltRuntimeArtifact } from "./plugin-runtime-artifact-selection.js";
78
import type { PluginRegistry } from "./registry-types.js";
89
import { getActivePluginRegistry, requireActivePluginRegistry } from "./runtime.js";
910

@@ -24,141 +25,6 @@ export function resolveCanonicalDistRuntimeSource(source: string): string {
2425
return fs.existsSync(candidate) ? candidate : source;
2526
}
2627

27-
function rewriteBundledRuntimeArtifactRelativePath(relativePath: string): string {
28-
return relativePath.replace(/\.[^.]+$/u, ".js");
29-
}
30-
31-
function listPackageLocalRuntimeArtifactOutputExtensions(sourceExt: string): string[] {
32-
switch (sourceExt) {
33-
case ".mts":
34-
case ".mjs":
35-
return [".mjs", ".js", ".cjs"];
36-
case ".cts":
37-
case ".cjs":
38-
return [".cjs", ".js", ".mjs"];
39-
default:
40-
return [".js", ".mjs", ".cjs"];
41-
}
42-
}
43-
44-
function listPackageLocalRuntimeArtifactRelativePathBases(relativePath: string): string[] {
45-
const ext = path.extname(relativePath).toLowerCase();
46-
const withoutExt = ext ? relativePath.slice(0, -ext.length) : relativePath;
47-
if (!withoutExt.startsWith(`src${path.sep}`) && !withoutExt.startsWith("src/")) {
48-
return [withoutExt];
49-
}
50-
return [withoutExt.slice(4), withoutExt];
51-
}
52-
53-
function listPackageLocalDistRuntimeArtifactRelativePaths(relativePath: string): string[] {
54-
const ext = path.extname(relativePath).toLowerCase();
55-
const candidates = new Set<string>();
56-
for (const base of listPackageLocalRuntimeArtifactRelativePathBases(relativePath)) {
57-
for (const outputExt of listPackageLocalRuntimeArtifactOutputExtensions(ext)) {
58-
candidates.add(`${base}${outputExt}`);
59-
}
60-
}
61-
return [...candidates];
62-
}
63-
64-
function shouldPreferPackageLocalDistRuntimeArtifact(source: string): boolean {
65-
switch (path.extname(source).toLowerCase()) {
66-
case ".ts":
67-
case ".tsx":
68-
case ".mts":
69-
case ".cts":
70-
return true;
71-
default:
72-
return false;
73-
}
74-
}
75-
76-
function resolvePackageLocalDistRuntimeArtifact(params: {
77-
source: string;
78-
rootDir: string;
79-
}): string | null {
80-
const relativeSource = path.relative(params.rootDir, params.source);
81-
if (
82-
!shouldPreferPackageLocalDistRuntimeArtifact(relativeSource) ||
83-
relativeSource === "" ||
84-
relativeSource.startsWith("..") ||
85-
path.isAbsolute(relativeSource)
86-
) {
87-
return null;
88-
}
89-
const artifactRoot = path.join(params.rootDir, "dist");
90-
for (const artifactRelativePath of listPackageLocalDistRuntimeArtifactRelativePaths(
91-
relativeSource,
92-
)) {
93-
const artifactSource = path.join(artifactRoot, artifactRelativePath);
94-
if (fs.existsSync(artifactSource)) {
95-
return resolveRealpathOrAbsolute(artifactSource);
96-
}
97-
}
98-
return null;
99-
}
100-
101-
function resolvePreferredBuiltRuntimeArtifact(params: {
102-
source: string;
103-
rootDir: string;
104-
origin: PluginOrigin;
105-
preferBuiltPluginArtifacts: boolean;
106-
packageManifest?: OpenClawPackageManifest;
107-
}): { source: string; rootDir: string } {
108-
const rootDir = resolveRealpathOrAbsolute(params.rootDir);
109-
const source = resolveRealpathOrAbsolute(params.source);
110-
if (!params.preferBuiltPluginArtifacts) {
111-
return { source, rootDir };
112-
}
113-
if (params.origin !== "bundled") {
114-
const artifactSource = resolvePackageLocalDistRuntimeArtifact({ source, rootDir });
115-
if (artifactSource) {
116-
return { source: artifactSource, rootDir };
117-
}
118-
return { source, rootDir };
119-
}
120-
// Source-external plugins keep source authoritative over package-local output;
121-
// only the lifecycle-owned canonical root build may replace that pair.
122-
const sourceExternal = params.packageManifest?.build?.bundledDist === false;
123-
const packageLocalArtifactSource = sourceExternal
124-
? null
125-
: resolvePackageLocalDistRuntimeArtifact({ source, rootDir });
126-
if (packageLocalArtifactSource) {
127-
return { source: packageLocalArtifactSource, rootDir };
128-
}
129-
const extensionsDir = path.dirname(rootDir);
130-
if (path.basename(extensionsDir) !== "extensions") {
131-
return { source, rootDir };
132-
}
133-
const packageRoot = path.dirname(extensionsDir);
134-
if (path.basename(packageRoot) === "dist" || path.basename(packageRoot) === "dist-runtime") {
135-
return { source, rootDir };
136-
}
137-
const relativeSource = path.relative(rootDir, source);
138-
if (relativeSource === "" || relativeSource.startsWith("..") || path.isAbsolute(relativeSource)) {
139-
return { source, rootDir };
140-
}
141-
const artifactRelativePath = rewriteBundledRuntimeArtifactRelativePath(relativeSource);
142-
// Source-external packaging can replace the flat root build while leaving its
143-
// staging wrapper behind, so only bundled artifacts may fall back to dist-runtime.
144-
for (const artifactRootName of sourceExternal ? ["dist"] : ["dist-runtime", "dist"]) {
145-
const artifactRoot = path.join(
146-
packageRoot,
147-
artifactRootName,
148-
"extensions",
149-
path.basename(rootDir),
150-
);
151-
const artifactSource = path.join(artifactRoot, artifactRelativePath);
152-
if (fs.existsSync(artifactSource)) {
153-
return {
154-
source: resolveRealpathOrAbsolute(artifactSource),
155-
rootDir: resolveRealpathOrAbsolute(artifactRoot),
156-
};
157-
}
158-
}
159-
return { source, rootDir };
160-
}
161-
16228
/** Applies both loader selection phases in their runtime order. */
16329
export function resolvePluginRuntimeArtifact(params: {
16430
pluginId: string;
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/** Selects built plugin artifacts without importing active runtime state. */
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
import { resolveRealpathOrAbsolute } from "../infra/boundary-path.js";
5+
import type { OpenClawPackageManifest } from "./manifest.js";
6+
import type { PluginOrigin } from "./plugin-origin.types.js";
7+
8+
function rewriteBundledRuntimeArtifactRelativePath(relativePath: string): string {
9+
return relativePath.replace(/\.[^.]+$/u, ".js");
10+
}
11+
12+
function listPackageLocalRuntimeArtifactOutputExtensions(sourceExt: string): string[] {
13+
switch (sourceExt) {
14+
case ".mts":
15+
case ".mjs":
16+
return [".mjs", ".js", ".cjs"];
17+
case ".cts":
18+
case ".cjs":
19+
return [".cjs", ".js", ".mjs"];
20+
default:
21+
return [".js", ".mjs", ".cjs"];
22+
}
23+
}
24+
25+
function listPackageLocalRuntimeArtifactRelativePathBases(relativePath: string): string[] {
26+
const ext = path.extname(relativePath).toLowerCase();
27+
const withoutExt = ext ? relativePath.slice(0, -ext.length) : relativePath;
28+
if (!withoutExt.startsWith(`src${path.sep}`) && !withoutExt.startsWith("src/")) {
29+
return [withoutExt];
30+
}
31+
return [withoutExt.slice(4), withoutExt];
32+
}
33+
34+
function listPackageLocalDistRuntimeArtifactRelativePaths(relativePath: string): string[] {
35+
const ext = path.extname(relativePath).toLowerCase();
36+
const candidates = new Set<string>();
37+
for (const base of listPackageLocalRuntimeArtifactRelativePathBases(relativePath)) {
38+
for (const outputExt of listPackageLocalRuntimeArtifactOutputExtensions(ext)) {
39+
candidates.add(`${base}${outputExt}`);
40+
}
41+
}
42+
return [...candidates];
43+
}
44+
45+
function shouldPreferPackageLocalDistRuntimeArtifact(source: string): boolean {
46+
switch (path.extname(source).toLowerCase()) {
47+
case ".ts":
48+
case ".tsx":
49+
case ".mts":
50+
case ".cts":
51+
return true;
52+
default:
53+
return false;
54+
}
55+
}
56+
57+
function resolvePackageLocalDistRuntimeArtifact(params: {
58+
source: string;
59+
rootDir: string;
60+
}): string | null {
61+
const relativeSource = path.relative(params.rootDir, params.source);
62+
if (
63+
!shouldPreferPackageLocalDistRuntimeArtifact(relativeSource) ||
64+
relativeSource === "" ||
65+
relativeSource.startsWith("..") ||
66+
path.isAbsolute(relativeSource)
67+
) {
68+
return null;
69+
}
70+
const artifactRoot = path.join(params.rootDir, "dist");
71+
for (const artifactRelativePath of listPackageLocalDistRuntimeArtifactRelativePaths(
72+
relativeSource,
73+
)) {
74+
const artifactSource = path.join(artifactRoot, artifactRelativePath);
75+
if (fs.existsSync(artifactSource)) {
76+
return resolveRealpathOrAbsolute(artifactSource);
77+
}
78+
}
79+
return null;
80+
}
81+
82+
function resolvePreferredBundledRootArtifactFromCanonicalPaths(params: {
83+
source: string;
84+
rootDir: string;
85+
packageManifest?: OpenClawPackageManifest;
86+
}): { source: string; rootDir: string } {
87+
const { rootDir, source } = params;
88+
const sourceExternal = params.packageManifest?.build?.bundledDist === false;
89+
const extensionsDir = path.dirname(rootDir);
90+
if (path.basename(extensionsDir) !== "extensions") {
91+
return { source, rootDir };
92+
}
93+
const packageRoot = path.dirname(extensionsDir);
94+
if (path.basename(packageRoot) === "dist" || path.basename(packageRoot) === "dist-runtime") {
95+
return { source, rootDir };
96+
}
97+
const relativeSource = path.relative(rootDir, source);
98+
if (relativeSource === "" || relativeSource.startsWith("..") || path.isAbsolute(relativeSource)) {
99+
return { source, rootDir };
100+
}
101+
const artifactRelativePath = rewriteBundledRuntimeArtifactRelativePath(relativeSource);
102+
// Source-external packaging can replace the flat root build while leaving its
103+
// staging wrapper behind, so only bundled artifacts may fall back to dist-runtime.
104+
for (const artifactRootName of sourceExternal ? ["dist"] : ["dist-runtime", "dist"]) {
105+
const artifactRoot = path.join(
106+
packageRoot,
107+
artifactRootName,
108+
"extensions",
109+
path.basename(rootDir),
110+
);
111+
const artifactSource = path.join(artifactRoot, artifactRelativePath);
112+
if (fs.existsSync(artifactSource)) {
113+
return {
114+
source: resolveRealpathOrAbsolute(artifactSource),
115+
rootDir: resolveRealpathOrAbsolute(artifactRoot),
116+
};
117+
}
118+
}
119+
return { source, rootDir };
120+
}
121+
122+
/** Selects the lifecycle-owned root build for one bundled source artifact. */
123+
export function resolvePreferredBundledRootArtifact(params: {
124+
source: string;
125+
rootDir: string;
126+
packageManifest?: OpenClawPackageManifest;
127+
}): { source: string; rootDir: string } {
128+
return resolvePreferredBundledRootArtifactFromCanonicalPaths({
129+
source: resolveRealpathOrAbsolute(params.source),
130+
rootDir: resolveRealpathOrAbsolute(params.rootDir),
131+
packageManifest: params.packageManifest,
132+
});
133+
}
134+
135+
/** Applies source, package-local, and root-build preference without runtime memo state. */
136+
export function resolvePreferredBuiltRuntimeArtifact(params: {
137+
source: string;
138+
rootDir: string;
139+
origin: PluginOrigin;
140+
preferBuiltPluginArtifacts: boolean;
141+
packageManifest?: OpenClawPackageManifest;
142+
}): { source: string; rootDir: string } {
143+
// The stateful resolver canonicalizes both paths before memo-key construction.
144+
const { rootDir, source } = params;
145+
if (!params.preferBuiltPluginArtifacts) {
146+
return { source, rootDir };
147+
}
148+
if (params.origin !== "bundled") {
149+
const artifactSource = resolvePackageLocalDistRuntimeArtifact({ source, rootDir });
150+
return artifactSource ? { source: artifactSource, rootDir } : { source, rootDir };
151+
}
152+
// Source-external plugins keep source authoritative over package-local output;
153+
// only the lifecycle-owned canonical root build may replace that pair.
154+
const sourceExternal = params.packageManifest?.build?.bundledDist === false;
155+
const packageLocalArtifactSource = sourceExternal
156+
? null
157+
: resolvePackageLocalDistRuntimeArtifact({ source, rootDir });
158+
if (packageLocalArtifactSource) {
159+
return { source: packageLocalArtifactSource, rootDir };
160+
}
161+
return resolvePreferredBundledRootArtifactFromCanonicalPaths({
162+
source,
163+
rootDir,
164+
packageManifest: params.packageManifest,
165+
});
166+
}

0 commit comments

Comments
 (0)