From e0900bdd1bd786d5da3151fa92ecbe0a3bb9f104 Mon Sep 17 00:00:00 2001 From: AskaEth Date: Tue, 23 Jun 2026 19:38:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=87=8D=E5=90=AF=E5=90=8E=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E4=B8=8A=E4=B8=8B=E6=96=87=20=E2=80=94=20=E4=BB=8E?= =?UTF-8?q?=E8=AE=B0=E5=BF=86=E6=9C=8D=E5=8A=A1=E6=9F=A5=E8=AF=A2=E6=9C=80?= =?UTF-8?q?=E8=BF=91=E6=B4=BB=E5=8A=A8=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 restoreContext() 在 Start() 时查询持久化记忆 - 取最近记忆的 CreatedAt 设为 lastUserMessage - 避免重启后思考出现"已经一天没说话了" Co-Authored-By: Claude --- .../ai-core/internal/background/thinker.go | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/backend/ai-core/internal/background/thinker.go b/backend/ai-core/internal/background/thinker.go index 1c910e3..2ce7955 100644 --- a/backend/ai-core/internal/background/thinker.go +++ b/backend/ai-core/internal/background/thinker.go @@ -490,7 +490,7 @@ func NewThinker( adminNickname: adminNickname, memClient: memClient, pendingThoughts: make([]*PendingThought, 0), - lastUserMessage: time.Now(), + lastUserMessage: time.Now().Add(-5 * time.Minute), // overwritten by restoreContext stopCh: make(chan struct{}), chain: NewThinkChain(10), autoToolPolicy: DefaultAutonomousToolPolicy(), @@ -501,6 +501,37 @@ func NewThinker( } } +// restoreContext recovers the last user activity time from persistent storage. +func (t *Thinker) restoreContext() { + if t.memClient == nil { + return + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + 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. + var latest time.Time + for _, m := range memories { + if m.CreatedAt.After(latest) { + latest = m.CreatedAt + } + } + if !latest.IsZero() { + t.mu.Lock() + t.lastUserMessage = latest + t.mu.Unlock() + log.Printf("[后台思考] 上下文已恢复: 最近活动 %v 前", time.Since(latest).Round(time.Second)) + } +} + // Start 初始化后台思考器 // // 双模式触发: @@ -512,6 +543,9 @@ func (t *Thinker) Start() { return } + // 恢复上下文:从持久化存储查询最近活动时间,避免重启后"失忆" + t.restoreContext() + // 初始化静默检测定时器(但不启动,等第一次用户消息后启动) if t.silenceTimeout > 0 { t.silenceTimer = time.NewTimer(t.silenceTimeout)