import { useState } from 'react' import { Button, Card, Form, Input, Table, Switch, Space, message } from 'antd' import { detectSpellingErrors, SpellCheckResult } from '@/api/spell-check-system' interface FeedbackItem extends SpellCheckResult { id: string status: 'pending' | 'confirmed' | 'rejected' } const SpellCheckSystem = () => { const [form] = Form.useForm() const [isDetecting, setIsDetecting] = useState(false) const [detectionResults, setDetectionResults] = useState([]) const [feedbackData, setFeedbackData] = useState<{ confirmed: string[] rejected: string[] }>({ confirmed: [], rejected: [] }) const handleDetect = async () => { try { const formValues = await form.validateFields() // 验证用户输入文本 if (!formValues.text_input?.trim()) { message.warning('请输入需要检测的文本内容') return } setIsDetecting(true) setDetectionResults([]) const response = await detectSpellingErrors({ text_input: formValues.text_input, enable_error_category: formValues.enable_error_category || false, enable_correction_suggestion: formValues.enable_correction_suggestion !== false, enable_batch_processing: formValues.enable_batch_processing || false }) if (!response.ok) { const errorText = await response.text() throw new Error(`检测请求失败: ${response.status} ${errorText}`) } if (!response.body) { throw new Error('响应体为空') } const reader = response.body.getReader() const decoder = new TextDecoder('utf-8') let buffer = '' let fullContent = '' let results: SpellCheckResult[] = [] try { while (true) { const { done, value } = await reader.read() if (done) break buffer += decoder.decode(value, { stream: true }) const lines = buffer.split('\n') buffer = lines.pop() || '' for (const line of lines) { const trimmedLine = line.trim() if (!trimmedLine || trimmedLine === 'data: [DONE]') { if (trimmedLine === 'data: [DONE]') { message.success('检测完成') break } continue } if (trimmedLine.startsWith('data: ')) { try { const data = trimmedLine.slice(6) const parsed = JSON.parse(data) if (parsed.event === 'message' && parsed.answer) { fullContent += parsed.answer // 尝试解析JSON结果 try { results = JSON.parse(fullContent) const feedbackItems: FeedbackItem[] = results.map((item, index) => ({ ...item, id: `item-${index}`, status: 'pending' as const })) setDetectionResults(feedbackItems) } catch (e) { // 解析失败,继续等待 } } else if (parsed.event === 'error') { throw new Error(parsed.message || 'Dify API 返回错误') } } catch (parseError) { console.warn('跳过无法解析的行:', trimmedLine) } } } } } finally { reader.releaseLock() } if (results.length === 0 && fullContent) { try { results = JSON.parse(fullContent) const feedbackItems: FeedbackItem[] = results.map((item, index) => ({ ...item, id: `item-${index}`, status: 'pending' as const })) setDetectionResults(feedbackItems) } catch (e) { message.warning('检测完成,但结果格式异常') } } } catch (error) { console.error('检测错误:', error) message.error(error instanceof Error ? error.message : '检测失败,请稍后重试') } finally { setIsDetecting(false) } } const handleFeedback = (id: string, status: 'confirmed' | 'rejected') => { setDetectionResults(prev => prev.map(item => item.id === id ? { ...item, status } : item ) ) } const handleSubmitFeedback = () => { const confirmed: string[] = [] const rejected: string[] = [] detectionResults.forEach(item => { if (item.status === 'confirmed') { confirmed.push(item.suggestion) } else if (item.status === 'rejected') { rejected.push(item.error_word) } }) const feedback = { confirmed, rejected } // 存储到本地存储 localStorage.setItem('spell_check_feedback', JSON.stringify(feedback)) setFeedbackData(feedback) message.success('反馈数据已提交并保存到本地存储') } const renderFeedbackButtons = (record: FeedbackItem) => { if (record.status !== 'pending') { return ( {record.status === 'confirmed' ? '已确认' : '已否决'} ) } return ( ) } const columns = [ { title: '错误词', dataIndex: 'error_word', key: 'error_word', width: 120, render: (text: string) => ( {text} ) }, { title: '上下文', dataIndex: 'context', key: 'context', ellipsis: true }, { title: '建议词', dataIndex: 'suggestion', key: 'suggestion', width: 120, render: (text: string) => ( {text} ) }, { title: '错误类型', dataIndex: 'error_type', key: 'error_type', width: 120, render: (text: string) => text || '-' }, { title: '操作', key: 'action', width: 150, render: (_: any, record: FeedbackItem) => renderFeedbackButtons(record) } ] return (
{/* 页面标题区 */}

错别字检测与修正系统

{/* 参数输入区 */}
{/* 操作按钮区 */}
{/* 内容展示区 */} 0 || isDetecting ? 'block' : 'none' }} > {isDetecting ? (
正在检测中,请稍候...
) : detectionResults.length > 0 ? ( <>
) : (
错别字检测结果将在这里显示
)} ) } export default SpellCheckSystem