import { useRef, useState } from 'react' import { EngineManager, ErrorCode, InferenceEngine, ThreadMessage, } from '@janhq/core' import { useAtomValue, useSetAtom } from 'jotai' import { CheckIcon, ClipboardIcon, SearchCodeIcon } from 'lucide-react' import AutoLink from '@/containers/AutoLink' import ModalTroubleShooting, { modalTroubleShootingAtom, } from '@/containers/ModalTroubleShoot' import { MainViewState } from '@/constants/screens' import { mainViewStateAtom } from '@/helpers/atoms/App.atom' import { activeAssistantAtom } from '@/helpers/atoms/Assistant.atom' import { selectedSettingAtom } from '@/helpers/atoms/Setting.atom' const ErrorMessage = ({ message }: { message: ThreadMessage }) => { const setModalTroubleShooting = useSetAtom(modalTroubleShootingAtom) const setMainState = useSetAtom(mainViewStateAtom) const setSelectedSettingScreen = useSetAtom(selectedSettingAtom) const activeAssistant = useAtomValue(activeAssistantAtom) const errorDivRef = useRef(null) const [copied, setCopied] = useState(false) const getEngine = () => { const engineName = activeAssistant?.model?.engine return engineName ? EngineManager.instance().get(engineName) : null } const handleCopy = () => { if (errorDivRef.current) { const errorText = errorDivRef.current.innerText if (errorText) { navigator.clipboard.writeText(errorText) setCopied(true) setTimeout(() => setCopied(false), 2000) } } } const getErrorTitle = () => { const engine = getEngine() switch (message.metadata?.error_code) { case ErrorCode.InvalidApiKey: case ErrorCode.AuthenticationError: return ( <> Invalid API key. Please check your API key from{' '} {' '} and try again. ) default: return (

{message.content[0]?.text?.value === 'Failed to fetch' && engine && engine?.name !== InferenceEngine.cortex_llamacpp ? ( No internet connection.
Switch to an on-device model or check connection.
) : ( <> {message?.content[0]?.text?.value && ( )} )}

) } } return (
Error
setModalTroubleShooting(true)} > Troubleshooting
{copied ? ( <> Copied ) : ( <> Copy )}
{getErrorTitle()}
) } export default ErrorMessage