continue-custom/extensions/vscode/src/webviewProtocol.ts
Fred Bricon d9c3d447b9 Refactor Ollama error management
Signed-off-by: Fred Bricon <fbricon@gmail.com>
2025-03-06 18:25:34 +01:00

210 lines
7.1 KiB
TypeScript

import { FromWebviewProtocol, ToWebviewProtocol } from "core/protocol";
import { Message } from "core/protocol/messenger";
import { extractMinimalStackTraceInfo } from "core/util/extractMinimalStackTraceInfo";
import { Telemetry } from "core/util/posthog";
import { v4 as uuidv4 } from "uuid";
import * as vscode from "vscode";
import { IMessenger } from "../../../core/protocol/messenger";
import { handleLLMError } from "./util/errorHandling";
import { showFreeTrialLoginMessage } from "./util/messages";
export class VsCodeWebviewProtocol
implements IMessenger<FromWebviewProtocol, ToWebviewProtocol>
{
listeners = new Map<
keyof FromWebviewProtocol,
((message: Message) => any)[]
>();
send(messageType: string, data: any, messageId?: string): string {
const id = messageId ?? uuidv4();
this.webview?.postMessage({
messageType,
data,
messageId: id,
});
return id;
}
on<T extends keyof FromWebviewProtocol>(
messageType: T,
handler: (
message: Message<FromWebviewProtocol[T][0]>,
) => Promise<FromWebviewProtocol[T][1]> | FromWebviewProtocol[T][1],
): void {
if (!this.listeners.has(messageType)) {
this.listeners.set(messageType, []);
}
this.listeners.get(messageType)?.push(handler);
}
_webview?: vscode.Webview;
_webviewListener?: vscode.Disposable;
get webview(): vscode.Webview | undefined {
return this._webview;
}
set webview(webView: vscode.Webview) {
this._webview = webView;
this._webviewListener?.dispose();
const handleMessage = async (msg: Message): Promise<void> => {
if (!("messageType" in msg) || !("messageId" in msg)) {
throw new Error(`Invalid webview protocol msg: ${JSON.stringify(msg)}`);
}
const respond = (message: any) =>
this.send(msg.messageType, message, msg.messageId);
const handlers =
this.listeners.get(msg.messageType as keyof FromWebviewProtocol) || [];
for (const handler of handlers) {
try {
const response = await handler(msg);
// For generator types e.g. llm/streamChat
if (
response &&
typeof response[Symbol.asyncIterator] === "function"
) {
let next = await response.next();
while (!next.done) {
respond({
done: false,
content: next.value,
status: "success",
});
next = await response.next();
}
respond({
done: true,
content: next.value,
status: "success",
});
} else {
respond({ done: true, content: response, status: "success" });
}
} catch (e: any) {
if (handleLLMError(e)) {
// Respond without an error, so the UI doesn't show the error component
respond({ done: true, status: "error" });
}
let message = e.message;
respond({ done: true, error: message, status: "error" });
const stringified = JSON.stringify({ msg }, null, 2);
console.error(
`Error handling webview message: ${stringified}\n\n${e}`,
);
if (
stringified.includes("llm/streamChat") ||
stringified.includes("chatDescriber/describe")
) {
return;
}
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`;
} else if (e.cause.code === "ECONNREFUSED") {
message = `Connection was refused. This likely means that there is no server running at the specified URL. If you are running your own server you may need to set the "apiBase" parameter in config.json. For example, you can set up an OpenAI-compatible server like here: https://docs.continue.dev/reference/Model%20Providers/openai#openai-compatible-servers--apis`;
} else {
message = `The request failed with "${e.cause.name}": ${e.cause.message}. If you're having trouble setting up Continue, please see the troubleshooting guide for help.`;
}
}
if (message.includes("https://proxy-server")) {
message = message.split("\n").filter((l: string) => l !== "")[1];
try {
message = JSON.parse(message).message;
} catch {}
if (message.includes("exceeded")) {
message +=
" To keep using Continue, you can set up a local model or use your own API key.";
}
vscode.window
.showInformationMessage(message, "Add API Key", "Use Local Model")
.then((selection) => {
if (selection === "Add API Key") {
this.request("addApiKey", undefined);
} else if (selection === "Use Local Model") {
this.request("setupLocalConfig", undefined);
}
});
} else if (message.includes("Please sign in with GitHub")) {
showFreeTrialLoginMessage(message, this.reloadConfig, () =>
this.request("openOnboardingCard", undefined),
);
} else {
Telemetry.capture(
"webview_protocol_error",
{
messageType: msg.messageType,
errorMsg: message.split("\n\n")[0],
stack: extractMinimalStackTraceInfo(e.stack),
},
false,
);
}
}
}
};
this._webviewListener = this._webview.onDidReceiveMessage(handleMessage);
}
constructor(private readonly reloadConfig: () => void) {}
invoke<T extends keyof FromWebviewProtocol>(
messageType: T,
data: FromWebviewProtocol[T][0],
messageId?: string,
): FromWebviewProtocol[T][1] {
throw new Error("Method not implemented.");
}
onError(handler: (message: Message, error: Error) => void): void {
throw new Error("Method not implemented.");
}
public request<T extends keyof ToWebviewProtocol>(
messageType: T,
data: ToWebviewProtocol[T][0],
retry: boolean = true,
): Promise<ToWebviewProtocol[T][1]> {
const messageId = uuidv4();
return new Promise(async (resolve) => {
if (retry) {
let i = 0;
while (!this.webview) {
if (i >= 10) {
resolve(undefined);
return;
} else {
await new Promise((res) => setTimeout(res, i >= 5 ? 1000 : 500));
i++;
}
}
}
this.send(messageType, data, messageId);
if (this.webview) {
const disposable = this.webview.onDidReceiveMessage(
(msg: Message<ToWebviewProtocol[T][1]>) => {
if (msg.messageId === messageId) {
resolve(msg.data);
disposable?.dispose();
}
},
);
} else if (!retry) {
resolve(undefined);
}
});
}
}