mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-10 00:11:19 +00:00
chore(deadcode): inline unused CLI helpers
This commit is contained in:
parent
99d8549de6
commit
d81ae7a441
2 changed files with 33 additions and 49 deletions
|
|
@ -15,13 +15,6 @@ function uniqueSortedCommandNames(commands: Iterable<string>): 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<string> = 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");
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue