264 lines
7.2 KiB
React
264 lines
7.2 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { Button, Empty, Popconfirm, Space, Tag, Typography } from 'antd';
|
|
import { Table } from '@/components/ui';
|
|
import { ConsoleAlert as Alert } from '../console/ConsolePrimitives';
|
|
import { Plus } from '../common/icons/ActionIcons';
|
|
import { copy, showError, showSuccess } from '../../helpers';
|
|
import { deleteProfile, listCandidateModels, listProfiles } from './api';
|
|
import ProfileEditor from './ProfileEditor';
|
|
import SimulateModal from './SimulateModal';
|
|
|
|
const { Text } = Typography;
|
|
|
|
const ProfileTable = ({ mode, orgId, canWrite = true, emptyHint }) => {
|
|
const { t } = useTranslation();
|
|
const [list, setList] = useState([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState('');
|
|
const [editing, setEditing] = useState(null); // null 关闭;{} 新建;profile 编辑
|
|
const [simulating, setSimulating] = useState(null);
|
|
const [models, setModels] = useState([]);
|
|
|
|
const load = useCallback(async () => {
|
|
setLoading(true);
|
|
setError('');
|
|
try {
|
|
setList((await listProfiles(mode, orgId)) || []);
|
|
} catch (e) {
|
|
setError(e?.message || t('加载失败'));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [mode, orgId, t]);
|
|
|
|
useEffect(() => {
|
|
load();
|
|
}, [load]);
|
|
|
|
useEffect(() => {
|
|
if (!canWrite) return;
|
|
listCandidateModels(mode, orgId)
|
|
.then(setModels)
|
|
.catch(() => setModels([]));
|
|
}, [mode, orgId, canWrite]);
|
|
|
|
const strategyText = (s) =>
|
|
({ quality: t('质量优先'), balanced: t('平衡'), cost: t('成本优先') })[s] ||
|
|
s;
|
|
|
|
const columns = [
|
|
{
|
|
title: t('虚拟模型'),
|
|
dataIndex: 'virtual_model',
|
|
width: 280,
|
|
render: (v, r) => (
|
|
<div className='min-w-0'>
|
|
<Button
|
|
type='link'
|
|
size='small'
|
|
className='smart-router-mono smart-router-copy'
|
|
aria-label={`${t('复制')} ${v}`}
|
|
onClick={async () => {
|
|
if (await copy(v)) showSuccess(t('已复制'));
|
|
}}
|
|
>
|
|
{v}
|
|
</Button>
|
|
<div>
|
|
<Text
|
|
type='secondary'
|
|
className='smart-router-sub smart-router-name'
|
|
ellipsis={{ tooltip: r.name }}
|
|
>
|
|
{r.name}
|
|
</Text>
|
|
</div>
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
title: t('策略'),
|
|
dataIndex: 'strategy',
|
|
width: 110,
|
|
render: strategyText,
|
|
},
|
|
{
|
|
title: t('候选模型'),
|
|
dataIndex: 'models',
|
|
width: 100,
|
|
align: 'right',
|
|
render: (ms) => (
|
|
<span className='smart-router-num'>
|
|
{(ms || []).filter((m) => m.enabled).length}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
title: t('默认模型'),
|
|
dataIndex: 'default_model',
|
|
width: 220,
|
|
render: (v) => <span className='smart-router-mono'>{v}</span>,
|
|
},
|
|
{
|
|
title: t('状态'),
|
|
dataIndex: 'status',
|
|
width: 140,
|
|
render: (v, r) => (
|
|
<Space size={8}>
|
|
{v === 1 ? (
|
|
<Tag color='success' variant='filled'>
|
|
{t('启用')}
|
|
</Tag>
|
|
) : (
|
|
<Tag color='default' variant='filled'>
|
|
{t('停用')}
|
|
</Tag>
|
|
)}
|
|
{r.jev_allowed && (
|
|
<Tag color='blue' variant='filled'>
|
|
Jev
|
|
</Tag>
|
|
)}
|
|
</Space>
|
|
),
|
|
},
|
|
{
|
|
title: t('操作'),
|
|
key: 'actions',
|
|
fixed: 'right',
|
|
width: 180,
|
|
render: (_, r) => (
|
|
<Space size={8} wrap>
|
|
<Button
|
|
type='link'
|
|
size='small'
|
|
onClick={() => setSimulating(r)}
|
|
disabled={!canWrite}
|
|
>
|
|
{t('试路由')}
|
|
</Button>
|
|
{canWrite && (
|
|
<>
|
|
<Button type='link' size='small' onClick={() => setEditing(r)}>
|
|
{t('编辑')}
|
|
</Button>
|
|
<Popconfirm
|
|
title={t('确认删除该集合?')}
|
|
description={t('使用该虚拟模型名的请求将返回"模型不存在"。')}
|
|
okButtonProps={{ danger: true }}
|
|
onConfirm={async () => {
|
|
try {
|
|
await deleteProfile(mode, orgId, r.id);
|
|
showSuccess(t('已删除'));
|
|
load();
|
|
} catch (e) {
|
|
if (e?.message) showError(e.message);
|
|
}
|
|
}}
|
|
>
|
|
<Button type='link' size='small' danger>
|
|
{t('删除')}
|
|
</Button>
|
|
</Popconfirm>
|
|
</>
|
|
)}
|
|
</Space>
|
|
),
|
|
},
|
|
];
|
|
|
|
return (
|
|
<div className='smart-router-stack'>
|
|
<div className='smart-router-toolbar'>
|
|
<Text type='secondary' className='smart-router-sub'>
|
|
{emptyHint}
|
|
</Text>
|
|
{canWrite && (
|
|
<Button
|
|
type='primary'
|
|
icon={<Plus size={16} />}
|
|
onClick={() => setEditing({})}
|
|
>
|
|
{t('新建集合')}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
{error ? (
|
|
<Alert
|
|
type='error'
|
|
showIcon
|
|
title={error}
|
|
action={
|
|
<Button size='small' onClick={load}>
|
|
{t('重试')}
|
|
</Button>
|
|
}
|
|
/>
|
|
) : (
|
|
<Table
|
|
rowKey='id'
|
|
size='middle'
|
|
loading={loading}
|
|
columns={columns}
|
|
dataSource={list}
|
|
pagination={false}
|
|
scroll={{ x: 'max-content' }}
|
|
empty={
|
|
<Empty
|
|
image={Empty.PRESENTED_IMAGE_SIMPLE}
|
|
description={t('还没有智能路由集合')}
|
|
>
|
|
{canWrite && (
|
|
<Button type='primary' onClick={() => setEditing({})}>
|
|
{t('新建集合')}
|
|
</Button>
|
|
)}
|
|
</Empty>
|
|
}
|
|
/>
|
|
)}
|
|
<ProfileEditor
|
|
visible={editing !== null}
|
|
mode={mode}
|
|
orgId={orgId}
|
|
profile={editing && editing.id ? editing : null}
|
|
candidateModels={models}
|
|
onClose={() => setEditing(null)}
|
|
onSaved={() => {
|
|
setEditing(null);
|
|
load();
|
|
}}
|
|
/>
|
|
<SimulateModal
|
|
visible={!!simulating}
|
|
mode={mode}
|
|
orgId={orgId}
|
|
profile={simulating}
|
|
onClose={() => setSimulating(null)}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default ProfileTable;
|