1
1
'use strict' ;
2
2
3
3
import { dynamicResponse } from '@dr' ;
4
- import { activeSessionRooms } from '@socketio' ;
4
+ import { activeSessionRooms , io } from '@socketio' ;
5
5
import {
6
6
getAgentById ,
7
7
getAgentNameMap ,
@@ -13,7 +13,8 @@ import { getAppById, unsafeGetAppById } from 'db/app';
13
13
import {
14
14
getChatMessageAfterId ,
15
15
getChatMessagesBySession ,
16
- unsafeGetChatMessagesBySession
16
+ unsafeGetChatMessagesBySession ,
17
+ upsertOrUpdateChatMessage
17
18
} from 'db/chat' ;
18
19
import { getCrewById , getCrewsByTeam , unsafeGetCrewById } from 'db/crew' ;
19
20
import {
@@ -33,6 +34,7 @@ import toObjectId from 'misc/toobjectid';
33
34
import { ObjectId } from 'mongodb' ;
34
35
import { sessionTaskQueue } from 'queue/bull' ;
35
36
import { client } from 'redis/redis' ;
37
+ import { v4 as uuidv4 } from 'uuid' ;
36
38
const log = debug ( 'webapp:controllers:session' ) ;
37
39
import { App , AppType } from 'struct/app' ;
38
40
import { SessionStatus } from 'struct/session' ;
@@ -536,6 +538,84 @@ export async function editSessionApi(req, res, next) {
536
538
} ) ;
537
539
}
538
540
541
+ export async function sendMessage ( req , res , next ) {
542
+ try {
543
+ const { sessionId } = req . params ;
544
+ const { messageText } = req . body ;
545
+ log (
546
+ `Sending message with found req.params = ${ req . params . sessionId } \nand req.body.messageText = ${ req . body . messageText } \nand req.body.sessionId = ${ req . body . sessionId } `
547
+ ) ;
548
+
549
+ let validationError = chainValidations (
550
+ req . body ,
551
+ [
552
+ {
553
+ field : 'messageText' ,
554
+ validation : { notEmpty : true , ofType : 'string' }
555
+ }
556
+ ] ,
557
+ {
558
+ messageText : 'Message Text'
559
+ }
560
+ ) ;
561
+
562
+ if ( validationError ) {
563
+ return dynamicResponse ( req , res , 400 , { error : validationError } ) ;
564
+ }
565
+
566
+ const session = await unsafeGetSessionById ( sessionId ) ;
567
+ if ( ! session ) {
568
+ return dynamicResponse ( req , res , 400 , { error : 'Session not found' } ) ;
569
+ }
570
+
571
+ const activeRoomSessionId = `_${ sessionId } ` ;
572
+
573
+ if ( ! activeSessionRooms . includes ( activeRoomSessionId ) ) {
574
+ activeSessionRooms . forEach ( v => {
575
+ log ( `roomId: ${ v } ` ) ;
576
+ } ) ;
577
+ return dynamicResponse ( req , res , 400 , {
578
+ error : 'Attempting to send message to inactive session.'
579
+ } ) ;
580
+ }
581
+
582
+ const messagePayload = {
583
+ room : activeRoomSessionId ,
584
+ message : messageText ,
585
+ authorName : res . locals ?. account ?. name || 'External API' ,
586
+ event : 'message'
587
+ } ;
588
+
589
+ console . log ( 'Message payload:' , JSON . stringify ( messagePayload , null , 2 ) ) ;
590
+
591
+ io . to ( activeRoomSessionId ) . emit ( 'message' , messagePayload ) ;
592
+
593
+ // // Persist the message in the database
594
+ // await upsertOrUpdateChatMessage(sessionId, {
595
+ // orgId: session.orgId,
596
+ // teamId: session.teamId,
597
+ // sessionId: session._id,
598
+ // authorId: res.locals?.account?._id || null, // API-triggered, no user ID
599
+ // authorName: res.locals?.account?.name || "External API",
600
+ // ts: Date.now(),
601
+ // message: messagePayload.message
602
+ // }, {
603
+ // ts: Date.now(),
604
+ // chunk: messagePayload.message,
605
+ // tokens: 0 // Update if tokenization is relevant
606
+ // });
607
+
608
+ return dynamicResponse ( req , res , 200 , {
609
+ message : 'Message sent successfully'
610
+ } ) ;
611
+ } catch ( error ) {
612
+ console . error ( error ) ;
613
+ return dynamicResponse ( req , res , 500 , {
614
+ error : 'Internal server error'
615
+ } ) ;
616
+ }
617
+ }
618
+
539
619
export async function startSession ( req , res , next ) {
540
620
const session = await unsafeGetSessionById ( req . body . sessionId ) ;
541
621
const canAccess = await checkCanAccessApp ( session ?. appId ?. toString ( ) , false , res . locals . account ) ;
0 commit comments