Skip to content

Suggest the closest slash command on unknown command errors #17

@esengine

Description

@esengine

Background

Typing a typo'd slash command (/upadte, /sesions, /dotor) currently returns a flat "unknown command — try /help". That's a guaranteed wasted turn: the user has to ask /help, scan the list, retype. A "did you mean: /update?" suggestion turns the typo into a one-keystroke fix.

Current behaviour

src/cli/ui/slash/dispatch.ts:48:

return { unknown: true, info: `unknown command: /${cmd}  (try /help)` };

The full set of registered command names is available at this point — dispatch.ts has them.

Expected behaviour

When the user types an unknown slash, find up to 3 commands within Levenshtein distance ≤ 3 of the typed one and append them to the message:

unknown command: /upadte — did you mean /update?
unknown command: /sesions — did you mean /sessions, /session?
unknown command: /xyz123 (try /help)

(Last case: nothing close enough, falls back to today's behaviour.)

Files to touch

  • src/cli/ui/slash/dispatch.ts:48 — the message construction
  • New util: src/cli/ui/slash/nearest.ts (or co-located in dispatch.ts if small)
  • New test: tests/slash-nearest.test.ts

Acceptance criteria

  • New helper nearestCommands(input: string, all: readonly string[], opts?: { max?: number; maxDistance?: number }): string[] returning the closest names, sorted by distance ascending
  • dispatch.ts calls it and appends "— did you mean /a, /b, /c?" when results exist
  • Defaults: max 3 suggestions, max Levenshtein distance 3, but capped at Math.floor(input.length / 2) for very short inputs (so /x doesn't suggest /xx etc.)
  • Tests cover: typo inside word (upadte→update), missing letter, extra letter, transposition, no-match-found returns []
  • No new dependencies — write the Levenshtein DP yourself, it's ~15 lines

Hints

  • Levenshtein DP: search "Wagner-Fischer". The standard 2-row tabular implementation is plenty fast for a few dozen names.
  • Keep the suggestion sort stable: tie-break by alphabetical so output is deterministic.
  • The current commands list is ~30 names — no need to optimise; readable beats clever.

Out of scope

  • Fuzzy matching beyond Levenshtein (e.g., trigram, soundex)
  • Suggesting /cmd <args> patterns; just suggest the command name
  • Integrating with the existing SlashSuggestions UI (that's the live-typed completion popover, different code path)

Difficulty

2-3 hours.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions