EditChannel.js 40 KB

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