mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 03:20:25 +00:00
Plugins: add root-alias shim and cache/docs updates
This commit is contained in:
@@ -4,7 +4,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { withEnvAsync } from "../test-utils/env.js";
|
||||
import { discoverOpenClawPlugins } from "./discovery.js";
|
||||
import { clearPluginDiscoveryCache, discoverOpenClawPlugins } from "./discovery.js";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
@@ -57,6 +57,7 @@ function expectEscapesPackageDiagnostic(diagnostics: Array<{ message: string }>)
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
clearPluginDiscoveryCache();
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
try {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
@@ -350,4 +351,40 @@ describe("discoverOpenClawPlugins", () => {
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it("reuses discovery results from cache until cleared", async () => {
|
||||
const stateDir = makeTempDir();
|
||||
const globalExt = path.join(stateDir, "extensions");
|
||||
fs.mkdirSync(globalExt, { recursive: true });
|
||||
const pluginPath = path.join(globalExt, "cached.ts");
|
||||
fs.writeFileSync(pluginPath, "export default function () {}", "utf-8");
|
||||
|
||||
const first = await withEnvAsync(
|
||||
{
|
||||
OPENCLAW_PLUGIN_DISCOVERY_CACHE_MS: "5000",
|
||||
},
|
||||
async () => withStateDir(stateDir, async () => discoverOpenClawPlugins({})),
|
||||
);
|
||||
expect(first.candidates.some((candidate) => candidate.idHint === "cached")).toBe(true);
|
||||
|
||||
fs.rmSync(pluginPath, { force: true });
|
||||
|
||||
const second = await withEnvAsync(
|
||||
{
|
||||
OPENCLAW_PLUGIN_DISCOVERY_CACHE_MS: "5000",
|
||||
},
|
||||
async () => withStateDir(stateDir, async () => discoverOpenClawPlugins({})),
|
||||
);
|
||||
expect(second.candidates.some((candidate) => candidate.idHint === "cached")).toBe(true);
|
||||
|
||||
clearPluginDiscoveryCache();
|
||||
|
||||
const third = await withEnvAsync(
|
||||
{
|
||||
OPENCLAW_PLUGIN_DISCOVERY_CACHE_MS: "5000",
|
||||
},
|
||||
async () => withStateDir(stateDir, async () => discoverOpenClawPlugins({})),
|
||||
);
|
||||
expect(third.candidates.some((candidate) => candidate.idHint === "cached")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,6 +33,56 @@ export type PluginDiscoveryResult = {
|
||||
diagnostics: PluginDiagnostic[];
|
||||
};
|
||||
|
||||
const discoveryCache = new Map<string, { expiresAt: number; result: PluginDiscoveryResult }>();
|
||||
|
||||
// Keep a short cache window to collapse bursty reloads during startup flows.
|
||||
const DEFAULT_DISCOVERY_CACHE_MS = 1000;
|
||||
|
||||
export function clearPluginDiscoveryCache(): void {
|
||||
discoveryCache.clear();
|
||||
}
|
||||
|
||||
function resolveDiscoveryCacheMs(env: NodeJS.ProcessEnv): number {
|
||||
const raw = env.OPENCLAW_PLUGIN_DISCOVERY_CACHE_MS?.trim();
|
||||
if (raw === "" || raw === "0") {
|
||||
return 0;
|
||||
}
|
||||
if (!raw) {
|
||||
return DEFAULT_DISCOVERY_CACHE_MS;
|
||||
}
|
||||
const parsed = Number.parseInt(raw, 10);
|
||||
if (!Number.isFinite(parsed)) {
|
||||
return DEFAULT_DISCOVERY_CACHE_MS;
|
||||
}
|
||||
return Math.max(0, parsed);
|
||||
}
|
||||
|
||||
function shouldUseDiscoveryCache(env: NodeJS.ProcessEnv): boolean {
|
||||
const disabled = env.OPENCLAW_DISABLE_PLUGIN_DISCOVERY_CACHE?.trim();
|
||||
if (disabled) {
|
||||
return false;
|
||||
}
|
||||
return resolveDiscoveryCacheMs(env) > 0;
|
||||
}
|
||||
|
||||
function buildDiscoveryCacheKey(params: {
|
||||
workspaceDir?: string;
|
||||
extraPaths?: string[];
|
||||
ownershipUid?: number | null;
|
||||
}): string {
|
||||
const workspaceKey = params.workspaceDir ? resolveUserPath(params.workspaceDir) : "";
|
||||
const configExtensionsRoot = path.join(resolveConfigDir(), "extensions");
|
||||
const bundledRoot = resolveBundledPluginsDir() ?? "";
|
||||
const normalizedExtraPaths = (params.extraPaths ?? [])
|
||||
.filter((entry): entry is string => typeof entry === "string")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean)
|
||||
.map((entry) => resolveUserPath(entry))
|
||||
.toSorted();
|
||||
const ownershipUid = params.ownershipUid ?? currentUid();
|
||||
return `${workspaceKey}::${ownershipUid ?? "none"}::${configExtensionsRoot}::${bundledRoot}::${JSON.stringify(normalizedExtraPaths)}`;
|
||||
}
|
||||
|
||||
function currentUid(overrideUid?: number | null): number | null {
|
||||
if (overrideUid !== undefined) {
|
||||
return overrideUid;
|
||||
@@ -569,7 +619,23 @@ export function discoverOpenClawPlugins(params: {
|
||||
workspaceDir?: string;
|
||||
extraPaths?: string[];
|
||||
ownershipUid?: number | null;
|
||||
cache?: boolean;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
}): PluginDiscoveryResult {
|
||||
const env = params.env ?? process.env;
|
||||
const cacheEnabled = params.cache !== false && shouldUseDiscoveryCache(env);
|
||||
const cacheKey = buildDiscoveryCacheKey({
|
||||
workspaceDir: params.workspaceDir,
|
||||
extraPaths: params.extraPaths,
|
||||
ownershipUid: params.ownershipUid,
|
||||
});
|
||||
if (cacheEnabled) {
|
||||
const cached = discoveryCache.get(cacheKey);
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
return cached.result;
|
||||
}
|
||||
}
|
||||
|
||||
const candidates: PluginCandidate[] = [];
|
||||
const diagnostics: PluginDiagnostic[] = [];
|
||||
const seen = new Set<string>();
|
||||
@@ -634,5 +700,12 @@ export function discoverOpenClawPlugins(params: {
|
||||
seen,
|
||||
});
|
||||
|
||||
return { candidates, diagnostics };
|
||||
const result = { candidates, diagnostics };
|
||||
if (cacheEnabled) {
|
||||
const ttl = resolveDiscoveryCacheMs(env);
|
||||
if (ttl > 0) {
|
||||
discoveryCache.set(cacheKey, { expiresAt: Date.now() + ttl, result });
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -211,14 +211,19 @@ function createEscapingEntryFixture(params: { id: string; sourceBody: string })
|
||||
return { pluginDir, outsideEntry, linkedEntry };
|
||||
}
|
||||
|
||||
function createPluginSdkAliasFixture() {
|
||||
function createPluginSdkAliasFixture(params?: {
|
||||
srcFile?: string;
|
||||
distFile?: string;
|
||||
srcBody?: string;
|
||||
distBody?: string;
|
||||
}) {
|
||||
const root = makeTempDir();
|
||||
const srcFile = path.join(root, "src", "plugin-sdk", "index.ts");
|
||||
const distFile = path.join(root, "dist", "plugin-sdk", "index.js");
|
||||
const srcFile = path.join(root, "src", "plugin-sdk", params?.srcFile ?? "index.ts");
|
||||
const distFile = path.join(root, "dist", "plugin-sdk", params?.distFile ?? "index.js");
|
||||
fs.mkdirSync(path.dirname(srcFile), { recursive: true });
|
||||
fs.mkdirSync(path.dirname(distFile), { recursive: true });
|
||||
fs.writeFileSync(srcFile, "export {};\n", "utf-8");
|
||||
fs.writeFileSync(distFile, "export {};\n", "utf-8");
|
||||
fs.writeFileSync(srcFile, params?.srcBody ?? "export {};\n", "utf-8");
|
||||
fs.writeFileSync(distFile, params?.distBody ?? "export {};\n", "utf-8");
|
||||
return { root, srcFile, distFile };
|
||||
}
|
||||
|
||||
@@ -707,6 +712,73 @@ describe("loadOpenClawPlugins", () => {
|
||||
expect(a?.status).toBe("disabled");
|
||||
});
|
||||
|
||||
it("skips importing bundled memory plugins that are disabled by memory slot", () => {
|
||||
const bundledDir = makeTempDir();
|
||||
const memoryADir = path.join(bundledDir, "memory-a");
|
||||
const memoryBDir = path.join(bundledDir, "memory-b");
|
||||
fs.mkdirSync(memoryADir, { recursive: true });
|
||||
fs.mkdirSync(memoryBDir, { recursive: true });
|
||||
writePlugin({
|
||||
id: "memory-a",
|
||||
dir: memoryADir,
|
||||
filename: "index.cjs",
|
||||
body: `throw new Error("memory-a should not be imported when slot selects memory-b");`,
|
||||
});
|
||||
writePlugin({
|
||||
id: "memory-b",
|
||||
dir: memoryBDir,
|
||||
filename: "index.cjs",
|
||||
body: `module.exports = { id: "memory-b", kind: "memory", register() {} };`,
|
||||
});
|
||||
fs.writeFileSync(
|
||||
path.join(memoryADir, "openclaw.plugin.json"),
|
||||
JSON.stringify(
|
||||
{
|
||||
id: "memory-a",
|
||||
kind: "memory",
|
||||
configSchema: EMPTY_PLUGIN_SCHEMA,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
"utf-8",
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(memoryBDir, "openclaw.plugin.json"),
|
||||
JSON.stringify(
|
||||
{
|
||||
id: "memory-b",
|
||||
kind: "memory",
|
||||
configSchema: EMPTY_PLUGIN_SCHEMA,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
"utf-8",
|
||||
);
|
||||
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledDir;
|
||||
|
||||
const registry = loadOpenClawPlugins({
|
||||
cache: false,
|
||||
config: {
|
||||
plugins: {
|
||||
allow: ["memory-a", "memory-b"],
|
||||
slots: { memory: "memory-b" },
|
||||
entries: {
|
||||
"memory-a": { enabled: true },
|
||||
"memory-b": { enabled: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const a = registry.plugins.find((entry) => entry.id === "memory-a");
|
||||
const b = registry.plugins.find((entry) => entry.id === "memory-b");
|
||||
expect(a?.status).toBe("disabled");
|
||||
expect(String(a?.error ?? "")).toContain('memory slot set to "memory-b"');
|
||||
expect(b?.status).toBe("loaded");
|
||||
});
|
||||
|
||||
it("disables memory plugins when slot is none", () => {
|
||||
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins";
|
||||
const memory = writePlugin({
|
||||
@@ -1051,4 +1123,38 @@ describe("loadOpenClawPlugins", () => {
|
||||
);
|
||||
expect(resolved).toBe(srcFile);
|
||||
});
|
||||
|
||||
it("prefers dist root-alias shim when loader runs from dist", () => {
|
||||
const { root, distFile } = createPluginSdkAliasFixture({
|
||||
srcFile: "root-alias.cjs",
|
||||
distFile: "root-alias.cjs",
|
||||
srcBody: "module.exports = {};\n",
|
||||
distBody: "module.exports = {};\n",
|
||||
});
|
||||
|
||||
const resolved = __testing.resolvePluginSdkAliasFile({
|
||||
srcFile: "root-alias.cjs",
|
||||
distFile: "root-alias.cjs",
|
||||
modulePath: path.join(root, "dist", "plugins", "loader.js"),
|
||||
});
|
||||
expect(resolved).toBe(distFile);
|
||||
});
|
||||
|
||||
it("prefers src root-alias shim when loader runs from src in non-production", () => {
|
||||
const { root, srcFile } = createPluginSdkAliasFixture({
|
||||
srcFile: "root-alias.cjs",
|
||||
distFile: "root-alias.cjs",
|
||||
srcBody: "module.exports = {};\n",
|
||||
distBody: "module.exports = {};\n",
|
||||
});
|
||||
|
||||
const resolved = withEnv({ NODE_ENV: undefined }, () =>
|
||||
__testing.resolvePluginSdkAliasFile({
|
||||
srcFile: "root-alias.cjs",
|
||||
distFile: "root-alias.cjs",
|
||||
modulePath: path.join(root, "src", "plugins", "loader.ts"),
|
||||
}),
|
||||
);
|
||||
expect(resolved).toBe(srcFile);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -86,7 +86,7 @@ const resolvePluginSdkAliasFile = (params: {
|
||||
};
|
||||
|
||||
const resolvePluginSdkAlias = (): string | null =>
|
||||
resolvePluginSdkAliasFile({ srcFile: "index.ts", distFile: "index.js" });
|
||||
resolvePluginSdkAliasFile({ srcFile: "root-alias.cjs", distFile: "root-alias.cjs" });
|
||||
|
||||
const resolvePluginSdkAccountIdAlias = (): string | null => {
|
||||
return resolvePluginSdkAliasFile({ srcFile: "account-id.ts", distFile: "account-id.js" });
|
||||
@@ -96,6 +96,10 @@ const resolvePluginSdkCoreAlias = (): string | null => {
|
||||
return resolvePluginSdkAliasFile({ srcFile: "core.ts", distFile: "core.js" });
|
||||
};
|
||||
|
||||
const resolvePluginSdkCompatAlias = (): string | null => {
|
||||
return resolvePluginSdkAliasFile({ srcFile: "compat.ts", distFile: "compat.js" });
|
||||
};
|
||||
|
||||
const resolvePluginSdkTelegramAlias = (): string | null => {
|
||||
return resolvePluginSdkAliasFile({ srcFile: "telegram.ts", distFile: "telegram.js" });
|
||||
};
|
||||
@@ -468,6 +472,7 @@ export function loadOpenClawPlugins(options: PluginLoadOptions = {}): PluginRegi
|
||||
const discovery = discoverOpenClawPlugins({
|
||||
workspaceDir: options.workspaceDir,
|
||||
extraPaths: normalized.loadPaths,
|
||||
cache: options.cache,
|
||||
});
|
||||
const manifestRegistry = loadPluginManifestRegistry({
|
||||
config: cfg,
|
||||
@@ -501,6 +506,7 @@ export function loadOpenClawPlugins(options: PluginLoadOptions = {}): PluginRegi
|
||||
const pluginSdkAlias = resolvePluginSdkAlias();
|
||||
const pluginSdkAccountIdAlias = resolvePluginSdkAccountIdAlias();
|
||||
const pluginSdkCoreAlias = resolvePluginSdkCoreAlias();
|
||||
const pluginSdkCompatAlias = resolvePluginSdkCompatAlias();
|
||||
const pluginSdkTelegramAlias = resolvePluginSdkTelegramAlias();
|
||||
const pluginSdkDiscordAlias = resolvePluginSdkDiscordAlias();
|
||||
const pluginSdkSlackAlias = resolvePluginSdkSlackAlias();
|
||||
@@ -511,6 +517,7 @@ export function loadOpenClawPlugins(options: PluginLoadOptions = {}): PluginRegi
|
||||
const aliasMap = {
|
||||
...(pluginSdkAlias ? { "openclaw/plugin-sdk": pluginSdkAlias } : {}),
|
||||
...(pluginSdkCoreAlias ? { "openclaw/plugin-sdk/core": pluginSdkCoreAlias } : {}),
|
||||
...(pluginSdkCompatAlias ? { "openclaw/plugin-sdk/compat": pluginSdkCompatAlias } : {}),
|
||||
...(pluginSdkTelegramAlias ? { "openclaw/plugin-sdk/telegram": pluginSdkTelegramAlias } : {}),
|
||||
...(pluginSdkDiscordAlias ? { "openclaw/plugin-sdk/discord": pluginSdkDiscordAlias } : {}),
|
||||
...(pluginSdkSlackAlias ? { "openclaw/plugin-sdk/slack": pluginSdkSlackAlias } : {}),
|
||||
@@ -610,6 +617,25 @@ export function loadOpenClawPlugins(options: PluginLoadOptions = {}): PluginRegi
|
||||
continue;
|
||||
}
|
||||
|
||||
// Fast-path bundled memory plugins that are guaranteed disabled by slot policy.
|
||||
// This avoids opening/importing heavy memory plugin modules that will never register.
|
||||
if (candidate.origin === "bundled" && manifestRecord.kind === "memory") {
|
||||
const earlyMemoryDecision = resolveMemorySlotDecision({
|
||||
id: record.id,
|
||||
kind: "memory",
|
||||
slot: memorySlot,
|
||||
selectedId: selectedMemoryPluginId,
|
||||
});
|
||||
if (!earlyMemoryDecision.enabled) {
|
||||
record.enabled = false;
|
||||
record.status = "disabled";
|
||||
record.error = earlyMemoryDecision.reason;
|
||||
registry.plugins.push(record);
|
||||
seenIds.set(pluginId, candidate.origin);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!manifestRecord.configSchema) {
|
||||
pushPluginLoadError("missing config schema");
|
||||
continue;
|
||||
|
||||
@@ -46,7 +46,8 @@ export type PluginManifestRegistry = {
|
||||
|
||||
const registryCache = new Map<string, { expiresAt: number; registry: PluginManifestRegistry }>();
|
||||
|
||||
const DEFAULT_MANIFEST_CACHE_MS = 200;
|
||||
// Keep a short cache window to collapse bursty reloads during startup flows.
|
||||
const DEFAULT_MANIFEST_CACHE_MS = 1000;
|
||||
|
||||
export function clearPluginManifestRegistryCache(): void {
|
||||
registryCache.clear();
|
||||
|
||||
Reference in New Issue
Block a user