vision-agent / lib /utils /message.ts
wuyiqunLu
feat: add responseBody field and serving data to hooks (#93)
bc1cf4e unverified
raw
history blame
796 Bytes
import { Message } from '@prisma/client';
import { MessageAssistantResponse, 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 } = message;
if (mediaUrl && prompt) {
acc.push({
id: id + '-user',
role: 'user',
content: prompt,
});
}
if (result) {
acc.push({
id: id + '-assistant',
role: 'assistant',
content: result?.payload.code,
});
}
return acc;
}, [] as MessageUI[]);
};