This commit is contained in:
lpf
2026-02-17 00:14:12 +08:00
parent ff3ae92acd
commit e5a3ea6863
3 changed files with 93 additions and 40 deletions

View File

@@ -800,6 +800,10 @@ func isAutonomySyntheticMessage(msg bus.InboundMessage) bool {
return strings.EqualFold(strings.TrimSpace(msg.Metadata["source"]), "autonomy") return strings.EqualFold(strings.TrimSpace(msg.Metadata["source"]), "autonomy")
} }
func shouldHandleControlIntents(msg bus.InboundMessage) bool {
return !isSyntheticMessage(msg)
}
func (al *AgentLoop) ProcessDirect(ctx context.Context, content, sessionKey string) (string, error) { func (al *AgentLoop) ProcessDirect(ctx context.Context, content, sessionKey string) (string, error) {
msg := bus.InboundMessage{ msg := bus.InboundMessage{
Channel: "cli", Channel: "cli",
@@ -827,56 +831,64 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
defer al.finishAutonomyRound(msg.SessionKey) defer al.finishAutonomyRound(msg.SessionKey)
} }
controlEligible := shouldHandleControlIntents(msg)
// Route system messages to processSystemMessage // Route system messages to processSystemMessage
if msg.Channel == "system" { if msg.Channel == "system" {
return al.processSystemMessage(ctx, msg) return al.processSystemMessage(ctx, msg)
} }
// Built-in slash commands (deterministic, no LLM roundtrip) // Built-in slash commands (deterministic, no LLM roundtrip)
if handled, result, err := al.handleSlashCommand(msg); handled { if controlEligible {
return result, err if handled, result, err := al.handleSlashCommand(msg); handled {
} return result, err
al.noteAutonomyUserActivity(msg)
if intent, ok := al.detectAutonomyIntent(ctx, msg.Content); ok {
switch intent.action {
case "start":
idle := autonomyDefaultIdleInterval
if intent.idleInterval != nil {
idle = *intent.idleInterval
}
return al.startAutonomy(msg, idle, intent.focus), nil
case "clear_focus":
if al.clearAutonomyFocus(msg.SessionKey) {
return "已确认:当前研究方向已完成,后续自主推进将转向其他高价值任务。", nil
}
return "自主模式当前未运行,无法清空研究方向。", nil
case "stop":
if al.stopAutonomy(msg.SessionKey) {
return "自主模式已关闭。", nil
}
return "自主模式当前未运行。", nil
case "status":
return al.autonomyStatus(msg.SessionKey), nil
} }
} }
if intent, ok := al.detectAutoLearnIntent(ctx, msg.Content); ok { if controlEligible {
switch intent.action { al.noteAutonomyUserActivity(msg)
case "start": }
interval := autoLearnDefaultInterval
if intent.interval != nil { if controlEligible {
interval = *intent.interval if intent, ok := al.detectAutonomyIntent(ctx, msg.Content); ok {
switch intent.action {
case "start":
idle := autonomyDefaultIdleInterval
if intent.idleInterval != nil {
idle = *intent.idleInterval
}
return al.startAutonomy(msg, idle, intent.focus), nil
case "clear_focus":
if al.clearAutonomyFocus(msg.SessionKey) {
return "已确认:当前研究方向已完成,后续自主推进将转向其他高价值任务。", nil
}
return "自主模式当前未运行,无法清空研究方向。", nil
case "stop":
if al.stopAutonomy(msg.SessionKey) {
return "自主模式已关闭。", nil
}
return "自主模式当前未运行。", nil
case "status":
return al.autonomyStatus(msg.SessionKey), nil
} }
return al.startAutoLearner(msg, interval), nil }
case "stop":
if al.stopAutoLearner(msg.SessionKey) { if intent, ok := al.detectAutoLearnIntent(ctx, msg.Content); ok {
return "自动学习已停止。", nil switch intent.action {
case "start":
interval := autoLearnDefaultInterval
if intent.interval != nil {
interval = *intent.interval
}
return al.startAutoLearner(msg, interval), nil
case "stop":
if al.stopAutoLearner(msg.SessionKey) {
return "自动学习已停止。", nil
}
return "自动学习当前未运行。", nil
case "status":
return al.autoLearnerStatus(msg.SessionKey), nil
} }
return "自动学习当前未运行。", nil
case "status":
return al.autoLearnerStatus(msg.SessionKey), nil
} }
} }
@@ -891,7 +903,7 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
if strings.TrimSpace(userPrompt) == "" { if strings.TrimSpace(userPrompt) == "" {
userPrompt = msg.Content userPrompt = msg.Content
} }
if al.isAutonomyEnabled(msg.SessionKey) && !isSyntheticMessage(msg) { if al.isAutonomyEnabled(msg.SessionKey) && controlEligible {
directives.stageReport = true directives.stageReport = true
userPrompt = buildAutonomyTaskPrompt(userPrompt) userPrompt = buildAutonomyTaskPrompt(userPrompt)
} }

View File

@@ -3,6 +3,8 @@ package agent
import ( import (
"testing" "testing"
"time" "time"
"clawgo/pkg/bus"
) )
func TestParseTaskExecutionDirectives_RunCommand(t *testing.T) { func TestParseTaskExecutionDirectives_RunCommand(t *testing.T) {
@@ -152,3 +154,36 @@ func TestExtractJSONObject_Invalid(t *testing.T) {
t.Fatalf("expected empty json, got: %q", raw) t.Fatalf("expected empty json, got: %q", raw)
} }
} }
func TestShouldHandleControlIntents_UserMessage(t *testing.T) {
msg := bus.InboundMessage{
SenderID: "user",
Content: "请进入自主模式",
}
if !shouldHandleControlIntents(msg) {
t.Fatalf("expected user message to be control-eligible")
}
}
func TestShouldHandleControlIntents_AutonomySyntheticSender(t *testing.T) {
msg := bus.InboundMessage{
SenderID: "autonomy",
Content: "自主模式第 1 轮推进",
}
if shouldHandleControlIntents(msg) {
t.Fatalf("expected autonomy synthetic message to be ignored for control intents")
}
}
func TestShouldHandleControlIntents_AutoLearnSyntheticMetadata(t *testing.T) {
msg := bus.InboundMessage{
SenderID: "gateway",
Content: "自动学习第 1 轮",
Metadata: map[string]string{
"source": "autolearn",
},
}
if shouldHandleControlIntents(msg) {
t.Fatalf("expected autolearn synthetic metadata message to be ignored for control intents")
}
}

View File

@@ -328,6 +328,12 @@ func (c *TelegramChannel) handleMessage(runCtx context.Context, message *telego.
if user == nil { if user == nil {
return return
} }
if user.IsBot {
logger.DebugCF("telegram", "Ignoring bot-originated message", map[string]interface{}{
"user_id": user.ID,
})
return
}
senderID := fmt.Sprintf("%d", user.ID) senderID := fmt.Sprintf("%d", user.ID)