/* 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 . For commercial licensing, please contact support@quantumnous.com */ import React, { useCallback, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, Select, Toast, Tooltip } from '@/components/ui'; import { Layers } from 'lucide-react'; import { API } from '../../../helpers/api'; import { showError } from '../../../helpers'; import OrgModal from './OrgModal'; const getOrgHeaders = (orgId) => ({ 'X-Org-Id': String(orgId) }); // 解析分组字段:兼容 JSON 字符串/数组;为空表示该维度未配置分组。 const parseGroups = (raw) => { if (Array.isArray(raw)) return raw; if (typeof raw === 'string' && raw.trim()) { try { const arr = JSON.parse(raw); if (Array.isArray(arr)) return arr; } catch { /* ignore */ } } return []; }; /** * 部门/项目「模型 / 分组」限制配置入口(操作列复用组件)。 * 生效规则:成员个人、项目、部门按维度取交集,由后端鉴权链路裁决。 * * props: * - orgId * - record: 行数据(含 model_limits_enabled / model_limits / groups) * - apiBase: '/api/org/dept' 或 '/api/org/project' * - scopeLabel: '部门' | '项目'(用于弹窗标题与文案) * - onSaved: 保存成功后回调(刷新列表) */ // variant='link':企业中心已按原型对齐的列表页使用 link 按钮(无图标、13px);默认保持原描边按钮 const OrgScopedLimitButtons = ({ orgId, record, apiBase, scopeLabel, onSaved, variant = 'default', }) => { const { t } = useTranslation(); const [modelModalVisible, setModelModalVisible] = useState(false); const [orgModels, setOrgModels] = useState([]); const [loadingModels, setLoadingModels] = useState(false); const [selectedModels, setSelectedModels] = useState([]); const [groupsModalVisible, setGroupsModalVisible] = useState(false); const [orgGroups, setOrgGroups] = useState([]); const [selectedGroups, setSelectedGroups] = useState([]); const scopeName = record?.name || ''; const openModelModal = useCallback(async () => { setSelectedModels( record.model_limits_enabled && record.model_limits ? record.model_limits.split(',').filter(Boolean) : [], ); setModelModalVisible(true); setLoadingModels(true); try { const res = await API.get('/api/org/models', { headers: getOrgHeaders(orgId) }); if (res.data.success) { setOrgModels((res.data.data || []).map((m) => ({ label: m, value: m }))); } } catch { /* ignore */ } setLoadingModels(false); }, [orgId, record]); const openGroupsModal = useCallback(async () => { setSelectedGroups(parseGroups(record?.groups)); setGroupsModalVisible(true); try { const res = await API.get('/api/org/info', { headers: getOrgHeaders(orgId) }); if (res.data.success) { const org = res.data.data?.organization || {}; let groups = []; if (Array.isArray(org.groups)) { groups = org.groups; } else if (typeof org.groups === 'string' && org.groups.trim()) { try { groups = JSON.parse(org.groups); } catch { groups = []; } } if (!groups.length && org.group) groups = [org.group]; setOrgGroups(groups.map((g) => ({ label: g, value: g }))); } } catch { /* ignore */ } }, [orgId, record]); const handleModelSave = useCallback(async () => { const enabled = selectedModels.length > 0; try { const res = await API.put( `${apiBase}/${record.id}/models`, { model_limits_enabled: enabled, model_limits: selectedModels.join(',') }, { headers: getOrgHeaders(orgId) }, ); if (res.data.success) { Toast.success(t('模型限制已更新')); setModelModalVisible(false); onSaved && onSaved(); } else { showError(res.data.message); } } catch (err) { showError(err.message); } }, [apiBase, record, orgId, selectedModels, onSaved, t]); const handleGroupsSave = useCallback(async () => { try { const res = await API.put( `${apiBase}/${record.id}/groups`, { groups: selectedGroups }, { headers: getOrgHeaders(orgId) }, ); if (res.data.success) { Toast.success(t('分组已更新')); setGroupsModalVisible(false); onSaved && onSaved(); } else { showError(res.data.message); } } catch (err) { showError(err.message); } }, [apiBase, record, orgId, selectedGroups, onSaved, t]); const modelConfigured = record.model_limits_enabled; const groupConfigured = parseGroups(record?.groups).length > 0; return ( <> {/* 模型限制 Modal */} setModelModalVisible(false)} okText={t('保存')} cancelText={t('取消')} width={520} >

{t( '限制该{{scope}}成员默认可使用的模型。留空则该维度不限制;若成员个人、项目、部门同时配置,最终可用模型取交集。', { scope: t(scopeLabel) }, )}

); }; export default OrgScopedLimitButtons;