1. Changed the assistant update

2. Added context reset if the prompt is different from the previous one.
This commit is contained in:
Artur
2026-01-28 15:40:30 +03:00
parent d07812910e
commit a1fd6aa9b7
5 changed files with 46 additions and 24 deletions

View File

@ -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) {

View File

@ -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);
});
});
}

View File

@ -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 = `<div>

View File

@ -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;

View File

@ -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 */