Vodič korak po korak: dodavanje prilagođenih AI funkcija AI agentu ONLYOFFICE-a

21 August 2025By Marko Todorovic

Sa našim novim AI agentom, pružamo najsavremenije alate za ispunjavanje zahteva savremenog, brzo menjajućeg digitalnog sveta. Kao projekat otvorenog koda, pozdravljamo inovacije vođene korisnicima. U ovom članku, prikazaćemo vam kako da dodate prilagođene funkcije AI agentu, čineći vaše rad na dokumentima bržim, lakšim i praktičnijim.

Step-by-step guide: adding custom AI functions to ONLYOFFICE AI agent

Koje su funkcije veštačke inteligencije i čemu služe?

AI funkcije su osnovni gradivni blokovi funkcionalnosti AI agenta. Suštinski, to su uputstva koja govore AI agentu:

  • Koji zahtev poslati AI modelu.
  • Koje manipulacije izvesti na vašem dokumentu.

Korišćenjem AI funkcija, možete proširiti i kontrolisati način na koji veštačka inteligencija interaguje sa sadržajem vašeg dokumenta.

Kako koristiti AI funkciju

  1. Dodajte model po vašem izboru u AI dodatak.
  2. Otvorite dijalog kutiju AI agenta pritiskom na CTRL + /.
  3. Unesite svoj zahtev i pritisnite Enter.

Primer: funkcija commentText

Funkcija commentText omogućava vam da dodate AI generisane komentare direktno u vaš dokument. Evo kako to funkcioniše:

  • Izaberite reč na koju želite da ostavite komentar
  • Otvorite dijalog kutiju AI agenta (CTRL + /).
  • Unesite svoju instrukciju, na primer: “Objasnite ovaj tekst.”
  • Pritisnite Enter.

Step-by-step guide: adding custom AI functions to ONLYOFFICE AI agent

AI agent će pokrenuti funkciju commentText i umetnuti relevantne komentare u vaš dokument:

Step-by-step guide: adding custom AI functions to ONLYOFFICE AI agent

Zašto dodavati prilagođene funkcije svom AI agentu?

Dodavanje prilagođenih AI funkcija omogućava vam da proširite mogućnosti pametnog AI agenta i prilagodite ga svojim tačnim potrebama. Bilo da radite sa dokumentima, tabelama ili prezentacijama, svestranost agenta, u kombinaciji sa snagom modernih AI modela, pomaže vam da ideje pretvorite u stvarnost i besprekorno ih integrišete u svoj radni proces.

Opšta logika dodavanja prilagođene AI funkcije

Proces dodavanja prilagođene funkcije uključuje dve glavne faze:

  • Registracija funkcije – Registruje AI funkciju i njene metapodatke unutar agentovog okruženja.
  • Izvršenje funkcije – Sprovodi osnovnu logiku, koja uključuje slanje zahteva AI modelu i manipulisanje sadržajem dokumenta koristeći naš Office API.

Sada hajde da bliže razmotrimo ove faze.

Registracija funkcije

Da bismo dodali novu funkciju, implementiramo objekat RegisteredFunction. On nam omogućava da dodamo metapodatke i logiku. Evo primera dodavanja funkcije commentText za uređivač dokumenata:

let func = new RegisteredFunction();
        func.name = "commentText";
        func.params = [
            "type (string): whether to add as a 'comment' or as a 'footnote'    default is 'comment')"
        ];


        func.examples = [
            "If you need to explain selected text as a comment, respond with:\n" +
            "[functionCalling (commentText)]: {\"prompt\" : \"Explain this text\", \"type\": \"comment\"}",


            "If you need to add a footnote to selected text, respond with:\n" +
            "[functionCalling (commentText)]: {\"prompt\" : \"Add a footnote to this text\", \"type\": \"footnote\"}",


            "If you need to comment selected text, respond with:\n" +
            "[functionCalling (commentText)]: {\"prompt\" : \"Comment this text\"}",


            "If you need to explain selected text as a footnote, respond with:\n" +
            "[functionCalling (commentText)]: {\"prompt\" : \"Explain this text\", \"type\": \"footnote\"}"
     ]

Gde:

  • func.ime: ime koje će AI koristiti za pozivanje ove funkcije (npr., “commentText”).
  • func.params: lista parametara koje funkcija očekuje od AI. Na primer:

prompt (string): opis ili instrukcija za komentar.

tip (string): “komentar” ili “fusnota” — specifikuje šta da se unese.

  • func.uputstva: primeri ispravnog poziva funkcije za AI.
  • func.opis: objašnjava veštačkoj inteligenciji za šta se funkcija koristi.

Ovi parametri se koriste od strane AI. Objekat RegisteredFunction() je definisan u fajlu helperFunc.js.

Logika izvršenja funkcije

Nakon registrovanja funkcije, implementiramo stvarnu logiku koja se izvršava kada veštačka inteligencija pozove ovu funkciju.

  • Preuzmite izabrani tekst koristeći Asc.Editor.callCommand().
func.call = async function(params) {
            let type = params.type;
            let isFootnote = "footnote" === type;


// Executes a block of code inside the editor's context using the office=js API.
            let text = await Asc.Editor.callCommand(function(){
                let doc = Api.GetDocument();
// Gets the current selected text range.
                let range = doc.GetRangeBySelect();
                let text = range ? range.GetText() : "";
                if (!text)
                {
                    text = doc.GetCurrentWord();
// Selects the current word so comments can be applied to it.
                    doc.SelectCurrentWord();
                }


                return text;
            });
  • Konstruišite upit za veštačku inteligenciju kombinovanjem params.prompt i odabranog teksta.
let argPromt = params.prompt + ":\n" + text;
  • Inicijalizujte AI.Request.create objekat koristeći AI.Request.create. Objekat je definisan u engine.js fajlu. Ovaj objekat olakšava slanje zahteva AI modelu.
// Initializes a request engine for communicating with the AI model (e.g. Chat, Translation).
            let requestEngine = AI.Request.create(AI.ActionType.Chat);
            if (!requestEngine)
                return;
  • Pošaljite zahtev koristeći chatRequest() i primite rezultat u povratnom pozivu.
// Sends a prompt to the AI model and processes the response via callback. Can stream or wait.
                let result = await requestEngine.chatRequest(argPromt, false, async function(data) {
                    if (!data)
                        return;
  • Ubaci odgovor kao komentar ili fusnotu koristeći AddFootnote() ili AddComment()

Implementacija AddFootnote:

if (isFootnote)
            {
                let addFootnote = true;
// Sends a prompt to the AI model and processes the response via callback. Can stream or wait.
                let result = await requestEngine.chatRequest(argPromt, false, async function(data) {
                    if (!data)
                        return;


// Marks the end of a logical group or block action in the editor.
                    await checkEndAction();
                    Asc.scope.data = data;
                    Asc.scope.model = requestEngine.modelUI.name;


                    if (addFootnote)
                    {
// Executes a block of code inside the editor's context using the document model API.
                        await Asc.Editor.callCommand(function(){
// Returns the main document object, which gives access to all editing, structure, and selection APIs.
                            Api.GetDocument().AddFootnote();
                        });
                        addFootnote = false;
                    }
// Inserts the AI-generated result into the document at the current selection or cursor.
                    await Asc.Library.PasteText(data);
                });

Implementacija AddComment:

let commentId = null;
// Sends a prompt to the AI model and processes the response via callback. Can stream or wait.
                let result = await requestEngine.chatRequest(argPromt, false, async function(data) {
                    if (!data)
                        return;


// Marks the end of a logical group or block action in the editor.
                    await checkEndAction();
                    Asc.scope.data = data;
                    Asc.scope.model = requestEngine.modelUI.name;
                    Asc.scope.commentId = commentId;


// Executes a block of code inside the editor's context using the document model API.
                    commentId = await Asc.Editor.callCommand(function(){
// Returns the main document object, which gives access to all editing, structure, and selection APIs.
                        let doc = Api.GetDocument();


                        let commentId = Asc.scope.commentId;
                        if (!commentId)
                        {
// Gets the current selected text range, which can be modified or annotated.
                            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;
                    });
                });
            }

Napomena!
Da bismo osigurali da se čitav blok promena može vratiti nakon što se zahtev izvrši, koristimo metode StartActionEndAction u funkciji commentText.

Cela implementacija funkcije commentText sa komentarima:

(function(){
// Defines the commentText function — lets AI insert a comment or footnote for selected text using AI response.
    WORD_FUNCTIONS.commentText = function()
    {
// Creates a new function object that will be registered and exposed to the AI.
        let func = new RegisteredFunction();
        func.name = "commentText";
// Lists the parameters expected by the function. These are passed as a JSON object by the AI agent.
        func.params = [
            "type (string): whether to add as a 'comment' or as a 'footnote' (default is 'comment')"
        ];


// Gives example JSON inputs to teach the AI how to correctly invoke this function.
        func.examples = [
            "If you need to explain selected text as a comment, respond with:\n" +
            "[functionCalling (commentText)]: {\"prompt\" : \"Explain this text\", \"type\": \"comment\"}",


            "If you need to add a footnote to selected text, respond with:\n" +
            "[functionCalling (commentText)]: {\"prompt\" : \"Add a footnote to this text\", \"type\": \"footnote\"}",


            "If you need to comment selected text, respond with:\n" +
            "[functionCalling (commentText)]: {\"prompt\" : \"Comment this text\"}",


            "If you need to explain selected text as a footnote, respond with:\n" +
            "[functionCalling (commentText)]: {\"prompt\" : \"Explain this text\", \"type\": \"footnote\"}"
        ];
        
// The actual logic that gets executed when the AI calls this function.
        func.call = async function(params) {
            let type = params.type;
            let isFootnote = "footnote" === type;


// Executes a block of code inside the editor's context using the office-js API.
            let text = await Asc.Editor.callCommand(function(){
                let doc = Api.GetDocument();
// Gets the current selected text range.
                let range = doc.GetRangeBySelect();
                let text = range ? range.GetText() : "";
                if (!text)
                {
                    text = doc.GetCurrentWord();
// Selects the current word so comments can be applied to it.
                    doc.SelectCurrentWord();
                }


                return text;
            });


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


// Initializes a request engine for communicating with the AI model (e.g. Chat, Translation).
            let requestEngine = AI.Request.create(AI.ActionType.Chat);
            if (!requestEngine)
                return;


            let isSendedEndLongAction = false;
// Marks the end of a logical group or block action in the editor.
            async function checkEndAction() {
                if (!isSendedEndLongAction) {
// Marks the end of a logical group or block action in the editor.
                    await Asc.Editor.callMethod("EndAction", ["Block", "AI (" + requestEngine.modelUI.name + ")"]);
                    isSendedEndLongAction = true
                }
            }


// Starts a block action in the editor, used for undo/redo 
            await Asc.Editor.callMethod("StartAction", ["Block", "AI (" + requestEngine.modelUI.name + ")"]);
// Starts a block action in the editor, used for undo/redo
            await Asc.Editor.callMethod("StartAction", ["GroupActions"]);


            if (isFootnote)
            {
                let addFootnote = true;
// Sends a prompt to the AI model and processes the response via callback
                let result = await requestEngine.chatRequest(argPromt, false, async function(data) {
                    if (!data)
                        return;


// Marks the end of a block action in the editor.
                    await checkEndAction();
                    Asc.scope.data = data;
                    Asc.scope.model = requestEngine.modelUI.name;


                    if (addFootnote)
                    {
// Executes a block of code inside the editor's context using the  office-js API.
                        await Asc.Editor.callCommand(function(){
                            Api.GetDocument().AddFootnote();
                        });
                        addFootnote = false;
                    }
// Inserts the AI-generated result into the document at the current selection or cursor.
                    await Asc.Library.PasteText(data);
                });
            }
            else 
            {
                let commentId = null;
// Sends a prompt to the AI model and processes the response via callback.
                let result = await requestEngine.chatRequest(argPromt, false, async function(data) {
                    if (!data)
                        return;


// Marks the end of a block action in the editor.
                    await checkEndAction();
                    Asc.scope.data = data;
                    Asc.scope.model = requestEngine.modelUI.name;
                    Asc.scope.commentId = commentId;


// Executes a block of code inside the editor's context using the office-js API.
                    commentId = await Asc.Editor.callCommand(function(){
                        let doc = Api.GetDocument();


                        let commentId = Asc.scope.commentId;
                        if (!commentId)
                        {
// Gets the current selected text range.
                            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;
                    });
                });
            }


// Marks the end of a  block action in the editor.
            await checkEndAction();
// Marks the end of a block action in the editor.
            await Asc.Editor.callMethod("EndAction", ["GroupActions"]);
        };


        return func;
    }

Posvećeni smo tome da pratimo savremenu tehnologiju, osiguravajući da naš pametan AI agent nastavi da se razvija uz potrebe modernog digitalnog sveta. Kreiranjem vlastitih prilagođenih funkcija, možete proširiti njegove mogućnosti da odgovaraju vašim preciznim, jedinstvenim zahtevima. Radujemo se vašoj kreativnosti i idejama.

Ako imate bilo kakva pitanja, sugestije ili povratne informacije, slobodno nam se obratite. Uvek smo spremni da sarađujemo i pomognemo vam da oživite svoju viziju! Srećno u vašim istraživačkim poduhvatima!

Create your free ONLYOFFICE account

View, edit and collaborate on docs, sheets, slides, forms, and PDF files online.