EditChannel.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. let localModelOptions = [...originModelOptions];
  289. inputs.models.forEach((model) => {
  290. if (!localModelOptions.find((option) => option.label === model)) {
  291. localModelOptions.push({
  292. label: model,
  293. value: model,
  294. });
  295. }
  296. });
  297. setModelOptions(localModelOptions);
  298. }, [originModelOptions, inputs.models]);
  299. useEffect(() => {
  300. fetchModels().then();
  301. fetchGroups().then();
  302. if (isEdit) {
  303. loadChannel().then(() => { });
  304. } else {
  305. setInputs(originInputs);
  306. let localModels = getChannelModels(inputs.type);
  307. setBasicModels(localModels);
  308. setInputs((inputs) => ({ ...inputs, models: localModels }));
  309. }
  310. }, [props.editingChannel.id]);
  311. const submit = async () => {
  312. if (!isEdit && (inputs.name === '' || inputs.key === '')) {
  313. showInfo(t('请填写渠道名称和渠道密钥!'));
  314. return;
  315. }
  316. if (inputs.models.length === 0) {
  317. showInfo(t('请至少选择一个模型!'));
  318. return;
  319. }
  320. if (inputs.model_mapping !== '' && !verifyJSON(inputs.model_mapping)) {
  321. showInfo(t('模型映射必须是合法的 JSON 格式!'));
  322. return;
  323. }
  324. let localInputs = { ...inputs };
  325. if (localInputs.base_url && localInputs.base_url.endsWith('/')) {
  326. localInputs.base_url = localInputs.base_url.slice(
  327. 0,
  328. localInputs.base_url.length - 1,
  329. );
  330. }
  331. if (localInputs.type === 18 && localInputs.other === '') {
  332. localInputs.other = 'v2.1';
  333. }
  334. let res;
  335. if (!Array.isArray(localInputs.models)) {
  336. showError(t('提交失败,请勿重复提交!'));
  337. handleCancel();
  338. return;
  339. }
  340. localInputs.auto_ban = autoBan ? 1 : 0;
  341. localInputs.models = localInputs.models.join(',');
  342. localInputs.group = localInputs.groups.join(',');
  343. if (isEdit) {
  344. res = await API.put(`/api/channel/`, {
  345. ...localInputs,
  346. id: parseInt(channelId),
  347. });
  348. } else {
  349. res = await API.post(`/api/channel/`, localInputs);
  350. }
  351. const { success, message } = res.data;
  352. if (success) {
  353. if (isEdit) {
  354. showSuccess(t('渠道更新成功!'));
  355. } else {
  356. showSuccess(t('渠道创建成功!'));
  357. setInputs(originInputs);
  358. }
  359. props.refresh();
  360. props.handleClose();
  361. } else {
  362. showError(message);
  363. }
  364. };
  365. const addCustomModels = () => {
  366. if (customModel.trim() === '') return;
  367. const modelArray = customModel.split(',').map((model) => model.trim());
  368. let localModels = [...inputs.models];
  369. let localModelOptions = [...modelOptions];
  370. const addedModels = [];
  371. modelArray.forEach((model) => {
  372. if (model && !localModels.includes(model)) {
  373. localModels.push(model);
  374. localModelOptions.push({
  375. key: model,
  376. text: model,
  377. value: model,
  378. });
  379. addedModels.push(model);
  380. }
  381. });
  382. setModelOptions(localModelOptions);
  383. setCustomModel('');
  384. handleInputChange('models', localModels);
  385. if (addedModels.length > 0) {
  386. showSuccess(
  387. t('已新增 {{count}} 个模型:{{list}}', {
  388. count: addedModels.length,
  389. list: addedModels.join(', '),
  390. })
  391. );
  392. } else {
  393. showInfo(t('未发现新增模型'));
  394. }
  395. };
  396. return (
  397. <>
  398. <SideSheet
  399. placement={isEdit ? 'right' : 'left'}
  400. title={
  401. <Space>
  402. <Tag color="blue" shape="circle">{isEdit ? t('编辑') : t('新建')}</Tag>
  403. <Title heading={4} className="m-0">
  404. {isEdit ? t('更新渠道信息') : t('创建新的渠道')}
  405. </Title>
  406. </Space>
  407. }
  408. headerStyle={{
  409. borderBottom: '1px solid var(--semi-color-border)',
  410. padding: '24px'
  411. }}
  412. bodyStyle={{
  413. backgroundColor: 'var(--semi-color-bg-0)',
  414. padding: '0'
  415. }}
  416. visible={props.visible}
  417. width={isMobile() ? '100%' : 600}
  418. footer={
  419. <div className="flex justify-end bg-white">
  420. <Space>
  421. <Button
  422. theme="solid"
  423. size="large"
  424. className="!rounded-full"
  425. onClick={submit}
  426. icon={<IconSave />}
  427. >
  428. {t('提交')}
  429. </Button>
  430. <Button
  431. theme="light"
  432. size="large"
  433. className="!rounded-full"
  434. type="primary"
  435. onClick={handleCancel}
  436. icon={<IconClose />}
  437. >
  438. {t('取消')}
  439. </Button>
  440. </Space>
  441. </div>
  442. }
  443. closeIcon={null}
  444. onCancel={() => handleCancel()}
  445. >
  446. <Spin spinning={loading}>
  447. <div className="p-6">
  448. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  449. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  450. background: 'linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #3b82f6 100%)',
  451. position: 'relative'
  452. }}>
  453. <div className="absolute inset-0 overflow-hidden">
  454. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  455. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  456. </div>
  457. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  458. <IconServer size="large" style={{ color: '#ffffff' }} />
  459. </div>
  460. <div className="relative">
  461. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('基本信息')}</Text>
  462. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('渠道的基本配置信息')}</div>
  463. </div>
  464. </div>
  465. <div className="space-y-4">
  466. <div>
  467. <Text strong className="block mb-2">{t('类型')}</Text>
  468. <Select
  469. name='type'
  470. required
  471. optionList={CHANNEL_OPTIONS}
  472. value={inputs.type}
  473. onChange={(value) => handleInputChange('type', value)}
  474. style={{ width: '100%' }}
  475. filter
  476. searchPosition='dropdown'
  477. placeholder={t('请选择渠道类型')}
  478. size="large"
  479. className="!rounded-lg"
  480. />
  481. </div>
  482. <div>
  483. <Text strong className="block mb-2">{t('名称')}</Text>
  484. <Input
  485. required
  486. name='name'
  487. placeholder={t('请为渠道命名')}
  488. onChange={(value) => {
  489. handleInputChange('name', value);
  490. }}
  491. value={inputs.name}
  492. autoComplete='new-password'
  493. size="large"
  494. className="!rounded-lg"
  495. />
  496. </div>
  497. <div>
  498. <Text strong className="block mb-2">{t('密钥')}</Text>
  499. {batch ? (
  500. <TextArea
  501. name='key'
  502. required
  503. placeholder={t('请输入密钥,一行一个')}
  504. onChange={(value) => {
  505. handleInputChange('key', value);
  506. }}
  507. value={inputs.key}
  508. style={{ minHeight: 150, fontFamily: 'JetBrains Mono, Consolas' }}
  509. autoComplete='new-password'
  510. className="!rounded-lg"
  511. />
  512. ) : (
  513. <>
  514. {inputs.type === 41 ? (
  515. <TextArea
  516. name='key'
  517. required
  518. placeholder={
  519. '{\n' +
  520. ' "type": "service_account",\n' +
  521. ' "project_id": "abc-bcd-123-456",\n' +
  522. ' "private_key_id": "123xxxxx456",\n' +
  523. ' "private_key": "-----BEGIN PRIVATE KEY-----xxxx\n' +
  524. ' "client_email": "xxx@developer.gserviceaccount.com",\n' +
  525. ' "client_id": "111222333",\n' +
  526. ' "auth_uri": "https://accounts.google.com/o/oauth2/auth",\n' +
  527. ' "token_uri": "https://oauth2.googleapis.com/token",\n' +
  528. ' "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",\n' +
  529. ' "client_x509_cert_url": "https://xxxxx.gserviceaccount.com",\n' +
  530. ' "universe_domain": "googleapis.com"\n' +
  531. '}'
  532. }
  533. onChange={(value) => {
  534. handleInputChange('key', value);
  535. }}
  536. autosize={{ minRows: 10 }}
  537. value={inputs.key}
  538. autoComplete='new-password'
  539. className="!rounded-lg font-mono"
  540. />
  541. ) : (
  542. <Input
  543. name='key'
  544. required
  545. placeholder={t(type2secretPrompt(inputs.type))}
  546. onChange={(value) => {
  547. handleInputChange('key', value);
  548. }}
  549. value={inputs.key}
  550. autoComplete='new-password'
  551. size="large"
  552. className="!rounded-lg"
  553. />
  554. )}
  555. </>
  556. )}
  557. </div>
  558. {!isEdit && (
  559. <div className="flex items-center">
  560. <Checkbox
  561. checked={batch}
  562. onChange={() => setBatch(!batch)}
  563. />
  564. <Text strong className="ml-2">{t('批量创建')}</Text>
  565. </div>
  566. )}
  567. </div>
  568. </Card>
  569. {/* API Configuration Card */}
  570. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  571. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  572. background: 'linear-gradient(135deg, #065f46 0%, #059669 50%, #10b981 100%)',
  573. position: 'relative'
  574. }}>
  575. <div className="absolute inset-0 overflow-hidden">
  576. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  577. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  578. </div>
  579. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  580. <IconGlobe size="large" style={{ color: '#ffffff' }} />
  581. </div>
  582. <div className="relative">
  583. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('API 配置')}</Text>
  584. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('API 地址和相关配置')}</div>
  585. </div>
  586. </div>
  587. <div className="space-y-4">
  588. {inputs.type === 40 && (
  589. <Banner
  590. type='info'
  591. description={
  592. <div>
  593. <Text strong>{t('邀请链接')}:</Text>
  594. <Text
  595. link
  596. underline
  597. className="ml-2 cursor-pointer"
  598. onClick={() => window.open('https://cloud.siliconflow.cn/i/hij0YNTZ')}
  599. >
  600. https://cloud.siliconflow.cn/i/hij0YNTZ
  601. </Text>
  602. </div>
  603. }
  604. className='!rounded-lg'
  605. />
  606. )}
  607. {inputs.type === 3 && (
  608. <>
  609. <Banner
  610. type='warning'
  611. description={t('2025年5月10日后添加的渠道,不需要再在部署的时候移除模型名称中的"."')}
  612. className='!rounded-lg'
  613. />
  614. <div>
  615. <Text strong className="block mb-2">AZURE_OPENAI_ENDPOINT</Text>
  616. <Input
  617. name='azure_base_url'
  618. placeholder={t('请输入 AZURE_OPENAI_ENDPOINT,例如:https://docs-test-001.openai.azure.com')}
  619. onChange={(value) => handleInputChange('base_url', value)}
  620. value={inputs.base_url}
  621. autoComplete='new-password'
  622. size="large"
  623. className="!rounded-lg"
  624. />
  625. </div>
  626. <div>
  627. <Text strong className="block mb-2">{t('默认 API 版本')}</Text>
  628. <Input
  629. name='azure_other'
  630. placeholder={t('请输入默认 API 版本,例如:2025-04-01-preview')}
  631. onChange={(value) => handleInputChange('other', value)}
  632. value={inputs.other}
  633. autoComplete='new-password'
  634. size="large"
  635. className="!rounded-lg"
  636. />
  637. </div>
  638. </>
  639. )}
  640. {inputs.type === 8 && (
  641. <>
  642. <Banner
  643. type='warning'
  644. description={t('如果你对接的是上游One API或者New API等转发项目,请使用OpenAI类型,不要使用此类型,除非你知道你在做什么。')}
  645. className='!rounded-lg'
  646. />
  647. <div>
  648. <Text strong className="block mb-2">{t('完整的 Base URL,支持变量{model}')}</Text>
  649. <Input
  650. name='base_url'
  651. placeholder={t('请输入完整的URL,例如:https://api.openai.com/v1/chat/completions')}
  652. onChange={(value) => handleInputChange('base_url', value)}
  653. value={inputs.base_url}
  654. autoComplete='new-password'
  655. size="large"
  656. className="!rounded-lg"
  657. />
  658. </div>
  659. </>
  660. )}
  661. {inputs.type === 37 && (
  662. <Banner
  663. type='warning'
  664. description={t('Dify渠道只适配chatflow和agent,并且agent不支持图片!')}
  665. className='!rounded-lg'
  666. />
  667. )}
  668. {inputs.type !== 3 && inputs.type !== 8 && inputs.type !== 22 && inputs.type !== 36 && inputs.type !== 45 && (
  669. <div>
  670. <Text strong className="block mb-2">{t('API地址')}</Text>
  671. <Input
  672. name='base_url'
  673. placeholder={t('此项可选,用于通过自定义API地址来进行 API 调用,末尾不要带/v1和/')}
  674. onChange={(value) => handleInputChange('base_url', value)}
  675. value={inputs.base_url}
  676. autoComplete='new-password'
  677. size="large"
  678. className="!rounded-lg"
  679. />
  680. <Text type="tertiary" className="mt-1 text-xs">
  681. {t('对于官方渠道,new-api已经内置地址,除非是第三方代理站点或者Azure的特殊接入地址,否则不需要填写')}
  682. </Text>
  683. </div>
  684. )}
  685. {inputs.type === 22 && (
  686. <div>
  687. <Text strong className="block mb-2">{t('私有部署地址')}</Text>
  688. <Input
  689. name='base_url'
  690. placeholder={t('请输入私有部署地址,格式为:https://fastgpt.run/api/openapi')}
  691. onChange={(value) => handleInputChange('base_url', value)}
  692. value={inputs.base_url}
  693. autoComplete='new-password'
  694. size="large"
  695. className="!rounded-lg"
  696. />
  697. </div>
  698. )}
  699. {inputs.type === 36 && (
  700. <div>
  701. <Text strong className="block mb-2">
  702. {t('注意非Chat API,请务必填写正确的API地址,否则可能导致无法使用')}
  703. </Text>
  704. <Input
  705. name='base_url'
  706. placeholder={t('请输入到 /suno 前的路径,通常就是域名,例如:https://api.example.com')}
  707. onChange={(value) => handleInputChange('base_url', value)}
  708. value={inputs.base_url}
  709. autoComplete='new-password'
  710. size="large"
  711. className="!rounded-lg"
  712. />
  713. </div>
  714. )}
  715. </div>
  716. </Card>
  717. {/* Model Configuration Card */}
  718. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  719. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  720. background: 'linear-gradient(135deg, #4c1d95 0%, #6d28d9 50%, #7c3aed 100%)',
  721. position: 'relative'
  722. }}>
  723. <div className="absolute inset-0 overflow-hidden">
  724. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  725. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  726. </div>
  727. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  728. <IconCode size="large" style={{ color: '#ffffff' }} />
  729. </div>
  730. <div className="relative">
  731. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('模型配置')}</Text>
  732. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('模型选择和映射设置')}</div>
  733. </div>
  734. </div>
  735. <div className="space-y-4">
  736. <div>
  737. <Text strong className="block mb-2">{t('模型')}</Text>
  738. <Select
  739. placeholder={t('请选择该渠道所支持的模型')}
  740. name='models'
  741. required
  742. multiple
  743. selection
  744. filter
  745. searchPosition='dropdown'
  746. onChange={(value) => handleInputChange('models', value)}
  747. value={inputs.models}
  748. autoComplete='new-password'
  749. optionList={modelOptions}
  750. size="large"
  751. className="!rounded-lg"
  752. />
  753. </div>
  754. <div className="flex flex-wrap gap-2">
  755. <Button
  756. type='primary'
  757. onClick={() => handleInputChange('models', basicModels)}
  758. size="large"
  759. className="!rounded-lg"
  760. >
  761. {t('填入相关模型')}
  762. </Button>
  763. <Button
  764. type='secondary'
  765. onClick={() => handleInputChange('models', fullModels)}
  766. size="large"
  767. className="!rounded-lg"
  768. >
  769. {t('填入所有模型')}
  770. </Button>
  771. <Button
  772. type='tertiary'
  773. onClick={() => fetchUpstreamModelList('models')}
  774. size="large"
  775. className="!rounded-lg"
  776. >
  777. {t('获取模型列表')}
  778. </Button>
  779. <Button
  780. type='warning'
  781. onClick={() => handleInputChange('models', [])}
  782. size="large"
  783. className="!rounded-lg"
  784. >
  785. {t('清除所有模型')}
  786. </Button>
  787. </div>
  788. <div>
  789. <Input
  790. addonAfter={
  791. <Button type='primary' onClick={addCustomModels} className="!rounded-r-lg">
  792. {t('填入')}
  793. </Button>
  794. }
  795. placeholder={t('输入自定义模型名称')}
  796. value={customModel}
  797. onChange={(value) => setCustomModel(value.trim())}
  798. size="large"
  799. className="!rounded-lg"
  800. />
  801. </div>
  802. <div>
  803. <Text strong className="block mb-2">{t('模型重定向')}</Text>
  804. <TextArea
  805. placeholder={
  806. t('此项可选,用于修改请求体中的模型名称,为一个 JSON 字符串,键为请求中模型名称,值为要替换的模型名称,例如:') +
  807. `\n${JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2)}`
  808. }
  809. name='model_mapping'
  810. onChange={(value) => handleInputChange('model_mapping', value)}
  811. autosize
  812. value={inputs.model_mapping}
  813. autoComplete='new-password'
  814. className="!rounded-lg font-mono"
  815. />
  816. <Text
  817. className="!text-semi-color-primary cursor-pointer mt-1 block"
  818. onClick={() => handleInputChange('model_mapping', JSON.stringify(MODEL_MAPPING_EXAMPLE, null, 2))}
  819. >
  820. {t('填入模板')}
  821. </Text>
  822. </div>
  823. <div>
  824. <Text strong className="block mb-2">{t('默认测试模型')}</Text>
  825. <Input
  826. name='test_model'
  827. placeholder={t('不填则为模型列表第一个')}
  828. onChange={(value) => handleInputChange('test_model', value)}
  829. value={inputs.test_model}
  830. size="large"
  831. className="!rounded-lg"
  832. />
  833. </div>
  834. </div>
  835. </Card>
  836. {/* Advanced Settings Card */}
  837. <Card className="!rounded-2xl shadow-sm border-0 mb-6">
  838. <div className="flex items-center mb-4 p-6 rounded-xl" style={{
  839. background: 'linear-gradient(135deg, #92400e 0%, #d97706 50%, #f59e0b 100%)',
  840. position: 'relative'
  841. }}>
  842. <div className="absolute inset-0 overflow-hidden">
  843. <div className="absolute -top-10 -right-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
  844. <div className="absolute -bottom-8 -left-8 w-24 h-24 bg-white opacity-10 rounded-full"></div>
  845. </div>
  846. <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center mr-4 relative">
  847. <IconSetting size="large" style={{ color: '#ffffff' }} />
  848. </div>
  849. <div className="relative">
  850. <Text style={{ color: '#ffffff' }} className="text-lg font-medium">{t('高级设置')}</Text>
  851. <div style={{ color: '#ffffff' }} className="text-sm opacity-80">{t('渠道的高级配置选项')}</div>
  852. </div>
  853. </div>
  854. <div className="space-y-4">
  855. <div>
  856. <Text strong className="block mb-2">{t('分组')}</Text>
  857. <Select
  858. placeholder={t('请选择可以使用该渠道的分组')}
  859. name='groups'
  860. required
  861. multiple
  862. selection
  863. allowAdditions
  864. additionLabel={t('请在系统设置页面编辑分组倍率以添加新的分组:')}
  865. onChange={(value) => handleInputChange('groups', value)}
  866. value={inputs.groups}
  867. autoComplete='new-password'
  868. optionList={groupOptions}
  869. size="large"
  870. className="!rounded-lg"
  871. />
  872. </div>
  873. {inputs.type === 18 && (
  874. <div>
  875. <Text strong className="block mb-2">{t('模型版本')}</Text>
  876. <Input
  877. name='other'
  878. placeholder={'请输入星火大模型版本,注意是接口地址中的版本号,例如:v2.1'}
  879. onChange={(value) => handleInputChange('other', value)}
  880. value={inputs.other}
  881. autoComplete='new-password'
  882. size="large"
  883. className="!rounded-lg"
  884. />
  885. </div>
  886. )}
  887. {inputs.type === 41 && (
  888. <div>
  889. <Text strong className="block mb-2">{t('部署地区')}</Text>
  890. <TextArea
  891. name='other'
  892. placeholder={t(
  893. '请输入部署地区,例如:us-central1\n支持使用模型映射格式\n' +
  894. '{\n' +
  895. ' "default": "us-central1",\n' +
  896. ' "claude-3-5-sonnet-20240620": "europe-west1"\n' +
  897. '}'
  898. )}
  899. autosize={{ minRows: 2 }}
  900. onChange={(value) => handleInputChange('other', value)}
  901. value={inputs.other}
  902. autoComplete='new-password'
  903. className="!rounded-lg font-mono"
  904. />
  905. <Text
  906. className="!text-semi-color-primary cursor-pointer mt-1 block"
  907. onClick={() => handleInputChange('other', JSON.stringify(REGION_EXAMPLE, null, 2))}
  908. >
  909. {t('填入模板')}
  910. </Text>
  911. </div>
  912. )}
  913. {inputs.type === 21 && (
  914. <div>
  915. <Text strong className="block mb-2">{t('知识库 ID')}</Text>
  916. <Input
  917. name='other'
  918. placeholder={'请输入知识库 ID,例如:123456'}
  919. onChange={(value) => handleInputChange('other', value)}
  920. value={inputs.other}
  921. autoComplete='new-password'
  922. size="large"
  923. className="!rounded-lg"
  924. />
  925. </div>
  926. )}
  927. {inputs.type === 39 && (
  928. <div>
  929. <Text strong className="block mb-2">Account ID</Text>
  930. <Input
  931. name='other'
  932. placeholder={'请输入Account ID,例如:d6b5da8hk1awo8nap34ube6gh'}
  933. onChange={(value) => handleInputChange('other', value)}
  934. value={inputs.other}
  935. autoComplete='new-password'
  936. size="large"
  937. className="!rounded-lg"
  938. />
  939. </div>
  940. )}
  941. {inputs.type === 49 && (
  942. <div>
  943. <Text strong className="block mb-2">{t('智能体ID')}</Text>
  944. <Input
  945. name='other'
  946. placeholder={'请输入智能体ID,例如:7342866812345'}
  947. onChange={(value) => handleInputChange('other', value)}
  948. value={inputs.other}
  949. autoComplete='new-password'
  950. size="large"
  951. className="!rounded-lg"
  952. />
  953. </div>
  954. )}
  955. <div>
  956. <Text strong className="block mb-2">{t('渠道标签')}</Text>
  957. <Input
  958. name='tag'
  959. placeholder={t('渠道标签')}
  960. onChange={(value) => handleInputChange('tag', value)}
  961. value={inputs.tag}
  962. autoComplete='new-password'
  963. size="large"
  964. className="!rounded-lg"
  965. />
  966. </div>
  967. <div>
  968. <Text strong className="block mb-2">{t('渠道优先级')}</Text>
  969. <Input
  970. name='priority'
  971. placeholder={t('渠道优先级')}
  972. onChange={(value) => {
  973. const number = parseInt(value);
  974. if (isNaN(number)) {
  975. handleInputChange('priority', value);
  976. } else {
  977. handleInputChange('priority', number);
  978. }
  979. }}
  980. value={inputs.priority}
  981. autoComplete='new-password'
  982. size="large"
  983. className="!rounded-lg"
  984. />
  985. </div>
  986. <div>
  987. <Text strong className="block mb-2">{t('渠道权重')}</Text>
  988. <Input
  989. name='weight'
  990. placeholder={t('渠道权重')}
  991. onChange={(value) => {
  992. const number = parseInt(value);
  993. if (isNaN(number)) {
  994. handleInputChange('weight', value);
  995. } else {
  996. handleInputChange('weight', number);
  997. }
  998. }}
  999. value={inputs.weight}
  1000. autoComplete='new-password'
  1001. size="large"
  1002. className="!rounded-lg"
  1003. />
  1004. </div>
  1005. <div>
  1006. <Text strong className="block mb-2">{t('渠道额外设置')}</Text>
  1007. <TextArea
  1008. placeholder={
  1009. t('此项可选,用于配置渠道特定设置,为一个 JSON 字符串,例如:') +
  1010. '\n{\n "force_format": true\n}'
  1011. }
  1012. name='setting'
  1013. onChange={(value) => handleInputChange('setting', value)}
  1014. autosize
  1015. value={inputs.setting}
  1016. autoComplete='new-password'
  1017. className="!rounded-lg font-mono"
  1018. />
  1019. <div className="flex gap-2 mt-1">
  1020. <Text
  1021. className="!text-semi-color-primary cursor-pointer"
  1022. onClick={() => {
  1023. handleInputChange(
  1024. 'setting',
  1025. JSON.stringify({ force_format: true }, null, 2),
  1026. );
  1027. }}
  1028. >
  1029. {t('填入模板')}
  1030. </Text>
  1031. <Text
  1032. className="!text-semi-color-primary cursor-pointer"
  1033. onClick={() => {
  1034. window.open(
  1035. 'https://github.com/QuantumNous/new-api/blob/main/docs/channel/other_setting.md',
  1036. );
  1037. }}
  1038. >
  1039. {t('设置说明')}
  1040. </Text>
  1041. </div>
  1042. </div>
  1043. <div>
  1044. <Text strong className="block mb-2">{t('参数覆盖')}</Text>
  1045. <TextArea
  1046. placeholder={
  1047. t('此项可选,用于覆盖请求参数。不支持覆盖 stream 参数。为一个 JSON 字符串,例如:') +
  1048. '\n{\n "temperature": 0\n}'
  1049. }
  1050. name='param_override'
  1051. onChange={(value) => handleInputChange('param_override', value)}
  1052. autosize
  1053. value={inputs.param_override}
  1054. autoComplete='new-password'
  1055. className="!rounded-lg font-mono"
  1056. />
  1057. </div>
  1058. {inputs.type === 1 && (
  1059. <div>
  1060. <Text strong className="block mb-2">{t('组织')}</Text>
  1061. <Input
  1062. name='openai_organization'
  1063. placeholder={t('请输入组织org-xxx')}
  1064. onChange={(value) => handleInputChange('openai_organization', value)}
  1065. value={inputs.openai_organization}
  1066. size="large"
  1067. className="!rounded-lg"
  1068. />
  1069. <Text type="tertiary" className="mt-1 text-xs">
  1070. {t('组织,可选,不填则为默认组织')}
  1071. </Text>
  1072. </div>
  1073. )}
  1074. <div className="flex items-center">
  1075. <Checkbox
  1076. checked={autoBan}
  1077. onChange={() => setAutoBan(!autoBan)}
  1078. />
  1079. <Text strong className="ml-2">
  1080. {t('是否自动禁用(仅当自动禁用开启时有效),关闭后不会自动禁用该渠道')}
  1081. </Text>
  1082. </div>
  1083. <div>
  1084. <Text strong className="block mb-2">
  1085. {t('状态码复写(仅影响本地判断,不修改返回到上游的状态码)')}
  1086. </Text>
  1087. <TextArea
  1088. placeholder={
  1089. t('此项可选,用于复写返回的状态码,比如将claude渠道的400错误复写为500(用于重试),请勿滥用该功能,例如:') +
  1090. '\n' +
  1091. JSON.stringify(STATUS_CODE_MAPPING_EXAMPLE, null, 2)
  1092. }
  1093. name='status_code_mapping'
  1094. onChange={(value) => handleInputChange('status_code_mapping', value)}
  1095. autosize
  1096. value={inputs.status_code_mapping}
  1097. autoComplete='new-password'
  1098. className="!rounded-lg font-mono"
  1099. />
  1100. <Text
  1101. className="!text-semi-color-primary cursor-pointer mt-1 block"
  1102. onClick={() => {
  1103. handleInputChange(
  1104. 'status_code_mapping',
  1105. JSON.stringify(STATUS_CODE_MAPPING_EXAMPLE, null, 2),
  1106. );
  1107. }}
  1108. >
  1109. {t('填入模板')}
  1110. </Text>
  1111. </div>
  1112. </div>
  1113. </Card>
  1114. </div>
  1115. </Spin>
  1116. <ImagePreview
  1117. src={modalImageUrl}
  1118. visible={isModalOpenurl}
  1119. onVisibleChange={(visible) => setIsModalOpenurl(visible)}
  1120. />
  1121. </SideSheet>
  1122. </>
  1123. );
  1124. };
  1125. export default EditChannel;