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:
2026-05-27 11:03:30 +02:00
parent 63f3c0dec8
commit b020d2e193
2 changed files with 93 additions and 33 deletions
+34 -10
View File
@@ -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
}