Files
monkey-ai-web/src/pages/Setting/Operation/SettingsSidebarModulesAdmin.jsx
T

832 lines
26 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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, { useState, useEffect, useContext } from 'react';
import { useTranslation } from 'react-i18next';
import {
Card,
Form,
Button,
Switch,
Row,
Col,
Select,
Typography,
} from '@/components/ui';
import { API, showSuccess, showError } from '../../../helpers';
import { StatusContext } from '../../../context/Status';
import {
getVisibleGroupOptions,
normalizeGroups,
} from '../../../helpers/navigationVisibility';
import { getDefaultSidebarModulesAdmin } from '../../../constants/sidebarModules';
import {
CLOUD_BRAIN_UI_ENABLED,
isCloudBrainOrgModule,
} from '../../../config/cloudBrainUi';
const { Text } = Typography;
const normalizeModuleGroups = (moduleGroups) => {
if (!moduleGroups || typeof moduleGroups !== 'object') return {};
return Object.keys(moduleGroups).reduce((acc, key) => {
acc[key] = normalizeGroups(moduleGroups[key]);
return acc;
}, {});
};
const withVisibilityMeta = (section = {}) => ({
...section,
groups: normalizeGroups(section.groups),
moduleGroups: normalizeModuleGroups(section.moduleGroups),
});
// Keep the settings UI in sync with runtime sidebar defaults.
const createDefaultSidebarModules = () =>
Object.entries(getDefaultSidebarModulesAdmin()).reduce(
(acc, [sectionKey, section]) => {
acc[sectionKey] = withVisibilityMeta(section);
return acc;
},
{},
);
const mergeSidebarModules = (savedModules = {}) => {
const defaults = createDefaultSidebarModules();
const merged = {};
Object.keys(defaults).forEach((sectionKey) => {
const savedSection = savedModules[sectionKey] || {};
merged[sectionKey] = withVisibilityMeta({
...defaults[sectionKey],
...savedSection,
});
});
Object.keys(savedModules || {}).forEach((sectionKey) => {
if (!merged[sectionKey]) {
const savedSection = savedModules[sectionKey] || {};
merged[sectionKey] = withVisibilityMeta(savedSection);
}
});
const ucFromDb = savedModules.userCenter || {};
const needsUsageRecordMigration =
ucFromDb.usageRecord === undefined &&
ucFromDb.usageLog === false &&
ucFromDb.taskLog === false;
if (needsUsageRecordMigration) {
merged.userCenter.usageRecord = false;
}
delete merged.userCenter.usageLog;
delete merged.userCenter.taskLog;
// 迁移清理:技能相关开关曾被错误放在体验中心区域(无任何消费方),
// 现已移至企业中心(orgCenter.*,由 OrgSidebar 与 /org 路由守卫消费)。
delete merged.experienceCenter.mySkills;
delete merged.experienceCenter.skillMarket;
delete merged.experienceCenter.skillLibrary;
delete merged.experienceCenter.skillAssignments;
return merged;
};
export default function SettingsSidebarModulesAdmin(props) {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const [groupOptions, setGroupOptions] = useState([]);
const [statusState, statusDispatch] = useContext(StatusContext);
const [sidebarModulesAdmin, setSidebarModulesAdmin] = useState(
createDefaultSidebarModules(),
);
const getMergedGroupOptions = (selectedGroups) =>
getVisibleGroupOptions(groupOptions, selectedGroups);
function handleSectionChange(sectionKey) {
return (checked) => {
setSidebarModulesAdmin((prev) => ({
...prev,
[sectionKey]: {
...prev[sectionKey],
enabled: checked,
},
}));
};
}
function handleSectionGroupsChange(sectionKey) {
return (groups) => {
setSidebarModulesAdmin((prev) => ({
...prev,
[sectionKey]: {
...prev[sectionKey],
groups: normalizeGroups(groups),
},
}));
};
}
function handleModuleChange(sectionKey, moduleKey) {
return (checked) => {
setSidebarModulesAdmin((prev) => ({
...prev,
[sectionKey]: {
...prev[sectionKey],
[moduleKey]: checked,
},
}));
};
}
function handleModuleGroupsChange(sectionKey, moduleKey) {
return (groups) => {
setSidebarModulesAdmin((prev) => ({
...prev,
[sectionKey]: {
...prev[sectionKey],
moduleGroups: {
...(prev[sectionKey]?.moduleGroups || {}),
[moduleKey]: normalizeGroups(groups),
},
},
}));
};
}
function resetSidebarModules() {
setSidebarModulesAdmin(createDefaultSidebarModules());
showSuccess(t('已重置为默认配置'));
}
async function onSubmit() {
setLoading(true);
try {
const res = await API.put('/api/option/', {
key: 'SidebarModulesAdmin',
value: JSON.stringify(sidebarModulesAdmin),
});
const { success, message } = res.data;
if (success) {
showSuccess(t('保存成功'));
statusDispatch({
type: 'set',
payload: {
...statusState.status,
SidebarModulesAdmin: JSON.stringify(sidebarModulesAdmin),
},
});
if (props.refresh) {
await props.refresh();
}
} else {
showError(message);
}
} catch (error) {
showError(t('保存失败,请重试'));
} finally {
setLoading(false);
}
}
const loadAllGroups = async () => {
try {
const res = await API.get('/api/group/');
if (res.data.success) {
const groups = Array.isArray(res.data.data) ? res.data.data : [];
setGroupOptions(
groups.map((group) => ({ value: group, label: group })),
);
}
} catch {
setGroupOptions([]);
}
};
useEffect(() => {
loadAllGroups();
}, []);
useEffect(() => {
if (props.options && props.options.SidebarModulesAdmin) {
try {
const modules = JSON.parse(props.options.SidebarModulesAdmin);
setSidebarModulesAdmin(mergeSidebarModules(modules));
} catch (error) {
setSidebarModulesAdmin(createDefaultSidebarModules());
}
}
}, [props.options]);
const sectionConfigs = [
{
key: 'experienceCenter',
title: t('体验中心'),
description: t('AI模型体验功能'),
modules: [
{
key: 'cloudBrain',
title: t('云端大脑(Beta)'),
description: t('精选专家异步任务工作空间'),
},
{
key: 'imageGeneration',
title: t('图片生成'),
description: t('AI图片生成功能'),
},
{
key: 'videoGeneration',
title: t('视频生成'),
description: t('AI视频生成功能'),
},
],
},
{
key: 'userCenter',
title: t('用户中心'),
description: t('用户个人中心功能'),
modules: [
{
key: 'dashboard',
title: t('数据看板'),
description: t('用户数据统计'),
},
{
key: 'usageRecord',
title: t('使用记录'),
description: t('API使用记录和任务执行记录'),
},
{
key: 'serviceDashboard',
title: t('服务监控'),
description: t('服务可用性监控'),
},
{
key: 'topup',
title: t('在线充值'),
description: t('跳转到充值页面'),
},
{
key: 'invoice',
title: t('发票管理'),
description: t('发票抬头和开票记录'),
},
{
key: 'invite',
title: t('邀请好友'),
description: t('邀请好友获得奖励'),
},
{
key: 'settings',
title: t('个人设置'),
description: t('个人信息设置'),
},
],
},
{
key: 'chat',
title: t('聊天区域'),
description: t('操练场和聊天功能'),
modules: [
{
key: 'playground',
title: t('操练场'),
description: t('AI模型测试环境'),
},
{ key: 'chat', title: t('聊天'), description: t('聊天会话管理') },
],
},
{
key: 'console',
title: t('控制台区域'),
description: t('数据管理、日志和监控入口'),
modules: [
{ key: 'detail', title: t('数据看板'), description: t('系统数据统计') },
{ key: 'token', title: t('密钥管理'), description: t('API密钥管理') },
{ key: 'log', title: t('使用日志'), description: t('API使用记录') },
{
key: 'midjourney',
title: t('绘图日志'),
description: t('绘图任务记录'),
},
{ key: 'task', title: t('任务日志'), description: t('系统任务记录') },
{
key: 'monitor',
title: t('监控中心'),
description: t('运行状态和监控面板'),
},
],
},
{
key: 'personal',
title: t('个人中心区域'),
description: t('控制台内的用户个人功能'),
modules: [
{ key: 'topup', title: t('钱包管理'), description: t('余额充值管理') },
{
key: 'personal',
title: t('个人设置'),
description: t('个人信息设置'),
},
],
},
{
key: 'analytics',
title: t('运营看板页面'),
description: t('控制 /console/analytics 页面 Tab 入口'),
// 该区域的消费方(Analytics 页守卫)只判 enabled,不做分组过滤;
// 展示分组选择器会让管理员误以为限制生效(实际静默失效),故隐藏。
groupsSupported: false,
modules: [
{
key: 'operations',
title: t('运营看板'),
description: t('平台运营数据统计'),
},
{
key: 'quality',
title: t('调用质量'),
description: t('调用成功率、兜底与失败分析'),
},
{
key: 'risk',
title: t('风控看板'),
description: t('高频低消耗和自动封号'),
},
{
key: 'org',
title: t('个人/企业消耗'),
description: t('个人与企业消耗统计'),
},
{
key: 'audit',
title: t('体验中心上传审计'),
description: t('体验中心上传审核入口'),
},
],
},
{
key: 'orgCenter',
title: t('企业中心'),
description: t('企业控制台侧边栏功能'),
// 企业中心按企业成员身份/权限位控制可见性(OrgSidebar),不消费
// C 端用户分组;隐藏分组选择器避免"配置了却静默失效"的误导。
groupsSupported: false,
modules: [
{
key: 'knowledge',
title: t('企业知识(Beta)'),
description: t('企业知识库、文档摄取与专家引用管理'),
},
{
key: 'cloudBrain',
title: t('云端大脑(Beta)'),
description: t('企业精选专家与任务产物'),
},
{
key: 'connectors',
title: t('Connector 管理'),
description: t('企业 Connector 实例、凭据、授权与审计'),
},
{
key: 'myConnectors',
title: t('我的 AI 能力 · 业务系统授权'),
description: t('员工查看自己的 Connector 授权范围(合并页页签)'),
},
{
key: 'mySkills',
title: t('我的 AI 能力 · 我的技能'),
description: t('员工有效技能集(合并页页签)'),
},
{
key: 'skillMarket',
title: t('技能超市 · 为企业启用'),
description: t(
'允许企业管理员在技能超市自助把平台技能加入企业技能库;关闭后仅平台可代为启用',
),
},
{
key: 'skillLibrary',
title: t('技能中心 · 企业技能库'),
description: t('企业 Skill 版本与定制(合并页页签)'),
},
{
key: 'skillAssignments',
title: t('分发与锁定'),
description: t('Skill 范围策略与成员预览'),
},
{
key: 'experts',
title: t('企业专家工坊'),
description: t('企业专家版本、模型与交付路径'),
},
{
key: 'agentAudit',
title: t('Agent 任务审计'),
description: t('专家任务元数据与正文审计'),
},
{
key: 'workbench',
title: t('工作台'),
description: t('员工自助工作台'),
},
{
key: 'dashboard',
title: t('数据看板'),
description: t('企业概览与额度统计'),
},
{
key: 'quality',
title: t('调用质量'),
description: t('企业调用成功率与失败分析'),
},
{
key: 'effectiveness',
title: t('效能分析'),
description: t('覆盖、质量、效率、治理与 A/B 实验'),
},
{
key: 'privateSkills',
title: t('技能中心 · 企业自研 Skill'),
description: t('企业私有 Skill 编辑、审核与发布(合并页页签)'),
},
{
key: 'analytics',
title: t('用量分析'),
description: t('模型与成员用量趋势'),
},
{
key: 'members',
title: t('成员管理'),
description: t('企业成员管理'),
},
{
key: 'memberAccounts',
title: t('员工账号'),
description: t('集中管理员工登录账号'),
},
{
key: 'departments',
title: t('组织架构'),
description: t('组织架构与部门预算'),
},
{
key: 'projects',
title: t('项目管理'),
description: t('项目成员与项目预算'),
},
{
key: 'deptManage',
title: t('部门管理(负责人)'),
description: t('部门负责人的本部门管理入口'),
},
{
key: 'projectManage',
title: t('我的项目(项目经理)'),
description: t('项目经理的项目管理入口'),
},
{
key: 'budgets',
title: t('预算中心'),
description: t('部门/项目/成员额度上限'),
},
{
key: 'requests',
title: t('审批中心'),
description: t('提额申请与审批'),
},
{ key: 'tokens', title: t('密钥管理'), description: t('企业密钥管理') },
{ key: 'logs', title: t('消耗明细'), description: t('企业消耗记录') },
{ key: 'audit', title: t('审计日志'), description: t('企业操作审计') },
{
key: 'recharge',
title: t('企业充值'),
description: t('企业余额充值'),
},
{
key: 'settings',
title: t('企业设置'),
description: t('企业资料与配置'),
},
],
},
{
key: 'admin',
title: t('管理员区域'),
description: t('系统管理功能'),
modules: [
{
key: 'analytics',
title: t('运营看板'),
description: t('运营数据统计分析'),
},
{ key: 'channel', title: t('渠道管理'), description: t('API渠道配置') },
{ key: 'models', title: t('模型管理'), description: t('AI模型配置') },
{
key: 'redemption',
title: t('兑换码管理'),
description: t('兑换码生成管理'),
},
{ key: 'user', title: t('用户管理'), description: t('用户账户管理') },
{
key: 'subscription',
title: t('会员卡管理'),
description: t('会员订阅和卡片配置'),
},
{
key: 'admin-token',
title: t('密钥管理'),
description: t('管理员密钥管理'),
},
{
key: 'organizations',
title: t('企业管理'),
description: t('企业账户管理'),
},
{
key: 'skill-platform',
title: t('平台 Skill 中心'),
description: t('Skill 来源、扫描、审核与发布'),
},
{
key: 'expert-platform',
title: t('平台专家中心'),
description: t('平台专家模板与模型能力验证'),
},
{ key: 'order', title: t('订单管理'), description: t('订单记录管理') },
{
key: 'invoice',
title: t('发票管理'),
description: t('发票审核管理'),
},
{
key: 'setting',
title: t('系统设置'),
description: t('系统参数配置'),
},
],
},
];
const visibleSectionConfigs = CLOUD_BRAIN_UI_ENABLED
? sectionConfigs
: sectionConfigs.map((section) => ({
...section,
modules: section.modules.filter((module) => {
if (section.key === 'experienceCenter') {
return !isCloudBrainOrgModule(module.key);
}
if (section.key === 'orgCenter') {
return !isCloudBrainOrgModule(module.key);
}
if (section.key === 'admin') {
return (
module.key !== 'skill-platform' &&
module.key !== 'expert-platform'
);
}
return true;
}),
}));
return (
<Card>
<Form.Section
text={t('侧边栏管理(全局控制)')}
extraText={t(
'全局控制侧边栏区域、功能和可见用户分组,管理员隐藏的功能用户无法启用',
)}
>
{visibleSectionConfigs.map((section) => {
const sectionConfig = sidebarModulesAdmin[section.key] || {};
const sectionGroups = normalizeGroups(sectionConfig.groups);
return (
<div key={section.key} style={{ marginBottom: '32px' }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
gap: '16px',
marginBottom: '16px',
padding: '12px 16px',
backgroundColor: 'var(--ui-color-fill-0)',
borderRadius: '8px',
border: '1px solid var(--ui-color-border)',
}}
>
<div style={{ flex: 1, minWidth: 0 }}>
<div
style={{
fontWeight: '600',
fontSize: '16px',
color: 'var(--ui-color-text-0)',
marginBottom: '4px',
}}
>
{section.title}
</div>
<Text
type='secondary'
size='small'
style={{
fontSize: '12px',
color: 'var(--ui-color-text-2)',
lineHeight: '1.4',
}}
>
{section.description}
</Text>
{section.groupsSupported !== false && (
<div style={{ marginTop: '10px', maxWidth: 420 }}>
<Text
type='secondary'
size='small'
style={{ display: 'block', marginBottom: '6px' }}
>
{t('区域可见用户分组(留空表示全部分组)')}
</Text>
<Select
multiple
filter
showClear
maxTagCount={3}
placeholder={t('选择分组')}
style={{ width: '100%' }}
value={sectionGroups}
optionList={getMergedGroupOptions(sectionGroups)}
onChange={handleSectionGroupsChange(section.key)}
disabled={!sectionConfig.enabled}
/>
</div>
)}
</div>
<Switch
checked={sectionConfig.enabled}
onChange={handleSectionChange(section.key)}
size='default'
/>
</div>
<Row gutter={[16, 16]}>
{section.modules.map((module) => {
const moduleGroups = normalizeGroups(
sectionConfig.moduleGroups?.[module.key],
);
return (
<Col key={module.key} xs={24} sm={12} md={8} lg={6} xl={6}>
<Card
bodyStyle={{ padding: '16px' }}
shadows='hover'
style={{
opacity: sectionConfig.enabled ? 1 : 0.5,
transition: 'opacity 0.2s',
}}
>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
height: '100%',
}}
>
<div style={{ flex: 1, textAlign: 'left' }}>
<div
style={{
fontWeight: '600',
fontSize: '14px',
color: 'var(--ui-color-text-0)',
marginBottom: '4px',
}}
>
{module.title}
</div>
<Text
type='secondary'
size='small'
style={{
fontSize: '12px',
color: 'var(--ui-color-text-2)',
lineHeight: '1.4',
display: 'block',
}}
>
{module.description}
</Text>
</div>
<div style={{ marginLeft: '16px' }}>
<Switch
checked={sectionConfig[module.key]}
onChange={handleModuleChange(
section.key,
module.key,
)}
size='default'
disabled={!sectionConfig.enabled}
/>
</div>
</div>
{section.groupsSupported !== false &&
sectionConfig.enabled &&
sectionConfig[module.key] && (
<div
style={{
borderTop: '1px solid var(--ui-color-border)',
marginTop: '12px',
paddingTop: '12px',
}}
>
<Text
type='secondary'
size='small'
style={{
display: 'block',
marginBottom: '6px',
}}
>
{t('功能可见用户分组(留空表示全部分组)')}
</Text>
<Select
multiple
filter
showClear
maxTagCount={2}
placeholder={t('选择分组')}
style={{ width: '100%' }}
value={moduleGroups}
optionList={getMergedGroupOptions(moduleGroups)}
onChange={handleModuleGroupsChange(
section.key,
module.key,
)}
/>
</div>
)}
</Card>
</Col>
);
})}
</Row>
</div>
);
})}
<div
style={{
display: 'flex',
gap: '12px',
justifyContent: 'flex-start',
alignItems: 'center',
paddingTop: '8px',
borderTop: '1px solid var(--ui-color-border)',
}}
>
<Button
size='default'
type='tertiary'
onClick={resetSidebarModules}
style={{
borderRadius: '6px',
fontWeight: '500',
}}
>
{t('重置为默认')}
</Button>
<Button
size='default'
type='primary'
onClick={onSubmit}
loading={loading}
style={{
borderRadius: '6px',
fontWeight: '500',
minWidth: '100px',
}}
>
{t('保存设置')}
</Button>
</div>
</Form.Section>
</Card>
);
}