Update from Vibe Studio

This commit is contained in:
Vibe Studio
2026-01-21 07:32:43 +00:00
parent 976704f9ec
commit a0a0d4285f

94
src/api/spell-check.ts Normal file
View File

@@ -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<string, any>;
}
/**
* 调用Dify API进行错别字检测与修正
*/
export async function detectSpellError(
text: string,
onMessage?: (content: string) => void
): Promise<string> {
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;
}
}