fix(agent): skip final reply only when message tool sent to same chat
When the message tool sends to a different chat_id (e.g. a newly created Feishu group), the final reply to the original chat was incorrectly suppressed. Track the target chat_id in MessageTool and only skip the outbound reply when it matches the inbound message's chat_id.
This commit is contained in:
parent
110fc71349
commit
5fb488ca31
2 changed files with 14 additions and 3 deletions
|
|
@ -336,15 +336,17 @@ func (al *AgentLoop) Run(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if response != "" {
|
if response != "" {
|
||||||
// Check if the message tool already sent a response during this round.
|
// Check if the message tool already sent a response to the SAME chat during this round.
|
||||||
// If so, skip publishing to avoid duplicate messages to the user.
|
// If so, skip publishing to avoid duplicate messages to the user.
|
||||||
|
// Only skip when the target chat_id matches — sending to a different chat
|
||||||
|
// (e.g. a newly created group) should not suppress the reply to the original chat.
|
||||||
// Use default agent's tools to check (message tool is shared).
|
// Use default agent's tools to check (message tool is shared).
|
||||||
alreadySent := false
|
alreadySent := false
|
||||||
defaultAgent := al.registry.GetDefaultAgent()
|
defaultAgent := al.registry.GetDefaultAgent()
|
||||||
if defaultAgent != nil {
|
if defaultAgent != nil {
|
||||||
if tool, ok := defaultAgent.Tools.Get("message"); ok {
|
if tool, ok := defaultAgent.Tools.Get("message"); ok {
|
||||||
if mt, ok := tool.(*tools.MessageTool); ok {
|
if mt, ok := tool.(*tools.MessageTool); ok {
|
||||||
alreadySent = mt.HasSentInRound()
|
alreadySent = mt.HasSentInRound() && mt.SentToChatID() == msg.ChatID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ type SendCallback func(channel, chatID, content string) error
|
||||||
type MessageTool struct {
|
type MessageTool struct {
|
||||||
sendCallback SendCallback
|
sendCallback SendCallback
|
||||||
sentInRound atomic.Bool // Tracks whether a message was sent in the current processing round
|
sentInRound atomic.Bool // Tracks whether a message was sent in the current processing round
|
||||||
|
sentChatID atomic.Value // Tracks the chat_id that the message was sent to in the current round
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMessageTool() *MessageTool {
|
func NewMessageTool() *MessageTool {
|
||||||
|
|
@ -50,6 +51,7 @@ func (t *MessageTool) Parameters() map[string]any {
|
||||||
// Called by the agent loop at the start of each inbound message processing round.
|
// Called by the agent loop at the start of each inbound message processing round.
|
||||||
func (t *MessageTool) ResetSentInRound() {
|
func (t *MessageTool) ResetSentInRound() {
|
||||||
t.sentInRound.Store(false)
|
t.sentInRound.Store(false)
|
||||||
|
t.sentChatID.Store("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasSentInRound returns true if the message tool sent a message during the current round.
|
// HasSentInRound returns true if the message tool sent a message during the current round.
|
||||||
|
|
@ -57,6 +59,12 @@ func (t *MessageTool) HasSentInRound() bool {
|
||||||
return t.sentInRound.Load()
|
return t.sentInRound.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SentToChatID returns the chat_id that the message was sent to in the current round.
|
||||||
|
func (t *MessageTool) SentToChatID() string {
|
||||||
|
v, _ := t.sentChatID.Load().(string)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func (t *MessageTool) SetSendCallback(callback SendCallback) {
|
func (t *MessageTool) SetSendCallback(callback SendCallback) {
|
||||||
t.sendCallback = callback
|
t.sendCallback = callback
|
||||||
}
|
}
|
||||||
|
|
@ -94,6 +102,7 @@ func (t *MessageTool) Execute(ctx context.Context, args map[string]any) *ToolRes
|
||||||
}
|
}
|
||||||
|
|
||||||
t.sentInRound.Store(true)
|
t.sentInRound.Store(true)
|
||||||
|
t.sentChatID.Store(chatID)
|
||||||
// Silent: user already received the message directly
|
// Silent: user already received the message directly
|
||||||
return &ToolResult{
|
return &ToolResult{
|
||||||
ForLLM: fmt.Sprintf("Message sent to %s:%s", channel, chatID),
|
ForLLM: fmt.Sprintf("Message sent to %s:%s", channel, chatID),
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue