Spaces:
Sleeping
Sleeping
File size: 1,113 Bytes
a1c5622 7286745 a1c5622 ba9285c a1c5622 ba9285c a1c5622 cef4233 a1c5622 0fd8446 cef4233 a1c5622 7286745 a1c5622 ba9285c a1c5622 |
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 |
import { Message } from '@prisma/client';
import { MessageUI } from '../types';
/**
* The Message we saved to database consists of a prompt and a response
* for the API to use, we need to break them to 2 messages, User and Assistant(if responded)
*/
export const convertDBMessageToAPIMessage = (
messages: Message[],
): MessageUI[] => {
return messages.reduce((acc, message) => {
const { id, mediaUrl, prompt, result, chatId } = message;
// fileName could be null if it's example media url
const [prefix, fileName] = mediaUrl.split(`${chatId}/`);
if (mediaUrl && prompt) {
acc.push({
id: id + '-user',
role: 'user',
content: prompt,
media: [
fileName
? prefix +
`${chatId}/` +
encodeURIComponent(decodeURIComponent(fileName))
: mediaUrl,
],
});
}
if (result && result.type === 'final_code') {
acc.push({
id: id + '-assistant',
role: 'assistant',
content: result?.payload.code,
});
}
return acc;
}, [] as MessageUI[]);
};
|