Skip to content

Commit d81ae7a

Browse files
committed
chore(deadcode): inline unused CLI helpers
1 parent 99d8549 commit d81ae7a

2 files changed

Lines changed: 33 additions & 49 deletions

File tree

src/cli/program/command-suggestions.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ function uniqueSortedCommandNames(commands: Iterable<string>): string[] {
1515
);
1616
}
1717

18-
export function getKnownCliCommandNames(): string[] {
19-
return uniqueSortedCommandNames([
20-
...getCoreCliCommandNames(),
21-
...getSubCliEntries().map((entry) => entry.name),
22-
]);
23-
}
24-
2518
export function levenshteinDistance(left: string, right: string): number {
2619
if (left === right) {
2720
return 0;
@@ -52,37 +45,40 @@ export function levenshteinDistance(left: string, right: string): number {
5245
return previous[right.length] ?? 0;
5346
}
5447

55-
export function suggestCliCommands(
56-
input: string,
57-
candidates: Iterable<string> = getKnownCliCommandNames(),
58-
): string[] {
48+
export function formatCliCommandSuggestions(input: string): string | undefined {
5949
const normalizedInput = input.trim().toLowerCase();
6050
if (!normalizedInput) {
61-
return [];
51+
return undefined;
6252
}
6353

64-
const knownCommands = uniqueSortedCommandNames(candidates);
54+
const knownCommands = uniqueSortedCommandNames([
55+
...getCoreCliCommandNames(),
56+
...getSubCliEntries().map((entry) => entry.name),
57+
]);
6558
const explicitAlias = EXPLICIT_COMMAND_ALIASES.get(normalizedInput);
6659
if (explicitAlias && knownCommands.includes(explicitAlias)) {
67-
return [explicitAlias];
60+
return formatCliSuggestionLines([explicitAlias]);
6861
}
62+
const suggestions = findCliCommandSuggestions(normalizedInput, knownCommands);
63+
if (suggestions.length === 0) {
64+
return undefined;
65+
}
66+
return formatCliSuggestionLines(suggestions);
67+
}
6968

70-
const maxDistance = Math.max(1, Math.floor(normalizedInput.length * 0.4));
71-
return knownCommands
72-
.map((command) => ({ command, distance: levenshteinDistance(normalizedInput, command) }))
73-
.filter(({ command, distance }) => command !== normalizedInput && distance <= maxDistance)
69+
function findCliCommandSuggestions(input: string, candidates: readonly string[]): string[] {
70+
const maxDistance = Math.max(1, Math.floor(input.length * 0.4));
71+
return candidates
72+
.map((command) => ({ command, distance: levenshteinDistance(input, command) }))
73+
.filter(({ command, distance }) => command !== input && distance <= maxDistance)
7474
.toSorted(
7575
(left, right) => left.distance - right.distance || left.command.localeCompare(right.command),
7676
)
7777
.slice(0, MAX_SUGGESTIONS)
7878
.map(({ command }) => command);
7979
}
8080

81-
export function formatCliCommandSuggestions(input: string): string | undefined {
82-
const suggestions = suggestCliCommands(input);
83-
if (suggestions.length === 0) {
84-
return undefined;
85-
}
81+
function formatCliSuggestionLines(suggestions: readonly string[]): string {
8682
const commandLines = suggestions
8783
.map((command) => ` ${formatCliCommand(`openclaw ${command}`)}`)
8884
.join("\n");

src/cli/root-help-live-config.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Root-help config probe for plugin-sensitive help rendering.
2-
import type { OpenClawConfig } from "../config/types.openclaw.js";
32
import type { RootHelpRenderOptions } from "./program/root-help.js";
43

54
function hasEntries(value: object | undefined): boolean {
@@ -10,30 +9,6 @@ function hasListEntries(value: string[] | undefined): boolean {
109
return Array.isArray(value) && value.length > 0;
1110
}
1211

13-
/** Detect config fields that can change which plugin help sections are rendered. */
14-
export function hasPluginHelpAffectingConfig(config: OpenClawConfig | null | undefined): boolean {
15-
const plugins = config?.plugins;
16-
if (!plugins) {
17-
return false;
18-
}
19-
return (
20-
plugins.enabled === false ||
21-
hasListEntries(plugins.allow) ||
22-
hasListEntries(plugins.deny) ||
23-
hasListEntries(plugins.load?.paths) ||
24-
hasEntries(plugins.slots) ||
25-
hasEntries(plugins.entries) ||
26-
hasEntries(plugins.installs)
27-
);
28-
}
29-
30-
/** Detect env vars that can change bundled plugin availability in root help. */
31-
export function hasPluginHelpAffectingEnv(env: NodeJS.ProcessEnv): boolean {
32-
return Boolean(
33-
env.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim() || env.OPENCLAW_DISABLE_BUNDLED_PLUGINS?.trim(),
34-
);
35-
}
36-
3712
/** Load render options only when config/env can affect plugin help output. */
3813
export async function loadRootHelpRenderOptionsForConfigSensitivePlugins(
3914
env: NodeJS.ProcessEnv = process.env,
@@ -46,7 +21,20 @@ export async function loadRootHelpRenderOptionsForConfigSensitivePlugins(
4621
if (!snapshot.valid) {
4722
return null;
4823
}
49-
if (!hasPluginHelpAffectingEnv(env) && !hasPluginHelpAffectingConfig(snapshot.sourceConfig)) {
24+
const plugins = snapshot.sourceConfig.plugins;
25+
const configAffectsPluginHelp =
26+
plugins &&
27+
(plugins.enabled === false ||
28+
hasListEntries(plugins.allow) ||
29+
hasListEntries(plugins.deny) ||
30+
hasListEntries(plugins.load?.paths) ||
31+
hasEntries(plugins.slots) ||
32+
hasEntries(plugins.entries) ||
33+
hasEntries(plugins.installs));
34+
const envAffectsPluginHelp = Boolean(
35+
env.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim() || env.OPENCLAW_DISABLE_BUNDLED_PLUGINS?.trim(),
36+
);
37+
if (!envAffectsPluginHelp && !configAffectsPluginHelp) {
5038
return null;
5139
}
5240
return {

0 commit comments

Comments
 (0)