From d81ae7a441e288a9aa87cdbac73f39c84cfecebf Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 20 Jun 2026 21:56:24 +0800 Subject: [PATCH] chore(deadcode): inline unused CLI helpers --- src/cli/program/command-suggestions.ts | 42 ++++++++++++-------------- src/cli/root-help-live-config.ts | 40 +++++++++--------------- 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/cli/program/command-suggestions.ts b/src/cli/program/command-suggestions.ts index 933d6d31e10..59c65bc0caf 100644 --- a/src/cli/program/command-suggestions.ts +++ b/src/cli/program/command-suggestions.ts @@ -15,13 +15,6 @@ function uniqueSortedCommandNames(commands: Iterable): string[] { ); } -export function getKnownCliCommandNames(): string[] { - return uniqueSortedCommandNames([ - ...getCoreCliCommandNames(), - ...getSubCliEntries().map((entry) => entry.name), - ]); -} - export function levenshteinDistance(left: string, right: string): number { if (left === right) { return 0; @@ -52,25 +45,32 @@ export function levenshteinDistance(left: string, right: string): number { return previous[right.length] ?? 0; } -export function suggestCliCommands( - input: string, - candidates: Iterable = getKnownCliCommandNames(), -): string[] { +export function formatCliCommandSuggestions(input: string): string | undefined { const normalizedInput = input.trim().toLowerCase(); if (!normalizedInput) { - return []; + return undefined; } - const knownCommands = uniqueSortedCommandNames(candidates); + const knownCommands = uniqueSortedCommandNames([ + ...getCoreCliCommandNames(), + ...getSubCliEntries().map((entry) => entry.name), + ]); const explicitAlias = EXPLICIT_COMMAND_ALIASES.get(normalizedInput); if (explicitAlias && knownCommands.includes(explicitAlias)) { - return [explicitAlias]; + return formatCliSuggestionLines([explicitAlias]); } + const suggestions = findCliCommandSuggestions(normalizedInput, knownCommands); + if (suggestions.length === 0) { + return undefined; + } + return formatCliSuggestionLines(suggestions); +} - const maxDistance = Math.max(1, Math.floor(normalizedInput.length * 0.4)); - return knownCommands - .map((command) => ({ command, distance: levenshteinDistance(normalizedInput, command) })) - .filter(({ command, distance }) => command !== normalizedInput && distance <= maxDistance) +function findCliCommandSuggestions(input: string, candidates: readonly string[]): string[] { + const maxDistance = Math.max(1, Math.floor(input.length * 0.4)); + return candidates + .map((command) => ({ command, distance: levenshteinDistance(input, command) })) + .filter(({ command, distance }) => command !== input && distance <= maxDistance) .toSorted( (left, right) => left.distance - right.distance || left.command.localeCompare(right.command), ) @@ -78,11 +78,7 @@ export function suggestCliCommands( .map(({ command }) => command); } -export function formatCliCommandSuggestions(input: string): string | undefined { - const suggestions = suggestCliCommands(input); - if (suggestions.length === 0) { - return undefined; - } +function formatCliSuggestionLines(suggestions: readonly string[]): string { const commandLines = suggestions .map((command) => ` ${formatCliCommand(`openclaw ${command}`)}`) .join("\n"); diff --git a/src/cli/root-help-live-config.ts b/src/cli/root-help-live-config.ts index 1af72d84366..ba7c8e45a7e 100644 --- a/src/cli/root-help-live-config.ts +++ b/src/cli/root-help-live-config.ts @@ -1,5 +1,4 @@ // Root-help config probe for plugin-sensitive help rendering. -import type { OpenClawConfig } from "../config/types.openclaw.js"; import type { RootHelpRenderOptions } from "./program/root-help.js"; function hasEntries(value: object | undefined): boolean { @@ -10,30 +9,6 @@ function hasListEntries(value: string[] | undefined): boolean { return Array.isArray(value) && value.length > 0; } -/** Detect config fields that can change which plugin help sections are rendered. */ -export function hasPluginHelpAffectingConfig(config: OpenClawConfig | null | undefined): boolean { - const plugins = config?.plugins; - if (!plugins) { - return false; - } - return ( - plugins.enabled === false || - hasListEntries(plugins.allow) || - hasListEntries(plugins.deny) || - hasListEntries(plugins.load?.paths) || - hasEntries(plugins.slots) || - hasEntries(plugins.entries) || - hasEntries(plugins.installs) - ); -} - -/** Detect env vars that can change bundled plugin availability in root help. */ -export function hasPluginHelpAffectingEnv(env: NodeJS.ProcessEnv): boolean { - return Boolean( - env.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim() || env.OPENCLAW_DISABLE_BUNDLED_PLUGINS?.trim(), - ); -} - /** Load render options only when config/env can affect plugin help output. */ export async function loadRootHelpRenderOptionsForConfigSensitivePlugins( env: NodeJS.ProcessEnv = process.env, @@ -46,7 +21,20 @@ export async function loadRootHelpRenderOptionsForConfigSensitivePlugins( if (!snapshot.valid) { return null; } - if (!hasPluginHelpAffectingEnv(env) && !hasPluginHelpAffectingConfig(snapshot.sourceConfig)) { + const plugins = snapshot.sourceConfig.plugins; + const configAffectsPluginHelp = + plugins && + (plugins.enabled === false || + hasListEntries(plugins.allow) || + hasListEntries(plugins.deny) || + hasListEntries(plugins.load?.paths) || + hasEntries(plugins.slots) || + hasEntries(plugins.entries) || + hasEntries(plugins.installs)); + const envAffectsPluginHelp = Boolean( + env.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim() || env.OPENCLAW_DISABLE_BUNDLED_PLUGINS?.trim(), + ); + if (!envAffectsPluginHelp && !configAffectsPluginHelp) { return null; } return {