fix(agent): reject send tool when targeting the same conversation

Pass replyTarget through the full pipeline (ChatRequest → gateway
identity → agent headers → MCP session) so the send tool can detect
when the destination matches the current conversation and return an
error guiding the agent to reply directly instead.
This commit is contained in:
Acbox
2026-03-11 16:59:42 +08:00
parent a2e5c4f893
commit 30653fbdbf
8 changed files with 40 additions and 2 deletions
@@ -227,6 +227,16 @@ func (p *Executor) callSend(ctx context.Context, session mcpgw.ToolSessionContex
return mcpgw.BuildToolErrorResult("target is required"), nil
}
// Reject send when the destination matches the current conversation.
if strings.EqualFold(channelType.String(), strings.TrimSpace(session.CurrentPlatform)) &&
target == strings.TrimSpace(session.ReplyTarget) {
return mcpgw.BuildToolErrorResult(
"You are trying to send a message to the SAME conversation you are already in. " +
"Do NOT use the send tool for this. Instead, write your reply as plain text directly. " +
"To include files, use the <attachments> block in your response (e.g. <attachments>[{\"type\":\"image\",\"path\":\"/data/media/file.jpg\"}]</attachments>).",
), nil
}
sendReq := channel.SendRequest{
Target: target,
Message: outboundMessage,
@@ -252,7 +252,7 @@ func TestExecutor_CallTool_SendError(t *testing.T) {
}
}
func TestExecutor_CallTool_Success(t *testing.T) {
func TestExecutor_CallTool_SameRouteRejected(t *testing.T) {
sender := &fakeSender{}
resolver := &fakeResolver{ct: channel.ChannelType("feishu")}
exec := NewExecutor(nil, sender, nil, resolver, nil)
@@ -263,6 +263,23 @@ func TestExecutor_CallTool_Success(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if isErr, _ := result["isError"].(bool); !isErr {
t.Error("expected error when sending to the same route as current session")
}
}
func TestExecutor_CallTool_Success(t *testing.T) {
sender := &fakeSender{}
resolver := &fakeResolver{ct: channel.ChannelType("feishu")}
exec := NewExecutor(nil, sender, nil, resolver, nil)
session := mcpgw.ToolSessionContext{BotID: "bot1", CurrentPlatform: "feishu", ReplyTarget: "chat1"}
result, err := exec.CallTool(context.Background(), session, toolSend, map[string]any{
"target": "chat2",
"text": "hello",
})
if err != nil {
t.Fatal(err)
}
if err := mcpgw.PayloadError(result); err != nil {
t.Fatal(err)
}
@@ -284,6 +301,7 @@ func TestExecutor_CallTool_ReplyTo(t *testing.T) {
exec := NewExecutor(nil, sender, nil, resolver, nil)
session := mcpgw.ToolSessionContext{BotID: "bot1", CurrentPlatform: "telegram", ReplyTarget: "123"}
result, err := exec.CallTool(context.Background(), session, toolSend, map[string]any{
"target": "456",
"text": "reply text",
"reply_to": "msg-789",
})
@@ -307,7 +325,8 @@ func TestExecutor_CallTool_NoReplyTo(t *testing.T) {
exec := NewExecutor(nil, sender, nil, resolver, nil)
session := mcpgw.ToolSessionContext{BotID: "bot1", CurrentPlatform: "telegram", ReplyTarget: "123"}
result, err := exec.CallTool(context.Background(), session, toolSend, map[string]any{
"text": "no reply",
"target": "456",
"text": "no reply",
})
if err != nil {
t.Fatal(err)