* feat: integrating cortex * Temporary prevent crash Signed-off-by: James <namnh0122@gmail.com> * fix yarn lint Signed-off-by: James <namnh0122@gmail.com> * refactor: remove core node module - fs - extensions and so on (#3151) * add migration script for threads, messages and models Signed-off-by: James <namnh0122@gmail.com> * remove freq_penalty and presence_penalty if model not supported Signed-off-by: James <namnh0122@gmail.com> * add back models in my models Signed-off-by: James <namnh0122@gmail.com> * fix api-url for setup API key popup Signed-off-by: James <namnh0122@gmail.com> * fix using model name for dropdown model Signed-off-by: James <namnh0122@gmail.com> * fix can't click to hotkey Signed-off-by: James <namnh0122@gmail.com> * fix: disable some UIs Signed-off-by: James <namnh0122@gmail.com> * fix build Signed-off-by: James <namnh0122@gmail.com> * reduce calling HF api Signed-off-by: James <namnh0122@gmail.com> * some ui update Signed-off-by: James <namnh0122@gmail.com> * feat: modal migration UI (#3153) * feat: handle popup migration * chore: update loader * chore: integrate script migration * chore: cleanup import * chore: moving out spinner loader * chore: update check thread message success migrate * chore: add handle script into retry button * remove warning from joi Signed-off-by: James <namnh0122@gmail.com> * chore: fix duplicate children * fix: path after migrating model Signed-off-by: James <namnh0122@gmail.com> * chore: apply mutation for config * chore: prevent calling too many create assistant api Signed-off-by: James <namnh0122@gmail.com> * using cortexso Signed-off-by: James <namnh0122@gmail.com> * update download api Signed-off-by: James <namnh0122@gmail.com> * fix use on slider item Signed-off-by: James <namnh0122@gmail.com> * fix: ui no download model or simple onboarding (#3166) * fix download huggingface model match with slider item Signed-off-by: James <namnh0122@gmail.com> * update owner_logo to logo and author Signed-off-by: James <namnh0122@gmail.com> * update new cortexso Signed-off-by: James <namnh0122@gmail.com> * Add install python step for macos * add engine table Signed-off-by: James <namnh0122@gmail.com> * fix local icons Signed-off-by: James <namnh0122@gmail.com> * feat: add search feature for model hub Signed-off-by: James <namnh0122@gmail.com> * fix misalign switch Signed-off-by: James <namnh0122@gmail.com> * fix: delete thread not focus on other thread Signed-off-by: James <namnh0122@gmail.com> * add get model from hugging face Signed-off-by: James <namnh0122@gmail.com> * fix download from hugging face Signed-off-by: James <namnh0122@gmail.com> * small update Signed-off-by: James <namnh0122@gmail.com> * update Signed-off-by: James <namnh0122@gmail.com> * fix system monitor rounded only on the left Signed-off-by: James <namnh0122@gmail.com> * chore: update ui new hub screen (#3174) * chore: update ui new hub screen * chore: update layout centerpanel thread and hub screen * chore: update detail model by group * update cortexso 0.1.13 Signed-off-by: James <namnh0122@gmail.com> * chore: add file size Signed-off-by: James <namnh0122@gmail.com> * chore: put engine to experimental feature Signed-off-by: James <namnh0122@gmail.com> * chore: open cortex folder Signed-off-by: James <namnh0122@gmail.com> * chore: add back user avatar Signed-off-by: James <namnh0122@gmail.com> * chore: minor UI hub (#3182) * chore: add back right click thread list and update 3 dots are overlapping with the text * chore: update position dropdown list my models * chore: make on-device tab showing 6 items instead of 4 * chore: update style description modals detail model * chore: update isGeneration loader and author name on modal * feat: integrate cortex single executable Signed-off-by: James <namnh0122@gmail.com> * fix build Signed-off-by: James <namnh0122@gmail.com> * chore: added blank state * chore: update ui component blank state * bump cortex binary version * fix: logic show modal migration (#3165) * fix: logic show modal migration * chore: fixed logic * chore: read contain format gguf local models * chore: change return hasLocalModel * chore: intiial skipmigration state * chore: filter embedding model * fix: delete top thread not focus on any other thread * chore: added UI no result component search models group (#3188) * fix: remote model should show all when user config that engine Signed-off-by: James <namnh0122@gmail.com> * chore: set state thread and models migration using getOnInit (#3189) * chore: set state thread and models migration using getOnInit * chore: add state as dependecies hooks * chore: system monitor panel show engine model (#3192) * fix: remove config api, replace with engine Signed-off-by: James <namnh0122@gmail.com> * update Signed-off-by: James <namnh0122@gmail.com> * update reactquery Signed-off-by: James <namnh0122@gmail.com> * bump cortex 0.4.35 * feat: add waiting for cortex popup Signed-off-by: James <namnh0122@gmail.com> * chore: add loader detail model popup (#3195) * chore: model start loader (#3197) * chore: added model loader when user starting chat without model active * chore: update copies loader * fix: select min file size if recommended quant does not exist Signed-off-by: James <namnh0122@gmail.com> * chore: temporary hide gpu config * fix: tensorrt not shown Signed-off-by: James <namnh0122@gmail.com> * fix lint Signed-off-by: James <namnh0122@gmail.com> * fix tests Signed-off-by: James <namnh0122@gmail.com> * fix e2e tests (wip) Signed-off-by: James <namnh0122@gmail.com> * update Signed-off-by: James <namnh0122@gmail.com> * fix: adding element and correct test to adapt new UI * fix: temp skip unstable part * fix: only show models which can be supported Signed-off-by: James <namnh0122@gmail.com> * Update version.txt * update send message Signed-off-by: James <namnh0122@gmail.com> * fix: not allow user send message when is generating Signed-off-by: James <namnh0122@gmail.com> * chore: temp skip Playwright test due to env issue * chore: temp skip Playwright test due to env issue * update Signed-off-by: James <namnh0122@gmail.com> * chore: minor-ui-feedback (#3202) --------- Signed-off-by: James <namnh0122@gmail.com> Co-authored-by: Louis <louis@jan.ai> Co-authored-by: Faisal Amir <urmauur@gmail.com> Co-authored-by: Hien To <tominhhien97@gmail.com> Co-authored-by: Van Pham <64197333+Van-QA@users.noreply.github.com> Co-authored-by: Van-QA <van@jan.ai>
264 lines
9.2 KiB
TypeScript
264 lines
9.2 KiB
TypeScript
import React, { Fragment, useEffect, useMemo, useRef, useState } from 'react'
|
|
|
|
import { Message, TextContentBlock } from '@janhq/core'
|
|
|
|
import { Tooltip } from '@janhq/joi'
|
|
import hljs from 'highlight.js'
|
|
|
|
import { useAtomValue } from 'jotai'
|
|
import { FolderOpenIcon } from 'lucide-react'
|
|
import { Marked, Renderer } from 'marked'
|
|
import { markedHighlight } from 'marked-highlight'
|
|
import markedKatex from 'marked-katex-extension'
|
|
|
|
import { twMerge } from 'tailwind-merge'
|
|
|
|
import UserAvatar from '@/components/UserAvatar'
|
|
|
|
import LogoMark from '@/containers/Brand/Logo/Mark'
|
|
|
|
import useAssistantQuery from '@/hooks/useAssistantQuery'
|
|
import { useClipboard } from '@/hooks/useClipboard'
|
|
import { usePath } from '@/hooks/usePath'
|
|
|
|
import { displayDate } from '@/utils/datetime'
|
|
|
|
import { openFileTitle } from '@/utils/titleUtils'
|
|
|
|
import EditChatInput from '../EditChatInput'
|
|
import MessageToolbar from '../MessageToolbar'
|
|
|
|
import { editMessageAtom } from '@/helpers/atoms/ChatMessage.atom'
|
|
|
|
type Props = {
|
|
isLatestMessage: boolean
|
|
msg: Message
|
|
}
|
|
|
|
const SimpleTextMessage: React.FC<Props> = ({ isLatestMessage, msg }) => {
|
|
const [text, setText] = useState('')
|
|
const { data: assistants } = useAssistantQuery()
|
|
const editMessage = useAtomValue(editMessageAtom)
|
|
const clipboard = useClipboard({ timeout: 1000 })
|
|
|
|
const senderName = useMemo(() => {
|
|
if (msg.role === 'user') return msg.role
|
|
const assistantId = msg.assistant_id
|
|
if (!assistantId) return msg.role
|
|
const assistant = assistants?.find(
|
|
(assistant) => assistant.id === assistantId
|
|
)
|
|
return assistant?.name ?? msg.role
|
|
}, [assistants, msg.assistant_id, msg.role])
|
|
|
|
useEffect(() => {
|
|
if (msg.content && msg.content.length > 0) {
|
|
const message = msg.content[0]
|
|
if (message && message.type === 'text') {
|
|
const textBlockContent = message as TextContentBlock
|
|
setText(textBlockContent.text.value)
|
|
}
|
|
}
|
|
}, [msg.content])
|
|
|
|
const marked = useMemo(() => {
|
|
const markedParser = new Marked(
|
|
markedHighlight({
|
|
langPrefix: 'hljs',
|
|
highlight(code, lang) {
|
|
if (lang === undefined || lang === '') {
|
|
return hljs.highlightAuto(code).value
|
|
}
|
|
try {
|
|
return hljs.highlight(code, { language: lang }).value
|
|
} catch (err) {
|
|
return hljs.highlight(code, { language: 'javascript' }).value
|
|
}
|
|
},
|
|
}),
|
|
{
|
|
renderer: {
|
|
link: (href, title, text) =>
|
|
Renderer.prototype.link
|
|
?.apply(this, [href, title, text])
|
|
.replace('<a', "<a target='_blank'"),
|
|
code(code, lang) {
|
|
return `
|
|
<div class="relative code-block group/item overflow-auto">
|
|
<button class='text-xs copy-action hidden group-hover/item:block p-2 rounded-lg absolute top-6 right-2'>
|
|
${
|
|
clipboard.copied
|
|
? `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-check pointer-events-none text-green-600"><path d="M20 6 9 17l-5-5"/></svg>`
|
|
: `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy pointer-events-none text-gray-400"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>`
|
|
}
|
|
</button>
|
|
<pre class="hljs">
|
|
<code class="language-${lang ?? ''}">${code}</code>
|
|
</pre>
|
|
</div>
|
|
`
|
|
},
|
|
},
|
|
}
|
|
)
|
|
markedParser.use(markedKatex({ throwOnError: false }))
|
|
return markedParser
|
|
}, [clipboard.copied])
|
|
const isUser = msg.role === 'user'
|
|
const { onViewFileContainer } = usePath()
|
|
const parsedText = useMemo(() => marked.parse(text), [marked, text])
|
|
const [tokenCount, setTokenCount] = useState(0)
|
|
const [lastTimestamp, setLastTimestamp] = useState<number | undefined>()
|
|
const [tokenSpeed, setTokenSpeed] = useState(0)
|
|
|
|
const codeBlockCopyEvent = useRef((e: Event) => {
|
|
const target: HTMLElement = e.target as HTMLElement
|
|
if (typeof target.className !== 'string') return null
|
|
|
|
const isCopyActionClassName = target?.className.includes('copy-action')
|
|
|
|
if (isCopyActionClassName) {
|
|
const content = target?.parentNode?.querySelector('code')?.innerText ?? ''
|
|
clipboard.copy(content)
|
|
}
|
|
})
|
|
|
|
useEffect(() => {
|
|
document.addEventListener('click', codeBlockCopyEvent.current)
|
|
return () => {
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
document.removeEventListener('click', codeBlockCopyEvent.current)
|
|
}
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
if (msg.status !== 'in_progress') {
|
|
return
|
|
}
|
|
const currentTimestamp = new Date().getTime() // Get current time in milliseconds
|
|
if (!lastTimestamp) {
|
|
// If this is the first update, just set the lastTimestamp and return
|
|
if (msg.content && msg.content.length > 0) {
|
|
const message = msg.content[0]
|
|
if (message && message.type === 'text') {
|
|
const textContentBlock = message as TextContentBlock
|
|
if (textContentBlock.text.value !== '') {
|
|
setLastTimestamp(currentTimestamp)
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
const timeDiffInSeconds = (currentTimestamp - lastTimestamp) / 1000 // Time difference in seconds
|
|
const totalTokenCount = tokenCount + 1
|
|
const averageTokenSpeed = totalTokenCount / timeDiffInSeconds // Calculate average token speed
|
|
|
|
setTokenSpeed(averageTokenSpeed)
|
|
setTokenCount(totalTokenCount)
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [msg.content])
|
|
|
|
return (
|
|
<div className="group relative mx-auto p-4">
|
|
<div
|
|
className={twMerge(
|
|
'mb-2 flex items-center justify-start gap-x-2',
|
|
!isUser && 'mt-2'
|
|
)}
|
|
>
|
|
{isUser ? <UserAvatar /> : <LogoMark width={32} height={32} />}
|
|
|
|
<div
|
|
className={twMerge(
|
|
'font-extrabold capitalize',
|
|
isUser && 'text-gray-500'
|
|
)}
|
|
>
|
|
{senderName}
|
|
</div>
|
|
<p className="text-xs font-medium text-gray-400">
|
|
{displayDate(msg.created_at)}
|
|
</p>
|
|
<div
|
|
className={twMerge(
|
|
'absolute right-0 cursor-pointer transition-all',
|
|
isLatestMessage && !isUser
|
|
? 'absolute -bottom-8 right-4'
|
|
: 'hidden group-hover:absolute group-hover:right-4 group-hover:top-4 group-hover:flex'
|
|
)}
|
|
>
|
|
<MessageToolbar message={msg} isLastMessage={isLatestMessage} />
|
|
</div>
|
|
{isLatestMessage &&
|
|
(msg.status === 'in_progress' || tokenSpeed > 0) && (
|
|
<p className="absolute right-8 text-xs font-medium text-[hsla(var(--text-secondary))]">
|
|
Token Speed: {Number(tokenSpeed).toFixed(2)}t/s
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
<div
|
|
className={twMerge(
|
|
'w-full',
|
|
!isUser && !text.includes(' ') && 'break-all'
|
|
)}
|
|
>
|
|
<>
|
|
{msg.content[0]?.type === 'image_file' && (
|
|
<div className="group/image relative mb-2 inline-flex cursor-pointer overflow-hidden rounded-xl">
|
|
<div className="left-0 top-0 z-20 h-full w-full group-hover/image:inline-block">
|
|
{/* <RelativeImage */}
|
|
{/* src={msg.content[0]?.text.annotations[0]} */}
|
|
{/* id={msg.id} */}
|
|
{/* onClick={() => */}
|
|
{/* onViewFile(`${msg.content[0]?.text.annotations[0]}`) */}
|
|
{/* } */}
|
|
{/* /> */}
|
|
</div>
|
|
<Tooltip
|
|
trigger={
|
|
<div
|
|
className="absolute right-2 top-2 z-20 hidden h-8 w-8 cursor-pointer items-center justify-center rounded-md bg-[hsla(var(--app-bg))] group-hover/image:flex"
|
|
onClick={onViewFileContainer}
|
|
>
|
|
<FolderOpenIcon size={20} />
|
|
</div>
|
|
}
|
|
content={<span>{openFileTitle()}</span>}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{isUser ? (
|
|
<Fragment>
|
|
{editMessage === msg.id ? (
|
|
<EditChatInput message={msg} />
|
|
) : (
|
|
<div
|
|
className={twMerge(
|
|
'message flex flex-col gap-y-2 leading-relaxed',
|
|
isUser ? 'whitespace-pre-wrap break-words' : 'p-4'
|
|
)}
|
|
>
|
|
{text}
|
|
</div>
|
|
)}
|
|
</Fragment>
|
|
) : (
|
|
<div
|
|
className={twMerge(
|
|
'message max-width-[100%] flex flex-col gap-y-2 overflow-auto leading-relaxed',
|
|
isUser && 'whitespace-pre-wrap break-words'
|
|
)}
|
|
dangerouslySetInnerHTML={{ __html: parsedText }}
|
|
/>
|
|
)}
|
|
</>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default SimpleTextMessage
|