跳到主要内容

commentText

This function adds an explanation or comment to the text. If no text or paragraph number is specified, the current paragraph is used by default. Specify whether the explanation should be added as a comment or as a footnote. The AI will generate the content based on your prompt and insert it in the chosen format.

Prompts

  • Explain this text
  • Add a footnote to this text
  • Comment this text

Function registration

let func = new RegisteredFunction({
name: "commentText",
description:
"Use this function if you asked to comment or explain anything. If text or paragraph number is not specified assume that we are working with the current paragraph. Specify whether the explanation should be added as a comment or as a footnote. The AI will generate the content based on your prompt and insert it in the chosen format.",
parameters: {
type: "object",
properties: {
type: {
type: "string",
description:
"Whether to add as a 'comment' or as a 'footnote' (default is 'comment')",
},
},
required: ["type"],
},
examples: [
{
prompt: "Explain this text",
arguments: { type: "comment" },
},
{
prompt: "Add a footnote to this text",
arguments: { type: "footnote" },
},
{
prompt: "Comment this text",
arguments: { type: "comment" },
},
{
prompt: "Explain this text as a footnote",
arguments: { type: "footnote" },
},
{
prompt: "Explain selected text as a comment",
arguments: { type: "comment" },
},
],
});

Parameters

NameTypeExampleDescription
typestring"comment"Specifies whether to add an explanation as a "comment" or as a "footnote". The default value is "comment".

Function execution

(function () {
let func = new RegisteredFunction({
name: "commentText",
description:
"Use this function if you asked to comment or explain anything. If text or paragraph number is not specified assume that we are working with the current paragraph. Specify whether the explanation should be added as a comment or as a footnote. The AI will generate the content based on your prompt and insert it in the chosen format.",
parameters: {
type: "object",
properties: {
type: {
type: "string",
description:
"Whether to add as a 'comment' or as a 'footnote' (default is 'comment')",
},
},
required: ["type"],
},
examples: [
{
prompt: "Explain this text",
arguments: { type: "comment" },
},
{
prompt: "Add a footnote to this text",
arguments: { type: "footnote" },
},
{
prompt: "Comment this text",
arguments: { type: "comment" },
},
{
prompt: "Explain this text as a footnote",
arguments: { type: "footnote" },
},
{
prompt: "Explain selected text as a comment",
arguments: { type: "comment" },
},
],
});

func.call = async function (params) {
let type = params.type;
let isFootnote = "footnote" === type;

let text = await Asc.Editor.callCommand(function () {
let doc = Api.GetDocument();
let range = doc.GetRangeBySelect();
let text = range ? range.GetText() : "";
if (!text) {
text = doc.GetCurrentWord();
doc.SelectCurrentWord();
}

return text;
});

let argPromt = params.prompt + ":\n" + text;

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

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

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

if (isFootnote) {
let addFootnote = true;
let result = await requestEngine.chatRequest(
argPromt,
false,
async function (data) {
if (!data) return;

await checkEndAction();
Asc.scope.data = data;
Asc.scope.model = requestEngine.modelUI.name;

if (addFootnote) {
await Asc.Editor.callCommand(function () {
Api.GetDocument().AddFootnote();
});
addFootnote = false;
}
await Asc.Library.PasteText(data);
},
);
} else {
let commentId = null;
let result = await requestEngine.chatRequest(
argPromt,
false,
async function (data) {
if (!data) return;

await checkEndAction();
Asc.scope.data = data;
Asc.scope.model = requestEngine.modelUI.name;
Asc.scope.commentId = commentId;

commentId = await Asc.Editor.callCommand(function () {
let doc = Api.GetDocument();

let commentId = Asc.scope.commentId;
if (!commentId) {
let range = doc.GetRangeBySelect();
if (!range) return null;

let comment = range.AddComment(
Asc.scope.data,
Asc.scope.model,
"uid" + Asc.scope.model,
);
if (!comment) return null;
doc.ShowComment([comment.GetId()]);
return comment.GetId();
}

let comment = doc.GetCommentById(commentId);
if (!comment) return commentId;

comment.SetText(comment.GetText() + scope.data);
return commentId;
});
},
);
}

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

return func;
})();

Methods used: GetDocument, GetRangeBySelect, GetText, AddComment, GetCurrentWord, SelectCurrentWord, AddFootnote, ShowComment, GetCommentById, GetId, SetText, GetText, EndAction, StartAction, Asc.scope object

Result