feat: complete REA remediation program
This commit is contained in:
@@ -1,5 +1,18 @@
|
||||
const SAFE_VALIDATION_MESSAGE =
|
||||
"REA could not read the command arguments. Run `rea --help`, correct the arguments, then try again.";
|
||||
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";
|
||||
|
||||
export type CliOutputArgumentValidation =
|
||||
| { readonly ok: true }
|
||||
| {
|
||||
readonly ok: false;
|
||||
readonly format: StructuredOutputFormat;
|
||||
readonly code: "UNSUPPORTED_OUTPUT_COMBINATION";
|
||||
readonly message: string;
|
||||
};
|
||||
|
||||
type JsonRecord = Record<string, unknown>;
|
||||
|
||||
@@ -14,6 +27,70 @@ const validationError = (value: unknown): JsonRecord | undefined => {
|
||||
return undefined;
|
||||
};
|
||||
|
||||
/** Reject text-window controls that would corrupt a structured document. */
|
||||
export const validateCliOutputArguments = (
|
||||
arguments_: readonly string[],
|
||||
): CliOutputArgumentValidation => {
|
||||
let format: string = "toon";
|
||||
let tokenWindow = false;
|
||||
for (let index = 0; index < arguments_.length; index += 1) {
|
||||
const argument = arguments_[index];
|
||||
if (argument === "--") break;
|
||||
if (argument === "--json") {
|
||||
format = "json";
|
||||
continue;
|
||||
}
|
||||
if (argument === "--format") {
|
||||
const value = arguments_[index + 1];
|
||||
if (value !== undefined) {
|
||||
format = value;
|
||||
index += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (argument?.startsWith("--format=")) {
|
||||
format = argument.slice("--format=".length);
|
||||
continue;
|
||||
}
|
||||
if (argument === "--token-limit" || argument === "--token-offset") {
|
||||
if (arguments_[index + 1] !== undefined) {
|
||||
tokenWindow = true;
|
||||
index += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
argument?.startsWith("--token-limit=") ||
|
||||
argument?.startsWith("--token-offset=")
|
||||
)
|
||||
tokenWindow = true;
|
||||
}
|
||||
if (
|
||||
tokenWindow &&
|
||||
(format === "json" || format === "jsonl" || format === "yaml")
|
||||
)
|
||||
return {
|
||||
ok: false,
|
||||
format,
|
||||
code: "UNSUPPORTED_OUTPUT_COMBINATION",
|
||||
message: UNSUPPORTED_OUTPUT_COMBINATION_MESSAGE,
|
||||
};
|
||||
return { ok: true };
|
||||
};
|
||||
|
||||
/** Render one complete structured error before command execution. */
|
||||
export const renderCliOutputArgumentError = (
|
||||
error: Exclude<CliOutputArgumentValidation, { readonly ok: true }>,
|
||||
): string => {
|
||||
const value = {
|
||||
ok: false,
|
||||
error: { code: error.code, message: error.message },
|
||||
};
|
||||
if (error.format === "yaml")
|
||||
return `ok: false\nerror:\n code: ${error.code}\n message: ${JSON.stringify(error.message)}\n`;
|
||||
return `${JSON.stringify(value)}\n`;
|
||||
};
|
||||
|
||||
/** Remove validator internals from Incur's caller-visible CLI output. */
|
||||
export const sanitizeCliOutput = (output: string): string => {
|
||||
const trimmed = output.trimStart();
|
||||
|
||||
Reference in New Issue
Block a user