refactor(runtime): lazy-load control-ui and channel-config surfaces

This commit is contained in:
Peter Steinberger
2026-04-04 08:59:38 +01:00
parent f9e9d4e357
commit be15805a84
7 changed files with 215 additions and 50 deletions

View File

@@ -3,7 +3,6 @@ import fs from "node:fs";
import path from "node:path";
import { createJiti } from "jiti";
import type { ChannelPlugin } from "../channels/plugins/types.js";
import { isChannelConfigured } from "../config/channel-configured.js";
import type { OpenClawConfig } from "../config/config.js";
import type { PluginInstallRecord } from "../config/types.plugins.js";
import type { GatewayRequestHandler } from "../gateway/server-methods/types.js";
@@ -163,6 +162,38 @@ export function clearPluginLoaderCache(): void {
const defaultLogger = () => createSubsystemLogger("plugins");
type ChannelConfiguredModule = {
isChannelConfigured?: (
cfg: OpenClawConfig,
channelId: string,
env?: NodeJS.ProcessEnv,
) => boolean;
};
let channelConfiguredLoader: ReturnType<typeof createJiti> | undefined;
let cachedChannelConfiguredModule: ChannelConfiguredModule | undefined;
function resolveChannelConfiguredModule(): ChannelConfiguredModule {
if (cachedChannelConfiguredModule) {
return cachedChannelConfiguredModule;
}
channelConfiguredLoader ??= createJiti(import.meta.url, { interopDefault: true });
const loaded = channelConfiguredLoader("../config/channel-configured.js");
if (!loaded || typeof loaded !== "object") {
throw new Error("failed to load config/channel-configured runtime");
}
cachedChannelConfiguredModule = loaded as ChannelConfiguredModule;
return cachedChannelConfiguredModule;
}
function resolveIsChannelConfigured() {
const fn = resolveChannelConfiguredModule().isChannelConfigured;
if (typeof fn !== "function") {
throw new Error("config/channel-configured runtime missing isChannelConfigured()");
}
return fn;
}
function createPluginJitiLoader(options: Pick<PluginLoadOptions, "pluginSdkResolution">) {
const jitiLoaders = new Map<string, ReturnType<typeof createJiti>>();
return (modulePath: string) => {
@@ -545,6 +576,7 @@ function shouldLoadChannelPluginInSetupRuntime(params: {
) {
return true;
}
const isChannelConfigured = resolveIsChannelConfigured();
return !params.manifestChannels.some((channelId) =>
isChannelConfigured(params.cfg, channelId, params.env),
);