Skip to content

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

:art: Automatically perform index fixing during idle time https://github.com/siyuan-note/siyuan/issues/17535

Daniel6b7927679449
diff --git a/kernel/model/session.go b/kernel/model/session.go
index 5c6992ab5e..734bfe3227 100644
--- a/kernel/model/session.go
+++ b/kernel/model/session.go
@@ -448,6 +448,16 @@ func Recover(c *gin.Context) {
c.Next()
}
+// Activity 记录用户写操作时间,用于 AutoFixIndex 的空闲判断。
+// 只认会产生数据变更的事务类请求(/api/transactions*),因为只有写操作才会引入索引不一致;
+// 读操作和前端定时轮询(如 /api/ai/embeddingStat)不计入,避免 SiYuan 开着却不操作时被判为活跃。
+func Activity(c *gin.Context) {
+ if strings.HasPrefix(c.Request.URL.Path, "/api/transactions") {
+ util.RefreshActivity()
+ }
+ c.Next()
+}
+
var (
requestingLock = sync.Mutex{}
requesting = map[string]*sync.Mutex{}
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index 7a44bce1e3..a03730d094 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -141,6 +141,7 @@ func Serve(fastMode bool, cookieKey string) {
model.ControlConcurrency, // 请求串行化 Concurrency control when requesting the kernel API https://github.com/siyuan-note/siyuan/issues/9939
model.Timing,
model.Recover,
+ model.Activity, // 记录用户活动时间,用于 AutoFixIndex 的空闲判断
corsMiddleware(), // 后端服务支持 CORS 预检请求验证 https://github.com/siyuan-note/siyuan/pull/5593
jwtMiddleware, // 解析 JWT https://github.com/siyuan-note/siyuan/issues/11364
gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedExtensions([]string{".pdf", ".mp3", ".wav", ".ogg", ".mov", ".weba", ".mkv", ".mp4", ".webm", ".flac"})),
@@ -867,7 +868,6 @@ func serveWebSocket(ginServer *gin.Engine) {
util.WebSocketServer.HandleMessage(func(s *melody.Session, msg []byte) {
start := time.Now()
- util.RefreshActivity()
logging.LogTracef("request [%s]", shortReqMsg(msg))
if util.IsAuthSession(s) {