Skip to content

Commit

Permalink
fix: previous response is shown after error
Browse files Browse the repository at this point in the history
  • Loading branch information
timkmecl committed Apr 11, 2023
1 parent 3f0f3c3 commit 5984975
Show file tree
Hide file tree
Showing 4 changed files with 2,220 additions and 2,287 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "chatgpt",
"displayName": "ChatGPT: write and improve code using AI",
"description": "Use ChatGPT and GPT4 right inside the IDE to enhance and automate your coding with AI-powered assistance (unofficial)",
"version": "1.1.0",
"version": "1.1.1",
"publisher": "timkmecl",
"icon": "resources/extensionIcon.png",
"license": "MIT",
Expand Down
20 changes: 15 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ class ChatGPTViewProvider implements vscode.WebviewViewProvider {
searchPrompt = prompt;
}
this._fullPrompt = searchPrompt;

// Increment the message number
this._currentMessageNumber++;
let currentMessageNumber = this._currentMessageNumber;

if (!this._chatGPTAPI) {
response = '[ERROR] "API key not set or wrong, please go to extension settings to set it (read README.md for more info)"';
Expand All @@ -267,12 +271,8 @@ class ChatGPTViewProvider implements vscode.WebviewViewProvider {
this._view?.webview.postMessage({ type: 'setPrompt', value: this._prompt });
this._view?.webview.postMessage({ type: 'addResponse', value: '...' });

// Increment the message number
this._currentMessageNumber++;

const agent = this._chatGPTAPI;

let currentMessageNumber = this._currentMessageNumber;
try {
// Send the search prompt to the ChatGPTAPI instance and store the response
const res = await agent.sendMessage(searchPrompt, {
Expand All @@ -284,7 +284,8 @@ class ChatGPTViewProvider implements vscode.WebviewViewProvider {
console.log("onProgress");
if (this._view && this._view.visible) {
response = partialResponse.text;
this._view.webview.postMessage({ type: 'addResponse', value: partialResponse.text });
this._response = response;
this._view.webview.postMessage({ type: 'addResponse', value: response });
}
},
timeoutMs: (this._settings.timeoutLength || 60) * 1000,
Expand All @@ -295,20 +296,29 @@ class ChatGPTViewProvider implements vscode.WebviewViewProvider {
return;
}


console.log(response);

response = res.text;
if (this._settings.keepConversation){
this._conversation = {
parentMessageId: res.id
};
}

} catch (e:any) {
console.error(e);
if (this._currentMessageNumber === currentMessageNumber){
response = this._response;
response += `\n\n---\n[ERROR] ${e}`;
}
}
}

if (this._currentMessageNumber !== currentMessageNumber) {
return;
}

// Saves the response
this._response = response;

Expand Down
Loading

0 comments on commit 5984975

Please sign in to comment.