From 08dfb90b6f58ef3eb28913c514065ade25bfc2a5 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 4 Dec 2026 12:40:52 +0800 Subject: [PATCH] refactor(cli): share error command projection (#16584) --- src/cli/projectCliAnalysisResult.ts | 45 ++++++++++------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/cli/projectCliAnalysisResult.ts b/src/cli/projectCliAnalysisResult.ts index d1bdff07f1d..cf02f67a544 100644 --- a/src/cli/projectCliAnalysisResult.ts +++ b/src/cli/projectCliAnalysisResult.ts @@ -44,6 +44,17 @@ export interface CliCommandOutputProjectionContext { readonly recordedCommands?: Readonly> | 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 = ( result: Result, @@ -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 | undefined => { if (!Array.isArray(details.related_operations)) return undefined; - const cliName = context.cliName ?? "rea"; const commands: Record = {}; 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`;