diff --git a/src/api/spell-check-tool.ts b/src/api/spell-check-tool.ts new file mode 100644 index 0000000..0bdad90 --- /dev/null +++ b/src/api/spell-check-tool.ts @@ -0,0 +1,31 @@ +/** + * 错别字检测与修正工具 API + * 调用 Dify 平台进行错别字检测与修正 + */ + +export interface DifyRequest { + inputs: { + user_input: string + } + query: string + response_mode: string +} + +export function checkSpellingWithDify(user_input: string) { + const requestBody: DifyRequest = { + inputs: { + user_input + }, + query: '1', + response_mode: 'streaming' + } + + return fetch('https://copilot.sino-bridge.com/v1/chat-messages', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer app-4SnGvNq4HPxFH4Ie8C0xo7ng' + }, + body: JSON.stringify(requestBody) + }) +} diff --git a/src/pages/spell-check-tool/index.tsx b/src/pages/spell-check-tool/index.tsx new file mode 100644 index 0000000..53e0c7c --- /dev/null +++ b/src/pages/spell-check-tool/index.tsx @@ -0,0 +1,316 @@ +import { useState } from 'react' +import { Button, Card, Form, Input, Space, message, Typography } from 'antd' +import { checkSpellingWithDify } from '@/api/spell-check-tool' + +const { TextArea } = Input +const { Title, Text } = Typography + +const SpellCheckTool = () => { + const [form] = Form.useForm() + const [inputText, setInputText] = useState('') + const [resultText, setResultText] = useState('') + const [isProcessing, setIsProcessing] = useState(false) + const [viewMode, setViewMode] = useState<'original' | 'corrected'>('original') + const maxChars = 5000 + + const handleGenerate = async () => { + try { + const formValues = await form.validateFields() + + // 验证输入内容 + if (!formValues.user_input?.trim()) { + message.warning('请输入需要检测错别字的文本') + return + } + + if (formValues.user_input.length > maxChars) { + message.warning(`输入文本不能超过${maxChars}个字符`) + return + } + + setIsProcessing(true) + setResultText('') + setViewMode('original') + + const response = await checkSpellingWithDify(formValues.user_input) + + 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 = '' + + 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 + setResultText(fullContent) + } else if (parsed.event === 'error') { + throw new Error(parsed.message || 'Dify API 返回错误') + } + } catch (parseError) { + console.warn('跳过无法解析的行:', trimmedLine) + } + } + } + } + } finally { + reader.releaseLock() + } + + if (fullContent) { + setResultText(fullContent) + setViewMode('corrected') + } + } catch (error) { + console.error('检测错误:', error) + message.error(error instanceof Error ? error.message : '检测失败,请稍后重试') + } finally { + setIsProcessing(false) + } + } + + const handleReset = () => { + form.resetFields() + setInputText('') + setResultText('') + setIsProcessing(false) + setViewMode('original') + message.info('已清空内容') + } + + const handleCopyResult = async () => { + try { + await navigator.clipboard.writeText(resultText) + message.success('已复制到剪贴板') + } catch (error) { + message.error('复制失败,请手动复制') + } + } + + const renderContent = () => { + if (isProcessing) { + return ( +
+
+
+
+ 正在检测中,请稍候... +
+ ) + } + + if (!resultText) { + return ( +
+ 请输入文本后点击'开始检测'按钮 +
+ ) + } + + if (viewMode === 'original') { + return ( +
+ {inputText} +
+ ) + } + + // 显示修正后的文本,不同颜色标记修改内容 + return ( +
+ {resultText} +
+ ) + } + + return ( +
+ {/* 页面标题区 */} +
+ + 错别字检测与修正工具 + +
+ + + {/* 参数输入区 */} + +
+ + 输入文本 + + } + rules={[ + { required: true, message: '请输入需要检测错别字的文本' }, + { max: maxChars, message: `输入文本不能超过${maxChars}个字符` } + ]} + > +