2023-05-05 01:10:02 +08:00
|
|
|
import * as React from 'react';
|
2023-07-24 11:34:10 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
2023-08-03 21:08:49 +08:00
|
|
|
import { ActionIcon, Alert, Center, Container, Stack, px } from '@mantine/core';
|
2023-05-05 01:10:02 +08:00
|
|
|
import { ScrollArea } from '@mantine/core';
|
2023-07-24 11:34:10 +08:00
|
|
|
import { useResizeObserver, useTimeout, useViewportSize } from '@mantine/hooks';
|
2023-06-13 17:03:48 +08:00
|
|
|
import messageUtil from '@/util/MessageUtil';
|
2023-08-03 16:53:31 +08:00
|
|
|
import CurrentMessage from "@/views/components/CurrentMessage";
|
2023-08-03 16:34:52 +08:00
|
|
|
import StopButton from '@/views/components/StopButton';
|
|
|
|
import RegenerationButton from '@/views/components/RegenerationButton';
|
2023-08-18 00:22:16 +08:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { useMst } from "@/views/stores/RootStore";
|
|
|
|
import { Message } from "@/views/stores/ChatStore";
|
2023-05-07 00:50:03 +08:00
|
|
|
|
2023-06-08 17:32:57 +08:00
|
|
|
|
2023-08-03 16:47:26 +08:00
|
|
|
import InputMessage from '@/views/components/InputMessage';
|
2023-08-21 13:53:32 +08:00
|
|
|
import MessageList from '@/views/components/MessageList';
|
2023-08-03 21:08:49 +08:00
|
|
|
import { IconCircleArrowDown, IconCircleArrowDownFilled } from '@tabler/icons-react';
|
2023-06-05 18:20:08 +08:00
|
|
|
|
2023-06-06 10:55:14 +08:00
|
|
|
|
2023-08-18 00:22:16 +08:00
|
|
|
const chatPanel = observer(() => {
|
|
|
|
const { input, chat } = useMst();
|
|
|
|
|
2023-06-05 19:12:58 +08:00
|
|
|
const [chatContainerRef, chatContainerRect] = useResizeObserver();
|
|
|
|
const scrollViewport = useRef<HTMLDivElement>(null);
|
|
|
|
const { height, width } = useViewportSize();
|
|
|
|
|
|
|
|
const scrollToBottom = () =>
|
|
|
|
scrollViewport?.current?.scrollTo({ top: scrollViewport.current.scrollHeight, behavior: 'smooth' });
|
|
|
|
|
|
|
|
const timer = useTimeout(() => {
|
2023-08-18 00:22:16 +08:00
|
|
|
if (chat.isBottom) {
|
2023-06-05 19:12:58 +08:00
|
|
|
scrollToBottom();
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
|
2023-06-13 11:54:31 +08:00
|
|
|
const onScrollPositionChange = ({ x, y }) => {
|
|
|
|
const sh = scrollViewport.current?.scrollHeight || 0;
|
|
|
|
const vh = scrollViewport.current?.clientHeight || 0;
|
|
|
|
const gap = sh - vh - y;
|
|
|
|
const isBottom = sh < vh ? true : gap < 100;
|
|
|
|
const isTop = y === 0;
|
|
|
|
// console.log(`sh:${sh},vh:${vh},x:${x},y:${y},gap:${gap}`);
|
|
|
|
if (isBottom) {
|
2023-08-18 00:22:16 +08:00
|
|
|
chat.onMessagesBottom();
|
2023-06-13 11:54:31 +08:00
|
|
|
} else if (isTop) {
|
2023-08-18 00:22:16 +08:00
|
|
|
chat.onMessagesTop()
|
|
|
|
if (!chat.isLastPage) {
|
2023-06-15 20:52:33 +08:00
|
|
|
//TODO: Data loading flickers and has poor performance, so I temporarily disabled the loading logic.
|
|
|
|
// dispatch(fetchHistoryMessages({ pageIndex: pageIndex + 1 }));
|
2023-06-13 17:53:05 +08:00
|
|
|
}
|
2023-06-13 11:54:31 +08:00
|
|
|
} else {
|
2023-08-18 00:22:16 +08:00
|
|
|
chat.onMessagesMiddle();
|
2023-06-13 11:54:31 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-05 19:12:58 +08:00
|
|
|
useEffect(() => {
|
2023-08-18 00:29:03 +08:00
|
|
|
chat.fetchHistoryMessages({ pageIndex: 0 }).then();
|
2023-06-05 20:41:37 +08:00
|
|
|
messageUtil.registerHandler('receiveMessagePartial', (message: { text: string; }) => {
|
2023-08-18 00:22:16 +08:00
|
|
|
chat.startResponsing(message.text);
|
2023-07-05 15:00:20 +08:00
|
|
|
timer.start();
|
2023-06-05 20:41:37 +08:00
|
|
|
});
|
2023-07-19 16:03:39 +08:00
|
|
|
messageUtil.registerHandler('receiveMessage', (message: { text: string; isError: boolean, hash }) => {
|
2023-08-18 12:11:12 +08:00
|
|
|
const messageItem = Message.create({ type: 'bot', message: message.text, hash: message.hash });
|
2023-08-18 00:22:16 +08:00
|
|
|
chat.stopGenerating(true, messageItem);
|
2023-06-05 20:41:37 +08:00
|
|
|
if (message.isError) {
|
2023-08-18 00:22:16 +08:00
|
|
|
chat.happendError(message.text);
|
2023-06-05 20:41:37 +08:00
|
|
|
}
|
|
|
|
});
|
2023-07-24 08:21:25 +08:00
|
|
|
|
2023-08-03 16:34:52 +08:00
|
|
|
messageUtil.registerHandler('systemMessage', (message: { text: string }) => {
|
2023-08-18 12:11:12 +08:00
|
|
|
const messageItem = Message.create({ type: 'system', message: message.text });
|
|
|
|
chat.newMessage(messageItem);
|
2023-07-24 08:21:25 +08:00
|
|
|
// start generating
|
2023-08-18 00:22:16 +08:00
|
|
|
chat.startSystemMessage();
|
2023-07-24 08:21:25 +08:00
|
|
|
// Clear the input field
|
2023-08-18 00:22:16 +08:00
|
|
|
input.setValue('');
|
|
|
|
input.clearContexts();
|
2023-07-24 08:21:25 +08:00
|
|
|
});
|
|
|
|
|
2023-06-05 19:12:58 +08:00
|
|
|
timer.start();
|
|
|
|
return () => {
|
|
|
|
timer.clear();
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2023-08-22 17:22:30 +08:00
|
|
|
useEffect(() => {
|
|
|
|
scrollToBottom();
|
|
|
|
}, [chat.scrollBottom]);
|
|
|
|
|
2023-05-05 01:10:02 +08:00
|
|
|
return (
|
2023-05-09 19:10:46 +08:00
|
|
|
<Container
|
|
|
|
ref={chatContainerRef}
|
|
|
|
sx={{
|
|
|
|
height: '100%',
|
2023-05-18 23:07:36 +08:00
|
|
|
margin: 0,
|
2023-05-19 00:21:53 +08:00
|
|
|
padding: 10,
|
|
|
|
background: 'var(--vscode-sideBar-background)',
|
2023-05-18 23:07:36 +08:00
|
|
|
color: 'var(--vscode-editor-foreground)',
|
|
|
|
minWidth: 240
|
2023-05-09 19:10:46 +08:00
|
|
|
}}>
|
2023-08-18 00:22:16 +08:00
|
|
|
{!chat.isBottom && <ActionIcon
|
2023-08-03 21:08:49 +08:00
|
|
|
onClick={() => { scrollToBottom() }}
|
|
|
|
title='Bottom'
|
|
|
|
variant='transparent' sx={{ position: "absolute", bottom: 60, right: 20, zIndex: 999 }}>
|
|
|
|
<IconCircleArrowDownFilled size="1.125rem" />
|
|
|
|
</ActionIcon>}
|
2023-05-09 19:10:46 +08:00
|
|
|
<ScrollArea
|
2023-05-18 23:07:36 +08:00
|
|
|
sx={{
|
2023-08-18 00:22:16 +08:00
|
|
|
height: chat.generating ? height - px('8rem') : height - px('5rem'),
|
2023-05-18 23:07:36 +08:00
|
|
|
width: chatContainerRect.width,
|
|
|
|
padding: 0,
|
|
|
|
margin: 0,
|
|
|
|
}}
|
2023-06-08 10:47:24 +08:00
|
|
|
onScrollPositionChange={onScrollPositionChange}
|
2023-05-09 19:10:46 +08:00
|
|
|
viewportRef={scrollViewport}>
|
2023-08-21 13:53:32 +08:00
|
|
|
<MessageList
|
2023-06-08 19:54:21 +08:00
|
|
|
width={chatContainerRect.width} />
|
2023-07-06 05:56:36 +08:00
|
|
|
<CurrentMessage width={chatContainerRect.width} />
|
2023-08-18 00:22:16 +08:00
|
|
|
{chat.errorMessage &&
|
2023-06-06 10:55:14 +08:00
|
|
|
<Alert styles={{ message: { fontSize: 'var(--vscode-editor-font-size)' } }} w={chatContainerRect.width} mb={20} color="gray" variant="filled">
|
2023-08-18 00:22:16 +08:00
|
|
|
{chat.errorMessage}
|
2023-07-05 15:00:20 +08:00
|
|
|
</Alert>}
|
2023-05-09 19:10:46 +08:00
|
|
|
</ScrollArea>
|
2023-05-10 17:38:24 +08:00
|
|
|
<Stack
|
2023-05-18 19:58:29 +08:00
|
|
|
spacing={5}
|
2023-06-05 21:13:06 +08:00
|
|
|
sx={{ position: 'absolute', bottom: 10, width: 'calc(100% - 20px)' }}>
|
2023-08-18 00:22:16 +08:00
|
|
|
{chat.generating &&
|
2023-05-11 15:10:05 +08:00
|
|
|
<Center>
|
2023-06-08 20:11:00 +08:00
|
|
|
<StopButton />
|
2023-05-11 15:10:05 +08:00
|
|
|
</Center>
|
|
|
|
}
|
2023-08-18 00:22:16 +08:00
|
|
|
{chat.errorMessage &&
|
2023-05-23 21:13:49 +08:00
|
|
|
<Center>
|
2023-06-08 20:11:00 +08:00
|
|
|
<RegenerationButton />
|
2023-05-23 21:13:49 +08:00
|
|
|
</Center>
|
|
|
|
}
|
2023-06-05 19:12:58 +08:00
|
|
|
<InputMessage
|
2023-06-08 20:11:00 +08:00
|
|
|
width={chatContainerRect.width} />
|
2023-05-10 17:38:24 +08:00
|
|
|
</Stack>
|
2023-06-08 17:32:57 +08:00
|
|
|
</Container>
|
2023-05-05 01:10:02 +08:00
|
|
|
);
|
2023-08-18 00:22:16 +08:00
|
|
|
});
|
2023-05-05 01:10:02 +08:00
|
|
|
|
|
|
|
export default chatPanel;
|