| |
| |
| |
| |
|
|
| import chalk from "chalk"; |
| import type { Argument, Command, Help, Option } from "commander"; |
| |
|
|
| |
|
|
| const cyanBold = chalk.cyan.bold; |
| const greenBold = chalk.green.bold; |
| const yellowBold = chalk.yellow.bold; |
| const yellow = chalk.yellow; |
| const bold = chalk.bold; |
| const dim = chalk.dim; |
| const dimBorder = chalk.dim; |
|
|
| |
|
|
| |
| const ANSI_RE = /\x1b\[[0-9;]*m/g; |
|
|
| function stripAnsi(str: string): number { |
| return str.replace(ANSI_RE, "").length; |
| } |
|
|
| |
|
|
| |
| const COMMAND_GROUPS: { panel: string; commands: string[] }[] = [ |
| { |
| panel: "Memory", |
| commands: ["add", "search", "get", "list", "update", "delete"], |
| }, |
| { |
| panel: "Management", |
| commands: ["init", "status", "import", "help", "entity", "event", "config"], |
| }, |
| ]; |
|
|
| |
| const COMMAND_ORDER: string[] = COMMAND_GROUPS.flatMap((g) => g.commands); |
|
|
| |
|
|
| const OPTION_PANELS: Record<string, Record<string, string>> = { |
| add: { |
| "--user-id": "Scope", |
| "--agent-id": "Scope", |
| "--app-id": "Scope", |
| "--run-id": "Scope", |
| "--output": "Output", |
| "--api-key": "Connection", |
| "--base-url": "Connection", |
| }, |
| search: { |
| "--user-id": "Scope", |
| "--agent-id": "Scope", |
| "--app-id": "Scope", |
| "--run-id": "Scope", |
| "--top-k": "Search", |
| "--threshold": "Search", |
| "--rerank": "Search", |
| "--keyword": "Search", |
| "--filter": "Search", |
| "--fields": "Search", |
| "--graph": "Search", |
| "--no-graph": "Search", |
| "--output": "Output", |
| "--api-key": "Connection", |
| "--base-url": "Connection", |
| }, |
| get: { |
| "--output": "Output", |
| "--api-key": "Connection", |
| "--base-url": "Connection", |
| }, |
| list: { |
| "--user-id": "Scope", |
| "--agent-id": "Scope", |
| "--app-id": "Scope", |
| "--run-id": "Scope", |
| "--page": "Pagination", |
| "--page-size": "Pagination", |
| "--category": "Filters", |
| "--after": "Filters", |
| "--before": "Filters", |
| "--graph": "Filters", |
| "--no-graph": "Filters", |
| "--output": "Output", |
| "--api-key": "Connection", |
| "--base-url": "Connection", |
| }, |
| update: { |
| "--output": "Output", |
| "--api-key": "Connection", |
| "--base-url": "Connection", |
| }, |
| delete: { |
| "--user-id": "Scope", |
| "--agent-id": "Scope", |
| "--app-id": "Scope", |
| "--run-id": "Scope", |
| "--output": "Output", |
| "--api-key": "Connection", |
| "--base-url": "Connection", |
| }, |
| status: { |
| "--output": "Output", |
| "--api-key": "Connection", |
| "--base-url": "Connection", |
| }, |
| import: { |
| "--user-id": "Scope", |
| "--agent-id": "Scope", |
| "--output": "Output", |
| "--api-key": "Connection", |
| "--base-url": "Connection", |
| }, |
| }; |
|
|
| const PANEL_ORDER: string[] = [ |
| "Scope", |
| "Search", |
| "Pagination", |
| "Filters", |
| "Output", |
| "Connection", |
| ]; |
|
|
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function renderPanel(title: string, rows: string[], width: number): string { |
| if (rows.length === 0) return ""; |
|
|
| |
| const inner = width - 2; |
|
|
| |
| const titleStr = ` ${title} `; |
| const fillLen = Math.max(0, inner - 1 - titleStr.length); |
| const topLine = |
| dimBorder("╭─") + |
| dimBorder(titleStr) + |
| dimBorder("─".repeat(fillLen)) + |
| dimBorder("╮"); |
|
|
| |
| const bottomLine = |
| dimBorder("╰") + dimBorder("─".repeat(inner)) + dimBorder("╯"); |
|
|
| |
| const contentLines = rows.map((row) => { |
| const visLen = stripAnsi(row); |
| const pad = Math.max(0, inner - 1 - visLen); |
| return `${dimBorder("│")} ${row}${" ".repeat(pad)}${dimBorder("│")}`; |
| }); |
|
|
| return [topLine, ...contentLines, bottomLine].join("\n"); |
| } |
|
|
| |
|
|
| function formatOptionTerm(opt: Option): string { |
| const parts: string[] = []; |
| if (opt.short) parts.push(opt.short); |
| if (opt.long) parts.push(opt.long); |
| let term = parts.join(", "); |
|
|
| |
| if (opt.flags) { |
| const match = opt.flags.match(/<[^>]+>|\[[^\]]+\]/); |
| if (match) { |
| term += ` ${match[0]}`; |
| } |
| } |
| return term; |
| } |
|
|
| |
|
|
| function getLongFlag(opt: Option): string { |
| if (opt.long) return opt.long; |
| return opt.short || ""; |
| } |
|
|
| |
|
|
| function formatDefault(opt: Option): string { |
| if (opt.defaultValue !== undefined && opt.defaultValue !== false) { |
| return dim(` [default: ${opt.defaultValue}]`); |
| } |
| return ""; |
| } |
|
|
| |
|
|
| export function richFormatHelp(cmd: Command, helper: Help): string { |
| const width = process.stdout.columns || 80; |
| const lines: string[] = []; |
|
|
| const isRoot = !cmd.parent; |
|
|
| |
| const usage = helper.commandUsage(cmd); |
| lines.push(""); |
| if (isRoot) { |
| |
| lines.push( |
| ` ${yellow("Usage:")} ${bold(cmd.name())} ${yellow("<command>")} ${bold("[options]")}`, |
| ); |
| } else { |
| |
| const usageParts = usage.split(" "); |
| const cmdPath: string[] = []; |
| const argParts: string[] = []; |
| let pastCmd = false; |
| for (const part of usageParts) { |
| if (!pastCmd && !part.startsWith("[") && !part.startsWith("<")) { |
| cmdPath.push(part); |
| } else { |
| pastCmd = true; |
| argParts.push(part); |
| } |
| } |
| lines.push( |
| ` ${yellow("Usage:")} ${bold(cmdPath.join(" "))} ${yellow(argParts.join(" "))}`, |
| ); |
| } |
| lines.push(""); |
|
|
| |
| const desc = helper.commandDescription(cmd); |
| if (desc) { |
| |
| const descLines = desc.split("\n"); |
| for (let i = 0; i < descLines.length; i++) { |
| const dLine = descLines[i]; |
| |
| if (i === 0 || dLine.trim() === "") { |
| lines.push(` ${dLine}`); |
| } else { |
| lines.push(` ${dim(dLine)}`); |
| } |
| } |
| lines.push(""); |
| } |
|
|
| |
| if (!isRoot) { |
| const visibleArgs = helper.visibleArguments(cmd); |
| if (visibleArgs.length > 0) { |
| const maxLen = Math.max( |
| ...visibleArgs.map((a: Argument) => a.name().length), |
| ); |
| const argRows = visibleArgs.map((a: Argument) => { |
| const name = cyanBold(a.name().padEnd(maxLen)); |
| const description = helper.argumentDescription(a); |
| return ` ${name} ${description}`; |
| }); |
| const panel = renderPanel("Arguments", argRows, width); |
| if (panel) lines.push(panel); |
| } |
| } |
|
|
| |
| const visibleOpts = helper.visibleOptions(cmd); |
| const cmdName = cmd.name(); |
| const panelMap = |
| !isRoot && OPTION_PANELS[cmdName] ? OPTION_PANELS[cmdName] : {}; |
|
|
| const grouped: Record<string, Option[]> = { Options: [] }; |
| for (const panelName of PANEL_ORDER) { |
| grouped[panelName] = []; |
| } |
|
|
| for (const opt of visibleOpts) { |
| const flag = getLongFlag(opt); |
| const panel = panelMap[flag]; |
| if (panel && PANEL_ORDER.includes(panel)) { |
| grouped[panel].push(opt); |
| } else { |
| grouped.Options.push(opt); |
| } |
| } |
|
|
| |
| const visibleCmds = helper.visibleCommands(cmd); |
|
|
| if (isRoot) { |
| |
| if (grouped.Options.length > 0) { |
| const optRows = formatOptionRows(grouped.Options); |
| const panel = renderPanel("Options", optRows, width); |
| if (panel) lines.push(panel); |
| } |
| if (visibleCmds.length > 0) { |
| const cmdMap = new Map(visibleCmds.map((c) => [c.name(), c])); |
| for (const group of COMMAND_GROUPS) { |
| const groupCmds = group.commands |
| .map((name) => cmdMap.get(name)) |
| .filter((c): c is Command => c !== undefined); |
| if (groupCmds.length === 0) continue; |
| const maxLen = Math.max(...groupCmds.map((c) => c.name().length)); |
| const cmdRows = groupCmds.map((c) => { |
| const name = cyanBold(c.name().padEnd(maxLen)); |
| const description = helper.subcommandDescription(c); |
| return ` ${name} ${description}`; |
| }); |
| const panel = renderPanel(group.panel, cmdRows, width); |
| if (panel) lines.push(panel); |
| } |
| } |
| } else { |
| |
| const panelSequence = ["Options", ...PANEL_ORDER]; |
| for (const panelName of panelSequence) { |
| const opts = grouped[panelName]; |
| if (opts && opts.length > 0) { |
| const optRows = formatOptionRows(opts); |
| const panel = renderPanel(panelName, optRows, width); |
| if (panel) lines.push(panel); |
| } |
| } |
| |
| if (visibleCmds.length > 0) { |
| const maxLen = Math.max(...visibleCmds.map((c) => c.name().length)); |
| const cmdRows = visibleCmds.map((c) => { |
| const name = cyanBold(c.name().padEnd(maxLen)); |
| const description = helper.subcommandDescription(c); |
| return ` ${name} ${description}`; |
| }); |
| const panel = renderPanel("Commands", cmdRows, width); |
| if (panel) lines.push(panel); |
| } |
| } |
|
|
| lines.push(""); |
| return lines.join("\n"); |
| } |
|
|
| |
|
|
| function formatOptionRows(opts: Option[]): string[] { |
| const terms = opts.map((o) => formatOptionTerm(o)); |
| const maxTermLen = Math.max(...terms.map((t) => t.length)); |
|
|
| return opts.map((opt, i) => { |
| const term = cyanBold(terms[i].padEnd(maxTermLen)); |
| const desc = opt.description || ""; |
| const def = formatDefault(opt); |
| return ` ${term} ${desc}${def}`; |
| }); |
| } |
|
|
| |
|
|
| function sortCommands(cmds: Command[]): Command[] { |
| return [...cmds].sort((a, b) => { |
| const ai = COMMAND_ORDER.indexOf(a.name()); |
| const bi = COMMAND_ORDER.indexOf(b.name()); |
| |
| const aIdx = ai === -1 ? COMMAND_ORDER.length : ai; |
| const bIdx = bi === -1 ? COMMAND_ORDER.length : bi; |
| return aIdx - bIdx; |
| }); |
| } |
|
|