UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

158 lines (157 loc) • 6.11 kB
/** * DevExtreme (esm/__internal/ui/html_editor/utils/ai.js) * Version: 25.2.3 * Build date: Fri Dec 12 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import localizationMessage from "../../../../common/core/localization/message"; import { capitalize } from "../../../core/utils/capitalize"; export const AI_DIALOG_ASKAI_COMMAND_NAME = "askAI"; export const AI_DIALOG_CUSTOM_COMMAND_NAME = "custom"; export const commandMessageKeys = { summarize: "dxHtmlEditor-aiCommandSummarize", proofread: "dxHtmlEditor-aiCommandProofread", expand: "dxHtmlEditor-aiCommandExpand", shorten: "dxHtmlEditor-aiCommandShorten", changeStyle: "dxHtmlEditor-aiCommandChangeStyle", changeTone: "dxHtmlEditor-aiCommandChangeTone", translate: "dxHtmlEditor-aiCommandTranslate", askAI: "dxHtmlEditor-aiCommandAskAI" }; export const getDefaultCommandName = name => { const key = commandMessageKeys[name]; if (key) { return localizationMessage.format(key) } return capitalize(name) }; 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 getLocalizedCommandOption = command => option => localizationMessage.format(`dxHtmlEditor-aiCommand${capitalize(command)}${capitalize(option)}`); export const getDefaultOptionsByCommand = command => { const getLocalizedOption = getLocalizedCommandOption(command); const commandToOptionsMap = { changeStyle: htmlEditorAIChangeStyleOptions.map(getLocalizedOption), changeTone: htmlEditorAIChangeToneOptions.map(getLocalizedOption), translate: htmlEditorAITranslateOptions.map(getLocalizedOption) }; return commandToOptionsMap[command] }; const createDefinitionFromString = commandName => { const text = getDefaultCommandName(commandName); const defaultOptions = getDefaultOptionsByCommand(commandName); return { id: commandName, text: text, name: commandName, options: defaultOptions } }; const createDefinitionFromObject = (id, name, text, rawOptions, prompt) => { const capitalizedRaw = null === rawOptions || void 0 === rawOptions ? void 0 : rawOptions.map(capitalize); const options = capitalizedRaw ?? getDefaultOptionsByCommand(name); const displayText = text ?? getDefaultCommandName(name); const definition = { id: id, name: name, text: displayText, options: options, prompt: prompt }; return definition }; export 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 }; export const getAICommandName = uiCommandName => aiCommandNames[uiCommandName]; 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 ("askAI" === uiCommandName) { customPrompt = askAIPrompt ?? "" } else if ("custom" === uiCommandName) { customPrompt = (null === getCustomPrompt || void 0 === getCustomPrompt ? void 0 : getCustomPrompt(option)) ?? "" } return customPrompt }; export 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 } } }; export const hasInvalidCustomCommand = commandsMap => Object.keys(commandsMap).some((command => command.startsWith("custom") && !commandsMap[command].prompt));