diff --git a/sdkjs-plugins/content/ai/scripts/code.js b/sdkjs-plugins/content/ai/scripts/code.js index 69081385..2a9195ac 100644 --- a/sdkjs-plugins/content/ai/scripts/code.js +++ b/sdkjs-plugins/content/ai/scripts/code.js @@ -1004,6 +1004,7 @@ function customAssistantWindowShow(assistantId, buttonAssistant) if (!element) return; if (buttonAssistant) { buttonAssistant.text = element.name; + customAssistantManager.updateAssistant(element); } else { buttonAssistant = new Asc.ButtonToolbar(null); buttonAssistant.text = element.name; @@ -1021,11 +1022,11 @@ function customAssistantWindowShow(assistantId, buttonAssistant) onclick: () => customAssistantWindowDeleteConfirm(element.id, buttonAssistant) }]; buttonAssistant.attachOnClick(async function(){ - onStartCustomAssistant(element.id, buttonAssistant); + customAssistantOnClickToolbarIcon(element.id, buttonAssistant); }); + customAssistantManager.createAssistant(element); } Asc.Buttons.updateToolbarMenu(window.buttonMainToolbar.id, window.buttonMainToolbar.name, [buttonAssistant]); - customAssistantManager.createAssistant(element); } customAssistantWindowClose(); } else { @@ -1161,7 +1162,7 @@ function customAssistantWarning(warningText, assistantData) { * @param {Asc.ButtonToolbar} buttonAssistant * @returns */ -async function onStartCustomAssistant(assistantId, buttonAssistant) +async function customAssistantOnClickToolbarIcon(assistantId, buttonAssistant) { const isAssistantRunning = customAssistantManager.checkNeedToRunAssistant(assistantId); if (isAssistantRunning) { diff --git a/sdkjs-plugins/content/ai/scripts/engine/register.js b/sdkjs-plugins/content/ai/scripts/engine/register.js index 286da822..65358fae 100644 --- a/sdkjs-plugins/content/ai/scripts/engine/register.js +++ b/sdkjs-plugins/content/ai/scripts/engine/register.js @@ -698,7 +698,7 @@ async function registerButtons(window, undefined) onclick: () => customAssistantWindowDeleteConfirm(element.id, buttonAssistant) }]; buttonAssistant.attachOnClick(async function(){ - onStartCustomAssistant(element.id, buttonAssistant); + customAssistantOnClickToolbarIcon(element.id, buttonAssistant); }); }); } diff --git a/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/annotation-popup.js b/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/annotation-popup.js index bf20a050..d0ef916f 100644 --- a/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/annotation-popup.js +++ b/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/annotation-popup.js @@ -137,6 +137,7 @@ function CustomAnnotationPopup() let textColor = window.Asc.plugin.theme ? window.Asc.plugin.theme["text-normal"] : "#3D3D3D"; let borderColor = window.Asc.plugin.theme ? window.Asc.plugin.theme["border-divider"] : "#666666"; let ballonColor = window.Asc.plugin.theme ? window.Asc.plugin.theme["canvas-background"] : "#F5F5F5"; + this.content = ""; if (data.type === 0) { // Hint this.content = `
diff --git a/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/custom-annotator.js b/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/custom-annotator.js index 3f7a5362..33c86940 100644 --- a/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/custom-annotator.js +++ b/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/custom-annotator.js @@ -42,6 +42,8 @@ function CustomAnnotator(annotationPopup, assistantData) { this.assistantData = assistantData; this.type = assistantData.type; this._skipNextChangeParagraph = false; + + this._lastUsedPrompt = ""; } CustomAnnotator.prototype = Object.create(TextAnnotator.prototype); CustomAnnotator.prototype.constructor = CustomAnnotator; @@ -60,9 +62,20 @@ Object.assign(CustomAnnotator.prototype, { const argPrompt = this._createPrompt(text); + if (this._lastUsedPrompt && argPrompt !== this._lastUsedPrompt) { + let resetInstruction = + `CRITICAL + - Ignore all previous messages and instructions. + - Please respond only to this new query and treat this as a new request. + + `; + argPrompt = resetInstruction + argPrompt; + this._lastUsedPrompt = argPrompt; + } + let response = await this.chatRequest(argPrompt); - if (!response || response === '[]') { + if (!response || response === "[]") { if (response === null) { return null; // no AI model selected } @@ -70,7 +83,11 @@ Object.assign(CustomAnnotator.prototype, { } try { - const ranges = this._convertToRanges(paraId, text, JSON.parse(response)); + const ranges = this._convertToRanges( + paraId, + text, + JSON.parse(response), + ); let obj = { type: "highlightText", paragraphId: paraId, @@ -124,7 +141,10 @@ Object.assign(CustomAnnotator.prototype, { this._skipNextChangeParagraph = false; return paraIds.map(() => false); } - return await TextAnnotator.prototype.checkParagraphs.call(this, paraIds); + return await TextAnnotator.prototype.checkParagraphs.call( + this, + paraIds, + ); }, onAccept: async function (paraId, rangeId) { this._skipNextChangeParagraph = true; diff --git a/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/manager.js b/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/manager.js index 20807af3..cbe25a5e 100644 --- a/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/manager.js +++ b/sdkjs-plugins/content/ai/scripts/text-annotations/custom-annotations/manager.js @@ -63,11 +63,9 @@ class CustomAssistantManager { /** * @param {localStorageCustomAssistantItem} assistantData + * @param {boolean} [isForUpdate] */ - createAssistant(assistantData) { - if (this._customAssistants.has(assistantData.id)) { - return this._updateAssistant(assistantData); - } + createAssistant(assistantData, isForUpdate) { /** @type {Assistant | null} */ let assistant = null; switch (assistantData.type) { @@ -96,40 +94,42 @@ class CustomAssistantManager { ); } - this._isCustomAssistantInit.set(assistantData.id, false); - this._isCustomAssistantRunning.set(assistantData.id, false); this._customAssistants.set(assistantData.id, assistant); + if (!isForUpdate) { + this._isCustomAssistantInit.set(assistantData.id, false); + this._isCustomAssistantRunning.set(assistantData.id, false); + } return assistant; } /** * @param {localStorageCustomAssistantItem} assistantData */ - _updateAssistant(assistantData) { - let assistant = this._customAssistants.get(assistantData.id); - if (!assistant) { + updateAssistant(assistantData) { + console.warn("Updating custom assistant: " + assistantData.id); + let oldAssistant = this._customAssistants.get(assistantData.id); + if (!oldAssistant) { throw new Error("Custom assistant not found: " + assistantData.id); } - assistant.assistantData = assistantData; - assistant.type = assistantData.type; - const isRunning = this._isCustomAssistantRunning.get(assistantData.id); + const newAssistant = this.createAssistant(assistantData, isRunning); if (!isRunning) { - return assistant; + return newAssistant; } this._paragraphsStack.forEach((value, paraId) => { - assistant.onChangeParagraph( + newAssistant.onChangeParagraph( paraId, value.recalcId, value.text, value.annotations, ); }); - const paragraphIdsToUpdate = [...assistant.checked]; - assistant.checkParagraphs(paragraphIdsToUpdate); + const paragraphIdsToUpdate = [...oldAssistant.checked]; + oldAssistant.checked.clear(); + newAssistant.checkParagraphs(paragraphIdsToUpdate); - return assistant; + return newAssistant; } /** @param {string} assistantId */