Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.6 KB
Raw Blame
import { AP_FUNCTIONS } from '@activepieces/core-formula';
import { Node, mergeAttributes } from '@tiptap/core';

export const FUNCTION_START_NODE_TYPE = 'function_start';
export const FUNCTION_END_NODE_TYPE = 'function_end';
export const FUNCTION_SEP_NODE_TYPE = 'function_sep';

const BADGE_CLASS =
  'ap-fn-badge inline-flex items-center bg-accent-4 text-accent-11 rounded-md px-[3px] py-[2px] mx-[6px] text-xs font-medium cursor-default select-none whitespace-nowrap';

export const FunctionStartNode = Node.create({
  name: FUNCTION_START_NODE_TYPE,
  group: 'inline',
  inline: true,
  atom: true,
  draggable: true,

  addAttributes() {
    return {
      id: { default: '' },
      functionName: { default: '' },
      broken: { default: false },
    };
  },

  parseHTML() {
    return [{ tag: 'span[data-function-start]' }];
  },

  renderHTML({ node, HTMLAttributes }) {
    const broken = node.attrs.broken === true;
    const fnName = node.attrs.functionName as string;
    const deprecated = AP_FUNCTIONS.find((f) => f.name === fnName)?.deprecated;
    const classes = [BADGE_CLASS];
    if (broken) classes.push('ap-fn-broken');
    if (deprecated) classes.push('ap-fn-deprecated');
    return [
      'span',
      mergeAttributes(HTMLAttributes, {
        'data-function-start': node.attrs.id,
        'data-function-name': fnName,
        class: classes.join(' '),
        contenteditable: 'false',
        ...(deprecated?.replacement
          ? { title: `Deprecated — use ${deprecated.replacement} instead` }
          : {}),
      }),
      `${fnName}(`,
    ];
  },
});

export const FunctionArgSeparatorNode = Node.create({
  name: FUNCTION_SEP_NODE_TYPE,
  group: 'inline',
  inline: true,
  atom: true,

  addAttributes() {
    return {
      openId: { default: '' },
    };
  },

  parseHTML() {
    return [{ tag: 'span[data-function-sep]' }];
  },

  renderHTML({ node, HTMLAttributes }) {
    return [
      'span',
      mergeAttributes(HTMLAttributes, {
        'data-function-sep': node.attrs.openId,
        class: BADGE_CLASS,
        contenteditable: 'false',
      }),
      ';',
    ];
  },
});

export const FunctionEndNode = Node.create({
  name: FUNCTION_END_NODE_TYPE,
  group: 'inline',
  inline: true,
  atom: true,
  draggable: true,

  addAttributes() {
    return {
      openId: { default: '' },
    };
  },

  parseHTML() {
    return [{ tag: 'span[data-function-end]' }];
  },

  renderHTML({ node, HTMLAttributes }) {
    return [
      'span',
      mergeAttributes(HTMLAttributes, {
        'data-function-end': node.attrs.openId,
        class: BADGE_CLASS,
        contenteditable: 'false',
      }),
      ')',
    ];
  },
});