UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

254 lines (251 loc) • 8.87 kB
/** * DevExtreme (cjs/__internal/grids/grid_core/ai_assistant/utils.js) * Version: 26.1.3 * Build date: Wed Jun 10 2026 * * Copyright (c) 2012 - 2026 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.isUserMessage = exports.isTitleOption = exports.isPopupOptions = exports.isEnabledOption = exports.isChatOptions = exports.isAIMessage = exports.hoistSchemaRefs = exports.hasCommandErrors = exports.hasAbortedCommands = exports.getMessageStatus = exports.expandTypeArraysToAnyOf = exports.createConfirmDialog = void 0; var _message = _interopRequireDefault(require("../../../../common/core/localization/message")); var _type = require("../../../../core/utils/type"); var _dialog = require("../../../../ui/dialog"); var _themes = require("../../../../ui/themes"); var _const = require("./const"); const _excluded = ["type"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } function _objectWithoutPropertiesLoose(r, e) { if (null == r) { return {} } var t = {}; for (var n in r) { if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) { continue } t[n] = r[n] } } return t } const isAIMessage = message => { var _message$author; return (null === (_message$author = message.author) || void 0 === _message$author ? void 0 : _message$author.id) === _const.AI_ASSISTANT_AUTHOR_ID }; exports.isAIMessage = isAIMessage; const isUserMessage = (message, userId) => { var _message$author2; return (null === (_message$author2 = message.author) || void 0 === _message$author2 ? void 0 : _message$author2.id) === userId }; exports.isUserMessage = isUserMessage; const isEnabledOption = (optionName, value) => optionName.startsWith("aiAssistant.enabled") || "aiAssistant" === optionName && (0, _type.isObject)(value) && "enabled" in value; exports.isEnabledOption = isEnabledOption; const isTitleOption = (optionName, value) => optionName.startsWith("aiAssistant.title") || "aiAssistant" === optionName && (0, _type.isObject)(value) && "title" in value; exports.isTitleOption = isTitleOption; const isPopupOptions = (optionName, value) => optionName.startsWith("aiAssistant.popup") || "aiAssistant" === optionName && (0, _type.isObject)(value) && "popup" in value; exports.isPopupOptions = isPopupOptions; const isChatOptions = (optionName, value) => optionName.startsWith("aiAssistant.chat") || "aiAssistant" === optionName && (0, _type.isObject)(value) && "chat" in value; exports.isChatOptions = isChatOptions; const hasCommandErrors = commands => !!(null !== commands && void 0 !== commands && commands.some(_ref => { let { status: status } = _ref; return "failure" === status })); exports.hasCommandErrors = hasCommandErrors; const hasAbortedCommands = commands => !!(null !== commands && void 0 !== commands && commands.some(_ref2 => { let { status: status } = _ref2; return "aborted" === status })); exports.hasAbortedCommands = hasAbortedCommands; const getMessageStatus = commands => { if (hasCommandErrors(commands) || hasAbortedCommands(commands)) { return "failure" } return "success" }; exports.getMessageStatus = getMessageStatus; const expandTypeArraysToAnyOf = schema => { const SCHEMA_TRAVERSAL_KEYS = ["properties", "items", "anyOf", "oneOf", "allOf", "additionalProperties", "additionalItems", "$defs", "definitions"]; const SCHEMA_MAP_KEYS = new Set(["properties", "$defs", "definitions"]); const transformNested = (key, value) => { if (Array.isArray(value)) { return value.map(item => expandTypeArraysToAnyOf(item)) } if (!value || "object" !== typeof value) { return value } if (SCHEMA_MAP_KEYS.has(key)) { return Object.fromEntries(Object.entries(value).map(_ref3 => { let [k, v] = _ref3; return [k, expandTypeArraysToAnyOf(v)] })) } return expandTypeArraysToAnyOf(value) }; if (!schema || "object" !== typeof schema) { return schema } const result = Object.assign({}, schema); if (Array.isArray(result.type)) { const { type: type } = result, rest = _objectWithoutPropertiesLoose(result, _excluded); return Object.assign({ anyOf: type.map(t => ({ type: t })) }, rest) } for (const key of SCHEMA_TRAVERSAL_KEYS) { if (key in result) { result[key] = transformNested(key, result[key]) } } return result }; exports.expandTypeArraysToAnyOf = expandTypeArraysToAnyOf; const resolveJsonPointer = (schema, pointer) => { if (!pointer.startsWith("#/")) { return } const segments = pointer.slice(2).split("/"); let currentNode = schema; for (const segment of segments) { if (!currentNode || "object" !== typeof currentNode) { return } currentNode = currentNode[segment] } return currentNode }; const collectAllRefs = (node, refs) => { if (!node || "object" !== typeof node) { return } if (Array.isArray(node)) { for (const item of node) { collectAllRefs(item, refs) } return } const obj = node; if ("string" === typeof obj.$ref) { refs.add(obj.$ref) } for (const value of Object.values(obj)) { collectAllRefs(value, refs) } }; const rewriteRefs = (node, refMap) => { if (!node || "object" !== typeof node) { return } if (Array.isArray(node)) { for (const item of node) { rewriteRefs(item, refMap) } return } const obj = node; if ("string" === typeof obj.$ref) { const newRef = refMap.get(obj.$ref); if (newRef) { obj.$ref = newRef } } for (const value of Object.values(obj)) { rewriteRefs(value, refMap) } }; const defNameFromRef = ref => { if (ref.startsWith("#/$defs/")) { return ref.slice(8) } return ref.slice(2).replace(/\//g, "_") }; const hoistSchemaRefs = items => { const mergedDefs = {}; for (const { prefix: prefix, schema: schema } of items) { const refs = new Set; collectAllRefs(schema, refs); if (0 === refs.size) { continue } const refMap = new Map; for (const ref of refs) { var _schema$$defs; const baseName = defNameFromRef(ref); const prefixedName = `${prefix}_${baseName}`; const newRef = `#/$defs/${prefixedName}`; refMap.set(ref, newRef); const resolved = ref.startsWith("#/$defs/") ? null === (_schema$$defs = schema.$defs) || void 0 === _schema$$defs ? void 0 : _schema$$defs[baseName] : resolveJsonPointer(schema, ref); if (resolved && "object" === typeof resolved) { mergedDefs[prefixedName] = JSON.parse(JSON.stringify(resolved)) } } if (schema.$defs) { delete schema.$defs } rewriteRefs(schema, refMap); for (const prefixedName of new Set(refMap.values())) { const defName = prefixedName.slice(8); rewriteRefs(mergedDefs[defName], refMap) } } return mergedDefs }; exports.hoistSchemaRefs = hoistSchemaRefs; const getApplyButtonConfig = () => { if ((0, _themes.isFluent)((0, _themes.current)())) { return { stylingMode: "outlined", type: "normal" } } return {} }; const getCancelButtonConfig = () => { if ((0, _themes.isFluent)((0, _themes.current)())) { return { stylingMode: "contained", type: "default" } } return {} }; const getConfirmDialogWidth = () => { if ((0, _themes.isCompact)((0, _themes.current)())) { return _const.AI_ASSISTANT_CONFIRM_DIALOG_COMPACT_WIDTH } return _const.AI_ASSISTANT_CONFIRM_DIALOG_WIDTH }; const createConfirmDialog = options => (0, _dialog.custom)(Object.assign({ messageHtml: _message.default.format("dxDataGrid-aiAssistantAbortConfirmText"), showTitle: false, dragEnabled: false, width: getConfirmDialogWidth(), buttons: [Object.assign({ text: _message.default.format("No"), onClick: () => false }, getCancelButtonConfig()), Object.assign({ text: _message.default.format("Yes"), onClick: () => true }, getApplyButtonConfig())] }, options)); exports.createConfirmDialog = createConfirmDialog;