mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 06:55:55 +00:00
* perf(cli): decouple lazy CLI startup from AnalysisView, session contracts, and MCP registration * test(cli): use bundled CLI runs for heavy startup positive controls
150 lines
4.7 KiB
TypeScript
150 lines
4.7 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { declaredTokenPattern } from "../domain/schemaConstraints.js";
|
|
import { decimalTokenPattern } from "./decimalTokenPattern.js";
|
|
|
|
/** Decimal integer argv text. Leading zeros stay decimal; `0x` and exponent spellings do not. */
|
|
const DECIMAL_INTEGER_TOKEN = /^[0-9]+$/u;
|
|
|
|
type DecimalWrapper =
|
|
| { readonly kind: "default"; readonly value: number | null }
|
|
| { readonly kind: "prefault"; readonly value: number | null }
|
|
| {
|
|
readonly kind: "optional" | "exactOptional" | "nullable" | "readonly";
|
|
};
|
|
|
|
const decimalToken = (domain: z.ZodNumber) =>
|
|
declaredTokenPattern(
|
|
z.string().regex(DECIMAL_INTEGER_TOKEN, "Expected a decimal integer"),
|
|
new RegExp(decimalTokenPattern(domain), "u"),
|
|
)
|
|
.pipe(z.coerce.number<string>())
|
|
.pipe(domain);
|
|
|
|
const unwrapSchema = (field: { unwrap: () => unknown }): z.ZodType =>
|
|
field.unwrap() as z.ZodType;
|
|
|
|
/**
|
|
* Command schemas advertise Zod's output view. Publish the input view so an argv token
|
|
* grammar that transforms text survives `--schema` and other SDK conversions.
|
|
*/
|
|
export const publishArgvInput = <Field extends z.ZodType>(
|
|
field: Field,
|
|
): Field => {
|
|
const { $schema: _dialect, ...inputSchema } = z.toJSONSchema(field, {
|
|
io: "input",
|
|
});
|
|
return field.meta(inputSchema);
|
|
};
|
|
|
|
const numericStack = (
|
|
field: z.ZodType,
|
|
):
|
|
| {
|
|
readonly core: z.ZodNumber;
|
|
readonly wrappers: readonly DecimalWrapper[];
|
|
readonly description: string | undefined;
|
|
}
|
|
| undefined => {
|
|
const wrappers: DecimalWrapper[] = [];
|
|
let description: string | undefined;
|
|
let current = field;
|
|
for (;;) {
|
|
if (description === undefined && current.description !== undefined)
|
|
description = current.description;
|
|
if (current instanceof z.ZodDefault) {
|
|
wrappers.push({
|
|
kind: "default",
|
|
value: current.parse(undefined) as number | null,
|
|
});
|
|
current = unwrapSchema(current);
|
|
continue;
|
|
}
|
|
if (current instanceof z.ZodPrefault) {
|
|
wrappers.push({
|
|
kind: "prefault",
|
|
value: current.parse(undefined) as number | null,
|
|
});
|
|
current = unwrapSchema(current);
|
|
continue;
|
|
}
|
|
if (current instanceof z.ZodOptional) {
|
|
wrappers.push({ kind: "optional" });
|
|
current = unwrapSchema(current);
|
|
continue;
|
|
}
|
|
if (current instanceof z.ZodExactOptional) {
|
|
wrappers.push({ kind: "exactOptional" });
|
|
current = unwrapSchema(current);
|
|
continue;
|
|
}
|
|
if (current instanceof z.ZodNullable) {
|
|
wrappers.push({ kind: "nullable" });
|
|
current = unwrapSchema(current);
|
|
continue;
|
|
}
|
|
if (current instanceof z.ZodReadonly) {
|
|
wrappers.push({ kind: "readonly" });
|
|
current = unwrapSchema(current);
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
return current instanceof z.ZodNumber
|
|
? { core: current, wrappers, description }
|
|
: undefined;
|
|
};
|
|
|
|
const applyDecimalWrappers = (
|
|
field: z.ZodType,
|
|
wrappers: readonly DecimalWrapper[],
|
|
): z.ZodType => {
|
|
let admitted = field;
|
|
for (const wrapper of wrappers.toReversed()) {
|
|
if (wrapper.kind === "nullable") admitted = admitted.nullable();
|
|
else if (wrapper.kind === "optional") admitted = admitted.optional();
|
|
else if (wrapper.kind === "exactOptional")
|
|
admitted = admitted.exactOptional();
|
|
else if (wrapper.kind === "readonly") admitted = admitted.readonly();
|
|
else if (wrapper.kind === "prefault")
|
|
admitted = admitted.prefault(wrapper.value as never);
|
|
else if (wrapper.kind === "default")
|
|
admitted = admitted.default(wrapper.value as never);
|
|
}
|
|
return admitted;
|
|
};
|
|
|
|
/**
|
|
* Parse a decimal token, then let the domain field own bounds, defaults,
|
|
* nullability and help. JSON numbers remain valid for schema consumers.
|
|
*/
|
|
export const cliDecimalField = <Field extends z.ZodType>(
|
|
field: Field,
|
|
): z.ZodType<z.output<Field>> => {
|
|
const numeric = numericStack(field);
|
|
if (numeric === undefined)
|
|
throw new Error(
|
|
`cliDecimalField expected a numeric domain field, received ${field.constructor.name}`,
|
|
);
|
|
const admitted = applyDecimalWrappers(
|
|
z.union([numeric.core, decimalToken(numeric.core)]),
|
|
numeric.wrappers,
|
|
);
|
|
const described =
|
|
numeric.description === undefined
|
|
? admitted
|
|
: admitted.describe(numeric.description);
|
|
return publishArgvInput(described) as z.ZodType<z.output<Field>>;
|
|
};
|
|
|
|
/** Adapt each numeric property of a shared shape; other properties stay unchanged. */
|
|
export const cliDecimalShape = <Shape extends Record<string, z.ZodType>>(
|
|
shape: Shape,
|
|
): Shape =>
|
|
Object.fromEntries(
|
|
Object.entries(shape).map(([key, field]) => [
|
|
key,
|
|
numericStack(field) === undefined ? field : cliDecimalField(field),
|
|
]),
|
|
) as Shape;
|