Skip to content

The open alternative to Salesforce, designed for AI.

730 B
Raw Blame
import { escapeMarkdownForChatReference } from '@/ai/utils/escapeMarkdownForChatReference';
import { formatChatReference } from '@/ai/utils/formatChatReference';
import { getChatReferenceSegments } from '@/ai/utils/getChatReferenceSegments';

export const protectChatReferencesForMarkdown = (text: string): string => {
  const segments = getChatReferenceSegments(text);

  if (segments.length === 1 && typeof segments[0] === 'string') {
    return segments[0];
  }

  return segments
    .map((segment) =>
      typeof segment === 'string'
        ? segment
        : formatChatReference({
            ...segment,
            displayName: escapeMarkdownForChatReference(segment.displayName),
          }),
    )
    .join('');
};