From a0a0d4285f4f35bce03c8af3b30466bc44135e4d Mon Sep 17 00:00:00 2001 From: Vibe Studio Date: Wed, 21 Jan 2026 07:32:43 +0000 Subject: [PATCH] Update from Vibe Studio --- src/api/spell-check.ts | 94 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/api/spell-check.ts diff --git a/src/api/spell-check.ts b/src/api/spell-check.ts new file mode 100644 index 0000000..d77b556 --- /dev/null +++ b/src/api/spell-check.ts @@ -0,0 +1,94 @@ +/** + * 错别字检测与修正 API + */ +export interface SpellCheckRequest { + inputs: { + text: string; + }; + query: string; + response_mode: 'streaming' | 'blocking'; + conversation_id?: string; + user?: string; +} + +export interface SpellCheckResponse { + event: string; + message_id: string; + mode: string; + answer: string; + metadata?: Record; +} + +/** + * 调用Dify API进行错别字检测与修正 + */ +export async function detectSpellError( + text: string, + onMessage?: (content: string) => void +): Promise { + const API_URL = 'https://copilot.sino-bridge.com/v1/chat-messages'; + const API_KEY = 'app-hH1RS9yot7bI7hHO8AbTlntN'; + + const requestData: SpellCheckRequest = { + inputs: { + text: text, + }, + query: '1', + response_mode: 'streaming', + }; + + try { + const response = await fetch(API_URL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${API_KEY}`, + }, + body: JSON.stringify(requestData), + }); + + if (!response.ok) { + throw new Error(`API请求失败: ${response.statusText}`); + } + + const reader = response.body?.getReader(); + if (!reader) { + throw new Error('无法读取响应流'); + } + + let result = ''; + const decoder = new TextDecoder(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + + const chunk = decoder.decode(value); + const lines = chunk.split('\n'); + + for (const line of lines) { + if (line.startsWith('data: ')) { + const data = line.slice(6); + if (data === '[DONE]') { + return result; + } + + try { + const parsed: SpellCheckResponse = JSON.parse(data); + if (parsed.event === 'message' && parsed.answer) { + result += parsed.answer; + onMessage?.(result); + } + } catch (e) { + // 忽略解析错误,继续处理下一行 + } + } + } + } + + return result; + } catch (error) { + console.error('错别字检测失败:', error); + throw error; + } +}