Stream AI responses (SSE) - text appears as it generates
- Backend streams tokens via Server-Sent Events - Frontend reads stream with fetch + ReadableStream - Edit mode: document updates live as tokens arrive - Chat mode: response text appears progressively - No more waiting for full generation to complete
This commit is contained in:
+34
-10
@@ -1014,21 +1014,45 @@ async function sendAiChat() {
|
||||
|
||||
const action = aiChatMode.value === 'edit' ? 'edit' : 'chat'
|
||||
try {
|
||||
const res = await api('/api/ai/chat', {
|
||||
path: currentFile.value,
|
||||
content: content.value,
|
||||
message: msg,
|
||||
mode: action,
|
||||
const res = await fetch('/api/ai/chat', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token.value },
|
||||
body: JSON.stringify({ path: currentFile.value, content: content.value, message: msg, mode: action }),
|
||||
})
|
||||
if (action === 'edit' && res.content) {
|
||||
content.value = res.content
|
||||
|
||||
const reader = res.body.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let fullText = ''
|
||||
let buffer = ''
|
||||
|
||||
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) {
|
||||
if (!line.startsWith('data: ')) continue
|
||||
const data = line.slice(6)
|
||||
if (data === '[DONE]') break
|
||||
try {
|
||||
const token_text = JSON.parse(data)
|
||||
fullText += token_text
|
||||
if (action === 'edit') {
|
||||
content.value = fullText
|
||||
} else {
|
||||
aiChatResponse.value = fullText
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
if (action === 'edit') {
|
||||
isDirty.value = true
|
||||
aiChatResponse.value = 'Document updated.'
|
||||
} else {
|
||||
aiChatResponse.value = res.result || res.content || 'No response'
|
||||
}
|
||||
} catch (e) {
|
||||
aiChatResponse.value = 'AI request failed. Check MH_AI_ENDPOINT.'
|
||||
aiChatResponse.value = 'AI request failed.'
|
||||
}
|
||||
aiChatLoading.value = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user