mirror of
https://github.com/ONLYOFFICE/onlyoffice.github.io.git
synced 2026-04-07 14:04:30 +08:00
1. Changed the assistant update
2. Added context reset if the prompt is different from the previous one.
This commit is contained in:
@ -1004,6 +1004,7 @@ function customAssistantWindowShow(assistantId, buttonAssistant)
|
|||||||
if (!element) return;
|
if (!element) return;
|
||||||
if (buttonAssistant) {
|
if (buttonAssistant) {
|
||||||
buttonAssistant.text = element.name;
|
buttonAssistant.text = element.name;
|
||||||
|
customAssistantManager.updateAssistant(element);
|
||||||
} else {
|
} else {
|
||||||
buttonAssistant = new Asc.ButtonToolbar(null);
|
buttonAssistant = new Asc.ButtonToolbar(null);
|
||||||
buttonAssistant.text = element.name;
|
buttonAssistant.text = element.name;
|
||||||
@ -1021,11 +1022,11 @@ function customAssistantWindowShow(assistantId, buttonAssistant)
|
|||||||
onclick: () => customAssistantWindowDeleteConfirm(element.id, buttonAssistant)
|
onclick: () => customAssistantWindowDeleteConfirm(element.id, buttonAssistant)
|
||||||
}];
|
}];
|
||||||
buttonAssistant.attachOnClick(async function(){
|
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]);
|
Asc.Buttons.updateToolbarMenu(window.buttonMainToolbar.id, window.buttonMainToolbar.name, [buttonAssistant]);
|
||||||
customAssistantManager.createAssistant(element);
|
|
||||||
}
|
}
|
||||||
customAssistantWindowClose();
|
customAssistantWindowClose();
|
||||||
} else {
|
} else {
|
||||||
@ -1161,7 +1162,7 @@ function customAssistantWarning(warningText, assistantData) {
|
|||||||
* @param {Asc.ButtonToolbar} buttonAssistant
|
* @param {Asc.ButtonToolbar} buttonAssistant
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async function onStartCustomAssistant(assistantId, buttonAssistant)
|
async function customAssistantOnClickToolbarIcon(assistantId, buttonAssistant)
|
||||||
{
|
{
|
||||||
const isAssistantRunning = customAssistantManager.checkNeedToRunAssistant(assistantId);
|
const isAssistantRunning = customAssistantManager.checkNeedToRunAssistant(assistantId);
|
||||||
if (isAssistantRunning) {
|
if (isAssistantRunning) {
|
||||||
|
|||||||
@ -698,7 +698,7 @@ async function registerButtons(window, undefined)
|
|||||||
onclick: () => customAssistantWindowDeleteConfirm(element.id, buttonAssistant)
|
onclick: () => customAssistantWindowDeleteConfirm(element.id, buttonAssistant)
|
||||||
}];
|
}];
|
||||||
buttonAssistant.attachOnClick(async function(){
|
buttonAssistant.attachOnClick(async function(){
|
||||||
onStartCustomAssistant(element.id, buttonAssistant);
|
customAssistantOnClickToolbarIcon(element.id, buttonAssistant);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -137,6 +137,7 @@ function CustomAnnotationPopup()
|
|||||||
let textColor = window.Asc.plugin.theme ? window.Asc.plugin.theme["text-normal"] : "#3D3D3D";
|
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 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";
|
let ballonColor = window.Asc.plugin.theme ? window.Asc.plugin.theme["canvas-background"] : "#F5F5F5";
|
||||||
|
this.content = "";
|
||||||
|
|
||||||
if (data.type === 0) { // Hint
|
if (data.type === 0) { // Hint
|
||||||
this.content = `<div>
|
this.content = `<div>
|
||||||
|
|||||||
@ -42,6 +42,8 @@ function CustomAnnotator(annotationPopup, assistantData) {
|
|||||||
this.assistantData = assistantData;
|
this.assistantData = assistantData;
|
||||||
this.type = assistantData.type;
|
this.type = assistantData.type;
|
||||||
this._skipNextChangeParagraph = false;
|
this._skipNextChangeParagraph = false;
|
||||||
|
|
||||||
|
this._lastUsedPrompt = "";
|
||||||
}
|
}
|
||||||
CustomAnnotator.prototype = Object.create(TextAnnotator.prototype);
|
CustomAnnotator.prototype = Object.create(TextAnnotator.prototype);
|
||||||
CustomAnnotator.prototype.constructor = CustomAnnotator;
|
CustomAnnotator.prototype.constructor = CustomAnnotator;
|
||||||
@ -60,9 +62,20 @@ Object.assign(CustomAnnotator.prototype, {
|
|||||||
|
|
||||||
const argPrompt = this._createPrompt(text);
|
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);
|
let response = await this.chatRequest(argPrompt);
|
||||||
|
|
||||||
if (!response || response === '[]') {
|
if (!response || response === "[]") {
|
||||||
if (response === null) {
|
if (response === null) {
|
||||||
return null; // no AI model selected
|
return null; // no AI model selected
|
||||||
}
|
}
|
||||||
@ -70,7 +83,11 @@ Object.assign(CustomAnnotator.prototype, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ranges = this._convertToRanges(paraId, text, JSON.parse(response));
|
const ranges = this._convertToRanges(
|
||||||
|
paraId,
|
||||||
|
text,
|
||||||
|
JSON.parse(response),
|
||||||
|
);
|
||||||
let obj = {
|
let obj = {
|
||||||
type: "highlightText",
|
type: "highlightText",
|
||||||
paragraphId: paraId,
|
paragraphId: paraId,
|
||||||
@ -124,7 +141,10 @@ Object.assign(CustomAnnotator.prototype, {
|
|||||||
this._skipNextChangeParagraph = false;
|
this._skipNextChangeParagraph = false;
|
||||||
return paraIds.map(() => 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) {
|
onAccept: async function (paraId, rangeId) {
|
||||||
this._skipNextChangeParagraph = true;
|
this._skipNextChangeParagraph = true;
|
||||||
|
|||||||
@ -63,11 +63,9 @@ class CustomAssistantManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {localStorageCustomAssistantItem} assistantData
|
* @param {localStorageCustomAssistantItem} assistantData
|
||||||
|
* @param {boolean} [isForUpdate]
|
||||||
*/
|
*/
|
||||||
createAssistant(assistantData) {
|
createAssistant(assistantData, isForUpdate) {
|
||||||
if (this._customAssistants.has(assistantData.id)) {
|
|
||||||
return this._updateAssistant(assistantData);
|
|
||||||
}
|
|
||||||
/** @type {Assistant | null} */
|
/** @type {Assistant | null} */
|
||||||
let assistant = null;
|
let assistant = null;
|
||||||
switch (assistantData.type) {
|
switch (assistantData.type) {
|
||||||
@ -96,40 +94,42 @@ class CustomAssistantManager {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._customAssistants.set(assistantData.id, assistant);
|
||||||
|
if (!isForUpdate) {
|
||||||
this._isCustomAssistantInit.set(assistantData.id, false);
|
this._isCustomAssistantInit.set(assistantData.id, false);
|
||||||
this._isCustomAssistantRunning.set(assistantData.id, false);
|
this._isCustomAssistantRunning.set(assistantData.id, false);
|
||||||
this._customAssistants.set(assistantData.id, assistant);
|
}
|
||||||
|
|
||||||
return assistant;
|
return assistant;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param {localStorageCustomAssistantItem} assistantData
|
* @param {localStorageCustomAssistantItem} assistantData
|
||||||
*/
|
*/
|
||||||
_updateAssistant(assistantData) {
|
updateAssistant(assistantData) {
|
||||||
let assistant = this._customAssistants.get(assistantData.id);
|
console.warn("Updating custom assistant: " + assistantData.id);
|
||||||
if (!assistant) {
|
let oldAssistant = this._customAssistants.get(assistantData.id);
|
||||||
|
if (!oldAssistant) {
|
||||||
throw new Error("Custom assistant not found: " + assistantData.id);
|
throw new Error("Custom assistant not found: " + assistantData.id);
|
||||||
}
|
}
|
||||||
assistant.assistantData = assistantData;
|
|
||||||
assistant.type = assistantData.type;
|
|
||||||
|
|
||||||
const isRunning = this._isCustomAssistantRunning.get(assistantData.id);
|
const isRunning = this._isCustomAssistantRunning.get(assistantData.id);
|
||||||
|
const newAssistant = this.createAssistant(assistantData, isRunning);
|
||||||
if (!isRunning) {
|
if (!isRunning) {
|
||||||
return assistant;
|
return newAssistant;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._paragraphsStack.forEach((value, paraId) => {
|
this._paragraphsStack.forEach((value, paraId) => {
|
||||||
assistant.onChangeParagraph(
|
newAssistant.onChangeParagraph(
|
||||||
paraId,
|
paraId,
|
||||||
value.recalcId,
|
value.recalcId,
|
||||||
value.text,
|
value.text,
|
||||||
value.annotations,
|
value.annotations,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
const paragraphIdsToUpdate = [...assistant.checked];
|
const paragraphIdsToUpdate = [...oldAssistant.checked];
|
||||||
assistant.checkParagraphs(paragraphIdsToUpdate);
|
oldAssistant.checked.clear();
|
||||||
|
newAssistant.checkParagraphs(paragraphIdsToUpdate);
|
||||||
|
|
||||||
return assistant;
|
return newAssistant;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param {string} assistantId */
|
/** @param {string} assistantId */
|
||||||
|
|||||||
Reference in New Issue
Block a user