Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"helper.min.js","sources":["../src/helper.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * The helper module or AI Subsystem.\n *\n * @module     core_ai/helper\n * @copyright  2024 Huong Nguyen <huongnv13@gmail.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nexport default class AIHelper {\n    /**\n     * Replace double line breaks with <br> and with </p><p> for paragraphs.\n     * This is to handle the difference in response from the AI to what is expected by the editor.\n     *\n     * @param {String} text The text to replace.\n     * @returns {String}\n     */\n    static replaceLineBreaks(text) {\n        // Replace double line breaks with </p><p> for paragraphs\n        const textWithParagraphs = text.replace(/\\n{2,}|\\r\\n/g, '<br/><br/>');\n\n        // Replace remaining single line breaks with <br> tags\n        const textWithBreaks = textWithParagraphs.replace(/\\n/g, '<br/>');\n\n        // Add opening and closing <p> tags to wrap the entire content\n        return `<p>${textWithBreaks}</p>`;\n    }\n\n    /**\n     * Replace markdown formatting.\n     * Even when asked not to, AI models will sometimes return markdown.\n     *\n     * @param {String} text The text to replace.\n     * @returns {String}\n     */\n    static replaceMarkdown(text) {\n        // Replace markdown bold formatting HTML equivalent.\n        const textWithMarkdown = text.replace(/\\*\\*(.*?)\\*\\*/g, '<strong>$1</strong>');\n\n        return textWithMarkdown;\n    }\n\n    /**\n     * Format the response provided by the AI model.\n     *\n     * @param {String} text The text to format.\n     * @returns {String}\n     */\n    static formatResponse(text) {\n        let formattedText = this.replaceLineBreaks(text) ;\n        formattedText = this.replaceMarkdown(formattedText);\n\n        return formattedText;\n    }\n}\n"],"names":["text","textWithBreaks","replace","formattedText","this","replaceLineBreaks","replaceMarkdown"],"mappings":";;;;;;;;+BA8B6BA,YAKfC,eAHqBD,KAAKE,QAAQ,eAAgB,cAGdA,QAAQ,MAAO,4BAG5CD,8CAUMD,aAEMA,KAAKE,QAAQ,iBAAkB,6CAWtCF,UACdG,cAAgBC,KAAKC,kBAAkBL,aAC3CG,cAAgBC,KAAKE,gBAAgBH,eAE9BA"}