import { useCallback, useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useDispatch, useSelector } from 'react-redux'; import { useParams } from 'react-router-dom'; import userService from '../api/services/userService'; import NoFilesDarkIcon from '../assets/no-files-dark.svg'; import NoFilesIcon from '../assets/no-files.svg'; import Robot from '../assets/robot.svg'; import MessageInput from '../components/MessageInput'; import Spinner from '../components/Spinner'; import ConversationMessages from '../conversation/ConversationMessages'; import { Query } from '../conversation/conversationModels'; import { addQuery, fetchAnswer, resendQuery, selectQueries, selectStatus, } from '../conversation/conversationSlice'; import { useDarkTheme } from '../hooks'; import { selectToken, setSelectedAgent } from '../preferences/preferenceSlice'; import { AppDispatch } from '../store'; import SharedAgentCard from './SharedAgentCard'; import { Agent } from './types'; export default function SharedAgent() { const { t } = useTranslation(); const { agentId } = useParams(); const dispatch = useDispatch(); const [isDarkTheme] = useDarkTheme(); const token = useSelector(selectToken); const queries = useSelector(selectQueries); const status = useSelector(selectStatus); const [sharedAgent, setSharedAgent] = useState(); const [isLoading, setIsLoading] = useState(true); const [input, setInput] = useState(''); const [lastQueryReturnedErr, setLastQueryReturnedErr] = useState(false); const fetchStream = useRef(null); const getSharedAgent = async () => { try { setIsLoading(true); const response = await userService.getSharedAgent(agentId ?? '', token); if (!response.ok) throw new Error('Failed to fetch Shared Agent'); const agent: Agent = await response.json(); setSharedAgent(agent); } catch (error) { console.error('Error: ', error); } finally { setIsLoading(false); } }; const handleFetchAnswer = useCallback( ({ question, index }: { question: string; index?: number }) => { fetchStream.current = dispatch(fetchAnswer({ question, indx: index })); }, [dispatch], ); const handleQuestion = useCallback( ({ question, isRetry = false, index = undefined, }: { question: string; isRetry?: boolean; index?: number; }) => { const trimmedQuestion = question.trim(); if (trimmedQuestion === '') return; if (index !== undefined) { if (!isRetry) dispatch(resendQuery({ index, prompt: trimmedQuestion })); handleFetchAnswer({ question: trimmedQuestion, index }); } else { if (!isRetry) { const newQuery: Query = { prompt: trimmedQuestion }; dispatch(addQuery(newQuery)); } handleFetchAnswer({ question: trimmedQuestion, index: undefined }); } }, [dispatch, handleFetchAnswer], ); const handleQuestionSubmission = ( question?: string, updated?: boolean, indx?: number, ) => { if (updated === true && question !== undefined && indx !== undefined) { handleQuestion({ question, index: indx, isRetry: false, }); } else if (question && status !== 'loading') { const currentInput = question.trim(); if (lastQueryReturnedErr && queries.length > 0) { const lastQueryIndex = queries.length - 1; handleQuestion({ question: currentInput, isRetry: true, index: lastQueryIndex, }); } else { handleQuestion({ question: currentInput, isRetry: false, index: undefined, }); } setInput(''); } }; useEffect(() => { if (agentId) getSharedAgent(); }, [agentId, token]); useEffect(() => { if (sharedAgent) dispatch(setSelectedAgent(sharedAgent)); }, [sharedAgent, dispatch]); if (isLoading) return (
); if (!sharedAgent) return (
No agent found

No agent found. Please ensure the agent is shared.

); return (
agent-logo

{sharedAgent.name}

} />
handleQuestionSubmission(text)} loading={status === 'loading'} showSourceButton={sharedAgent ? false : true} showToolButton={sharedAgent ? false : true} autoFocus={false} />

{t('tagline')}

); }