From 29f24b303626e05877f757517e7fcba9f1faacad Mon Sep 17 00:00:00 2001 From: AskaEth Date: Sun, 28 Jun 2026 11:30:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20restoreContext=20=E4=BB=8E=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E5=8E=86=E5=8F=B2=E5=8F=96=E6=9C=80=E8=BF=91=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8=E6=97=B6=E9=97=B4=EF=BC=8C=E4=B8=8D=E5=86=8D=E7=94=A8?= =?UTF-8?q?=E8=AE=B0=E5=BF=86=E6=9D=A1=E7=9B=AE=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai-core/internal/background/thinker.go | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/backend/ai-core/internal/background/thinker.go b/backend/ai-core/internal/background/thinker.go index f22bb18..f157476 100644 --- a/backend/ai-core/internal/background/thinker.go +++ b/backend/ai-core/internal/background/thinker.go @@ -533,31 +533,20 @@ func NewThinker( } } -// restoreContext recovers the last user activity time from persistent storage. +// restoreContext recovers the last user activity time from conversation history (DB). func (t *Thinker) restoreContext() { - if t.memClient == nil { + if t.convStore == nil { return } - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - defer func() { t.muLock(); t.thinkCancel = nil; t.muUnlock() }() - - memories, err := t.memClient.Query(ctx, model.MemoryQuery{ - UserID: t.adminUserID, - Limit: 5, - }) - if err != nil || len(memories) == 0 { - return - } - - // Find the most recent memory/activity timestamp. + // 从已恢复的会话历史中获取最后一条用户消息的时间 + history := t.convStore.GetHistory(t.adminSessionID, 5) var latest time.Time - for _, m := range memories { - if m.CreatedAt.After(latest) { - latest = m.CreatedAt + for i := len(history) - 1; i >= 0; i-- { + if history[i].Role == model.RoleUser && !history[i].Timestamp.IsZero() { + latest = history[i].Timestamp + break } } - // Only use if reasonably recent (within 24h). Old memories don't represent user activity. if !latest.IsZero() && time.Since(latest) < 24*time.Hour { t.muLock() t.lastUserMessage = latest @@ -565,7 +554,7 @@ func (t *Thinker) restoreContext() { t.muUnlock() log.Printf("[后台思考] 上下文已恢复: 最近活动 %v 前", time.Since(latest).Round(time.Second)) } else if !latest.IsZero() { - log.Printf("[后台思考] 上下文恢复跳过: 最近记忆 %v 前(>24h),使用默认值", time.Since(latest).Round(time.Second)) + log.Printf("[后台思考] 上下文恢复跳过: 最近消息 %v 前(>24h),使用默认值", time.Since(latest).Round(time.Second)) } }