File size: 2,901 Bytes
0d6f04b
 
6b8f69a
 
 
 
 
 
 
 
 
 
 
0d6f04b
 
 
 
 
 
 
 
f665131
 
 
 
 
 
 
0d6f04b
 
 
f665131
 
 
0d6f04b
 
 
 
 
 
5d7d435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b8f69a
 
5d7d435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b8f69a
5d7d435
 
 
3a22cf3
 
 
 
 
5d7d435
 
3a22cf3
 
0d6f04b
 
 
 
3a22cf3
 
5d7d435
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { MessageBase } from './types';
import { CLEANED_SEPARATOR } from './constants';

const PAIRS: Record<string, string> = {
  '┍': 'β”‘',
  '┝': 'β”₯',
  'β”œ': '─',
  'β”•': 'β”™',
};

const MIDDLE_STARTER = '┝';
const MIDDLE_SEPARATOR = 'β”Ώ';

const ANSWERS_PREFIX = 'answers';
const INPUT_PREFIX = 'input';

export const generateAnswersImageMarkdown = (index: number, url: string) => {
  return `![${ANSWERS_PREFIX}-${index}](${url})`;
};

export const generateInputImageMarkdown = (url: string, index = 0) => {
  if (url.toLowerCase().endsWith('.mp4')) {
    const prefix = 'input-video';
    return `![${INPUT_PREFIX}-${index}](<${url}>)`;
  } else {
    const prefix = 'input';
    return `![${INPUT_PREFIX}-${index}](<${url}>)`;
  }
};

export const cleanInputMessage = (content: string) => {
  return content
    .replace(/!\[input-.*?\)/g, '')
    .replace(/<video[^>]*>.*?<\/video>/g, '');
};

export const cleanAnswerMessage = (content: string) => {
  return content.replace(/!\[answers.*?\.png\)/g, '');
};

export const getCleanedUpMessages = ({
  content,
  role,
}: Pick<MessageBase, 'role' | 'content'>) => {
  if (role === 'user') {
    return {
      content,
    };
  }
  if (content.split(CLEANED_SEPARATOR).length === 2) {
    return {
      logs: content.split(CLEANED_SEPARATOR)[0],
      content: content.split(CLEANED_SEPARATOR)[1],
    };
  }
  const [logs = '', answer = ''] = content.split('<ANSWER>');
  const cleanedLogs = [];
  let left = 0;
  let right = 0;
  while (right < logs.length) {
    if (Object.keys(PAIRS).includes(content[right])) {
      cleanedLogs.push(content.substring(left, right));
      left = right++;
      while (
        right < content.length &&
        PAIRS[content[left]] !== content[right]
      ) {
        right++;
      }
      if (content[left] === MIDDLE_STARTER) {
        // add the text alignment so it can be shown as a table
        const separators = logs
          .substring(left, right)
          .split(MIDDLE_SEPARATOR).length;
        if (separators > 0) {
          cleanedLogs.push(
            Array(separators + 1)
              .fill('|')
              .join(' :- '),
          );
        }
      }
      left = ++right;
    } else {
      right++;
    }
  }
  cleanedLogs.push(content.substring(left, right));
  const [answerText, imagesStr = ''] = answer.split('<VIZ>');
  const [imagesArrayStr, ...rest] = imagesStr.split('</VIZ>');
  const images = imagesArrayStr
    .split('</IMG>')
    .map(str => str.replace('<IMG>', ''))
    .slice(0, -1);
  return {
    logs: cleanedLogs.join('').replace(/β”‚/g, '|').split('|\n\n|').join('|\n|'),
    content:
      answerText.replace('</</ANSWER>', '').replace('</ANSWER>', '') +
      '\n\n' +
      images
        .map((_, index) => generateAnswersImageMarkdown(index, '/loading.gif'))
        .join('') +
      rest.join(''),
    images: images,
  };
};