| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131 |
- /*
- Copyright (C) 2025 QuantumNous
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>.
- For commercial licensing, please contact support@quantumnous.com
- */
- import { useState, useEffect, useRef, useMemo } from 'react';
- import { useTranslation } from 'react-i18next';
- import {
- API,
- showError,
- showInfo,
- showSuccess,
- loadChannelModels,
- copy,
- } from '../../helpers';
- import {
- CHANNEL_OPTIONS,
- ITEMS_PER_PAGE,
- MODEL_TABLE_PAGE_SIZE,
- } from '../../constants';
- import { useIsMobile } from '../common/useIsMobile';
- import { useTableCompactMode } from '../common/useTableCompactMode';
- import { Modal } from '@douyinfe/semi-ui';
- export const useChannelsData = () => {
- const { t } = useTranslation();
- const isMobile = useIsMobile();
- // Basic states
- const [channels, setChannels] = useState([]);
- const [loading, setLoading] = useState(true);
- const [activePage, setActivePage] = useState(1);
- const [idSort, setIdSort] = useState(false);
- const [searching, setSearching] = useState(false);
- const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE);
- const [channelCount, setChannelCount] = useState(0);
- const [groupOptions, setGroupOptions] = useState([]);
- // UI states
- const [showEdit, setShowEdit] = useState(false);
- const [enableBatchDelete, setEnableBatchDelete] = useState(false);
- const [editingChannel, setEditingChannel] = useState({ id: undefined });
- const [showEditTag, setShowEditTag] = useState(false);
- const [editingTag, setEditingTag] = useState('');
- const [selectedChannels, setSelectedChannels] = useState([]);
- const [enableTagMode, setEnableTagMode] = useState(false);
- const [showBatchSetTag, setShowBatchSetTag] = useState(false);
- const [batchSetTagValue, setBatchSetTagValue] = useState('');
- const [compactMode, setCompactMode] = useTableCompactMode('channels');
- // Column visibility states
- const [visibleColumns, setVisibleColumns] = useState({});
- const [showColumnSelector, setShowColumnSelector] = useState(false);
- // Status filter
- const [statusFilter, setStatusFilter] = useState(
- localStorage.getItem('channel-status-filter') || 'all',
- );
- // Type tabs states
- const [activeTypeKey, setActiveTypeKey] = useState('all');
- const [typeCounts, setTypeCounts] = useState({});
- // Model test states
- const [showModelTestModal, setShowModelTestModal] = useState(false);
- const [currentTestChannel, setCurrentTestChannel] = useState(null);
- const [modelSearchKeyword, setModelSearchKeyword] = useState('');
- const [modelTestResults, setModelTestResults] = useState({});
- const [testingModels, setTestingModels] = useState(new Set());
- const [selectedModelKeys, setSelectedModelKeys] = useState([]);
- const [isBatchTesting, setIsBatchTesting] = useState(false);
- const [modelTablePage, setModelTablePage] = useState(1);
- const [selectedEndpointType, setSelectedEndpointType] = useState('');
- // 使用 ref 来避免闭包问题,类似旧版实现
- const shouldStopBatchTestingRef = useRef(false);
- // Multi-key management states
- const [showMultiKeyManageModal, setShowMultiKeyManageModal] = useState(false);
- const [currentMultiKeyChannel, setCurrentMultiKeyChannel] = useState(null);
- // Refs
- const requestCounter = useRef(0);
- const allSelectingRef = useRef(false);
- const [formApi, setFormApi] = useState(null);
- const formInitValues = {
- searchKeyword: '',
- searchGroup: '',
- searchModel: '',
- };
- // Column keys
- const COLUMN_KEYS = {
- ID: 'id',
- NAME: 'name',
- GROUP: 'group',
- TYPE: 'type',
- STATUS: 'status',
- RESPONSE_TIME: 'response_time',
- BALANCE: 'balance',
- PRIORITY: 'priority',
- WEIGHT: 'weight',
- OPERATE: 'operate',
- };
- // Initialize from localStorage
- useEffect(() => {
- const localIdSort = localStorage.getItem('id-sort') === 'true';
- const localPageSize =
- parseInt(localStorage.getItem('page-size')) || ITEMS_PER_PAGE;
- const localEnableTagMode =
- localStorage.getItem('enable-tag-mode') === 'true';
- const localEnableBatchDelete =
- localStorage.getItem('enable-batch-delete') === 'true';
- setIdSort(localIdSort);
- setPageSize(localPageSize);
- setEnableTagMode(localEnableTagMode);
- setEnableBatchDelete(localEnableBatchDelete);
- loadChannels(1, localPageSize, localIdSort, localEnableTagMode)
- .then()
- .catch((reason) => {
- showError(reason);
- });
- fetchGroups().then();
- loadChannelModels().then();
- }, []);
- // Column visibility management
- const getDefaultColumnVisibility = () => {
- return {
- [COLUMN_KEYS.ID]: true,
- [COLUMN_KEYS.NAME]: true,
- [COLUMN_KEYS.GROUP]: true,
- [COLUMN_KEYS.TYPE]: true,
- [COLUMN_KEYS.STATUS]: true,
- [COLUMN_KEYS.RESPONSE_TIME]: true,
- [COLUMN_KEYS.BALANCE]: true,
- [COLUMN_KEYS.PRIORITY]: true,
- [COLUMN_KEYS.WEIGHT]: true,
- [COLUMN_KEYS.OPERATE]: true,
- };
- };
- const initDefaultColumns = () => {
- const defaults = getDefaultColumnVisibility();
- setVisibleColumns(defaults);
- };
- // Load saved column preferences
- useEffect(() => {
- const savedColumns = localStorage.getItem('channels-table-columns');
- if (savedColumns) {
- try {
- const parsed = JSON.parse(savedColumns);
- const defaults = getDefaultColumnVisibility();
- const merged = { ...defaults, ...parsed };
- setVisibleColumns(merged);
- } catch (e) {
- console.error('Failed to parse saved column preferences', e);
- initDefaultColumns();
- }
- } else {
- initDefaultColumns();
- }
- }, []);
- // Save column preferences
- useEffect(() => {
- if (Object.keys(visibleColumns).length > 0) {
- localStorage.setItem(
- 'channels-table-columns',
- JSON.stringify(visibleColumns),
- );
- }
- }, [visibleColumns]);
- const handleColumnVisibilityChange = (columnKey, checked) => {
- const updatedColumns = { ...visibleColumns, [columnKey]: checked };
- setVisibleColumns(updatedColumns);
- };
- const handleSelectAll = (checked) => {
- const allKeys = Object.keys(COLUMN_KEYS).map((key) => COLUMN_KEYS[key]);
- const updatedColumns = {};
- allKeys.forEach((key) => {
- updatedColumns[key] = checked;
- });
- setVisibleColumns(updatedColumns);
- };
- // Data formatting
- const setChannelFormat = (channels, enableTagMode) => {
- let channelDates = [];
- let channelTags = {};
- for (let i = 0; i < channels.length; i++) {
- channels[i].key = '' + channels[i].id;
- if (!enableTagMode) {
- channelDates.push(channels[i]);
- } else {
- let tag = channels[i].tag ? channels[i].tag : '';
- let tagIndex = channelTags[tag];
- let tagChannelDates = undefined;
- if (tagIndex === undefined) {
- channelTags[tag] = 1;
- tagChannelDates = {
- key: tag,
- id: tag,
- tag: tag,
- name: '标签:' + tag,
- group: '',
- used_quota: 0,
- response_time: 0,
- priority: -1,
- weight: -1,
- };
- tagChannelDates.children = [];
- channelDates.push(tagChannelDates);
- } else {
- tagChannelDates = channelDates.find((item) => item.key === tag);
- }
- if (tagChannelDates.priority === -1) {
- tagChannelDates.priority = channels[i].priority;
- } else {
- if (tagChannelDates.priority !== channels[i].priority) {
- tagChannelDates.priority = '';
- }
- }
- if (tagChannelDates.weight === -1) {
- tagChannelDates.weight = channels[i].weight;
- } else {
- if (tagChannelDates.weight !== channels[i].weight) {
- tagChannelDates.weight = '';
- }
- }
- if (tagChannelDates.group === '') {
- tagChannelDates.group = channels[i].group;
- } else {
- let channelGroupsStr = channels[i].group;
- channelGroupsStr.split(',').forEach((item, index) => {
- if (tagChannelDates.group.indexOf(item) === -1) {
- tagChannelDates.group += ',' + item;
- }
- });
- }
- tagChannelDates.children.push(channels[i]);
- if (channels[i].status === 1) {
- tagChannelDates.status = 1;
- }
- tagChannelDates.used_quota += channels[i].used_quota;
- tagChannelDates.response_time += channels[i].response_time;
- tagChannelDates.response_time = tagChannelDates.response_time / 2;
- }
- }
- setChannels(channelDates);
- };
- // Get form values helper
- const getFormValues = () => {
- const formValues = formApi ? formApi.getValues() : {};
- return {
- searchKeyword: formValues.searchKeyword || '',
- searchGroup: formValues.searchGroup || '',
- searchModel: formValues.searchModel || '',
- };
- };
- // Load channels
- const loadChannels = async (
- page,
- pageSize,
- idSort,
- enableTagMode,
- typeKey = activeTypeKey,
- statusF,
- ) => {
- if (statusF === undefined) statusF = statusFilter;
- const { searchKeyword, searchGroup, searchModel } = getFormValues();
- if (searchKeyword !== '' || searchGroup !== '' || searchModel !== '') {
- setLoading(true);
- await searchChannels(
- enableTagMode,
- typeKey,
- statusF,
- page,
- pageSize,
- idSort,
- );
- setLoading(false);
- return;
- }
- const reqId = ++requestCounter.current;
- setLoading(true);
- const typeParam = typeKey !== 'all' ? `&type=${typeKey}` : '';
- const statusParam = statusF !== 'all' ? `&status=${statusF}` : '';
- const res = await API.get(
- `/api/channel/?p=${page}&page_size=${pageSize}&id_sort=${idSort}&tag_mode=${enableTagMode}${typeParam}${statusParam}`,
- );
- if (res === undefined || reqId !== requestCounter.current) {
- return;
- }
- const { success, message, data } = res.data;
- if (success) {
- const { items, total, type_counts } = data;
- if (type_counts) {
- const sumAll = Object.values(type_counts).reduce(
- (acc, v) => acc + v,
- 0,
- );
- setTypeCounts({ ...type_counts, all: sumAll });
- }
- setChannelFormat(items, enableTagMode);
- setChannelCount(total);
- } else {
- showError(message);
- }
- setLoading(false);
- };
- // Search channels
- const searchChannels = async (
- enableTagMode,
- typeKey = activeTypeKey,
- statusF = statusFilter,
- page = 1,
- pageSz = pageSize,
- sortFlag = idSort,
- ) => {
- const { searchKeyword, searchGroup, searchModel } = getFormValues();
- setSearching(true);
- try {
- if (searchKeyword === '' && searchGroup === '' && searchModel === '') {
- await loadChannels(
- page,
- pageSz,
- sortFlag,
- enableTagMode,
- typeKey,
- statusF,
- );
- return;
- }
- const typeParam = typeKey !== 'all' ? `&type=${typeKey}` : '';
- const statusParam = statusF !== 'all' ? `&status=${statusF}` : '';
- const res = await API.get(
- `/api/channel/search?keyword=${searchKeyword}&group=${searchGroup}&model=${searchModel}&id_sort=${sortFlag}&tag_mode=${enableTagMode}&p=${page}&page_size=${pageSz}${typeParam}${statusParam}`,
- );
- const { success, message, data } = res.data;
- if (success) {
- const { items = [], total = 0, type_counts = {} } = data;
- const sumAll = Object.values(type_counts).reduce(
- (acc, v) => acc + v,
- 0,
- );
- setTypeCounts({ ...type_counts, all: sumAll });
- setChannelFormat(items, enableTagMode);
- setChannelCount(total);
- setActivePage(page);
- } else {
- showError(message);
- }
- } finally {
- setSearching(false);
- }
- };
- // Refresh
- const refresh = async (page = activePage) => {
- const { searchKeyword, searchGroup, searchModel } = getFormValues();
- if (searchKeyword === '' && searchGroup === '' && searchModel === '') {
- await loadChannels(page, pageSize, idSort, enableTagMode);
- } else {
- await searchChannels(
- enableTagMode,
- activeTypeKey,
- statusFilter,
- page,
- pageSize,
- idSort,
- );
- }
- };
- // Channel management
- const manageChannel = async (id, action, record, value) => {
- let data = { id };
- let res;
- switch (action) {
- case 'delete':
- res = await API.delete(`/api/channel/${id}/`);
- break;
- case 'enable':
- data.status = 1;
- res = await API.put('/api/channel/', data);
- break;
- case 'disable':
- data.status = 2;
- res = await API.put('/api/channel/', data);
- break;
- case 'priority':
- if (value === '') return;
- data.priority = parseInt(value);
- res = await API.put('/api/channel/', data);
- break;
- case 'weight':
- if (value === '') return;
- data.weight = parseInt(value);
- if (data.weight < 0) data.weight = 0;
- res = await API.put('/api/channel/', data);
- break;
- case 'enable_all':
- data.channel_info = record.channel_info;
- data.channel_info.multi_key_status_list = {};
- res = await API.put('/api/channel/', data);
- break;
- }
- const { success, message } = res.data;
- if (success) {
- showSuccess(t('操作成功完成!'));
- let channel = res.data.data;
- let newChannels = [...channels];
- if (action !== 'delete') {
- record.status = channel.status;
- }
- setChannels(newChannels);
- } else {
- showError(message);
- }
- };
- // Tag management
- const manageTag = async (tag, action) => {
- let res;
- switch (action) {
- case 'enable':
- res = await API.post('/api/channel/tag/enabled', { tag: tag });
- break;
- case 'disable':
- res = await API.post('/api/channel/tag/disabled', { tag: tag });
- break;
- }
- const { success, message } = res.data;
- if (success) {
- showSuccess('操作成功完成!');
- let newChannels = [...channels];
- for (let i = 0; i < newChannels.length; i++) {
- if (newChannels[i].tag === tag) {
- let status = action === 'enable' ? 1 : 2;
- newChannels[i]?.children?.forEach((channel) => {
- channel.status = status;
- });
- newChannels[i].status = status;
- }
- }
- setChannels(newChannels);
- } else {
- showError(message);
- }
- };
- // Page handlers
- const handlePageChange = (page) => {
- const { searchKeyword, searchGroup, searchModel } = getFormValues();
- setActivePage(page);
- if (searchKeyword === '' && searchGroup === '' && searchModel === '') {
- loadChannels(page, pageSize, idSort, enableTagMode).then(() => {});
- } else {
- searchChannels(
- enableTagMode,
- activeTypeKey,
- statusFilter,
- page,
- pageSize,
- idSort,
- );
- }
- };
- const handlePageSizeChange = async (size) => {
- localStorage.setItem('page-size', size + '');
- setPageSize(size);
- setActivePage(1);
- const { searchKeyword, searchGroup, searchModel } = getFormValues();
- if (searchKeyword === '' && searchGroup === '' && searchModel === '') {
- loadChannels(1, size, idSort, enableTagMode)
- .then()
- .catch((reason) => {
- showError(reason);
- });
- } else {
- searchChannels(
- enableTagMode,
- activeTypeKey,
- statusFilter,
- 1,
- size,
- idSort,
- );
- }
- };
- // Fetch groups
- const fetchGroups = async () => {
- try {
- let res = await API.get(`/api/group/`);
- if (res === undefined) return;
- setGroupOptions(
- res.data.data.map((group) => ({
- label: group,
- value: group,
- })),
- );
- } catch (error) {
- showError(error.message);
- }
- };
- // Copy channel
- const copySelectedChannel = async (record) => {
- try {
- const res = await API.post(`/api/channel/copy/${record.id}`);
- if (res?.data?.success) {
- showSuccess(t('渠道复制成功'));
- await refresh();
- } else {
- showError(res?.data?.message || t('渠道复制失败'));
- }
- } catch (error) {
- showError(
- t('渠道复制失败: ') +
- (error?.response?.data?.message || error?.message || error),
- );
- }
- };
- // Update channel property
- const updateChannelProperty = (channelId, updateFn) => {
- const newChannels = [...channels];
- let updated = false;
- newChannels.forEach((channel) => {
- if (channel.children !== undefined) {
- channel.children.forEach((child) => {
- if (child.id === channelId) {
- updateFn(child);
- updated = true;
- }
- });
- } else if (channel.id === channelId) {
- updateFn(channel);
- updated = true;
- }
- });
- if (updated) {
- setChannels(newChannels);
- }
- };
- // Tag edit
- const submitTagEdit = async (type, data) => {
- switch (type) {
- case 'priority':
- if (data.priority === undefined || data.priority === '') {
- showInfo('优先级必须是整数!');
- return;
- }
- data.priority = parseInt(data.priority);
- break;
- case 'weight':
- if (
- data.weight === undefined ||
- data.weight < 0 ||
- data.weight === ''
- ) {
- showInfo('权重必须是非负整数!');
- return;
- }
- data.weight = parseInt(data.weight);
- break;
- }
- try {
- const res = await API.put('/api/channel/tag', data);
- if (res?.data?.success) {
- showSuccess('更新成功!');
- await refresh();
- }
- } catch (error) {
- showError(error);
- }
- };
- // Close edit
- const closeEdit = () => {
- setShowEdit(false);
- };
- // Row style
- const handleRow = (record, index) => {
- if (record.status !== 1) {
- return {
- style: {
- background: 'var(--semi-color-disabled-border)',
- },
- };
- } else {
- return {};
- }
- };
- // Batch operations
- const batchSetChannelTag = async () => {
- if (selectedChannels.length === 0) {
- showError(t('请先选择要设置标签的渠道!'));
- return;
- }
- if (batchSetTagValue === '') {
- showError(t('标签不能为空!'));
- return;
- }
- let ids = selectedChannels.map((channel) => channel.id);
- const res = await API.post('/api/channel/batch/tag', {
- ids: ids,
- tag: batchSetTagValue === '' ? null : batchSetTagValue,
- });
- if (res.data.success) {
- showSuccess(
- t('已为 ${count} 个渠道设置标签!').replace('${count}', res.data.data),
- );
- await refresh();
- setShowBatchSetTag(false);
- } else {
- showError(res.data.message);
- }
- };
- const batchDeleteChannels = async () => {
- if (selectedChannels.length === 0) {
- showError(t('请先选择要删除的通道!'));
- return;
- }
- setLoading(true);
- let ids = [];
- selectedChannels.forEach((channel) => {
- ids.push(channel.id);
- });
- const res = await API.post(`/api/channel/batch`, { ids: ids });
- const { success, message, data } = res.data;
- if (success) {
- showSuccess(t('已删除 ${data} 个通道!').replace('${data}', data));
- await refresh();
- setTimeout(() => {
- if (channels.length === 0 && activePage > 1) {
- refresh(activePage - 1);
- }
- }, 100);
- } else {
- showError(message);
- }
- setLoading(false);
- };
- // Channel operations
- const testAllChannels = async () => {
- const res = await API.get(`/api/channel/test`);
- const { success, message } = res.data;
- if (success) {
- showInfo(t('已成功开始测试所有已启用通道,请刷新页面查看结果。'));
- } else {
- showError(message);
- }
- };
- const deleteAllDisabledChannels = async () => {
- const res = await API.delete(`/api/channel/disabled`);
- const { success, message, data } = res.data;
- if (success) {
- showSuccess(
- t('已删除所有禁用渠道,共计 ${data} 个').replace('${data}', data),
- );
- await refresh();
- } else {
- showError(message);
- }
- };
- const updateAllChannelsBalance = async () => {
- const res = await API.get(`/api/channel/update_balance`);
- const { success, message } = res.data;
- if (success) {
- showInfo(t('已更新完毕所有已启用通道余额!'));
- } else {
- showError(message);
- }
- };
- const updateChannelBalance = async (record) => {
- const res = await API.get(`/api/channel/update_balance/${record.id}/`);
- const { success, message, balance } = res.data;
- if (success) {
- updateChannelProperty(record.id, (channel) => {
- channel.balance = balance;
- channel.balance_updated_time = Date.now() / 1000;
- });
- showInfo(
- t('通道 ${name} 余额更新成功!').replace('${name}', record.name),
- );
- } else {
- showError(message);
- }
- };
- const fixChannelsAbilities = async () => {
- const res = await API.post(`/api/channel/fix`);
- const { success, message, data } = res.data;
- if (success) {
- showSuccess(
- t('已修复 ${success} 个通道,失败 ${fails} 个通道。')
- .replace('${success}', data.success)
- .replace('${fails}', data.fails),
- );
- await refresh();
- } else {
- showError(message);
- }
- };
- // Test channel - 单个模型测试,参考旧版实现
- const testChannel = async (record, model, endpointType = '') => {
- const testKey = `${record.id}-${model}`;
- // 检查是否应该停止批量测试
- if (shouldStopBatchTestingRef.current && isBatchTesting) {
- return Promise.resolve();
- }
- // 添加到正在测试的模型集合
- setTestingModels((prev) => new Set([...prev, model]));
- try {
- let url = `/api/channel/test/${record.id}?model=${model}`;
- if (endpointType) {
- url += `&endpoint_type=${endpointType}`;
- }
- const res = await API.get(url);
- // 检查是否在请求期间被停止
- if (shouldStopBatchTestingRef.current && isBatchTesting) {
- return Promise.resolve();
- }
- const { success, message, time } = res.data;
- // 更新测试结果
- setModelTestResults((prev) => ({
- ...prev,
- [testKey]: {
- success,
- message,
- time: time || 0,
- timestamp: Date.now(),
- },
- }));
- if (success) {
- // 更新渠道响应时间
- updateChannelProperty(record.id, (channel) => {
- channel.response_time = time * 1000;
- channel.test_time = Date.now() / 1000;
- });
- if (!model || model === '') {
- showInfo(
- t('通道 ${name} 测试成功,耗时 ${time.toFixed(2)} 秒。')
- .replace('${name}', record.name)
- .replace('${time.toFixed(2)}', time.toFixed(2)),
- );
- } else {
- showInfo(
- t(
- '通道 ${name} 测试成功,模型 ${model} 耗时 ${time.toFixed(2)} 秒。',
- )
- .replace('${name}', record.name)
- .replace('${model}', model)
- .replace('${time.toFixed(2)}', time.toFixed(2)),
- );
- }
- } else {
- showError(`${t('模型')} ${model}: ${message}`);
- }
- } catch (error) {
- // 处理网络错误
- const testKey = `${record.id}-${model}`;
- setModelTestResults((prev) => ({
- ...prev,
- [testKey]: {
- success: false,
- message: error.message || t('网络错误'),
- time: 0,
- timestamp: Date.now(),
- },
- }));
- showError(`${t('模型')} ${model}: ${error.message || t('测试失败')}`);
- } finally {
- // 从正在测试的模型集合中移除
- setTestingModels((prev) => {
- const newSet = new Set(prev);
- newSet.delete(model);
- return newSet;
- });
- }
- };
- // 批量测试单个渠道的所有模型,参考旧版实现
- const batchTestModels = async () => {
- if (!currentTestChannel || !currentTestChannel.models) {
- showError(t('渠道模型信息不完整'));
- return;
- }
- const models = currentTestChannel.models
- .split(',')
- .filter((model) =>
- model.toLowerCase().includes(modelSearchKeyword.toLowerCase()),
- );
- if (models.length === 0) {
- showError(t('没有找到匹配的模型'));
- return;
- }
- setIsBatchTesting(true);
- shouldStopBatchTestingRef.current = false; // 重置停止标志
- // 清空该渠道之前的测试结果
- setModelTestResults((prev) => {
- const newResults = { ...prev };
- models.forEach((model) => {
- const testKey = `${currentTestChannel.id}-${model}`;
- delete newResults[testKey];
- });
- return newResults;
- });
- try {
- showInfo(
- t('开始批量测试 ${count} 个模型,已清空上次结果...').replace(
- '${count}',
- models.length,
- ),
- );
- // 提高并发数量以加快测试速度,参考旧版的并发限制
- const concurrencyLimit = 5;
- const results = [];
- for (let i = 0; i < models.length; i += concurrencyLimit) {
- // 检查是否应该停止
- if (shouldStopBatchTestingRef.current) {
- showInfo(t('批量测试已停止'));
- break;
- }
- const batch = models.slice(i, i + concurrencyLimit);
- showInfo(
- t('正在测试第 ${current} - ${end} 个模型 (共 ${total} 个)')
- .replace('${current}', i + 1)
- .replace('${end}', Math.min(i + concurrencyLimit, models.length))
- .replace('${total}', models.length),
- );
- const batchPromises = batch.map((model) =>
- testChannel(currentTestChannel, model, selectedEndpointType),
- );
- const batchResults = await Promise.allSettled(batchPromises);
- results.push(...batchResults);
- // 再次检查是否应该停止
- if (shouldStopBatchTestingRef.current) {
- showInfo(t('批量测试已停止'));
- break;
- }
- // 短暂延迟避免过于频繁的请求
- if (i + concurrencyLimit < models.length) {
- await new Promise((resolve) => setTimeout(resolve, 100));
- }
- }
- if (!shouldStopBatchTestingRef.current) {
- // 等待一小段时间确保所有结果都已更新
- await new Promise((resolve) => setTimeout(resolve, 300));
- // 使用当前状态重新计算结果统计
- setModelTestResults((currentResults) => {
- let successCount = 0;
- let failCount = 0;
- models.forEach((model) => {
- const testKey = `${currentTestChannel.id}-${model}`;
- const result = currentResults[testKey];
- if (result && result.success) {
- successCount++;
- } else {
- failCount++;
- }
- });
- // 显示完成消息
- setTimeout(() => {
- showSuccess(
- t('批量测试完成!成功: ${success}, 失败: ${fail}, 总计: ${total}')
- .replace('${success}', successCount)
- .replace('${fail}', failCount)
- .replace('${total}', models.length),
- );
- }, 100);
- return currentResults; // 不修改状态,只是为了获取最新值
- });
- }
- } catch (error) {
- showError(t('批量测试过程中发生错误: ') + error.message);
- } finally {
- setIsBatchTesting(false);
- }
- };
- // 停止批量测试
- const stopBatchTesting = () => {
- shouldStopBatchTestingRef.current = true;
- setIsBatchTesting(false);
- setTestingModels(new Set());
- showInfo(t('已停止批量测试'));
- };
- // 清空测试结果
- const clearTestResults = () => {
- setModelTestResults({});
- showInfo(t('已清空测试结果'));
- };
- // Handle close modal
- const handleCloseModal = () => {
- // 如果正在批量测试,先停止测试
- if (isBatchTesting) {
- shouldStopBatchTestingRef.current = true;
- showInfo(t('关闭弹窗,已停止批量测试'));
- }
- setShowModelTestModal(false);
- setModelSearchKeyword('');
- setIsBatchTesting(false);
- setTestingModels(new Set());
- setSelectedModelKeys([]);
- setModelTablePage(1);
- setSelectedEndpointType('');
- // 可选择性保留测试结果,这里不清空以便用户查看
- };
- // Type counts
- const channelTypeCounts = useMemo(() => {
- if (Object.keys(typeCounts).length > 0) return typeCounts;
- const counts = { all: channels.length };
- channels.forEach((channel) => {
- const collect = (ch) => {
- const type = ch.type;
- counts[type] = (counts[type] || 0) + 1;
- };
- if (channel.children !== undefined) {
- channel.children.forEach(collect);
- } else {
- collect(channel);
- }
- });
- return counts;
- }, [typeCounts, channels]);
- const availableTypeKeys = useMemo(() => {
- const keys = ['all'];
- Object.entries(channelTypeCounts).forEach(([k, v]) => {
- if (k !== 'all' && v > 0) keys.push(String(k));
- });
- return keys;
- }, [channelTypeCounts]);
- return {
- // Basic states
- channels,
- loading,
- searching,
- activePage,
- pageSize,
- channelCount,
- groupOptions,
- idSort,
- enableTagMode,
- enableBatchDelete,
- statusFilter,
- compactMode,
- // UI states
- showEdit,
- setShowEdit,
- editingChannel,
- setEditingChannel,
- showEditTag,
- setShowEditTag,
- editingTag,
- setEditingTag,
- selectedChannels,
- setSelectedChannels,
- showBatchSetTag,
- setShowBatchSetTag,
- batchSetTagValue,
- setBatchSetTagValue,
- // Column states
- visibleColumns,
- showColumnSelector,
- setShowColumnSelector,
- COLUMN_KEYS,
- // Type tab states
- activeTypeKey,
- setActiveTypeKey,
- typeCounts,
- channelTypeCounts,
- availableTypeKeys,
- // Model test states
- showModelTestModal,
- setShowModelTestModal,
- currentTestChannel,
- setCurrentTestChannel,
- modelSearchKeyword,
- setModelSearchKeyword,
- modelTestResults,
- testingModels,
- selectedModelKeys,
- setSelectedModelKeys,
- isBatchTesting,
- modelTablePage,
- setModelTablePage,
- selectedEndpointType,
- setSelectedEndpointType,
- allSelectingRef,
- // Multi-key management states
- showMultiKeyManageModal,
- setShowMultiKeyManageModal,
- currentMultiKeyChannel,
- setCurrentMultiKeyChannel,
- // Form
- formApi,
- setFormApi,
- formInitValues,
- // Helpers
- t,
- isMobile,
- // Functions
- loadChannels,
- searchChannels,
- refresh,
- manageChannel,
- manageTag,
- handlePageChange,
- handlePageSizeChange,
- copySelectedChannel,
- updateChannelProperty,
- submitTagEdit,
- closeEdit,
- handleRow,
- batchSetChannelTag,
- batchDeleteChannels,
- testAllChannels,
- deleteAllDisabledChannels,
- updateAllChannelsBalance,
- updateChannelBalance,
- fixChannelsAbilities,
- testChannel,
- batchTestModels,
- handleCloseModal,
- getFormValues,
- // Column functions
- handleColumnVisibilityChange,
- handleSelectAll,
- initDefaultColumns,
- getDefaultColumnVisibility,
- // Setters
- setIdSort,
- setEnableTagMode,
- setEnableBatchDelete,
- setStatusFilter,
- setCompactMode,
- setActivePage,
- };
- };
|