Merge branch 'main' into feat/base-service-delivery

This commit is contained in:
ztkkOip
2026-07-28 18:03:04 +08:00
committed by GitHub
8 changed files with 443 additions and 55 deletions
+46
View File
@@ -79,3 +79,49 @@ function parseResponseBody(text: string) {
return { error: text }
}
}
export interface TaskLogStreamCallbacks {
onInit?: (lines: TaskLogLine[]) => void
onUpdate?: (lines: TaskLogLine[]) => void
onFinished?: () => void
onError?: (error: string) => void
}
export function createTaskLogStream(taskId: string, callbacks: TaskLogStreamCallbacks): EventSource {
const token = getToken()
const url = `/auth/api/v1/task-logs/stream?task_id=${encodeURIComponent(taskId)}&access_token=${encodeURIComponent(token || '')}`
const es = new EventSource(url)
es.onmessage = (event) => {
try {
const data = JSON.parse(event.data)
switch (data.type) {
case 'init':
callbacks.onInit?.(Array.isArray(data.lines) ? data.lines : [])
break
case 'update':
callbacks.onUpdate?.(Array.isArray(data.lines) ? data.lines : [])
break
case 'finished':
callbacks.onFinished?.()
es.close()
break
case 'error':
callbacks.onError?.(data.error || 'Unknown error')
break
}
} catch {
// ignore parse errors for heartbeat etc.
}
}
es.onerror = () => {
// EventSource will auto-reconnect, but we report connection errors
if (es.readyState === EventSource.CLOSED) {
callbacks.onError?.('Connection closed')
}
}
return es
}