From 752624a2f2e54c9467dcd267609ca66b569a3c94 Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Thu, 26 Oct 2023 14:27:09 +0200 Subject: [PATCH] [FIX] web_editor: abort old ChatGpt alternatives batch on select mode This implements a way to abort the generation of a batch of alternatives by the AI Copywriter. This is needed because if a batch was being generated when the user clicked one of the buttons to select a mode, it continued to be generated and to update the state while the new batch was in the making, resulting in a potential total of more alternatives than requested. task-3572397 Part-of: odoo/odoo#139857 --- .../widgets/chatgpt_alternatives_dialog.js | 45 +++++++++++-------- .../wysiwyg/widgets/chatgpt_prompt_dialog.js | 2 +- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/addons/web_editor/static/src/js/wysiwyg/widgets/chatgpt_alternatives_dialog.js b/addons/web_editor/static/src/js/wysiwyg/widgets/chatgpt_alternatives_dialog.js index d1a10a238ce..6472d71fea0 100644 --- a/addons/web_editor/static/src/js/wysiwyg/widgets/chatgpt_alternatives_dialog.js +++ b/addons/web_editor/static/src/js/wysiwyg/widgets/chatgpt_alternatives_dialog.js @@ -40,7 +40,9 @@ export class ChatGPTAlternativesDialog extends ChatGPTDialog { }], messages: [], alternativesMode: '', + currentBatchId: null, }); + this._generationIndex = 0; this._generateAlternatives(); } @@ -59,34 +61,41 @@ export class ChatGPTAlternativesDialog extends ChatGPTDialog { //-------------------------------------------------------------------------- async _generateAlternatives() { + const batchId = new Date().getTime(); + this.state.currentBatchId = batchId; let wasError = false; let messageIndex = 0; - while (!wasError && messageIndex < this.props.numberOfAlternatives) { + while (!wasError && messageIndex < this.props.numberOfAlternatives && this.state.currentBatchId === batchId) { + this._generationIndex += 1; let query = messageIndex ? 'Write one alternative version of the original text.' : 'Try again another single version of the original text.'; if (this.state.alternativesMode && !messageIndex) { query += ` Make it more ${this.state.alternativesMode} than your last answer.`; } await this._generate(query, (content, isError) => { - const alternative = content.replace(/.*/, '').replace(/<\/generated_text>.*/, ''); - if (isError) { - wasError = true; - } else { - this.state.conversationHistory.push({ - role: 'user', - content: query, - }, { - role: 'assistant', - content, + if (this.state.currentBatchId === batchId) { + const alternative = content.replace(/.*/, '').replace(/<\/generated_text>.*/, ''); + if (isError) { + wasError = true; + } else { + this.state.conversationHistory.push({ + role: 'user', + content: query, + }, { + role: 'assistant', + content, + }); + } + this.state.messages.push({ + author: 'assistant', + text: alternative, + isError, }); } - this.state.messages.push({ - author: 'assistant', - text: alternative, - isError, - }); }).catch(() => { - wasError = true; - this.state.messages = []; + if (this.state.currentBatchId === batchId) { + wasError = true; + this.state.messages = []; + } }); messageIndex += 1; if (wasError) { diff --git a/addons/web_editor/static/src/js/wysiwyg/widgets/chatgpt_prompt_dialog.js b/addons/web_editor/static/src/js/wysiwyg/widgets/chatgpt_prompt_dialog.js index 5c0ba12f522..c14058f979c 100644 --- a/addons/web_editor/static/src/js/wysiwyg/widgets/chatgpt_prompt_dialog.js +++ b/addons/web_editor/static/src/js/wysiwyg/widgets/chatgpt_prompt_dialog.js @@ -55,7 +55,7 @@ export class ChatGPTPromptDialog extends ChatGPTDialog { ev.preventDefault(); const prompt = this.state.prompt; this.state.messages.push({ author: 'user', text: prompt }); - const datetime = new Date(); + const datetime = new Date().getTime(); const conversation = { role: 'user', content: prompt }; this.state.conversationHistory.push(conversation); this.state.messages.push({ author: 'assistant', datetime });