Files
hyperframes/packages/cli/src/utils/didYouMean.ts
T
WaterrrForever 618c486734 fix(cli): measure and diff report why they failed, and reverse reports its runs (#6997)
* fix(cli): measure and diff report why they failed, and reverse reports its runs

Since launch, 97 people saw a bare "Command failed" from measure: runMeasure
and diff called failCommand() without the error, so telemetry kept only the
wrapper. 59 agents ran `measure <file>` with no op and got no hint. Reverse
runs had no skill tag, so nobody could count how many finished.

- runMeasure and diff pass the error to failCommand.
- An unknown word that is an existing file names the commands to put first.
- references/start.md sends skill_invoked and skill_completed beacons.

* fix(cli): the file hint stays out of the root command list

Jerrai: `hyperframes index.html` would list every top-level command. fileHint moves to
didYouMean.ts, answers only inside a command group, and has tests for the root, a missing
file and a group with no commands.
2026-11-02 10:03:08 +00:00

67 lines
2.4 KiB
TypeScript

import { existsSync } from "node:fs";
function editDistance(a: string, b: string): number {
let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
for (let i = 1; i <= a.length; i++) {
const current = [i];
for (let j = 1; j <= b.length; j++) {
const substitution = previous[j - 1]! + (a[i - 1] === b[j - 1] ? 0 : 1);
current[j] = Math.min(previous[j]! + 1, current[j - 1]! + 1, substitution);
}
previous = current;
}
return previous[b.length]!;
}
/** The candidate `input` most likely misspells or abbreviates, or undefined when none is close. */
export function closestMatch(input: string, candidates: Iterable<string>): string | undefined {
const names = [...candidates];
const prefixed = input.length >= 2 ? names.find((name) => name.startsWith(input)) : undefined;
if (prefixed) return prefixed;
const limit = input.length <= 3 ? 1 : Math.max(2, Math.floor(input.length / 3));
let best: string | undefined;
let bestDistance = limit + 1;
for (const name of names) {
const distance = editDistance(input, name);
if (distance < bestDistance) {
best = name;
bestDistance = distance;
}
}
return best;
}
export function closestNames(input: string, candidates: Iterable<string>, limit = 5): string[] {
const words = new Set(input.split("-"));
return [...candidates]
.map((name) => ({
name,
shared: name.split("-").filter((word) => words.has(word)).length,
distance: editDistance(input, name),
}))
.filter(({ shared }) => shared > 0)
.sort((a, b) => b.shared - a.shared || a.distance - b.distance || a.name.localeCompare(b.name))
.slice(0, limit)
.map(({ name }) => name);
}
const COMMAND_SYNONYMS: Record<string, string> = {
search: "catalog",
find: "catalog",
browse: "catalog",
registry: "catalog",
list: "catalog",
install: "add",
};
export function synonymMatch(input: string, candidates: Iterable<string>): string | undefined {
const target = COMMAND_SYNONYMS[input];
return target !== undefined && [...candidates].includes(target) ? target : undefined;
}
export function fileHint(word: string, path: string[], names: string[]): string | undefined {
if (path.length === 0 || names.length === 0 || !existsSync(word)) return undefined;
const group = ["hyperframes", ...path].join(" ");
return `'${word}' is a file; name a command first: '${group} <${names.join("|")}> ${word}'.`;
}