fix: restoreContext 从会话历史取最近活动时间,不再用记忆条目时间

This commit is contained in:
2026-06-28 11:30:15 +08:00
parent fd44b15d81
commit 29f24b3036
+9 -20
View File
@@ -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))
}
}