File size: 6,090 Bytes
25d91a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | // Builds plugin activation context from config, discovery, and manifests.
import { applyPluginAutoEnable } from "../config/plugin-auto-enable.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { withBundledPluginEnablementCompat } from "./bundled-compat.js";
import {
createPluginActivationSource,
normalizePluginsConfig,
type NormalizedPluginsConfig,
type PluginActivationConfigSource,
} from "./config-state.js";
import { getCurrentPluginMetadataSnapshot } from "./current-plugin-metadata-snapshot.js";
import type { PluginDiscoveryResult } from "./discovery.js";
import type { PluginManifestRegistry } from "./manifest-registry.js";
type PluginActivationInputs = {
rawConfig?: OpenClawConfig;
config?: OpenClawConfig;
normalized: NormalizedPluginsConfig;
activationSourceConfig?: OpenClawConfig;
activationSource: PluginActivationConfigSource;
autoEnabledReasons: Record<string, string[]>;
};
type PluginActivationParams = {
rawConfig?: OpenClawConfig;
resolvedConfig?: OpenClawConfig;
autoEnabledReasons?: Record<string, string[]>;
env?: NodeJS.ProcessEnv;
workspaceDir?: string;
applyAutoEnable?: boolean;
discovery?: PluginDiscoveryResult;
manifestRegistry?: PluginManifestRegistry;
};
type BundledCompatActivationParams = PluginActivationParams & {
activation?: "defaults" | "selected";
onlyPluginIds?: readonly string[];
resolveBundledPluginIds: (params: {
config?: OpenClawConfig;
workspaceDir?: string;
env?: NodeJS.ProcessEnv;
onlyPluginIds?: readonly string[];
manifestRegistry?: PluginManifestRegistry;
}) => string[];
};
export function withActivatedPluginIds(params: {
config?: OpenClawConfig;
pluginIds: readonly string[];
overrideGlobalDisable?: boolean;
overrideExplicitDisable?: boolean;
}): OpenClawConfig | undefined {
if (params.pluginIds.length === 0) {
return params.config;
}
const originalAllow = params.config?.plugins?.allow ?? [];
const useAllowlistDiscovery = originalAllow.length > 0;
const originalAllowSet = useAllowlistDiscovery ? new Set(originalAllow) : undefined;
const allow = new Set(originalAllow);
const entries = {
...params.config?.plugins?.entries,
};
for (const pluginId of params.pluginIds) {
const normalized = pluginId.trim();
if (!normalized) {
continue;
}
if (originalAllowSet && !originalAllowSet.has(normalized)) {
continue;
}
allow.add(normalized);
const existingEntry = entries[normalized];
entries[normalized] = {
...existingEntry,
enabled: existingEntry?.enabled !== false || params.overrideExplicitDisable === true,
};
}
const forcePluginsEnabled =
params.overrideGlobalDisable === true && params.config?.plugins?.enabled === false;
return {
...params.config,
plugins: {
...params.config?.plugins,
...(forcePluginsEnabled ? { enabled: true } : {}),
...(allow.size > 0 ? { allow: [...allow] } : {}),
entries,
},
};
}
function applyPluginAutoEnableForActivation(params: {
config: OpenClawConfig;
env: NodeJS.ProcessEnv;
workspaceDir?: string;
discovery?: PluginDiscoveryResult;
manifestRegistry?: PluginManifestRegistry;
}) {
const currentSnapshot = params.manifestRegistry
? undefined
: getCurrentPluginMetadataSnapshot({
config: params.config,
env: params.env,
workspaceDir: params.workspaceDir,
allowWorkspaceScopedSnapshot: true,
});
const defaultDiscoverySnapshot =
!params.manifestRegistry && normalizePluginsConfig(params.config.plugins).loadPaths.length === 0
? getCurrentPluginMetadataSnapshot({
env: params.env,
workspaceDir: params.workspaceDir,
allowWorkspaceScopedSnapshot: true,
requireDefaultDiscoveryContext: true,
})
: undefined;
const currentManifestRegistry =
params.manifestRegistry ??
currentSnapshot?.manifestRegistry ??
defaultDiscoverySnapshot?.manifestRegistry;
return applyPluginAutoEnable({
config: params.config,
env: params.env,
manifestRegistry: currentManifestRegistry,
discovery:
params.discovery ?? currentSnapshot?.discovery ?? defaultDiscoverySnapshot?.discovery,
});
}
function resolvePluginActivationInputs(params: PluginActivationParams): PluginActivationInputs {
const env = params.env ?? process.env;
const rawConfig = params.rawConfig ?? params.resolvedConfig;
let resolvedConfig = params.resolvedConfig ?? params.rawConfig;
let autoEnabledReasons = params.autoEnabledReasons;
if (params.applyAutoEnable && rawConfig !== undefined) {
const autoEnabled = applyPluginAutoEnableForActivation({
config: rawConfig,
env,
workspaceDir: params.workspaceDir,
discovery: params.discovery,
manifestRegistry: params.manifestRegistry,
});
resolvedConfig = autoEnabled.config;
autoEnabledReasons = autoEnabled.autoEnabledReasons;
}
return {
rawConfig,
config: resolvedConfig,
normalized: normalizePluginsConfig(resolvedConfig?.plugins),
activationSourceConfig: rawConfig,
activationSource: createPluginActivationSource({
config: rawConfig,
}),
autoEnabledReasons: autoEnabledReasons ?? {},
};
}
export function resolveBundledCompatActivationInputs(
params: BundledCompatActivationParams,
): PluginActivationInputs {
const env = params.env ?? process.env;
const snapshot = resolvePluginActivationInputs({ ...params, env });
const bundledPluginIds = params.resolveBundledPluginIds({
config: snapshot.config,
workspaceDir: params.workspaceDir,
env,
onlyPluginIds: params.onlyPluginIds,
...(params.manifestRegistry ? { manifestRegistry: params.manifestRegistry } : {}),
});
const config = withBundledPluginEnablementCompat({
config: snapshot.config,
pluginIds: bundledPluginIds,
env,
...(params.activation ? { activation: params.activation } : {}),
});
return {
...snapshot,
config,
normalized: normalizePluginsConfig(config?.plugins),
};
}
|