refactor(cli): share error command projection (#16584)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-04 12:40:52 +08:00
1 parent 275ad75606
commit 08dfb90b6f
1 file changed
+16 -29
+16 -29
View File
@@ -44,6 +44,17 @@ export interface CliCommandOutputProjectionContext {
readonly recordedCommands?: Readonly<Record<string, string>> | undefined;
}
const projectedCommand = (
context: CliCommandOutputProjectionContext,
operation: string,
): string | undefined =>
cliCommandForOperation(
operation,
context.commands,
context.cliName ?? "rea",
context.recordedCommands,
);
/** Flatten a finalized outcome only when returning the command's JSON output. */
export const projectCliAnalysisResult = <Value>(
result: Result<Value, JsonValue>,
@@ -310,16 +321,8 @@ const cliTargetFailureRemediation = (
};
const mcp = renderTargetFailureRecovery(facts, MCP_TARGET_FAILURE_SPELLING);
if (!remediation.action.startsWith(mcp)) return undefined;
const cliName = context.cliName ?? "rea";
const mappedOperation = (operation: string): string | undefined =>
cliCommandForOperation(
operation,
context.commands,
cliName,
context.recordedCommands,
);
const spelling: TargetFailureSpelling = {
operation: (operation) => mappedOperation(operation) ?? operation,
operation: (operation) => projectedCommand(context, operation) ?? operation,
provider: (providerId) => `--provider ${providerId}`,
};
const operations = orderedOperations(context.operations, details.operation);
@@ -327,7 +330,7 @@ const cliTargetFailureRemediation = (
(field) => cliFlagForOperations(field, operations) ?? field,
);
const cliOperations = facts.alternatives?.operations?.filter(
(operation) => mappedOperation(operation) !== undefined,
(operation) => projectedCommand(context, operation) !== undefined,
);
const cliProviders = facts.alternatives?.providers;
const cliFacts: TargetFailureRecoveryFacts = {
@@ -372,12 +375,7 @@ const cliInputSchemaRemediation = (
);
const index = remediation.action.indexOf(mcp);
if (index < 0) return undefined;
const command = cliCommandForOperation(
operation,
context.commands,
context.cliName ?? "rea",
context.recordedCommands,
);
const command = projectedCommand(context, operation);
if (command === undefined) return undefined;
const spelling: InputSchemaRemediationSpelling = {
subject: () => command,
@@ -431,16 +429,10 @@ const relatedCliCommands = (
context: CliCommandOutputProjectionContext,
): Record<string, JsonValue> | undefined => {
if (!Array.isArray(details.related_operations)) return undefined;
const cliName = context.cliName ?? "rea";
const commands: Record<string, JsonValue> = {};
for (const operation of details.related_operations) {
if (typeof operation !== "string") continue;
const command = cliCommandForOperation(
operation,
context.commands,
cliName,
context.recordedCommands,
);
const command = projectedCommand(context, operation);
if (command !== undefined) commands[operation] = command;
}
return Object.keys(commands).length === 0 ? undefined : commands;
@@ -561,12 +553,7 @@ export const cliRefusalSpelling = (
: `${parameter} (in ${flag})`;
},
call: (operation) => {
const command = cliCommandForOperation(
operation,
context.commands,
context.cliName ?? "rea",
context.recordedCommands,
);
const command = projectedCommand(context, operation);
return command === undefined
? `the next ${operation} call`
: `the next ${command} run`;