2023-05-05 01:10:02 +08:00
|
|
|
import * as React from 'react';
|
2023-05-07 01:19:48 +08:00
|
|
|
import { useState, useEffect, useRef } from 'react';
|
2023-06-05 22:18:51 +08:00
|
|
|
import { Alert, Center, Container, Stack, px } from '@mantine/core';
|
2023-05-05 01:10:02 +08:00
|
|
|
import { ScrollArea } from '@mantine/core';
|
2023-06-05 19:27:25 +08:00
|
|
|
import { Button } from '@mantine/core';
|
2023-05-18 13:58:17 +08:00
|
|
|
import { useListState, useResizeObserver, useTimeout, useViewportSize } from '@mantine/hooks';
|
2023-06-05 22:18:51 +08:00
|
|
|
import { IconAlertCircle, IconPlayerStop, IconRotateDot } from '@tabler/icons-react';
|
2023-05-17 20:41:31 +08:00
|
|
|
import messageUtil from '../../util/MessageUtil';
|
2023-05-07 00:50:03 +08:00
|
|
|
|
2023-06-08 17:32:57 +08:00
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
import {
|
|
|
|
setValue
|
|
|
|
} from './inputSlice';
|
|
|
|
|
2023-06-05 19:21:10 +08:00
|
|
|
import InputMessage from './InputMessage';
|
2023-06-05 19:27:25 +08:00
|
|
|
import MessageContainer from './MessageContainer';
|
2023-06-05 18:20:08 +08:00
|
|
|
|
2023-06-06 10:55:14 +08:00
|
|
|
const RegenerationButton = (props: any) => {
|
|
|
|
const { onClick } = props;
|
|
|
|
return (<Button
|
|
|
|
size='xs'
|
|
|
|
leftIcon={<IconRotateDot color='var(--vscode-button-foreground)' />}
|
|
|
|
sx={{
|
|
|
|
backgroundColor: 'var(--vscode-button-background)',
|
|
|
|
}}
|
|
|
|
styles={{
|
|
|
|
icon: {
|
|
|
|
color: 'var(--vscode-button-foreground)'
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
color: 'var(--vscode-button-foreground)',
|
|
|
|
fontSize: 'var(--vscode-editor-font-size)',
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onClick={onClick}
|
|
|
|
variant="white">
|
|
|
|
Regeneration
|
|
|
|
</Button>);
|
|
|
|
};
|
|
|
|
|
|
|
|
const StopButton = (props: any) => {
|
|
|
|
const { onClick } = props;
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
size='xs'
|
|
|
|
leftIcon={<IconPlayerStop color='var(--vscode-button-foreground)' />}
|
|
|
|
sx={{
|
|
|
|
backgroundColor: 'var(--vscode-button-background)',
|
|
|
|
}}
|
|
|
|
styles={{
|
|
|
|
icon: {
|
|
|
|
color: 'var(--vscode-button-foreground)'
|
|
|
|
},
|
|
|
|
label: {
|
|
|
|
color: 'var(--vscode-button-foreground)',
|
|
|
|
fontSize: 'var(--vscode-editor-font-size)',
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onClick={onClick}
|
|
|
|
variant="white">
|
|
|
|
Stop generating
|
|
|
|
</Button>);
|
|
|
|
};
|
|
|
|
|
2023-06-05 19:12:58 +08:00
|
|
|
const chatPanel = () => {
|
2023-06-08 17:32:57 +08:00
|
|
|
const dispatch = useDispatch();
|
2023-06-05 19:12:58 +08:00
|
|
|
const [chatContainerRef, chatContainerRect] = useResizeObserver();
|
|
|
|
const scrollViewport = useRef<HTMLDivElement>(null);
|
|
|
|
const [messages, messageHandlers] = useListState<{ type: string; message: string; contexts?: any[] }>([]);
|
|
|
|
const [currentMessage, setCurrentMessage] = useState('');
|
|
|
|
const [generating, setGenerating] = useState(false);
|
|
|
|
const [responsed, setResponsed] = useState(false);
|
2023-06-05 22:18:51 +08:00
|
|
|
const [hasError, setHasError] = useState('');
|
2023-06-05 19:12:58 +08:00
|
|
|
const { height, width } = useViewportSize();
|
|
|
|
const [scrollPosition, onScrollPositionChange] = useState({ x: 0, y: 0 });
|
|
|
|
const [stopScrolling, setStopScrolling] = useState(false);
|
|
|
|
const messageCount = 10;
|
2023-06-06 18:13:26 +08:00
|
|
|
const [contexts, contextsHandlers] = useListState<any>([]);
|
2023-06-05 19:12:58 +08:00
|
|
|
|
|
|
|
const scrollToBottom = () =>
|
|
|
|
scrollViewport?.current?.scrollTo({ top: scrollViewport.current.scrollHeight, behavior: 'smooth' });
|
|
|
|
|
|
|
|
const timer = useTimeout(() => {
|
|
|
|
// console.log(`stopScrolling:${stopScrolling}`);
|
|
|
|
if (!stopScrolling) {
|
|
|
|
scrollToBottom();
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
messageUtil.sendMessage({ command: 'regContextList' });
|
|
|
|
messageUtil.sendMessage({ command: 'regCommandList' });
|
|
|
|
messageUtil.sendMessage({ command: 'historyMessages' });
|
2023-06-05 20:41:37 +08:00
|
|
|
messageUtil.registerHandler('receiveMessagePartial', (message: { text: string; }) => {
|
|
|
|
setCurrentMessage(message.text);
|
|
|
|
setResponsed(true);
|
|
|
|
});
|
|
|
|
messageUtil.registerHandler('receiveMessage', (message: { text: string; isError: boolean }) => {
|
|
|
|
setGenerating(false);
|
|
|
|
setResponsed(true);
|
|
|
|
if (message.isError) {
|
2023-06-05 22:18:51 +08:00
|
|
|
setHasError(message.text);
|
2023-06-05 20:41:37 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
messageUtil.registerHandler('loadHistoryMessages', (message: { command: string; entries: [{ hash: '', user: '', date: '', request: '', response: '', context: [{ content: '', role: '' }] }] }) => {
|
|
|
|
message.entries?.forEach(({ hash, user, date, request, response, context }, index) => {
|
|
|
|
if (index < message.entries.length - messageCount) return;
|
|
|
|
const contexts = context.map(({ content, role }) => ({ context: JSON.parse(content) }));
|
|
|
|
messageHandlers.append({ type: 'user', message: request, contexts: contexts });
|
|
|
|
messageHandlers.append({ type: 'bot', message: response });
|
|
|
|
});
|
|
|
|
});
|
2023-06-05 19:12:58 +08:00
|
|
|
timer.start();
|
|
|
|
return () => {
|
|
|
|
timer.clear();
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const sh = scrollViewport.current?.scrollHeight || 0;
|
|
|
|
const vh = scrollViewport.current?.clientHeight || 0;
|
|
|
|
const isBottom = sh < vh ? true : sh - vh - scrollPosition.y < 3;
|
|
|
|
if (isBottom) {
|
|
|
|
setStopScrolling(false);
|
|
|
|
} else {
|
|
|
|
setStopScrolling(true);
|
|
|
|
}
|
|
|
|
}, [scrollPosition]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (generating) {
|
|
|
|
// new a bot message
|
|
|
|
messageHandlers.append({ type: 'bot', message: currentMessage });
|
|
|
|
}
|
|
|
|
}, [generating]);
|
|
|
|
|
|
|
|
// Add the received message to the chat UI as a bot message
|
|
|
|
useEffect(() => {
|
|
|
|
const lastIndex = messages?.length - 1;
|
|
|
|
const lastMessage = messages[lastIndex];
|
|
|
|
if (currentMessage && lastMessage?.type === 'bot') {
|
|
|
|
// update the last one bot message
|
|
|
|
messageHandlers.setItem(lastIndex, { type: 'bot', message: currentMessage });
|
|
|
|
}
|
|
|
|
timer.start();
|
|
|
|
}, [currentMessage]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (messages.length > messageCount * 2) {
|
|
|
|
messageHandlers.remove(0, 1);
|
|
|
|
}
|
|
|
|
timer.start();
|
|
|
|
}, [messages]);
|
|
|
|
|
2023-05-05 01:10:02 +08:00
|
|
|
return (
|
2023-05-09 19:10:46 +08:00
|
|
|
<Container
|
|
|
|
id='chat-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
|
|
|
}}>
|
|
|
|
<ScrollArea
|
|
|
|
id='chat-scroll-area'
|
|
|
|
type="never"
|
2023-05-18 23:07:36 +08:00
|
|
|
sx={{
|
|
|
|
height: generating ? height - px('8rem') : height - px('5rem'),
|
|
|
|
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-06-06 18:13:26 +08:00
|
|
|
<MessageContainer
|
|
|
|
onRefillClick={(params: any) => {
|
|
|
|
const { message, contexts: messageContexts } = params;
|
2023-06-08 17:32:57 +08:00
|
|
|
dispatch(setValue(message));
|
2023-06-06 18:13:26 +08:00
|
|
|
contexts.length = 0;
|
|
|
|
contextsHandlers.append(...messageContexts);
|
|
|
|
}}
|
|
|
|
width={chatContainerRect.width}
|
|
|
|
generating={generating}
|
|
|
|
messages={messages}
|
|
|
|
responsed={responsed} />
|
2023-06-05 22:18:51 +08:00
|
|
|
{hasError &&
|
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-06-05 22:18:51 +08:00
|
|
|
{hasError}
|
|
|
|
</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-05-11 15:10:05 +08:00
|
|
|
{generating &&
|
|
|
|
<Center>
|
2023-06-06 10:55:14 +08:00
|
|
|
<StopButton
|
|
|
|
onClick={() => {
|
|
|
|
messageUtil.sendMessage({
|
|
|
|
command: 'stopDevChat'
|
|
|
|
});
|
|
|
|
setGenerating(false);
|
|
|
|
}} />
|
2023-05-11 15:10:05 +08:00
|
|
|
</Center>
|
|
|
|
}
|
2023-05-23 21:13:49 +08:00
|
|
|
{hasError &&
|
|
|
|
<Center>
|
2023-06-06 10:55:14 +08:00
|
|
|
<RegenerationButton
|
|
|
|
onClick={() => {
|
|
|
|
messageUtil.sendMessage({
|
|
|
|
command: 'regeneration'
|
|
|
|
});
|
|
|
|
messageHandlers.pop();
|
|
|
|
setHasError('');
|
|
|
|
setGenerating(true);
|
|
|
|
setResponsed(false);
|
|
|
|
setCurrentMessage('');
|
|
|
|
}} />
|
2023-05-23 21:13:49 +08:00
|
|
|
</Center>
|
|
|
|
}
|
2023-06-05 19:12:58 +08:00
|
|
|
<InputMessage
|
2023-06-06 18:13:26 +08:00
|
|
|
contexts={contexts}
|
|
|
|
contextsHandlers={contextsHandlers}
|
2023-06-05 19:12:58 +08:00
|
|
|
generating={generating}
|
2023-06-05 21:13:06 +08:00
|
|
|
width={chatContainerRect.width}
|
2023-06-05 19:12:58 +08:00
|
|
|
onSendClick={(input: string, contexts: any) => {
|
|
|
|
// Add the user's message to the chat UI
|
|
|
|
messageHandlers.append({ type: 'user', message: input, contexts: contexts ? [...contexts].map((item) => ({ ...item })) : undefined });
|
|
|
|
// start generating
|
|
|
|
setGenerating(true);
|
|
|
|
setResponsed(false);
|
|
|
|
setCurrentMessage('');
|
2023-06-06 11:06:19 +08:00
|
|
|
setHasError('');
|
2023-06-05 19:12:58 +08:00
|
|
|
}} />
|
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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default chatPanel;
|