openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
98 lines (97 loc) • 4.56 kB
JavaScript
import { _ as getSubCliCommandMetadata, b as getCoreCliCommandNamesCore, n as getCommandPathWithRootOptions } from "./argv-CbnKpH-r.mjs";
import { i as stripAnsi } from "./ansi-CZa2t0Hf.mjs";
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { r as theme } from "./theme-BOI0xyRZ.mjs";
import { t as ExpectedCliError } from "./failure-output-DVVDqwuu.mjs";
import { t as formatDocsLink } from "./links-41lO3DKP.mjs";
import { t as levenshteinDistance } from "./levenshtein-distance-DsLDlE7K.mjs";
//#region src/cli/program/error-output.ts
const MAX_SUGGESTIONS = 3;
function formatCliCommandSuggestions(e, t = [], n) {
const r = e.trim().toLowerCase();
if (!r) return;
const i = n ?? (t.length === 0 ? [...getCoreCliCommandNamesCore(), ...getSubCliCommandMetadata().map((e) => e.name)] : []);
const a = [...new Set([...i].filter(Boolean))].toSorted((e, t) => e.localeCompare(t));
if ((r === "upgrade" || r === "udpate") && a.includes("update")) return formatCliSuggestionLines(["update"], t);
const o = Math.max(1, Math.floor(r.length * .4));
const s = a.map((e) => ({
command: e,
distance: levenshteinDistance(r, e)
})).filter(({ command: e, distance: t }) => e !== r && t <= o).toSorted((e, t) => e.distance - t.distance || e.command.localeCompare(t.command)).slice(0, MAX_SUGGESTIONS).map(({ command: e }) => e);
if (s.length === 0) return;
return formatCliSuggestionLines(s, t);
}
function formatCliSuggestionLines(e, t) {
const n = ["openclaw", ...t].join(" ");
return `Did you mean this?\n${e.map((e) => ` ${formatCliCommand(`${n} ${e}`)}`).join("\n")}`;
}
function stripCommanderErrorPrefix(e) {
return e.trim().replace(/^error:\s*/i, "").trim();
}
function lines(...e) {
return `${e.filter((e) => Boolean(e)).join("\n")}\n`;
}
function formatHelpHint(e, t) {
const n = t?.commandPath ?? (e ? getCommandPathWithRootOptions(e, 2) : []);
const r = formatCliCommand([
"openclaw",
...n,
"--help"
].join(" "));
return `${theme.muted("Try:")} ${theme.command(r)}`;
}
function formatCliMachineOutput(e) {
const t = `Docs: ${formatDocsLink("/cli", "docs.openclaw.ai/cli", { force: false })}`;
return stripAnsi(e).replace(/^Docs:.*$/mu, t);
}
function formatUnknownCommandMessage(e, t) {
return t.length > 0 ? `OpenClaw ${t.join(" ")} has no command "${e}".` : `OpenClaw does not know the command "${e}".`;
}
function formatCliUnknownCommandOutput(e, t = {}) {
const n = t.commandPath ?? [];
const r = n.length > 0;
return lines(theme.error(formatUnknownCommandMessage(e, n)), formatCliCommandSuggestions(e, n, t.commandNames), formatHelpHint(t.argv, { commandPath: n }), r ? void 0 : `${theme.muted("Plugin command?")} ${theme.command(formatCliCommand("openclaw plugins list"))}`, `${theme.muted("Docs:")} ${formatDocsLink("/cli", "docs.openclaw.ai/cli")}`);
}
function createCliParseError(e, t = {}) {
const n = stripCommanderErrorPrefix(e);
const r = n.match(/^unknown command ['"`](.+?)['"`]/i);
if (r) return createCliUnknownCommandError(r[1] ?? "", t, { humanOutputWritten: true });
const i = formatCliParseErrorOutput(e, t);
return new ExpectedCliError({
message: n,
humanOutput: i,
humanOutputWritten: true,
machineOutput: formatCliMachineOutput(i)
});
}
function createCliUnknownCommandError(e, t = {}, n = {}) {
const r = t.commandPath ?? [];
const i = formatCliUnknownCommandOutput(e, t);
return new ExpectedCliError({
message: formatUnknownCommandMessage(e, r),
humanOutput: i,
humanOutputWritten: n.humanOutputWritten,
machineOutput: formatCliMachineOutput(i)
});
}
function formatOrdinaryCliParseErrorMessage(e) {
for (const [t, n] of [
[/^unknown option ['"`](.+?)['"`]/i, "OpenClaw does not recognize option"],
[/^missing required argument ['"`](.+?)['"`]/i, "Missing required argument"],
[/^required option ['"`](.+?)['"`] not specified/i, "Missing required option"]
]) {
const r = e.match(t);
if (r) return `${n} "${r[1] ?? ""}".`;
}
if (/^too many arguments\b/i.test(e)) return "Too many arguments for this command.";
return `OpenClaw could not parse this command: ${e}`;
}
function formatCliParseErrorOutput(e, t = {}) {
const n = stripCommanderErrorPrefix(e);
const r = n.match(/^unknown command ['"`](.+?)['"`]/i);
if (r) return formatCliUnknownCommandOutput(r[1] ?? "", t);
const i = formatOrdinaryCliParseErrorMessage(n);
return lines(theme.error(i), formatHelpHint(t.argv, { commandPath: t.commandPath }));
}
//#endregion
export { createCliUnknownCommandError as n, formatCliParseErrorOutput as r, createCliParseError as t };