Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercept Ollama errors before Webview, to display Download/Start options #3740

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/util/ollamaHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions extensions/vscode/src/autocomplete/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand All @@ -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);
}
});
Expand Down
4 changes: 4 additions & 0 deletions extensions/vscode/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Battery } from "./util/battery";
import { VsCodeIde } from "./VsCodeIde";

import type { VsCodeWebviewProtocol } from "./webviewProtocol";
import { startLocalOllama } from "core/util/ollamaHelper";

let fullScreenPanel: vscode.WebviewPanel | undefined;

Expand Down Expand Up @@ -1000,6 +1001,9 @@ const getCommandsMap: (
"continue.openAccountDialog": () => {
sidebar.webviewProtocol?.request("openDialogMessage", "account");
},
"continue.startLocalOllama": () => {
startLocalOllama(ide);
},
};
};

Expand Down
25 changes: 24 additions & 1 deletion extensions/vscode/src/webviewProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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);
Expand All @@ -96,7 +120,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`;
Expand Down
Loading