devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
150 lines (149 loc) • 5.72 kB
JavaScript
/**
* DevExtreme (esm/__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/
*/
import {
capitalize
} from "../../../core/utils/capitalize";
export const AI_DIALOG_ASKAI_COMMAND_NAME = "askAI";
export const AI_DIALOG_CUSTOM_COMMAND_NAME = "custom";
export const 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"
};
export const getDefaultOptionsByCommand = command => {
const commandToOptionsMap = {
changeStyle: htmlEditorAIChangeStyleOptions,
changeTone: htmlEditorAIChangeToneOptions,
translate: htmlEditorAITranslateOptions
};
return commandToOptionsMap[command]
};
const createDefinitionFromString = commandName => {
var _getDefaultOptionsByC;
const text = defaultCommandNames[commandName] ?? capitalize(commandName);
const defaultOptions = null === (_getDefaultOptionsByC = getDefaultOptionsByCommand(commandName)) || void 0 === _getDefaultOptionsByC ? void 0 : _getDefaultOptionsByC.map(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);
const options = capitalizedRaw ?? (null === (_getDefaultOptionsByC2 = getDefaultOptionsByCommand(name)) || void 0 === _getDefaultOptionsByC2 ? void 0 : _getDefaultOptionsByC2.map(capitalize));
const displayText = text ?? defaultCommandNames[name] ?? capitalize(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));