Skip to content

Commit bc57acb

Browse files
committed
refactor(cli): reuse function-shaped lazy loaders
1 parent 19d61d9 commit bc57acb

3 files changed

Lines changed: 25 additions & 126 deletions

File tree

‎src/cli/daemon-cli/status.gather.ts‎

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import {
6666
detectPluginVersionDrift,
6767
type PluginVersionDriftReport,
6868
} from "../../plugins/plugin-version-drift.js";
69-
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
69+
import { createLazyPromise } from "../../shared/lazy-promise.js";
7070
import { VERSION } from "../../version.js";
7171
import { resolveGatewayLocalPortOverride } from "../gateway-port-option.js";
7272
import { parseTimeoutMsWithFallback } from "../parse-timeout.js";
@@ -133,45 +133,13 @@ type CliStatusSummary = {
133133

134134
type GatewayConnectFailureKind = ReturnType<typeof classifyGatewayConnectFailure>["kind"];
135135

136-
const gatewayProbeAuthModuleLoader = createLazyImportLoader(
137-
() => import("../../gateway/probe-auth.js"),
138-
);
139-
const daemonInspectModuleLoader = createLazyImportLoader(() => import("../../daemon/inspect.js"));
140-
const launchdModuleLoader = createLazyImportLoader(() => import("../../daemon/launchd.js"));
141-
const serviceAuditModuleLoader = createLazyImportLoader(
142-
() => import("../../daemon/service-audit.js"),
143-
);
144-
const gatewayTlsModuleLoader = createLazyImportLoader(() => import("../../infra/tls/gateway.js"));
145-
const daemonProbeModuleLoader = createLazyImportLoader(() => import("./probe.js"));
146-
const restartHealthModuleLoader = createLazyImportLoader(() => import("./restart-health.js"));
147-
148-
function loadGatewayProbeAuthModule() {
149-
return gatewayProbeAuthModuleLoader.load();
150-
}
151-
152-
function loadDaemonInspectModule() {
153-
return daemonInspectModuleLoader.load();
154-
}
155-
156-
function loadLaunchdModule() {
157-
return launchdModuleLoader.load();
158-
}
159-
160-
function loadServiceAuditModule() {
161-
return serviceAuditModuleLoader.load();
162-
}
163-
164-
function loadGatewayTlsModule() {
165-
return gatewayTlsModuleLoader.load();
166-
}
167-
168-
function loadDaemonProbeModule() {
169-
return daemonProbeModuleLoader.load();
170-
}
171-
172-
function loadRestartHealthModule() {
173-
return restartHealthModuleLoader.load();
174-
}
136+
const loadGatewayProbeAuthModule = createLazyPromise(() => import("../../gateway/probe-auth.js"));
137+
const loadDaemonInspectModule = createLazyPromise(() => import("../../daemon/inspect.js"));
138+
const loadLaunchdModule = createLazyPromise(() => import("../../daemon/launchd.js"));
139+
const loadServiceAuditModule = createLazyPromise(() => import("../../daemon/service-audit.js"));
140+
const loadGatewayTlsModule = createLazyPromise(() => import("../../infra/tls/gateway.js"));
141+
const loadDaemonProbeModule = createLazyPromise(() => import("./probe.js"));
142+
const loadRestartHealthModule = createLazyPromise(() => import("./restart-health.js"));
175143

176144
function resolveSnapshotRuntimeConfig(snapshot: ConfigFileSnapshot | null): OpenClawConfig | null {
177145
if (!snapshot?.valid || !snapshot.runtimeConfig) {

‎src/cli/gateway-cli/register.ts‎

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
} from "../../logging/diagnostic-stability.js";
1818
import type { WriteDiagnosticSupportExportResult } from "../../logging/diagnostic-support-export.js";
1919
import { defaultRuntime } from "../../runtime.js";
20-
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
20+
import { createLazyPromise } from "../../shared/lazy-promise.js";
2121
import { inheritOptionFromParent } from "../command-options.js";
2222
import { addGatewayServiceCommands } from "../daemon-cli/register-service-commands.js";
2323
import { formatCliJsonFailure, rethrowExpectedCliError } from "../failure-output.js";
@@ -38,28 +38,26 @@ import { runGatewayResume, runGatewaySuspend } from "./suspend-cli.js";
3838

3939
type GatewayRpcOpts = Parameters<typeof callGatewayFromCliWithTransport>[1];
4040

41-
const configModuleLoader = createLazyImportLoader(
41+
const loadConfigModule = createLazyPromise(
4242
() => import("../../config/read-best-effort-config.runtime.js"),
4343
);
44-
const gatewayStatusModuleLoader = createLazyImportLoader(
45-
() => import("../../commands/gateway-status.js"),
46-
);
47-
const gatewayHealthModuleLoader = createLazyImportLoader(() => import("../../commands/health.js"));
48-
const bonjourDiscoveryModuleLoader = createLazyImportLoader(
44+
const loadGatewayStatusModule = createLazyPromise(() => import("../../commands/gateway-status.js"));
45+
const loadGatewayHealthModule = createLazyPromise(() => import("../../commands/health.js"));
46+
const loadBonjourDiscoveryModule = createLazyPromise(
4947
() => import("../../infra/bonjour-discovery.js"),
5048
);
51-
const wideAreaDnsModuleLoader = createLazyImportLoader(() => import("../../infra/widearea-dns.js"));
52-
const healthStyleModuleLoader = createLazyImportLoader(
49+
const loadWideAreaDnsModule = createLazyPromise(() => import("../../infra/widearea-dns.js"));
50+
const loadHealthStyleModule = createLazyPromise(
5351
() => import("../../../packages/terminal-core/src/health-style.js"),
5452
);
55-
const usageFormatModuleLoader = createLazyImportLoader(() => import("../../utils/usage-format.js"));
56-
const stabilityBundleModuleLoader = createLazyImportLoader(
53+
const loadUsageFormatModule = createLazyPromise(() => import("../../utils/usage-format.js"));
54+
const loadStabilityBundleModule = createLazyPromise(
5755
() => import("../../logging/diagnostic-stability-bundle.js"),
5856
);
59-
const supportExportModuleLoader = createLazyImportLoader(
57+
const loadSupportExportModule = createLazyPromise(
6058
() => import("../../logging/diagnostic-support-export.js"),
6159
);
62-
const daemonStatusGatherModuleLoader = createLazyImportLoader(
60+
const loadDaemonStatusGatherModule = createLazyPromise(
6361
() => import("../daemon-cli/status.gather.js"),
6462
);
6563

@@ -70,46 +68,6 @@ type GatewayCliDependencies = {
7068
loadHealthStyleModule?: typeof loadHealthStyleModule;
7169
};
7270

73-
function loadConfigModule() {
74-
return configModuleLoader.load();
75-
}
76-
77-
function loadGatewayStatusModule() {
78-
return gatewayStatusModuleLoader.load();
79-
}
80-
81-
function loadGatewayHealthModule() {
82-
return gatewayHealthModuleLoader.load();
83-
}
84-
85-
function loadBonjourDiscoveryModule() {
86-
return bonjourDiscoveryModuleLoader.load();
87-
}
88-
89-
function loadWideAreaDnsModule() {
90-
return wideAreaDnsModuleLoader.load();
91-
}
92-
93-
function loadHealthStyleModule() {
94-
return healthStyleModuleLoader.load();
95-
}
96-
97-
function loadUsageFormatModule() {
98-
return usageFormatModuleLoader.load();
99-
}
100-
101-
function loadStabilityBundleModule() {
102-
return stabilityBundleModuleLoader.load();
103-
}
104-
105-
function loadSupportExportModule() {
106-
return supportExportModuleLoader.load();
107-
}
108-
109-
function loadDaemonStatusGatherModule() {
110-
return daemonStatusGatherModuleLoader.load();
111-
}
112-
11371
function gatewayCallOpts(cmd: Command, defaultTimeoutMs = DEFAULT_GATEWAY_RPC_TIMEOUT_MS): Command {
11472
return addGatewayClientOptions(cmd, { timeoutMs: defaultTimeoutMs }).option(
11573
"--json",

‎src/cli/program/routed-command-definitions.ts‎

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Lazy command implementations for routes that can bypass full Commander registration.
22
import { defaultRuntime } from "../../runtime.js";
3-
import { createLazyImportLoader } from "../../shared/lazy-promise.js";
3+
import { createLazyPromise } from "../../shared/lazy-promise.js";
44
import {
55
parseAgentsListRouteArgs,
66
parseChannelsListRouteArgs,
@@ -22,11 +22,6 @@ import {
2222
type RouteArgParser<TArgs> = (argv: string[]) => TArgs | null;
2323

2424
type ParsedRouteArgs<TParse extends RouteArgParser<unknown>> = Exclude<ReturnType<TParse>, null>;
25-
type AgentsListCommandModule = typeof import("../../commands/agents.commands.list.js");
26-
type ConfigCliModule = typeof import("../config-cli.js");
27-
type ModelsListCommandModule = typeof import("../../commands/models/list.list-command.js");
28-
type ModelsStatusCommandModule = typeof import("../../commands/models/list.status-command.js");
29-
type TasksJsonCommandModule = typeof import("../../commands/tasks-json.js");
3025

3126
/** Typed parsed route definition that binds one parser to its runner. */
3227
type RoutedCommandDefinition<TParse extends RouteArgParser<unknown>> = {
@@ -46,39 +41,17 @@ function defineRoutedCommand<TParse extends RouteArgParser<unknown>>(
4641
return definition;
4742
}
4843

49-
const configCliLoader = createLazyImportLoader<ConfigCliModule>(() => import("../config-cli.js"));
50-
const agentsListCommandLoader = createLazyImportLoader<AgentsListCommandModule>(
44+
const loadConfigCli = createLazyPromise(() => import("../config-cli.js"));
45+
const loadAgentsListCommand = createLazyPromise(
5146
() => import("../../commands/agents.commands.list.js"),
5247
);
53-
const modelsListCommandLoader = createLazyImportLoader<ModelsListCommandModule>(
48+
const loadModelsListCommand = createLazyPromise(
5449
() => import("../../commands/models/list.list-command.js"),
5550
);
56-
const modelsStatusCommandLoader = createLazyImportLoader<ModelsStatusCommandModule>(
51+
const loadModelsStatusCommand = createLazyPromise(
5752
() => import("../../commands/models/list.status-command.js"),
5853
);
59-
const tasksJsonCommandLoader = createLazyImportLoader<TasksJsonCommandModule>(
60-
() => import("../../commands/tasks-json.js"),
61-
);
62-
63-
function loadConfigCli(): Promise<ConfigCliModule> {
64-
return configCliLoader.load();
65-
}
66-
67-
function loadAgentsListCommand(): Promise<AgentsListCommandModule> {
68-
return agentsListCommandLoader.load();
69-
}
70-
71-
function loadModelsListCommand(): Promise<ModelsListCommandModule> {
72-
return modelsListCommandLoader.load();
73-
}
74-
75-
function loadModelsStatusCommand(): Promise<ModelsStatusCommandModule> {
76-
return modelsStatusCommandLoader.load();
77-
}
78-
79-
function loadTasksJsonCommand(): Promise<TasksJsonCommandModule> {
80-
return tasksJsonCommandLoader.load();
81-
}
54+
const loadTasksJsonCommand = createLazyPromise(() => import("../../commands/tasks-json.js"));
8255

8356
/** Route id to lazy parser/runner definition. */
8457
export const routedCommandDefinitions = {

0 commit comments

Comments
 (0)