@atlaskit/editor-plugin-mentions
Version:
Mentions plugin for @atlaskit/editor-core
81 lines (80 loc) • 3.03 kB
JavaScript
var PASS_THROUGH_PROMPT_INLINE_NODE_TYPES = new Set(['hardBreak', 'inlineCard', 'mention', 'text']);
var isAgentMention = function isAgentMention(node) {
var _node$attrs;
return node.type === 'mention' && ((_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.userType) === 'APP';
};
var isAllowedAgentMention = function isAllowedAgentMention(node, agentMentionLocalId) {
var _node$attrs2;
return !isAgentMention(node) || ((_node$attrs2 = node.attrs) === null || _node$attrs2 === void 0 ? void 0 : _node$attrs2.localId) === agentMentionLocalId;
};
var getStatusText = function getStatusText(node) {
var _node$attrs3;
var text = (_node$attrs3 = node.attrs) === null || _node$attrs3 === void 0 ? void 0 : _node$attrs3.text;
return typeof text === 'string' && text ? text : null;
};
var getDateText = function getDateText(node) {
var _node$attrs4;
var timestamp = (_node$attrs4 = node.attrs) === null || _node$attrs4 === void 0 ? void 0 : _node$attrs4.timestamp;
if (typeof timestamp !== 'string' || !timestamp) {
return null;
}
var date = new Date(Number(timestamp));
if (Number.isNaN(date.getTime())) {
return null;
}
return date.toISOString().slice(0, 10);
};
var toPromptInlineNode = function 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') {
var text = getStatusText(node);
return text ? {
type: 'text',
text: text
} : null;
}
if (node.type === 'date') {
var _text = getDateText(node);
return _text ? {
type: 'text',
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 var getAgentMentionParentContext = function getAgentMentionParentContext(parentNode, agentMentionLocalId) {
var _parentAdf$content;
var parentAdf = parentNode.toJSON();
var supportedInlineContent = ((_parentAdf$content = parentAdf.content) !== null && _parentAdf$content !== void 0 ? _parentAdf$content : []).flatMap(function (node) {
var promptNode = toPromptInlineNode(node, agentMentionLocalId);
return promptNode ? [promptNode] : [];
});
return {
type: 'doc',
version: 1,
content: [{
type: 'paragraph',
content: supportedInlineContent
}]
};
};