EditChannel.js 41 KB

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