UNPKG

@atlaskit/editor-plugin-mentions

Version:

Mentions plugin for @atlaskit/editor-core

81 lines (80 loc) 2.89 kB
const PASS_THROUGH_PROMPT_INLINE_NODE_TYPES = new Set(['hardBreak', 'inlineCard', 'mention', 'text']); const isAgentMention = node => { var _node$attrs; return node.type === 'mention' && ((_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.userType) === 'APP'; }; const isAllowedAgentMention = (node, agentMentionLocalId) => { var _node$attrs2; return !isAgentMention(node) || ((_node$attrs2 = node.attrs) === null || _node$attrs2 === void 0 ? void 0 : _node$attrs2.localId) === agentMentionLocalId; }; const getStatusText = node => { var _node$attrs3; const text = (_node$attrs3 = node.attrs) === null || _node$attrs3 === void 0 ? void 0 : _node$attrs3.text; return typeof text === 'string' && text ? text : null; }; const getDateText = node => { var _node$attrs4; const timestamp = (_node$attrs4 = node.attrs) === null || _node$attrs4 === void 0 ? void 0 : _node$attrs4.timestamp; if (typeof timestamp !== 'string' || !timestamp) { return null; } const date = new Date(Number(timestamp)); if (Number.isNaN(date.getTime())) { return null; } return date.toISOString().slice(0, 10); }; const toPromptInlineNode = (node, agentMentionLocalId) => { if (!isAllowedAgentMention(node, agentMentionLocalId)) { return null; } if (PASS_THROUGH_PROMPT_INLINE_NODE_TYPES.has(node.type)) { return node; } if (node.type === 'status') { const text = getStatusText(node); return text ? { type: 'text', text } : null; } if (node.type === 'date') { const text = getDateText(node); return text ? { type: 'text', text } : null; } return null; }; /** * Returns the direct parent content of an inserted agent mention as prompt ADF. * * The chat input supports only a subset of editor inline ADF, so this keeps prompt content * intentionally narrow: * - `text`, `inlineCard`, and `hardBreak` pass through unchanged. * - people mentions pass through unchanged. * - only the invoked agent mention is kept, identified by `localId`; other agent mentions are dropped. * - `status` becomes its label text. * - `date` becomes ISO date text. * - everything else is dropped. * * This only reads direct parent content. It does not recursively pull content out of nested or * unsupported nodes. */ export const getAgentMentionParentContext = (parentNode, agentMentionLocalId) => { var _parentAdf$content; const parentAdf = parentNode.toJSON(); const supportedInlineContent = ((_parentAdf$content = parentAdf.content) !== null && _parentAdf$content !== void 0 ? _parentAdf$content : []).flatMap(node => { const promptNode = toPromptInlineNode(node, agentMentionLocalId); return promptNode ? [promptNode] : []; }); return { type: 'doc', version: 1, content: [{ type: 'paragraph', content: supportedInlineContent }] }; };