mirror of
https://github.com/ONLYOFFICE/onlyoffice.github.io.git
synced 2026-02-10 18:05:06 +08:00
Add check if AI response in the wrong format
This commit is contained in:
@ -50,28 +50,14 @@ AssistantHint.prototype.annotateParagraph = async function(paraId, recalcId, tex
|
||||
{
|
||||
this.paragraphs[paraId] = {};
|
||||
|
||||
let requestEngine = AI.Request.create(AI.ActionType.Chat);
|
||||
if (!requestEngine || text.length === 0)
|
||||
if (text.length === 0)
|
||||
return false;
|
||||
|
||||
let isSendedEndLongAction = false;
|
||||
async function checkEndAction()
|
||||
{
|
||||
if (!isSendedEndLongAction)
|
||||
isSendedEndLongAction = true;
|
||||
}
|
||||
|
||||
const argPrompt = this._createPrompt(text);
|
||||
|
||||
let response = "";
|
||||
await requestEngine.chatRequest(argPrompt, false, async function (/** @type {string} */data)
|
||||
{
|
||||
if (!data)
|
||||
return;
|
||||
await checkEndAction();
|
||||
response += data;
|
||||
});
|
||||
await checkEndAction();
|
||||
let response = await this.chatRequest(argPrompt);;
|
||||
if (!response)
|
||||
return false;
|
||||
|
||||
let rangeId = 1;
|
||||
let ranges = [];
|
||||
|
||||
@ -50,29 +50,14 @@ AssistantReplaceHint.prototype.annotateParagraph = async function(paraId, recalc
|
||||
{
|
||||
this.paragraphs[paraId] = {};
|
||||
|
||||
let requestEngine = AI.Request.create(AI.ActionType.Chat);
|
||||
if (!requestEngine || text.length === 0)
|
||||
if (text.length === 0)
|
||||
return false;
|
||||
|
||||
let isSendedEndLongAction = false;
|
||||
async function checkEndAction()
|
||||
{
|
||||
if (!isSendedEndLongAction)
|
||||
isSendedEndLongAction = true;
|
||||
}
|
||||
|
||||
|
||||
const argPrompt = this._createPrompt(text);
|
||||
|
||||
let response = "";
|
||||
await requestEngine.chatRequest(argPrompt, false, async function (/** @type {string} */data)
|
||||
{
|
||||
if (!data)
|
||||
return;
|
||||
await checkEndAction();
|
||||
response += data;
|
||||
});
|
||||
await checkEndAction();
|
||||
|
||||
let response = await this.chatRequest(argPrompt);;
|
||||
if (!response)
|
||||
return false;
|
||||
|
||||
let rangeId = 1;
|
||||
let ranges = [];
|
||||
|
||||
|
||||
@ -50,29 +50,14 @@ AssistantReplace.prototype.annotateParagraph = async function(paraId, recalcId,
|
||||
{
|
||||
this.paragraphs[paraId] = {};
|
||||
|
||||
let requestEngine = AI.Request.create(AI.ActionType.Chat);
|
||||
if (!requestEngine || text.length === 0)
|
||||
if (text.length === 0)
|
||||
return false;
|
||||
|
||||
let isSendedEndLongAction = false;
|
||||
async function checkEndAction()
|
||||
{
|
||||
if (!isSendedEndLongAction)
|
||||
isSendedEndLongAction = true;
|
||||
}
|
||||
|
||||
|
||||
const argPrompt = this._createPrompt(text);
|
||||
|
||||
let response = "";
|
||||
await requestEngine.chatRequest(argPrompt, false, async function (/** @type {string} */data)
|
||||
{
|
||||
if (!data)
|
||||
return;
|
||||
await checkEndAction();
|
||||
response += data;
|
||||
});
|
||||
await checkEndAction();
|
||||
|
||||
let response = await this.chatRequest(argPrompt);;
|
||||
if (!response)
|
||||
return false;
|
||||
|
||||
let rangeId = 1;
|
||||
let ranges = [];
|
||||
|
||||
|
||||
@ -180,6 +180,40 @@ CustomAnnotator.prototype._handleNewRanges = function(ranges, paraId, text)
|
||||
CustomAnnotator.prototype._handleNewRangePositions = function(range, paraId, text)
|
||||
{
|
||||
};
|
||||
CustomAnnotator.prototype.chatRequest = async function(prompt)
|
||||
{
|
||||
let requestEngine = AI.Request.create(AI.ActionType.Chat);
|
||||
if (!requestEngine)
|
||||
return null;
|
||||
|
||||
let response = await requestEngine.chatRequest(prompt, false);
|
||||
return this.normalizeResponse(response);
|
||||
};
|
||||
/**
|
||||
* Normalizes AI response by removing markdown code block wrappers
|
||||
* @param {string} response - The raw AI response that might be wrapped in ```json``` blocks
|
||||
* @returns {string} - The normalized response with markdown code blocks removed
|
||||
*/
|
||||
CustomAnnotator.prototype.normalizeResponse = function(response) {
|
||||
if (typeof response !== 'string') {
|
||||
return response;
|
||||
}
|
||||
|
||||
// Trim whitespace
|
||||
let normalized = response.trim();
|
||||
|
||||
// Check if response is wrapped in markdown code blocks
|
||||
// Patterns: ```json\n{...}\n``` or ```\n{...}\n```
|
||||
const codeBlockPattern = /^```(?:json)?\s*\n?([\s\S]*?)\n?```$/;
|
||||
const match = normalized.match(codeBlockPattern);
|
||||
|
||||
if (match) {
|
||||
// Extract content between code block markers
|
||||
normalized = match[1].trim();
|
||||
}
|
||||
|
||||
return normalized;
|
||||
};
|
||||
/**
|
||||
* @param {string} str
|
||||
* @param {string} searchStr
|
||||
|
||||
@ -43,17 +43,6 @@ GrammarChecker.prototype.annotateParagraph = async function(paraId, recalcId, te
|
||||
{
|
||||
this.paragraphs[paraId] = {};
|
||||
|
||||
let requestEngine = AI.Request.create(AI.ActionType.Chat);
|
||||
if (!requestEngine)
|
||||
return false;
|
||||
|
||||
let isSendedEndLongAction = false;
|
||||
async function checkEndAction()
|
||||
{
|
||||
if (!isSendedEndLongAction)
|
||||
isSendedEndLongAction = true;
|
||||
}
|
||||
|
||||
let argPrompt = `You are a grammar correction tool that analyzes text for punctuation and style issues only. You will receive text to analyze and must respond with corrections in a specific JSON format.
|
||||
|
||||
CRITICAL REQUIREMENT - READ CAREFULLY:
|
||||
@ -153,17 +142,10 @@ CRITICAL - Output Format:
|
||||
|
||||
Text to check:`;
|
||||
argPrompt += text;
|
||||
|
||||
let response = "";
|
||||
await requestEngine.chatRequest(argPrompt, false, async function (data)
|
||||
{
|
||||
if (!data)
|
||||
return;
|
||||
await checkEndAction();
|
||||
|
||||
response += data;
|
||||
});
|
||||
await checkEndAction();
|
||||
|
||||
let response = await this.chatRequest(argPrompt);
|
||||
if (!response)
|
||||
return false;
|
||||
|
||||
let rangeId = 1;
|
||||
let ranges = [];
|
||||
|
||||
@ -42,17 +42,6 @@ SpellChecker.prototype.annotateParagraph = async function(paraId, recalcId, text
|
||||
{
|
||||
this.paragraphs[paraId] = {};
|
||||
|
||||
let requestEngine = AI.Request.create(AI.ActionType.Chat);
|
||||
if (!requestEngine)
|
||||
return false;
|
||||
|
||||
let isSendedEndLongAction = false;
|
||||
async function checkEndAction()
|
||||
{
|
||||
if (!isSendedEndLongAction)
|
||||
isSendedEndLongAction = true;
|
||||
}
|
||||
|
||||
let argPrompt = `You are a spellcheck corrector. I will provide text that may contain spelling errors in any language. Your task is to identify ALL spelling mistakes and return ONLY the corrections in the following JSON format:
|
||||
|
||||
[
|
||||
@ -144,16 +133,9 @@ Output: []
|
||||
Text to check:`;
|
||||
argPrompt += text;
|
||||
|
||||
let response = "";
|
||||
await requestEngine.chatRequest(argPrompt, false, async function (data)
|
||||
{
|
||||
if (!data)
|
||||
return;
|
||||
await checkEndAction();
|
||||
|
||||
response += data;
|
||||
});
|
||||
await checkEndAction();
|
||||
let response = await this.chatRequest(argPrompt);
|
||||
if (!response)
|
||||
return false;
|
||||
|
||||
let rangeId = 1;
|
||||
let ranges = [];
|
||||
|
||||
@ -173,10 +173,44 @@ TextAnnotator.prototype._handleNewRanges = function(ranges, paraId, text)
|
||||
TextAnnotator.prototype._handleNewRangePositions = function(range, paraId, text)
|
||||
{
|
||||
};
|
||||
TextAnnotator.prototype.chatRequest = async function(prompt)
|
||||
{
|
||||
let requestEngine = AI.Request.create(AI.ActionType.Chat);
|
||||
if (!requestEngine)
|
||||
return null;
|
||||
|
||||
let response = await requestEngine.chatRequest(prompt, false);
|
||||
return this.normalizeResponse(response);
|
||||
};
|
||||
/**
|
||||
* @param {string} str
|
||||
* @param {string} searchStr
|
||||
* @param {string} [fromIndex]
|
||||
* Normalizes AI response by removing markdown code block wrappers
|
||||
* @param {string} response - The raw AI response that might be wrapped in ```json``` blocks
|
||||
* @returns {string} - The normalized response with markdown code blocks removed
|
||||
*/
|
||||
TextAnnotator.prototype.normalizeResponse = function(response) {
|
||||
if (typeof response !== 'string') {
|
||||
return response;
|
||||
}
|
||||
|
||||
// Trim whitespace
|
||||
let normalized = response.trim();
|
||||
|
||||
// Check if response is wrapped in markdown code blocks
|
||||
// Patterns: ```json\n{...}\n``` or ```\n{...}\n```
|
||||
const codeBlockPattern = /^```(?:json)?\s*\n?([\s\S]*?)\n?```$/;
|
||||
const match = normalized.match(codeBlockPattern);
|
||||
|
||||
if (match) {
|
||||
// Extract content between code block markers
|
||||
normalized = match[1].trim();
|
||||
}
|
||||
|
||||
return normalized;
|
||||
};
|
||||
/**
|
||||
* @param {string} str
|
||||
* @param {string} searchStr
|
||||
* @param {string} [fromIndex]
|
||||
* @returns {number}
|
||||
*/
|
||||
TextAnnotator.prototype.simpleGraphemeIndexOf = function(str, searchStr, fromIndex = 0) {
|
||||
@ -185,7 +219,7 @@ TextAnnotator.prototype.simpleGraphemeIndexOf = function(str, searchStr, fromInd
|
||||
return codeUnitIndex;
|
||||
}
|
||||
const adjustedIndex = adjustIndexForSurrogates(str, codeUnitIndex);
|
||||
|
||||
|
||||
function adjustIndexForSurrogates(str, codeUnitIndex) {
|
||||
let surrogateCount = 0;
|
||||
for (let i = 0; i < codeUnitIndex; i++) {
|
||||
@ -198,3 +232,4 @@ TextAnnotator.prototype.simpleGraphemeIndexOf = function(str, searchStr, fromInd
|
||||
}
|
||||
return adjustedIndex;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user