Files
monkey-ai-web/src/components/table/channels/modals/EditChannelModal.jsx
T
qiubaishen 61294dc597 merge: 合入总仓库 weiyuan(bfdfb339) web/ 新功能,保留 ui-optimization 的 UI 重构
来源:luoguang/monkey-ai weiyuan@bfdfb339 的 web/(经 sync/main-weiyuan-web-bfdfb339 同步)
同步基准:总仓库 a2ca13cb = 本仓库初始导入 d8f3aee2

冲突处理原则:保留 ui-optimization 的 UI(样式/结构/组件),嫁接 weiyuan 的功能逻辑。
- i18n:按键三方合并;ours 的「令牌→密钥」改名键上移植 theirs 的术语修订
- task-logs / OrgLogs:ours 已手工移植同一功能(6ad95e2),保留 ours
- 删除:OrgCreate(theirs 下线 /org/create)、旧 image 组件与 VideoRegenerateModal(theirs 体验中心重写)、
  OrgAvatarSection / PricingBentoPage / CommissionConsole(ours 重构中已移除)
- 测试:users-pagination 的 i18next mock 补 t();theirs 新增文件补 license header
2026-11-10 12:01:21 +08:00

3881 lines
153 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 '../../../console/module-alignment.css';
import React, { useEffect, useState, useRef, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import {
API,
showError,
showInfo,
showSuccess,
verifyJSON,
} from '../../../../helpers';
import { useIsMobile } from '../../../../hooks/common/useIsMobile';
import { CHANNEL_OPTIONS } from '../../../../constants';
import {
SideSheet,
Space,
Spin,
Button,
Typography,
Checkbox,
Banner,
Modal,
ImagePreview,
Card,
Tag,
Avatar,
Form,
Row,
Col,
Highlight,
} from '@/components/ui';
import {
getChannelModels,
copy,
getChannelIcon,
getModelCategories,
selectFilter,
} from '../../../../helpers';
import ModelSelectModal from './ModelSelectModal';
import { mergeChannelSettings } from '../../../../helpers/baiduChannelSettings';
import JSONEditor from '../../../common/ui/JSONEditor';
import SecureVerificationModal from '../../../common/modals/SecureVerificationModal';
import ChannelKeyDisplay from '../../../common/ui/ChannelKeyDisplay';
import { useSecureVerification } from '../../../../hooks/common/useSecureVerification';
import { createApiCalls } from '../../../../services/secureVerification';
import {
IconServer,
IconSetting,
IconCode,
IconGlobe,
IconBolt,
} from '@/components/ui/icons';
const { Text } = Typography;
const MODEL_MAPPING_EXAMPLE = {
'gpt-3.5-turbo': 'gpt-3.5-turbo-0125',
};
const STATUS_CODE_MAPPING_EXAMPLE = {
400: '500',
};
const REGION_EXAMPLE = {
default: 'global',
'gemini-1.5-pro-002': 'europe-west2',
'gemini-1.5-flash-002': 'europe-west2',
'claude-3-5-sonnet-20240620': 'europe-west1',
};
// 支持并且已适配通过接口获取模型列表的渠道类型
const MODEL_FETCHABLE_TYPES = new Set([
1, 4, 14, 34, 17, 26, 27, 24, 47, 25, 20, 23, 31, 35, 40, 42, 48, 43,
]);
function type2secretPrompt(type) {
// inputs.type === 15 ? '按照如下格式输入:APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入:APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥')
switch (type) {
case 15:
return '按照如下格式输入:APIKey|SecretKey';
case 18:
return '按照如下格式输入:APPID|APISecret|APIKey';
case 22:
return '按照如下格式输入:APIKey-AppId,例如:fastgpt-0sp2gtvfdgyi4k30jwlgwf1i-64f335d84283f05518e9e041';
case 23:
return '按照如下格式输入:AppId|SecretId|SecretKey';
case 33:
return '按照如下格式输入:Ak|Sk|Region';
case 45:
return '请输入渠道对应的鉴权密钥, 豆包语音输入:AppId|AccessToken';
case 50:
return '按照如下格式输入: AccessKey|SecretKey, 如果上游是New API,则直接输ApiKey';
case 51:
return '按照如下格式输入: AccessKey|SecretAccessKey';
case 57:
case 59:
return '按照如下格式输入:SecretId|SecretKey|SubAppId[|Region]';
default:
return '请输入渠道对应的鉴权密钥';
}
}
// 工作时间小时选项列表(0-23),提升到组件外部避免重复创建
const WORK_HOURS_OPTIONS = Array.from({ length: 24 }, (_, i) => ({
value: i,
label: `${String(i).padStart(2, '0')}:00`,
}));
// 时区选项列表
const TIMEZONE_OPTIONS = [
{ value: 'Asia/Shanghai', label: 'Asia/Shanghai (UTC+8)' },
{ value: 'Asia/Tokyo', label: 'Asia/Tokyo (UTC+9)' },
{ value: 'Asia/Singapore', label: 'Asia/Singapore (UTC+8)' },
{ value: 'Asia/Hong_Kong', label: 'Asia/Hong_Kong (UTC+8)' },
{ value: 'America/New_York', label: 'America/New_York (UTC-5)' },
{ value: 'America/Los_Angeles', label: 'America/Los_Angeles (UTC-8)' },
{ value: 'America/Chicago', label: 'America/Chicago (UTC-6)' },
{ value: 'Europe/London', label: 'Europe/London (UTC+0)' },
{ value: 'Europe/Paris', label: 'Europe/Paris (UTC+1)' },
{ value: 'Europe/Berlin', label: 'Europe/Berlin (UTC+1)' },
{ value: 'Pacific/Auckland', label: 'Pacific/Auckland (UTC+12)' },
{ value: 'Australia/Sydney', label: 'Australia/Sydney (UTC+10)' },
{ value: 'UTC', label: 'UTC (UTC+0)' },
];
// 表单初始值定义在组件外部,保持引用稳定,避免 Semi Design Form 的 initValues 在每次渲染时创建新对象
const originInputs = {
name: '',
type: 1,
key: '',
openai_organization: '',
max_input_tokens: 0,
base_url: '',
other: '',
model_mapping: '',
status_code_mapping: '',
models: [],
auto_ban: 1,
test_model: '',
groups: ['default'],
priority: 0,
weight: 0,
fallback_channel_ids: [], // 兜底渠道 ID 数组,按数组顺序为兜底链;空数组 = 不启用兜底
fallback_primary_attempts: 1, // 主渠道连续失败达到该次数才切兜底(1 = 失败即切)
fallback_hop_attempts: 1, // 兜底链每一跳连续失败达到该次数才切下一个
region: '', // 渠道地域(CN/US/EU/SG/JP),用于跨境合规判定;空 = 未标记
daily_quota_limit: 0,
work_hours_start: undefined,
work_hours_end: undefined,
work_hours_timezone: 'Asia/Shanghai',
tag: '',
multi_key_mode: 'random',
// 渠道额外设置的默认值
ali_data_inspection_disabled: false,
baidu_liveness_header_mode: 'legacy',
force_format: false,
thinking_to_content: false,
proxy: '',
pass_through_body_enabled: false,
chat_image_to_images_enabled: false,
system_prompt: '',
system_prompt_override: false,
client_only: false, // 只接受 Claude Code 客户端请求
bound_user_ids: '', // 绑定用户ID列表
os_restriction: [], // 操作系统白名单,仅 Claude 原生渠道(type=14)生效,空数组=无限制
client_type_restriction: [], // 客户端类型白名单(cli/vscode),仅 type=14 生效
client_version_min: '', // 客户端版本下界(x.y.z),空=从最早
client_version_max: '', // 客户端版本上界(x.y.z),空=至最新
sticky_user_id_fill: false, // 为缺失的 metadata.user_id 补会话内稳定值,让上游按 user_id 哈希粘号
sticky_user_id_mode: 'native', // 注入形态:native(仿 Claude Code 原生)/ opaque
cache_breakpoint_fill: false, // 给完全没有 cache_control 的大上下文请求补一个缓存断点
cache_breakpoint_min_tokens: 0, // 注入门限(估算 prompt token),0=用后端默认 10000
settings: '',
// 仅 Vertex: 密钥格式(存入 settings.vertex_key_type)
vertex_key_type: 'json',
// 仅 AWS: 密钥格式和区域(存入 settings.aws_key_type 和 settings.aws_region)
aws_key_type: 'ak_sk',
// 企业账户设置
is_enterprise_account: false,
// 字段透传控制默认值
allow_service_tier: false,
disable_store: false, // false = 允许透传(默认开启)
allow_safety_identifier: false,
};
const EditChannelModal = (props) => {
const { t } = useTranslation();
const channelId = props.editingChannel.id;
const isEdit = channelId !== undefined;
const [loading, setLoading] = useState(isEdit);
const isMobile = useIsMobile();
const handleCancel = () => {
props.handleClose();
};
const [batch, setBatch] = useState(false);
const [multiToSingle, setMultiToSingle] = useState(false);
const [multiKeyMode, setMultiKeyMode] = useState('random');
const [autoBan, setAutoBan] = useState(true);
const [inputs, setInputs] = useState(originInputs);
const [originModelOptions, setOriginModelOptions] = useState([]);
const [modelOptions, setModelOptions] = useState([]);
const [groupOptions, setGroupOptions] = useState([]);
const [basicModels, setBasicModels] = useState([]);
const [fullModels, setFullModels] = useState([]);
const [modelGroups, setModelGroups] = useState([]);
const [customModel, setCustomModel] = useState('');
const [modalImageUrl] = useState('');
const [isModalOpenurl, setIsModalOpenurl] = useState(false);
const [modelModalVisible, setModelModalVisible] = useState(false);
const [fetchedModels, setFetchedModels] = useState([]);
const formApiRef = useRef(null);
const [vertexKeys, setVertexKeys] = useState([]);
const [vertexFileList, setVertexFileList] = useState([]);
const vertexErroredNames = useRef(new Set()); // 避免重复报错
const [isMultiKeyChannel, setIsMultiKeyChannel] = useState(false);
const [channelSearchValue, setChannelSearchValue] = useState('');
const [useManualInput, setUseManualInput] = useState(false); // 是否使用手动输入模式
const [keyMode, setKeyMode] = useState('append'); // 密钥模式:replace(覆盖)或 append(追加)
const [, setIsEnterpriseAccount] = useState(false); // 是否为企业账户
const [doubaoApiEditUnlocked, setDoubaoApiEditUnlocked] = useState(false); // 豆包渠道自定义 API 地址隐藏入口
const redirectModelList = useMemo(() => {
const mapping = inputs.model_mapping;
if (typeof mapping !== 'string') return [];
const trimmed = mapping.trim();
if (!trimmed) return [];
try {
const parsed = JSON.parse(trimmed);
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
return [];
}
const values = Object.values(parsed)
.map((value) => (typeof value === 'string' ? value.trim() : undefined))
.filter((value) => value);
return Array.from(new Set(values));
} catch (error) {
return [];
}
}, [inputs.model_mapping]);
// 密钥显示状态
const [keyDisplayState, setKeyDisplayState] = useState({
showModal: false,
keyData: '',
});
// 专门的2FA验证状态(用于TwoFactorAuthModal)
const [_show2FAVerifyModal, _setShow2FAVerifyModal] = useState(false);
const [_verifyCode, _setVerifyCode] = useState('');
const [_verifyLoading, _setVerifyLoading] = useState(false);
// 兜底渠道候选列表(用于"兜底渠道"下拉选择)
// 走专用接口拉全部渠道(不分页、含禁用),过滤掉自己;
// 禁用渠道作为 disabled option 仅用于回显,不能新增
// 请求序号:弹窗切换渠道时丢弃过期响应,避免 A 的数据落到 B 上
const fallbackLoadSeqRef = useRef(0);
const [fallbackCandidates, setFallbackCandidates] = useState([]);
const [fallbackCandidatesLoaded, setFallbackCandidatesLoaded] =
useState(false);
const loadFallbackCandidates = async (seq, selfId) => {
try {
const res = await API.get('/api/channel/fallback_candidates');
if (seq !== fallbackLoadSeqRef.current) return;
if (res?.data?.success && Array.isArray(res.data.data)) {
setFallbackCandidates(
res.data.data
.filter((ch) => ch.id !== selfId)
.map((ch) => {
if (ch.status !== 1) {
return {
value: ch.id,
label: `#${ch.id} ${ch.name}${t('(已禁用)')}`,
disabled: true,
};
}
const parts = [`#${ch.id} ${ch.name}`];
if (ch.group) parts.push(ch.group);
if (ch.region) parts.push(ch.region);
return { value: ch.id, label: parts.join(' · ') };
}),
);
setFallbackCandidatesLoaded(true);
}
} catch (err) {
console.error('加载兜底渠道候选失败:', err);
}
};
// 反向引用:当前渠道被哪些其他渠道列为兜底(Phase 3)
const [fallbackReferrers, setFallbackReferrers] = useState([]);
const loadFallbackReferrers = async (seq, selfId) => {
if (selfId === undefined || selfId === null) return;
try {
const res = await API.get(`/api/channel/${selfId}/fallback_referrers`);
if (seq !== fallbackLoadSeqRef.current) return;
if (res?.data?.success && Array.isArray(res.data.data)) {
setFallbackReferrers(res.data.data);
}
} catch (err) {
console.error('加载反向引用失败:', err);
}
};
const reloadFallbackData = () => {
fallbackLoadSeqRef.current += 1;
const seq = fallbackLoadSeqRef.current;
setFallbackReferrers([]);
setFallbackCandidates([]);
setFallbackCandidatesLoaded(false);
loadFallbackCandidates(seq, channelId);
loadFallbackReferrers(seq, channelId);
};
// 兜底链下拉选项:候选 + 链中已存在但候选里没有的 ID(渠道已删除)。
// 仅在候选加载成功后才补"已删除",避免加载中把全部 ID 误标为已删除
const fallbackOptionList = useMemo(() => {
const ids = Array.isArray(inputs.fallback_channel_ids)
? inputs.fallback_channel_ids
: [];
if (!fallbackCandidatesLoaded || ids.length === 0) {
return fallbackCandidates;
}
const known = new Set(fallbackCandidates.map((o) => o.value));
const missing = ids
.filter((id) => !known.has(id) && id !== channelId)
.map((id) => ({
value: id,
label: `#${id}${t('(已删除)')}`,
disabled: true,
}));
return missing.length
? [...fallbackCandidates, ...missing]
: fallbackCandidates;
}, [
fallbackCandidates,
fallbackCandidatesLoaded,
inputs.fallback_channel_ids,
channelId,
t,
]);
const doubaoApiClickCountRef = useRef(0);
const initialModelsRef = useRef([]);
const initialModelMappingRef = useRef('');
// 使用通用安全验证 Hook
const {
isModalVisible,
verificationMethods,
verificationState,
withVerification,
executeVerification,
cancelVerification,
setVerificationCode,
switchVerificationMethod,
} = useSecureVerification({
onSuccess: (result) => {
// 验证成功后显示密钥
console.log('Verification success, result:', result);
if (result && result.success && result.data?.key) {
showSuccess(t('密钥获取成功'));
setKeyDisplayState({
showModal: true,
keyData: result.data.key,
});
} else if (result && result.key) {
// 直接返回了 key(没有包装在 data 中)
showSuccess(t('密钥获取成功'));
setKeyDisplayState({
showModal: true,
keyData: result.key,
});
}
},
});
// 重置密钥显示状态
const resetKeyDisplayState = () => {
setKeyDisplayState({
showModal: false,
keyData: '',
});
};
const handleApiConfigSecretClick = () => {
if (inputs.type !== 45) return;
const next = doubaoApiClickCountRef.current + 1;
doubaoApiClickCountRef.current = next;
if (next >= 10) {
setDoubaoApiEditUnlocked((unlocked) => {
if (!unlocked) {
showInfo(t('已解锁豆包自定义 API 地址编辑'));
}
return true;
});
}
};
// 渠道额外设置状态
const [channelSettings, setChannelSettings] = useState({
ali_data_inspection_disabled: false,
force_format: false,
thinking_to_content: false,
proxy: '',
pass_through_body_enabled: false,
chat_image_to_images_enabled: false,
system_prompt: '',
client_only: false,
bound_user_ids: '',
os_restriction: [],
client_type_restriction: [],
client_version_min: '',
client_version_max: '',
sticky_user_id_fill: false,
sticky_user_id_mode: 'native',
cache_breakpoint_fill: false,
cache_breakpoint_min_tokens: 0,
});
const showApiConfigCard = true; // 控制是否显示 API 配置卡片
const getInitValues = () => ({
...originInputs,
models: [],
groups: ['default'],
});
// 处理渠道额外设置的更新
const handleChannelSettingsChange = (key, value) => {
// 更新内部状态
setChannelSettings((prev) => ({ ...prev, [key]: value }));
// 同步更新到表单字段
if (formApiRef.current) {
formApiRef.current.setValue(key, value);
}
// 同步更新inputs状态
setInputs((prev) => ({ ...prev, [key]: value }));
// 生成setting JSON并更新
const newSettings = { ...channelSettings, [key]: value };
const settingsJson = JSON.stringify(newSettings);
handleInputChange('setting', settingsJson);
};
const handleChannelOtherSettingsChange = (key, value) => {
// 更新内部状态
setChannelSettings((prev) => ({ ...prev, [key]: value }));
// 同步更新到表单字段
if (formApiRef.current) {
formApiRef.current.setValue(key, value);
}
// 同步更新inputs状态
setInputs((prev) => ({ ...prev, [key]: value }));
// 需要更新settings,是一个json,例如{"azure_responses_version": "preview"}
let settings = {};
if (inputs.settings) {
try {
settings = JSON.parse(inputs.settings);
} catch (error) {
console.error('解析设置失败:', error);
}
}
settings[key] = value;
const settingsJson = JSON.stringify(settings);
handleInputChange('settings', settingsJson);
};
const handleInputChange = (name, value) => {
if (formApiRef.current) {
formApiRef.current.setValue(name, value);
}
if (name === 'models' && Array.isArray(value)) {
value = Array.from(new Set(value.map((m) => (m || '').trim())));
}
if (name === 'base_url' && value.endsWith('/v1')) {
Modal.confirm({
title: t('警告'),
content: t(
'不需要在末尾加/v1,New API会自动处理,添加后可能导致请求失败,是否继续?',
),
onOk: () => {
setInputs((inputs) => ({ ...inputs, [name]: value }));
},
});
return;
}
setInputs((inputs) => ({ ...inputs, [name]: value }));
if (name === 'type') {
let localModels = [];
switch (value) {
case 2:
localModels = [
'mj_imagine',
'mj_variation',
'mj_reroll',
'mj_blend',
'mj_upscale',
'mj_describe',
'mj_uploads',
];
break;
case 5:
localModels = [
'swap_face',
'mj_imagine',
'mj_video',
'mj_edits',
'mj_variation',
'mj_reroll',
'mj_blend',
'mj_upscale',
'mj_describe',
'mj_zoom',
'mj_shorten',
'mj_modal',
'mj_inpaint',
'mj_custom_zoom',
'mj_high_variation',
'mj_low_variation',
'mj_pan',
'mj_uploads',
];
break;
case 36:
localModels = ['suno_music', 'suno_lyrics'];
break;
case 45:
localModels = getChannelModels(value);
setInputs((prevInputs) => ({
...prevInputs,
base_url: 'https://ark.cn-beijing.volces.com',
}));
break;
default:
localModels = getChannelModels(value);
break;
}
if (inputs.models.length === 0) {
setInputs((inputs) => ({ ...inputs, models: localModels }));
}
setBasicModels(localModels);
// 重置手动输入模式状态
setUseManualInput(false);
}
//setAutoBan
};
const loadChannel = async () => {
setLoading(true);
let res = await API.get(`/api/channel/${channelId}`);
if (res === undefined) {
return;
}
const { success, message, data } = res.data;
if (success) {
if (data.models === '') {
data.models = [];
} else {
data.models = data.models.split(',');
}
if (data.group === '') {
data.groups = [];
} else {
data.groups = data.group.split(',');
}
if (data.model_mapping !== '') {
data.model_mapping = JSON.stringify(
JSON.parse(data.model_mapping),
null,
2,
);
}
const chInfo = data.channel_info || {};
const isMulti = chInfo.is_multi_key === true;
setIsMultiKeyChannel(isMulti);
if (isMulti) {
setBatch(true);
setMultiToSingle(true);
const modeVal = chInfo.multi_key_mode || 'random';
setMultiKeyMode(modeVal);
data.multi_key_mode = modeVal;
} else {
setBatch(false);
setMultiToSingle(false);
}
// 解析渠道额外设置并合并到data中
data.baidu_liveness_header_mode = 'legacy';
if (data.setting) {
try {
const parsedSettings = JSON.parse(data.setting);
data.baidu_liveness_header_mode =
parsedSettings.baidu_liveness_header_mode ?? 'legacy';
data.ali_data_inspection_disabled =
parsedSettings.ali_data_inspection_disabled === true;
data.force_format = parsedSettings.force_format || false;
data.thinking_to_content =
parsedSettings.thinking_to_content || false;
data.proxy = parsedSettings.proxy || '';
data.pass_through_body_enabled =
parsedSettings.pass_through_body_enabled || false;
data.chat_image_to_images_enabled =
parsedSettings.chat_image_to_images_enabled || false;
data.system_prompt = parsedSettings.system_prompt || '';
data.system_prompt_override =
parsedSettings.system_prompt_override || false;
data.client_only = parsedSettings.client_only || false;
data.bound_user_ids = parsedSettings.bound_user_ids || '';
data.os_restriction = Array.isArray(parsedSettings.os_restriction)
? parsedSettings.os_restriction
: [];
data.client_type_restriction = Array.isArray(
parsedSettings.client_type_restriction,
)
? parsedSettings.client_type_restriction
: [];
data.client_version_min = parsedSettings.client_version_min || '';
data.client_version_max = parsedSettings.client_version_max || '';
data.sticky_user_id_fill =
parsedSettings.sticky_user_id_fill || false;
data.sticky_user_id_mode =
parsedSettings.sticky_user_id_mode || 'native';
data.cache_breakpoint_fill =
parsedSettings.cache_breakpoint_fill || false;
data.cache_breakpoint_min_tokens =
parsedSettings.cache_breakpoint_min_tokens || 0;
} catch (error) {
console.error('解析渠道设置失败:', error);
data.force_format = false;
data.ali_data_inspection_disabled = false;
data.thinking_to_content = false;
data.proxy = '';
data.pass_through_body_enabled = false;
data.chat_image_to_images_enabled = false;
data.system_prompt = '';
data.system_prompt_override = false;
data.client_only = false;
data.bound_user_ids = '';
data.os_restriction = [];
data.client_type_restriction = [];
data.client_version_min = '';
data.client_version_max = '';
data.sticky_user_id_fill = false;
data.sticky_user_id_mode = 'native';
data.cache_breakpoint_fill = false;
data.cache_breakpoint_min_tokens = 0;
}
} else {
data.force_format = false;
data.ali_data_inspection_disabled = false;
data.thinking_to_content = false;
data.proxy = '';
data.pass_through_body_enabled = false;
data.chat_image_to_images_enabled = false;
data.system_prompt = '';
data.system_prompt_override = false;
data.client_only = false;
data.bound_user_ids = '';
data.os_restriction = [];
data.client_type_restriction = [];
data.client_version_min = '';
data.client_version_max = '';
data.sticky_user_id_fill = false;
data.sticky_user_id_mode = 'native';
data.cache_breakpoint_fill = false;
data.cache_breakpoint_min_tokens = 0;
}
if (data.settings) {
try {
const parsedSettings = JSON.parse(data.settings);
data.azure_responses_version =
parsedSettings.azure_responses_version || '';
// 读取 Vertex 密钥格式
data.vertex_key_type = parsedSettings.vertex_key_type || 'json';
// 读取 AWS 密钥格式和区域
data.aws_key_type = parsedSettings.aws_key_type || 'ak_sk';
// 读取企业账户设置
data.is_enterprise_account =
parsedSettings.openrouter_enterprise === true;
// 读取字段透传控制设置
data.allow_service_tier = parsedSettings.allow_service_tier || false;
data.disable_store = parsedSettings.disable_store || false;
data.allow_safety_identifier =
parsedSettings.allow_safety_identifier || false;
} catch (error) {
console.error('解析其他设置失败:', error);
data.azure_responses_version = '';
data.region = '';
data.vertex_key_type = 'json';
data.aws_key_type = 'ak_sk';
data.is_enterprise_account = false;
data.allow_service_tier = false;
data.disable_store = false;
data.allow_safety_identifier = false;
}
} else {
// 兼容历史数据:老渠道没有 settings 时,默认按 json 展示
data.vertex_key_type = 'json';
data.aws_key_type = 'ak_sk';
data.is_enterprise_account = false;
data.allow_service_tier = false;
data.disable_store = false;
data.allow_safety_identifier = false;
}
if (
data.type === 45 &&
(!data.base_url ||
(typeof data.base_url === 'string' && data.base_url.trim() === ''))
) {
data.base_url = 'https://ark.cn-beijing.volces.com';
}
// 日消耗限额统一使用 token(quota)单位,无需转换
// 工作时间字段:将 null 转为 undefined,确保 Semi Design Form.Select 能正确识别"无选中"状态
if (data.work_hours_start === null) data.work_hours_start = undefined;
if (data.work_hours_end === null) data.work_hours_end = undefined;
if (data.work_hours_timezone === null)
data.work_hours_timezone = undefined;
// 解析兜底渠道 ID 列表(后端 JSON string -> 前端数组)
if (
data.fallback_channel_ids &&
typeof data.fallback_channel_ids === 'string'
) {
try {
const parsed = JSON.parse(data.fallback_channel_ids);
data.fallback_channel_ids = Array.isArray(parsed)
? parsed.filter((x) => Number.isInteger(x) && x > 0)
: [];
} catch (e) {
data.fallback_channel_ids = [];
}
} else if (!Array.isArray(data.fallback_channel_ids)) {
data.fallback_channel_ids = [];
}
// 兜底触发失败次数:后端 NULL 表示 1
if (!data.fallback_primary_attempts) data.fallback_primary_attempts = 1;
if (!data.fallback_hop_attempts) data.fallback_hop_attempts = 1;
setInputs(data);
if (formApiRef.current) {
formApiRef.current.setValues(data);
// 显式设置工作时间字段,确保 Form.Select 组件正确回显
formApiRef.current.setValue('work_hours_start', data.work_hours_start);
formApiRef.current.setValue('work_hours_end', data.work_hours_end);
formApiRef.current.setValue(
'work_hours_timezone',
data.work_hours_timezone || 'Asia/Shanghai',
);
}
if (data.auto_ban === 0) {
setAutoBan(false);
} else {
setAutoBan(true);
}
// 同步企业账户状态
setIsEnterpriseAccount(data.is_enterprise_account || false);
setBasicModels(getChannelModels(data.type));
// 同步更新channelSettings状态显示
setChannelSettings({
ali_data_inspection_disabled:
data.ali_data_inspection_disabled === true,
force_format: data.force_format,
thinking_to_content: data.thinking_to_content,
proxy: data.proxy,
pass_through_body_enabled: data.pass_through_body_enabled,
chat_image_to_images_enabled:
data.chat_image_to_images_enabled || false,
system_prompt: data.system_prompt,
system_prompt_override: data.system_prompt_override || false,
client_only: data.client_only || false,
bound_user_ids: data.bound_user_ids || '',
os_restriction: Array.isArray(data.os_restriction)
? data.os_restriction
: [],
client_type_restriction: Array.isArray(data.client_type_restriction)
? data.client_type_restriction
: [],
client_version_min: data.client_version_min || '',
client_version_max: data.client_version_max || '',
sticky_user_id_fill: data.sticky_user_id_fill || false,
sticky_user_id_mode: data.sticky_user_id_mode || 'native',
cache_breakpoint_fill: data.cache_breakpoint_fill || false,
cache_breakpoint_min_tokens: data.cache_breakpoint_min_tokens || 0,
});
initialModelsRef.current = (data.models || [])
.map((model) => (model || '').trim())
.filter(Boolean);
initialModelMappingRef.current = data.model_mapping || '';
// console.log(data);
} else {
showError(message);
}
setLoading(false);
};
const fetchUpstreamModelList = async (name) => {
// if (inputs['type'] !== 1) {
// showError(t('仅支持 OpenAI 接口格式'));
// return;
// }
setLoading(true);
const models = [];
let err = false;
if (isEdit) {
// 如果是编辑模式,使用已有的 channelId 获取模型列表
const res = await API.get('/api/channel/fetch_models/' + channelId, {
skipErrorHandler: true,
});
if (res && res.data && res.data.success) {
models.push(...res.data.data);
} else {
err = true;
}
} else {
// 如果是新建模式,通过后端代理获取模型列表
if (!inputs?.['key']) {
showError(t('请填写密钥'));
err = true;
} else {
try {
const res = await API.post(
'/api/channel/fetch_models',
{
base_url: inputs['base_url'],
type: inputs['type'],
key: inputs['key'],
},
{ skipErrorHandler: true },
);
if (res && res.data && res.data.success) {
models.push(...res.data.data);
} else {
err = true;
}
} catch (error) {
console.error('Error fetching models:', error);
err = true;
}
}
}
if (!err) {
const uniqueModels = Array.from(new Set(models));
setFetchedModels(uniqueModels);
setModelModalVisible(true);
} else {
showError(t('获取模型列表失败'));
}
setLoading(false);
};
const fetchModels = async () => {
try {
let res = await API.get(`/api/channel/models`);
const localModelOptions = res.data.data.map((model) => {
const id = (model.id || '').trim();
return {
key: id,
label: id,
value: id,
};
});
setOriginModelOptions(localModelOptions);
setFullModels(res.data.data.map((model) => model.id));
setBasicModels(
res.data.data
.filter((model) => {
return model.id.startsWith('gpt-') || model.id.startsWith('text-');
})
.map((model) => model.id),
);
} catch (error) {
showError(error.message);
}
};
const fetchGroups = async () => {
try {
let res = await API.get(`/api/group/`);
if (res === undefined) {
return;
}
setGroupOptions(
res.data.data.map((group) => ({
label: group,
value: group,
})),
);
} catch (error) {
showError(error.message);
}
};
const fetchModelGroups = async () => {
try {
const res = await API.get('/api/prefill_group?type=model');
if (res?.data?.success) {
setModelGroups(res.data.data || []);
}
} catch (error) {
// ignore
}
};
// 查看渠道密钥(透明验证)
const handleShow2FAModal = async () => {
try {
// 使用 withVerification 包装,会自动处理需要验证的情况
const result = await withVerification(
createApiCalls.viewChannelKey(channelId),
{
title: t('查看渠道密钥'),
description: t('为了保护账户安全,请验证您的身份。'),
preferredMethod: 'passkey', // 优先使用 Passkey
},
);
// 如果直接返回了结果(已验证),显示密钥
if (result && result.success && result.data?.key) {
showSuccess(t('密钥获取成功'));
setKeyDisplayState({
showModal: true,
keyData: result.data.key,
});
}
} catch (error) {
console.error('Failed to view channel key:', error);
showError(error.message || t('获取密钥失败'));
}
};
useEffect(() => {
if (inputs.type !== 45) {
doubaoApiClickCountRef.current = 0;
setDoubaoApiEditUnlocked(false);
}
}, [inputs.type]);
useEffect(() => {
const modelMap = new Map();
originModelOptions.forEach((option) => {
const v = (option.value || '').trim();
if (!modelMap.has(v)) {
modelMap.set(v, option);
}
});
inputs.models.forEach((model) => {
const v = (model || '').trim();
if (!modelMap.has(v)) {
modelMap.set(v, {
key: v,
label: v,
value: v,
});
}
});
const categories = getModelCategories(t);
const optionsWithIcon = Array.from(modelMap.values()).map((opt) => {
const modelName = opt.value;
let icon = null;
for (const [key, category] of Object.entries(categories)) {
if (key !== 'all' && category.filter({ model_name: modelName })) {
icon = category.icon;
break;
}
}
return {
...opt,
label: (
<span className='flex items-center gap-1'>
{icon}
{modelName}
</span>
),
};
});
setModelOptions(optionsWithIcon);
}, [originModelOptions, inputs.models, t]);
useEffect(() => {
fetchModels().then();
fetchGroups().then();
if (!isEdit) {
const initVals = getInitValues();
setInputs(initVals);
if (formApiRef.current) {
formApiRef.current.setValues(initVals);
}
let localModels = getChannelModels(inputs.type);
setBasicModels(localModels);
setInputs((inputs) => ({ ...inputs, models: localModels }));
}
}, [props.editingChannel.id]);
useEffect(() => {
if (formApiRef.current) {
formApiRef.current.setValues(inputs);
}
}, [inputs]);
useEffect(() => {
if (props.visible) {
if (isEdit) {
loadChannel();
} else {
formApiRef.current?.setValues(getInitValues());
}
// 每次打开(或打开状态下切换渠道)都重新拉兜底候选与反向引用,
// 避免连续新建或重复打开同一渠道时沿用旧数据
reloadFallbackData();
fetchModelGroups();
// 重置手动输入模式状态
setUseManualInput(false);
} else {
// 统一的模态框关闭重置逻辑
resetModalState();
// 关闭后作废在途的兜底数据请求
fallbackLoadSeqRef.current += 1;
}
}, [props.visible, channelId]);
useEffect(() => {
if (!isEdit) {
initialModelsRef.current = [];
initialModelMappingRef.current = '';
}
}, [isEdit, props.visible]);
// 统一的模态框重置函数
const resetModalState = () => {
formApiRef.current?.reset();
// 重置渠道设置状态
setChannelSettings({
ali_data_inspection_disabled: false,
force_format: false,
thinking_to_content: false,
proxy: '',
pass_through_body_enabled: false,
chat_image_to_images_enabled: false,
system_prompt: '',
system_prompt_override: false,
client_only: false,
bound_user_ids: '',
os_restriction: [],
client_type_restriction: [],
client_version_min: '',
client_version_max: '',
sticky_user_id_fill: false,
sticky_user_id_mode: 'native',
cache_breakpoint_fill: false,
cache_breakpoint_min_tokens: 0,
});
// 重置密钥模式状态
setKeyMode('append');
// 重置企业账户状态
setIsEnterpriseAccount(false);
// 重置豆包隐藏入口状态
setDoubaoApiEditUnlocked(false);
doubaoApiClickCountRef.current = 0;
// 清空表单中的key_mode字段
if (formApiRef.current) {
formApiRef.current.setValue('key_mode', undefined);
}
// 重置本地输入,避免下次打开残留上一次的 JSON 字段值
setInputs(getInitValues());
// 重置密钥显示状态
resetKeyDisplayState();
};
const handleVertexUploadChange = ({ fileList }) => {
vertexErroredNames.current.clear();
(async () => {
let validFiles = [];
let keys = [];
const errorNames = [];
for (const item of fileList) {
const fileObj = item.fileInstance;
if (!fileObj) continue;
try {
const txt = await fileObj.text();
keys.push(JSON.parse(txt));
validFiles.push(item);
} catch (err) {
if (!vertexErroredNames.current.has(item.name)) {
errorNames.push(item.name);
vertexErroredNames.current.add(item.name);
}
}
}
// 非批量模式下只保留一个文件(最新选择的),避免重复叠加
if (!batch && validFiles.length > 1) {
validFiles = [validFiles[validFiles.length - 1]];
keys = [keys[keys.length - 1]];
}
setVertexKeys(keys);
setVertexFileList(validFiles);
if (formApiRef.current) {
formApiRef.current.setValue('vertex_files', validFiles);
}
setInputs((prev) => ({ ...prev, vertex_files: validFiles }));
if (errorNames.length > 0) {
showError(
t('以下文件解析失败,已忽略:{{list}}', {
list: errorNames.join(', '),
}),
);
}
})();
};
const confirmMissingModelMappings = (missingModels) =>
new Promise((resolve) => {
const modal = Modal.confirm({
title: t('模型未加入列表,可能无法调用'),
content: (
<div className='text-sm leading-6'>
<div>
{t(
'模型重定向里的下列模型尚未添加到“模型”列表,调用时会因为缺少可用模型而失败:',
)}
</div>
<div className='font-mono text-xs break-all text-red-600 mt-1'>
{missingModels.join(', ')}
</div>
<div className='mt-2'>
{t(
'你可以在“自定义模型名称”处手动添加它们,然后点击填入后再提交,或者直接使用下方操作自动处理。',
)}
</div>
</div>
),
centered: true,
footer: (
<Space align='center' className='w-full justify-end'>
<Button
type='tertiary'
onClick={() => {
modal.destroy();
resolve('cancel');
}}
>
{t('返回修改')}
</Button>
<Button
type='primary'
theme='light'
onClick={() => {
modal.destroy();
resolve('submit');
}}
>
{t('直接提交')}
</Button>
<Button
type='primary'
theme='solid'
onClick={() => {
modal.destroy();
resolve('add');
}}
>
{t('添加后提交')}
</Button>
</Space>
),
});
});
const hasModelConfigChanged = (normalizedModels, modelMappingStr) => {
if (!isEdit) return true;
const initialModels = initialModelsRef.current;
if (normalizedModels.length !== initialModels.length) {
return true;
}
for (let i = 0; i < normalizedModels.length; i++) {
if (normalizedModels[i] !== initialModels[i]) {
return true;
}
}
const normalizedMapping = (modelMappingStr || '').trim();
const initialMapping = (initialModelMappingRef.current || '').trim();
return normalizedMapping !== initialMapping;
};
const submit = async () => {
const formValues = formApiRef.current ? formApiRef.current.getValues() : {};
let localInputs = { ...formValues };
if (localInputs.type === 41) {
const keyType = localInputs.vertex_key_type || 'json';
if (keyType === 'api_key') {
// 直接作为普通字符串密钥处理
if (!isEdit && (!localInputs.key || localInputs.key.trim() === '')) {
showInfo(t('请输入密钥!'));
return;
}
} else {
// JSON 服务账号密钥
if (useManualInput) {
if (localInputs.key && localInputs.key.trim() !== '') {
try {
const parsedKey = JSON.parse(localInputs.key);
localInputs.key = JSON.stringify(parsedKey);
} catch (err) {
showError(t('密钥格式无效,请输入有效的 JSON 格式密钥'));
return;
}
} else if (!isEdit) {
showInfo(t('请输入密钥!'));
return;
}
} else {
// 文件上传模式
let keys = vertexKeys;
if (keys.length === 0 && vertexFileList.length > 0) {
try {
const parsed = await Promise.all(
vertexFileList.map(async (item) => {
const fileObj = item.fileInstance;
if (!fileObj) return null;
const txt = await fileObj.text();
return JSON.parse(txt);
}),
);
keys = parsed.filter(Boolean);
} catch (err) {
showError(t('解析密钥文件失败: {{msg}}', { msg: err.message }));
return;
}
}
if (keys.length === 0) {
if (!isEdit) {
showInfo(t('请上传密钥文件!'));
return;
} else {
delete localInputs.key;
}
} else {
localInputs.key = batch
? JSON.stringify(keys)
: JSON.stringify(keys[0]);
}
}
}
}
// 如果是编辑模式且 key 为空字符串,避免提交空值覆盖旧密钥
if (isEdit && (!localInputs.key || localInputs.key.trim() === '')) {
delete localInputs.key;
}
delete localInputs.vertex_files;
if (!isEdit && (!localInputs.name || !localInputs.key)) {
showInfo(t('请填写渠道名称和渠道密钥!'));
return;
}
if (!Array.isArray(localInputs.models) || localInputs.models.length === 0) {
showInfo(t('请至少选择一个模型!'));
return;
}
if (
localInputs.type === 45 &&
(!localInputs.base_url || localInputs.base_url.trim() === '')
) {
showInfo(t('请输入API地址!'));
return;
}
const hasModelMapping =
typeof localInputs.model_mapping === 'string' &&
localInputs.model_mapping.trim() !== '';
let parsedModelMapping = null;
if (hasModelMapping) {
if (!verifyJSON(localInputs.model_mapping)) {
showInfo(t('模型映射必须是合法的 JSON 格式!'));
return;
}
try {
parsedModelMapping = JSON.parse(localInputs.model_mapping);
} catch (error) {
showInfo(t('模型映射必须是合法的 JSON 格式!'));
return;
}
}
// 客户端版本范围校验:三段式格式 + 上下界不倒置
{
const vMin = (localInputs.client_version_min || '').trim();
const vMax = (localInputs.client_version_max || '').trim();
const verRe = /^\d+\.\d+\.\d+$/;
if (vMin && !verRe.test(vMin)) {
showInfo(t('客户端版本下界格式应为 x.y.z'));
return;
}
if (vMax && !verRe.test(vMax)) {
showInfo(t('客户端版本上界格式应为 x.y.z'));
return;
}
if (vMin && vMax) {
const cmp = vMin
.split('.')
.map(Number)
.reduce(
(acc, n, i) => (acc !== 0 ? acc : n - Number(vMax.split('.')[i])),
0,
);
if (cmp > 0) {
showInfo(t('客户端版本上界需不低于下界'));
return;
}
}
}
const normalizedModels = (localInputs.models || [])
.map((model) => (model || '').trim())
.filter(Boolean);
localInputs.models = normalizedModels;
if (
parsedModelMapping &&
typeof parsedModelMapping === 'object' &&
!Array.isArray(parsedModelMapping)
) {
const modelSet = new Set(normalizedModels);
const missingModels = Object.keys(parsedModelMapping)
.map((key) => (key || '').trim())
.filter((key) => key && !modelSet.has(key));
const shouldPromptMissing =
missingModels.length > 0 &&
hasModelConfigChanged(normalizedModels, localInputs.model_mapping);
if (shouldPromptMissing) {
const confirmAction = await confirmMissingModelMappings(missingModels);
if (confirmAction === 'cancel') {
return;
}
if (confirmAction === 'add') {
const updatedModels = Array.from(
new Set([...normalizedModels, ...missingModels]),
);
localInputs.models = updatedModels;
handleInputChange('models', updatedModels);
}
}
}
if (localInputs.base_url && localInputs.base_url.endsWith('/')) {
localInputs.base_url = localInputs.base_url.slice(
0,
localInputs.base_url.length - 1,
);
}
if (localInputs.type === 18 && localInputs.other === '') {
localInputs.other = 'v2.1';
}
// 生成渠道额外设置JSON
const channelExtraSettings = {
ali_data_inspection_disabled:
localInputs.ali_data_inspection_disabled === true,
force_format: localInputs.force_format || false,
thinking_to_content: localInputs.thinking_to_content || false,
proxy: localInputs.proxy || '',
pass_through_body_enabled: localInputs.pass_through_body_enabled || false,
chat_image_to_images_enabled:
localInputs.chat_image_to_images_enabled || false,
system_prompt: localInputs.system_prompt || '',
system_prompt_override: localInputs.system_prompt_override || false,
client_only: localInputs.client_only || false,
bound_user_ids: localInputs.bound_user_ids || '',
os_restriction: Array.isArray(localInputs.os_restriction)
? localInputs.os_restriction.filter((o) =>
['windows', 'macos', 'linux'].includes(o),
)
: [],
client_type_restriction: Array.isArray(
localInputs.client_type_restriction,
)
? localInputs.client_type_restriction.filter((t) =>
['cli', 'vscode'].includes(t),
)
: [],
client_version_min: (localInputs.client_version_min || '').trim(),
client_version_max: (localInputs.client_version_max || '').trim(),
sticky_user_id_fill: localInputs.sticky_user_id_fill || false,
sticky_user_id_mode: ['native', 'opaque'].includes(
localInputs.sticky_user_id_mode,
)
? localInputs.sticky_user_id_mode
: 'native',
cache_breakpoint_fill: localInputs.cache_breakpoint_fill || false,
// 0 = 用后端默认(10000)。负数按 0 处理,低于 1024 的值后端会自己抬到 1024
// (1024 是 opus/sonnet 的最小可缓存前缀,再小注入了也产不出缓存)。
cache_breakpoint_min_tokens: Math.max(
0,
parseInt(localInputs.cache_breakpoint_min_tokens, 10) || 0,
),
};
try {
localInputs.setting = mergeChannelSettings(
inputs.setting,
channelExtraSettings,
localInputs.type,
localInputs.baidu_liveness_header_mode,
);
} catch (error) {
showError(error.message);
return;
}
// 处理 settings 字段(包括企业账户设置和字段透传控制)
let settings = {};
if (localInputs.settings) {
try {
settings = JSON.parse(localInputs.settings);
} catch (error) {
console.error('解析settings失败:', error);
}
}
// type === 20: 设置企业账户标识,无论是true还是false都要传到后端
if (localInputs.type === 20) {
settings.openrouter_enterprise =
localInputs.is_enterprise_account === true;
}
// type === 33 (AWS): 保存 aws_key_type 到 settings
if (localInputs.type === 33) {
settings.aws_key_type = localInputs.aws_key_type || 'ak_sk';
}
// type === 41 (Vertex): 始终保存 vertex_key_type 到 settings,避免编辑时被重置
if (localInputs.type === 41) {
settings.vertex_key_type = localInputs.vertex_key_type || 'json';
} else if ('vertex_key_type' in settings) {
delete settings.vertex_key_type;
}
// type === 1 (OpenAI) 或 type === 14 (Claude): 设置字段透传控制(显式保存布尔值)
if (localInputs.type === 1 || localInputs.type === 14) {
settings.allow_service_tier = localInputs.allow_service_tier === true;
// 仅 OpenAI 渠道需要 store 和 safety_identifier
if (localInputs.type === 1) {
settings.disable_store = localInputs.disable_store === true;
settings.allow_safety_identifier =
localInputs.allow_safety_identifier === true;
}
}
localInputs.settings = JSON.stringify(settings);
// 清理不需要发送到后端的字段
delete localInputs.force_format;
delete localInputs.ali_data_inspection_disabled;
delete localInputs.thinking_to_content;
delete localInputs.proxy;
delete localInputs.pass_through_body_enabled;
delete localInputs.chat_image_to_images_enabled;
delete localInputs.client_only;
delete localInputs.os_restriction;
delete localInputs.client_type_restriction;
delete localInputs.client_version_min;
delete localInputs.client_version_max;
delete localInputs.sticky_user_id_fill;
delete localInputs.sticky_user_id_mode;
delete localInputs.cache_breakpoint_fill;
delete localInputs.cache_breakpoint_min_tokens;
delete localInputs.system_prompt;
delete localInputs.system_prompt_override;
delete localInputs.is_enterprise_account;
// 顶层的 vertex_key_type 不应发送给后端
delete localInputs.vertex_key_type;
// 顶层的 aws_key_type 不应发送给后端
delete localInputs.aws_key_type;
// 清理字段透传控制的临时字段
delete localInputs.allow_service_tier;
delete localInputs.disable_store;
delete localInputs.allow_safety_identifier;
// 工作时间字段:确保 undefined 转为 null,以便后端正确解析(JSON 中 undefined 会被省略)
if (localInputs.work_hours_start === undefined)
localInputs.work_hours_start = null;
if (localInputs.work_hours_end === undefined)
localInputs.work_hours_end = null;
if (localInputs.work_hours_timezone === undefined)
localInputs.work_hours_timezone = null;
// 兜底渠道 ID 数组 -> JSON string;空数组 -> 空字符串(显式清空)
if (Array.isArray(localInputs.fallback_channel_ids)) {
const ids = localInputs.fallback_channel_ids
.map((x) => parseInt(x))
.filter((x) => Number.isInteger(x) && x > 0);
localInputs.fallback_channel_ids =
ids.length > 0 ? JSON.stringify(ids) : '';
}
// 兜底触发失败次数:兜底链为空时传 null(后端同样会清空);否则取 1~5 的整数
const normalizeAttempts = (value) => {
const n = parseInt(value, 10);
if (!Number.isInteger(n) || n < 1) return 1;
return Math.min(n, 5);
};
if (localInputs.fallback_channel_ids) {
localInputs.fallback_primary_attempts = normalizeAttempts(
localInputs.fallback_primary_attempts,
);
localInputs.fallback_hop_attempts = normalizeAttempts(
localInputs.fallback_hop_attempts,
);
} else {
localInputs.fallback_primary_attempts = null;
localInputs.fallback_hop_attempts = null;
}
let res;
localInputs.auto_ban = localInputs.auto_ban ? 1 : 0;
localInputs.models = localInputs.models.join(',');
localInputs.group = (localInputs.groups || []).join(',');
// 日消耗限额统一使用 token(quota)单位,确保为整数
if (localInputs.daily_quota_limit && localInputs.daily_quota_limit > 0) {
localInputs.daily_quota_limit = Math.round(localInputs.daily_quota_limit);
}
let mode = 'single';
if (batch) {
mode =
multiToSingle && localInputs.type !== 61 ? 'multi_to_single' : 'batch';
}
if (isEdit) {
res = await API.put(
`/api/channel/`,
{
...localInputs,
id: parseInt(channelId),
key_mode: isMultiKeyChannel ? keyMode : undefined, // 只在多key模式下传递
},
{
validateStatus: (status) =>
(status >= 200 && status < 300) || status === 409,
},
);
} else {
res = await API.post(`/api/channel/`, {
mode: mode,
multi_key_mode: mode === 'multi_to_single' ? multiKeyMode : undefined,
channel: localInputs,
});
}
const { success, message } = res.data;
if (success) {
if (isEdit) {
showSuccess(t('渠道更新成功!'));
} else {
showSuccess(t('渠道创建成功!'));
setInputs(originInputs);
}
props.refresh();
props.handleClose();
} else {
showError(message);
}
};
// 密钥去重函数
const deduplicateKeys = () => {
const currentKey = formApiRef.current?.getValue('key') || inputs.key || '';
if (!currentKey.trim()) {
showInfo(t('请先输入密钥'));
return;
}
// 按行分割密钥
const keyLines = currentKey.split('\n');
const beforeCount = keyLines.length;
// 使用哈希表去重,保持原有顺序
const keySet = new Set();
const deduplicatedKeys = [];
keyLines.forEach((line) => {
const trimmedLine = line.trim();
if (trimmedLine && !keySet.has(trimmedLine)) {
keySet.add(trimmedLine);
deduplicatedKeys.push(trimmedLine);
}
});
const afterCount = deduplicatedKeys.length;
const deduplicatedKeyText = deduplicatedKeys.join('\n');
// 更新表单和状态
if (formApiRef.current) {
formApiRef.current.setValue('key', deduplicatedKeyText);
}
handleInputChange('key', deduplicatedKeyText);
// 显示去重结果
const message = t(
'去重完成:去重前 {{before}} 个密钥,去重后 {{after}} 个密钥',
{
before: beforeCount,
after: afterCount,
},
);
if (beforeCount === afterCount) {
showInfo(t('未发现重复密钥'));
} else {
showSuccess(message);
}
};
const addCustomModels = () => {
if (customModel.trim() === '') return;
const modelArray = customModel.split(',').map((model) => model.trim());
let localModels = [...inputs.models];
let localModelOptions = [...modelOptions];
const addedModels = [];
modelArray.forEach((model) => {
if (model && !localModels.includes(model)) {
localModels.push(model);
localModelOptions.push({
key: model,
label: model,
value: model,
});
addedModels.push(model);
}
});
setModelOptions(localModelOptions);
setCustomModel('');
handleInputChange('models', localModels);
if (addedModels.length > 0) {
showSuccess(
t('已新增 {{count}} 个模型:{{list}}', {
count: addedModels.length,
list: addedModels.join(', '),
}),
);
} else {
showInfo(t('未发现新增模型'));
}
};
const batchAllowed = !isEdit || isMultiKeyChannel;
const batchExtra = batchAllowed ? (
<Space wrap className='console-channel-key-actions'>
{!isEdit && (
<Checkbox
disabled={isEdit}
checked={batch}
onChange={(e) => {
const checked = e.target.checked;
if (!checked && vertexFileList.length > 1) {
Modal.confirm({
title: t('切换为单密钥模式'),
content: t(
'将仅保留第一个密钥文件,其余文件将被移除,是否继续?',
),
onOk: () => {
const firstFile = vertexFileList[0];
const firstKey = vertexKeys[0] ? [vertexKeys[0]] : [];
setVertexFileList([firstFile]);
setVertexKeys(firstKey);
formApiRef.current?.setValue('vertex_files', [firstFile]);
setInputs((prev) => ({ ...prev, vertex_files: [firstFile] }));
setBatch(false);
setMultiToSingle(false);
setMultiKeyMode('random');
},
onCancel: () => {
setBatch(true);
},
centered: true,
});
return;
}
setBatch(checked);
if (!checked) {
setMultiToSingle(false);
setMultiKeyMode('random');
} else {
// 批量模式下禁用手动输入,并清空手动输入的内容
setUseManualInput(false);
if (inputs.type === 41) {
// 清空手动输入的密钥内容
if (formApiRef.current) {
formApiRef.current.setValue('key', '');
}
handleInputChange('key', '');
}
}
}}
>
{t('批量创建')}
</Checkbox>
)}
{batch && (
<>
<Checkbox
disabled={isEdit || inputs.type === 61}
checked={inputs.type !== 61 && multiToSingle}
onChange={() => {
setMultiToSingle((prev) => {
const nextValue = !prev;
setInputs((prevInputs) => {
const newInputs = { ...prevInputs };
if (nextValue) {
newInputs.multi_key_mode = multiKeyMode;
} else {
delete newInputs.multi_key_mode;
}
return newInputs;
});
return nextValue;
});
}}
>
{t('密钥聚合模式')}
</Checkbox>
{inputs.type !== 41 && (
<Button
size='small'
type='tertiary'
theme='outline'
onClick={deduplicateKeys}
style={{ textDecoration: 'underline' }}
>
{t('密钥去重')}
</Button>
)}
</>
)}
</Space>
) : null;
const channelOptionList = useMemo(
() =>
CHANNEL_OPTIONS.map((opt) => ({
...opt,
// 保持 label 为纯文本以支持搜索
label: opt.label,
})),
// label 是按当前语言取值的 getter,切换语言后需重建
[t],
);
const renderChannelOption = (renderProps) => {
const {
disabled,
selected,
label,
value,
focused,
className,
style,
onMouseEnter,
onClick,
} = renderProps;
const searchWords = channelSearchValue ? [channelSearchValue] : [];
// 构建样式类名
const optionClassName = [
'flex items-center gap-3 px-3 py-2 transition-all duration-200 rounded-lg mx-2 my-1',
focused && 'bg-blue-50 shadow-sm',
selected &&
'bg-blue-100 text-blue-700 shadow-lg ring-2 ring-blue-200 ring-opacity-50',
disabled && 'opacity-50 cursor-not-allowed',
!disabled && 'hover:bg-gray-50 hover:shadow-md cursor-pointer',
className,
]
.filter(Boolean)
.join(' ');
return (
<div
style={style}
className={optionClassName}
onClick={() => !disabled && onClick()}
onMouseEnter={(e) => onMouseEnter()}
>
<div className='flex items-center gap-3 w-full'>
<div className='flex-shrink-0 w-5 h-5 flex items-center justify-center'>
{getChannelIcon(value)}
</div>
<div className='flex-1 min-w-0'>
<Highlight
sourceString={label}
searchWords={searchWords}
className='text-sm font-medium truncate'
/>
</div>
{selected && (
<div className='flex-shrink-0 text-blue-600'>
<svg
width='16'
height='16'
viewBox='0 0 16 16'
fill='currentColor'
>
<path d='M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z' />
</svg>
</div>
)}
</div>
</div>
);
};
return (
<>
<SideSheet
rootClassName='console-channel-drawer'
classNames={{ header: 'console-channel-editor-header' }}
placement='right'
title={isEdit ? t('更新渠道信息') : t('创建新的渠道')}
bodyStyle={{ padding: '0' }}
visible={props.visible}
width={isMobile ? '100%' : 600}
footer={
<div className='console-channel-footer'>
<Button onClick={handleCancel}>{t('取消')}</Button>
<Button
theme='solid'
type='primary'
onClick={() => formApiRef.current?.submitForm()}
>
{t('提交')}
</Button>
</div>
}
onCancel={() => handleCancel()}
>
<Form
key={isEdit ? 'edit' : 'new'}
initValues={originInputs}
getFormApi={(api) => (formApiRef.current = api)}
onSubmit={submit}
>
{() => (
<Spin spinning={loading}>
<div className='console-channel-sections p-2'>
<div>
<Card className='!rounded-2xl shadow-sm border-0 mb-6'>
{/* Header: Basic Info */}
<div className='console-channel-section'>
<Avatar size='small' color='blue'>
<IconServer size={16} />
</Avatar>
<div>
<Text className='text-lg font-medium'>
{t('基本信息')}
</Text>
<div className='text-xs text-gray-600'>
{t('渠道的基本配置信息')}
</div>
</div>
</div>
<Form.Select
field='type'
label={t('类型')}
placeholder={t('请选择渠道类型')}
rules={[{ required: true, message: t('请选择渠道类型') }]}
optionList={channelOptionList}
style={{ width: '100%' }}
filter={selectFilter}
autoClearSearchValue={false}
searchPosition='dropdown'
onSearch={(value) => setChannelSearchValue(value)}
renderOptionItem={renderChannelOption}
onChange={(value) => handleInputChange('type', value)}
/>
{inputs.type === 20 && (
<Form.Switch
field='is_enterprise_account'
label={t('是否为企业账户')}
checkedText={t('是')}
uncheckedText={t('否')}
onChange={(value) => {
setIsEnterpriseAccount(value);
handleInputChange('is_enterprise_account', value);
}}
extraText={t(
'企业账户为特殊返回格式,需要特殊处理,如果非企业账户,请勿勾选',
)}
initValue={inputs.is_enterprise_account}
/>
)}
<Form.Input
field='name'
label={t('名称')}
placeholder={t('请为渠道命名')}
rules={[{ required: true, message: t('请为渠道命名') }]}
showClear
onChange={(value) => handleInputChange('name', value)}
autoComplete='new-password'
/>
{inputs.type === 33 && (
<>
<Form.Select
field='aws_key_type'
label={t('密钥格式')}
placeholder={t('请选择密钥格式')}
optionList={[
{
label: 'AccessKey / SecretAccessKey',
value: 'ak_sk',
},
{ label: 'API Key', value: 'api_key' },
]}
style={{ width: '100%' }}
value={inputs.aws_key_type || 'ak_sk'}
onChange={(value) => {
handleChannelOtherSettingsChange(
'aws_key_type',
value,
);
}}
extraText={t(
'AK/SK 模式:使用 AccessKey 和 SecretAccessKey;API Key 模式:使用 API Key',
)}
/>
</>
)}
{inputs.type === 41 && (
<Form.Select
field='vertex_key_type'
label={t('密钥格式')}
placeholder={t('请选择密钥格式')}
optionList={[
{ label: 'JSON', value: 'json' },
{ label: 'API Key', value: 'api_key' },
]}
style={{ width: '100%' }}
value={inputs.vertex_key_type || 'json'}
onChange={(value) => {
// 更新设置中的 vertex_key_type
handleChannelOtherSettingsChange(
'vertex_key_type',
value,
);
// 切换为 api_key 时,关闭批量与手动/文件切换,并清理已选文件
if (value === 'api_key') {
setBatch(false);
setUseManualInput(false);
setVertexKeys([]);
setVertexFileList([]);
if (formApiRef.current) {
formApiRef.current.setValue('vertex_files', []);
}
}
}}
extraText={
inputs.vertex_key_type === 'api_key'
? t('API Key 模式下不支持批量创建')
: t('JSON 模式支持手动输入或上传服务账号 JSON')
}
/>
)}
{batch ? (
inputs.type === 41 &&
(inputs.vertex_key_type || 'json') === 'json' ? (
<Form.Upload
field='vertex_files'
label={t('密钥文件 (.json)')}
accept='.json'
multiple
draggable
dragIcon={<IconBolt />}
dragMainText={t('点击上传文件或拖拽文件到这里')}
dragSubText={t('仅支持 JSON 文件,支持多文件')}
style={{ marginTop: 10 }}
uploadTrigger='custom'
beforeUpload={() => false}
onChange={handleVertexUploadChange}
fileList={vertexFileList}
rules={
isEdit
? []
: [
{
required: true,
message: t('请上传密钥文件'),
},
]
}
extraText={batchExtra}
/>
) : (
<Form.TextArea
field='key'
label={t('密钥')}
placeholder={
inputs.type === 33
? inputs.aws_key_type === 'api_key'
? t(
'请输入 API Key,一行一个,格式:APIKey|Region',
)
: t(
'请输入密钥,一行一个,格式:AccessKey|SecretAccessKey|Region',
)
: t('请输入密钥,一行一个')
}
rules={
isEdit
? []
: [{ required: true, message: t('请输入密钥') }]
}
autosize
autoComplete='new-password'
onChange={(value) => handleInputChange('key', value)}
extraText={
<div className='flex items-center gap-2 flex-wrap'>
{isEdit &&
isMultiKeyChannel &&
keyMode === 'append' && (
<Text type='warning' size='small'>
{t(
'追加模式:新密钥将添加到现有密钥列表的末尾',
)}
</Text>
)}
{isEdit && (
<Button
size='small'
type='primary'
theme='outline'
onClick={handleShow2FAModal}
>
{t('查看密钥')}
</Button>
)}
{batchExtra}
</div>
}
showClear
/>
)
) : (
<>
{inputs.type === 41 &&
(inputs.vertex_key_type || 'json') === 'json' ? (
<>
{!batch && (
<div className='flex items-center justify-between mb-3'>
<Text className='text-sm font-medium'>
{t('密钥输入方式')}
</Text>
<Space>
<Button
size='small'
type={
!useManualInput ? 'primary' : 'tertiary'
}
onClick={() => {
setUseManualInput(false);
// 切换到文件上传模式时清空手动输入的密钥
if (formApiRef.current) {
formApiRef.current.setValue('key', '');
}
handleInputChange('key', '');
}}
>
{t('文件上传')}
</Button>
<Button
size='small'
type={
useManualInput ? 'primary' : 'tertiary'
}
onClick={() => {
setUseManualInput(true);
// 切换到手动输入模式时清空文件上传相关状态
setVertexKeys([]);
setVertexFileList([]);
if (formApiRef.current) {
formApiRef.current.setValue(
'vertex_files',
[],
);
}
setInputs((prev) => ({
...prev,
vertex_files: [],
}));
}}
>
{t('手动输入')}
</Button>
</Space>
</div>
)}
{batch && (
<Banner
type='info'
description={t(
'批量创建模式下仅支持文件上传,不支持手动输入',
)}
className='!rounded-lg mb-3'
/>
)}
{useManualInput && !batch ? (
<Form.TextArea
field='key'
label={
isEdit
? t(
'密钥(编辑模式下,保存的密钥不会显示)',
)
: t('密钥')
}
placeholder={t(
'请输入 JSON 格式的密钥内容,例如:\n{\n "type": "service_account",\n "project_id": "your-project-id",\n "private_key_id": "...",\n "private_key": "...",\n "client_email": "...",\n "client_id": "...",\n "auth_uri": "...",\n "token_uri": "...",\n "auth_provider_x509_cert_url": "...",\n "client_x509_cert_url": "..."\n}',
)}
rules={
isEdit
? []
: [
{
required: true,
message: t('请输入密钥'),
},
]
}
autoComplete='new-password'
onChange={(value) =>
handleInputChange('key', value)
}
extraText={
<div className='flex items-center gap-2'>
<Text type='tertiary' size='small'>
{t('请输入完整的 JSON 格式密钥内容')}
</Text>
{isEdit &&
isMultiKeyChannel &&
keyMode === 'append' && (
<Text type='warning' size='small'>
{t(
'追加模式:新密钥将添加到现有密钥列表的末尾',
)}
</Text>
)}
{isEdit && (
<Button
size='small'
type='primary'
theme='outline'
onClick={handleShow2FAModal}
>
{t('查看密钥')}
</Button>
)}
{batchExtra}
</div>
}
autosize
showClear
/>
) : (
<Form.Upload
field='vertex_files'
label={t('密钥文件 (.json)')}
accept='.json'
draggable
dragIcon={<IconBolt />}
dragMainText={t('点击上传文件或拖拽文件到这里')}
dragSubText={t('仅支持 JSON 文件')}
style={{ marginTop: 10 }}
uploadTrigger='custom'
beforeUpload={() => false}
onChange={handleVertexUploadChange}
fileList={vertexFileList}
rules={
isEdit
? []
: [
{
required: true,
message: t('请上传密钥文件'),
},
]
}
extraText={batchExtra}
/>
)}
</>
) : (
<Form.Input
field='key'
label={
isEdit
? t('密钥(编辑模式下,保存的密钥不会显示)')
: t('密钥')
}
placeholder={
inputs.type === 33
? inputs.aws_key_type === 'api_key'
? t('请输入 API Key,格式:APIKey|Region')
: t(
'按照如下格式输入:AccessKey|SecretAccessKey|Region',
)
: t(type2secretPrompt(inputs.type))
}
rules={
isEdit
? []
: [{ required: true, message: t('请输入密钥') }]
}
autoComplete='new-password'
onChange={(value) =>
handleInputChange('key', value)
}
extraText={
<div className='flex items-center gap-2'>
{isEdit &&
isMultiKeyChannel &&
keyMode === 'append' && (
<Text type='warning' size='small'>
{t(
'追加模式:新密钥将添加到现有密钥列表的末尾',
)}
</Text>
)}
{isEdit && (
<Button
size='small'
type='primary'
theme='outline'
onClick={handleShow2FAModal}
>
{t('查看密钥')}
</Button>
)}
{batchExtra}
</div>
}
showClear
/>
)}
</>
)}
{isEdit && isMultiKeyChannel && (
<Form.Select
field='key_mode'
label={t('密钥更新模式')}
placeholder={t('请选择密钥更新模式')}
optionList={[
{ label: t('追加到现有密钥'), value: 'append' },
{ label: t('覆盖现有密钥'), value: 'replace' },
]}
style={{ width: '100%' }}
value={keyMode}
onChange={(value) => setKeyMode(value)}
extraText={
<Text type='tertiary' size='small'>
{keyMode === 'replace'
? t('覆盖模式:将完全替换现有的所有密钥')
: t('追加模式:将新密钥添加到现有密钥列表末尾')}
</Text>
}
/>
)}
{batch && multiToSingle && (
<>
<Form.Select
field='multi_key_mode'
label={t('密钥聚合模式')}
placeholder={t('请选择多密钥使用策略')}
optionList={[
{ label: t('随机'), value: 'random' },
{ label: t('轮询'), value: 'polling' },
]}
style={{ width: '100%' }}
value={inputs.multi_key_mode || 'random'}
onChange={(value) => {
setMultiKeyMode(value);
handleInputChange('multi_key_mode', value);
}}
/>
{inputs.multi_key_mode === 'polling' && (
<Banner
type='warning'
description={t(
'轮询模式必须搭配Redis和内存缓存功能使用,否则性能将大幅降低,并且无法实现轮询功能',
)}
className='!rounded-lg mt-2'
/>
)}
</>
)}
{inputs.type === 18 && (
<Form.Input
field='other'
label={t('模型版本')}
placeholder={t(
'请输入星火大模型版本,注意是接口地址中的版本号,例如:v2.1',
)}
onChange={(value) => handleInputChange('other', value)}
showClear
/>
)}
{inputs.type === 41 && (
<JSONEditor
key={`region-${isEdit ? channelId : 'new'}`}
field='other'
label={t('部署地区')}
placeholder={t(
'请输入部署地区,例如:us-central1\n支持使用模型映射格式\n{\n "default": "us-central1",\n "claude-3-5-sonnet-20240620": "europe-west1"\n}',
)}
value={inputs.other || ''}
onChange={(value) => handleInputChange('other', value)}
rules={[
{ required: true, message: t('请填写部署地区') },
]}
template={REGION_EXAMPLE}
templateLabel={t('填入模板')}
editorType='region'
formApi={formApiRef.current}
extraText={t('设置默认地区和特定模型的专用地区')}
/>
)}
{inputs.type === 21 && (
<Form.Input
field='other'
label={t('知识库 ID')}
placeholder={t('请输入知识库 ID,例如:123456')}
onChange={(value) => handleInputChange('other', value)}
showClear
/>
)}
{inputs.type === 39 && (
<Form.Input
field='other'
label='Account ID'
placeholder={t(
'请输入Account ID,例如:d6b5da8hk1awo8nap34ube6gh',
)}
onChange={(value) => handleInputChange('other', value)}
showClear
/>
)}
{inputs.type === 49 && (
<Form.Input
field='other'
label={t('智能体ID')}
placeholder={t('请输入智能体ID,例如:7342866812345')}
onChange={(value) => handleInputChange('other', value)}
showClear
/>
)}
{inputs.type === 1 && (
<Form.Input
field='openai_organization'
label={t('组织')}
placeholder={t('组织 org-xxx(留空使用默认组织)')}
showClear
onChange={(value) =>
handleInputChange('openai_organization', value)
}
/>
)}
</Card>
</div>
{/* API Configuration Card */}
{showApiConfigCard && (
<div>
<Card className='!rounded-2xl shadow-sm border-0 mb-6'>
{/* Header: API Config */}
<div
className='console-channel-section'
onClick={handleApiConfigSecretClick}
>
<Avatar size='small' color='green'>
<IconGlobe size={16} />
</Avatar>
<div>
<Text className='text-lg font-medium'>
{t('API 配置')}
</Text>
<div className='text-xs text-gray-600'>
{t('API 地址和相关配置')}
</div>
</div>
</div>
{inputs.type === 40 && (
<Banner
type='info'
description={
<div>
<Text strong>{t('邀请链接')}:</Text>
<Text
link
underline
className='ml-2 cursor-pointer'
onClick={() =>
window.open(
'https://cloud.siliconflow.cn/i/hij0YNTZ',
)
}
>
https://cloud.siliconflow.cn/i/hij0YNTZ
</Text>
</div>
}
className='!rounded-lg'
/>
)}
{inputs.type === 3 && (
<>
<Banner
type='warning'
description={t(
'2025年5月10日后添加的渠道,不需要再在部署的时候移除模型名称中的"."',
)}
className='!rounded-lg'
/>
<div>
<Form.Input
field='base_url'
label='AZURE_OPENAI_ENDPOINT'
placeholder={t(
'请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com',
)}
onChange={(value) =>
handleInputChange('base_url', value)
}
showClear
/>
</div>
<div>
<Form.Input
field='other'
label={t('默认 API 版本')}
placeholder={t(
'请输入默认 API 版本,例如:2025-04-01-preview',
)}
onChange={(value) =>
handleInputChange('other', value)
}
showClear
/>
</div>
<div>
<Form.Input
field='azure_responses_version'
label={t(
'默认 Responses API 版本,为空则使用上方版本',
)}
placeholder={t('例如:preview')}
onChange={(value) =>
handleChannelOtherSettingsChange(
'azure_responses_version',
value,
)
}
showClear
/>
</div>
</>
)}
{inputs.type === 8 && (
<>
<Banner
type='warning'
description={t(
'如果你对接的是上游One API或者New API等转发项目,请使用OpenAI类型,不要使用此类型,除非你知道你在做什么。',
)}
className='!rounded-lg'
/>
<div>
<Form.Input
field='base_url'
label={t('完整的 Base URL,支持变量{model}')}
placeholder={t(
'请输入完整的URL,例如:https://api.openai.com/v1/chat/completions',
)}
onChange={(value) =>
handleInputChange('base_url', value)
}
showClear
/>
</div>
</>
)}
{inputs.type === 37 && (
<Banner
type='warning'
description={t(
'Dify渠道只适配chatflow和agent,并且agent不支持图片!',
)}
className='!rounded-lg'
/>
)}
{inputs.type !== 3 &&
inputs.type !== 8 &&
inputs.type !== 22 &&
inputs.type !== 36 &&
(inputs.type !== 45 || doubaoApiEditUnlocked) && (
<div>
<Form.Input
field='base_url'
label={t('API地址')}
placeholder={t(
'此项可选,用于通过自定义API地址来进行 API 调用,末尾不要带/v1和/',
)}
onChange={(value) =>
handleInputChange('base_url', value)
}
showClear
extraText={t(
'对于官方渠道,new-api已经内置地址,除非是第三方代理站点或者Azure的特殊接入地址,否则不需要填写',
)}
/>
</div>
)}
{inputs.type === 22 && (
<div>
<Form.Input
field='base_url'
label={t('私有部署地址')}
placeholder={t(
'请输入私有部署地址,格式为:https://fastgpt.run/api/openapi',
)}
onChange={(value) =>
handleInputChange('base_url', value)
}
showClear
/>
</div>
)}
{inputs.type === 36 && (
<div>
<Form.Input
field='base_url'
label={t(
'注意非Chat API,请务必填写正确的API地址,否则可能导致无法使用',
)}
placeholder={t(
'请输入到 /suno 前的路径,通常就是域名,例如:https://api.example.com',
)}
onChange={(value) =>
handleInputChange('base_url', value)
}
showClear
/>
</div>
)}
{inputs.type === 45 && !doubaoApiEditUnlocked && (
<div>
<Form.Select
field='base_url'
label={t('API地址')}
placeholder={t('请选择API地址')}
onChange={(value) =>
handleInputChange('base_url', value)
}
optionList={[
{
value: 'https://ark.cn-beijing.volces.com',
label: 'https://ark.cn-beijing.volces.com',
},
{
value:
'https://ark.ap-southeast.bytepluses.com',
label:
'https://ark.ap-southeast.bytepluses.com',
},
{
value: 'doubao-coding-plan',
label: 'Doubao Coding Plan',
},
]}
defaultValue='https://ark.cn-beijing.volces.com'
/>
</div>
)}
</Card>
</div>
)}
{/* Model Configuration Card */}
<div>
<Card className='!rounded-2xl shadow-sm border-0 mb-6'>
{/* Header: Model Config */}
<div className='console-channel-section'>
<Avatar size='small' color='purple'>
<IconCode size={16} />
</Avatar>
<div>
<Text className='text-lg font-medium'>
{t('模型配置')}
</Text>
<div className='text-xs text-gray-600'>
{t('模型选择和映射设置')}
</div>
</div>
</div>
<Form.Select
field='models'
label={t('模型')}
placeholder={t('请选择该渠道所支持的模型')}
rules={[{ required: true, message: t('请选择模型') }]}
multiple
filter={selectFilter}
autoClearSearchValue={false}
searchPosition='dropdown'
optionList={modelOptions}
style={{ width: '100%' }}
onChange={(value) => handleInputChange('models', value)}
renderSelectedItem={(optionNode) => {
const modelName = String(optionNode?.value ?? '');
return {
isRenderInTag: true,
content: (
<span
className='cursor-pointer select-none'
role='button'
tabIndex={0}
title={t('点击复制模型名称')}
onClick={async (e) => {
e.stopPropagation();
const ok = await copy(modelName);
if (ok) {
showSuccess(
t('已复制:{{name}}', { name: modelName }),
);
} else {
showError(t('复制失败'));
}
}}
>
{optionNode.label || modelName}
</span>
),
};
}}
extraText={
<Space wrap className='console-channel-model-actions'>
<Button
size='small'
type='primary'
onClick={() =>
handleInputChange('models', basicModels)
}
>
{t('填入相关模型')}
</Button>
<Button
size='small'
type='secondary'
onClick={() =>
handleInputChange('models', fullModels)
}
>
{t('填入所有模型')}
</Button>
{MODEL_FETCHABLE_TYPES.has(inputs.type) && (
<Button
size='small'
type='tertiary'
onClick={() => fetchUpstreamModelList('models')}
>
{t('获取模型列表')}
</Button>
)}
<Button
size='small'
type='warning'
onClick={() => handleInputChange('models', [])}
>
{t('清除所有模型')}
</Button>
<Button
size='small'
type='tertiary'
onClick={async () => {
if (inputs.models.length === 0) {
showInfo(t('没有模型可以复制'));
return;
}
try {
if (!(await copy(inputs.models.join(',')))) {
throw new Error('Clipboard copy failed');
}
showSuccess(t('模型列表已复制到剪贴板'));
} catch (error) {
showError(t('复制失败'));
}
}}
>
{t('复制所有模型')}
</Button>
{modelGroups &&
modelGroups.length > 0 &&
modelGroups.map((group) => (
<Button
key={group.id}
size='small'
type='primary'
onClick={() => {
let items = [];
try {
if (Array.isArray(group.items)) {
items = group.items;
} else if (
typeof group.items === 'string'
) {
const parsed = JSON.parse(
group.items || '[]',
);
if (Array.isArray(parsed)) items = parsed;
}
} catch {}
const current =
formApiRef.current?.getValue('models') ||
inputs.models ||
[];
const merged = Array.from(
new Set(
[...current, ...items]
.map((m) => (m || '').trim())
.filter(Boolean),
),
);
handleInputChange('models', merged);
}}
>
{group.name}
</Button>
))}
</Space>
}
/>
<Form.Input
field='custom_model'
label={t('自定义模型名称')}
placeholder={t('输入自定义模型名称')}
onChange={(value) => setCustomModel(value.trim())}
value={customModel}
suffix={
<Button
size='small'
type='primary'
onClick={addCustomModels}
>
{t('填入')}
</Button>
}
/>
<Form.Input
field='test_model'
label={t('默认测试模型')}
placeholder={t('不填则为模型列表第一个')}
onChange={(value) =>
handleInputChange('test_model', value)
}
showClear
/>
<JSONEditor
key={`model_mapping-${isEdit ? channelId : 'new'}`}
field='model_mapping'
label={t('模型重定向')}
placeholder={
t(
'此项可选,用于修改请求体中的模型名称,为一个 JSON 字符串,键为请求中模型名称,值为要替换的模型名称,例如:',
) +
`\n${JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2)}`
}
value={inputs.model_mapping || ''}
onChange={(value) =>
handleInputChange('model_mapping', value)
}
template={MODEL_MAPPING_EXAMPLE}
templateLabel={t('填入模板')}
editorType='keyValue'
formApi={formApiRef.current}
extraText={t(
'键为请求中的模型名称,值为要替换的模型名称',
)}
/>
</Card>
</div>
{/* Advanced Settings Card */}
<div>
<Card className='!rounded-2xl shadow-sm border-0 mb-6'>
{/* Header: Advanced Settings */}
<div className='console-channel-section'>
<Avatar size='small' color='orange'>
<IconSetting size={16} />
</Avatar>
<div>
<Text className='text-lg font-medium'>
{t('高级设置')}
</Text>
<div className='text-xs text-gray-600'>
{t('渠道的高级配置选项')}
</div>
</div>
</div>
<Form.Select
field='groups'
label={t('分组')}
placeholder={t('请选择可以使用该渠道的分组')}
multiple
allowAdditions
additionLabel={t(
'请在系统设置页面编辑分组倍率以添加新的分组:',
)}
optionList={groupOptions}
style={{ width: '100%' }}
onChange={(value) => handleInputChange('groups', value)}
/>
<Form.Input
field='tag'
label={t('渠道标签')}
placeholder={t('渠道标签')}
showClear
onChange={(value) => handleInputChange('tag', value)}
/>
<Form.TextArea
field='remark'
label={t('备注')}
placeholder={t('请输入备注(仅管理员可见)')}
maxLength={255}
showClear
onChange={(value) => handleInputChange('remark', value)}
/>
<Row gutter={12}>
<Col span={12}>
<Form.InputNumber
field='priority'
label={t('渠道优先级')}
placeholder={t('渠道优先级')}
min={0}
onNumberChange={(value) =>
handleInputChange('priority', value)
}
style={{ width: '100%' }}
/>
</Col>
<Col span={12}>
<Form.InputNumber
field='weight'
label={t('渠道权重')}
placeholder={t('渠道权重')}
min={0}
onNumberChange={(value) =>
handleInputChange('weight', value)
}
style={{ width: '100%' }}
/>
</Col>
</Row>
{isEdit && fallbackReferrers.length > 0 && (
<Banner
type='warning'
closeIcon={null}
style={{ marginBottom: 12 }}
description={
<span>
{t(
'当前渠道被以下 {{count}} 个渠道列为兜底,删除/禁用本渠道将影响它们:',
{ count: fallbackReferrers.length },
)}{' '}
{fallbackReferrers.slice(0, 5).map((r) => (
<Tag
key={r.id}
color='orange'
style={{ marginRight: 4 }}
>
#{r.id} {r.name}
</Tag>
))}
{fallbackReferrers.length > 5
? t('等 {{n}} 个', {
n: fallbackReferrers.length,
})
: null}
</span>
}
/>
)}
<Form.Select
field='fallback_channel_ids'
label={t('兜底渠道(多选,按选择顺序作为兜底链)')}
placeholder={t(
'选填,当本渠道请求失败时,按顺序尝试这些兜底渠道',
)}
multiple
filter
showClear
max={20}
optionList={fallbackOptionList}
// Semi 对 disabled option 的已选标签不给关闭按钮;
// 已禁用/已删除的渠道需要能从链中移除,故自绘可关闭标签
renderSelectedItem={(option, { onClose }) => ({
isRenderInTag: false,
content: (
<Tag
closable
color='white'
size='large'
onClose={onClose}
style={{ maxWidth: '100%' }}
>
{option.label}
</Tag>
),
})}
onChange={(value) =>
handleInputChange('fallback_channel_ids', value || [])
}
style={{ width: '100%' }}
extraText={t(
'需在「运营设置-兜底渠道」开启总开关后生效;仅 Root 管理员可配置;最多 20 个,运行时最多尝试「最大兜底链长」个;被禁用、不支持该模型、分组或地域不匹配的渠道会在运行时被跳过',
)}
/>
{Array.isArray(inputs.fallback_channel_ids) &&
inputs.fallback_channel_ids.length > 0 && (
<>
<Row gutter={12}>
<Col span={12}>
<Form.InputNumber
field='fallback_primary_attempts'
label={t('主渠道失败次数')}
min={1}
max={5}
precision={0}
onChange={(value) =>
handleInputChange(
'fallback_primary_attempts',
value,
)
}
style={{ width: '100%' }}
extraText={t(
'本渠道连续失败达到该次数后才切换兜底,1 = 失败即切',
)}
/>
</Col>
<Col span={12}>
<Form.InputNumber
field='fallback_hop_attempts'
label={t('兜底渠道失败次数')}
min={1}
max={5}
precision={0}
onChange={(value) =>
handleInputChange(
'fallback_hop_attempts',
value,
)
}
style={{ width: '100%' }}
extraText={t(
'兜底链上每个渠道连续失败达到该次数后才切换下一个',
)}
/>
</Col>
</Row>
{(inputs.fallback_primary_attempts > 1 ||
inputs.fallback_hop_attempts > 1) && (
<Banner
type='info'
closeIcon={null}
style={{ marginBottom: 12 }}
description={t(
'所有渠道故障(含 401/429)都会立即重试;剩余总超时不足以同时容纳重试和兜底、达到单请求最大尝试次数、或渠道近期已试满失败时,会提前切换兜底;总超时较紧时,重试的单次超时会被缩短,耗时较长的流式输出可能被提前中断',
)}
/>
)}
</>
)}
<Form.Select
field='region'
label={t('渠道地域(用于跨境合规判定)')}
placeholder={t('选填,未标记时不参与跨境拦截')}
showClear
optionList={[
{ value: 'CN', label: t('CN(中国大陆)') },
{ value: 'HK', label: t('HK(中国香港)') },
{ value: 'SG', label: t('SG(新加坡)') },
{ value: 'JP', label: t('JP(日本)') },
{ value: 'US', label: t('US(美国)') },
{ value: 'EU', label: t('EU(欧盟)') },
{ value: 'OTHER', label: t('其他') },
]}
onChange={(value) =>
handleInputChange('region', value || '')
}
style={{ width: '100%' }}
extraText={t(
'跨境兜底关闭时,仅当本渠道与兜底渠道都标记了地域且地域不同才会被拦截;任一方未标记则不拦截',
)}
/>
<Form.InputNumber
field='daily_quota_limit'
label={t('日消耗限额')}
placeholder={t('设置为 0 表示不限制')}
min={0}
onNumberChange={(value) =>
handleInputChange('daily_quota_limit', value)
}
style={{ width: '100%' }}
extraText={t('日消耗限额说明')}
/>
<div style={{ marginBottom: 12 }}>
<div style={{ marginBottom: 8 }}>
<Text strong>{t('工作时间段')}</Text>
<Text
type='tertiary'
size='small'
style={{ marginLeft: 8 }}
>
{t('工作时间段说明')}
</Text>
</div>
<Row gutter={12}>
<Col span={8}>
<Form.Select
field='work_hours_start'
label={t('开始时间')}
placeholder={t('不限制')}
showClear
optionList={WORK_HOURS_OPTIONS}
onChange={(value) =>
handleInputChange('work_hours_start', value)
}
style={{ width: '100%' }}
/>
</Col>
<Col span={8}>
<Form.Select
field='work_hours_end'
label={t('结束时间')}
placeholder={t('不限制')}
showClear
optionList={WORK_HOURS_OPTIONS}
onChange={(value) =>
handleInputChange('work_hours_end', value)
}
style={{ width: '100%' }}
/>
</Col>
<Col span={8}>
<Form.Select
field='work_hours_timezone'
label={t('时区')}
optionList={TIMEZONE_OPTIONS}
onChange={(value) =>
handleInputChange('work_hours_timezone', value)
}
style={{ width: '100%' }}
filter
/>
</Col>
</Row>
{inputs.work_hours_start != null &&
inputs.work_hours_end != null &&
inputs.work_hours_start !== inputs.work_hours_end && (
<Text
type='tertiary'
size='small'
style={{ marginTop: 4, display: 'block' }}
>
{inputs.work_hours_start < inputs.work_hours_end
? `${t('工作时间')}: ${String(inputs.work_hours_start).padStart(2, '0')}:00 ~ ${String(inputs.work_hours_end).padStart(2, '0')}:00`
: `${t('工作时间')}: ${String(inputs.work_hours_start).padStart(2, '0')}:00 ~ ${t('次日')} ${String(inputs.work_hours_end).padStart(2, '0')}:00`}
{` (${inputs.work_hours_timezone || 'Asia/Shanghai'})`}
</Text>
)}
</div>
<Form.Switch
field='auto_ban'
label={t('是否自动禁用')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) => setAutoBan(value)}
extraText={t(
'仅当自动禁用开启时有效,关闭后不会自动禁用该渠道',
)}
initValue={autoBan}
/>
<Form.TextArea
field='param_override'
label={t('参数覆盖')}
placeholder={
t(
'此项可选,用于覆盖请求参数。不支持覆盖 stream 参数',
) +
'\n' +
t('旧格式(直接覆盖):') +
'\n{\n "temperature": 0,\n "max_tokens": 1000\n}' +
'\n\n' +
t('新格式(支持条件判断与json自定义):') +
'\n{\n "operations": [\n {\n "path": "temperature",\n "mode": "set",\n "value": 0.7,\n "conditions": [\n {\n "path": "model",\n "mode": "prefix",\n "value": "gpt"\n }\n ]\n }\n ]\n}'
}
autosize
onChange={(value) =>
handleInputChange('param_override', value)
}
extraText={
<div className='flex gap-2 flex-wrap'>
<Text
className='!text-ui-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'param_override',
JSON.stringify({ temperature: 0 }, null, 2),
)
}
>
{t('旧格式模板')}
</Text>
<Text
className='!text-ui-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'param_override',
JSON.stringify(
{
operations: [
{
path: 'temperature',
mode: 'set',
value: 0.7,
conditions: [
{
path: 'model',
mode: 'prefix',
value: 'gpt',
},
],
logic: 'AND',
},
],
},
null,
2,
),
)
}
>
{t('新格式模板')}
</Text>
</div>
}
showClear
/>
<Form.TextArea
field='header_override'
label={t('请求头覆盖')}
placeholder={
t('此项可选,用于覆盖请求头参数') +
'\n' +
t('格式示例:') +
'\n{\n "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0",\n "Authorization": "Bearer {api_key}"\n}'
}
autosize
onChange={(value) =>
handleInputChange('header_override', value)
}
extraText={
<div className='flex flex-col gap-1'>
<div className='flex gap-2 flex-wrap items-center'>
<Text
className='!text-ui-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'header_override',
JSON.stringify(
{
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0',
Authorization: 'Bearer{api_key}',
},
null,
2,
),
)
}
>
{t('填入模板')}
</Text>
</div>
<div>
<Text type='tertiary' size='small'>
{t('支持变量:')}
</Text>
<div className='text-xs text-tertiary ml-2'>
<div>
{t('渠道密钥')}: {'{api_key}'}
</div>
</div>
</div>
</div>
}
showClear
/>
<JSONEditor
key={`status_code_mapping-${isEdit ? channelId : 'new'}`}
field='status_code_mapping'
label={t('状态码复写')}
placeholder={
t(
'此项可选,用于复写返回的状态码,仅影响本地判断,不修改返回到上游的状态码,比如将claude渠道的400错误复写为500(用于重试),请勿滥用该功能,例如:',
) +
'\n' +
JSON.stringify(STATUS_CODE_MAPPING_EXAMPLE, null, 2)
}
value={inputs.status_code_mapping || ''}
onChange={(value) =>
handleInputChange('status_code_mapping', value)
}
template={STATUS_CODE_MAPPING_EXAMPLE}
templateLabel={t('填入模板')}
editorType='keyValue'
formApi={formApiRef.current}
extraText={t(
'键为原状态码,值为要复写的状态码,仅影响本地判断',
)}
/>
{/* 字段透传控制 - OpenAI 渠道 */}
{inputs.type === 1 && (
<>
<div className='mt-4 mb-2 text-sm font-medium text-gray-700'>
{t('字段透传控制')}
</div>
<Form.Switch
field='allow_service_tier'
label={t('允许 service_tier 透传')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelOtherSettingsChange(
'allow_service_tier',
value,
)
}
extraText={t(
'service_tier 字段用于指定服务层级,允许透传可能导致实际计费高于预期。默认关闭以避免额外费用',
)}
/>
<Form.Switch
field='disable_store'
label={t('禁用 store 透传')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelOtherSettingsChange(
'disable_store',
value,
)
}
extraText={t(
'store 字段用于授权 OpenAI 存储请求数据以评估和优化产品。默认关闭,开启后可能导致 Codex 无法正常使用',
)}
/>
<Form.Switch
field='allow_safety_identifier'
label={t('允许 safety_identifier 透传')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelOtherSettingsChange(
'allow_safety_identifier',
value,
)
}
extraText={t(
'safety_identifier 字段用于帮助 OpenAI 识别可能违反使用政策的应用程序用户。默认关闭以保护用户隐私',
)}
/>
</>
)}
{/* 字段透传控制 - Claude 渠道 */}
{inputs.type === 14 && (
<>
<div className='mt-4 mb-2 text-sm font-medium text-gray-700'>
{t('字段透传控制')}
</div>
<Form.Switch
field='allow_service_tier'
label={t('允许 service_tier 透传')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelOtherSettingsChange(
'allow_service_tier',
value,
)
}
extraText={t(
'service_tier 字段用于指定服务层级,允许透传可能导致实际计费高于预期。默认关闭以避免额外费用',
)}
/>
</>
)}
</Card>
</div>
{/* Channel Extra Settings Card */}
<div>
<Card className='!rounded-2xl shadow-sm border-0 mb-6'>
{/* Header: Channel Extra Settings */}
<div className='console-channel-section'>
<Avatar size='small' color='violet'>
<IconBolt size={16} />
</Avatar>
<div>
<Text className='text-lg font-medium'>
{t('渠道额外设置')}
</Text>
</div>
</div>
{inputs.type === 17 && (
<Form.Switch
field='ali_data_inspection_disabled'
label={t('阿里云关闭输入/输出绿网(DataInspection)')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange(
'ali_data_inspection_disabled',
value,
)
}
extraText={t(
'关闭阿里云输入和输出内容安全检测。需要上游权限,生产前请先使用测试 Key 验证;默认关闭。',
)}
/>
)}
{inputs.type === 62 && (
<Form.Select
field='baidu_liveness_header_mode'
label={t('百度真人素材请求头模式')}
optionList={[
{
label: t('兼容模式(默认,发送真人请求头)'),
value: 'legacy',
},
{
label: t('不发送(供应商明确要求时选择)'),
value: 'omit',
},
]}
onChange={(value) =>
handleChannelSettingsChange(
'baidu_liveness_header_mode',
value,
)
}
extraText={t(
'根据供应商账号要求选择,与模型名称无关。已有认证、素材或未完成操作的渠道不能切换,请新建独立渠道。',
)}
/>
)}
{inputs.type === 1 && (
<Form.Switch
field='force_format'
label={t('强制格式化')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange('force_format', value)
}
extraText={t(
'强制将响应格式化为 OpenAI 标准格式(只适用于OpenAI渠道类型)',
)}
/>
)}
<Form.Switch
field='thinking_to_content'
label={t('思考内容转换')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange(
'thinking_to_content',
value,
)
}
extraText={t(
'将 reasoning_content 转换为 <think> 标签拼接到内容中',
)}
/>
<Form.Switch
field='pass_through_body_enabled'
label={t('透传请求体')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange(
'pass_through_body_enabled',
value,
)
}
extraText={t('启用请求体透传功能')}
/>
{(inputs.type === 1 || inputs.type === 3) && (
<Form.Switch
field='chat_image_to_images_enabled'
label={t('Chat 图片请求桥接')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange(
'chat_image_to_images_enabled',
value,
)
}
extraText={t(
'将图片模型的 Chat Completions 请求转换为 Images API,并把结果转换回 Chat 格式;仅为需要协议适配的渠道开启',
)}
/>
)}
<Form.Switch
field='client_only'
label={t('仅限客户端')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange('client_only', value)
}
extraText={t(
'只接受来自 Claude Code 客户端的请求,开启后将自动透传请求体和请求头',
)}
/>
<Form.TextArea
field='bound_user_ids'
label={t('绑定用户 ID')}
placeholder={t('如 1001,1002,1003')}
onChange={(value) =>
handleChannelSettingsChange('bound_user_ids', value)
}
onBlur={(e) => {
const raw = e.target.value || '';
if (!raw.trim()) return;
const parts = raw.split(',');
const invalids = [];
const seen = new Set();
const duplicates = [];
for (const p of parts) {
const trimmed = p.trim();
if (!trimmed) continue;
if (
!/^\d+$/.test(trimmed) ||
parseInt(trimmed, 10) <= 0
) {
invalids.push(trimmed);
} else {
if (seen.has(trimmed)) {
duplicates.push(trimmed);
}
seen.add(trimmed);
}
}
if (invalids.length > 0) {
showError(
t('绑定用户 ID 格式错误,以下不是有效的正整数') +
': ' +
invalids.join(', '),
);
}
if (duplicates.length > 0) {
showInfo(
t('绑定用户 ID 存在重复') +
': ' +
duplicates.join(', '),
);
}
}}
autosize={{ minRows: 1, maxRows: 3 }}
showClear
extraText={t(
'填写用户 ID(逗号分隔),这些用户的请求将强制路由到此渠道。留空表示不限制。',
)}
/>
<Form.Select
field='os_restriction'
label={t('操作系统限制')}
placeholder={t('不勾选则不限制(承载所有流量)')}
multiple
filter
optionList={[
{ label: 'Windows', value: 'windows' },
{ label: 'macOS', value: 'macos' },
{ label: 'Linux', value: 'linux' },
]}
style={{ width: '100%' }}
onChange={(value) =>
handleChannelSettingsChange(
'os_restriction',
Array.isArray(value) ? value : [],
)
}
extraText={t(
'依据 X-Stainless-OS 请求头识别客户端操作系统并路由。勾选后该渠道只接受白名单操作系统的请求,不勾选则为公共池,承载所有操作系统及无标识的流量。目前仅对 Claude 原生渠道(Anthropic)在运行时生效。',
)}
/>
<Form.Select
field='client_type_restriction'
label={t('客户端类型限制')}
placeholder={t('不勾选则不限制(承载所有客户端)')}
multiple
filter
optionList={[
{ label: t('命令行 (cli)'), value: 'cli' },
{ label: 'VSCode', value: 'vscode' },
]}
style={{ width: '100%' }}
onChange={(value) =>
handleChannelSettingsChange(
'client_type_restriction',
Array.isArray(value) ? value : [],
)
}
extraText={t(
'依据 claude-cli User-Agent 识别客户端类型(cli / vscode)并路由。与操作系统、版本范围为「同时满足(AND)」关系。仅对 Claude 原生渠道生效。',
)}
/>
<div style={{ display: 'flex', gap: 8, width: '100%' }}>
<Form.Input
field='client_version_min'
label={t('客户端版本下界')}
placeholder={t('如 2.1.90,留空=从最早')}
style={{ flex: 1 }}
onChange={(value) =>
handleChannelSettingsChange(
'client_version_min',
value,
)
}
validateStatus={
inputs.client_version_min &&
!/^\d+\.\d+\.\d+$/.test(
(inputs.client_version_min || '').trim(),
)
? 'error'
: 'default'
}
/>
<Form.Input
field='client_version_max'
label={t('客户端版本上界')}
placeholder={t('如 2.1.154,留空=至最新')}
style={{ flex: 1 }}
onChange={(value) =>
handleChannelSettingsChange(
'client_version_max',
value,
)
}
validateStatus={
inputs.client_version_max &&
!/^\d+\.\d+\.\d+$/.test(
(inputs.client_version_max || '').trim(),
)
? 'error'
: 'default'
}
/>
</div>
<div
style={{
marginTop: -8,
marginBottom: 8,
fontSize: 12,
color: 'var(--ui-color-text-2)',
}}
>
{t(
'客户端版本闭区间(含端点),三段式 x.y.z。仅对 Claude 原生渠道生效。有任一路由规则的渠道=专用池只接命中流量;三维全空=公共兜底池。',
)}
</div>
<div className='mt-4 mb-2 text-sm font-medium text-gray-700'>
{t('上游缓存优化(仅 Claude 原生渠道生效)')}
</div>
<Form.Switch
field='sticky_user_id_fill'
label={t('会话粘性 user_id 填充')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange(
'sticky_user_id_fill',
value,
)
}
extraText={t(
'为缺失 metadata.user_id 的请求补一个「会话内稳定」的值,使按 user_id 哈希选号的上游把同一会话粘在同一账号上。仅对已确认按 user_id 粘号的上游开启。',
)}
/>
<Form.Select
field='sticky_user_id_mode'
label={t('粘性 user_id 形态')}
style={{ width: '100%' }}
optionList={[
{
label: t('native(仿 Claude Code 原生)'),
value: 'native',
},
{
label: t('opaque(wl_ 前缀不透明串)'),
value: 'opaque',
},
]}
onChange={(value) =>
handleChannelSettingsChange(
'sticky_user_id_mode',
value,
)
}
extraText={t(
'上游只做哈希,形态不影响路由;native 可避免上游按形态做白名单校验时被判成异常客户端。',
)}
/>
<Form.Switch
field='cache_breakpoint_fill'
label={t('缓存断点注入')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange(
'cache_breakpoint_fill',
value,
)
}
extraText={t(
'给「一个 cache_control 都没有」的大上下文请求补一个缓存断点。必须同时开启上面的会话粘性填充,否则注入后每轮哈希到不同账号、只写不读,按 1h 档成本直接翻倍(未开粘性时后端会拒绝注入并记 no_sticky)。',
)}
/>
<Form.InputNumber
field='cache_breakpoint_min_tokens'
label={t('缓存断点注入门限(prompt token)')}
placeholder={t('0 = 用默认值 10000')}
min={0}
step={1000}
style={{ width: '100%' }}
onChange={(value) =>
handleChannelSettingsChange(
'cache_breakpoint_min_tokens',
value,
)
}
extraText={t(
'低于该输入规模的请求不注入。门限越低覆盖越广但边际收益递减:≥50000 覆盖 2.7%、≥10000 覆盖 7.7%、≥2000 覆盖 21.4%。0 = 用后端默认 10000;小于 1024(最小可缓存前缀)时后端自动抬到 1024。',
)}
/>
<Form.Input
field='proxy'
label={t('代理地址')}
placeholder={t('例如: socks5://user:pass@host:port')}
onChange={(value) =>
handleChannelSettingsChange('proxy', value)
}
showClear
extraText={t('用于配置网络代理,支持 socks5 协议')}
/>
<Form.TextArea
field='system_prompt'
label={t('系统提示词')}
placeholder={t(
'输入系统提示词,用户的系统提示词将优先于此设置',
)}
onChange={(value) =>
handleChannelSettingsChange('system_prompt', value)
}
autosize
showClear
extraText={t(
'用户优先:如果用户在请求中指定了系统提示词,将优先使用用户的设置',
)}
/>
<Form.Switch
field='system_prompt_override'
label={t('系统提示词拼接')}
checkedText={t('开')}
uncheckedText={t('关')}
onChange={(value) =>
handleChannelSettingsChange(
'system_prompt_override',
value,
)
}
extraText={t(
'如果用户请求中包含系统提示词,则使用此设置拼接到用户的系统提示词前面',
)}
/>
</Card>
</div>
</div>
</Spin>
)}
</Form>
<ImagePreview
src={modalImageUrl}
visible={isModalOpenurl}
onVisibleChange={(visible) => setIsModalOpenurl(visible)}
/>
</SideSheet>
{/* 使用通用安全验证模态框 */}
<SecureVerificationModal
visible={isModalVisible}
verificationMethods={verificationMethods}
verificationState={verificationState}
onVerify={executeVerification}
onCancel={cancelVerification}
onCodeChange={setVerificationCode}
onMethodSwitch={switchVerificationMethod}
title={verificationState.title}
description={verificationState.description}
/>
{/* 使用ChannelKeyDisplay组件显示密钥 */}
<Modal
title={
<div className='flex items-center'>
<div className='w-8 h-8 rounded-full bg-green-100 dark:bg-green-900 flex items-center justify-center mr-3'>
<svg
className='w-4 h-4 text-green-600 dark:text-green-400'
fill='currentColor'
viewBox='0 0 20 20'
>
<path
fillRule='evenodd'
d='M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z'
clipRule='evenodd'
/>
</svg>
</div>
{t('渠道密钥信息')}
</div>
}
visible={keyDisplayState.showModal}
onCancel={resetKeyDisplayState}
footer={
<Button type='primary' onClick={resetKeyDisplayState}>
{t('完成')}
</Button>
}
width={700}
style={{ maxWidth: '90vw' }}
>
<ChannelKeyDisplay
keyData={keyDisplayState.keyData}
showSuccessIcon={true}
successText={t('密钥获取成功')}
showWarning={true}
warningText={t(
'请妥善保管密钥信息,不要泄露给他人。如有安全疑虑,请及时更换密钥。',
)}
/>
</Modal>
<ModelSelectModal
visible={modelModalVisible}
models={fetchedModels}
selected={inputs.models}
redirectModels={redirectModelList}
onConfirm={(selectedModels) => {
handleInputChange('models', selectedModels);
showSuccess(t('模型列表已更新'));
setModelModalVisible(false);
}}
onCancel={() => setModelModalVisible(false)}
/>
</>
);
};
export default EditChannelModal;