refactor(cli): reuse function-shaped lazy loaders (#131144)

This commit is contained in:
Peter Steinberger
2026-08-27 13:24:50 -07:00
committed by GitHub
parent 621f7c43f7
commit 3bc404d7c6
3 changed files with 25 additions and 126 deletions
+8 -40
View File
@@ -66,7 +66,7 @@ import {
detectPluginVersionDrift,
type PluginVersionDriftReport,
} from "../../plugins/plugin-version-drift.js";
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
import { createLazyPromise } from "../../shared/lazy-promise.js";
import { VERSION } from "../../version.js";
import { resolveGatewayLocalPortOverride } from "../gateway-port-option.js";
import { parseTimeoutMsWithFallback } from "../parse-timeout.js";
@@ -133,45 +133,13 @@ type CliStatusSummary = {
type GatewayConnectFailureKind = ReturnType<typeof classifyGatewayConnectFailure>["kind"];
const gatewayProbeAuthModuleLoader = createLazyImportLoader(
() => import("../../gateway/probe-auth.js"),
);
const daemonInspectModuleLoader = createLazyImportLoader(() => import("../../daemon/inspect.js"));
const launchdModuleLoader = createLazyImportLoader(() => import("../../daemon/launchd.js"));
const serviceAuditModuleLoader = createLazyImportLoader(
() => import("../../daemon/service-audit.js"),
);
const gatewayTlsModuleLoader = createLazyImportLoader(() => import("../../infra/tls/gateway.js"));
const daemonProbeModuleLoader = createLazyImportLoader(() => import("./probe.js"));
const restartHealthModuleLoader = createLazyImportLoader(() => import("./restart-health.js"));
function loadGatewayProbeAuthModule() {
return gatewayProbeAuthModuleLoader.load();
}
function loadDaemonInspectModule() {
return daemonInspectModuleLoader.load();
}
function loadLaunchdModule() {
return launchdModuleLoader.load();
}
function loadServiceAuditModule() {
return serviceAuditModuleLoader.load();
}
function loadGatewayTlsModule() {
return gatewayTlsModuleLoader.load();
}
function loadDaemonProbeModule() {
return daemonProbeModuleLoader.load();
}
function loadRestartHealthModule() {
return restartHealthModuleLoader.load();
}
const loadGatewayProbeAuthModule = createLazyPromise(() => import("../../gateway/probe-auth.js"));
const loadDaemonInspectModule = createLazyPromise(() => import("../../daemon/inspect.js"));
const loadLaunchdModule = createLazyPromise(() => import("../../daemon/launchd.js"));
const loadServiceAuditModule = createLazyPromise(() => import("../../daemon/service-audit.js"));
const loadGatewayTlsModule = createLazyPromise(() => import("../../infra/tls/gateway.js"));
const loadDaemonProbeModule = createLazyPromise(() => import("./probe.js"));
const loadRestartHealthModule = createLazyPromise(() => import("./restart-health.js"));
function resolveSnapshotRuntimeConfig(snapshot: ConfigFileSnapshot | null): OpenClawConfig | null {
if (!snapshot?.valid || !snapshot.runtimeConfig) {
+11 -53
View File
@@ -17,7 +17,7 @@ import type {
} from "../../logging/diagnostic-stability.js";
import type { WriteDiagnosticSupportExportResult } from "../../logging/diagnostic-support-export.js";
import { defaultRuntime } from "../../runtime.js";
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
import { createLazyPromise } from "../../shared/lazy-promise.js";
import { inheritOptionFromParent } from "../command-options.js";
import { addGatewayServiceCommands } from "../daemon-cli/register-service-commands.js";
import { formatCliJsonFailure, rethrowExpectedCliError } from "../failure-output.js";
@@ -38,28 +38,26 @@ import { runGatewayResume, runGatewaySuspend } from "./suspend-cli.js";
type GatewayRpcOpts = Parameters<typeof callGatewayFromCliWithTransport>[1];
const configModuleLoader = createLazyImportLoader(
const loadConfigModule = createLazyPromise(
() => import("../../config/read-best-effort-config.runtime.js"),
);
const gatewayStatusModuleLoader = createLazyImportLoader(
() => import("../../commands/gateway-status.js"),
);
const gatewayHealthModuleLoader = createLazyImportLoader(() => import("../../commands/health.js"));
const bonjourDiscoveryModuleLoader = createLazyImportLoader(
const loadGatewayStatusModule = createLazyPromise(() => import("../../commands/gateway-status.js"));
const loadGatewayHealthModule = createLazyPromise(() => import("../../commands/health.js"));
const loadBonjourDiscoveryModule = createLazyPromise(
() => import("../../infra/bonjour-discovery.js"),
);
const wideAreaDnsModuleLoader = createLazyImportLoader(() => import("../../infra/widearea-dns.js"));
const healthStyleModuleLoader = createLazyImportLoader(
const loadWideAreaDnsModule = createLazyPromise(() => import("../../infra/widearea-dns.js"));
const loadHealthStyleModule = createLazyPromise(
() => import("../../../packages/terminal-core/src/health-style.js"),
);
const usageFormatModuleLoader = createLazyImportLoader(() => import("../../utils/usage-format.js"));
const stabilityBundleModuleLoader = createLazyImportLoader(
const loadUsageFormatModule = createLazyPromise(() => import("../../utils/usage-format.js"));
const loadStabilityBundleModule = createLazyPromise(
() => import("../../logging/diagnostic-stability-bundle.js"),
);
const supportExportModuleLoader = createLazyImportLoader(
const loadSupportExportModule = createLazyPromise(
() => import("../../logging/diagnostic-support-export.js"),
);
const daemonStatusGatherModuleLoader = createLazyImportLoader(
const loadDaemonStatusGatherModule = createLazyPromise(
() => import("../daemon-cli/status.gather.js"),
);
@@ -70,46 +68,6 @@ type GatewayCliDependencies = {
loadHealthStyleModule?: typeof loadHealthStyleModule;
};
function loadConfigModule() {
return configModuleLoader.load();
}
function loadGatewayStatusModule() {
return gatewayStatusModuleLoader.load();
}
function loadGatewayHealthModule() {
return gatewayHealthModuleLoader.load();
}
function loadBonjourDiscoveryModule() {
return bonjourDiscoveryModuleLoader.load();
}
function loadWideAreaDnsModule() {
return wideAreaDnsModuleLoader.load();
}
function loadHealthStyleModule() {
return healthStyleModuleLoader.load();
}
function loadUsageFormatModule() {
return usageFormatModuleLoader.load();
}
function loadStabilityBundleModule() {
return stabilityBundleModuleLoader.load();
}
function loadSupportExportModule() {
return supportExportModuleLoader.load();
}
function loadDaemonStatusGatherModule() {
return daemonStatusGatherModuleLoader.load();
}
function gatewayCallOpts(cmd: Command, defaultTimeoutMs = DEFAULT_GATEWAY_RPC_TIMEOUT_MS): Command {
return addGatewayClientOptions(cmd, { timeoutMs: defaultTimeoutMs }).option(
"--json",
+6 -33
View File
@@ -1,6 +1,6 @@
// Lazy command implementations for routes that can bypass full Commander registration.
import { defaultRuntime } from "../../runtime.js";
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
import { createLazyPromise } from "../../shared/lazy-promise.js";
import {
parseAgentsListRouteArgs,
parseChannelsListRouteArgs,
@@ -22,11 +22,6 @@ import {
type RouteArgParser<TArgs> = (argv: string[]) => TArgs | null;
type ParsedRouteArgs<TParse extends RouteArgParser<unknown>> = Exclude<ReturnType<TParse>, null>;
type AgentsListCommandModule = typeof import("../../commands/agents.commands.list.js");
type ConfigCliModule = typeof import("../config-cli.js");
type ModelsListCommandModule = typeof import("../../commands/models/list.list-command.js");
type ModelsStatusCommandModule = typeof import("../../commands/models/list.status-command.js");
type TasksJsonCommandModule = typeof import("../../commands/tasks-json.js");
/** Typed parsed route definition that binds one parser to its runner. */
type RoutedCommandDefinition<TParse extends RouteArgParser<unknown>> = {
@@ -46,39 +41,17 @@ function defineRoutedCommand<TParse extends RouteArgParser<unknown>>(
return definition;
}
const configCliLoader = createLazyImportLoader<ConfigCliModule>(() => import("../config-cli.js"));
const agentsListCommandLoader = createLazyImportLoader<AgentsListCommandModule>(
const loadConfigCli = createLazyPromise(() => import("../config-cli.js"));
const loadAgentsListCommand = createLazyPromise(
() => import("../../commands/agents.commands.list.js"),
);
const modelsListCommandLoader = createLazyImportLoader<ModelsListCommandModule>(
const loadModelsListCommand = createLazyPromise(
() => import("../../commands/models/list.list-command.js"),
);
const modelsStatusCommandLoader = createLazyImportLoader<ModelsStatusCommandModule>(
const loadModelsStatusCommand = createLazyPromise(
() => import("../../commands/models/list.status-command.js"),
);
const tasksJsonCommandLoader = createLazyImportLoader<TasksJsonCommandModule>(
() => import("../../commands/tasks-json.js"),
);
function loadConfigCli(): Promise<ConfigCliModule> {
return configCliLoader.load();
}
function loadAgentsListCommand(): Promise<AgentsListCommandModule> {
return agentsListCommandLoader.load();
}
function loadModelsListCommand(): Promise<ModelsListCommandModule> {
return modelsListCommandLoader.load();
}
function loadModelsStatusCommand(): Promise<ModelsStatusCommandModule> {
return modelsStatusCommandLoader.load();
}
function loadTasksJsonCommand(): Promise<TasksJsonCommandModule> {
return tasksJsonCommandLoader.load();
}
const loadTasksJsonCommand = createLazyPromise(() => import("../../commands/tasks-json.js"));
/** Route id to lazy parser/runner definition. */
export const routedCommandDefinitions = {