mirror of
https://github.com/morluto/rea
synced 2026-10-27 22:45:19 -04:00
fix(cli): preserve output and comparison input validation (#981)
This commit is contained in:
+20
-11
@@ -6,6 +6,7 @@ const UNSUPPORTED_OUTPUT_COMBINATION_MESSAGE =
|
||||
"Token windows cannot preserve structured output. Remove --token-limit/--token-offset and use --filter-output or command pagination.";
|
||||
|
||||
type StructuredOutputFormat = "json" | "jsonl" | "yaml";
|
||||
const INCUR_OUTPUT_FORMATS = new Set(["toon", "json", "yaml", "md", "jsonl"]);
|
||||
|
||||
export type CliOutputArgumentValidation =
|
||||
| { readonly ok: true }
|
||||
@@ -21,6 +22,7 @@ type JsonRecord = Record<string, unknown>;
|
||||
interface ParsedCliOutputArguments {
|
||||
readonly filterOutput: boolean;
|
||||
readonly format: string;
|
||||
readonly parseError: boolean;
|
||||
readonly tokenWindow: boolean;
|
||||
}
|
||||
|
||||
@@ -50,40 +52,45 @@ const parseCliOutputArguments = (
|
||||
}
|
||||
if (argument === "--format") {
|
||||
const value = arguments_[index + 1];
|
||||
if (value !== undefined) {
|
||||
if (value) {
|
||||
// Incur stops parsing at an invalid format before applying any later
|
||||
// output controls. Let it report that malformed flag itself.
|
||||
if (!INCUR_OUTPUT_FORMATS.has(value))
|
||||
return { filterOutput, format, parseError: true, tokenWindow: false };
|
||||
format = value;
|
||||
index += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (argument === "--filter-output") {
|
||||
if (arguments_[index + 1] !== undefined) {
|
||||
if (arguments_[index + 1]) {
|
||||
filterOutput = true;
|
||||
index += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (argument === "--token-limit" || argument === "--token-offset") {
|
||||
if (arguments_[index + 1] !== undefined) {
|
||||
const value = arguments_[index + 1];
|
||||
if (value) {
|
||||
const numericValue = Number(value);
|
||||
if (!Number.isFinite(numericValue) || value.trim() === "")
|
||||
return { filterOutput, format, parseError: true, tokenWindow: false };
|
||||
tokenWindow = true;
|
||||
index += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
argument?.startsWith("--token-limit=") ||
|
||||
argument?.startsWith("--token-offset=")
|
||||
)
|
||||
tokenWindow = true;
|
||||
}
|
||||
return { filterOutput, format, tokenWindow };
|
||||
return { filterOutput, format, parseError: false, tokenWindow };
|
||||
};
|
||||
|
||||
/** Reject text-window controls that would corrupt a structured document. */
|
||||
export const validateCliOutputArguments = (
|
||||
arguments_: readonly string[],
|
||||
): CliOutputArgumentValidation => {
|
||||
const { format, tokenWindow } = parseCliOutputArguments(arguments_);
|
||||
const { format, parseError, tokenWindow } =
|
||||
parseCliOutputArguments(arguments_);
|
||||
if (parseError) return { ok: true };
|
||||
if (
|
||||
tokenWindow &&
|
||||
(format === "json" || format === "jsonl" || format === "yaml")
|
||||
@@ -101,7 +108,9 @@ export const validateCliOutputArguments = (
|
||||
export const renderEmptyFilteredCliOutput = (
|
||||
arguments_: readonly string[],
|
||||
): string | undefined => {
|
||||
const { filterOutput, format } = parseCliOutputArguments(arguments_);
|
||||
const { filterOutput, format, parseError } =
|
||||
parseCliOutputArguments(arguments_);
|
||||
if (parseError) return undefined;
|
||||
if (
|
||||
filterOutput &&
|
||||
(format === "json" || format === "jsonl" || format === "yaml")
|
||||
|
||||
Reference in New Issue
Block a user