|
| 1 | +import { formatCliCommand } from "../command-format.js"; |
| 2 | +import { getCoreCliCommandNames } from "./core-command-descriptors.js"; |
| 3 | +import { getSubCliEntries } from "./subcli-descriptors.js"; |
| 4 | + |
| 5 | +const EXPLICIT_COMMAND_ALIASES = new Map<string, string>([ |
| 6 | + ["upgrade", "update"], |
| 7 | + ["udpate", "update"], |
| 8 | +]); |
| 9 | + |
| 10 | +const MAX_SUGGESTIONS = 3; |
| 11 | + |
| 12 | +function uniqueSortedCommandNames(commands: Iterable<string>): string[] { |
| 13 | + return [...new Set([...commands].filter(Boolean))].toSorted((left, right) => |
| 14 | + left.localeCompare(right), |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +export function getKnownCliCommandNames(): string[] { |
| 19 | + return uniqueSortedCommandNames([ |
| 20 | + ...getCoreCliCommandNames(), |
| 21 | + ...getSubCliEntries().map((entry) => entry.name), |
| 22 | + ]); |
| 23 | +} |
| 24 | + |
| 25 | +export function levenshteinDistance(left: string, right: string): number { |
| 26 | + if (left === right) { |
| 27 | + return 0; |
| 28 | + } |
| 29 | + if (left.length === 0) { |
| 30 | + return right.length; |
| 31 | + } |
| 32 | + if (right.length === 0) { |
| 33 | + return left.length; |
| 34 | + } |
| 35 | + |
| 36 | + let previous = Array.from({ length: right.length + 1 }, (_, index) => index); |
| 37 | + let current = Array.from<number>({ length: right.length + 1 }); |
| 38 | + |
| 39 | + for (let leftIndex = 0; leftIndex < left.length; leftIndex += 1) { |
| 40 | + current[0] = leftIndex + 1; |
| 41 | + for (let rightIndex = 0; rightIndex < right.length; rightIndex += 1) { |
| 42 | + const substitutionCost = left[leftIndex] === right[rightIndex] ? 0 : 1; |
| 43 | + current[rightIndex + 1] = Math.min( |
| 44 | + current[rightIndex] + 1, |
| 45 | + previous[rightIndex + 1] + 1, |
| 46 | + previous[rightIndex] + substitutionCost, |
| 47 | + ); |
| 48 | + } |
| 49 | + [previous, current] = [current, previous]; |
| 50 | + } |
| 51 | + |
| 52 | + return previous[right.length] ?? 0; |
| 53 | +} |
| 54 | + |
| 55 | +export function suggestCliCommands( |
| 56 | + input: string, |
| 57 | + candidates: Iterable<string> = getKnownCliCommandNames(), |
| 58 | +): string[] { |
| 59 | + const normalizedInput = input.trim().toLowerCase(); |
| 60 | + if (!normalizedInput) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + |
| 64 | + const knownCommands = uniqueSortedCommandNames(candidates); |
| 65 | + const explicitAlias = EXPLICIT_COMMAND_ALIASES.get(normalizedInput); |
| 66 | + if (explicitAlias && knownCommands.includes(explicitAlias)) { |
| 67 | + return [explicitAlias]; |
| 68 | + } |
| 69 | + |
| 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) |
| 74 | + .toSorted( |
| 75 | + (left, right) => left.distance - right.distance || left.command.localeCompare(right.command), |
| 76 | + ) |
| 77 | + .slice(0, MAX_SUGGESTIONS) |
| 78 | + .map(({ command }) => command); |
| 79 | +} |
| 80 | + |
| 81 | +export function formatCliCommandSuggestions(input: string): string | undefined { |
| 82 | + const suggestions = suggestCliCommands(input); |
| 83 | + if (suggestions.length === 0) { |
| 84 | + return undefined; |
| 85 | + } |
| 86 | + const commandLines = suggestions |
| 87 | + .map((command) => ` ${formatCliCommand(`openclaw ${command}`)}`) |
| 88 | + .join("\n"); |
| 89 | + return `Did you mean this?\n${commandLines}`; |
| 90 | +} |
0 commit comments