UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

158 lines (157 loc) • 6.51 kB
/** * DevExtreme (cjs/__internal/ui/html_editor/utils/ai.js) * Version: 25.1.3 * Build date: Wed Jun 25 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasInvalidCustomCommand = exports.getDefaultOptionsByCommand = exports.getAICommandName = exports.defaultCommandNames = exports.buildCommandsMap = exports.buildAICommandParams = exports.AI_DIALOG_CUSTOM_COMMAND_NAME = exports.AI_DIALOG_ASKAI_COMMAND_NAME = void 0; var _capitalize = require("../../../core/utils/capitalize"); const AI_DIALOG_ASKAI_COMMAND_NAME = exports.AI_DIALOG_ASKAI_COMMAND_NAME = "askAI"; const AI_DIALOG_CUSTOM_COMMAND_NAME = exports.AI_DIALOG_CUSTOM_COMMAND_NAME = "custom"; const defaultCommandNames = exports.defaultCommandNames = { summarize: "Summarize", proofread: "Proofread", expand: "Expand", shorten: "Shorten", changeStyle: "Change Style", changeTone: "Change Tone", translate: "Translate", askAI: "Ask AI" }; const htmlEditorAIChangeStyleOptions = ["formal", "informal", "technical", "business", "creative", "journalistic", "academic", "persuasive", "narrative", "expository", "descriptive", "conversational"]; const htmlEditorAIChangeToneOptions = ["professional", "casual", "straightforward", "confident", "friendly"]; const htmlEditorAITranslateOptions = ["arabic", "chinese", "english", "french", "german", "japanese", "spanish"]; const aiCommandNames = { summarize: "summarize", proofread: "proofread", expand: "expand", shorten: "shorten", changeStyle: "changeStyle", changeTone: "changeTone", translate: "translate", askAI: "execute", custom: "execute" }; const getDefaultOptionsByCommand = command => { const commandToOptionsMap = { changeStyle: htmlEditorAIChangeStyleOptions, changeTone: htmlEditorAIChangeToneOptions, translate: htmlEditorAITranslateOptions }; return commandToOptionsMap[command] }; exports.getDefaultOptionsByCommand = getDefaultOptionsByCommand; const createDefinitionFromString = commandName => { var _getDefaultOptionsByC; const text = defaultCommandNames[commandName] ?? (0, _capitalize.capitalize)(commandName); const defaultOptions = null === (_getDefaultOptionsByC = getDefaultOptionsByCommand(commandName)) || void 0 === _getDefaultOptionsByC ? void 0 : _getDefaultOptionsByC.map(_capitalize.capitalize); return { id: commandName, text: text, name: commandName, options: defaultOptions } }; const createDefinitionFromObject = (id, name, text, rawOptions, prompt) => { var _getDefaultOptionsByC2; const capitalizedRaw = null === rawOptions || void 0 === rawOptions ? void 0 : rawOptions.map(_capitalize.capitalize); const options = capitalizedRaw ?? (null === (_getDefaultOptionsByC2 = getDefaultOptionsByCommand(name)) || void 0 === _getDefaultOptionsByC2 ? void 0 : _getDefaultOptionsByC2.map(_capitalize.capitalize)); const displayText = text ?? defaultCommandNames[name] ?? (0, _capitalize.capitalize)(name); const definition = { id: id, name: name, text: displayText, options: options, prompt: prompt }; return definition }; const buildCommandsMap = commands => { const map = {}; let index = 0; null === commands || void 0 === commands || commands.forEach((command => { if ("string" === typeof command) { map[command] = createDefinitionFromString(command) } else { const { name: name, text: text, options: options } = command; const isCustom = "custom" === name; const { prompt: prompt } = command; const id = `${name}${isCustom?index:""}`; map[id] = createDefinitionFromObject(id, name, text, options, prompt); if (isCustom) { index += 1 } } })); return map }; exports.buildCommandsMap = buildCommandsMap; const getAICommandName = uiCommandName => aiCommandNames[uiCommandName]; exports.getAICommandName = getAICommandName; const getUserCustomPrompt = function(uiCommandName) { let askAIPrompt = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : ""; let option = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : ""; let getCustomPrompt = arguments.length > 3 ? arguments[3] : void 0; let customPrompt = ""; if (uiCommandName === AI_DIALOG_ASKAI_COMMAND_NAME) { customPrompt = askAIPrompt ?? "" } else if (uiCommandName === AI_DIALOG_CUSTOM_COMMAND_NAME) { customPrompt = (null === getCustomPrompt || void 0 === getCustomPrompt ? void 0 : getCustomPrompt(option)) ?? "" } return customPrompt }; const buildAICommandParams = (uiCommandName, askAIPrompt, option, getCustomPrompt, payloadText) => { const text = payloadText ?? ""; switch (uiCommandName) { case "expand": case "proofread": case "summarize": case "shorten": return { text: text }; case "changeStyle": { const params = { text: text, writingStyle: option }; return params } case "changeTone": { const params = { text: text, tone: option }; return params } case "translate": { const params = { text: text, lang: option }; return params } default: { const userPrompt = getUserCustomPrompt(uiCommandName, askAIPrompt, option, getCustomPrompt); const params = { text: `Text: "${text}". ${userPrompt}`.trim() }; return params } } }; exports.buildAICommandParams = buildAICommandParams; const hasInvalidCustomCommand = commandsMap => Object.keys(commandsMap).some((command => command.startsWith("custom") && !commandsMap[command].prompt)); exports.hasInvalidCustomCommand = hasInvalidCustomCommand;