Skip to content

Commit 87fa5f0

Browse files
zhangtong26lanzhi-lee
authored andcommitted
fix(outbound): capture NO_REPLY token before directive parsing and refactor suppressed send payload
(cherry picked from commit 8d4cbb113dd58780493184d458d3c94c855e6a6f)
1 parent 0c4fc0a commit 87fa5f0

3 files changed

Lines changed: 153 additions & 1 deletion

File tree

‎src/infra/outbound/message-action-runner.core-send.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,55 @@ describe("runMessageAction core send routing", () => {
187187
expect(mediaInput.mediaUrl).toBe("https://example.com/file.txt");
188188
});
189189

190+
it("strips trailing NO_REPLY from direct tool sends before delivery", async () => {
191+
const sendText = vi.fn().mockResolvedValue({
192+
channel: "testchat",
193+
messageId: "t3",
194+
chatId: "c1",
195+
});
196+
setActivePluginRegistry(
197+
createTestRegistry([
198+
{
199+
pluginId: "testchat",
200+
source: "test",
201+
plugin: createOutboundTestPlugin({
202+
id: "testchat",
203+
outbound: {
204+
deliveryMode: "direct",
205+
sendText,
206+
},
207+
}),
208+
},
209+
]),
210+
);
211+
const cfg = {
212+
channels: {
213+
testchat: {
214+
enabled: true,
215+
},
216+
},
217+
} as OpenClawConfig;
218+
219+
const result = await runMessageAction({
220+
cfg,
221+
action: "send",
222+
params: {
223+
channel: "testchat",
224+
target: "channel:abc",
225+
message: "visible update\n\nNO_REPLY",
226+
},
227+
toolContext: {
228+
chatType: "direct",
229+
},
230+
dryRun: false,
231+
});
232+
233+
expect(result.kind).toBe("send");
234+
expect(sendText).toHaveBeenCalledOnce();
235+
const textInput = firstMockArg(sendText, "send text");
236+
expect(textInput.text).toBe("visible update");
237+
});
238+
190239
it("accepts Telegram numeric forum topic targets through plugin-owned grammar", async () => {
191240
setActivePluginRegistry(
192241
createTestRegistry([

‎src/infra/outbound/message-action-runner.send-validation.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,30 @@ describe("runMessageAction send validation", () => {
8989
expect(result.kind).toBe("send");
9090
});
9191

92+
it("returns silent success when NO_REPLY stripping leaves no send payload", async () => {
93+
const result = await runDrySend({
94+
cfg: workspaceConfig,
95+
actionParams: {
96+
channel: "workspace",
97+
target: "#C12345678",
98+
message: "NO_REPLY",
99+
},
100+
});
101+
102+
expect(result).toMatchObject({
103+
kind: "send",
104+
channel: "workspace",
105+
to: "C12345678",
106+
handledBy: "core",
107+
dryRun: true,
108+
payload: {
109+
ok: true,
110+
suppressed: true,
111+
reason: "silent_reply_token",
112+
},
113+
});
114+
});
115+
92116
it("uses the current internal UI source as the message-tool-only send sink", async () => {
93117
const result = await runMessageAction({
94118
cfg: emptyConfig,

‎src/infra/outbound/message-action-runner.ts‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ import {
1515
} from "../../agents/tools/common.js";
1616
import type { SourceReplyDeliveryMode } from "../../auto-reply/get-reply-options.types.js";
1717
import type { ReplyPayload } from "../../auto-reply/reply-payload.js";
18+
import { parseReplyDirectives } from "../../auto-reply/reply/reply-directives.js";
19+
import {
20+
isSilentReplyText,
21+
SILENT_REPLY_TOKEN,
22+
startsWithSilentToken,
23+
stripLeadingSilentToken,
24+
stripSilentToken,
25+
} from "../../auto-reply/tokens.js";
1826
import { normalizeChatType, type ChatType } from "../../channels/chat-type.js";
1927
import type { InboundEventKind } from "../../channels/inbound-event/kind.js";
2028
import { getChannelPlugin } from "../../channels/plugins/index.js";
@@ -455,7 +463,14 @@ type ResolvedActionContext = {
455463
abortSignal?: AbortSignal;
456464
};
457465

458-
type SendPayloadParts = {
466+
type SendPayloadSuppressedPayload = {
467+
ok: true;
468+
suppressed: true;
469+
reason: "silent_reply_token";
470+
};
471+
472+
type DeliverableSendPayloadParts = {
473+
suppressed?: false;
459474
message: string;
460475
payload: ReplyPayload;
461476
mediaUrl?: string;
@@ -467,10 +482,22 @@ type SendPayloadParts = {
467482
silent?: boolean;
468483
};
469484

485+
type SuppressedSendPayloadParts = {
486+
suppressed: true;
487+
reason: "silent_reply_token";
488+
message: "";
489+
payload: SendPayloadSuppressedPayload;
490+
};
491+
492+
type SendPayloadParts = DeliverableSendPayloadParts | SuppressedSendPayloadParts;
493+
470494
function updateSendPayloadPartsFromReplyPayload(
471495
parts: SendPayloadParts,
472496
payload: ReplyPayload,
473497
): SendPayloadParts {
498+
if (parts.suppressed) {
499+
return parts;
500+
}
474501
const sendable = resolveSendableOutboundReplyParts(payload);
475502
const mediaUrls = sendable.mediaUrls.length > 0 ? sendable.mediaUrls : undefined;
476503
return {
@@ -487,6 +514,9 @@ function applySendPayloadPartsToActionParams(
487514
actionParams: Record<string, unknown>,
488515
parts: SendPayloadParts,
489516
) {
517+
if (parts.suppressed) {
518+
return;
519+
}
490520
actionParams.message = parts.message;
491521
actionParams.media = parts.mediaUrl;
492522
actionParams.mediaUrl = parts.mediaUrl;
@@ -833,6 +863,17 @@ async function handleInternalSourceReplySendAction(
833863
? resolveSessionAgentId({ sessionKey: input.sessionKey, config: input.cfg })
834864
: undefined),
835865
});
866+
if (sourceReply.suppressed) {
867+
return {
868+
kind: "send",
869+
channel: INTERNAL_MESSAGE_CHANNEL,
870+
action: "send",
871+
to: "current-run",
872+
handledBy: "internal-source",
873+
payload: sourceReply.payload,
874+
dryRun,
875+
};
876+
}
836877
const payload = {
837878
status: "ok",
838879
deliveryStatus: dryRun ? "dry_run" : "sent",
@@ -959,10 +1000,17 @@ async function buildSendPayloadParts(params: {
9591000
message = caption;
9601001
}
9611002

1003+
const rawMessage = message;
9621004
const parsed = parseInlineDirectives(message, {
9631005
stripAudioTag: true,
9641006
stripReplyTags: true,
9651007
});
1008+
const replyDirectives = parseReplyDirectives(rawMessage);
1009+
const hadSilentReplyToken =
1010+
replyDirectives.isSilent ||
1011+
rawMessage.includes(SILENT_REPLY_TOKEN) ||
1012+
isSilentReplyText(rawMessage) ||
1013+
startsWithSilentToken(rawMessage, SILENT_REPLY_TOKEN);
9661014
const mergedMediaUrls: string[] = [];
9671015
const seenMedia = new Set<string>();
9681016
const pushMedia = (value?: string | null) => {
@@ -989,6 +1037,18 @@ async function buildSendPayloadParts(params: {
9891037
mergedMediaUrls.push(...normalizedMediaUrls);
9901038

9911039
message = stripPlainTextToolCallBlocks(stripUnsupportedCitationControlMarkers(parsed.text));
1040+
const isGroupContext =
1041+
input.toolContext?.chatType === "group" || input.toolContext?.chatType === "channel";
1042+
if (message && hadSilentReplyToken) {
1043+
if (isGroupContext) {
1044+
message = "";
1045+
} else {
1046+
message =
1047+
isSilentReplyText(message) || startsWithSilentToken(message, SILENT_REPLY_TOKEN)
1048+
? stripLeadingSilentToken(message, SILENT_REPLY_TOKEN)
1049+
: stripSilentToken(message);
1050+
}
1051+
}
9921052
actionParams.message = message;
9931053
if (!actionParams.replyTo && parsed.replyToId) {
9941054
actionParams.replyTo = parsed.replyToId;
@@ -1023,6 +1083,14 @@ async function buildSendPayloadParts(params: {
10231083
interactive: actionParams.interactive,
10241084
})
10251085
) {
1086+
if (hadSilentReplyToken) {
1087+
return {
1088+
suppressed: true,
1089+
reason: "silent_reply_token",
1090+
message: "",
1091+
payload: { ok: true, suppressed: true, reason: "silent_reply_token" },
1092+
};
1093+
}
10261094
throw new Error("send requires text or media");
10271095
}
10281096
actionParams.message = message;
@@ -1098,6 +1166,17 @@ async function handleSendAction(ctx: ResolvedActionContext): Promise<MessageActi
10981166
accountId,
10991167
agentId,
11001168
});
1169+
if (sendPayload.suppressed) {
1170+
return {
1171+
kind: "send",
1172+
channel,
1173+
action,
1174+
to,
1175+
handledBy: "core",
1176+
payload: sendPayload.payload,
1177+
dryRun,
1178+
};
1179+
}
11011180

11021181
const replyToId = resolveAndApplyOutboundReplyToId(params, {
11031182
channel,

0 commit comments

Comments
 (0)