fix: retain knowledge indexing errors and explain failure causes

This commit is contained in:
Ed1s0nZ committed 2026-10-06 03:02:14 +08:00
1 parent bb96a084e0
commit 5dc9cb1541
7 files changed
+94 -11

No files matched your search

+10 -7
View File
@@ -489,13 +489,8 @@ func (h *KnowledgeHandler) GetIndexStatus(c *gin.Context) {
// 获取索引器的错误信息
if h.indexer != nil {
lastError, lastErrorTime := h.indexer.GetLastError()
if lastError != "" {
// 如果错误是最近发生的(5 分钟内),则返回错误信息
if time.Since(lastErrorTime) < 5*time.Minute {
status["last_error"] = lastError
status["last_error_time"] = lastErrorTime.Format(time.RFC3339)
}
}
// Keep a failed operation visible until the indexer clears it for the next run.
appendKnowledgeIndexError(status, lastError, lastErrorTime)
// 获取重建索引状态
isRebuilding, totalItems, current, failed, lastItemID, lastChunks, startTime := h.indexer.GetRebuildStatus()
@@ -564,3 +559,11 @@ func parseInt(s string) (int, error) {
_, err := fmt.Sscanf(s, "%d", &result)
return result, err
}
func appendKnowledgeIndexError(status map[string]interface{}, message string, occurredAt time.Time) {
if message == "" {
return
}
status["last_error"] = message
status["last_error_time"] = occurredAt.Format(time.RFC3339)
}
@@ -0,0 +1,20 @@
package handler
import (
"testing"
"time"
)
func TestKnowledgeIndexFailureRemainsVisibleUntilCleared(t *testing.T) {
occurred := time.Now().Add(-time.Hour)
status := map[string]interface{}{}
appendKnowledgeIndexError(status, "test-only embedding failure: 401 Unauthorized", occurred)
if status["last_error"] == nil || status["last_error_time"] != occurred.Format(time.RFC3339) {
t.Fatal("older failure disappeared")
}
cleared := map[string]interface{}{}
appendKnowledgeIndexError(cleared, "", time.Time{})
if _, ok := cleared["last_error"]; ok {
t.Fatal("cleared failure remained")
}
}