import { apiInterceptors, postChatModeParamsFileLoad } from '@/client/api'; import { dbMapper } from '@/utils'; import { FolderAddOutlined, LoadingOutlined, SwapOutlined } from '@ant-design/icons'; import { useRequest } from 'ahooks'; import type { MenuProps } from 'antd'; import { Dropdown, Spin, Upload } from 'antd'; import React, { useContext, useMemo, useState } from 'react'; import { MobileChatContext } from '../'; import OptionIcon from './OptionIcon'; const Resource: React.FC = () => { const { appInfo, resourceList, scene, model, conv_uid, getChatHistoryRun, setResource, resource } = useContext(MobileChatContext); const [selectedVal, setSelectedVal] = useState(null); // 资源类型 const resourceVal = useMemo(() => { return appInfo?.param_need?.filter(item => item.type === 'resource')?.[0]?.value; }, [appInfo]); const items: MenuProps['items'] = useMemo(() => { if (resourceList && resourceList.length > 0) { return resourceList.map(item => { return { label: (
{ setSelectedVal(item); setResource(item.space_id || item.param); }} > {item.param}
), key: item.space_id || item.param, }; }); } return []; }, [resourceList, setResource]); // 上传文件 const { run: uploadFile, loading } = useRequest( async formData => { const [, res] = await apiInterceptors( postChatModeParamsFileLoad({ convUid: conv_uid, chatMode: scene, data: formData, model, config: { timeout: 1000 * 60 * 60, }, }), ); setResource(res); return res; }, { manual: true, onSuccess: async () => { await getChatHistoryRun(); }, }, ); // 上传文件变化 const handleFileChange = async (info: any) => { const formData = new FormData(); formData.append('doc_file', info?.file); await uploadFile(formData); }; // 上传文件展示内容 const uploadContent = useMemo(() => { if (loading) { return (
} /> 上传中
); } if (resource) { return (
{resource.file_name}
); } return (
上传文件
); }, [loading, resource]); const renderContent = () => { switch (resourceVal) { case 'excel_file': case 'text_file': case 'image_file': return (
false} onChange={handleFileChange} className='flex h-full w-full items-center justify-center' > {uploadContent}
); case 'database': case 'knowledge': case 'plugin': case 'awel_flow': if (!resourceList?.length) { return null; } return (
{selectedVal?.param || resourceList?.[0]?.param}
); } }; return <>{renderContent()}; }; export default Resource;