* fix: update new api from cortex to support 0.5.0 Signed-off-by: James <namnh0122@gmail.com> * fix stop button for streaming Signed-off-by: James <namnh0122@gmail.com> * fix stop inference for nonstreaming Signed-off-by: James <namnh0122@gmail.com> * chore: remove umami prevent tracking call to vercel Signed-off-by: James <namnh0122@gmail.com> * add warning modal when running more than 2 model concurrently Signed-off-by: James <namnh0122@gmail.com> * fix: skip summarize if abort Signed-off-by: James <namnh0122@gmail.com> * 0.5.0-3 * add inference error popup Signed-off-by: James <namnh0122@gmail.com> * add back import local model Signed-off-by: James <namnh0122@gmail.com> * fix: max token issue (#3225) Signed-off-by: James <namnh0122@gmail.com> * format status Signed-off-by: James <namnh0122@gmail.com> * fix migration missing instructions Signed-off-by: James <namnh0122@gmail.com> * fix: wait for cortex process overlay should be on top (#3224) * fix: wait for cortex process overlay should be on top * chore: update cortex.js * Cortex 0.5.0-5 * add import model to my model screen Signed-off-by: James <namnh0122@gmail.com> * fix: should migrate symlink models (#3226) * fix import on windows (#3229) Signed-off-by: James <namnh0122@gmail.com> * fix yarn lint Signed-off-by: James <namnh0122@gmail.com> * fix: clean up port before start jan (#3232) Signed-off-by: James <namnh0122@gmail.com> --------- Signed-off-by: James <namnh0122@gmail.com> Co-authored-by: Van Pham <64197333+Van-QA@users.noreply.github.com> Co-authored-by: Louis <louis@jan.ai>
96 lines
2.8 KiB
TypeScript
96 lines
2.8 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef } from 'react'
|
|
|
|
import { TextArea } from '@janhq/joi'
|
|
import { useAtom, useAtomValue } from 'jotai'
|
|
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
import { currentPromptAtom } from '@/containers/Providers/Jotai'
|
|
|
|
import { getCurrentChatMessagesAtom } from '@/helpers/atoms/ChatMessage.atom'
|
|
|
|
import { spellCheckAtom } from '@/helpers/atoms/Setting.atom'
|
|
import {
|
|
getActiveThreadIdAtom,
|
|
isGeneratingResponseAtom,
|
|
} from '@/helpers/atoms/Thread.atom'
|
|
|
|
type Props = {
|
|
isSettingActive: boolean
|
|
onSendMessageClick: (message: string) => void
|
|
}
|
|
|
|
const ChatTextInput: React.FC<Props> = ({
|
|
isSettingActive,
|
|
onSendMessageClick,
|
|
}) => {
|
|
const messages = useAtomValue(getCurrentChatMessagesAtom)
|
|
const [currentPrompt, setCurrentPrompt] = useAtom(currentPromptAtom)
|
|
const textareaRef = useRef<HTMLTextAreaElement>(null)
|
|
const activeThreadId = useAtomValue(getActiveThreadIdAtom)
|
|
const spellCheck = useAtomValue(spellCheckAtom)
|
|
|
|
const isGeneratingResponse = useAtomValue(isGeneratingResponseAtom)
|
|
|
|
const disabled = useMemo(() => {
|
|
return !activeThreadId
|
|
}, [activeThreadId])
|
|
|
|
const onChange = useCallback(
|
|
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
setCurrentPrompt(e.target.value)
|
|
},
|
|
[setCurrentPrompt]
|
|
)
|
|
|
|
useEffect(() => {
|
|
if (textareaRef.current) {
|
|
textareaRef.current.focus()
|
|
}
|
|
}, [activeThreadId])
|
|
|
|
useEffect(() => {
|
|
if (textareaRef.current?.clientHeight) {
|
|
textareaRef.current.style.height = isSettingActive ? '100px' : '40px'
|
|
textareaRef.current.style.height = textareaRef.current.scrollHeight + 'px'
|
|
textareaRef.current.style.overflow =
|
|
textareaRef.current.clientHeight >= 390 ? 'auto' : 'hidden'
|
|
}
|
|
}, [textareaRef.current?.clientHeight, currentPrompt, isSettingActive])
|
|
|
|
const onKeyDown = useCallback(
|
|
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
|
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
|
|
e.preventDefault()
|
|
if (isGeneratingResponse) return
|
|
const lastMessage = messages[messages.length - 1]
|
|
if (!lastMessage || lastMessage.status !== 'in_progress') {
|
|
onSendMessageClick(currentPrompt)
|
|
return
|
|
}
|
|
}
|
|
},
|
|
[messages, isGeneratingResponse, currentPrompt, onSendMessageClick]
|
|
)
|
|
|
|
return (
|
|
<TextArea
|
|
className={twMerge(
|
|
'relative max-h-[400px] resize-none pr-20',
|
|
isSettingActive && 'pb-14 pr-16'
|
|
)}
|
|
spellCheck={spellCheck}
|
|
data-testid="txt-input-chat"
|
|
style={{ height: isSettingActive ? '100px' : '40px' }}
|
|
ref={textareaRef}
|
|
onKeyDown={onKeyDown}
|
|
placeholder="Ask me anything"
|
|
disabled={disabled}
|
|
value={currentPrompt}
|
|
onChange={onChange}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export default ChatTextInput
|