EditChannel.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. import React, { useEffect, useState } from 'react';
  2. import { useNavigate } from 'react-router-dom';
  3. import { useTranslation } from 'react-i18next';
  4. import {
  5. API,
  6. isMobile,
  7. showError,
  8. showInfo,
  9. showSuccess,
  10. verifyJSON,
  11. } from '../../helpers';
  12. import { CHANNEL_OPTIONS } from '../../constants';
  13. import {
  14. SideSheet,
  15. Space,
  16. Spin,
  17. Button,
  18. Input,
  19. Typography,
  20. Select,
  21. TextArea,
  22. Checkbox,
  23. Banner,
  24. Modal,
  25. ImagePreview,
  26. Card,
  27. Tag,
  28. } from '@douyinfe/semi-ui';
  29. import { getChannelModels } from '../../helpers';
  30. import {
  31. IconSave,
  32. IconClose,
  33. IconServer,
  34. IconSetting,
  35. IconCode,
  36. IconGlobe,
  37. } from '@douyinfe/semi-icons';
  38. const { Text, Title } = Typography;
  39. const MODEL_MAPPING_EXAMPLE = {
  40. 'gpt-3.5-turbo': 'gpt-3.5-turbo-0125',
  41. };
  42. const STATUS_CODE_MAPPING_EXAMPLE = {
  43. 400: '500',
  44. };
  45. const REGION_EXAMPLE = {
  46. default: 'us-central1',
  47. 'claude-3-5-sonnet-20240620': 'europe-west1',
  48. };
  49. function type2secretPrompt(type) {
  50. // inputs.type === 15 ? '按照如下格式输入:APIKey|SecretKey' : (inputs.type === 18 ? '按照如下格式输入:APPID|APISecret|APIKey' : '请输入渠道对应的鉴权密钥')
  51. switch (type) {
  52. case 15:
  53. return '按照如下格式输入:APIKey|SecretKey';
  54. case 18:
  55. return '按照如下格式输入:APPID|APISecret|APIKey';
  56. case 22:
  57. return '按照如下格式输入:APIKey-AppId,例如:fastgpt-0sp2gtvfdgyi4k30jwlgwf1i-64f335d84283f05518e9e041';
  58. case 23:
  59. return '按照如下格式输入:AppId|SecretId|SecretKey';
  60. case 33:
  61. return '按照如下格式输入:Ak|Sk|Region';
  62. default:
  63. return '请输入渠道对应的鉴权密钥';
  64. }
  65. }
  66. const EditChannel = (props) => {
  67. const { t } = useTranslation();
  68. const navigate = useNavigate();
  69. const channelId = props.editingChannel.id;
  70. const isEdit = channelId !== undefined;
  71. const [loading, setLoading] = useState(isEdit);
  72. const handleCancel = () => {
  73. props.handleClose();
  74. };
  75. const originInputs = {
  76. name: '',
  77. type: 1,
  78. key: '',
  79. openai_organization: '',
  80. max_input_tokens: 0,
  81. base_url: '',
  82. other: '',
  83. model_mapping: '',
  84. status_code_mapping: '',
  85. models: [],
  86. auto_ban: 1,
  87. test_model: '',
  88. groups: ['default'],
  89. priority: 0,
  90. weight: 0,
  91. tag: '',
  92. };
  93. const [batch, setBatch] = useState(false);
  94. const [autoBan, setAutoBan] = useState(true);
  95. // const [autoBan, setAutoBan] = useState(true);
  96. const [inputs, setInputs] = useState(originInputs);
  97. const [originModelOptions, setOriginModelOptions] = useState([]);
  98. const [modelOptions, setModelOptions] = useState([]);
  99. const [groupOptions, setGroupOptions] = useState([]);
  100. const [basicModels, setBasicModels] = useState([]);
  101. const [fullModels, setFullModels] = useState([]);
  102. const [customModel, setCustomModel] = useState('');
  103. const [modalImageUrl, setModalImageUrl] = useState('');
  104. const [isModalOpenurl, setIsModalOpenurl] = useState(false);
  105. const handleInputChange = (name, value) => {
  106. if (name === 'base_url' && value.endsWith('/v1')) {
  107. Modal.confirm({
  108. title: '警告',
  109. content:
  110. '不需要在末尾加/v1,New API会自动处理,添加后可能导致请求失败,是否继续?',
  111. onOk: () => {
  112. setInputs((inputs) => ({ ...inputs, [name]: value }));
  113. },
  114. });
  115. return;
  116. }
  117. setInputs((inputs) => ({ ...inputs, [name]: value }));
  118. if (name === 'type') {
  119. let localModels = [];
  120. switch (value) {
  121. case 2:
  122. localModels = [
  123. 'mj_imagine',
  124. 'mj_variation',
  125. 'mj_reroll',
  126. 'mj_blend',
  127. 'mj_upscale',
  128. 'mj_describe',
  129. 'mj_uploads',
  130. ];
  131. break;
  132. case 5:
  133. localModels = [
  134. 'swap_face',
  135. 'mj_imagine',
  136. 'mj_variation',
  137. 'mj_reroll',
  138. 'mj_blend',
  139. 'mj_upscale',
  140. 'mj_describe',
  141. 'mj_zoom',
  142. 'mj_shorten',
  143. 'mj_modal',
  144. 'mj_inpaint',
  145. 'mj_custom_zoom',
  146. 'mj_high_variation',
  147. 'mj_low_variation',
  148. 'mj_pan',
  149. 'mj_uploads',
  150. ];
  151. break;
  152. case 36:
  153. localModels = ['suno_music', 'suno_lyrics'];
  154. break;
  155. default:
  156. localModels = getChannelModels(value);
  157. break;
  158. }
  159. if (inputs.models.length === 0) {
  160. setInputs((inputs) => ({ ...inputs, models: localModels }));
  161. }
  162. setBasicModels(localModels);
  163. }
  164. //setAutoBan
  165. };
  166. const loadChannel = async () => {
  167. setLoading(true);
  168. let res = await API.get(`/api/channel/${channelId}`);
  169. if (res === undefined) {
  170. return;
  171. }
  172. const { success, message, data } = res.data;
  173. if (success) {
  174. if (data.models === '') {
  175. data.models = [];
  176. } else {
  177. data.models = data.models.split(',');
  178. }
  179. if (data.group === '') {
  180. data.groups = [];
  181. } else {
  182. data.groups = data.group.split(',');
  183. }
  184. if (data.model_mapping !== '') {
  185. data.model_mapping = JSON.stringify(
  186. JSON.parse(data.model_mapping),
  187. null,
  188. 2,
  189. );
  190. }
  191. setInputs(data);
  192. if (data.auto_ban === 0) {
  193. setAutoBan(false);
  194. } else {
  195. setAutoBan(true);
  196. }
  197. setBasicModels(getChannelModels(data.type));
  198. // console.log(data);
  199. } else {
  200. showError(message);
  201. }
  202. setLoading(false);
  203. };
  204. const fetchUpstreamModelList = async (name) => {
  205. // if (inputs['type'] !== 1) {
  206. // showError(t('仅支持 OpenAI 接口格式'));
  207. // return;
  208. // }
  209. setLoading(true);
  210. const models = inputs['models'] || [];
  211. let err = false;
  212. if (isEdit) {
  213. // 如果是编辑模式,使用已有的channel id获取模型列表
  214. const res = await API.get('/api/channel/fetch_models/' + channelId);
  215. if (res.data && res.data?.success) {
  216. models.push(...res.data.data);
  217. } else {
  218. err = true;
  219. }
  220. } else {
  221. // 如果是新建模式,通过后端代理获取模型列表
  222. if (!inputs?.['key']) {
  223. showError(t('请填写密钥'));
  224. err = true;
  225. } else {
  226. try {
  227. const res = await API.post('/api/channel/fetch_models', {
  228. base_url: inputs['base_url'],
  229. type: inputs['type'],
  230. key: inputs['key'],
  231. });
  232. if (res.data && res.data.success) {
  233. models.push(...res.data.data);
  234. } else {
  235. err = true;
  236. }
  237. } catch (error) {
  238. console.error('Error fetching models:', error);
  239. err = true;
  240. }
  241. }
  242. }
  243. if (!err) {
  244. handleInputChange(name, Array.from(new Set(models)));
  245. showSuccess(t('获取模型列表成功'));
  246. } else {
  247. showError(t('获取模型列表失败'));
  248. }
  249. setLoading(false);
  250. };
  251. const fetchModels = async () => {
  252. try {
  253. let res = await API.get(`/api/channel/models`);
  254. let localModelOptions = res.data.data.map((model) => ({
  255. label: model.id,
  256. value: model.id,
  257. }));
  258. setOriginModelOptions(localModelOptions);
  259. setFullModels(res.data.data.map((model) => model.id));
  260. setBasicModels(
  261. res.data.data
  262. .filter((model) => {
  263. return model.id.startsWith('gpt-') || model.id.startsWith('text-');
  264. })
  265. .map((model) => model.id),
  266. );
  267. } catch (error) {
  268. showError(error.message);
  269. }
  270. };
  271. const fetchGroups = async () => {
  272. try {
  273. let res = await API.get(`/api/group/`);
  274. if (res === undefined) {
  275. return;
  276. }
  277. setGroupOptions(
  278. res.data.data.map((group) => ({
  279. label: group,
  280. value: group,
  281. })),
  282. );
  283. } catch (error) {
  284. showError(error.message);
  285. }
  286. };
  287. useEffect(() => {
  288. // 使用 Map 来避免重复,以 value 为键
  289. const modelMap = new Map();
  290. // 先添加原始模型选项
  291. originModelOptions.forEach(option => {
  292. modelMap.set(option.value, option);
  293. });
  294. // 再添加当前选中的模型(如果不存在)
  295. inputs.models.forEach(model => {
  296. if (!modelMap.has(model)) {
  297. modelMap.set(model, {
  298. label: model,
  299. value: model,
  300. });
  301. }
  302. });
  303. setModelOptions(Array.from(modelMap.values()));
  304. }, [originModelOptions, inputs.models]);
  305. useEffect(() => {
  306. fetchModels().then();
  307. fetchGroups().then();
  308. if (isEdit) {
  309. loadChannel().then(() => { });
  310. } else {
  311. setInputs(originInputs);
  312. let localModels = getChannelModels(inputs.type);
  313. setBasicModels(localModels);
  314. setInputs((inputs) => ({ ...inputs, models: localModels }));
  315. }
  316. }, [props.editingChannel.id]);
  317. const submit = async () => {
  318. if (!isEdit && (inputs.name === '' || inputs.key === '')) {
  319. showInfo(t('请填写渠道名称和渠道密钥!'));
  320. return;
  321. }
  322. if (inputs.models.length === 0) {
  323. showInfo(t('请至少选择一个模型!'));
  324. return;
  325. }
  326. if (inputs.model_mapping !== '' && !verifyJSON(inputs.model_mapping)) {
  327. showInfo(t('模型映射必须是合法的 JSON 格式!'));
  328. return;
  329. }
  330. let localInputs = { ...inputs };
  331. if (localInputs.base_url && localInputs.base_url.endsWith('/')) {
  332. localInputs.base_url = localInputs.base_url.slice(
  333. 0,
  334. localInputs.base_url.length - 1,
  335. );
  336. }
  337. if (localInputs.type === 18 && localInputs.other === '') {
  338. localInputs.other = 'v2.1';
  339. }
  340. let res;
  341. if (!Array.isArray(localInputs.models)) {
  342. showError(t('提交失败,请勿重复提交!'));
  343. handleCancel();
  344. return;
  345. }
  346. localInputs.auto_ban = autoBan ? 1 : 0;
  347. localInputs.models = localInputs.models.join(',');
  348. localInputs.group = localInputs.groups.join(',');
  349. if (isEdit) {
  350. res = await API.put(`/api/channel/`, {
  351. ...localInputs,
  352. id: parseInt(channelId),
  353. });
  354. } else {
  355. res = await API.post(`/api/channel/`, localInputs);
  356. }
  357. const { success, message } = res.data;
  358. if (success) {
  359. if (isEdit) {
  360. showSuccess(t('渠道更新成功!'));
  361. } else {
  362. showSuccess(t('渠道创建成功!'));
  363. setInputs(originInputs);
  364. }
  365. props.refresh();
  366. props.handleClose();
  367. } else {
  368. showError(message);
  369. }
  370. };
  371. const addCustomModels = () => {
  372. if (customModel.trim() === '') return;
  373. const modelArray = customModel.split(',').map((model) => model.trim());
  374. let localModels = [...inputs.models];
  375. let localModelOptions = [...modelOptions];
  376. const addedModels = [];
  377. modelArray.forEach((model) => {
  378. if (model && !localModels.includes(model)) {
  379. localModels.push(model);
  380. localModelOptions.push({
  381. key: model,
  382. text: model,
  383. value: model,
  384. });
  385. addedModels.push(model);
  386. }
  387. });
  388. setModelOptions(localModelOptions);
  389. setCustomModel('');
  390. handleInputChange('models', localModels);
  391. if (addedModels.length > 0) {
  392. showSuccess(
  393. t('已新增 {{count}} 个模型:{{list}}', {
  394. count: addedModels.length,
  395. list: addedModels.join(', '),
  396. })
  397. );
  398. } else {
  399. showInfo(t('未发现新增模型'));
  400. }
  401. };
  402. return (
  403. <>
  404. <SideSheet
  405. placement={isEdit ? 'right' : 'left'}
  406. title={
  407. <Space>
  408. <Tag color="blue" shape="circle">{isEdit ? t('编辑') : t('新建')}</Tag>
  409. <Title heading={4} className="m-0">
  410. {isEdit ? t('更新渠道信息') : t('创建新的渠道')}
  411. </Title>
  412. </Space>
  413. }
  414. headerStyle={{
  415. borderBottom: '1px solid var(--semi-color-border)',
  416. padding: '24px'
  417. }}
  418. bodyStyle={{
  419. backgroundColor: 'var(--semi-color-bg-0)',
  420. padding: '0'
  421. }}
  422. visible={props.visible}
  423. width={isMobile() ? '100%' : 600}
  424. footer={
  425. <div className="flex justify-end bg-white">
  426. <Space>
  427. <Button
  428. theme="solid"
  429. size="large"
  430. className="!rounded-full"
  431. onClick={submit}
  432. icon={<IconSave />}
  433. >
  434. {t('提交')}
  435. </Button>
  436. <Button
  437. theme="light"
  438. size="large"
  439. className="!rounded-full"
  440. type="primary"
  441. onClick={handleCancel}
  442. icon={<IconClose />}
  443. >
  444. {t('取消')}
  445. </Button>
  446. </Space>
  447. </div>
  448. }
  449. closeIcon={null}
  450. onCancel={() => handleCancel()}
  451. >
  452. <Spin spinning={loading}>
  453. <div className="p-6">
  454. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  455. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  456. background: 'linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #3b82f6 100%)',
  457. position: 'relative'
  458. }}>
  459. <div className="absolute inset-0 overflow-hidden">
  460. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  461. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  462. </div>
  463. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  464. <IconServer size="large" style={{ color: '#ffffff' }} />
  465. </div>
  466. <div className="relative">
  467. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('基本信息')}</Text>
  468. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('渠道的基本配置信息')}</div>
  469. </div>
  470. </div>
  471. <div className="space-y-4">
  472. <div>
  473. <Text strong className="block mb-2">{t('类型')}</Text>
  474. <Select
  475. name='type'
  476. required
  477. optionList={CHANNEL_OPTIONS}
  478. value={inputs.type}
  479. onChange={(value) => handleInputChange('type', value)}
  480. style={{ width: '100%' }}
  481. filter
  482. searchPosition='dropdown'
  483. placeholder={t('请选择渠道类型')}
  484. size="large"
  485. className="!rounded-lg"
  486. />
  487. </div>
  488. <div>
  489. <Text strong className="block mb-2">{t('名称')}</Text>
  490. <Input
  491. required
  492. name='name'
  493. placeholder={t('请为渠道命名')}
  494. onChange={(value) => {
  495. handleInputChange('name', value);
  496. }}
  497. value={inputs.name}
  498. autoComplete='new-password'
  499. size="large"
  500. className="!rounded-lg"
  501. />
  502. </div>
  503. <div>
  504. <Text strong className="block mb-2">{t('密钥')}</Text>
  505. {batch ? (
  506. <TextArea
  507. name='key'
  508. required
  509. placeholder={t('请输入密钥,一行一个')}
  510. onChange={(value) => {
  511. handleInputChange('key', value);
  512. }}
  513. value={inputs.key}
  514. autosize={{ minRows: 6, maxRows: 6 }}
  515. autoComplete='new-password'
  516. className="!rounded-lg"
  517. />
  518. ) : (
  519. <>
  520. {inputs.type === 41 ? (
  521. <TextArea
  522. name='key'
  523. required
  524. placeholder={
  525. '{\n' +
  526. ' "type": "service_account",\n' +
  527. ' "project_id": "abc-bcd-123-456",\n' +
  528. ' "private_key_id": "123xxxxx456",\n' +
  529. ' "private_key": "-----BEGIN PRIVATE KEY-----xxxx\n' +
  530. ' "client_email": "xxx@developer.gserviceaccount.com",\n' +
  531. ' "client_id": "111222333",\n' +
  532. ' "auth_uri": "https://accounts.google.com/o/oauth2/auth",\n' +
  533. ' "token_uri": "https://oauth2.googleapis.com/token",\n' +
  534. ' "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",\n' +
  535. ' "client_x509_cert_url": "https://xxxxx.gserviceaccount.com",\n' +
  536. ' "universe_domain": "googleapis.com"\n' +
  537. '}'
  538. }
  539. onChange={(value) => {
  540. handleInputChange('key', value);
  541. }}
  542. autosize={{ minRows: 10 }}
  543. value={inputs.key}
  544. autoComplete='new-password'
  545. className="!rounded-lg font-mono"
  546. />
  547. ) : (
  548. <Input
  549. name='key'
  550. required
  551. placeholder={t(type2secretPrompt(inputs.type))}
  552. onChange={(value) => {
  553. handleInputChange('key', value);
  554. }}
  555. value={inputs.key}
  556. autoComplete='new-password'
  557. size="large"
  558. className="!rounded-lg"
  559. />
  560. )}
  561. </>
  562. )}
  563. </div>
  564. {!isEdit && (
  565. <div className="flex items-center">
  566. <Checkbox
  567. checked={batch}
  568. onChange={() => setBatch(!batch)}
  569. />
  570. <Text strong className="ml-2">{t('批量创建')}</Text>
  571. </div>
  572. )}
  573. </div>
  574. </Card>
  575. {/* API Configuration Card */}
  576. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  577. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  578. background: 'linear-gradient(135deg, #065f46 0%, #059669 50%, #10b981 100%)',
  579. position: 'relative'
  580. }}>
  581. <div className="absolute inset-0 overflow-hidden">
  582. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  583. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  584. </div>
  585. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  586. <IconGlobe size="large" style={{ color: '#ffffff' }} />
  587. </div>
  588. <div className="relative">
  589. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('API 配置')}</Text>
  590. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('API 地址和相关配置')}</div>
  591. </div>
  592. </div>
  593. <div className="space-y-4">
  594. {inputs.type === 40 && (
  595. <Banner
  596. type='info'
  597. description={
  598. <div>
  599. <Text strong>{t('邀请链接')}:</Text>
  600. <Text
  601. link
  602. underline
  603. className="ml-2 cursor-pointer"
  604. onClick={() => window.open('https://cloud.siliconflow.cn/i/hij0YNTZ')}
  605. >
  606. https://cloud.siliconflow.cn/i/hij0YNTZ
  607. </Text>
  608. </div>
  609. }
  610. className='!rounded-lg'
  611. />
  612. )}
  613. {inputs.type === 3 && (
  614. <>
  615. <Banner
  616. type='warning'
  617. description={t('2025年5月10日后添加的渠道,不需要再在部署的时候移除模型名称中的"."')}
  618. className='!rounded-lg'
  619. />
  620. <div>
  621. <Text strong className="block mb-2">AZURE_OPENAI_ENDPOINT</Text>
  622. <Input
  623. name='azure_base_url'
  624. placeholder={t('请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com')}
  625. onChange={(value) => handleInputChange('base_url', value)}
  626. value={inputs.base_url}
  627. autoComplete='new-password'
  628. size="large"
  629. className="!rounded-lg"
  630. />
  631. </div>
  632. <div>
  633. <Text strong className="block mb-2">{t('默认 API 版本')}</Text>
  634. <Input
  635. name='azure_other'
  636. placeholder={t('请输入默认 API 版本,例如:2025-04-01-preview')}
  637. onChange={(value) => handleInputChange('other', value)}
  638. value={inputs.other}
  639. autoComplete='new-password'
  640. size="large"
  641. className="!rounded-lg"
  642. />
  643. </div>
  644. </>
  645. )}
  646. {inputs.type === 8 && (
  647. <>
  648. <Banner
  649. type='warning'
  650. description={t('如果你对接的是上游One API或者New API等转发项目,请使用OpenAI类型,不要使用此类型,除非你知道你在做什么。')}
  651. className='!rounded-lg'
  652. />
  653. <div>
  654. <Text strong className="block mb-2">{t('完整的 Base URL,支持变量{model}')}</Text>
  655. <Input
  656. name='base_url'
  657. placeholder={t('请输入完整的URL,例如:https://api.openai.com/v1/chat/completions')}
  658. onChange={(value) => handleInputChange('base_url', value)}
  659. value={inputs.base_url}
  660. autoComplete='new-password'
  661. size="large"
  662. className="!rounded-lg"
  663. />
  664. </div>
  665. </>
  666. )}
  667. {inputs.type === 37 && (
  668. <Banner
  669. type='warning'
  670. description={t('Dify渠道只适配chatflow和agent,并且agent不支持图片!')}
  671. className='!rounded-lg'
  672. />
  673. )}
  674. {inputs.type !== 3 && inputs.type !== 8 && inputs.type !== 22 && inputs.type !== 36 && inputs.type !== 45 && (
  675. <div>
  676. <Text strong className="block mb-2">{t('API地址')}</Text>
  677. <Input
  678. name='base_url'
  679. placeholder={t('此项可选,用于通过自定义API地址来进行 API 调用,末尾不要带/v1和/')}
  680. onChange={(value) => handleInputChange('base_url', value)}
  681. value={inputs.base_url}
  682. autoComplete='new-password'
  683. size="large"
  684. className="!rounded-lg"
  685. />
  686. <Text type="tertiary" className="mt-1 text-xs">
  687. {t('对于官方渠道,new-api已经内置地址,除非是第三方代理站点或者Azure的特殊接入地址,否则不需要填写')}
  688. </Text>
  689. </div>
  690. )}
  691. {inputs.type === 22 && (
  692. <div>
  693. <Text strong className="block mb-2">{t('私有部署地址')}</Text>
  694. <Input
  695. name='base_url'
  696. placeholder={t('请输入私有部署地址,格式为:https://fastgpt.run/api/openapi')}
  697. onChange={(value) => handleInputChange('base_url', value)}
  698. value={inputs.base_url}
  699. autoComplete='new-password'
  700. size="large"
  701. className="!rounded-lg"
  702. />
  703. </div>
  704. )}
  705. {inputs.type === 36 && (
  706. <div>
  707. <Text strong className="block mb-2">
  708. {t('注意非Chat API,请务必填写正确的API地址,否则可能导致无法使用')}
  709. </Text>
  710. <Input
  711. name='base_url'
  712. placeholder={t('请输入到 /suno 前的路径,通常就是域名,例如:https://api.example.com')}
  713. onChange={(value) => handleInputChange('base_url', value)}
  714. value={inputs.base_url}
  715. autoComplete='new-password'
  716. size="large"
  717. className="!rounded-lg"
  718. />
  719. </div>
  720. )}
  721. </div>
  722. </Card>
  723. {/* Model Configuration Card */}
  724. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  725. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  726. background: 'linear-gradient(135deg, #4c1d95 0%, #6d28d9 50%, #7c3aed 100%)',
  727. position: 'relative'
  728. }}>
  729. <div className="absolute inset-0 overflow-hidden">
  730. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  731. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  732. </div>
  733. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  734. <IconCode size="large" style={{ color: '#ffffff' }} />
  735. </div>
  736. <div className="relative">
  737. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('模型配置')}</Text>
  738. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('模型选择和映射设置')}</div>
  739. </div>
  740. </div>
  741. <div className="space-y-4">
  742. <div>
  743. <Text strong className="block mb-2">{t('模型')}</Text>
  744. <Select
  745. placeholder={t('请选择该渠道所支持的模型')}
  746. name='models'
  747. required
  748. multiple
  749. selection
  750. filter
  751. searchPosition='dropdown'
  752. onChange={(value) => handleInputChange('models', value)}
  753. value={inputs.models}
  754. autoComplete='new-password'
  755. optionList={modelOptions}
  756. size="large"
  757. className="!rounded-lg"
  758. />
  759. </div>
  760. <div className="flex flex-wrap gap-2">
  761. <Button
  762. type='primary'
  763. onClick={() => handleInputChange('models', basicModels)}
  764. size="large"
  765. className="!rounded-lg"
  766. >
  767. {t('填入相关模型')}
  768. </Button>
  769. <Button
  770. type='secondary'
  771. onClick={() => handleInputChange('models', fullModels)}
  772. size="large"
  773. className="!rounded-lg"
  774. >
  775. {t('填入所有模型')}
  776. </Button>
  777. <Button
  778. type='tertiary'
  779. onClick={() => fetchUpstreamModelList('models')}
  780. size="large"
  781. className="!rounded-lg"
  782. >
  783. {t('获取模型列表')}
  784. </Button>
  785. <Button
  786. type='warning'
  787. onClick={() => handleInputChange('models', [])}
  788. size="large"
  789. className="!rounded-lg"
  790. >
  791. {t('清除所有模型')}
  792. </Button>
  793. </div>
  794. <div>
  795. <Input
  796. addonAfter={
  797. <Button type='primary' onClick={addCustomModels} className="!rounded-r-lg">
  798. {t('填入')}
  799. </Button>
  800. }
  801. placeholder={t('输入自定义模型名称')}
  802. value={customModel}
  803. onChange={(value) => setCustomModel(value.trim())}
  804. size="large"
  805. className="!rounded-lg"
  806. />
  807. </div>
  808. <div>
  809. <Text strong className="block mb-2">{t('模型重定向')}</Text>
  810. <TextArea
  811. placeholder={
  812. t('此项可选,用于修改请求体中的模型名称,为一个 JSON 字符串,键为请求中模型名称,值为要替换的模型名称,例如:') +
  813. `\n${JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2)}`
  814. }
  815. name='model_mapping'
  816. onChange={(value) => handleInputChange('model_mapping', value)}
  817. autosize
  818. value={inputs.model_mapping}
  819. autoComplete='new-password'
  820. className="!rounded-lg font-mono"
  821. />
  822. <Text
  823. className="!text-semi-color-primary cursor-pointer mt-1 block"
  824. onClick={() => handleInputChange('model_mapping', JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2))}
  825. >
  826. {t('填入模板')}
  827. </Text>
  828. </div>
  829. <div>
  830. <Text strong className="block mb-2">{t('默认测试模型')}</Text>
  831. <Input
  832. name='test_model'
  833. placeholder={t('不填则为模型列表第一个')}
  834. onChange={(value) => handleInputChange('test_model', value)}
  835. value={inputs.test_model}
  836. size="large"
  837. className="!rounded-lg"
  838. />
  839. </div>
  840. </div>
  841. </Card>
  842. {/* Advanced Settings Card */}
  843. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  844. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  845. background: 'linear-gradient(135deg, #92400e 0%, #d97706 50%, #f59e0b 100%)',
  846. position: 'relative'
  847. }}>
  848. <div className="absolute inset-0 overflow-hidden">
  849. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  850. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  851. </div>
  852. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  853. <IconSetting size="large" style={{ color: '#ffffff' }} />
  854. </div>
  855. <div className="relative">
  856. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('高级设置')}</Text>
  857. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('渠道的高级配置选项')}</div>
  858. </div>
  859. </div>
  860. <div className="space-y-4">
  861. <div>
  862. <Text strong className="block mb-2">{t('分组')}</Text>
  863. <Select
  864. placeholder={t('请选择可以使用该渠道的分组')}
  865. name='groups'
  866. required
  867. multiple
  868. selection
  869. allowAdditions
  870. additionLabel={t('请在系统设置页面编辑分组倍率以添加新的分组:')}
  871. onChange={(value) => handleInputChange('groups', value)}
  872. value={inputs.groups}
  873. autoComplete='new-password'
  874. optionList={groupOptions}
  875. size="large"
  876. className="!rounded-lg"
  877. />
  878. </div>
  879. {inputs.type === 18 && (
  880. <div>
  881. <Text strong className="block mb-2">{t('模型版本')}</Text>
  882. <Input
  883. name='other'
  884. placeholder={'请输入星火大模型版本,注意是接口地址中的版本号,例如:v2.1'}
  885. onChange={(value) => handleInputChange('other', value)}
  886. value={inputs.other}
  887. autoComplete='new-password'
  888. size="large"
  889. className="!rounded-lg"
  890. />
  891. </div>
  892. )}
  893. {inputs.type === 41 && (
  894. <div>
  895. <Text strong className="block mb-2">{t('部署地区')}</Text>
  896. <TextArea
  897. name='other'
  898. placeholder={t(
  899. '请输入部署地区,例如:us-central1\n支持使用模型映射格式\n' +
  900. '{\n' +
  901. ' "default": "us-central1",\n' +
  902. ' "claude-3-5-sonnet-20240620": "europe-west1"\n' +
  903. '}'
  904. )}
  905. autosize={{ minRows: 2 }}
  906. onChange={(value) => handleInputChange('other', value)}
  907. value={inputs.other}
  908. autoComplete='new-password'
  909. className="!rounded-lg font-mono"
  910. />
  911. <Text
  912. className="!text-semi-color-primary cursor-pointer mt-1 block"
  913. onClick={() => handleInputChange('other', JSON.stringify(REGION_EXAMPLE, null, 2))}
  914. >
  915. {t('填入模板')}
  916. </Text>
  917. </div>
  918. )}
  919. {inputs.type === 21 && (
  920. <div>
  921. <Text strong className="block mb-2">{t('知识库 ID')}</Text>
  922. <Input
  923. name='other'
  924. placeholder={'请输入知识库 ID,例如:123456'}
  925. onChange={(value) => handleInputChange('other', value)}
  926. value={inputs.other}
  927. autoComplete='new-password'
  928. size="large"
  929. className="!rounded-lg"
  930. />
  931. </div>
  932. )}
  933. {inputs.type === 39 && (
  934. <div>
  935. <Text strong className="block mb-2">Account ID</Text>
  936. <Input
  937. name='other'
  938. placeholder={'请输入Account ID,例如:d6b5da8hk1awo8nap34ube6gh'}
  939. onChange={(value) => handleInputChange('other', value)}
  940. value={inputs.other}
  941. autoComplete='new-password'
  942. size="large"
  943. className="!rounded-lg"
  944. />
  945. </div>
  946. )}
  947. {inputs.type === 49 && (
  948. <div>
  949. <Text strong className="block mb-2">{t('智能体ID')}</Text>
  950. <Input
  951. name='other'
  952. placeholder={'请输入智能体ID,例如:7342866812345'}
  953. onChange={(value) => handleInputChange('other', value)}
  954. value={inputs.other}
  955. autoComplete='new-password'
  956. size="large"
  957. className="!rounded-lg"
  958. />
  959. </div>
  960. )}
  961. <div>
  962. <Text strong className="block mb-2">{t('渠道标签')}</Text>
  963. <Input
  964. name='tag'
  965. placeholder={t('渠道标签')}
  966. onChange={(value) => handleInputChange('tag', value)}
  967. value={inputs.tag}
  968. autoComplete='new-password'
  969. size="large"
  970. className="!rounded-lg"
  971. />
  972. </div>
  973. <div>
  974. <Text strong className="block mb-2">{t('渠道优先级')}</Text>
  975. <Input
  976. name='priority'
  977. placeholder={t('渠道优先级')}
  978. onChange={(value) => {
  979. const number = parseInt(value);
  980. if (isNaN(number)) {
  981. handleInputChange('priority', value);
  982. } else {
  983. handleInputChange('priority', number);
  984. }
  985. }}
  986. value={inputs.priority}
  987. autoComplete='new-password'
  988. size="large"
  989. className="!rounded-lg"
  990. />
  991. </div>
  992. <div>
  993. <Text strong className="block mb-2">{t('渠道权重')}</Text>
  994. <Input
  995. name='weight'
  996. placeholder={t('渠道权重')}
  997. onChange={(value) => {
  998. const number = parseInt(value);
  999. if (isNaN(number)) {
  1000. handleInputChange('weight', value);
  1001. } else {
  1002. handleInputChange('weight', number);
  1003. }
  1004. }}
  1005. value={inputs.weight}
  1006. autoComplete='new-password'
  1007. size="large"
  1008. className="!rounded-lg"
  1009. />
  1010. </div>
  1011. <div>
  1012. <Text strong className="block mb-2">{t('渠道额外设置')}</Text>
  1013. <TextArea
  1014. placeholder={
  1015. t('此项可选,用于配置渠道特定设置,为一个 JSON 字符串,例如:') +
  1016. '\n{\n "force_format": true\n}'
  1017. }
  1018. name='setting'
  1019. onChange={(value) => handleInputChange('setting', value)}
  1020. autosize
  1021. value={inputs.setting}
  1022. autoComplete='new-password'
  1023. className="!rounded-lg font-mono"
  1024. />
  1025. <div className="flex gap-2 mt-1">
  1026. <Text
  1027. className="!text-semi-color-primary cursor-pointer"
  1028. onClick={() => {
  1029. handleInputChange(
  1030. 'setting',
  1031. JSON.stringify({ force_format: true }, null, 2),
  1032. );
  1033. }}
  1034. >
  1035. {t('填入模板')}
  1036. </Text>
  1037. <Text
  1038. className="!text-semi-color-primary cursor-pointer"
  1039. onClick={() => {
  1040. window.open(
  1041. 'https://github.com/QuantumNous/new-api/blob/main/docs/channel/other_setting.md',
  1042. );
  1043. }}
  1044. >
  1045. {t('设置说明')}
  1046. </Text>
  1047. </div>
  1048. </div>
  1049. <div>
  1050. <Text strong className="block mb-2">{t('参数覆盖')}</Text>
  1051. <TextArea
  1052. placeholder={
  1053. t('此项可选,用于覆盖请求参数。不支持覆盖 stream 参数。为一个 JSON 字符串,例如:') +
  1054. '\n{\n "temperature": 0\n}'
  1055. }
  1056. name='param_override'
  1057. onChange={(value) => handleInputChange('param_override', value)}
  1058. autosize
  1059. value={inputs.param_override}
  1060. autoComplete='new-password'
  1061. className="!rounded-lg font-mono"
  1062. />
  1063. </div>
  1064. {inputs.type === 1 && (
  1065. <div>
  1066. <Text strong className="block mb-2">{t('组织')}</Text>
  1067. <Input
  1068. name='openai_organization'
  1069. placeholder={t('请输入组织org-xxx')}
  1070. onChange={(value) => handleInputChange('openai_organization', value)}
  1071. value={inputs.openai_organization}
  1072. size="large"
  1073. className="!rounded-lg"
  1074. />
  1075. <Text type="tertiary" className="mt-1 text-xs">
  1076. {t('组织,可选,不填则为默认组织')}
  1077. </Text>
  1078. </div>
  1079. )}
  1080. <div className="flex items-center">
  1081. <Checkbox
  1082. checked={autoBan}
  1083. onChange={() => setAutoBan(!autoBan)}
  1084. />
  1085. <Text strong className="ml-2">
  1086. {t('是否自动禁用(仅当自动禁用开启时有效),关闭后不会自动禁用该渠道')}
  1087. </Text>
  1088. </div>
  1089. <div>
  1090. <Text strong className="block mb-2">
  1091. {t('状态码复写(仅影响本地判断,不修改返回到上游的状态码)')}
  1092. </Text>
  1093. <TextArea
  1094. placeholder={
  1095. t('此项可选,用于复写返回的状态码,比如将claude渠道的400错误复写为500(用于重试),请勿滥用该功能,例如:') +
  1096. '\n' +
  1097. JSON.stringify(STATUS_CODE_MAPPING_EXAMPLE, null, 2)
  1098. }
  1099. name='status_code_mapping'
  1100. onChange={(value) => handleInputChange('status_code_mapping', value)}
  1101. autosize
  1102. value={inputs.status_code_mapping}
  1103. autoComplete='new-password'
  1104. className="!rounded-lg font-mono"
  1105. />
  1106. <Text
  1107. className="!text-semi-color-primary cursor-pointer mt-1 block"
  1108. onClick={() => {
  1109. handleInputChange(
  1110. 'status_code_mapping',
  1111. JSON.stringify(STATUS_CODE_MAPPING_EXAMPLE, null, 2),
  1112. );
  1113. }}
  1114. >
  1115. {t('填入模板')}
  1116. </Text>
  1117. </div>
  1118. </div>
  1119. </Card>
  1120. </div>
  1121. </Spin>
  1122. <ImagePreview
  1123. src={modalImageUrl}
  1124. visible={isModalOpenurl}
  1125. onVisibleChange={(visible) => setIsModalOpenurl(visible)}
  1126. />
  1127. </SideSheet>
  1128. </>
  1129. );
  1130. };
  1131. export default EditChannel;