UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

228 lines (227 loc) • 7.7 kB
/** * DevExtreme (esm/__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/ */ import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["type"]; import messageLocalization from "../../../../common/core/localization/message"; import { isObject } from "../../../../core/utils/type"; import { custom } from "../../../../ui/dialog"; import { current, isCompact, isFluent } from "../../../../ui/themes"; import { AI_ASSISTANT_AUTHOR_ID, AI_ASSISTANT_CONFIRM_DIALOG_COMPACT_WIDTH, AI_ASSISTANT_CONFIRM_DIALOG_WIDTH } from "./const"; export const isAIMessage = message => { var _message$author; return (null === (_message$author = message.author) || void 0 === _message$author ? void 0 : _message$author.id) === AI_ASSISTANT_AUTHOR_ID }; export const isUserMessage = (message, userId) => { var _message$author2; return (null === (_message$author2 = message.author) || void 0 === _message$author2 ? void 0 : _message$author2.id) === userId }; export const isEnabledOption = (optionName, value) => optionName.startsWith("aiAssistant.enabled") || "aiAssistant" === optionName && isObject(value) && "enabled" in value; export const isTitleOption = (optionName, value) => optionName.startsWith("aiAssistant.title") || "aiAssistant" === optionName && isObject(value) && "title" in value; export const isPopupOptions = (optionName, value) => optionName.startsWith("aiAssistant.popup") || "aiAssistant" === optionName && isObject(value) && "popup" in value; export const isChatOptions = (optionName, value) => optionName.startsWith("aiAssistant.chat") || "aiAssistant" === optionName && isObject(value) && "chat" in value; export const hasCommandErrors = commands => !!(null !== commands && void 0 !== commands && commands.some(_ref => { let { status: status } = _ref; return "failure" === status })); export const hasAbortedCommands = commands => !!(null !== commands && void 0 !== commands && commands.some(_ref2 => { let { status: status } = _ref2; return "aborted" === status })); export const getMessageStatus = commands => { if (hasCommandErrors(commands) || hasAbortedCommands(commands)) { return "failure" } return "success" }; export 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 }; 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, "_") }; export 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 }; const getApplyButtonConfig = () => { if (isFluent(current())) { return { stylingMode: "outlined", type: "normal" } } return {} }; const getCancelButtonConfig = () => { if (isFluent(current())) { return { stylingMode: "contained", type: "default" } } return {} }; const getConfirmDialogWidth = () => { if (isCompact(current())) { return AI_ASSISTANT_CONFIRM_DIALOG_COMPACT_WIDTH } return AI_ASSISTANT_CONFIRM_DIALOG_WIDTH }; export const createConfirmDialog = options => custom(Object.assign({ messageHtml: messageLocalization.format("dxDataGrid-aiAssistantAbortConfirmText"), showTitle: false, dragEnabled: false, width: getConfirmDialogWidth(), buttons: [Object.assign({ text: messageLocalization.format("No"), onClick: () => false }, getCancelButtonConfig()), Object.assign({ text: messageLocalization.format("Yes"), onClick: () => true }, getApplyButtonConfig())] }, options));