|
| 1 | +import { useParams } from 'react-router-dom' |
| 2 | +import useCourse from '../../hooks/useCourse' |
| 3 | +import useUserStatus from '../../hooks/useUserStatus' |
| 4 | +import { useRef, useState } from 'react' |
| 5 | +import useLocalStorageState from '../../hooks/useLocalStorageState' |
| 6 | +import { DEFAULT_MODEL } from '../../../config' |
| 7 | +import useInfoTexts from '../../hooks/useInfoTexts' |
| 8 | +import { Message } from '../../types' |
| 9 | +import useRetryTimeout from '../../hooks/useRetryTimeout' |
| 10 | +import { useTranslation } from 'react-i18next' |
| 11 | +import { handleCompletionStreamError } from './error' |
| 12 | +import { Box } from '@mui/material' |
| 13 | +import { Disclaimer } from './Disclaimer' |
| 14 | +import { Conversation } from './Conversation' |
| 15 | +import { ChatBox } from './ChatBox' |
| 16 | + |
| 17 | +export const ChatV2 = () => { |
| 18 | + const { courseId } = useParams() |
| 19 | + |
| 20 | + const { course } = useCourse(courseId) |
| 21 | + const { |
| 22 | + userStatus, |
| 23 | + isLoading: statusLoading, |
| 24 | + refetch: refetchStatus, |
| 25 | + } = useUserStatus(courseId) |
| 26 | + const [model, setModel] = useLocalStorageState(DEFAULT_MODEL, 'model') |
| 27 | + const { infoTexts, isLoading: infoTextsLoading } = useInfoTexts() |
| 28 | + const [activePromptId, setActivePromptId] = useState('') |
| 29 | + const [system, setSystem] = useLocalStorageState('general-chat-system', '') |
| 30 | + const [message, setMessage] = useLocalStorageState('general-chat-current', '') |
| 31 | + const [messages, setMessages] = useLocalStorageState<Message[]>( |
| 32 | + 'general-chat-messages', |
| 33 | + [] |
| 34 | + ) |
| 35 | + const inputFileRef = useRef<HTMLInputElement>(null) |
| 36 | + const [fileName, setFileName] = useState<string>('') |
| 37 | + const [completion, setCompletion] = useState('') |
| 38 | + const [streamController, setStreamController] = useState<AbortController>() |
| 39 | + const [alertOpen, setAlertOpen] = useState(false) |
| 40 | + const [disallowedFileType, setDisallowedFileType] = useState('') |
| 41 | + const [tokenUsageWarning, setTokenUsageWarning] = useState('') |
| 42 | + const [tokenWarningVisible, setTokenWarningVisible] = useState(false) |
| 43 | + const [modelTemperature, setModelTemperature] = useState(0.5) |
| 44 | + const [setRetryTimeout, clearRetryTimeout] = useRetryTimeout() |
| 45 | + const [saveConsent, setSaveConsent] = useState(true) |
| 46 | + |
| 47 | + const { t, i18n } = useTranslation() |
| 48 | + const { language } = i18n |
| 49 | + |
| 50 | + const disclaimerInfo = |
| 51 | + infoTexts?.find((infoText) => infoText.name === 'disclaimer')?.text[ |
| 52 | + language |
| 53 | + ] ?? null |
| 54 | + const systemMessageInfo = |
| 55 | + infoTexts?.find((infoText) => infoText.name === 'systemMessage')?.text[ |
| 56 | + language |
| 57 | + ] ?? null |
| 58 | + |
| 59 | + const processStream = async (stream: ReadableStream) => { |
| 60 | + try { |
| 61 | + const reader = stream.getReader() |
| 62 | + |
| 63 | + let content = '' |
| 64 | + const decoder = new TextDecoder() |
| 65 | + |
| 66 | + while (true) { |
| 67 | + const { value, done } = await reader.read() |
| 68 | + if (done) break |
| 69 | + |
| 70 | + const text = decoder.decode(value) |
| 71 | + setCompletion((prev) => prev + text) |
| 72 | + content += text |
| 73 | + } |
| 74 | + |
| 75 | + setMessages(messages.concat({ role: 'assistant', content })) |
| 76 | + } catch (err: any) { |
| 77 | + handleCompletionStreamError(err, fileName) |
| 78 | + } finally { |
| 79 | + setStreamController(undefined) |
| 80 | + setCompletion('') |
| 81 | + refetchStatus() |
| 82 | + inputFileRef.current.value = '' |
| 83 | + setFileName('') |
| 84 | + clearRetryTimeout() |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <Box> |
| 90 | + {disclaimerInfo && <Disclaimer disclaimer={disclaimerInfo} />} |
| 91 | + <Conversation messages={messages} /> |
| 92 | + <ChatBox |
| 93 | + disabled={false} |
| 94 | + onSubmit={(message) => { |
| 95 | + if (message.trim()) { |
| 96 | + setMessages(messages.concat({ role: 'user', content: message })) |
| 97 | + setMessage('') |
| 98 | + setCompletion('') |
| 99 | + setStreamController(new AbortController()) |
| 100 | + setRetryTimeout(() => { |
| 101 | + if (streamController) { |
| 102 | + streamController.abort() |
| 103 | + } |
| 104 | + }, 5000) |
| 105 | + } |
| 106 | + }} |
| 107 | + /> |
| 108 | + </Box> |
| 109 | + ) |
| 110 | +} |
0 commit comments