/** * `_schema` — internal hidden command exposing the entire commander tree as JSON. * * Adapter consumers (e.g. `openclaw-clawclaw`) call this to auto-generate one tool * per leaf subcommand without having to hand-translate each `ccl` command. * * Hidden by convention: name starts with `_` so the walker excludes it from output, * and `--help` listings of regular tooling generally skip underscore-prefixed entries. */ import { Command, Option, Argument } from 'commander'; import { getMeta, type CommandMeta } from '../lib/command-meta.js'; interface OptionSchema { flags: string; long?: string | null; short?: string | null; attributeName: string; description?: string; defaultValue?: unknown; /** option marked with `` — value required */ required: boolean; /** option marked with `[value]` — value optional */ optional: boolean; /** option marked with `.makeOptionMandatory()` — option itself required */ mandatory: boolean; negate: boolean; takesValue: boolean; } interface ArgumentSchema { name: string; description?: string; required: boolean; variadic: boolean; defaultValue?: unknown; } interface CommandSchema { name: string; description?: string; aliases: string[]; options: OptionSchema[]; arguments: ArgumentSchema[]; subcommands: CommandSchema[]; /** path from root, joined by space, e.g. "account leaderboard" */ path: string; /** true when the command has no subcommands and is therefore directly invocable */ leaf: boolean; /** Adapter-facing metadata (omitted if no metadata is set on this command). */ meta?: CommandMeta; } interface WalkOptions { includeHidden: boolean; } function dumpOption(o: Option): OptionSchema { const flags = o.flags; const takesValue = flags.includes('<') || flags.includes('['); return { flags, long: o.long ?? null, short: o.short ?? null, attributeName: o.attributeName(), description: o.description || undefined, defaultValue: o.defaultValue, required: o.required, optional: o.optional, mandatory: o.mandatory, negate: o.negate, takesValue, }; } function dumpArgument(a: Argument): ArgumentSchema { return { name: a.name(), description: a.description || undefined, required: a.required, variadic: a.variadic, defaultValue: a.defaultValue, }; } function isHiddenCommand(cmd: Command): boolean { return (cmd as any)._hidden === true; } function isHiddenOption(option: Option): boolean { return (option as any).hidden === true; } function walkCommand(cmd: Command, parentPath: string[] = [], opts: WalkOptions): CommandSchema { const path = [...parentPath, cmd.name()]; const visibleSubs = (cmd.commands || []).filter((c) => ( opts.includeHidden || (!c.name().startsWith('_') && !isHiddenCommand(c)) )); const meta = getMeta(cmd); return { name: cmd.name(), description: cmd.description() || undefined, aliases: cmd.aliases() ?? [], options: (cmd.options || []).filter((o) => opts.includeHidden || !isHiddenOption(o)).map(dumpOption), arguments: (cmd.registeredArguments || []).map(dumpArgument), subcommands: visibleSubs.map((c) => walkCommand(c, path, opts)), path: path.slice(1).join(' '), // exclude root program name from path leaf: visibleSubs.length === 0, ...(meta && Object.keys(meta).length > 0 ? { meta } : {}), }; } export function createSchemaCommand(getProgram: () => Command): Command { return new Command('_schema') .description('[internal] dump full command tree as JSON for adapter auto-generation') .option('--all', 'Include hidden/deprecated/debug commands and options') .option('--pretty', 'Indent JSON output') .action((opts: { all?: boolean; pretty?: boolean }) => { const tree = walkCommand(getProgram(), [], { includeHidden: opts.all === true }); console.log(JSON.stringify(tree, null, opts.pretty ? 2 : 0)); }); // Note: to hide from `ccl --help`, the parent passes { hidden: true } to addCommand(). // See cli.ts registration site. }