Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve booting

Daniel6b059372173a
diff --git a/app/appearance/boot/index.html b/app/appearance/boot/index.html
index 17b21d7b88..c25d44b373 100644
--- a/app/appearance/boot/index.html
+++ b/app/appearance/boot/index.html
@@ -30,10 +30,6 @@
style="color: #9aa0a6;text-overflow: ellipsis;white-space: nowrap;overflow: hidden;padding: 8px;"></div>
</div>
<script>
- const sleep = (ms) => {
- return new Promise(resolve => setTimeout(resolve, ms))
- }
-
const getSearch = (key) => {
if (window.location.search.indexOf('?') === -1) {
return ''
@@ -58,24 +54,30 @@
const v = getSearch('v')
document.getElementById('details').textContent = "v" + v + ' Booting kernel...'
let progressing = false
- while (!progressing) {
- try {
- const progressResult = await fetch('http://127.0.0.1:' + location.port + '/api/system/bootProgress')
- const progressData = await progressResult.json()
- document.getElementById('progress').style.width = progressData.data.progress + '%'
- document.getElementById('details').textContent = progressData.data.details
- if (progressData.data.progress >= 100) {
+ const subscribe = () => {
+ const es = new EventSource('http://127.0.0.1:' + location.port + '/api/system/bootProgressSSE')
+ es.onmessage = (event) => {
+ const progressData = JSON.parse(event.data)
+ document.getElementById('progress').style.width = progressData.progress + '%'
+ document.getElementById('details').textContent = progressData.details
+ if (progressData.progress >= 100) {
progressing = true
+ es.close()
if (navigator.userAgent.indexOf('Electron') === -1) {
redirect()
}
- } else {
- await sleep(100)
}
- } catch (e) {
- await sleep(100)
+ }
+ es.onerror = () => {
+ es.close()
+ // 内核启动早期 HTTP 服务可能尚未就绪,EventSource 原生重连间隔较长(约 3s),
+ // 这里主动短延迟重连,保持与原轮询一致的快速重试语义
+ if (!progressing) {
+ setTimeout(subscribe, 100)
+ }
}
}
+ subscribe()
})()
</script>
</body>
diff --git a/kernel/api/router.go b/kernel/api/router.go
index d4d9cb5651..5daa374361 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -31,6 +31,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("GET", "/api/system/bootProgress", bootProgress)
ginServer.Handle("POST", "/api/system/bootProgress", bootProgress)
+ ginServer.Handle("GET", "/api/system/bootProgressSSE", bootProgressSSE)
ginServer.Handle("GET", "/api/system/version", version)
ginServer.Handle("POST", "/api/system/version", version)
ginServer.Handle("POST", "/api/system/currentTime", currentTime)
diff --git a/kernel/api/system.go b/kernel/api/system.go
index 9b00661fda..750185f63f 100644
--- a/kernel/api/system.go
+++ b/kernel/api/system.go
@@ -17,6 +17,8 @@
package api
import (
+ "encoding/json"
+ "fmt"
"io"
"net/http"
"os"
@@ -715,6 +717,64 @@ func bootProgress(c *gin.Context) {
ret.Data = map[string]any{"progress": progress, "details": details}
}
+// bootProgressSSE 以 Server-Sent Events 推送启动进度,仅在进度发生变化时写一帧。
+func bootProgressSSE(c *gin.Context) {
+ c.Header("Content-Type", "text/event-stream")
+ c.Header("Cache-Control", "no-cache")
+ c.Header("Connection", "keep-alive")
+ c.Writer.Flush()
+
+ flusher, ok := c.Writer.(http.Flusher)
+ if !ok {
+ return
+ }
+
+ // 连接后立即推送当前进度,避免等待第一个 tick
+ progress, details := util.GetBootProgressDetails()
+ lastProgress, lastDetails := progress, details
+ if err := writeBootProgressSSE(c, flusher, progress, details); err != nil {
+ return
+ }
+ if 100 <= progress {
+ return
+ }
+
+ ticker := time.NewTicker(100 * time.Millisecond)
+ defer ticker.Stop()
+ ctx := c.Request.Context()
+ for {
+ select {
+ case <-ctx.Done():
+ // 客户端断开连接
+ return
+ case <-ticker.C:
+ progress, details = util.GetBootProgressDetails()
+ if progress == lastProgress && details == lastDetails {
+ continue
+ }
+ lastProgress, lastDetails = progress, details
+ if err := writeBootProgressSSE(c, flusher, progress, details); err != nil {
+ return
+ }
+ if 100 <= progress {
+ return
+ }
+ }
+ }
+}
+
+func writeBootProgressSSE(c *gin.Context, flusher http.Flusher, progress int32, details string) error {
+ data, err := json.Marshal(map[string]any{"progress": progress, "details": details})
+ if err != nil {
+ return err
+ }
+ if _, err = fmt.Fprintf(c.Writer, "data: %s\n\n", data); err != nil {
+ return err
+ }
+ flusher.Flush()
+ return nil
+}
+
func setAppearanceMode(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)