From ebd6f40425a81611cc5f9b7e3a37e0a288fd81d6 Mon Sep 17 00:00:00 2001 From: AskaEth Date: Wed, 24 Jun 2026 18:21:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8E=E5=8F=B0=E6=80=9D=E8=80=83?= =?UTF-8?q?=E5=8F=AF=E8=A2=AB=E5=89=8D=E5=8F=B0=E6=B6=88=E6=81=AF=E4=B8=AD?= =?UTF-8?q?=E6=96=AD=20=E2=80=94=20=E7=94=A8=E6=88=B7=E5=8F=91=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=97=B6cancel=E8=BF=90=E8=A1=8C=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=80=9D=E8=80=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai-core/internal/background/thinker.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/backend/ai-core/internal/background/thinker.go b/backend/ai-core/internal/background/thinker.go index 1e31b65..56be89e 100644 --- a/backend/ai-core/internal/background/thinker.go +++ b/backend/ai-core/internal/background/thinker.go @@ -199,6 +199,7 @@ type Thinker struct { lastUserMessage time.Time lastThinkTime time.Time lastProactiveMsgTime time.Time + thinkCancel context.CancelFunc // 取消当前思考,让步于前台 // 思考计数器(用于周期性记忆维护,每 N 次思考触发一次) thinkCount int @@ -417,6 +418,9 @@ func (t *Thinker) IsUserRecentlyActive(d time.Duration) bool { return time.Since(t.lastUserMessage) < d } + + + // ThinkerConfig 后台思考配置 type ThinkerConfig struct { Enabled bool @@ -528,6 +532,7 @@ func (t *Thinker) restoreContext() { } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() + defer func() { t.mu.Lock(); t.thinkCancel = nil; t.mu.Unlock() }() memories, err := t.memClient.Query(ctx, model.MemoryQuery{ UserID: t.adminUserID, @@ -631,6 +636,10 @@ func (t *Thinker) RecordUserMessage(sessionID string) { // 用户主动发消息时重置主动消息推送冷却——活跃对话中应允许昔涟回复 t.lastProactiveMsgTime = time.Time{} t.mu.Unlock() + if t.thinkCancel != nil { + t.thinkCancel() + t.thinkCancel = nil + } // 重置静默检测定时器 t.resetSilenceTimer() @@ -1070,6 +1079,20 @@ func (t *Thinker) performThink(triggerReason string) { ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) defer cancel() + t.mu.Lock() + t.thinkCancel = cancel + t.mu.Unlock() + + // 0. 让步于前台——如果用户最近有活动(非post_chat),跳过本次思考。 + if triggerReason != "post_chat" { + t.mu.Lock() + sinceLastUser := time.Since(t.lastUserMessage) + t.mu.Unlock() + if sinceLastUser < 10*time.Second { + log.Printf("[后台思考] 用户 %v 前有活动,让步于前台回复,跳过思考", sinceLastUser.Round(time.Second)) + return + } + } log.Printf("[后台思考] 开始思考周期 (触发原因=%s, 计数=%d)...", triggerReason, currentCount)