From 109bf2d8f5609992a0bd40aeefbb70bfb61f532a Mon Sep 17 00:00:00 2001 From: nang-dev Date: Tue, 22 Oct 2024 00:20:44 -0700 Subject: [PATCH] Added command retries --- core/llm/llms/Aider.ts | 113 ++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 46 deletions(-) diff --git a/core/llm/llms/Aider.ts b/core/llm/llms/Aider.ts index 84903e52ed..9233a3e90b 100644 --- a/core/llm/llms/Aider.ts +++ b/core/llm/llms/Aider.ts @@ -158,56 +158,75 @@ public async aiderResetSession(model: string, apiKey: string | undefined): Promi let command: string[]; - switch (model) { - case "claude-3-5-sonnet-20240620": - console.log("claude model chosen"); - command = ["aider --model claude-3-5-sonnet-20240620"]; - break; - case "gpt-4o": - command = ["aider --model gpt-4o"]; - break; - case "pearai_model": - default: - await this.credentials.checkAndUpdateCredentials(); - const accessToken = this.credentials.getAccessToken(); - if (!accessToken) { - let message = "PearAI token invalid. Please try logging in or contact PearAI support." - vscode.window - .showErrorMessage( - message, - 'Login To PearAI', - 'Show Logs', - ) - .then((selection: any) => { - if (selection === 'Login To PearAI') { - // Redirect to auth login URL - vscode.env.openExternal( - vscode.Uri.parse( - 'https://trypear.ai/signin?callback=pearai://pearai.pearai/auth', - ), - ); - } else if (selection === 'Show Logs') { - vscode.commands.executeCommand( - 'workbench.action.toggleDevTools', - ); + const aiderFlags = "--no-pretty --yes-always --no-auto-commits"; + const aiderCommands = [ + `python -m aider ${aiderFlags}`, + `python3 -m aider ${aiderFlags}`, + `aider ${aiderFlags}` + ]; + let commandFound = false; + + for (const aiderCommand of aiderCommands) { + try { + await execSync(`${aiderCommand} --version`, { stdio: 'ignore' }); + commandFound = true; + + switch (model) { + case "claude-3-5-sonnet-20240620": + console.log("claude model chosen"); + command = [`${aiderCommand} --model claude-3-5-sonnet-20240620`]; + break; + case "gpt-4o": + command = [`${aiderCommand} --model gpt-4o`]; + break; + case "pearai_model": + default: + await this.credentials.checkAndUpdateCredentials(); + const accessToken = this.credentials.getAccessToken(); + if (!accessToken) { + let message = "PearAI token invalid. Please try logging in or contact PearAI support." + vscode.window + .showErrorMessage( + message, + 'Login To PearAI', + 'Show Logs', + ) + .then((selection: any) => { + if (selection === 'Login To PearAI') { + // Redirect to auth login URL + vscode.env.openExternal( + vscode.Uri.parse( + 'https://trypear.ai/signin?callback=pearai://pearai.pearai/auth', + ), + ); + } else if (selection === 'Show Logs') { + vscode.commands.executeCommand( + 'workbench.action.toggleDevTools', + ); + } + }); + throw new Error("User not logged in to PearAI."); } - }); - throw new Error("User not logged in to PearAI."); + command = [ + aiderCommand, + "--openai-api-key", + accessToken, + "--openai-api-base", + `${SERVER_URL}/integrations/aider`, + ]; + break; } - command = [ - "aider", - "--openai-api-key", - accessToken, - "--openai-api-base", - `${SERVER_URL}/integrations/aider`, - ]; - break; + break; // Exit the loop if a working command is found + } catch (error) { + console.log(`Command ${aiderCommand} not found or errored. Trying next...`); + } } - // disable pretty printing - command.push("--no-pretty"); - command.push("--yes-always"); - command.push("--no-auto-commits"); + if (!commandFound) { + throw new Error("Aider command not found. Please ensure it's installed correctly."); + } + + const userPath = this.getUserPath(); const userShell = this.getUserShell(); @@ -254,6 +273,8 @@ public async aiderResetSession(model: string, apiKey: string | undefined): Promi env: { ...process.env, PATH: userPath, + PYTHONIOENCODING: "utf-8", + AIDER_SIMPLE_OUTPUT: "1", }, windowsHide: true, });