2023-06-05 19:27:25 +08:00
|
|
|
import { keyframes } from "@emotion/react";
|
2023-06-06 15:13:49 +08:00
|
|
|
import { Center, Text, Flex, Avatar, Accordion, Box, Stack, Container, Divider, ActionIcon, Tooltip } from "@mantine/core";
|
2023-06-05 19:27:25 +08:00
|
|
|
import React from "react";
|
|
|
|
import CodeBlock from "./CodeBlock";
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
import SvgAvatarDevChat from './avatar_devchat.svg';
|
|
|
|
// @ts-ignore
|
|
|
|
import SvgAvatarUser from './avatar_spaceman.png';
|
2023-06-06 15:13:49 +08:00
|
|
|
import { IconCheck, IconCopy } from "@tabler/icons-react";
|
2023-06-05 19:27:25 +08:00
|
|
|
|
|
|
|
const MessageContainer = (props: any) => {
|
2023-06-06 18:13:26 +08:00
|
|
|
const { generating, messages, width, responsed, onRefillClick } = props;
|
2023-06-05 19:27:25 +08:00
|
|
|
|
|
|
|
const DefaultMessage = (<Center>
|
|
|
|
<Text size="lg" color="gray" weight={500}>No messages yet</Text>
|
|
|
|
</Center>);
|
|
|
|
|
2023-06-06 15:13:49 +08:00
|
|
|
const MessageHeader = (props: any) => {
|
2023-06-06 18:13:26 +08:00
|
|
|
const { type, message, contexts } = props;
|
2023-06-06 15:13:49 +08:00
|
|
|
const [refilled, setRefilled] = React.useState(false);
|
2023-06-05 19:27:25 +08:00
|
|
|
return (<Flex
|
|
|
|
m='10px 0 10px 0'
|
|
|
|
gap="sm"
|
|
|
|
justify="flex-start"
|
|
|
|
align="center"
|
|
|
|
direction="row"
|
|
|
|
wrap="wrap">
|
|
|
|
{
|
|
|
|
type === 'bot'
|
|
|
|
? <Avatar
|
|
|
|
color="indigo"
|
|
|
|
size={25}
|
|
|
|
radius="xl"
|
|
|
|
src={SvgAvatarDevChat} />
|
|
|
|
: <Avatar
|
|
|
|
color="cyan"
|
|
|
|
size={25}
|
|
|
|
radius="xl"
|
|
|
|
src={SvgAvatarUser} />
|
|
|
|
}
|
|
|
|
<Text weight='bold'>{type === 'bot' ? 'DevChat' : 'User'}</Text>
|
2023-06-06 15:13:49 +08:00
|
|
|
{type === 'user'
|
|
|
|
? <Tooltip sx={{ padding: '3px', fontSize: 'var(--vscode-editor-font-size)' }} label={refilled ? 'Refilled' : 'Refill prompt'} withArrow position="left" color="gray">
|
|
|
|
<ActionIcon size='sm' style={{ marginLeft: 'auto' }}
|
|
|
|
onClick={() => {
|
2023-06-06 18:13:26 +08:00
|
|
|
onRefillClick({
|
|
|
|
message: message,
|
|
|
|
contexts: contexts
|
|
|
|
});
|
2023-06-06 15:13:49 +08:00
|
|
|
setRefilled(true);
|
|
|
|
setTimeout(() => { setRefilled(false); }, 2000);
|
|
|
|
}}>
|
|
|
|
{refilled ? <IconCheck size="1rem" /> : <IconCopy size="1.125rem" />}
|
|
|
|
</ActionIcon>
|
|
|
|
</Tooltip>
|
|
|
|
: <></>
|
|
|
|
}
|
2023-06-05 19:27:25 +08:00
|
|
|
</Flex>);
|
|
|
|
};
|
|
|
|
|
|
|
|
const MessageContext = (props: any) => {
|
|
|
|
const { contexts } = props;
|
|
|
|
return (contexts &&
|
|
|
|
<Accordion variant="contained" chevronPosition="left"
|
|
|
|
sx={{
|
|
|
|
marginTop: 5,
|
|
|
|
borderRadius: 5,
|
|
|
|
backgroundColor: 'var(--vscode-menu-background)',
|
|
|
|
}}
|
|
|
|
styles={{
|
|
|
|
item: {
|
|
|
|
borderColor: 'var(--vscode-menu-border)',
|
|
|
|
backgroundColor: 'var(--vscode-menu-background)',
|
|
|
|
'&[data-active]': {
|
|
|
|
backgroundColor: 'var(--vscode-menu-background)',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
control: {
|
|
|
|
height: 30,
|
|
|
|
borderRadius: 3,
|
|
|
|
backgroundColor: 'var(--vscode-menu-background)',
|
|
|
|
'&[aria-expanded="true"]': {
|
|
|
|
borderBottomLeftRadius: 0,
|
|
|
|
borderBottomRightRadius: 0,
|
|
|
|
},
|
|
|
|
'&:hover': {
|
|
|
|
backgroundColor: 'var(--vscode-menu-background)',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
chevron: {
|
|
|
|
color: 'var(--vscode-menu-foreground)',
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
color: 'var(--vscode-menu-foreground)',
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
color: 'var(--vscode-menu-foreground)',
|
|
|
|
},
|
|
|
|
panel: {
|
|
|
|
color: 'var(--vscode-menu-foreground)',
|
|
|
|
backgroundColor: 'var(--vscode-menu-background)',
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
borderRadius: 3,
|
|
|
|
backgroundColor: 'var(--vscode-menu-background)',
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
contexts?.map((item: any, index: number) => {
|
|
|
|
const { context } = item;
|
|
|
|
return (
|
|
|
|
<Accordion.Item value={`item-${index}`} mah='200'>
|
|
|
|
<Box sx={{ display: 'flex', alignItems: 'center' }}>
|
|
|
|
<Accordion.Control >
|
|
|
|
{'command' in context ? context.command : context.path}
|
|
|
|
</Accordion.Control>
|
|
|
|
</Box>
|
|
|
|
<Accordion.Panel>
|
|
|
|
{
|
|
|
|
context.content
|
2023-06-06 11:00:52 +08:00
|
|
|
? <pre style={{ overflowWrap: 'normal' }}>{context.content}</pre>
|
2023-06-05 19:27:25 +08:00
|
|
|
: <Center>
|
|
|
|
<Text c='gray.3'>No content</Text>
|
|
|
|
</Center>
|
|
|
|
}
|
|
|
|
|
|
|
|
</Accordion.Panel>
|
|
|
|
</Accordion.Item>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
</Accordion>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const MessageBlink = (props: any) => {
|
|
|
|
const { generating, messageType, lastMessage } = props;
|
|
|
|
const blink = keyframes({
|
|
|
|
'50%': { opacity: 0 },
|
|
|
|
});
|
|
|
|
|
|
|
|
return (generating && messageType === 'bot' && lastMessage
|
|
|
|
? <Text sx={{
|
|
|
|
animation: `${blink} 0.5s infinite;`,
|
|
|
|
width: 5,
|
|
|
|
marginTop: responsed ? 0 : '1em',
|
|
|
|
backgroundColor: 'black',
|
|
|
|
display: 'block'
|
|
|
|
|
|
|
|
}}>|</Text>
|
|
|
|
: <></>);
|
|
|
|
};
|
|
|
|
|
|
|
|
const messageList = messages.map((item: any, index: number) => {
|
|
|
|
const { message: messageText, type: messageType, contexts } = item;
|
|
|
|
// setMessage(messageText);
|
|
|
|
return (<>
|
|
|
|
<Stack
|
|
|
|
spacing={0}
|
|
|
|
key={`message-${index}`}
|
|
|
|
sx={{
|
2023-06-05 21:13:06 +08:00
|
|
|
width: width,
|
2023-06-05 19:27:25 +08:00
|
|
|
padding: 0,
|
|
|
|
margin: 0,
|
|
|
|
}}>
|
2023-06-06 18:13:26 +08:00
|
|
|
<MessageHeader type={messageType} message={messageText} contexts={contexts} />
|
2023-06-05 19:27:25 +08:00
|
|
|
<Container sx={{
|
|
|
|
margin: 0,
|
|
|
|
padding: 0,
|
2023-06-05 21:13:06 +08:00
|
|
|
width: width,
|
2023-06-05 19:27:25 +08:00
|
|
|
pre: {
|
|
|
|
whiteSpace: 'break-spaces'
|
|
|
|
},
|
|
|
|
}}>
|
|
|
|
<MessageContext contexts={contexts} />
|
|
|
|
<CodeBlock messageText={messageText} />
|
|
|
|
<MessageBlink generating={generating} messageType={messageType} lastMessage={index === messages.length - 1} />
|
|
|
|
</Container >
|
|
|
|
</Stack >
|
|
|
|
{index !== messages.length - 1 && <Divider my={3} />}
|
|
|
|
</>);
|
|
|
|
});
|
|
|
|
|
|
|
|
return (messageList.length > 0 ? messageList : DefaultMessage);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MessageContainer;
|