perf(plugin-sdk): narrow provider contract config types

This commit is contained in:
Vincent Koc
2026-04-07 11:55:02 +01:00
parent 1e5b026e61
commit 9ea3da08df
3 changed files with 43 additions and 11 deletions

View File

@@ -1,8 +1,18 @@
import { isRecord } from "../utils.js";
import type { OpenClawConfig } from "./config.js";
type PluginWebSearchConfigCarrier = {
plugins?: {
entries?: Record<
string,
{
config?: unknown;
}
>;
};
};
export function resolvePluginWebSearchConfig(
config: OpenClawConfig | undefined,
config: PluginWebSearchConfigCarrier | undefined,
pluginId: string,
): Record<string, unknown> | undefined {
const pluginConfig = config?.plugins?.entries?.[pluginId]?.config;

View File

@@ -1,6 +1,13 @@
import type { OpenClawConfig } from "./config.js";
type PluginAllowlistConfigCarrier = {
plugins?: {
allow?: string[];
};
};
export function ensurePluginAllowlisted(cfg: OpenClawConfig, pluginId: string): OpenClawConfig {
export function ensurePluginAllowlisted<T extends PluginAllowlistConfigCarrier>(
cfg: T,
pluginId: string,
): T {
const allow = cfg.plugins?.allow;
if (!Array.isArray(allow) || allow.includes(pluginId)) {
return cfg;
@@ -11,5 +18,5 @@ export function ensurePluginAllowlisted(cfg: OpenClawConfig, pluginId: string):
...cfg.plugins,
allow: [...allow, pluginId],
},
};
} as T;
}

View File

@@ -1,8 +1,20 @@
import type { OpenClawConfig } from "../config/config.js";
import { ensurePluginAllowlisted } from "../config/plugins-allowlist.js";
export type PluginEnableResult = {
config: OpenClawConfig;
type ProviderPluginConfig = {
enabled?: boolean;
};
type ProviderEnableConfigCarrier = {
plugins?: {
enabled?: boolean;
deny?: string[];
allow?: string[];
entries?: Record<string, ProviderPluginConfig | undefined>;
};
};
export type PluginEnableResult<TConfig extends ProviderEnableConfigCarrier> = {
config: TConfig;
enabled: boolean;
reason?: string;
};
@@ -11,7 +23,10 @@ export type PluginEnableResult = {
* Provider contract surfaces only ever enable provider plugins, so they do not
* need the built-in channel normalization path from plugins/enable.ts.
*/
export function enablePluginInConfig(cfg: OpenClawConfig, pluginId: string): PluginEnableResult {
export function enablePluginInConfig<TConfig extends ProviderEnableConfigCarrier>(
cfg: TConfig,
pluginId: string,
): PluginEnableResult<TConfig> {
if (cfg.plugins?.enabled === false) {
return { config: cfg, enabled: false, reason: "plugins disabled" };
}
@@ -19,7 +34,7 @@ export function enablePluginInConfig(cfg: OpenClawConfig, pluginId: string): Plu
return { config: cfg, enabled: false, reason: "blocked by denylist" };
}
let next: OpenClawConfig = {
let next = {
...cfg,
plugins: {
...cfg.plugins,
@@ -31,7 +46,7 @@ export function enablePluginInConfig(cfg: OpenClawConfig, pluginId: string): Plu
},
},
},
};
} as TConfig;
next = ensurePluginAllowlisted(next, pluginId);
return { config: next, enabled: true };
}