fix: LoadFromDB Timestamp 赋值 + restoreContext 查20条消息

This commit is contained in:
2026-06-28 11:32:17 +08:00
parent 29f24b3036
commit 5725075a31
2 changed files with 14 additions and 7 deletions
+11 -5
View File
@@ -538,13 +538,19 @@ func (t *Thinker) restoreContext() {
if t.convStore == nil { if t.convStore == nil {
return return
} }
// 从已恢复的会话历史中获取最后一条用户消息的时间 // 从已恢复的会话历史中获取最后一条消息的时间(优先用户消息)
history := t.convStore.GetHistory(t.adminSessionID, 5) history := t.convStore.GetHistory(t.adminSessionID, 20)
var latest time.Time var latest time.Time
for i := len(history) - 1; i >= 0; i-- { for i := len(history) - 1; i >= 0; i-- {
if history[i].Role == model.RoleUser && !history[i].Timestamp.IsZero() { if !history[i].Timestamp.IsZero() {
latest = history[i].Timestamp if history[i].Role == model.RoleUser {
break latest = history[i].Timestamp
break
}
// 回退:非 system 消息的时间也可以
if latest.IsZero() && history[i].Role != model.RoleSystem {
latest = history[i].Timestamp
}
} }
} }
if !latest.IsZero() && time.Since(latest) < 24*time.Hour { if !latest.IsZero() && time.Since(latest) < 24*time.Hour {
+3 -2
View File
@@ -162,8 +162,9 @@ func (cs *ConversationStore) LoadFromDB(databaseURL, sessionID string, limit int
role = model.RoleAssistant role = model.RoleAssistant
} }
cs.messages[sessionID] = append(cs.messages[sessionID], model.LLMMessage{ cs.messages[sessionID] = append(cs.messages[sessionID], model.LLMMessage{
Role: role, Role: role,
Content: content, Content: content,
Timestamp: createdAt,
}) })
loaded++ loaded++
} }