refactor: extract helper functions to reduce complexity in discovery and commands (#1172)

Reduced complexity in 2 functions by extracting focused helpers:

1. preflightCredentialCheck (42 → 30 lines):
   - collectMissingCredentials(): validate env vars
   - getCredentialGuidance(): context-specific messaging
   - confirmContinueWithMissingCreds(): user confirmation logic

2. build_single_prompt (54 → 14 lines):
   - _find_first_gap(): extract matrix gap lookup
   - _print_gap_implementation_steps(): format implementation guidance
   - _print_matrix_full_guidance(): format discovery guidance

Improves testability and readability while preserving behavior.

Agent: complexity-hunter

Co-authored-by: spawn-refactor-bot <refactor@openrouter.ai>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
A 2026-02-14 20:23:26 -08:00 committed by GitHub
parent df96db3499
commit 2fbe225855
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 58 additions and 35 deletions

View file

@ -603,13 +603,8 @@ function getAuthHint(manifest: Manifest, cloud: string): string | undefined {
/** Check for missing credentials before running a script and warn the user.
* In interactive mode, asks for confirmation. In non-interactive mode, just warns. */
export async function preflightCredentialCheck(manifest: Manifest, cloud: string): Promise<void> {
const cloudAuth = manifest.clouds[cloud].auth;
if (cloudAuth.toLowerCase() === "none") return;
const authVars = parseAuthEnvVars(cloudAuth);
function collectMissingCredentials(authVars: string[]): string[] {
const missing: string[] = [];
if (!process.env.OPENROUTER_API_KEY) {
missing.push("OPENROUTER_API_KEY");
}
@ -618,29 +613,44 @@ export async function preflightCredentialCheck(manifest: Manifest, cloud: string
missing.push(v);
}
}
return missing;
}
function getCredentialGuidance(cloud: string, onlyOpenRouter: boolean): string {
if (onlyOpenRouter) {
return "The script will open your browser to authenticate with OpenRouter.";
}
return `Run ${pc.cyan(`spawn ${cloud}`)} for setup instructions.`;
}
async function confirmContinueWithMissingCreds(onlyOpenRouter: boolean): Promise<boolean> {
const confirmMsg = onlyOpenRouter
? "Continue? You'll authenticate via browser."
: "Continue anyway? The script will prompt for missing credentials.";
const shouldContinue = await p.confirm({
message: confirmMsg,
initialValue: true,
});
return !p.isCancel(shouldContinue) && shouldContinue;
}
export async function preflightCredentialCheck(manifest: Manifest, cloud: string): Promise<void> {
const cloudAuth = manifest.clouds[cloud].auth;
if (cloudAuth.toLowerCase() === "none") return;
const authVars = parseAuthEnvVars(cloudAuth);
const missing = collectMissingCredentials(authVars);
if (missing.length === 0) return;
const cloudName = manifest.clouds[cloud].name;
p.log.warn(`Missing credentials for ${cloudName}: ${missing.map(v => pc.cyan(v)).join(", ")}`);
// Give context-specific guidance
const onlyOpenRouter = missing.length === 1 && missing[0] === "OPENROUTER_API_KEY";
if (onlyOpenRouter) {
p.log.info(`The script will open your browser to authenticate with OpenRouter.`);
} else {
p.log.info(`Run ${pc.cyan(`spawn ${cloud}`)} for setup instructions.`);
}
p.log.info(getCredentialGuidance(cloud, onlyOpenRouter));
if (isInteractiveTTY()) {
const confirmMsg = onlyOpenRouter
? "Continue? You'll authenticate via browser."
: "Continue anyway? The script will prompt for missing credentials.";
const shouldContinue = await p.confirm({
message: confirmMsg,
initialValue: true,
});
if (p.isCancel(shouldContinue) || !shouldContinue) {
const shouldContinue = await confirmContinueWithMissingCreds(onlyOpenRouter);
if (!shouldContinue) {
handleCancel();
}
}