import DropdownButtons from '@/components/drop-down-buttons'; import PageTools from '@/components/page-tools'; import SealTable from '@/components/seal-table'; import SealColumn from '@/components/seal-table/components/seal-column'; import { PageAction } from '@/config'; import type { PageActionType } from '@/config/types'; import useExpandedRowKeys from '@/hooks/use-expanded-row-keys'; import useTableRowSelection from '@/hooks/use-table-row-selection'; import useTableSort from '@/hooks/use-table-sort'; import { handleBatchRequest } from '@/utils'; import { DeleteOutlined, EditOutlined, PlusOutlined, SyncOutlined, WechatWorkOutlined } from '@ant-design/icons'; import { PageContainer } from '@ant-design/pro-components'; import { Access, useAccess, useIntl, useNavigate } from '@umijs/max'; import { Button, Input, Modal, Space, message } from 'antd'; import dayjs from 'dayjs'; import { memo, useCallback, useState } from 'react'; import { MODELS_API, MODEL_INSTANCE_API, createModel, deleteModel, deleteModelInstance, queryModelInstancesList, updateModel } from '../apis'; import { modelSourceMap } from '../config'; import { FormData, ListItem, ModelInstanceListItem } from '../config/types'; import AddModal from './add-modal'; import InstanceItem from './instance-item'; import ViewLogsModal from './view-logs-modal'; interface ModelsProps { handleSearch: (e: any) => void; handleNameChange: (e: any) => void; fetchData: () => Promise; handleShowSizeChange: (page: number, size: number) => void; handlePageChange: (page: number, pageSize: number | undefined) => void; createModelsChunkRequest: () => void; queryParams: { page: number; perPage: number; query?: string; }; dataSource: ListItem[]; loading: boolean; total: number; } const Models: React.FC = ({ dataSource, handleNameChange, handleSearch, handleShowSizeChange, handlePageChange, queryParams, loading, total }) => { // const { modal } = App.useApp(); console.log('model list====2'); const access = useAccess(); const intl = useIntl(); const navigate = useNavigate(); const rowSelection = useTableRowSelection(); const { handleExpandChange, updateExpandedRowKeys, expandedRowKeys } = useExpandedRowKeys(); const { sortOrder, setSortOrder } = useTableSort({ defaultSortOrder: 'descend' }); const [openLogModal, setOpenLogModal] = useState(false); const [openAddModal, setOpenAddModal] = useState(false); const [action, setAction] = useState(PageAction.CREATE); const [title, setTitle] = useState(''); const [currentData, setCurrentData] = useState( undefined ); const [currentInstanceUrl, setCurrentInstanceUrl] = useState(''); const ActionList = [ { label: intl.formatMessage({ id: 'common.button.edit' }), key: 'edit', icon: }, { label: intl.formatMessage({ id: 'models.openinplayground' }), key: 'chat', icon: }, { label: intl.formatMessage({ id: 'common.button.delete' }), key: 'delete', danger: true, icon: } ]; const handleTableChange = (pagination: any, filters: any, sorter: any) => { console.log('handleTableChange=======', pagination, filters, sorter); setSortOrder(sorter.order); }; const handleAddModal = () => { setOpenAddModal(true); setAction(PageAction.CREATE); setTitle(intl.formatMessage({ id: 'models.button.deploy' })); }; const handleModalOk = useCallback( async (data: FormData) => { try { if (action === PageAction.CREATE) { await createModel({ data }); } if (action === PageAction.EDIT) { await updateModel({ data, id: currentData?.id as number }); } setOpenAddModal(false); message.success(intl.formatMessage({ id: 'common.message.success' })); } catch (error) {} }, [action, currentData] ); const handleModalCancel = useCallback(() => { console.log('handleModalCancel'); setOpenAddModal(false); }, []); const handleLogModalCancel = useCallback(() => { setOpenLogModal(false); }, []); const handleDelete = async (row: any) => { Modal.confirm({ title: '', content: intl.formatMessage( { id: 'common.delete.confirm' }, { type: intl.formatMessage({ id: 'models.table.models' }) } ), async onOk() { await deleteModel(row.id); message.success(intl.formatMessage({ id: 'common.message.success' })); updateExpandedRowKeys([row.id]); }, onCancel() { console.log('Cancel'); } }); }; const handleDeleteBatch = () => { Modal.confirm({ title: '', content: intl.formatMessage( { id: 'common.delete.confirm' }, { type: intl.formatMessage({ id: 'models.table.models' }) } ), async onOk() { await handleBatchRequest(rowSelection.selectedRowKeys, deleteModel); message.success(intl.formatMessage({ id: 'common.message.success' })); rowSelection.clearSelections(); updateExpandedRowKeys(rowSelection.selectedRowKeys); }, onCancel() { console.log('Cancel'); } }); }; const handleOpenPlayGround = (row: any) => { navigate(`/playground?model=${row.name}`); }; const handleViewLogs = async (row: any) => { try { setCurrentInstanceUrl(`${MODEL_INSTANCE_API}/${row.id}/logs`); setOpenLogModal(true); } catch (error) { console.log('error:', error); } }; const handleDeleteInstace = (row: any, list: ModelInstanceListItem[]) => { Modal.confirm({ title: '', content: intl.formatMessage( { id: 'common.delete.confirm' }, { type: intl.formatMessage({ id: 'models.instances' }) } ), async onOk() { await deleteModelInstance(row.id); message.success(intl.formatMessage({ id: 'common.message.success' })); if (list.length === 1) { updateExpandedRowKeys([row.model_id]); } }, onCancel() { console.log('Cancel'); } }); }; const getModelInstances = useCallback(async (row: any) => { const params = { id: row.id, page: 1, perPage: 100 }; const data = await queryModelInstancesList(params); return data.items || []; }, []); const generateChildrenRequestAPI = (params: any) => { return `${MODELS_API}/${params.id}/instances`; }; const handleEdit = (row: ListItem) => { setCurrentData(row); setOpenAddModal(true); setAction(PageAction.EDIT); setTitle(intl.formatMessage({ id: 'models.title.edit' })); }; const handleSelect = useCallback((val: any, row: ListItem) => { if (val === 'edit') { handleEdit(row); } if (val === 'chat') { handleOpenPlayGround(row); } if (val === 'delete') { handleDelete(row); } }, []); const handleChildSelect = useCallback( (val: any, row: ModelInstanceListItem, list: ModelInstanceListItem[]) => { if (val === 'delete') { handleDeleteInstace(row, list); } if (val === 'viewlog') { handleViewLogs(row); } }, [] ); const renderChildren = (list: any) => { return ( ); }; return ( <> } right={ } > { return modelSourceMap[text] || '-'; }} /> { return dayjs(val).format('YYYY-MM-DD HH:mm:ss'); }} /> { return !record.transition ? ( handleSelect(val, record)} > ) : null; }} /> ); }; export default memo(Models);