Skip to main content

checkSpelling

This function checks spelling or fixes other text errors in the current paragraph.

Prompts

  • Check spelling

Function registration

let func = new RegisteredFunction({
name: "checkSpelling",
description:
"Use this function if you asked to check spelling for current paragraph or fix other type of text errors in the current paragraph.",
parameters: {
type: "object",
properties: {},
required: [],
},
examples: [
{
prompt: "Check spelling for the current paragraph",
arguments: {},
},
{
prompt: "Fix grammar in current paragraph",
arguments: {},
},
{
prompt: "Check for text errors",
arguments: {},
},
],
});

Function execution

(function () {
let func = new RegisteredFunction({
name: "checkSpelling",
description:
"Use this function if you asked to check spelling for current paragraph or fix other type of text errors in the current paragraph.",
parameters: {
type: "object",
properties: {},
required: [],
},
examples: [
{
prompt: "Check spelling for the current paragraph",
arguments: {},
},
{
prompt: "Fix grammar in current paragraph",
arguments: {},
},
{
prompt: "Check for text errors",
arguments: {},
},
],
});

func.call = async function (params) {
let text = await Asc.Editor.callCommand(function () {
let par = Api.GetDocument().GetCurrentParagraph();
if (!par) return "";
par.Select();
return par.GetText();
});

let argPrompt =
"Check spelling and grammar for text:\n" +
text +
"\n Answer with only the new corrected text, no need of any explanations.";

let isTrackChanges = await Asc.Editor.callCommand(function () {
let isOn = Api.GetDocument().IsTrackRevisions();
if (isOn) Api.GetDocument().SetTrackRevisions(false);
return isOn;
});

let requestEngine = AI.Request.create(AI.ActionType.Chat);
if (!requestEngine) return;

await Asc.Editor.callMethod("StartAction", ["GroupActions"]);

await Asc.Editor.callMethod("StartAction", [
"Block",
"AI (" + requestEngine.modelUI.name + ")",
]);

let isSendedEndLongAction = false;
async function checkEndAction() {
if (!isSendedEndLongAction) {
await Asc.Editor.callMethod("EndAction", [
"Block",
"AI (" + requestEngine.modelUI.name + ")",
]);
isSendedEndLongAction = true;
}
}

let resultText = "";

let result = await requestEngine.chatRequest(
argPrompt,
false,
async function (data) {
if (!data) return;
await checkEndAction();

resultText += data;

await Asc.Editor.callMethod("EndAction", [
"GroupActions",
"",
"cancel",
]);
await Asc.Editor.callMethod("StartAction", ["GroupActions"]);

Asc.scope.text = resultText;
await Asc.Editor.callCommand(function () {
let par = Api.GetDocument().GetCurrentParagraph();
if (!par) return "";
par.Select();
Api.ReplaceTextSmart([Asc.scope.text]);
});
},
);

await checkEndAction();

await Asc.Editor.callMethod("EndAction", [
"GroupActions",
"",
"cancel",
]);
await Asc.Editor.callMethod("StartAction", ["GroupActions"]);

Asc.scope.modelName = requestEngine.modelUI.name;
await Asc.Editor.callCommand(function () {
return Api.GetDocument().SetAssistantTrackRevisions(
true,
Asc.scope.modelName,
);
});

Asc.scope.text = resultText;
await Asc.Editor.callCommand(function () {
let par = Api.GetDocument().GetCurrentParagraph();
if (!par) return "";
par.Select();
Api.ReplaceTextSmart([Asc.scope.text]);
});

await Asc.Editor.callCommand(function () {
return Api.GetDocument().SetAssistantTrackRevisions(false);
});

if (isTrackChanges) {
await Asc.Editor.callCommand(function () {
Api.GetDocument().SetTrackRevisions(true);
});
}

await Asc.Editor.callMethod("EndAction", ["GroupActions"]);
};

return func;
})();

Methods used: GetDocument, GetCurrentParagraph, Select, GetText, IsTrackRevisions, SetTrackRevisions, ReplaceTextSmart, SetAssistantTrackRevisions, EndAction, StartAction, Asc.scope object

Result