@@ -15,6 +15,14 @@ import {
1515} from "../../agents/tools/common.js" ;
1616import type { SourceReplyDeliveryMode } from "../../auto-reply/get-reply-options.types.js" ;
1717import 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" ;
1826import { normalizeChatType , type ChatType } from "../../channels/chat-type.js" ;
1927import type { InboundEventKind } from "../../channels/inbound-event/kind.js" ;
2028import { 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+
470494function 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