Spaces:
Sleeping
Sleeping
File size: 1,650 Bytes
6b8f69a |
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 |
import { useMemo } from 'react';
import { MessageBase } from '../types';
const PAIRS: Record<string, string> = {
'β': 'β',
'β': 'β₯',
'β': 'β€',
'β': 'β',
};
const MIDDLE_STARTER = 'β';
const MIDDLE_SEPARATOR = 'βΏ';
export const useCleanedUpMessages = ({ content, role }: MessageBase) => {
return useMemo(() => {
if (role === 'user') {
return {
content,
};
}
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));
return {
logs: cleanedLogs
.join('')
.replace(/β/g, '|')
.split('|\n\n|')
.join('|\n|'),
content: answer.replace('</</ANSWER>', '').replace('</ANSWER>', ''),
};
}, [content, role]);
};
|