@@ -604,6 +604,45 @@ app.post('/api/mock/email-monitor/start', (req, res) => {
604604 res . json ( { ok : true , started : true , sessionId } )
605605} )
606606
607+ app . post ( '/api/sessions/:id/email-send' , async ( req , res ) => {
608+ const session = getSession ( req . params . id )
609+ if ( ! session ) return res . status ( 404 ) . json ( { error : 'Session not found' } )
610+
611+ const payload = session . payload as Record < string , unknown >
612+ const inbox = Array . isArray ( payload . inbox ) ? payload . inbox as Array < Record < string , unknown > > : [ ]
613+ const emailId = typeof req . body ?. emailId === 'string' ? req . body . emailId : ''
614+ if ( ! emailId ) return res . status ( 400 ) . json ( { error : 'Missing emailId' } )
615+
616+ const nextInbox = inbox . filter ( email => String ( email . id ?? '' ) !== emailId )
617+ const result = req . body && typeof req . body === 'object' ? req . body as Record < string , unknown > : { }
618+
619+ updateSessionPayload ( req . params . id , {
620+ ...payload ,
621+ inbox : nextInbox ,
622+ } )
623+ updateSessionPageStatus ( req . params . id , { state : 'submitted' , updatedAt : Date . now ( ) } )
624+
625+ let callbackFailed = false
626+ let callbackError = ''
627+ if ( session . sessionKey ) {
628+ try {
629+ const lines = [ '[agentclick] User sent an email reply from inbox review.' ]
630+ const editedDraft = result . editedDraft && typeof result . editedDraft === 'object'
631+ ? result . editedDraft as Record < string , unknown >
632+ : null
633+ const subject = typeof editedDraft ?. subject === 'string' ? editedDraft . subject : ''
634+ if ( subject ) lines . push ( `- Subject: ${ subject } ` )
635+ lines . push ( `- Removed email ${ emailId } from inbox UI after send.` )
636+ await callWebhook ( { message : lines . join ( '\n' ) , sessionKey : session . sessionKey , deliver : true } )
637+ } catch ( err ) {
638+ callbackFailed = true
639+ callbackError = String ( err )
640+ }
641+ }
642+
643+ res . json ( { ok : true , callbackFailed, callbackError, remaining : nextInbox . length } )
644+ } )
645+
607646app . get ( '/api/sessions/:id/summary' , ( req , res ) => {
608647 const session = getSession ( req . params . id )
609648 if ( ! session ) return res . status ( 404 ) . json ( { error : 'Session not found' } )
0 commit comments