Files
reverse-engineering-anything/src/cli/omittableDefaults.ts
T
Ysopwnableandgensokyo-dev 62014b566d refactor(cli)!: dispatch commands through REA-owned execution
Run ordinary CLI commands through REA's registry, argv parser and result owner.
Remove Incur, the second admission parse, SDK error middleware, output capture
and redundant command traversal. Preserve explicit redaction, publication
receipts, incremental output, terminal notice eligibility and drain ownership.
Migrate fixtures to real registrations and retain the derived-source notices.

Validation includes unchanged public parity goldens, public and lifecycle
regressions, repaired fixture consumers, real TTY base comparisons, exports
beyond the engine string limit, four schema catalogs and final static/docs
checks. Native provider and optional validator coverage limits are in the PR.

Fixes #11654. Fixes #10583. Refs #11682, #11476 and #10511.

BREAKING CHANGE: Markdown argument diagnostics use REA's literal escaping,
table widths and token counts. Flag-shaped unknown MCP subcommands honor the
selected format. Completion --schema/--version routes use the canonical
REA-owned hook rather than the retired SDK generator. Refresh affected literal
snapshots and regenerate completion hooks with rea completions <shell>.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-16 13:56:34 +00:00

56 lines
1.9 KiB
TypeScript

import { z } from "zod";
import type { CliInstance } from "./types.js";
import { cliCommands } from "./commandRegistry.js";
type CommandInputs = Partial<Record<(typeof INPUT_GROUPS)[number], unknown>>;
const INPUT_GROUPS = ["args", "options"] as const;
/**
* Advertise parser-filled defaults as omittable CLI inputs.
*
* The CLI derives `--schema`, the `--llms` manifests, and help synopses from
* Zod's output view, where a defaulted field always has a value and is
* therefore listed as required. Callers may omit such a field, so each one is
* wrapped as optional while its default, constraints, metadata, and parsed
* value stay unchanged.
*/
export const presentOmittableDefaults = (cli: CliInstance): void => {
for (const { definition } of cliCommands(cli)) presentInputs(definition);
};
const presentInputs = (command: CommandInputs): void => {
for (const group of INPUT_GROUPS) {
const schema = command[group];
if (schema instanceof z.ZodObject)
command[group] = withOmittableDefaults(schema);
}
};
const withOmittableDefaults = (schema: z.ZodObject): z.ZodObject => {
const omittable = Object.entries(schema.shape).filter(([, field]) =>
fillsOmission(field),
);
if (omittable.length === 0) return schema;
const metadata = schema.meta();
const result = schema.safeExtend(
Object.fromEntries(
omittable.map(([key, field]) => [key, optionalWithMetadata(field)]),
),
);
return metadata === undefined ? result : result.meta(metadata);
};
/** Let the object parser decide omission; the CLI advertises required outputs. */
const fillsOmission = (field: z.ZodType): boolean =>
field._zod.optout === undefined &&
z.object({ value: field }).safeParse({}).success;
const optionalWithMetadata = (field: z.ZodType): z.ZodType => {
const metadata = field.meta();
return metadata === undefined
? field.optional()
: field.optional().meta(metadata);
};