2024-07-12 15:25:41 +00:00
|
|
|
import type { Message } from 'ai';
|
2024-07-24 15:43:32 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { classNames } from '~/utils/classNames';
|
2024-07-12 15:25:41 +00:00
|
|
|
import { AssistantMessage } from './AssistantMessage';
|
|
|
|
import { UserMessage } from './UserMessage';
|
|
|
|
|
|
|
|
interface MessagesProps {
|
|
|
|
id?: string;
|
|
|
|
className?: string;
|
|
|
|
isStreaming?: boolean;
|
|
|
|
messages?: Message[];
|
|
|
|
}
|
|
|
|
|
2024-07-24 13:47:48 +00:00
|
|
|
export const Messages = React.forwardRef<HTMLDivElement, MessagesProps>((props: MessagesProps, ref) => {
|
2024-07-12 15:25:41 +00:00
|
|
|
const { id, isStreaming = false, messages = [] } = props;
|
|
|
|
|
|
|
|
return (
|
2024-07-24 13:47:48 +00:00
|
|
|
<div id={id} ref={ref} className={props.className}>
|
2024-07-12 15:25:41 +00:00
|
|
|
{messages.length > 0
|
2024-08-08 13:56:36 +00:00
|
|
|
? messages.map((message, index) => {
|
2024-07-12 15:25:41 +00:00
|
|
|
const { role, content } = message;
|
2024-08-08 13:56:36 +00:00
|
|
|
const isUserMessage = role === 'user';
|
|
|
|
const isFirst = index === 0;
|
|
|
|
const isLast = index === messages.length - 1;
|
2024-07-12 15:25:41 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2024-08-08 13:56:36 +00:00
|
|
|
key={index}
|
|
|
|
className={classNames('flex gap-4 p-6 w-full rounded-[calc(0.75rem-1px)]', {
|
|
|
|
'bg-bolt-elements-messages-background': isUserMessage || !isStreaming || (isStreaming && !isLast),
|
|
|
|
'bg-gradient-to-b from-bolt-elements-messages-background from-30% to-transparent':
|
|
|
|
isStreaming && isLast,
|
2024-07-12 15:25:41 +00:00
|
|
|
'mt-4': !isFirst,
|
|
|
|
})}
|
|
|
|
>
|
2024-08-08 13:56:36 +00:00
|
|
|
{isUserMessage && (
|
2024-07-12 15:25:41 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
2024-08-08 13:56:36 +00:00
|
|
|
'flex items-center justify-center min-w-[34px] min-h-[34px] bg-white text-gray-600 rounded-full p-1 self-start',
|
2024-07-12 15:25:41 +00:00
|
|
|
)}
|
|
|
|
>
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="i-ph:user-fill text-xl"></div>
|
2024-07-17 18:54:46 +00:00
|
|
|
</div>
|
2024-08-08 13:56:36 +00:00
|
|
|
)}
|
|
|
|
<div className="grid grid-col-1 w-full">
|
|
|
|
{isUserMessage ? <UserMessage content={content} /> : <AssistantMessage content={content} />}
|
2024-07-12 15:25:41 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
: null}
|
2024-08-08 13:56:36 +00:00
|
|
|
{isStreaming && (
|
|
|
|
<div className="text-center w-full text-bolt-elements-textSecondary i-svg-spinners:3-dots-fade text-4xl mt-4"></div>
|
|
|
|
)}
|
2024-07-12 15:25:41 +00:00
|
|
|
</div>
|
|
|
|
);
|
2024-07-24 13:47:48 +00:00
|
|
|
});
|