()
| 4 | import { processIncompleteThinkTags } from '../helpers'; |
| 5 | |
| 6 | export const usePlaygroundState = () => { |
| 7 | // 使用惰性初始化,确保只在组件首次挂载时加载配置和消息 |
| 8 | const [savedConfig] = useState(() => loadConfig()); |
| 9 | const [initialMessages] = useState(() => loadMessages() || DEFAULT_MESSAGES); |
| 10 | |
| 11 | // 基础配置状态 |
| 12 | const [inputs, setInputs] = useState(savedConfig.inputs || DEFAULT_CONFIG.inputs); |
| 13 | const [parameterEnabled, setParameterEnabled] = useState( |
| 14 | savedConfig.parameterEnabled || DEFAULT_CONFIG.parameterEnabled |
| 15 | ); |
| 16 | const [showDebugPanel, setShowDebugPanel] = useState( |
| 17 | savedConfig.showDebugPanel || DEFAULT_CONFIG.showDebugPanel |
| 18 | ); |
| 19 | const [customRequestMode, setCustomRequestMode] = useState( |
| 20 | savedConfig.customRequestMode || DEFAULT_CONFIG.customRequestMode |
| 21 | ); |
| 22 | const [customRequestBody, setCustomRequestBody] = useState( |
| 23 | savedConfig.customRequestBody || DEFAULT_CONFIG.customRequestBody |
| 24 | ); |
| 25 | |
| 26 | // UI状态 |
| 27 | const [showSettings, setShowSettings] = useState(false); |
| 28 | const [models, setModels] = useState([]); |
| 29 | const [groups, setGroups] = useState([]); |
| 30 | const [status, setStatus] = useState({}); |
| 31 | |
| 32 | // 消息相关状态 - 使用加载的消息初始化 |
| 33 | const [message, setMessage] = useState(initialMessages); |
| 34 | |
| 35 | // 调试状态 |
| 36 | const [debugData, setDebugData] = useState({ |
| 37 | request: null, |
| 38 | response: null, |
| 39 | timestamp: null, |
| 40 | previewRequest: null, |
| 41 | previewTimestamp: null |
| 42 | }); |
| 43 | const [activeDebugTab, setActiveDebugTab] = useState(DEBUG_TABS.PREVIEW); |
| 44 | const [previewPayload, setPreviewPayload] = useState(null); |
| 45 | |
| 46 | // 编辑状态 |
| 47 | const [editingMessageId, setEditingMessageId] = useState(null); |
| 48 | const [editValue, setEditValue] = useState(''); |
| 49 | |
| 50 | // Refs |
| 51 | const sseSourceRef = useRef(null); |
| 52 | const chatRef = useRef(null); |
| 53 | const saveConfigTimeoutRef = useRef(null); |
| 54 | const saveMessagesTimeoutRef = useRef(null); |
| 55 | |
| 56 | // 配置更新函数 |
| 57 | const handleInputChange = useCallback((name, value) => { |
| 58 | setInputs(prev => ({ ...prev, [name]: value })); |
| 59 | }, []); |
| 60 | |
| 61 | const handleParameterToggle = useCallback((paramName) => { |
| 62 | setParameterEnabled(prev => ({ |
| 63 | ...prev, |
no test coverage detected