refactor: dedupe plugin activation helpers

This commit is contained in:
Peter Steinberger
2026-04-06 17:37:40 +01:00
parent 87617c44ba
commit 345c71f264
3 changed files with 79 additions and 67 deletions

View File

@@ -0,0 +1,50 @@
type EnableStateLike = {
enabled: boolean;
reason?: string;
};
type PluginKindLike = string | readonly string[] | undefined;
export function toEnableStateResult<TState extends EnableStateLike>(
state: TState,
): { enabled: boolean; reason?: string } {
return state.enabled ? { enabled: true } : { enabled: false, reason: state.reason };
}
function hasKind(kind: PluginKindLike, target: string): boolean {
if (!kind) {
return false;
}
return Array.isArray(kind) ? kind.includes(target) : kind === target;
}
export function resolveMemorySlotDecisionShared(params: {
id: string;
kind?: PluginKindLike;
slot: string | null | undefined;
selectedId: string | null;
}): { enabled: boolean; reason?: string; selected?: boolean } {
if (!hasKind(params.kind, "memory")) {
return { enabled: true };
}
// A dual-kind plugin (e.g. ["memory", "context-engine"]) that lost the
// memory slot must stay enabled so its other slot role can still load.
const isMultiKind = Array.isArray(params.kind) && params.kind.length > 1;
if (params.slot === null) {
return isMultiKind ? { enabled: true } : { enabled: false, reason: "memory slot disabled" };
}
if (typeof params.slot === "string") {
if (params.slot === params.id) {
return { enabled: true, selected: true };
}
return isMultiKind
? { enabled: true }
: { enabled: false, reason: `memory slot set to "${params.slot}"` };
}
if (params.selectedId && params.selectedId !== params.id) {
return isMultiKind
? { enabled: true }
: { enabled: false, reason: `memory slot already filled by "${params.selectedId}"` };
}
return { enabled: true, selected: true };
}

View File

@@ -1,4 +1,8 @@
import type { OpenClawConfig } from "../config/config.js";
import {
resolveMemorySlotDecisionShared,
toEnableStateResult,
} from "./config-activation-shared.js";
import {
hasExplicitPluginConfig as hasExplicitPluginConfigShared,
identityNormalizePluginId,
@@ -7,7 +11,6 @@ import {
type NormalizePluginId,
type NormalizedPluginsConfig as SharedNormalizedPluginsConfig,
} from "./config-normalization-shared.js";
import { hasKind } from "./slots.js";
import type { PluginKind, PluginOrigin } from "./types.js";
export type PluginActivationSource = "disabled" | "explicit" | "auto" | "default";
@@ -199,13 +202,14 @@ export function resolveEnableState(
config: NormalizedPluginsConfig,
enabledByDefault?: boolean,
): { enabled: boolean; reason?: string } {
const state = resolvePluginActivationState({
id,
origin,
config,
enabledByDefault,
});
return state.enabled ? { enabled: true } : { enabled: false, reason: state.reason };
return toEnableStateResult(
resolvePluginActivationState({
id,
origin,
config,
enabledByDefault,
}),
);
}
export function isBundledChannelEnabledByChannelConfig(
@@ -225,8 +229,7 @@ export function resolveEffectiveEnableState(params: {
sourceRootConfig?: OpenClawConfig;
autoEnabledReason?: string;
}): { enabled: boolean; reason?: string } {
const state = resolveEffectivePluginActivationState(params);
return state.enabled ? { enabled: true } : { enabled: false, reason: state.reason };
return toEnableStateResult(resolveEffectivePluginActivationState(params));
}
export function resolveEffectivePluginActivationState(params: {
@@ -248,27 +251,5 @@ export function resolveMemorySlotDecision(params: {
slot: string | null | undefined;
selectedId: string | null;
}): { enabled: boolean; reason?: string; selected?: boolean } {
if (!hasKind(params.kind, "memory")) {
return { enabled: true };
}
// A dual-kind plugin (e.g. ["memory", "context-engine"]) that lost the
// memory slot must stay enabled so its other slot role can still load.
const isMultiKind = Array.isArray(params.kind) && params.kind.length > 1;
if (params.slot === null) {
return isMultiKind ? { enabled: true } : { enabled: false, reason: "memory slot disabled" };
}
if (typeof params.slot === "string") {
if (params.slot === params.id) {
return { enabled: true, selected: true };
}
return isMultiKind
? { enabled: true }
: { enabled: false, reason: `memory slot set to "${params.slot}"` };
}
if (params.selectedId && params.selectedId !== params.id) {
return isMultiKind
? { enabled: true }
: { enabled: false, reason: `memory slot already filled by "${params.selectedId}"` };
}
return { enabled: true, selected: true };
return resolveMemorySlotDecisionShared(params);
}

View File

@@ -1,4 +1,8 @@
import type { OpenClawConfig } from "../config/config.js";
import {
resolveMemorySlotDecisionShared,
toEnableStateResult,
} from "./config-activation-shared.js";
import {
hasExplicitPluginConfig as hasExplicitPluginConfigShared,
isBundledChannelEnabledByChannelConfig as isBundledChannelEnabledByChannelConfigShared,
@@ -6,8 +10,7 @@ import {
type NormalizedPluginsConfig as SharedNormalizedPluginsConfig,
} from "./config-normalization-shared.js";
import { loadPluginManifestRegistry } from "./manifest-registry.js";
import { hasKind } from "./slots.js";
import type { PluginKind, PluginOrigin } from "./types.js";
import type { PluginOrigin } from "./types.js";
export type PluginActivationSource = "disabled" | "explicit" | "auto" | "default";
@@ -377,13 +380,14 @@ export function resolveEnableState(
config: NormalizedPluginsConfig,
enabledByDefault?: boolean,
): { enabled: boolean; reason?: string } {
const state = resolvePluginActivationState({
id,
origin,
config,
enabledByDefault,
});
return state.enabled ? { enabled: true } : { enabled: false, reason: state.reason };
return toEnableStateResult(
resolvePluginActivationState({
id,
origin,
config,
enabledByDefault,
}),
);
}
export function isBundledChannelEnabledByChannelConfig(
@@ -401,8 +405,7 @@ export function resolveEffectiveEnableState(params: {
enabledByDefault?: boolean;
activationSource?: PluginActivationConfigSource;
}): { enabled: boolean; reason?: string } {
const state = resolveEffectivePluginActivationState(params);
return state.enabled ? { enabled: true } : { enabled: false, reason: state.reason };
return toEnableStateResult(resolveEffectivePluginActivationState(params));
}
export function resolveEffectivePluginActivationState(params: {
@@ -423,27 +426,5 @@ export function resolveMemorySlotDecision(params: {
slot: string | null | undefined;
selectedId: string | null;
}): { enabled: boolean; reason?: string; selected?: boolean } {
if (!hasKind(params.kind as PluginKind | PluginKind[] | undefined, "memory")) {
return { enabled: true };
}
// A dual-kind plugin (e.g. ["memory", "context-engine"]) that lost the
// memory slot must stay enabled so its other slot role can still load.
const isMultiKind = Array.isArray(params.kind) && params.kind.length > 1;
if (params.slot === null) {
return isMultiKind ? { enabled: true } : { enabled: false, reason: "memory slot disabled" };
}
if (typeof params.slot === "string") {
if (params.slot === params.id) {
return { enabled: true, selected: true };
}
return isMultiKind
? { enabled: true }
: { enabled: false, reason: `memory slot set to "${params.slot}"` };
}
if (params.selectedId && params.selectedId !== params.id) {
return isMultiKind
? { enabled: true }
: { enabled: false, reason: `memory slot already filled by "${params.selectedId}"` };
}
return { enabled: true, selected: true };
return resolveMemorySlotDecisionShared(params);
}