| 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 only handles Ollama-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 | if (!error.message.toLowerCase().includes("ollama")) { |
| 14 | return false; |
| 15 | } |
| 16 | let message: string = error.message; |
| 17 | let options: string[] | undefined; |
| 18 | let modelName = undefined; |
| 19 | if (message.includes("Ollama may not be installed")) { |
| 20 | options = ["Download Ollama"]; |
| 21 | } else if (message.includes("Ollama may not be running")) { |
| 22 | options = ["Start Ollama"]; // We want "Start" to be the only choice |
| 23 | } else if (message.includes("ollama run") && "llm" in error) { |
| 24 | //extract model name from error message matching the pattern "ollama run <model-name>" |
| 25 | modelName = message.match(/`ollama run (.*)`/)?.[1]; |
| 26 | const llm = error.llm as ILLM; |
| 27 | if (isModelInstaller(llm) && (await llm.isInstallingModel(modelName!))) { |
| 28 | console.log(`${llm.providerName} already installing ${modelName}`); |
| 29 | return false; |
| 30 | } |
| 31 | message = `Model "${modelName}" is not found in Ollama. You need to install it.`; |
| 32 | options = [`Install Model`]; |
| 33 | } |
| 34 | if (options === undefined) { |
| 35 | console.log("Found an unhandled Ollama error: ", message); |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | vscode.window.showErrorMessage(message, ...options).then((val) => { |
| 40 | if (val === "Download Ollama") { |
| 41 | vscode.env.openExternal(vscode.Uri.parse("https://ollama.ai/download")); |
| 42 | } else if (val === "Start Ollama") { |
| 43 | vscode.commands.executeCommand("continue.startLocalOllama"); |
| 44 | } else if (val === "Install Model" && "llm" in error) { |
| 45 | //Eventually, we might be able to support installing models for other LLM providers than Ollama |
| 46 | vscode.commands.executeCommand( |
| 47 | "continue.installModel", |
| 48 | modelName, |
| 49 | error.llm, |
| 50 | ); |
| 51 | } |
| 52 | }); |
| 53 | return true; |
| 54 | } |