@@ -4,10 +4,14 @@ import { listCliCommandNames } from '../../command-catalog.ts';
44 * Curated guess -> canonical command mapping for unknown CLI command names.
55 *
66 * Agents (and humans) commonly guess command names that don't exist under that
7- * spelling, such as `relaunch` instead of `open <app> --relaunch`. Each entry's
8- * `command` must resolve to a real, registered CLI command name; the
9- * "alias suggestion targets exist" test in
10- * `src/cli/parser/__tests__/command-suggestions.test.ts` fails the build on drift.
7+ * spelling, such as `relaunch` instead of `open <app> --relaunch`. Keys must be
8+ * lowercase (lookups lowercase the input token first). Each entry's `command`
9+ * must resolve to a real, registered CLI command name, and each `example` must
10+ * parse as a valid invocation of it; the registry-drift tests in
11+ * `src/cli/parser/__tests__/command-suggestions.test.ts` fail the build on drift.
12+ *
13+ * `tap` is normalized to `press` as a true alias before the unknown-command
14+ * check runs, so its entry here only catches case variants such as `TAP`.
1115 */
1216type CommandAliasSuggestion = {
1317 /** Canonical command name this guess should have used. */
@@ -16,11 +20,14 @@ type CommandAliasSuggestion = {
1620 example : string ;
1721} ;
1822
23+ const OPEN_RELAUNCH_EXAMPLE = 'open <app> --relaunch' ;
24+
1925const COMMAND_ALIAS_SUGGESTIONS : Record < string , CommandAliasSuggestion > = {
20- launch : { command : 'open' , example : 'open <app> --relaunch' } ,
21- relaunch : { command : 'open' , example : 'open <app> --relaunch' } ,
22- start : { command : 'open' , example : 'open <app> --relaunch' } ,
23- restart : { command : 'open' , example : 'open <app> --relaunch' } ,
26+ launch : { command : 'open' , example : OPEN_RELAUNCH_EXAMPLE } ,
27+ relaunch : { command : 'open' , example : OPEN_RELAUNCH_EXAMPLE } ,
28+ start : { command : 'open' , example : OPEN_RELAUNCH_EXAMPLE } ,
29+ restart : { command : 'open' , example : OPEN_RELAUNCH_EXAMPLE } ,
30+ tap : { command : 'press' , example : 'press' } ,
2431 touch : { command : 'press' , example : 'press' } ,
2532 input : { command : 'fill' , example : 'fill' } ,
2633 settext : { command : 'fill' , example : 'fill' } ,
@@ -34,29 +41,42 @@ export function listCommandAliasSuggestionEntries(): Array<[string, CommandAlias
3441 return Object . entries ( COMMAND_ALIAS_SUGGESTIONS ) ;
3542}
3643
37- function getCuratedCommandSuggestion ( command : string ) : string | undefined {
38- return COMMAND_ALIAS_SUGGESTIONS [ command ] ?. example ;
39- }
40-
4144const NEAREST_COMMAND_SUGGESTION_LIMIT = 3 ;
4245
4346/**
44- * Nearest registered command names for an unrecognized command, ranked by a
45- * prefix-aware edit distance. Names are derived from the live command
46- * descriptor registry (via `listCliCommandNames`), never hardcoded, so the
47- * suggestion list can't drift from what the CLI actually supports.
47+ * Nearest registered command names for an unrecognized (lowercased) command
48+ * token. Names are derived from the live command descriptor registry (via
49+ * `listCliCommandNames`), never hardcoded, so the suggestion list can't drift
50+ * from what the CLI actually supports.
51+ *
52+ * Precision rules: 1-2 character tokens never get a suggestion, exact prefix
53+ * matches win outright, and otherwise only ties at the minimum edit distance
54+ * are kept so a strong match is not bundled with a coincidental weak one.
4855 */
49- export function getNearestCommandNames ( command : string ) : string [ ] {
56+ function getNearestCommandNames ( command : string ) : string [ ] {
57+ if ( command . length <= 2 ) return [ ] ;
5058 const names = listCliCommandNames ( ) ;
59+ const prefixMatches = names . filter ( ( name ) => name . startsWith ( command ) ) ;
60+ if ( prefixMatches . length > 0 ) {
61+ return prefixMatches
62+ . sort ( ( a , b ) => a . length - b . length || a . localeCompare ( b ) )
63+ . slice ( 0 , NEAREST_COMMAND_SUGGESTION_LIMIT ) ;
64+ }
65+ const threshold = nearestMatchThreshold ( command ) ;
5166 const scored = names
5267 . map ( ( name ) => ( { name, distance : commandNameDistance ( command , name ) } ) )
53- . filter ( ( entry ) => entry . distance <= nearestMatchThreshold ( command ) )
54- . sort ( ( a , b ) => a . distance - b . distance || a . name . localeCompare ( b . name ) ) ;
55- return scored . slice ( 0 , NEAREST_COMMAND_SUGGESTION_LIMIT ) . map ( ( entry ) => entry . name ) ;
68+ . filter ( ( entry ) => entry . distance <= threshold ) ;
69+ if ( scored . length === 0 ) return [ ] ;
70+ const best = Math . min ( ...scored . map ( ( entry ) => entry . distance ) ) ;
71+ return scored
72+ . filter ( ( entry ) => entry . distance === best )
73+ . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
74+ . slice ( 0 , NEAREST_COMMAND_SUGGESTION_LIMIT )
75+ . map ( ( entry ) => entry . name ) ;
5676}
5777
5878function nearestMatchThreshold ( command : string ) : number {
59- if ( command . length <= 3 ) return 1 ;
79+ if ( command . length < 4 ) return 1 ;
6080 if ( command . length <= 6 ) return 2 ;
6181 return 3 ;
6282}
@@ -91,12 +111,14 @@ function levenshteinDistance(a: string, b: string): number {
91111/**
92112 * Builds the "Did you mean ...?" fragment for an unknown command, or
93113 * `undefined` when neither the curated alias map nor the nearest-name
94- * fallback has a confident suggestion.
114+ * fallback has a confident suggestion. Matching is case-insensitive so
115+ * `RELAUNCH` and `Touch` get the same hint as their lowercase forms.
95116 */
96117export function suggestCommandFor ( command : string ) : string | undefined {
97- const curated = getCuratedCommandSuggestion ( command ) ;
118+ const normalized = command . toLowerCase ( ) ;
119+ const curated = COMMAND_ALIAS_SUGGESTIONS [ normalized ] ?. example ;
98120 if ( curated ) return curated ;
99- const nearest = getNearestCommandNames ( command ) ;
121+ const nearest = getNearestCommandNames ( normalized ) ;
100122 if ( nearest . length === 0 ) return undefined ;
101123 if ( nearest . length === 1 ) return nearest [ 0 ] ;
102124 return `one of: ${ nearest . join ( ', ' ) } ` ;
@@ -121,7 +143,7 @@ const POSITIONAL_APP_FLAG_GUESSES = new Set([
121143
122144export function formatUnknownFlagMessage ( token : string ) : string {
123145 if ( POSITIONAL_APP_FLAG_GUESSES . has ( token . toLowerCase ( ) ) ) {
124- return `Unknown flag: ${ token } . The app or bundle id is a positional argument, e.g. open <app> --relaunch .` ;
146+ return `Unknown flag: ${ token } . The app or bundle id is a positional argument, e.g. ${ OPEN_RELAUNCH_EXAMPLE } .` ;
125147 }
126148 return `Unknown flag: ${ token } ` ;
127149}
0 commit comments