EditChannelModal.jsx 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useEffect, useState, useRef, useMemo } from 'react';
  16. import { useTranslation } from 'react-i18next';
  17. import {
  18. API,
  19. showError,
  20. showInfo,
  21. showSuccess,
  22. verifyJSON,
  23. } from '../../../../helpers';
  24. import { useIsMobile } from '../../../../hooks/common/useIsMobile';
  25. import { CHANNEL_OPTIONS } from '../../../../constants';
  26. import {
  27. SideSheet,
  28. Space,
  29. Spin,
  30. Button,
  31. Typography,
  32. Checkbox,
  33. Banner,
  34. Modal,
  35. ImagePreview,
  36. Card,
  37. Tag,
  38. Avatar,
  39. Form,
  40. Row,
  41. Col,
  42. Highlight,
  43. } from '@douyinfe/semi-ui';
  44. import { getChannelModels, copy, getChannelIcon, getModelCategories, selectFilter } from '../../../../helpers';
  45. import ModelSelectModal from './ModelSelectModal';
  46. import JSONEditor from '../../../common/ui/JSONEditor';
  47. import {
  48. IconSave,
  49. IconClose,
  50. IconServer,
  51. IconSetting,
  52. IconCode,
  53. IconGlobe,
  54. IconBolt,
  55. } from '@douyinfe/semi-icons';
  56. const { Text, Title } = Typography;
  57. const MODEL_MAPPING_EXAMPLE = {
  58. 'gpt-3.5-turbo': 'gpt-3.5-turbo-0125',
  59. };
  60. const STATUS_CODE_MAPPING_EXAMPLE = {
  61. 400: '500',
  62. };
  63. const REGION_EXAMPLE = {
  64. "default": 'global',
  65. "gemini-1.5-pro-002": "europe-west2",
  66. "gemini-1.5-flash-002": "europe-west2",
  67. 'claude-3-5-sonnet-20240620': 'europe-west1',
  68. };
  69. function type2secretPrompt(type) {
  70. // inputs.type === 15 ? '按照如下格式输入:APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入:APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥')
  71. switch (type) {
  72. case 15:
  73. return '按照如下格式输入:APIKey|SecretKey';
  74. case 18:
  75. return '按照如下格式输入:APPID|APISecret|APIKey';
  76. case 22:
  77. return '按照如下格式输入:APIKey-AppId,例如:fastgpt-0sp2gtvfdgyi4k30jwlgwf1i-64f335d84283f05518e9e041';
  78. case 23:
  79. return '按照如下格式输入:AppId|SecretId|SecretKey';
  80. case 33:
  81. return '按照如下格式输入:Ak|Sk|Region';
  82. case 50:
  83. return '按照如下格式输入: AccessKey|SecretKey, 如果上游是New API,则直接输ApiKey';
  84. case 51:
  85. return '按照如下格式输入: Access Key ID|Secret Access Key';
  86. default:
  87. return '请输入渠道对应的鉴权密钥';
  88. }
  89. }
  90. const EditChannelModal = (props) => {
  91. const { t } = useTranslation();
  92. const channelId = props.editingChannel.id;
  93. const isEdit = channelId !== undefined;
  94. const [loading, setLoading] = useState(isEdit);
  95. const isMobile = useIsMobile();
  96. const handleCancel = () => {
  97. props.handleClose();
  98. };
  99. const originInputs = {
  100. name: '',
  101. type: 1,
  102. key: '',
  103. openai_organization: '',
  104. max_input_tokens: 0,
  105. base_url: '',
  106. other: '',
  107. model_mapping: '',
  108. status_code_mapping: '',
  109. models: [],
  110. auto_ban: 1,
  111. test_model: '',
  112. groups: ['default'],
  113. priority: 0,
  114. weight: 0,
  115. tag: '',
  116. multi_key_mode: 'random',
  117. // 渠道额外设置的默认值
  118. force_format: false,
  119. thinking_to_content: false,
  120. proxy: '',
  121. pass_through_body_enabled: false,
  122. system_prompt: '',
  123. system_prompt_override: false,
  124. settings: '',
  125. };
  126. const [batch, setBatch] = useState(false);
  127. const [multiToSingle, setMultiToSingle] = useState(false);
  128. const [multiKeyMode, setMultiKeyMode] = useState('random');
  129. const [autoBan, setAutoBan] = useState(true);
  130. const [inputs, setInputs] = useState(originInputs);
  131. const [originModelOptions, setOriginModelOptions] = useState([]);
  132. const [modelOptions, setModelOptions] = useState([]);
  133. const [groupOptions, setGroupOptions] = useState([]);
  134. const [basicModels, setBasicModels] = useState([]);
  135. const [fullModels, setFullModels] = useState([]);
  136. const [modelGroups, setModelGroups] = useState([]);
  137. const [customModel, setCustomModel] = useState('');
  138. const [modalImageUrl, setModalImageUrl] = useState('');
  139. const [isModalOpenurl, setIsModalOpenurl] = useState(false);
  140. const [modelModalVisible, setModelModalVisible] = useState(false);
  141. const [fetchedModels, setFetchedModels] = useState([]);
  142. const formApiRef = useRef(null);
  143. const [vertexKeys, setVertexKeys] = useState([]);
  144. const [vertexFileList, setVertexFileList] = useState([]);
  145. const vertexErroredNames = useRef(new Set()); // 避免重复报错
  146. const [isMultiKeyChannel, setIsMultiKeyChannel] = useState(false);
  147. const [channelSearchValue, setChannelSearchValue] = useState('');
  148. const [useManualInput, setUseManualInput] = useState(false); // 是否使用手动输入模式
  149. const [keyMode, setKeyMode] = useState('append'); // 密钥模式:replace(覆盖)或 append(追加)
  150. // 渠道额外设置状态
  151. const [channelSettings, setChannelSettings] = useState({
  152. force_format: false,
  153. thinking_to_content: false,
  154. proxy: '',
  155. pass_through_body_enabled: false,
  156. system_prompt: '',
  157. });
  158. const showApiConfigCard = inputs.type !== 45; // 控制是否显示 API 配置卡片(仅当渠道类型不是 豆包 时显示)
  159. const getInitValues = () => ({ ...originInputs });
  160. // 处理渠道额外设置的更新
  161. const handleChannelSettingsChange = (key, value) => {
  162. // 更新内部状态
  163. setChannelSettings(prev => ({ ...prev, [key]: value }));
  164. // 同步更新到表单字段
  165. if (formApiRef.current) {
  166. formApiRef.current.setValue(key, value);
  167. }
  168. // 同步更新inputs状态
  169. setInputs(prev => ({ ...prev, [key]: value }));
  170. // 生成setting JSON并更新
  171. const newSettings = { ...channelSettings, [key]: value };
  172. const settingsJson = JSON.stringify(newSettings);
  173. handleInputChange('setting', settingsJson);
  174. };
  175. const handleChannelOtherSettingsChange = (key, value) => {
  176. // 更新内部状态
  177. setChannelSettings(prev => ({ ...prev, [key]: value }));
  178. // 同步更新到表单字段
  179. if (formApiRef.current) {
  180. formApiRef.current.setValue(key, value);
  181. }
  182. // 同步更新inputs状态
  183. setInputs(prev => ({ ...prev, [key]: value }));
  184. // 需要更新settings,是一个json,例如{"azure_responses_version": "preview"}
  185. let settings = {};
  186. if (inputs.settings) {
  187. try {
  188. settings = JSON.parse(inputs.settings);
  189. } catch (error) {
  190. console.error('解析设置失败:', error);
  191. }
  192. }
  193. settings[key] = value;
  194. const settingsJson = JSON.stringify(settings);
  195. handleInputChange('settings', settingsJson);
  196. }
  197. const handleInputChange = (name, value) => {
  198. if (formApiRef.current) {
  199. formApiRef.current.setValue(name, value);
  200. }
  201. if (name === 'models' && Array.isArray(value)) {
  202. value = Array.from(new Set(value.map((m) => (m || '').trim())));
  203. }
  204. if (name === 'base_url' && value.endsWith('/v1')) {
  205. Modal.confirm({
  206. title: '警告',
  207. content:
  208. '不需要在末尾加/v1,New API会自动处理,添加后可能导致请求失败,是否继续?',
  209. onOk: () => {
  210. setInputs((inputs) => ({ ...inputs, [name]: value }));
  211. },
  212. });
  213. return;
  214. }
  215. setInputs((inputs) => ({ ...inputs, [name]: value }));
  216. if (name === 'type') {
  217. let localModels = [];
  218. switch (value) {
  219. case 2:
  220. localModels = [
  221. 'mj_imagine',
  222. 'mj_variation',
  223. 'mj_reroll',
  224. 'mj_blend',
  225. 'mj_upscale',
  226. 'mj_describe',
  227. 'mj_uploads',
  228. ];
  229. break;
  230. case 5:
  231. localModels = [
  232. 'swap_face',
  233. 'mj_imagine',
  234. 'mj_video',
  235. 'mj_edits',
  236. 'mj_variation',
  237. 'mj_reroll',
  238. 'mj_blend',
  239. 'mj_upscale',
  240. 'mj_describe',
  241. 'mj_zoom',
  242. 'mj_shorten',
  243. 'mj_modal',
  244. 'mj_inpaint',
  245. 'mj_custom_zoom',
  246. 'mj_high_variation',
  247. 'mj_low_variation',
  248. 'mj_pan',
  249. 'mj_uploads',
  250. ];
  251. break;
  252. case 36:
  253. localModels = ['suno_music', 'suno_lyrics'];
  254. break;
  255. default:
  256. localModels = getChannelModels(value);
  257. break;
  258. }
  259. if (inputs.models.length === 0) {
  260. setInputs((inputs) => ({ ...inputs, models: localModels }));
  261. }
  262. setBasicModels(localModels);
  263. // 重置手动输入模式状态
  264. setUseManualInput(false);
  265. }
  266. //setAutoBan
  267. };
  268. const loadChannel = async () => {
  269. setLoading(true);
  270. let res = await API.get(`/api/channel/${channelId}`);
  271. if (res === undefined) {
  272. return;
  273. }
  274. const { success, message, data } = res.data;
  275. if (success) {
  276. if (data.models === '') {
  277. data.models = [];
  278. } else {
  279. data.models = data.models.split(',');
  280. }
  281. if (data.group === '') {
  282. data.groups = [];
  283. } else {
  284. data.groups = data.group.split(',');
  285. }
  286. if (data.model_mapping !== '') {
  287. data.model_mapping = JSON.stringify(
  288. JSON.parse(data.model_mapping),
  289. null,
  290. 2,
  291. );
  292. }
  293. const chInfo = data.channel_info || {};
  294. const isMulti = chInfo.is_multi_key === true;
  295. setIsMultiKeyChannel(isMulti);
  296. if (isMulti) {
  297. setBatch(true);
  298. setMultiToSingle(true);
  299. const modeVal = chInfo.multi_key_mode || 'random';
  300. setMultiKeyMode(modeVal);
  301. data.multi_key_mode = modeVal;
  302. } else {
  303. setBatch(false);
  304. setMultiToSingle(false);
  305. }
  306. // 解析渠道额外设置并合并到data中
  307. if (data.setting) {
  308. try {
  309. const parsedSettings = JSON.parse(data.setting);
  310. data.force_format = parsedSettings.force_format || false;
  311. data.thinking_to_content = parsedSettings.thinking_to_content || false;
  312. data.proxy = parsedSettings.proxy || '';
  313. data.pass_through_body_enabled = parsedSettings.pass_through_body_enabled || false;
  314. data.system_prompt = parsedSettings.system_prompt || '';
  315. data.system_prompt_override = parsedSettings.system_prompt_override || false;
  316. } catch (error) {
  317. console.error('解析渠道设置失败:', error);
  318. data.force_format = false;
  319. data.thinking_to_content = false;
  320. data.proxy = '';
  321. data.pass_through_body_enabled = false;
  322. data.system_prompt = '';
  323. data.system_prompt_override = false;
  324. }
  325. } else {
  326. data.force_format = false;
  327. data.thinking_to_content = false;
  328. data.proxy = '';
  329. data.pass_through_body_enabled = false;
  330. data.system_prompt = '';
  331. data.system_prompt_override = false;
  332. }
  333. if (data.settings) {
  334. try {
  335. const parsedSettings = JSON.parse(data.settings);
  336. data.azure_responses_version = parsedSettings.azure_responses_version || '';
  337. } catch (error) {
  338. console.error('解析其他设置失败:', error);
  339. data.azure_responses_version = '';
  340. data.region = '';
  341. }
  342. }
  343. setInputs(data);
  344. if (formApiRef.current) {
  345. formApiRef.current.setValues(data);
  346. }
  347. if (data.auto_ban === 0) {
  348. setAutoBan(false);
  349. } else {
  350. setAutoBan(true);
  351. }
  352. setBasicModels(getChannelModels(data.type));
  353. // 同步更新channelSettings状态显示
  354. setChannelSettings({
  355. force_format: data.force_format,
  356. thinking_to_content: data.thinking_to_content,
  357. proxy: data.proxy,
  358. pass_through_body_enabled: data.pass_through_body_enabled,
  359. system_prompt: data.system_prompt,
  360. system_prompt_override: data.system_prompt_override || false,
  361. });
  362. // console.log(data);
  363. } else {
  364. showError(message);
  365. }
  366. setLoading(false);
  367. };
  368. const fetchUpstreamModelList = async (name) => {
  369. // if (inputs['type'] !== 1) {
  370. // showError(t('仅支持 OpenAI 接口格式'));
  371. // return;
  372. // }
  373. setLoading(true);
  374. const models = [];
  375. let err = false;
  376. if (isEdit) {
  377. // 如果是编辑模式,使用已有的 channelId 获取模型列表
  378. const res = await API.get('/api/channel/fetch_models/' + channelId, { skipErrorHandler: true });
  379. if (res && res.data && res.data.success) {
  380. models.push(...res.data.data);
  381. } else {
  382. err = true;
  383. }
  384. } else {
  385. // 如果是新建模式,通过后端代理获取模型列表
  386. if (!inputs?.['key']) {
  387. showError(t('请填写密钥'));
  388. err = true;
  389. } else {
  390. try {
  391. const res = await API.post(
  392. '/api/channel/fetch_models',
  393. {
  394. base_url: inputs['base_url'],
  395. type: inputs['type'],
  396. key: inputs['key'],
  397. },
  398. { skipErrorHandler: true },
  399. );
  400. if (res && res.data && res.data.success) {
  401. models.push(...res.data.data);
  402. } else {
  403. err = true;
  404. }
  405. } catch (error) {
  406. console.error('Error fetching models:', error);
  407. err = true;
  408. }
  409. }
  410. }
  411. if (!err) {
  412. const uniqueModels = Array.from(new Set(models));
  413. setFetchedModels(uniqueModels);
  414. setModelModalVisible(true);
  415. } else {
  416. showError(t('获取模型列表失败'));
  417. }
  418. setLoading(false);
  419. };
  420. const fetchModels = async () => {
  421. try {
  422. let res = await API.get(`/api/channel/models`);
  423. const localModelOptions = res.data.data.map((model) => {
  424. const id = (model.id || '').trim();
  425. return {
  426. key: id,
  427. label: id,
  428. value: id,
  429. };
  430. });
  431. setOriginModelOptions(localModelOptions);
  432. setFullModels(res.data.data.map((model) => model.id));
  433. setBasicModels(
  434. res.data.data
  435. .filter((model) => {
  436. return model.id.startsWith('gpt-') || model.id.startsWith('text-');
  437. })
  438. .map((model) => model.id),
  439. );
  440. } catch (error) {
  441. showError(error.message);
  442. }
  443. };
  444. const fetchGroups = async () => {
  445. try {
  446. let res = await API.get(`/api/group/`);
  447. if (res === undefined) {
  448. return;
  449. }
  450. setGroupOptions(
  451. res.data.data.map((group) => ({
  452. label: group,
  453. value: group,
  454. })),
  455. );
  456. } catch (error) {
  457. showError(error.message);
  458. }
  459. };
  460. const fetchModelGroups = async () => {
  461. try {
  462. const res = await API.get('/api/prefill_group?type=model');
  463. if (res?.data?.success) {
  464. setModelGroups(res.data.data || []);
  465. }
  466. } catch (error) {
  467. // ignore
  468. }
  469. };
  470. useEffect(() => {
  471. const modelMap = new Map();
  472. originModelOptions.forEach((option) => {
  473. const v = (option.value || '').trim();
  474. if (!modelMap.has(v)) {
  475. modelMap.set(v, option);
  476. }
  477. });
  478. inputs.models.forEach((model) => {
  479. const v = (model || '').trim();
  480. if (!modelMap.has(v)) {
  481. modelMap.set(v, {
  482. key: v,
  483. label: v,
  484. value: v,
  485. });
  486. }
  487. });
  488. const categories = getModelCategories(t);
  489. const optionsWithIcon = Array.from(modelMap.values()).map((opt) => {
  490. const modelName = opt.value;
  491. let icon = null;
  492. for (const [key, category] of Object.entries(categories)) {
  493. if (key !== 'all' && category.filter({ model_name: modelName })) {
  494. icon = category.icon;
  495. break;
  496. }
  497. }
  498. return {
  499. ...opt,
  500. label: (
  501. <span className="flex items-center gap-1">
  502. {icon}
  503. {modelName}
  504. </span>
  505. ),
  506. };
  507. });
  508. setModelOptions(optionsWithIcon);
  509. }, [originModelOptions, inputs.models, t]);
  510. useEffect(() => {
  511. fetchModels().then();
  512. fetchGroups().then();
  513. if (!isEdit) {
  514. setInputs(originInputs);
  515. if (formApiRef.current) {
  516. formApiRef.current.setValues(originInputs);
  517. }
  518. let localModels = getChannelModels(inputs.type);
  519. setBasicModels(localModels);
  520. setInputs((inputs) => ({ ...inputs, models: localModels }));
  521. }
  522. }, [props.editingChannel.id]);
  523. useEffect(() => {
  524. if (formApiRef.current) {
  525. formApiRef.current.setValues(inputs);
  526. }
  527. }, [inputs]);
  528. useEffect(() => {
  529. if (props.visible) {
  530. if (isEdit) {
  531. loadChannel();
  532. } else {
  533. formApiRef.current?.setValues(getInitValues());
  534. }
  535. fetchModelGroups();
  536. // 重置手动输入模式状态
  537. setUseManualInput(false);
  538. } else {
  539. formApiRef.current?.reset();
  540. // 重置渠道设置状态
  541. setChannelSettings({
  542. force_format: false,
  543. thinking_to_content: false,
  544. proxy: '',
  545. pass_through_body_enabled: false,
  546. system_prompt: '',
  547. system_prompt_override: false,
  548. });
  549. // 重置密钥模式状态
  550. setKeyMode('append');
  551. // 清空表单中的key_mode字段
  552. if (formApiRef.current) {
  553. formApiRef.current.setValue('key_mode', undefined);
  554. }
  555. // 重置本地输入,避免下次打开残留上一次的 JSON 字段值
  556. setInputs(getInitValues());
  557. }
  558. }, [props.visible, channelId]);
  559. const handleVertexUploadChange = ({ fileList }) => {
  560. vertexErroredNames.current.clear();
  561. (async () => {
  562. let validFiles = [];
  563. let keys = [];
  564. const errorNames = [];
  565. for (const item of fileList) {
  566. const fileObj = item.fileInstance;
  567. if (!fileObj) continue;
  568. try {
  569. const txt = await fileObj.text();
  570. keys.push(JSON.parse(txt));
  571. validFiles.push(item);
  572. } catch (err) {
  573. if (!vertexErroredNames.current.has(item.name)) {
  574. errorNames.push(item.name);
  575. vertexErroredNames.current.add(item.name);
  576. }
  577. }
  578. }
  579. // 非批量模式下只保留一个文件(最新选择的),避免重复叠加
  580. if (!batch && validFiles.length > 1) {
  581. validFiles = [validFiles[validFiles.length - 1]];
  582. keys = [keys[keys.length - 1]];
  583. }
  584. setVertexKeys(keys);
  585. setVertexFileList(validFiles);
  586. if (formApiRef.current) {
  587. formApiRef.current.setValue('vertex_files', validFiles);
  588. }
  589. setInputs((prev) => ({ ...prev, vertex_files: validFiles }));
  590. if (errorNames.length > 0) {
  591. showError(t('以下文件解析失败,已忽略:{{list}}', { list: errorNames.join(', ') }));
  592. }
  593. })();
  594. };
  595. const submit = async () => {
  596. const formValues = formApiRef.current ? formApiRef.current.getValues() : {};
  597. let localInputs = { ...formValues };
  598. if (localInputs.type === 41) {
  599. if (useManualInput) {
  600. // 手动输入模式
  601. if (localInputs.key && localInputs.key.trim() !== '') {
  602. try {
  603. // 验证 JSON 格式
  604. const parsedKey = JSON.parse(localInputs.key);
  605. // 确保是有效的密钥格式
  606. localInputs.key = JSON.stringify(parsedKey);
  607. } catch (err) {
  608. showError(t('密钥格式无效,请输入有效的 JSON 格式密钥'));
  609. return;
  610. }
  611. } else if (!isEdit) {
  612. showInfo(t('请输入密钥!'));
  613. return;
  614. }
  615. } else {
  616. // 文件上传模式
  617. let keys = vertexKeys;
  618. // 若当前未选择文件,尝试从已上传文件列表解析(异步读取)
  619. if (keys.length === 0 && vertexFileList.length > 0) {
  620. try {
  621. const parsed = await Promise.all(
  622. vertexFileList.map(async (item) => {
  623. const fileObj = item.fileInstance;
  624. if (!fileObj) return null;
  625. const txt = await fileObj.text();
  626. return JSON.parse(txt);
  627. })
  628. );
  629. keys = parsed.filter(Boolean);
  630. } catch (err) {
  631. showError(t('解析密钥文件失败: {{msg}}', { msg: err.message }));
  632. return;
  633. }
  634. }
  635. // 创建模式必须上传密钥;编辑模式可选
  636. if (keys.length === 0) {
  637. if (!isEdit) {
  638. showInfo(t('请上传密钥文件!'));
  639. return;
  640. } else {
  641. // 编辑模式且未上传新密钥,不修改 key
  642. delete localInputs.key;
  643. }
  644. } else {
  645. // 有新密钥,则覆盖
  646. if (batch) {
  647. localInputs.key = JSON.stringify(keys);
  648. } else {
  649. localInputs.key = JSON.stringify(keys[0]);
  650. }
  651. }
  652. }
  653. }
  654. // 如果是编辑模式且 key 为空字符串,避免提交空值覆盖旧密钥
  655. if (isEdit && (!localInputs.key || localInputs.key.trim() === '')) {
  656. delete localInputs.key;
  657. }
  658. delete localInputs.vertex_files;
  659. if (!isEdit && (!localInputs.name || !localInputs.key)) {
  660. showInfo(t('请填写渠道名称和渠道密钥!'));
  661. return;
  662. }
  663. if (!Array.isArray(localInputs.models) || localInputs.models.length === 0) {
  664. showInfo(t('请至少选择一个模型!'));
  665. return;
  666. }
  667. if (localInputs.model_mapping && localInputs.model_mapping !== '' && !verifyJSON(localInputs.model_mapping)) {
  668. showInfo(t('模型映射必须是合法的 JSON 格式!'));
  669. return;
  670. }
  671. if (localInputs.base_url && localInputs.base_url.endsWith('/')) {
  672. localInputs.base_url = localInputs.base_url.slice(
  673. 0,
  674. localInputs.base_url.length - 1,
  675. );
  676. }
  677. if (localInputs.type === 18 && localInputs.other === '') {
  678. localInputs.other = 'v2.1';
  679. }
  680. // 生成渠道额外设置JSON
  681. const channelExtraSettings = {
  682. force_format: localInputs.force_format || false,
  683. thinking_to_content: localInputs.thinking_to_content || false,
  684. proxy: localInputs.proxy || '',
  685. pass_through_body_enabled: localInputs.pass_through_body_enabled || false,
  686. system_prompt: localInputs.system_prompt || '',
  687. system_prompt_override: localInputs.system_prompt_override || false,
  688. };
  689. localInputs.setting = JSON.stringify(channelExtraSettings);
  690. // 清理不需要发送到后端的字段
  691. delete localInputs.force_format;
  692. delete localInputs.thinking_to_content;
  693. delete localInputs.proxy;
  694. delete localInputs.pass_through_body_enabled;
  695. delete localInputs.system_prompt;
  696. delete localInputs.system_prompt_override;
  697. let res;
  698. localInputs.auto_ban = localInputs.auto_ban ? 1 : 0;
  699. localInputs.models = localInputs.models.join(',');
  700. localInputs.group = (localInputs.groups || []).join(',');
  701. let mode = 'single';
  702. if (batch) {
  703. mode = multiToSingle ? 'multi_to_single' : 'batch';
  704. }
  705. if (isEdit) {
  706. res = await API.put(`/api/channel/`, {
  707. ...localInputs,
  708. id: parseInt(channelId),
  709. key_mode: isMultiKeyChannel ? keyMode : undefined, // 只在多key模式下传递
  710. });
  711. } else {
  712. res = await API.post(`/api/channel/`, {
  713. mode: mode,
  714. multi_key_mode: mode === 'multi_to_single' ? multiKeyMode : undefined,
  715. channel: localInputs,
  716. });
  717. }
  718. const { success, message } = res.data;
  719. if (success) {
  720. if (isEdit) {
  721. showSuccess(t('渠道更新成功!'));
  722. } else {
  723. showSuccess(t('渠道创建成功!'));
  724. setInputs(originInputs);
  725. }
  726. props.refresh();
  727. props.handleClose();
  728. } else {
  729. showError(message);
  730. }
  731. };
  732. const addCustomModels = () => {
  733. if (customModel.trim() === '') return;
  734. const modelArray = customModel.split(',').map((model) => model.trim());
  735. let localModels = [...inputs.models];
  736. let localModelOptions = [...modelOptions];
  737. const addedModels = [];
  738. modelArray.forEach((model) => {
  739. if (model && !localModels.includes(model)) {
  740. localModels.push(model);
  741. localModelOptions.push({
  742. key: model,
  743. label: model,
  744. value: model,
  745. });
  746. addedModels.push(model);
  747. }
  748. });
  749. setModelOptions(localModelOptions);
  750. setCustomModel('');
  751. handleInputChange('models', localModels);
  752. if (addedModels.length > 0) {
  753. showSuccess(
  754. t('已新增 {{count}} 个模型:{{list}}', {
  755. count: addedModels.length,
  756. list: addedModels.join(', '),
  757. })
  758. );
  759. } else {
  760. showInfo(t('未发现新增模型'));
  761. }
  762. };
  763. const batchAllowed = !isEdit || isMultiKeyChannel;
  764. const batchExtra = batchAllowed ? (
  765. <Space>
  766. {!isEdit && (
  767. <Checkbox
  768. disabled={isEdit}
  769. checked={batch}
  770. onChange={(e) => {
  771. const checked = e.target.checked;
  772. if (!checked && vertexFileList.length > 1) {
  773. Modal.confirm({
  774. title: t('切换为单密钥模式'),
  775. content: t('将仅保留第一个密钥文件,其余文件将被移除,是否继续?'),
  776. onOk: () => {
  777. const firstFile = vertexFileList[0];
  778. const firstKey = vertexKeys[0] ? [vertexKeys[0]] : [];
  779. setVertexFileList([firstFile]);
  780. setVertexKeys(firstKey);
  781. formApiRef.current?.setValue('vertex_files', [firstFile]);
  782. setInputs((prev) => ({ ...prev, vertex_files: [firstFile] }));
  783. setBatch(false);
  784. setMultiToSingle(false);
  785. setMultiKeyMode('random');
  786. },
  787. onCancel: () => {
  788. setBatch(true);
  789. },
  790. centered: true,
  791. });
  792. return;
  793. }
  794. setBatch(checked);
  795. if (!checked) {
  796. setMultiToSingle(false);
  797. setMultiKeyMode('random');
  798. } else {
  799. // 批量模式下禁用手动输入,并清空手动输入的内容
  800. setUseManualInput(false);
  801. if (inputs.type === 41) {
  802. // 清空手动输入的密钥内容
  803. if (formApiRef.current) {
  804. formApiRef.current.setValue('key', '');
  805. }
  806. handleInputChange('key', '');
  807. }
  808. }
  809. }}
  810. >
  811. {t('批量创建')}
  812. </Checkbox>
  813. )}
  814. {batch && (
  815. <Checkbox disabled={isEdit} checked={multiToSingle} onChange={() => {
  816. setMultiToSingle(prev => !prev);
  817. setInputs(prev => {
  818. const newInputs = { ...prev };
  819. if (!multiToSingle) {
  820. newInputs.multi_key_mode = multiKeyMode;
  821. } else {
  822. delete newInputs.multi_key_mode;
  823. }
  824. return newInputs;
  825. });
  826. }}>{t('密钥聚合模式')}</Checkbox>
  827. )}
  828. </Space>
  829. ) : null;
  830. const channelOptionList = useMemo(
  831. () =>
  832. CHANNEL_OPTIONS.map((opt) => ({
  833. ...opt,
  834. // 保持 label 为纯文本以支持搜索
  835. label: opt.label,
  836. })),
  837. [],
  838. );
  839. const renderChannelOption = (renderProps) => {
  840. const {
  841. disabled,
  842. selected,
  843. label,
  844. value,
  845. focused,
  846. className,
  847. style,
  848. onMouseEnter,
  849. onClick,
  850. ...rest
  851. } = renderProps;
  852. const searchWords = channelSearchValue ? [channelSearchValue] : [];
  853. // 构建样式类名
  854. const optionClassName = [
  855. 'flex items-center gap-3 px-3 py-2 transition-all duration-200 rounded-lg mx-2 my-1',
  856. focused && 'bg-blue-50 shadow-sm',
  857. selected && 'bg-blue-100 text-blue-700 shadow-lg ring-2 ring-blue-200 ring-opacity-50',
  858. disabled && 'opacity-50 cursor-not-allowed',
  859. !disabled && 'hover:bg-gray-50 hover:shadow-md cursor-pointer',
  860. className
  861. ].filter(Boolean).join(' ');
  862. return (
  863. <div
  864. style={style}
  865. className={optionClassName}
  866. onClick={() => !disabled && onClick()}
  867. onMouseEnter={e => onMouseEnter()}
  868. >
  869. <div className="flex items-center gap-3 w-full">
  870. <div className="flex-shrink-0 w-5 h-5 flex items-center justify-center">
  871. {getChannelIcon(value)}
  872. </div>
  873. <div className="flex-1 min-w-0">
  874. <Highlight
  875. sourceString={label}
  876. searchWords={searchWords}
  877. className="text-sm font-medium truncate"
  878. />
  879. </div>
  880. {selected && (
  881. <div className="flex-shrink-0 text-blue-600">
  882. <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
  883. <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" />
  884. </svg>
  885. </div>
  886. )}
  887. </div>
  888. </div>
  889. );
  890. };
  891. return (
  892. <>
  893. <SideSheet
  894. placement={isEdit ? 'right' : 'left'}
  895. title={
  896. <Space>
  897. <Tag color="blue" shape="circle">{isEdit ? t('编辑') : t('新建')}</Tag>
  898. <Title heading={4} className="m-0">
  899. {isEdit ? t('更新渠道信息') : t('创建新的渠道')}
  900. </Title>
  901. </Space>
  902. }
  903. bodyStyle={{ padding: '0' }}
  904. visible={props.visible}
  905. width={isMobile ? '100%' : 600}
  906. footer={
  907. <div className="flex justify-end bg-white">
  908. <Space>
  909. <Button
  910. theme="solid"
  911. onClick={() => formApiRef.current?.submitForm()}
  912. icon={<IconSave />}
  913. >
  914. {t('提交')}
  915. </Button>
  916. <Button
  917. theme="light"
  918. type="primary"
  919. onClick={handleCancel}
  920. icon={<IconClose />}
  921. >
  922. {t('取消')}
  923. </Button>
  924. </Space>
  925. </div>
  926. }
  927. closeIcon={null}
  928. onCancel={() => handleCancel()}
  929. >
  930. <Form
  931. key={isEdit ? 'edit' : 'new'}
  932. initValues={originInputs}
  933. getFormApi={(api) => (formApiRef.current = api)}
  934. onSubmit={submit}
  935. >
  936. {() => (
  937. <Spin spinning={loading}>
  938. <div className="p-2">
  939. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  940. {/* Header: Basic Info */}
  941. <div className="flex items-center mb-2">
  942. <Avatar size="small" color="blue" className="mr-2 shadow-md">
  943. <IconServer size={16} />
  944. </Avatar>
  945. <div>
  946. <Text className="text-lg font-medium">{t('基本信息')}</Text>
  947. <div className="text-xs text-gray-600">{t('渠道的基本配置信息')}</div>
  948. </div>
  949. </div>
  950. <Form.Select
  951. field='type'
  952. label={t('类型')}
  953. placeholder={t('请选择渠道类型')}
  954. rules={[{ required: true, message: t('请选择渠道类型') }]}
  955. optionList={channelOptionList}
  956. style={{ width: '100%' }}
  957. filter={selectFilter}
  958. autoClearSearchValue={false}
  959. searchPosition='dropdown'
  960. onSearch={(value) => setChannelSearchValue(value)}
  961. renderOptionItem={renderChannelOption}
  962. onChange={(value) => handleInputChange('type', value)}
  963. />
  964. <Form.Input
  965. field='name'
  966. label={t('名称')}
  967. placeholder={t('请为渠道命名')}
  968. rules={[{ required: true, message: t('请为渠道命名') }]}
  969. showClear
  970. onChange={(value) => handleInputChange('name', value)}
  971. autoComplete='new-password'
  972. />
  973. {batch ? (
  974. inputs.type === 41 ? (
  975. <Form.Upload
  976. field='vertex_files'
  977. label={t('密钥文件 (.json)')}
  978. accept='.json'
  979. multiple
  980. draggable
  981. dragIcon={<IconBolt />}
  982. dragMainText={t('点击上传文件或拖拽文件到这里')}
  983. dragSubText={t('仅支持 JSON 文件,支持多文件')}
  984. style={{ marginTop: 10 }}
  985. uploadTrigger='custom'
  986. beforeUpload={() => false}
  987. onChange={handleVertexUploadChange}
  988. fileList={vertexFileList}
  989. rules={isEdit ? [] : [{ required: true, message: t('请上传密钥文件') }]}
  990. extraText={batchExtra}
  991. />
  992. ) : (
  993. <Form.TextArea
  994. field='key'
  995. label={t('密钥')}
  996. placeholder={t('请输入密钥,一行一个')}
  997. rules={isEdit ? [] : [{ required: true, message: t('请输入密钥') }]}
  998. autosize
  999. autoComplete='new-password'
  1000. onChange={(value) => handleInputChange('key', value)}
  1001. extraText={
  1002. <div className="flex items-center gap-2">
  1003. {isEdit && isMultiKeyChannel && keyMode === 'append' && (
  1004. <Text type="warning" size="small">
  1005. {t('追加模式:新密钥将添加到现有密钥列表的末尾')}
  1006. </Text>
  1007. )}
  1008. {batchExtra}
  1009. </div>
  1010. }
  1011. showClear
  1012. />
  1013. )
  1014. ) : (
  1015. <>
  1016. {inputs.type === 41 ? (
  1017. <>
  1018. {!batch && (
  1019. <div className="flex items-center justify-between mb-3">
  1020. <Text className="text-sm font-medium">{t('密钥输入方式')}</Text>
  1021. <Space>
  1022. <Button
  1023. size="small"
  1024. type={!useManualInput ? 'primary' : 'tertiary'}
  1025. onClick={() => {
  1026. setUseManualInput(false);
  1027. // 切换到文件上传模式时清空手动输入的密钥
  1028. if (formApiRef.current) {
  1029. formApiRef.current.setValue('key', '');
  1030. }
  1031. handleInputChange('key', '');
  1032. }}
  1033. >
  1034. {t('文件上传')}
  1035. </Button>
  1036. <Button
  1037. size="small"
  1038. type={useManualInput ? 'primary' : 'tertiary'}
  1039. onClick={() => {
  1040. setUseManualInput(true);
  1041. // 切换到手动输入模式时清空文件上传相关状态
  1042. setVertexKeys([]);
  1043. setVertexFileList([]);
  1044. if (formApiRef.current) {
  1045. formApiRef.current.setValue('vertex_files', []);
  1046. }
  1047. setInputs((prev) => ({ ...prev, vertex_files: [] }));
  1048. }}
  1049. >
  1050. {t('手动输入')}
  1051. </Button>
  1052. </Space>
  1053. </div>
  1054. )}
  1055. {batch && (
  1056. <Banner
  1057. type='info'
  1058. description={t('批量创建模式下仅支持文件上传,不支持手动输入')}
  1059. className='!rounded-lg mb-3'
  1060. />
  1061. )}
  1062. {useManualInput && !batch ? (
  1063. <Form.TextArea
  1064. field='key'
  1065. label={isEdit ? t('密钥(编辑模式下,保存的密钥不会显示)') : t('密钥')}
  1066. 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}')}
  1067. rules={isEdit ? [] : [{ required: true, message: t('请输入密钥') }]}
  1068. autoComplete='new-password'
  1069. onChange={(value) => handleInputChange('key', value)}
  1070. extraText={
  1071. <div className="flex items-center gap-2">
  1072. <Text type="tertiary" size="small">
  1073. {t('请输入完整的 JSON 格式密钥内容')}
  1074. </Text>
  1075. {isEdit && isMultiKeyChannel && keyMode === 'append' && (
  1076. <Text type="warning" size="small">
  1077. {t('追加模式:新密钥将添加到现有密钥列表的末尾')}
  1078. </Text>
  1079. )}
  1080. {batchExtra}
  1081. </div>
  1082. }
  1083. autosize
  1084. showClear
  1085. />
  1086. ) : (
  1087. <Form.Upload
  1088. field='vertex_files'
  1089. label={t('密钥文件 (.json)')}
  1090. accept='.json'
  1091. draggable
  1092. dragIcon={<IconBolt />}
  1093. dragMainText={t('点击上传文件或拖拽文件到这里')}
  1094. dragSubText={t('仅支持 JSON 文件')}
  1095. style={{ marginTop: 10 }}
  1096. uploadTrigger='custom'
  1097. beforeUpload={() => false}
  1098. onChange={handleVertexUploadChange}
  1099. fileList={vertexFileList}
  1100. rules={isEdit ? [] : [{ required: true, message: t('请上传密钥文件') }]}
  1101. extraText={batchExtra}
  1102. />
  1103. )}
  1104. </>
  1105. ) : (
  1106. <Form.Input
  1107. field='key'
  1108. label={isEdit ? t('密钥(编辑模式下,保存的密钥不会显示)') : t('密钥')}
  1109. placeholder={t(type2secretPrompt(inputs.type))}
  1110. rules={isEdit ? [] : [{ required: true, message: t('请输入密钥') }]}
  1111. autoComplete='new-password'
  1112. onChange={(value) => handleInputChange('key', value)}
  1113. extraText={
  1114. <div className="flex items-center gap-2">
  1115. {isEdit && isMultiKeyChannel && keyMode === 'append' && (
  1116. <Text type="warning" size="small">
  1117. {t('追加模式:新密钥将添加到现有密钥列表的末尾')}
  1118. </Text>
  1119. )}
  1120. {batchExtra}
  1121. </div>
  1122. }
  1123. showClear
  1124. />
  1125. )}
  1126. </>
  1127. )}
  1128. {isEdit && isMultiKeyChannel && (
  1129. <Form.Select
  1130. field='key_mode'
  1131. label={t('密钥更新模式')}
  1132. placeholder={t('请选择密钥更新模式')}
  1133. optionList={[
  1134. { label: t('追加到现有密钥'), value: 'append' },
  1135. { label: t('覆盖现有密钥'), value: 'replace' },
  1136. ]}
  1137. style={{ width: '100%' }}
  1138. value={keyMode}
  1139. onChange={(value) => setKeyMode(value)}
  1140. extraText={
  1141. <Text type="tertiary" size="small">
  1142. {keyMode === 'replace'
  1143. ? t('覆盖模式:将完全替换现有的所有密钥')
  1144. : t('追加模式:将新密钥添加到现有密钥列表末尾')
  1145. }
  1146. </Text>
  1147. }
  1148. />
  1149. )}
  1150. {batch && multiToSingle && (
  1151. <>
  1152. <Form.Select
  1153. field='multi_key_mode'
  1154. label={t('密钥聚合模式')}
  1155. placeholder={t('请选择多密钥使用策略')}
  1156. optionList={[
  1157. { label: t('随机'), value: 'random' },
  1158. { label: t('轮询'), value: 'polling' },
  1159. ]}
  1160. style={{ width: '100%' }}
  1161. value={inputs.multi_key_mode || 'random'}
  1162. onChange={(value) => {
  1163. setMultiKeyMode(value);
  1164. handleInputChange('multi_key_mode', value);
  1165. }}
  1166. />
  1167. {inputs.multi_key_mode === 'polling' && (
  1168. <Banner
  1169. type='warning'
  1170. description={t('轮询模式必须搭配Redis和内存缓存功能使用,否则性能将大幅降低,并且无法实现轮询功能')}
  1171. className='!rounded-lg mt-2'
  1172. />
  1173. )}
  1174. </>
  1175. )}
  1176. {inputs.type === 18 && (
  1177. <Form.Input
  1178. field='other'
  1179. label={t('模型版本')}
  1180. placeholder={'请输入星火大模型版本,注意是接口地址中的版本号,例如:v2.1'}
  1181. onChange={(value) => handleInputChange('other', value)}
  1182. showClear
  1183. />
  1184. )}
  1185. {inputs.type === 41 && (
  1186. <JSONEditor
  1187. key={`region-${isEdit ? channelId : 'new'}`}
  1188. field='other'
  1189. label={t('部署地区')}
  1190. placeholder={t(
  1191. '请输入部署地区,例如:us-central1\n支持使用模型映射格式\n{\n "default": "us-central1",\n "claude-3-5-sonnet-20240620": "europe-west1"\n}'
  1192. )}
  1193. value={inputs.other || ''}
  1194. onChange={(value) => handleInputChange('other', value)}
  1195. rules={[{ required: true, message: t('请填写部署地区') }]}
  1196. template={REGION_EXAMPLE}
  1197. templateLabel={t('填入模板')}
  1198. editorType="region"
  1199. formApi={formApiRef.current}
  1200. extraText={t('设置默认地区和特定模型的专用地区')}
  1201. />
  1202. )}
  1203. {inputs.type === 21 && (
  1204. <Form.Input
  1205. field='other'
  1206. label={t('知识库 ID')}
  1207. placeholder={'请输入知识库 ID,例如:123456'}
  1208. onChange={(value) => handleInputChange('other', value)}
  1209. showClear
  1210. />
  1211. )}
  1212. {inputs.type === 39 && (
  1213. <Form.Input
  1214. field='other'
  1215. label='Account ID'
  1216. placeholder={'请输入Account ID,例如:d6b5da8hk1awo8nap34ube6gh'}
  1217. onChange={(value) => handleInputChange('other', value)}
  1218. showClear
  1219. />
  1220. )}
  1221. {inputs.type === 49 && (
  1222. <Form.Input
  1223. field='other'
  1224. label={t('智能体ID')}
  1225. placeholder={'请输入智能体ID,例如:7342866812345'}
  1226. onChange={(value) => handleInputChange('other', value)}
  1227. showClear
  1228. />
  1229. )}
  1230. {inputs.type === 1 && (
  1231. <Form.Input
  1232. field='openai_organization'
  1233. label={t('组织')}
  1234. placeholder={t('请输入组织org-xxx')}
  1235. showClear
  1236. helpText={t('组织,不填则为默认组织')}
  1237. onChange={(value) => handleInputChange('openai_organization', value)}
  1238. />
  1239. )}
  1240. </Card>
  1241. {/* API Configuration Card */}
  1242. {showApiConfigCard && (
  1243. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  1244. {/* Header: API Config */}
  1245. <div className="flex items-center mb-2">
  1246. <Avatar size="small" color="green" className="mr-2 shadow-md">
  1247. <IconGlobe size={16} />
  1248. </Avatar>
  1249. <div>
  1250. <Text className="text-lg font-medium">{t('API 配置')}</Text>
  1251. <div className="text-xs text-gray-600">{t('API 地址和相关配置')}</div>
  1252. </div>
  1253. </div>
  1254. {inputs.type === 40 && (
  1255. <Banner
  1256. type='info'
  1257. description={
  1258. <div>
  1259. <Text strong>{t('邀请链接')}:</Text>
  1260. <Text
  1261. link
  1262. underline
  1263. className="ml-2 cursor-pointer"
  1264. onClick={() => window.open('https://cloud.siliconflow.cn/i/hij0YNTZ')}
  1265. >
  1266. https://cloud.siliconflow.cn/i/hij0YNTZ
  1267. </Text>
  1268. </div>
  1269. }
  1270. className='!rounded-lg'
  1271. />
  1272. )}
  1273. {inputs.type === 3 && (
  1274. <>
  1275. <Banner
  1276. type='warning'
  1277. description={t('2025年5月10日后添加的渠道,不需要再在部署的时候移除模型名称中的"."')}
  1278. className='!rounded-lg'
  1279. />
  1280. <div>
  1281. <Form.Input
  1282. field='base_url'
  1283. label='AZURE_OPENAI_ENDPOINT'
  1284. placeholder={t('请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com')}
  1285. onChange={(value) => handleInputChange('base_url', value)}
  1286. showClear
  1287. />
  1288. </div>
  1289. <div>
  1290. <Form.Input
  1291. field='other'
  1292. label={t('默认 API 版本')}
  1293. placeholder={t('请输入默认 API 版本,例如:2025-04-01-preview')}
  1294. onChange={(value) => handleInputChange('other', value)}
  1295. showClear
  1296. />
  1297. </div>
  1298. <div>
  1299. <Form.Input
  1300. field='azure_responses_version'
  1301. label={t('默认 Responses API 版本,为空则使用上方版本')}
  1302. placeholder={t('例如:preview')}
  1303. onChange={(value) => handleChannelOtherSettingsChange('azure_responses_version', value)}
  1304. showClear
  1305. />
  1306. </div>
  1307. </>
  1308. )}
  1309. {inputs.type === 8 && (
  1310. <>
  1311. <Banner
  1312. type='warning'
  1313. description={t('如果你对接的是上游One API或者New API等转发项目,请使用OpenAI类型,不要使用此类型,除非你知道你在做什么。')}
  1314. className='!rounded-lg'
  1315. />
  1316. <div>
  1317. <Form.Input
  1318. field='base_url'
  1319. label={t('完整的 Base URL,支持变量{model}')}
  1320. placeholder={t('请输入完整的URL,例如:https://api.openai.com/v1/chat/completions')}
  1321. onChange={(value) => handleInputChange('base_url', value)}
  1322. showClear
  1323. />
  1324. </div>
  1325. </>
  1326. )}
  1327. {inputs.type === 37 && (
  1328. <Banner
  1329. type='warning'
  1330. description={t('Dify渠道只适配chatflow和agent,并且agent不支持图片!')}
  1331. className='!rounded-lg'
  1332. />
  1333. )}
  1334. {inputs.type !== 3 && inputs.type !== 8 && inputs.type !== 22 && inputs.type !== 36 && inputs.type !== 45 && (
  1335. <div>
  1336. <Form.Input
  1337. field='base_url'
  1338. label={t('API地址')}
  1339. placeholder={t('此项可选,用于通过自定义API地址来进行 API 调用,末尾不要带/v1和/')}
  1340. onChange={(value) => handleInputChange('base_url', value)}
  1341. showClear
  1342. extraText={t('对于官方渠道,new-api已经内置地址,除非是第三方代理站点或者Azure的特殊接入地址,否则不需要填写')}
  1343. />
  1344. </div>
  1345. )}
  1346. {inputs.type === 22 && (
  1347. <div>
  1348. <Form.Input
  1349. field='base_url'
  1350. label={t('私有部署地址')}
  1351. placeholder={t('请输入私有部署地址,格式为:https://fastgpt.run/api/openapi')}
  1352. onChange={(value) => handleInputChange('base_url', value)}
  1353. showClear
  1354. />
  1355. </div>
  1356. )}
  1357. {inputs.type === 36 && (
  1358. <div>
  1359. <Form.Input
  1360. field='base_url'
  1361. label={t('注意非Chat API,请务必填写正确的API地址,否则可能导致无法使用')}
  1362. placeholder={t('请输入到 /suno 前的路径,通常就是域名,例如:https://api.example.com')}
  1363. onChange={(value) => handleInputChange('base_url', value)}
  1364. showClear
  1365. />
  1366. </div>
  1367. )}
  1368. </Card>
  1369. )}
  1370. {/* Model Configuration Card */}
  1371. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  1372. {/* Header: Model Config */}
  1373. <div className="flex items-center mb-2">
  1374. <Avatar size="small" color="purple" className="mr-2 shadow-md">
  1375. <IconCode size={16} />
  1376. </Avatar>
  1377. <div>
  1378. <Text className="text-lg font-medium">{t('模型配置')}</Text>
  1379. <div className="text-xs text-gray-600">{t('模型选择和映射设置')}</div>
  1380. </div>
  1381. </div>
  1382. <Form.Select
  1383. field='models'
  1384. label={t('模型')}
  1385. placeholder={t('请选择该渠道所支持的模型')}
  1386. rules={[{ required: true, message: t('请选择模型') }]}
  1387. multiple
  1388. filter={selectFilter}
  1389. autoClearSearchValue={false}
  1390. searchPosition='dropdown'
  1391. optionList={modelOptions}
  1392. style={{ width: '100%' }}
  1393. onChange={(value) => handleInputChange('models', value)}
  1394. renderSelectedItem={(optionNode) => {
  1395. const modelName = String(optionNode?.value ?? '');
  1396. return {
  1397. isRenderInTag: true,
  1398. content: (
  1399. <span
  1400. className="cursor-pointer select-none"
  1401. role="button"
  1402. tabIndex={0}
  1403. title={t('点击复制模型名称')}
  1404. onClick={async (e) => {
  1405. e.stopPropagation();
  1406. const ok = await copy(modelName);
  1407. if (ok) {
  1408. showSuccess(t('已复制:{{name}}', { name: modelName }));
  1409. } else {
  1410. showError(t('复制失败'));
  1411. }
  1412. }}
  1413. >
  1414. {optionNode.label || modelName}
  1415. </span>
  1416. ),
  1417. };
  1418. }}
  1419. extraText={(
  1420. <Space wrap>
  1421. <Button size='small' type='primary' onClick={() => handleInputChange('models', basicModels)}>
  1422. {t('填入相关模型')}
  1423. </Button>
  1424. <Button size='small' type='secondary' onClick={() => handleInputChange('models', fullModels)}>
  1425. {t('填入所有模型')}
  1426. </Button>
  1427. <Button size='small' type='tertiary' onClick={() => fetchUpstreamModelList('models')}>
  1428. {t('获取模型列表')}
  1429. </Button>
  1430. <Button size='small' type='warning' onClick={() => handleInputChange('models', [])}>
  1431. {t('清除所有模型')}
  1432. </Button>
  1433. <Button
  1434. size='small'
  1435. type='tertiary'
  1436. onClick={() => {
  1437. if (inputs.models.length === 0) {
  1438. showInfo(t('没有模型可以复制'));
  1439. return;
  1440. }
  1441. try {
  1442. copy(inputs.models.join(','));
  1443. showSuccess(t('模型列表已复制到剪贴板'));
  1444. } catch (error) {
  1445. showError(t('复制失败'));
  1446. }
  1447. }}
  1448. >
  1449. {t('复制所有模型')}
  1450. </Button>
  1451. {modelGroups && modelGroups.length > 0 && modelGroups.map(group => (
  1452. <Button
  1453. key={group.id}
  1454. size='small'
  1455. type='primary'
  1456. onClick={() => {
  1457. let items = [];
  1458. try {
  1459. if (Array.isArray(group.items)) {
  1460. items = group.items;
  1461. } else if (typeof group.items === 'string') {
  1462. const parsed = JSON.parse(group.items || '[]');
  1463. if (Array.isArray(parsed)) items = parsed;
  1464. }
  1465. } catch { }
  1466. const current = formApiRef.current?.getValue('models') || inputs.models || [];
  1467. const merged = Array.from(new Set([...
  1468. current,
  1469. ...items
  1470. ].map(m => (m || '').trim()).filter(Boolean)));
  1471. handleInputChange('models', merged);
  1472. }}
  1473. >
  1474. {group.name}
  1475. </Button>
  1476. ))}
  1477. </Space>
  1478. )}
  1479. />
  1480. <Form.Input
  1481. field='custom_model'
  1482. label={t('自定义模型名称')}
  1483. placeholder={t('输入自定义模型名称')}
  1484. onChange={(value) => setCustomModel(value.trim())}
  1485. value={customModel}
  1486. suffix={
  1487. <Button size='small' type='primary' onClick={addCustomModels}>
  1488. {t('填入')}
  1489. </Button>
  1490. }
  1491. />
  1492. <Form.Input
  1493. field='test_model'
  1494. label={t('默认测试模型')}
  1495. placeholder={t('不填则为模型列表第一个')}
  1496. onChange={(value) => handleInputChange('test_model', value)}
  1497. showClear
  1498. />
  1499. <JSONEditor
  1500. key={`model_mapping-${isEdit ? channelId : 'new'}`}
  1501. field='model_mapping'
  1502. label={t('模型重定向')}
  1503. placeholder={
  1504. t('此项可选,用于修改请求体中的模型名称,为一个 JSON 字符串,键为请求中模型名称,值为要替换的模型名称,例如:') +
  1505. `\n${JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2)}`
  1506. }
  1507. value={inputs.model_mapping || ''}
  1508. onChange={(value) => handleInputChange('model_mapping', value)}
  1509. template={MODEL_MAPPING_EXAMPLE}
  1510. templateLabel={t('填入模板')}
  1511. editorType="keyValue"
  1512. formApi={formApiRef.current}
  1513. extraText={t('键为请求中的模型名称,值为要替换的模型名称')}
  1514. />
  1515. </Card>
  1516. {/* Advanced Settings Card */}
  1517. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  1518. {/* Header: Advanced Settings */}
  1519. <div className="flex items-center mb-2">
  1520. <Avatar size="small" color="orange" className="mr-2 shadow-md">
  1521. <IconSetting size={16} />
  1522. </Avatar>
  1523. <div>
  1524. <Text className="text-lg font-medium">{t('高级设置')}</Text>
  1525. <div className="text-xs text-gray-600">{t('渠道的高级配置选项')}</div>
  1526. </div>
  1527. </div>
  1528. <Form.Select
  1529. field='groups'
  1530. label={t('分组')}
  1531. placeholder={t('请选择可以使用该渠道的分组')}
  1532. multiple
  1533. allowAdditions
  1534. additionLabel={t('请在系统设置页面编辑分组倍率以添加新的分组:')}
  1535. optionList={groupOptions}
  1536. style={{ width: '100%' }}
  1537. onChange={(value) => handleInputChange('groups', value)}
  1538. />
  1539. <Form.Input
  1540. field='tag'
  1541. label={t('渠道标签')}
  1542. placeholder={t('渠道标签')}
  1543. showClear
  1544. onChange={(value) => handleInputChange('tag', value)}
  1545. />
  1546. <Row gutter={12}>
  1547. <Col span={12}>
  1548. <Form.InputNumber
  1549. field='priority'
  1550. label={t('渠道优先级')}
  1551. placeholder={t('渠道优先级')}
  1552. min={0}
  1553. onNumberChange={(value) => handleInputChange('priority', value)}
  1554. style={{ width: '100%' }}
  1555. />
  1556. </Col>
  1557. <Col span={12}>
  1558. <Form.InputNumber
  1559. field='weight'
  1560. label={t('渠道权重')}
  1561. placeholder={t('渠道权重')}
  1562. min={0}
  1563. onNumberChange={(value) => handleInputChange('weight', value)}
  1564. style={{ width: '100%' }}
  1565. />
  1566. </Col>
  1567. </Row>
  1568. <Form.Switch
  1569. field='auto_ban'
  1570. label={t('是否自动禁用')}
  1571. checkedText={t('开')}
  1572. uncheckedText={t('关')}
  1573. onChange={(value) => setAutoBan(value)}
  1574. extraText={t('仅当自动禁用开启时有效,关闭后不会自动禁用该渠道')}
  1575. initValue={autoBan}
  1576. />
  1577. <Form.TextArea
  1578. field='param_override'
  1579. label={t('参数覆盖')}
  1580. placeholder={
  1581. t('此项可选,用于覆盖请求参数。不支持覆盖 stream 参数') +
  1582. '\n' + t('旧格式(直接覆盖):') +
  1583. '\n{\n "temperature": 0,\n "max_tokens": 1000\n}' +
  1584. '\n\n' + t('新格式(支持条件判断与json自定义):') +
  1585. '\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}'
  1586. }
  1587. autosize
  1588. onChange={(value) => handleInputChange('param_override', value)}
  1589. extraText={
  1590. <div className="flex gap-2 flex-wrap">
  1591. <Text
  1592. className="!text-semi-color-primary cursor-pointer"
  1593. onClick={() => handleInputChange('param_override', JSON.stringify({ temperature: 0 }, null, 2))}
  1594. >
  1595. {t('旧格式模板')}
  1596. </Text>
  1597. <Text
  1598. className="!text-semi-color-primary cursor-pointer"
  1599. onClick={() => handleInputChange('param_override', JSON.stringify({
  1600. operations: [
  1601. {
  1602. path: "temperature",
  1603. mode: "set",
  1604. value: 0.7,
  1605. conditions: [
  1606. {
  1607. path: "model",
  1608. mode: "prefix",
  1609. value: "gpt"
  1610. }
  1611. ],
  1612. logic: "AND"
  1613. }
  1614. ]
  1615. }, null, 2))}
  1616. >
  1617. {t('新格式模板')}
  1618. </Text>
  1619. </div>
  1620. }
  1621. showClear
  1622. />
  1623. <Form.TextArea
  1624. field='header_override'
  1625. label={t('请求头覆盖')}
  1626. placeholder={
  1627. t('此项可选,用于覆盖请求头参数') +
  1628. '\n' + t('格式示例:') +
  1629. '\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}'
  1630. }
  1631. autosize
  1632. onChange={(value) => handleInputChange('header_override', value)}
  1633. extraText={
  1634. <div className="flex gap-2 flex-wrap">
  1635. <Text
  1636. className="!text-semi-color-primary cursor-pointer"
  1637. onClick={() => handleInputChange('header_override', JSON.stringify({
  1638. "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"
  1639. }, null, 2))}
  1640. >
  1641. {t('格式模板')}
  1642. </Text>
  1643. </div>
  1644. }
  1645. showClear
  1646. />
  1647. <JSONEditor
  1648. key={`status_code_mapping-${isEdit ? channelId : 'new'}`}
  1649. field='status_code_mapping'
  1650. label={t('状态码复写')}
  1651. placeholder={
  1652. t('此项可选,用于复写返回的状态码,仅影响本地判断,不修改返回到上游的状态码,比如将claude渠道的400错误复写为500(用于重试),请勿滥用该功能,例如:') +
  1653. '\n' +
  1654. JSON.stringify(STATUS_CODE_MAPPING_EXAMPLE, null, 2)
  1655. }
  1656. value={inputs.status_code_mapping || ''}
  1657. onChange={(value) => handleInputChange('status_code_mapping', value)}
  1658. template={STATUS_CODE_MAPPING_EXAMPLE}
  1659. templateLabel={t('填入模板')}
  1660. editorType="keyValue"
  1661. formApi={formApiRef.current}
  1662. extraText={t('键为原状态码,值为要复写的状态码,仅影响本地判断')}
  1663. />
  1664. </Card>
  1665. {/* Channel Extra Settings Card */}
  1666. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  1667. {/* Header: Channel Extra Settings */}
  1668. <div className="flex items-center mb-2">
  1669. <Avatar size="small" color="violet" className="mr-2 shadow-md">
  1670. <IconBolt size={16} />
  1671. </Avatar>
  1672. <div>
  1673. <Text className="text-lg font-medium">{t('渠道额外设置')}</Text>
  1674. </div>
  1675. </div>
  1676. {inputs.type === 1 && (
  1677. <Form.Switch
  1678. field='force_format'
  1679. label={t('强制格式化')}
  1680. checkedText={t('开')}
  1681. uncheckedText={t('关')}
  1682. onChange={(value) => handleChannelSettingsChange('force_format', value)}
  1683. extraText={t('强制将响应格式化为 OpenAI 标准格式(只适用于OpenAI渠道类型)')}
  1684. />
  1685. )}
  1686. <Form.Switch
  1687. field='thinking_to_content'
  1688. label={t('思考内容转换')}
  1689. checkedText={t('开')}
  1690. uncheckedText={t('关')}
  1691. onChange={(value) => handleChannelSettingsChange('thinking_to_content', value)}
  1692. extraText={t('将 reasoning_content 转换为 <think> 标签拼接到内容中')}
  1693. />
  1694. <Form.Switch
  1695. field='pass_through_body_enabled'
  1696. label={t('透传请求体')}
  1697. checkedText={t('开')}
  1698. uncheckedText={t('关')}
  1699. onChange={(value) => handleChannelSettingsChange('pass_through_body_enabled', value)}
  1700. extraText={t('启用请求体透传功能')}
  1701. />
  1702. <Form.Input
  1703. field='proxy'
  1704. label={t('代理地址')}
  1705. placeholder={t('例如: socks5://user:pass@host:port')}
  1706. onChange={(value) => handleChannelSettingsChange('proxy', value)}
  1707. showClear
  1708. extraText={t('用于配置网络代理,支持 socks5 协议')}
  1709. />
  1710. <Form.TextArea
  1711. field='system_prompt'
  1712. label={t('系统提示词')}
  1713. placeholder={t('输入系统提示词,用户的系统提示词将优先于此设置')}
  1714. onChange={(value) => handleChannelSettingsChange('system_prompt', value)}
  1715. autosize
  1716. showClear
  1717. extraText={t('用户优先:如果用户在请求中指定了系统提示词,将优先使用用户的设置')}
  1718. />
  1719. <Form.Switch
  1720. field='system_prompt_override'
  1721. label={t('系统提示词拼接')}
  1722. checkedText={t('开')}
  1723. uncheckedText={t('关')}
  1724. onChange={(value) => handleChannelSettingsChange('system_prompt_override', value)}
  1725. extraText={t('如果用户请求中包含系统提示词,则使用此设置拼接到用户的系统提示词前面')}
  1726. />
  1727. </Card>
  1728. </div>
  1729. </Spin>
  1730. )}
  1731. </Form>
  1732. <ImagePreview
  1733. src={modalImageUrl}
  1734. visible={isModalOpenurl}
  1735. onVisibleChange={(visible) => setIsModalOpenurl(visible)}
  1736. />
  1737. </SideSheet>
  1738. <ModelSelectModal
  1739. visible={modelModalVisible}
  1740. models={fetchedModels}
  1741. selected={inputs.models}
  1742. onConfirm={(selectedModels) => {
  1743. handleInputChange('models', selectedModels);
  1744. showSuccess(t('模型列表已更新'));
  1745. setModelModalVisible(false);
  1746. }}
  1747. onCancel={() => setModelModalVisible(false)}
  1748. />
  1749. </>
  1750. );
  1751. };
  1752. export default EditChannelModal;