[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
This commit is contained in:
Antoine Guenet
2023-11-02 16:57:48 +00:00
parent cbac216157
commit 752624a2f2
2 changed files with 28 additions and 19 deletions
@@ -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(/.*<generated_text>/, '').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(/.*<generated_text>/, '').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) {
@@ -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 });