import { useState, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { ThinkingBudgetConfig, ThinkingBudgetMode, ThinkingEffort } from "../../types/config"; interface ThinkingBudgetProps { config: ThinkingBudgetConfig; onChange: (config: ThinkingBudgetConfig) => void; } const DEFAULT_CONFIG: ThinkingBudgetConfig = { mode: 'auto', custom_value: 24576, }; export default function ThinkingBudget({ config = DEFAULT_CONFIG, onChange, }: ThinkingBudgetProps) { const { t } = useTranslation(); // 使用本地 state 管理输入值,允许临时的无效输入 const [inputValue, setInputValue] = useState(String(config.custom_value)); // 同步外部 config 变化 useEffect(() => { setInputValue(String(config.custom_value)); }, [config.custom_value]); const handleModeChange = (mode: ThinkingBudgetMode) => { // 切换到 adaptive 模式时,如果未设置 effort,默认设置为 high if (mode === 'adaptive' && !config.effort) { onChange({ ...config, mode, effort: 'high' }); } else { onChange({ ...config, mode }); } }; const handleEffortChange = (effort: ThinkingEffort) => { onChange({ ...config, effort }); }; // 输入时只更新本地 state const handleInputChange = (val: string) => { setInputValue(val); }; // 失焦时校验并提交 const handleInputBlur = () => { let num = parseInt(inputValue, 10); if (isNaN(num) || num < 1024) num = 1024; if (num > 65536) num = 65536; setInputValue(String(num)); onChange({ ...config, custom_value: num }); }; const modes: ThinkingBudgetMode[] = ['auto', 'adaptive', 'passthrough', 'custom']; // Ensure adaptive is included const efforts: ThinkingEffort[] = ['low', 'medium', 'high']; return (

{t("settings.thinking_budget.title", { defaultValue: "思考预算 (Thinking Budget)" })}

{t("settings.thinking_budget.mode_label", { defaultValue: "处理模式" })}

{modes.map((key) => ( ))}
{/* Mode-specific UI (Compact) */}
{config.mode === 'auto' && (

{t("settings.thinking_budget.auto_hint", { defaultValue: "自动模式:对 Gemini/Thinking 及联网请求自动限制在 24576 以避免错误。", })}

)} {config.mode === 'passthrough' && (

{t("settings.thinking_budget.passthrough_warning", { defaultValue: "透传:直接使用调用方原始值,不支持高值可能导致失败。", })}

)} {config.mode === 'adaptive' && (
{t("settings.thinking_budget.effort_label", { defaultValue: "思考强度" })}:
{efforts.map((effort) => ( ))}

{t("settings.thinking_budget.adaptive_hint", { defaultValue: "自适应模式:由模型根据任务复杂度自动调整思考量。Claude 4.6+ 推荐使用此模式。", })}

)} {config.mode === 'custom' && (
handleInputChange(e.target.value)} onBlur={handleInputBlur} className="w-24 bg-white dark:bg-base-100 border border-gray-200 dark:border-gray-700 rounded-md px-2 py-1 text-xs font-mono focus:ring-1 focus:ring-blue-500 outline-none transition-all [appearance:textfield]" min={1024} max={65536} step={1024} /> TOKENS

{t("settings.thinking_budget.custom_value_hint", { defaultValue: "推荐:24576 (Flash) 或 51200 (扩展)", })}

)}
); }