From 5725075a31e7be06568bf58279137c92b0ed544f Mon Sep 17 00:00:00 2001 From: AskaEth Date: Sun, 28 Jun 2026 11:32:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20LoadFromDB=20Timestamp=20=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=20+=20restoreContext=20=E6=9F=A520=E6=9D=A1=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/ai-core/internal/background/thinker.go | 16 +++++++++++----- backend/ai-core/internal/context/builder.go | 5 +++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/backend/ai-core/internal/background/thinker.go b/backend/ai-core/internal/background/thinker.go index f157476..d6b166c 100644 --- a/backend/ai-core/internal/background/thinker.go +++ b/backend/ai-core/internal/background/thinker.go @@ -538,13 +538,19 @@ func (t *Thinker) restoreContext() { if t.convStore == nil { return } - // 从已恢复的会话历史中获取最后一条用户消息的时间 - history := t.convStore.GetHistory(t.adminSessionID, 5) + // 从已恢复的会话历史中获取最后一条消息的时间(优先用户消息) + history := t.convStore.GetHistory(t.adminSessionID, 20) var latest time.Time for i := len(history) - 1; i >= 0; i-- { - if history[i].Role == model.RoleUser && !history[i].Timestamp.IsZero() { - latest = history[i].Timestamp - break + if !history[i].Timestamp.IsZero() { + if history[i].Role == model.RoleUser { + 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 { diff --git a/backend/ai-core/internal/context/builder.go b/backend/ai-core/internal/context/builder.go index eb33535..e527b74 100644 --- a/backend/ai-core/internal/context/builder.go +++ b/backend/ai-core/internal/context/builder.go @@ -162,8 +162,9 @@ func (cs *ConversationStore) LoadFromDB(databaseURL, sessionID string, limit int role = model.RoleAssistant } cs.messages[sessionID] = append(cs.messages[sessionID], model.LLMMessage{ - Role: role, - Content: content, + Role: role, + Content: content, + Timestamp: createdAt, }) loaded++ }