Files
monkey-ai-web/src/components/playground/SettingsPanel.jsx
T

432 lines
17 KiB
React

/*
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 { X as ActionX } from '@/components/common/icons/ActionIcons';
import React from 'react';
import { Select, Typography, Button, Switch } from '@/components/ui';
import { Users, ToggleLeft, Settings, Sliders, Bot, Zap } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import {
renderGroupOption,
renderModelOption,
selectFilter,
} from '../../helpers';
import ParameterControl from './ParameterControl';
import ImageUrlInput from './ImageUrlInput';
import ConfigManager from './ConfigManager';
import CustomRequestEditor from './CustomRequestEditor';
const SettingsPanel = ({
inputs,
parameterEnabled,
models,
groups,
styleState,
showDebugPanel,
customRequestMode,
customRequestBody,
onInputChange,
onParameterToggle,
onCloseSettings,
onConfigImport,
onConfigReset,
onCustomRequestModeChange,
onCustomRequestBodyChange,
previewPayload,
messages,
}) => {
const { t } = useTranslation();
const currentConfig = {
inputs,
parameterEnabled,
showDebugPanel,
customRequestMode,
customRequestBody,
};
const isExperienceMode = styleState?.isExperienceMode;
// 设置项卡片组件
const SettingCard = ({ icon: Icon, title, children, disabled, gradient }) => (
<div className={`relative ${disabled ? 'opacity-50' : ''}`}>
{isExperienceMode && (
<div className='flex items-center gap-2 mb-2.5'>
<div
className={`w-6 h-6 rounded-lg bg-gradient-to-br ${gradient} flex items-center justify-center shadow-sm`}
>
<Icon size={12} className='text-white' />
</div>
<Typography.Text
strong
className='text-sm text-gray-700 dark:text-gray-200'
>
{title}
</Typography.Text>
</div>
)}
{!isExperienceMode && (
<div className='flex items-center gap-2 mb-2'>
<Icon size={16} className='text-gray-500' />
<Typography.Text strong className='text-sm'>
{title}
</Typography.Text>
{customRequestMode && disabled && (
<Typography.Text className='text-xs text-orange-600'>
({t('已在自定义模式中忽略')})
</Typography.Text>
)}
</div>
)}
{children}
</div>
);
return (
<div
className={`h-full flex flex-col ${isExperienceMode ? 'bg-gradient-to-b from-white to-gray-50/50 dark:from-neutral-900 dark:to-neutral-950/50 border-r border-gray-100 dark:border-neutral-800' : 'bg-transparent'}`}
style={{
padding: styleState.isMobile
? '16px'
: isExperienceMode
? '20px'
: '24px',
height: '100%',
display: 'flex',
flexDirection: 'column',
}}
>
{/* 标题区域 - 非体验模式显示 */}
{!isExperienceMode && (
<div className='flex items-center justify-between mb-6 flex-shrink-0'>
<div className='flex items-center'>
<div className='w-10 h-10 rounded-full bg-gradient-to-r from-brand-from to-pink-500 flex items-center justify-center mr-3'>
<Settings size={20} className='text-white' />
</div>
<Typography.Title heading={5} className='mb-0'>
{t('模型配置')}
</Typography.Title>
</div>
{styleState.isMobile && onCloseSettings && (
<Button
icon={<ActionX size={16} />}
onClick={onCloseSettings}
theme='borderless'
type='tertiary'
size='small'
className='!rounded-lg'
/>
)}
</div>
)}
{/* 体验模式下的美化标题 */}
{isExperienceMode && !styleState.isMobile && (
<div className='mb-6 flex-shrink-0'>
<div className='flex items-center gap-3 mb-2'>
<div className='w-10 h-10 rounded-xl bg-gradient-to-br from-brand-from to-brand-to flex items-center justify-center shadow-lg shadow-brand/20'>
<Sliders size={18} className='text-white' />
</div>
<div>
<Typography.Title
heading={5}
style={{ fontWeight: 600, marginBottom: 0 }}
>
{t('模型配置')}
</Typography.Title>
<Typography.Text className='text-xs text-gray-400 dark:text-gray-500'>
{t('自定义对话参数')}
</Typography.Text>
</div>
</div>
{/* 装饰性分隔线 */}
<div className='h-px bg-gradient-to-r from-brand-from/30 via-brand-via/20 to-transparent mt-4' />
</div>
)}
{/* 移动端配置管理 */}
{styleState.isMobile && (
<div className='mb-4 flex-shrink-0'>
<ConfigManager
currentConfig={currentConfig}
onConfigImport={onConfigImport}
onConfigReset={onConfigReset}
styleState={{ ...styleState, isMobile: false }}
messages={messages}
/>
</div>
)}
<div
className={`space-y-4 overflow-y-auto flex-1 pr-2 model-settings-scroll ${isExperienceMode ? 'px-1' : ''}`}
>
{/* 自定义请求体编辑器 - 体验模式隐藏 */}
{!isExperienceMode && (
<CustomRequestEditor
customRequestMode={customRequestMode}
customRequestBody={customRequestBody}
onCustomRequestModeChange={onCustomRequestModeChange}
onCustomRequestBodyChange={onCustomRequestBodyChange}
defaultPayload={previewPayload}
/>
)}
{/* 分组选择 - 体验模式隐藏 */}
{!isExperienceMode && (
<SettingCard
icon={Users}
title={t('分组')}
disabled={customRequestMode}
gradient='from-blue-500 to-cyan-500'
>
<Select
placeholder={t('请选择分组')}
name='group'
required
selection
filter={selectFilter}
autoClearSearchValue={false}
onChange={(value) => onInputChange('group', value)}
value={inputs.group}
autoComplete='new-password'
optionList={groups}
renderOptionItem={renderGroupOption}
style={{ width: '100%' }}
dropdownStyle={{ width: '100%', maxWidth: '100%' }}
className='!rounded-xl'
disabled={customRequestMode}
/>
</SettingCard>
)}
{/* 模型选择 - 体验模式下使用增强样式 */}
{isExperienceMode ? (
<div className='group relative'>
{/* 外发光效果 */}
<div className='absolute -inset-0.5 bg-gradient-to-r from-brand-from/30 via-brand-via/30 to-brand-to/30 rounded-2xl blur-md opacity-0 group-hover:opacity-100 transition-all duration-500' />
<div className='relative p-4 rounded-xl bg-gradient-to-br from-white/80 to-gray-50/80 dark:from-neutral-800/80 dark:to-neutral-900/80 backdrop-blur-sm border border-white/50 dark:border-neutral-700/50 shadow-lg shadow-brand/5 hover:shadow-brand/10 transition-all duration-300'>
{/* 顶部装饰条 */}
<div className='absolute top-0 left-4 right-4 h-[2px] bg-gradient-to-r from-transparent via-brand-via/50 to-transparent rounded-full' />
<div className='flex items-center gap-3 mb-3'>
<div className='relative'>
<div className='absolute -inset-1 bg-gradient-to-br from-brand-from to-brand-to rounded-xl blur-sm opacity-60' />
<div className='relative w-8 h-8 rounded-lg bg-gradient-to-br from-brand-from to-brand-to flex items-center justify-center shadow-lg'>
<Bot size={16} className='text-white' />
</div>
</div>
<div>
<Typography.Text
strong
className='text-sm text-gray-800 dark:text-gray-100'
>
{t('模型')}
</Typography.Text>
<Typography.Text className='text-xs text-gray-400 dark:text-gray-500 block'>
{t('选择 AI 模型')}
</Typography.Text>
</div>
</div>
<Select
placeholder={t('请选择模型')}
name='model'
required
selection
filter={selectFilter}
autoClearSearchValue={false}
onChange={(value) => onInputChange('model', value)}
value={inputs.model}
autoComplete='new-password'
optionList={models}
renderOptionItem={renderModelOption}
style={{ width: '100%' }}
dropdownStyle={{ width: '100%', maxWidth: '100%' }}
className='!rounded-xl !border-brand/50 dark:!border-brand-deep/30 hover:!border-brand-from transition-colors'
disabled={customRequestMode}
/>
</div>
</div>
) : (
<SettingCard
icon={Bot}
title={t('模型')}
disabled={customRequestMode}
gradient='from-brand-from to-brand-to'
>
<div className='relative'>
<Select
placeholder={t('请选择模型')}
name='model'
required
selection
filter={selectFilter}
autoClearSearchValue={false}
onChange={(value) => onInputChange('model', value)}
value={inputs.model}
autoComplete='new-password'
optionList={models}
renderOptionItem={renderModelOption}
style={{ width: '100%' }}
dropdownStyle={{ width: '100%', maxWidth: '100%' }}
className='!rounded-xl'
disabled={customRequestMode}
/>
</div>
</SettingCard>
)}
{/* 图片URL输入 - 体验模式下使用增强样式 */}
<div
className={`${customRequestMode ? 'opacity-50' : ''} ${isExperienceMode ? 'group relative' : ''}`}
>
{isExperienceMode && (
<div className='absolute -inset-0.5 bg-gradient-to-r from-pink-500/20 via-rose-500/20 to-orange-500/20 rounded-2xl blur-md opacity-0 group-hover:opacity-100 transition-all duration-500' />
)}
<div
className={
isExperienceMode
? 'relative p-4 rounded-xl bg-gradient-to-br from-white/80 to-gray-50/80 dark:from-neutral-800/80 dark:to-neutral-900/80 backdrop-blur-sm border border-white/50 dark:border-neutral-700/50 shadow-lg shadow-pink-500/5 hover:shadow-pink-500/10 transition-all duration-300'
: ''
}
>
{isExperienceMode && (
<div className='absolute top-0 left-4 right-4 h-[2px] bg-gradient-to-r from-transparent via-pink-500/50 to-transparent rounded-full' />
)}
<ImageUrlInput
imageUrls={inputs.imageUrls}
imageEnabled={inputs.imageEnabled}
onImageUrlsChange={(urls) => onInputChange('imageUrls', urls)}
onImageEnabledChange={(enabled) =>
onInputChange('imageEnabled', enabled)
}
disabled={customRequestMode}
/>
</div>
</div>
{/* 参数控制组件 - 体验模式下使用增强样式 */}
<div
className={`${customRequestMode ? 'opacity-50' : ''} ${isExperienceMode ? 'group relative' : ''}`}
>
{isExperienceMode && (
<div className='absolute -inset-0.5 bg-gradient-to-r from-blue-500/20 via-cyan-500/20 to-teal-500/20 rounded-2xl blur-md opacity-0 group-hover:opacity-100 transition-all duration-500' />
)}
<div
className={
isExperienceMode
? 'relative p-4 rounded-xl bg-gradient-to-br from-white/80 to-gray-50/80 dark:from-neutral-800/80 dark:to-neutral-900/80 backdrop-blur-sm border border-white/50 dark:border-neutral-700/50 shadow-lg shadow-blue-500/5 hover:shadow-blue-500/10 transition-all duration-300'
: ''
}
>
{isExperienceMode && (
<div className='absolute top-0 left-4 right-4 h-[2px] bg-gradient-to-r from-transparent via-blue-500/50 to-transparent rounded-full' />
)}
<ParameterControl
inputs={inputs}
parameterEnabled={parameterEnabled}
onInputChange={onInputChange}
onParameterToggle={onParameterToggle}
disabled={customRequestMode}
/>
</div>
</div>
{/* 流式输出开关 - 体验模式下使用增强样式 */}
<div
className={`${customRequestMode ? 'opacity-50' : ''} ${isExperienceMode ? 'group relative' : ''}`}
>
{isExperienceMode && (
<div className='absolute -inset-0.5 bg-gradient-to-r from-emerald-500/20 via-teal-500/20 to-cyan-500/20 rounded-2xl blur-md opacity-0 group-hover:opacity-100 transition-all duration-500' />
)}
<div
className={
isExperienceMode
? 'relative p-4 rounded-xl bg-gradient-to-br from-white/80 to-gray-50/80 dark:from-neutral-800/80 dark:to-neutral-900/80 backdrop-blur-sm border border-white/50 dark:border-neutral-700/50 shadow-lg shadow-emerald-500/5 hover:shadow-emerald-500/10 transition-all duration-300'
: ''
}
>
{isExperienceMode && (
<div className='absolute top-0 left-4 right-4 h-[2px] bg-gradient-to-r from-transparent via-emerald-500/50 to-transparent rounded-full' />
)}
<div className='flex items-center justify-between'>
<div className='flex items-center gap-2'>
{isExperienceMode ? (
<div className='relative'>
<div className='absolute -inset-0.5 bg-gradient-to-br from-emerald-500 to-teal-600 rounded-lg blur-sm opacity-60' />
<div className='relative w-7 h-7 rounded-lg bg-gradient-to-br from-emerald-500 to-teal-600 flex items-center justify-center shadow-md'>
<Zap size={14} className='text-white' />
</div>
</div>
) : (
<ToggleLeft size={16} className='text-gray-500' />
)}
<div>
<Typography.Text strong className='text-sm'>
{t('流式输出')}
</Typography.Text>
{isExperienceMode && (
<Typography.Text className='text-xs text-gray-400 dark:text-gray-500 block'>
{t('实时显示AI回复内容')}
</Typography.Text>
)}
</div>
{customRequestMode && !isExperienceMode && (
<Typography.Text className='text-xs text-orange-600'>
({t('已在自定义模式中忽略')})
</Typography.Text>
)}
</div>
<Switch
checked={inputs.stream}
onChange={(checked) => onInputChange('stream', checked)}
checkedText={t('开')}
uncheckedText={t('关')}
size='small'
disabled={customRequestMode}
/>
</div>
</div>
</div>
</div>
{/* 桌面端的配置管理放在底部 */}
{!styleState.isMobile && (
<div className='flex-shrink-0 pt-4'>
{isExperienceMode && (
<div className='h-px bg-gradient-to-r from-transparent via-gray-200 dark:via-neutral-700 to-transparent mb-4' />
)}
<ConfigManager
currentConfig={currentConfig}
onConfigImport={onConfigImport}
onConfigReset={onConfigReset}
styleState={styleState}
messages={messages}
/>
</div>
)}
</div>
);
};
export default SettingsPanel;