Skip to content

Commit

Permalink
feat: improve error handling and remove unnecessary console.log state…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
Iamshankhadeep committed Mar 26, 2024
1 parent 71de336 commit d9a6691
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
7 changes: 2 additions & 5 deletions extensions/vscode/src/webviewProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ export class VsCodeWebviewProtocol {
) {
this.on("abort", (msg) => {
this.abortedMessageIds.add(msg.messageId);
const controller = this.abortControllerMap.get(msg.messageId);
console.log("Aborting message", msg.messageId, controller);
controller?.abort();
});
this.on("showFile", (msg) => {
this.ide.openFile(msg.data.filepath);
Expand Down Expand Up @@ -412,8 +409,8 @@ export class VsCodeWebviewProtocol {
config,
signal,
})) {
if (signal.aborted) {
console.log("Aborted command/run");
if (protocol.abortedMessageIds.has(msg.messageId)) {
controller.abort();
break;
}
if (content) {
Expand Down
23 changes: 14 additions & 9 deletions gui/src/util/ide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function _postToIde(messageType: string, data: any, messageId?: string) {
console.log(
"Unable to send message: postIntellijMessage is undefined. ",
messageType,
data,
data
);
throw new Error("postIntellijMessage is undefined");
}
Expand All @@ -30,7 +30,7 @@ function _postToIde(messageType: string, data: any, messageId?: string) {
console.log(
"Unable to send message: vscode is undefined. ",
messageType,
data,
data
);
return;
}
Expand All @@ -47,7 +47,7 @@ export function postToIde<T extends keyof WebviewProtocol>(
messageType: T,
data: WebviewProtocol[T][0],
messageId?: string,
attempt: number = 0,
attempt: number = 0
) {
try {
_postToIde(messageType, data, messageId);
Expand All @@ -56,7 +56,7 @@ export function postToIde<T extends keyof WebviewProtocol>(
console.log(`Attempt ${attempt} failed. Retrying...`);
setTimeout(
() => postToIde(messageType, data, messageId, attempt + 1),
Math.pow(2, attempt) * 1000,
Math.pow(2, attempt) * 1000
);
} else {
console.error("Max attempts reached. Message could not be sent.", error);
Expand All @@ -67,7 +67,7 @@ export function postToIde<T extends keyof WebviewProtocol>(
export function respondToIde<T extends keyof ReverseWebviewProtocol>(
messageType: T,
data: ReverseWebviewProtocol[T][1],
messageId: string,
messageId: string
) {
_postToIde(messageType, data, messageId);
}
Expand All @@ -82,7 +82,7 @@ function safeParseResponse(data: any) {

export async function ideRequest<T extends keyof WebviewProtocol>(
messageType: T,
data: WebviewProtocol[T][0],
data: WebviewProtocol[T][0]
): Promise<WebviewProtocol[T][1]> {
const messageId = uuidv4();

Expand All @@ -102,7 +102,7 @@ export async function ideRequest<T extends keyof WebviewProtocol>(
export async function* ideStreamRequest<T extends keyof WebviewProtocol>(
messageType: T,
data: WebviewProtocol[T][0],
cancelToken?: AbortSignal,
cancelToken?: AbortSignal
): WebviewProtocol[T][1] {
const messageId = uuidv4();

Expand All @@ -114,6 +114,10 @@ export async function* ideStreamRequest<T extends keyof WebviewProtocol>(
let returnVal = undefined;

const handler = (event: { data: Message }) => {
// console.log("Received message", event.data);
if (event.data.messageType === "setInactive") {
postToIde("abort", undefined, messageId);
}
if (event.data.messageId === messageId) {
const responseData = safeParseResponse(event.data.data);
if (responseData.done) {
Expand Down Expand Up @@ -145,6 +149,7 @@ export async function* ideStreamRequest<T extends keyof WebviewProtocol>(
index = buffer.length;
yield chunk;
}
console.log("Stream complete", returnVal);

return returnVal;
}
Expand All @@ -153,7 +158,7 @@ export async function* llmStreamChat(
modelTitle: string,
cancelToken: AbortSignal | undefined,
messages: ChatMessage[],
options: LLMFullCompletionOptions = {},
options: LLMFullCompletionOptions = {}
): AsyncGenerator<ChatMessage, LLMReturnValue> {
const gen = ideStreamRequest(
"llm/streamChat",
Expand All @@ -162,7 +167,7 @@ export async function* llmStreamChat(
title: modelTitle,
completionOptions: options,
},
cancelToken,
cancelToken
);

let next = await gen.next();
Expand Down

0 comments on commit d9a6691

Please sign in to comment.