Files
monkey-ai-web/src/components/common/ModelSelector.jsx
T
qiubaishen 43e7fbc20e merge: 同步 origin/ui-optimization(d224d5b6),保留 weiyuan 功能与 Semi 迁移
- 冲突以 weiyuan 功能(小程序登录、i18n 文案)为准,并保留远端 lint 重构与依赖收敛
- 删除 weiyuan 已移除、远端仅做 lint 修正的 3 个图片相关文件
- 适配远端零警告 ESLint:Monitoring/Subscription 改用 useEffectEvent,EditChannelModal 注明 t 依赖
- effect-events 单测补充 react-i18next mock
- 对合并后仍不符合 Prettier 的文件做纯格式化
2026-11-10 14:59:34 +08:00

177 lines
5.2 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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 React, { useEffect, useRef } from 'react';
import { Select, Spin, Typography, Empty, Tag, Tooltip } from '@/components/ui';
import { Lock } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { useModelSelector } from '../../hooks/models/useModelSelector';
import { MODEL_CATEGORY_NAMES } from '../../constants';
const { Text } = Typography;
/**
* 模型选择器组件
* @param {string} category - 模型分类(可选)
* @param {string} value - 当前选中的模型ID或名称
* @param {function} onChange - 模型变化回调
* @param {string} placeholder - 占位文本
* @param {boolean} disabled - 是否禁用
* @param {string} size - 尺寸
*/
const ModelSelector = ({
category = null,
value,
onChange,
placeholder,
disabled = false,
size = 'default',
showCategoryLabel = false,
// 加载完成后向父组件回传模型列表(用于父组件按 name 反查 model 对象,避免重复 fetch)
onModelsLoaded,
// 是否在初次加载完成后自动选中第一个模型(外部需要"先回填再生成"时可关闭)
autoSelectFirst = true,
}) => {
const { t } = useTranslation();
const { models, loading, selectedModel, setSelectedModel } =
useModelSelector(category);
const isInitializedRef = useRef(false);
// 当模型加载完成时,自动选择第一个「可调用」模型(避免默认选中锁定模型)
useEffect(() => {
if (!loading && models.length > 0 && !isInitializedRef.current) {
if (autoSelectFirst) {
const firstModel = models.find((m) => !m.level_locked);
setSelectedModel(firstModel || null);
if (firstModel && onChange) {
onChange(firstModel);
}
}
isInitializedRef.current = true;
}
}, [loading, models, setSelectedModel, onChange, autoSelectFirst]);
// 把加载好的模型列表传给父组件
useEffect(() => {
if (!onModelsLoaded) return;
if (loading) return;
onModelsLoaded(models);
}, [models, loading, onModelsLoaded]);
// 处理模型选择:锁定(仅可见)模型不可选中
const handleChange = (selectedValue) => {
const model = models.find(
(m) => m.id === selectedValue || m.model_name === selectedValue,
);
if (model && !model.level_locked) {
setSelectedModel(model);
if (onChange) {
onChange(model);
}
}
};
// 渲染模型选项:level_state=visible 展示锁态并禁用
const renderOption = (model) => {
const locked = model.level_locked;
return (
<Select.Option
key={model.id}
value={model.id}
disabled={locked}
showTick={!locked}
>
<div style={{ display: 'flex', alignItems: 'center' }}>
<Text strong type={locked ? 'tertiary' : undefined}>
{model.model_name}
</Text>
{model.vendor && (
<Text type='secondary' size='small' style={{ marginLeft: '8px' }}>
({model.vendor})
</Text>
)}
{locked && (
<Tooltip content={t('当前等级无权调用此模型,升级等级后可用')}>
<Tag
size='small'
color='grey'
prefixIcon={<Lock size={12} />}
style={{ marginLeft: '8px' }}
>
{t('仅可见')}
</Tag>
</Tooltip>
)}
</div>
</Select.Option>
);
};
if (loading) {
return (
<div style={{ textAlign: 'center', padding: '20px' }}>
<Spin />
</div>
);
}
if (models.length === 0) {
return (
<Empty
title={t('暂无可用模型')}
description={
category
? t('{{category}}类模型暂无可用', {
category: MODEL_CATEGORY_NAMES[category],
})
: t('请先添加模型')
}
/>
);
}
return (
<div>
{showCategoryLabel && category && (
<Text
type='secondary'
size='small'
style={{ display: 'block', marginBottom: '4px' }}
>
{MODEL_CATEGORY_NAMES[category]}
</Text>
)}
<Select
value={value || selectedModel?.id}
onChange={handleChange}
placeholder={placeholder || t('请选择模型')}
disabled={disabled}
size={size}
style={{ width: '100%' }}
filter
showClear
>
{models.map(renderOption)}
</Select>
</div>
);
};
export default ModelSelector;