mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
* refactor(cli): render help pages with REA's help owner REA now renders the root page, every command page, help after an unknown command, the mcp group page, the mcp doctor builtin page and the completions page before incur sees argv, with incur's precedence (update check, --update without help, --mcp and the LLM manifests first; then the integrations; then --version; then root and command pages). help.ts gains arguments, custom global options, the root-only flags and integrations, and a command-list page. The --format row on the root and command pages now lists csv and tsv (#10250), the approved difference; the mcp and mcp add pages keep the document formats they accept. Every other help byte is unchanged: 397 pages compared with incur's Help for all 196 registered commands. Fixes #10250 * test(cli): elide the root command list wherever the root page is printed The golden renderer chose root-page elision by case name, so help after an unknown command froze the whole command list. It now elides any stdout that opens with the root page's rea@<version> title. * refactor(cli): answer --version, --schema and the LLM manifests in REA (#11690) * refactor(cli): answer --version, --schema and the LLM manifests in REA src/cli/discovery.ts answers help, --version, --schema, --llms and --llms-full with incur's precedence, reading the arguments as the dispatcher receives them (after result output has adapted them, so token counts and buffering are unchanged). commandManifests.ts renders the Markdown index and full manifest, the structured manifests and the --schema document; commandSuggestion.ts ranks Did-you-mean candidates. Both are adapted from incur under its MIT licence. --schema --format md stays with incur: incur renders it without Markdown escaping, unlike REA's renderer, and that difference awaits a decision on #11654. Compared with incur's serve for every registered command and format, 3746 of 3945 discovery invocations are identical; the rest are the 196 Markdown schemas left to incur and the three root help pages with the approved csv/tsv row. * test(cli): pin which discovery document wins when several are asked for Goldens for --llms with --help, --llms with --version, --llms with --llms-full, --schema with --version and --schema with --help, recorded with incur's rendering on the base branch.
75 lines
2.8 KiB
TypeScript
75 lines
2.8 KiB
TypeScript
/*
|
|
* The ranking is adapted from Incur's command suggestion.
|
|
* Copyright (c) 2025-present weth, LLC
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to
|
|
* deal in the Software without restriction, including without limitation the
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
* IN THE SOFTWARE.
|
|
*/
|
|
|
|
/** The edit distance between two strings. */
|
|
const editDistance = (left: string, right: string): number => {
|
|
const row = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
for (let i = 1; i <= left.length; i += 1) {
|
|
let diagonal = row[0]!;
|
|
row[0] = i;
|
|
for (let j = 1; j <= right.length; j += 1) {
|
|
const above = row[j]!;
|
|
row[j] =
|
|
left[i - 1] === right[j - 1]
|
|
? diagonal
|
|
: 1 + Math.min(diagonal, above, row[j - 1]!);
|
|
diagonal = above;
|
|
}
|
|
}
|
|
return row[right.length]!;
|
|
};
|
|
|
|
/**
|
|
* The command a mistyped name most likely meant, or undefined. Candidates
|
|
* that extend the input rank first, then ones that contain it, then ones
|
|
* within a small edit distance (2 for inputs of up to 4 characters, otherwise
|
|
* half the input's length). Within a tier the smallest distance wins, and the
|
|
* first candidate wins a tie. Case is ignored.
|
|
*/
|
|
export const suggestCommand = (
|
|
input: string,
|
|
candidates: Iterable<string>,
|
|
): string | undefined => {
|
|
const threshold = input.length <= 4 ? 2 : Math.floor(input.length / 2);
|
|
const lower = input.toLowerCase();
|
|
let best: string | undefined;
|
|
let bestScore = Infinity;
|
|
for (const candidate of candidates) {
|
|
const name = candidate.toLowerCase();
|
|
const distance = editDistance(lower, name);
|
|
const score =
|
|
name.startsWith(lower) && name !== lower
|
|
? distance
|
|
: name.includes(lower)
|
|
? 100 + distance
|
|
: distance <= threshold
|
|
? 200 + distance
|
|
: undefined;
|
|
if (score !== undefined && score < bestScore) {
|
|
bestScore = score;
|
|
best = candidate;
|
|
}
|
|
}
|
|
return best;
|
|
};
|