From 1433c5d3365bda5fa4036b7a6591e44b861dd52b Mon Sep 17 00:00:00 2001 From: Vibe Studio Date: Wed, 21 Jan 2026 08:48:09 +0000 Subject: [PATCH] Update from Vibe Studio --- src/api/spellcheck.ts | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/api/spellcheck.ts diff --git a/src/api/spellcheck.ts b/src/api/spellcheck.ts new file mode 100644 index 0000000..a35eb01 --- /dev/null +++ b/src/api/spellcheck.ts @@ -0,0 +1,77 @@ +/** + * 错别字检测与修正API + * 与Dify平台交互,实现错别字检测和修正功能 + */ + +export interface SpellCheckParams { + user_input: string; +} + +export interface SpellCheckResponse { + message?: { + content?: string; + }; +} + +/** + * 发送错别字检测请求到Dify平台 + * @param params 包含用户输入文本的参数 + * @returns Promise 返回修正后的文本内容 + */ +export async function fetchSpellCheck(params: SpellCheckParams): Promise { + const requestBody = { + inputs: { + user_input: params.user_input + }, + query: "1", + response_mode: "streaming" as const + }; + + try { + const response = await fetch('https://copilot.sino-bridge.com/v1/chat-messages', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer app-AZ3jH4bHyOPsTzDjEQyoqwOy' + }, + body: JSON.stringify(requestBody) + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + // 处理流式响应 + const reader = response.body?.getReader(); + const decoder = new TextDecoder(); + let result = ''; + + if (reader) { + while (true) { + const { done, value } = await reader.read(); + if (done) break; + + const chunk = decoder.decode(value, { stream: true }); + const lines = chunk.split('\n'); + + for (const line of lines) { + if (line.startsWith('data: ')) { + try { + const data = JSON.parse(line.slice(6)) as SpellCheckResponse; + if (data.message?.content) { + result += data.message.content; + } + } catch (e) { + // 忽略解析错误,继续处理下一行 + } + } + } + } + } + + return result; + } catch (error) { + console.error('错别字检测请求失败:', error); + throw error; + } +}