Files
openclaw/src/cli/plugins-registry-refresh.ts
snowzlmbot 604d607311 fix(onboard): refresh provider plugin registry after setup installs (#95792)
Merged via squash.

Prepared head SHA: c99d09f762
Co-authored-by: snowzlmbot <293528334+snowzlmbot@users.noreply.github.com>
Co-authored-by: steipete <58493+steipete@users.noreply.github.com>
Reviewed-by: @steipete
2026-06-22 09:55:08 -07:00

65 lines
2.7 KiB
TypeScript

// Registry refresh helper shared by plugin config mutations that need post-write discovery repair.
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { formatErrorMessage } from "../infra/errors.js";
import { loadInstalledPluginIndexInstallRecords } from "../plugins/installed-plugin-index-records.js";
import type { InstalledPluginIndexRefreshReason } from "../plugins/installed-plugin-index.js";
import { tracePluginLifecyclePhaseAsync } from "../plugins/plugin-lifecycle-trace.js";
import { refreshPluginRegistry } from "../plugins/plugin-registry.js";
/** Optional warning sink for best-effort registry/cache refresh failures. */
export type PluginRegistryRefreshLogger = {
warn?: (message: string) => void;
};
/** Refresh persisted plugin registry and clear runtime discovery after a config mutation. */
export async function refreshPluginRegistryAfterConfigMutation(params: {
config: OpenClawConfig;
reason: InstalledPluginIndexRefreshReason;
workspaceDir?: string;
env?: NodeJS.ProcessEnv;
installRecords?: Awaited<ReturnType<typeof loadInstalledPluginIndexInstallRecords>>;
invalidateRuntimeCache?: boolean;
policyPluginIds?: readonly string[];
traceCommand?: string;
logger?: PluginRegistryRefreshLogger;
}): Promise<void> {
try {
const installRecords =
params.installRecords ??
(await tracePluginLifecyclePhaseAsync(
"install records load",
() => loadInstalledPluginIndexInstallRecords(params.env ? { env: params.env } : {}),
{ command: params.traceCommand ?? "registry-refresh" },
));
await tracePluginLifecyclePhaseAsync(
"registry refresh",
() =>
refreshPluginRegistry({
config: params.config,
reason: params.reason,
installRecords,
...(params.policyPluginIds ? { policyPluginIds: params.policyPluginIds } : {}),
...(params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}),
...(params.env ? { env: params.env } : {}),
}),
{ command: params.traceCommand ?? "registry-refresh", reason: params.reason },
);
} catch (error) {
params.logger?.warn?.(`Plugin registry refresh failed: ${formatErrorMessage(error)}`);
}
if (params.invalidateRuntimeCache !== false) {
await invalidatePluginRuntimeDiscoveryAfterConfigMutation(params);
}
}
export async function invalidatePluginRuntimeDiscoveryAfterConfigMutation(params: {
logger?: PluginRegistryRefreshLogger;
}): Promise<void> {
try {
const { clearPluginRegistryLoadCache } = await import("../plugins/loader.js");
clearPluginRegistryLoadCache();
} catch (error) {
params.logger?.warn?.(`Plugin runtime cache invalidation failed: ${formatErrorMessage(error)}`);
}
}