| 1 | import { ILLM } from "core"; |
| 2 | import { isModelInstaller } from "core/llm"; |
| 3 | import * as vscode from "vscode"; |
| 4 | |
| 5 | /** |
| 6 | * @param error Handles common LLM errors. Currently handles Ollama and Lemonade-related errors. |
| 7 | * @returns true if error is handled, false otherwise |
| 8 | */ |
| 9 | export async function handleLLMError(error: unknown): Promise<boolean> { |
| 10 | if (!error || !(error instanceof Error) || !error.message) { |
| 11 | return false; |
| 12 | } |
| 13 | |
| 14 | // Handle Lemonade errors |
| 15 | if (error.message.toLowerCase().includes("lemonade")) { |
| 16 | let message: string = error.message; |
| 17 | let options: string[] | undefined; |
| 18 | |
| 19 | // For Windows, offer to start Lemonade if it's installed but not running |
| 20 | if ( |
| 21 | process.platform === "win32" && |
| 22 | message.includes("Lemonade server may not be running") |
| 23 | ) { |
| 24 | options = ["Start Lemonade", "Setup Instructions"]; |
| 25 | } else { |
| 26 | // For all other cases (Linux, not installed, etc.), direct to setup instructions |
| 27 | options = ["Setup Instructions"]; |
| 28 | } |
| 29 | |
| 30 | vscode.window.showErrorMessage(message, ...options).then((val) => { |
| 31 | if (val === "Setup Instructions") { |
| 32 | vscode.env.openExternal(vscode.Uri.parse("https://lemonade-server.ai")); |
| 33 | } else if (val === "Start Lemonade") { |
| 34 | vscode.commands.executeCommand("continue.startLocalLemonade"); |
| 35 | } |
| 36 | }); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | // Handle Ollama errors |
| 41 | if (!error.message.toLowerCase().includes("ollama")) { |
| 42 | return false; |
| 43 | } |
| 44 | let message: string = error.message; |
| 45 | let options: string[] | undefined; |
| 46 | let modelName = undefined; |
| 47 | if (message.includes("Ollama may not be installed")) { |
| 48 | options = ["Download Ollama"]; |
| 49 | } else if (message.includes("Ollama may not be running")) { |
| 50 | options = ["Start Ollama"]; // We want "Start" to be the only choice |
| 51 | } else if (message.includes("ollama run") && "llm" in error) { |
| 52 | //extract model name from error message matching the pattern "ollama run <model-name>" |
| 53 | modelName = message.match(/`ollama run (.*)`/)?.[1]; |
| 54 | const llm = error.llm as ILLM; |
| 55 | if (isModelInstaller(llm) && (await llm.isInstallingModel(modelName!))) { |
| 56 | console.log(`${llm.providerName} already installing ${modelName}`); |
| 57 | return false; |
| 58 | } |
| 59 | message = `Model "${modelName}" is not found in Ollama. You need to install it.`; |
| 60 | options = [`Install Model`]; |
| 61 | } |
| 62 | if (options === undefined) { |
| 63 | console.log("Found an unhandled Ollama error: ", message); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | vscode.window.showErrorMessage(message, ...options).then((val) => { |
| 68 | if (val === "Download Ollama") { |
| 69 | vscode.env.openExternal(vscode.Uri.parse("https://ollama.ai/download")); |
| 70 | } else if (val === "Start Ollama") { |
| 71 | vscode.commands.executeCommand("continue.startLocalOllama"); |
| 72 | } else if (val === "Install Model" && "llm" in error) { |
| 73 | //Eventually, we might be able to support installing models for other LLM providers than Ollama |
| 74 | vscode.commands.executeCommand( |
| 75 | "continue.installModel", |
| 76 | modelName, |
| 77 | error.llm, |
| 78 | ); |
| 79 | } |
| 80 | }); |
| 81 | return true; |
| 82 | } |