fix: restoreContext 从会话历史取最近活动时间,不再用记忆条目时间
This commit is contained in:
@@ -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() {
|
func (t *Thinker) restoreContext() {
|
||||||
if t.memClient == nil {
|
if t.convStore == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
// 从已恢复的会话历史中获取最后一条用户消息的时间
|
||||||
defer cancel()
|
history := t.convStore.GetHistory(t.adminSessionID, 5)
|
||||||
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.
|
|
||||||
var latest time.Time
|
var latest time.Time
|
||||||
for _, m := range memories {
|
for i := len(history) - 1; i >= 0; i-- {
|
||||||
if m.CreatedAt.After(latest) {
|
if history[i].Role == model.RoleUser && !history[i].Timestamp.IsZero() {
|
||||||
latest = m.CreatedAt
|
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 {
|
if !latest.IsZero() && time.Since(latest) < 24*time.Hour {
|
||||||
t.muLock()
|
t.muLock()
|
||||||
t.lastUserMessage = latest
|
t.lastUserMessage = latest
|
||||||
@@ -565,7 +554,7 @@ func (t *Thinker) restoreContext() {
|
|||||||
t.muUnlock()
|
t.muUnlock()
|
||||||
log.Printf("[后台思考] 上下文已恢复: 最近活动 %v 前", time.Since(latest).Round(time.Second))
|
log.Printf("[后台思考] 上下文已恢复: 最近活动 %v 前", time.Since(latest).Round(time.Second))
|
||||||
} else if !latest.IsZero() {
|
} else if !latest.IsZero() {
|
||||||
log.Printf("[后台思考] 上下文恢复跳过: 最近记忆 %v 前(>24h),使用默认值", time.Since(latest).Round(time.Second))
|
log.Printf("[后台思考] 上下文恢复跳过: 最近消息 %v 前(>24h),使用默认值", time.Since(latest).Round(time.Second))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user