import { apiInterceptors, newDialogue } from '@/client/api'; import useChat from '@/hooks/use-chat'; import { IChatDialogueMessageSchema, IChatDialogueSchema } from '@/types/chat'; import { CaretLeftOutlined } from '@ant-design/icons'; import { useRequest } from 'ahooks'; import classNames from 'classnames'; import { useCallback, useState } from 'react'; import AgentContent from '../chat/agent-content'; import ChatContent from '../chat/chat-content'; import { renderModelIcon } from '../chat/header/model-selector'; import MyEmpty from './MyEmpty'; import CompletionInput from './completion-input'; interface Props { title?: string; completionApi?: string; chatMode: IChatDialogueSchema['chat_mode']; chatParams?: { select_param?: string; } & Record; model?: string; } function ChatDialog({ title, chatMode, completionApi, chatParams, model = '' }: Props) { const { chat } = useChat({ queryAgentURL: completionApi }); const [loading, setLoading] = useState(false); const [list, setList] = useState([]); const [open, setOpen] = useState(false); const { data } = useRequest( async () => { const [, res] = await apiInterceptors(newDialogue({ chat_mode: chatMode })); return res; }, { ready: !!chatMode, }, ); const handleChat = useCallback( (content: string) => { if (!data) return; return new Promise(resolve => { const tempList: IChatDialogueMessageSchema[] = [ ...list, { role: 'human', context: content, model_name: model, order: 0, time_stamp: 0 }, { role: 'view', context: '', model_name: model, order: 0, time_stamp: 0 }, ]; const index = tempList.length - 1; setList([...tempList]); setLoading(true); chat({ chatId: data?.conv_uid, data: { ...chatParams, chat_mode: chatMode, model_name: model, user_input: content }, onMessage: message => { tempList[index].context = message; setList([...tempList]); }, onDone: () => { resolve(); }, onClose: () => { resolve(); }, onError: message => { tempList[index].context = message; setList([...tempList]); resolve(); }, }).finally(() => { setLoading(false); }); }); }, [chat, list, data?.conv_uid], ); return (
{title &&
{title}
}
{list.map((item, index) => ( <> {chatParams?.chat_mode === 'chat_agent' ? ( ) : ( )} ))} {!list.length && }
{model &&
{renderModelIcon(model)}
}
{ setOpen(!open); }} >
); } export default ChatDialog;