diff --git a/src/cli/gateway-cli/register.ts b/src/cli/gateway-cli/register.ts index 05185f3ca56..cdb9c41ef5e 100644 --- a/src/cli/gateway-cli/register.ts +++ b/src/cli/gateway-cli/register.ts @@ -1,15 +1,9 @@ import type { Command } from "commander"; -import { gatewayStatusCommand } from "../../commands/gateway-status.js"; -import { formatHealthChannelLines, type HealthSummary } from "../../commands/health.js"; -import { readBestEffortConfig } from "../../config/config.js"; -import { discoverGatewayBeacons } from "../../infra/bonjour-discovery.js"; +import type { HealthSummary } from "../../commands/health.js"; import type { CostUsageSummary } from "../../infra/session-cost-usage.js"; -import { resolveWideAreaDiscoveryDomain } from "../../infra/widearea-dns.js"; import { defaultRuntime } from "../../runtime.js"; -import { styleHealthChannelLine } from "../../terminal/health-style.js"; import { formatDocsLink } from "../../terminal/links.js"; import { colorize, isRich, theme } from "../../terminal/theme.js"; -import { formatTokenCount, formatUsd } from "../../utils/usage-format.js"; import { runCommandWithRuntime } from "../cli-utils.js"; import { inheritOptionFromParent } from "../command-options.js"; import { addGatewayServiceCommands } from "../daemon-cli.js"; @@ -26,6 +20,53 @@ import { } from "./discover.js"; import { addGatewayRunCommand } from "./run.js"; +let configModulePromise: Promise | undefined; +let gatewayStatusModulePromise: + | Promise + | undefined; +let gatewayHealthModulePromise: Promise | undefined; +let bonjourDiscoveryModulePromise: + | Promise + | undefined; +let wideAreaDnsModulePromise: Promise | undefined; +let healthStyleModulePromise: Promise | undefined; +let usageFormatModulePromise: Promise | undefined; + +function loadConfigModule() { + configModulePromise ??= import("../../config/config.js"); + return configModulePromise; +} + +function loadGatewayStatusModule() { + gatewayStatusModulePromise ??= import("../../commands/gateway-status.js"); + return gatewayStatusModulePromise; +} + +function loadGatewayHealthModule() { + gatewayHealthModulePromise ??= import("../../commands/health.js"); + return gatewayHealthModulePromise; +} + +function loadBonjourDiscoveryModule() { + bonjourDiscoveryModulePromise ??= import("../../infra/bonjour-discovery.js"); + return bonjourDiscoveryModulePromise; +} + +function loadWideAreaDnsModule() { + wideAreaDnsModulePromise ??= import("../../infra/widearea-dns.js"); + return wideAreaDnsModulePromise; +} + +function loadHealthStyleModule() { + healthStyleModulePromise ??= import("../../terminal/health-style.js"); + return healthStyleModulePromise; +} + +function loadUsageFormatModule() { + usageFormatModulePromise ??= import("../../utils/usage-format.js"); + return usageFormatModulePromise; +} + function runGatewayCommand(action: () => Promise, label?: string) { return runCommandWithRuntime(defaultRuntime, action, (err) => { const message = String(err); @@ -60,7 +101,12 @@ function resolveGatewayRpcOptions { + const { formatTokenCount, formatUsd } = await loadUsageFormatModule(); const totalCost = formatUsd(summary.totals.totalCost) ?? "$0.00"; const totalTokens = formatTokenCount(summary.totals.totalTokens) ?? "0"; const lines = [ @@ -120,6 +166,7 @@ export function registerGatewayCli(program: Command) { .action(async (method, opts, command) => { await runGatewayCommand(async () => { const rpcOpts = resolveGatewayRpcOptions(opts, command); + const { readBestEffortConfig } = await loadConfigModule(); const config = await readBestEffortConfig(); const params = JSON.parse(String(opts.params ?? "{}")); const result = await callGatewayCli(method, { ...rpcOpts, config }, params); @@ -145,6 +192,7 @@ export function registerGatewayCli(program: Command) { await runGatewayCommand(async () => { const rpcOpts = resolveGatewayRpcOptions(opts, command); const days = parseDaysOption(opts.days); + const { readBestEffortConfig } = await loadConfigModule(); const config = await readBestEffortConfig(); const result = await callGatewayCli("usage.cost", { ...rpcOpts, config }, { days }); if (rpcOpts.json) { @@ -153,7 +201,7 @@ export function registerGatewayCli(program: Command) { } const rich = isRich(); const summary = result as CostUsageSummary; - for (const line of renderCostUsageSummary(summary, days, rich)) { + for (const line of await renderCostUsageSummaryAsync(summary, days, rich)) { defaultRuntime.log(line); } }, "Gateway usage cost failed"); @@ -167,6 +215,15 @@ export function registerGatewayCli(program: Command) { .action(async (opts, command) => { await runGatewayCommand(async () => { const rpcOpts = resolveGatewayRpcOptions(opts, command); + const [ + { readBestEffortConfig }, + { formatHealthChannelLines }, + { styleHealthChannelLine }, + ] = await Promise.all([ + loadConfigModule(), + loadGatewayHealthModule(), + loadHealthStyleModule(), + ]); const config = await readBestEffortConfig(); const result = await callGatewayCli("health", { ...rpcOpts, config }); if (rpcOpts.json) { @@ -203,6 +260,7 @@ export function registerGatewayCli(program: Command) { .action(async (opts, command) => { await runGatewayCommand(async () => { const rpcOpts = resolveGatewayRpcOptions(opts, command); + const { gatewayStatusCommand } = await loadGatewayStatusModule(); await gatewayStatusCommand(rpcOpts, defaultRuntime); }); }); @@ -214,6 +272,15 @@ export function registerGatewayCli(program: Command) { .option("--json", "Output JSON", false) .action(async (opts: GatewayDiscoverOpts) => { await runGatewayCommand(async () => { + const [ + { readBestEffortConfig }, + { discoverGatewayBeacons }, + { resolveWideAreaDiscoveryDomain }, + ] = await Promise.all([ + loadConfigModule(), + loadBonjourDiscoveryModule(), + loadWideAreaDnsModule(), + ]); const cfg = await readBestEffortConfig(); const wideAreaDomain = resolveWideAreaDiscoveryDomain({ configDomain: cfg.discovery?.wideArea?.domain,