Merge pull request #3740 from fbricon/start-ollama-chat

Intercept Ollama errors before Webview, to display Download/Start options
This commit is contained in:
Nate Sesti 2025-02-05 13:08:27 -08:00 • committed by GitHub
commit 1cbc0f0b4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 8 deletions

View file

@ -11,7 +11,7 @@ export async function isOllamaInstalled(): Promise<boolean> {
});
}
export async function startLocalOllama(ide: IDE): Promise<void> {
export async function startLocalOllama(ide: IDE): Promise<any> {
let startCommand: string | undefined;
switch (process.platform) {
@ -20,16 +20,16 @@ export async function startLocalOllama(ide: IDE): Promise<void> {
break;
case "win32"://Windows
startCommand = `& "ollama app.exe"\n`;
startCommand = "& \"ollama app.exe\"\n";
break;
default: //Linux...
const start_script_path = path.resolve(__dirname, './start_ollama.sh');
const start_script_path = path.resolve(__dirname, "./start_ollama.sh");
if (await ide.fileExists(`file:/${start_script_path}`)) {
startCommand = `set -e && chmod +x ${start_script_path} && ${start_script_path}\n`;
console.log(`Ollama Linux startup script at : ${start_script_path}`);
} else {
ide.showToast("error", `Cannot start Ollama: could not find ${start_script_path}!`)
return ide.showToast("error", `Cannot start Ollama: could not find ${start_script_path}!`)
}
}
if (startCommand) {

View file

@ -44,11 +44,11 @@ export class ContinueCompletionProvider
implements vscode.InlineCompletionItemProvider
{
private onError(e: any) {
const options = ["Documentation"];
let options = ["Documentation"];
if (e.message.includes("Ollama may not be installed")) {
options.push("Download Ollama");
} else if (e.message.includes("Ollama may not be running")) {
options.unshift("Start Ollama"); // We want "Start" to be the default choice
options = ["Start Ollama"]; // We want "Start" to be the only choice
}
if (e.message.includes("Please sign in with GitHub")) {
@ -70,7 +70,7 @@ export class ContinueCompletionProvider
);
} else if (val === "Download Ollama") {
vscode.env.openExternal(vscode.Uri.parse("https://ollama.ai/download"));
} else if (val == "Start Ollama") {
} else if (val === "Start Ollama") {
startLocalOllama(this.ide);
}
});

View file

@ -34,6 +34,7 @@ import { VsCodeIde } from "./VsCodeIde";
import { getMetaKeyLabel } from "./util/util";
import type { VsCodeWebviewProtocol } from "./webviewProtocol";
import { startLocalOllama } from "core/util/ollamaHelper";
let fullScreenPanel: vscode.WebviewPanel | undefined;
@ -1002,6 +1003,9 @@ const getCommandsMap: (
"continue.openAccountDialog": () => {
sidebar.webviewProtocol?.request("openDialogMessage", "account");
},
"continue.startLocalOllama": () => {
startLocalOllama(ide);
},
};
};

View file

@ -85,6 +85,30 @@ export class VsCodeWebviewProtocol
respond({ done: true, content: response, status: "success" });
}
} catch (e: any) {
let message = e.message;
//Intercept Ollama errors for special handling
if (message.includes("Ollama may not")) {
const options = [];
if (message.includes("be installed")) {
options.push("Download Ollama");
} else if (message.includes("be running")) {
options.push("Start Ollama");
}
if (options.length > 0) {
// Respond without an error, so the UI doesn't show the error component
respond({ done: true, status: "error" });
// Show native vscode error message instead, with options to download/start Ollama
vscode.window.showErrorMessage(e.message, ...options).then(async (val) => {
if (val === "Download Ollama") {
vscode.env.openExternal(vscode.Uri.parse("https://ollama.ai/download"));
} else if (val === "Start Ollama") {
vscode.commands.executeCommand("continue.startLocalOllama");
}
});
return;
}
}
respond({ done: true, error: e.message, status: "error" });
const stringified = JSON.stringify({ msg }, null, 2);
@ -99,7 +123,6 @@ export class VsCodeWebviewProtocol
return;
}
let message = e.message;
if (e.cause) {
if (e.cause.name === "ConnectTimeoutError") {
message = `Connection timed out. If you expect it to take a long time to connect, you can increase the timeout in config.json by setting "requestOptions": { "timeout": 10000 }. You can find the full config reference here: https://docs.continue.dev/reference/config`;