@@ -80,6 +80,7 @@ import {
8080 type MattermostWebSocketFactory ,
8181} from "./monitor-websocket.js" ;
8282import { runWithReconnect } from "./reconnect.js" ;
83+ import { deliverMattermostReplyPayload } from "./reply-delivery.js" ;
8384import { sendMessageMattermost } from "./send.js" ;
8485import {
8586 DEFAULT_COMMAND_SPECS ,
@@ -682,44 +683,21 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {}
682683 ...prefixOptions ,
683684 humanDelay : core . channel . reply . resolveHumanDelayConfig ( cfg , route . agentId ) ,
684685 deliver : async ( payload : ReplyPayload ) => {
685- const mediaUrls = payload . mediaUrls ?? ( payload . mediaUrl ? [ payload . mediaUrl ] : [ ] ) ;
686- const text = core . channel . text . convertMarkdownTables ( payload . text ?? "" , tableMode ) ;
687- if ( mediaUrls . length === 0 ) {
688- const chunkMode = core . channel . text . resolveChunkMode (
689- cfg ,
690- "mattermost" ,
691- account . accountId ,
692- ) ;
693- const chunks = core . channel . text . chunkMarkdownTextWithMode (
694- text ,
695- textLimit ,
696- chunkMode ,
697- ) ;
698- for ( const chunk of chunks . length > 0 ? chunks : [ text ] ) {
699- if ( ! chunk ) continue ;
700- await sendMessageMattermost ( to , chunk , {
701- accountId : account . accountId ,
702- replyToId : resolveMattermostReplyRootId ( {
703- threadRootId : threadContext . effectiveReplyToId ,
704- replyToId : payload . replyToId ,
705- } ) ,
706- } ) ;
707- }
708- } else {
709- let first = true ;
710- for ( const mediaUrl of mediaUrls ) {
711- const caption = first ? text : "" ;
712- first = false ;
713- await sendMessageMattermost ( to , caption , {
714- accountId : account . accountId ,
715- mediaUrl,
716- replyToId : resolveMattermostReplyRootId ( {
717- threadRootId : threadContext . effectiveReplyToId ,
718- replyToId : payload . replyToId ,
719- } ) ,
720- } ) ;
721- }
722- }
686+ await deliverMattermostReplyPayload ( {
687+ core,
688+ cfg,
689+ payload,
690+ to,
691+ accountId : account . accountId ,
692+ agentId : route . agentId ,
693+ replyToId : resolveMattermostReplyRootId ( {
694+ threadRootId : threadContext . effectiveReplyToId ,
695+ replyToId : payload . replyToId ,
696+ } ) ,
697+ textLimit,
698+ tableMode,
699+ sendMessage : sendMessageMattermost ,
700+ } ) ;
723701 runtime . log ?.( `delivered button-click reply to ${ to } ` ) ;
724702 } ,
725703 onError : ( err , info ) => {
@@ -1000,53 +978,34 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {}
1000978 ...prefixOptions ,
1001979 // Picker-triggered confirmations should stay immediate.
1002980 deliver : async ( payload : ReplyPayload ) => {
1003- const mediaUrls = payload . mediaUrls ?? ( payload . mediaUrl ? [ payload . mediaUrl ] : [ ] ) ;
1004- const text = core . channel . text
1005- . convertMarkdownTables ( payload . text ?? "" , tableMode )
1006- . trim ( ) ;
981+ const trimmedPayload = {
982+ ... payload ,
983+ text : core . channel . text . convertMarkdownTables ( payload . text ?? "" , tableMode ) . trim ( ) ,
984+ } ;
1007985
1008986 if ( ! shouldDeliverReplies ) {
1009- if ( text ) {
1010- capturedTexts . push ( text ) ;
987+ if ( trimmedPayload . text ) {
988+ capturedTexts . push ( trimmedPayload . text ) ;
1011989 }
1012990 return ;
1013991 }
1014992
1015- if ( mediaUrls . length === 0 ) {
1016- const chunkMode = core . channel . text . resolveChunkMode (
1017- cfg ,
1018- "mattermost" ,
1019- account . accountId ,
1020- ) ;
1021- const chunks = core . channel . text . chunkMarkdownTextWithMode ( text , textLimit , chunkMode ) ;
1022- for ( const chunk of chunks . length > 0 ? chunks : [ text ] ) {
1023- if ( ! chunk ) {
1024- continue ;
1025- }
1026- await sendMessageMattermost ( to , chunk , {
1027- accountId : account . accountId ,
1028- replyToId : resolveMattermostReplyRootId ( {
1029- threadRootId : params . effectiveReplyToId ,
1030- replyToId : payload . replyToId ,
1031- } ) ,
1032- } ) ;
1033- }
1034- return ;
1035- }
1036-
1037- let first = true ;
1038- for ( const mediaUrl of mediaUrls ) {
1039- const caption = first ? text : "" ;
1040- first = false ;
1041- await sendMessageMattermost ( to , caption , {
1042- accountId : account . accountId ,
1043- mediaUrl,
1044- replyToId : resolveMattermostReplyRootId ( {
1045- threadRootId : params . effectiveReplyToId ,
1046- replyToId : payload . replyToId ,
1047- } ) ,
1048- } ) ;
1049- }
993+ await deliverMattermostReplyPayload ( {
994+ core,
995+ cfg,
996+ payload : trimmedPayload ,
997+ to,
998+ accountId : account . accountId ,
999+ agentId : params . route . agentId ,
1000+ replyToId : resolveMattermostReplyRootId ( {
1001+ threadRootId : params . effectiveReplyToId ,
1002+ replyToId : trimmedPayload . replyToId ,
1003+ } ) ,
1004+ textLimit,
1005+ // The picker path already converts and trims text before capture/delivery.
1006+ tableMode : "off" ,
1007+ sendMessage : sendMessageMattermost ,
1008+ } ) ;
10501009 } ,
10511010 onError : ( err , info ) => {
10521011 runtime . error ?.( `mattermost model picker ${ info . kind } reply failed: ${ String ( err ) } ` ) ;
@@ -1743,42 +1702,21 @@ export async function monitorMattermostProvider(opts: MonitorMattermostOpts = {}
17431702 humanDelay : core . channel . reply . resolveHumanDelayConfig ( cfg , route . agentId ) ,
17441703 typingCallbacks,
17451704 deliver : async ( payload : ReplyPayload ) => {
1746- const mediaUrls = payload . mediaUrls ?? ( payload . mediaUrl ? [ payload . mediaUrl ] : [ ] ) ;
1747- const text = core . channel . text . convertMarkdownTables ( payload . text ?? "" , tableMode ) ;
1748- if ( mediaUrls . length === 0 ) {
1749- const chunkMode = core . channel . text . resolveChunkMode (
1750- cfg ,
1751- "mattermost" ,
1752- account . accountId ,
1753- ) ;
1754- const chunks = core . channel . text . chunkMarkdownTextWithMode ( text , textLimit , chunkMode ) ;
1755- for ( const chunk of chunks . length > 0 ? chunks : [ text ] ) {
1756- if ( ! chunk ) {
1757- continue ;
1758- }
1759- await sendMessageMattermost ( to , chunk , {
1760- accountId : account . accountId ,
1761- replyToId : resolveMattermostReplyRootId ( {
1762- threadRootId : effectiveReplyToId ,
1763- replyToId : payload . replyToId ,
1764- } ) ,
1765- } ) ;
1766- }
1767- } else {
1768- let first = true ;
1769- for ( const mediaUrl of mediaUrls ) {
1770- const caption = first ? text : "" ;
1771- first = false ;
1772- await sendMessageMattermost ( to , caption , {
1773- accountId : account . accountId ,
1774- mediaUrl,
1775- replyToId : resolveMattermostReplyRootId ( {
1776- threadRootId : effectiveReplyToId ,
1777- replyToId : payload . replyToId ,
1778- } ) ,
1779- } ) ;
1780- }
1781- }
1705+ await deliverMattermostReplyPayload ( {
1706+ core,
1707+ cfg,
1708+ payload,
1709+ to,
1710+ accountId : account . accountId ,
1711+ agentId : route . agentId ,
1712+ replyToId : resolveMattermostReplyRootId ( {
1713+ threadRootId : effectiveReplyToId ,
1714+ replyToId : payload . replyToId ,
1715+ } ) ,
1716+ textLimit,
1717+ tableMode,
1718+ sendMessage : sendMessageMattermost ,
1719+ } ) ;
17821720 runtime . log ?.( `delivered reply to ${ to } ` ) ;
17831721 } ,
17841722 onError : ( err , info ) => {
0 commit comments