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++ }