/*
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
*/
// 系统设置 →「WeyAi」配置分组(方案 M8)。只对超级管理员展示;
// 这些 key 在后端登记为 root-only(setting/console_setting/admin_delegation.go)。
//
// 分区:网页端接入(总开关/地址/凭证)→ 开放范围(模式 + 黑名单)→
// 个人用户 / 企业用户(各自的开放规则 + 设备密钥默认分组)→ 客户端 → 桌面端下载。
// 个人与企业分开成两张卡,管理员一眼能看出"这条规则管的是谁"。
import React, {
useCallback,
useContext,
useEffect,
useRef,
useState,
} from 'react';
import {
Banner,
Button,
Card,
Col,
Form,
Input,
Radio,
RadioGroup,
Row,
Select,
Spin,
Switch,
Tag,
TagInput,
Typography,
} from '@/components/ui';
import { useTranslation } from 'react-i18next';
import { API, setStatusData, showError, showSuccess } from '../../../helpers';
import { StatusContext } from '../../../context/Status';
import { invalidateWeyAiAccessCache } from '../../../hooks/common/useWeyAiWebEntry';
import {
ACCESS_MODE_ALL,
ACCESS_MODE_LIMITED,
OPEN_LIST_KEYS,
planSaveStages,
} from './saveStages';
import './SettingsWeyAi.css';
const { Text } = Typography;
// 后端对结尾为 Token/Secret/Key 的配置项回读为该占位符(controller/option.go)
const MASKED_PLACEHOLDER = '***ALREADY_SET***';
const DEFAULT_RELEASE_BASE_URL = 'https://download.weytoken.com/releases';
const BOOL_KEYS = [
'WeyAiWebEnabled',
'WeyAiDownloadEnabled',
'WeyAiRemoteEnabled',
];
const LIST_KEYS = [
'WeyAiWebAccessGroups',
'WeyAiWebAccessOrgs',
'WeyAiWebAccessOrgGroups',
'WeyAiWebAccessUsers',
'WeyAiWebDenyUsers',
];
// 开放分组两项(个人 / 企业):后端 ValidateWeyAiWebAccessGroups 要求每一项都在
// 分组倍率表内
const ACCESS_GROUP_KEYS = [
['WeyAiWebAccessGroups', '个人用户'],
['WeyAiWebAccessOrgGroups', '企业用户'],
];
// 企业下拉一次拉多少条搜索结果;按 ID 回填名称时用后端分页上限
const ORG_SEARCH_PAGE_SIZE = 50;
const ORG_RESOLVE_PAGE_SIZE = 100;
const TEXT_KEYS = [
'WeyAiWebUrl',
'WeyAiWebAccessMode',
'WeyAiPersonalKeyGroup',
'WeyAiOrgKeyGroup',
'WeyAiDefaultModel',
'WeyAiReleaseBaseUrl',
'ClientMinVersion',
'WeyAiRemoteUrl',
];
const SECRET_KEY = 'WeyAiWebClientSecret';
// 公网访问(中转)调用 auth/identify 的密钥,回读同样是占位符
const REMOTE_SECRET_KEY = 'WeyAiRemoteClientSecret';
// 会作为地址保存、需要去掉结尾斜杠并做格式校验的配置项
const URL_KEYS = ['WeyAiWebUrl', 'WeyAiReleaseBaseUrl', 'WeyAiRemoteUrl'];
// 与后端 MinWeyAiWebClientSecretLength 一致:短于这个长度后端拒绝保存,
// 等于保存了别的、密钥没存——前端先拦
const MIN_SECRET_LENGTH = 32;
// 配置项 key → 页面上的名称:报错和"尚未提交"提示里用它,管理员看到的是
// 「企业开放分组」而不是 WeyAiWebAccessOrgGroups
const KEY_LABELS = {
WeyAiWebEnabled: '网页端接入',
WeyAiWebUrl: '网页端地址',
WeyAiWebClientSecret: '网页端客户端密钥',
WeyAiWebAccessMode: '开放范围',
WeyAiWebAccessGroups: '开放分组',
WeyAiWebAccessOrgs: '开放企业',
WeyAiWebAccessOrgGroups: '企业开放分组',
WeyAiWebAccessUsers: '开放用户 ID',
WeyAiWebDenyUsers: '黑名单用户 ID',
WeyAiPersonalKeyGroup: '个人用户密钥默认分组',
WeyAiOrgKeyGroup: '企业用户密钥默认分组',
WeyAiDefaultModel: '默认模型',
WeyAiDownloadEnabled: '桌面端下载',
WeyAiReleaseBaseUrl: '更新源地址',
ClientMinVersion: '客户端最低版本',
WeyAiRemoteEnabled: '公网访问',
WeyAiRemoteUrl: '公网访问地址',
WeyAiRemoteClientSecret: '公网访问客户端密钥',
};
// 开放范围模式的取值(ACCESS_MODE_ALL / ACCESS_MODE_LIMITED)在 saveStages.js;
// 读取时兼容 limited 写法。
// 密钥默认分组下拉里"不指定"那一项的哨兵值:后端存空串,Semi Select 用空串当
// value 会被当作未选中而显示 placeholder,所以界面上用哨兵、保存时映射回空串
const INHERIT_GROUP = '__inherit__';
// 列表类配置统一存为英文逗号分隔的字符串
const splitList = (value) =>
(value || '')
.split(',')
.map((item) => item.trim())
.filter(Boolean);
const joinList = (list) =>
Array.from(new Set((list || []).map((item) => `${item}`.trim())))
.filter(Boolean)
.join(',');
const trimTrailingSlash = (url) => (url || '').trim().replace(/\/+$/, '');
const createEmptyState = () => ({
WeyAiWebEnabled: false,
WeyAiWebUrl: '',
WeyAiWebAccessMode: ACCESS_MODE_ALL,
WeyAiWebAccessGroups: [],
WeyAiWebAccessOrgs: [],
WeyAiWebAccessOrgGroups: [],
WeyAiWebAccessUsers: [],
WeyAiWebDenyUsers: [],
WeyAiPersonalKeyGroup: '',
WeyAiOrgKeyGroup: '',
WeyAiDefaultModel: '',
WeyAiDownloadEnabled: false,
WeyAiReleaseBaseUrl: '',
ClientMinVersion: '',
WeyAiRemoteEnabled: false,
WeyAiRemoteUrl: '',
});
// 与后端 parseIdSet 口径一致:只认正整数,0 会被后端静默丢弃,前端直接拒掉
const isIdList = (list) => list.every((item) => /^[1-9]\d*$/.test(`${item}`));
// 分区卡片:标题带 + 说明 + 右侧标签,正文由调用方填
function Section({ title, desc, extra, accent, children }) {
return (
{title}
{desc && {desc}}
{extra &&
{extra}
}
{children}
);
}
function SubHead({ children }) {
return
{children}
;
}
// 设备密钥默认分组下拉(带标签)。value 为空串表示"不指定",界面上映射为
// INHERIT_GROUP。已保存的分组若已从分组倍率里删掉,仍要能回显出来,否则下拉
// 看起来像没选。Semi Select 不转发 aria-label(combobox 上写死为 selected),
// 所以标签用 id + aria-labelledby 关联,读屏器才念得出"个人用户密钥默认分组"。
function KeyGroupSelect({
id,
label,
value,
onChange,
groupOptions,
inheritLabel,
}) {
const options = [{ value: INHERIT_GROUP, label: inheritLabel }];
if (value && !groupOptions.some((opt) => opt.value === value)) {
options.push({ value, label: value });
}
// auto 是智能路由伪分组,后端 ValidateWeyAiKeyGroup 一律拒绝,不要让它出现在下拉里
options.push(...groupOptions.filter((opt) => opt.value !== 'auto'));
return (
<>
{label}