HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/LC/api/server/utils/citations.js
const citationRegex = /\[\^\d+?\^\]/g;
const regex = / \[.*?]\(.*?\)/g;

/** Helper function to escape special characters in regex
 * @param {string} string - The string to escape.
 * @returns {string} The escaped string.
 */
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

const getCitations = (res) => {
  const adaptiveCards = res.details.adaptiveCards;
  const textBlocks = adaptiveCards && adaptiveCards[0].body;
  if (!textBlocks) {
    return '';
  }
  let links = textBlocks[textBlocks.length - 1]?.text.match(regex);
  if (links?.length === 0 || !links) {
    return '';
  }
  links = links.map((link) => link.trim());
  return links.join('\n - ');
};

const citeText = (res, noLinks = false) => {
  let result = res.text || res;
  const citations = Array.from(new Set(result.match(citationRegex)));
  if (citations?.length === 0) {
    return result;
  }

  if (noLinks) {
    citations.forEach((citation) => {
      const digit = citation.match(/\d+?/g)[0];
      // result = result.replaceAll(citation, `<sup>[${digit}](#)  </sup>`);
      result = result.replaceAll(citation, `[^${digit}^](#)`);
    });

    return result;
  }

  let sources = res.details.sourceAttributions;
  if (sources?.length === 0) {
    return result;
  }
  sources = sources.map((source) => source.seeMoreUrl);

  citations.forEach((citation) => {
    const digit = citation.match(/\d+?/g)[0];
    result = result.replaceAll(citation, `[^${digit}^](${sources[digit - 1]})`);
    // result = result.replaceAll(citation, `<sup>[${digit}](${sources[digit - 1]})  </sup>`);
  });

  return result;
};

module.exports = { getCitations, citeText, escapeRegExp };