Skip to content

Commit a67bda8

Browse files
committed
adding 'session/:sessionId/send-message' endpoint that allows for message sending, there exists an issue where the user-sent message doesn't appear in DB or show in the UI chat but the message sends successfully
1 parent 8d7d053 commit a67bda8

File tree

7 files changed

+77991
-21
lines changed

7 files changed

+77991
-21
lines changed

webapp/src/animations/SignInAnimationTransparent.json

+5,868-1
Large diffs are not rendered by default.

webapp/src/animations/dataSyncLoaderAnimation.json

+71,998-1
Large diffs are not rendered by default.

webapp/src/controllers/session.ts

+82-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import { dynamicResponse } from '@dr';
4-
import { activeSessionRooms } from '@socketio';
4+
import { activeSessionRooms, io } from '@socketio';
55
import {
66
getAgentById,
77
getAgentNameMap,
@@ -13,7 +13,8 @@ import { getAppById, unsafeGetAppById } from 'db/app';
1313
import {
1414
getChatMessageAfterId,
1515
getChatMessagesBySession,
16-
unsafeGetChatMessagesBySession
16+
unsafeGetChatMessagesBySession,
17+
upsertOrUpdateChatMessage
1718
} from 'db/chat';
1819
import { getCrewById, getCrewsByTeam, unsafeGetCrewById } from 'db/crew';
1920
import {
@@ -33,6 +34,7 @@ import toObjectId from 'misc/toobjectid';
3334
import { ObjectId } from 'mongodb';
3435
import { sessionTaskQueue } from 'queue/bull';
3536
import { client } from 'redis/redis';
37+
import { v4 as uuidv4 } from 'uuid';
3638
const log = debug('webapp:controllers:session');
3739
import { App, AppType } from 'struct/app';
3840
import { SessionStatus } from 'struct/session';
@@ -536,6 +538,84 @@ export async function editSessionApi(req, res, next) {
536538
});
537539
}
538540

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+
539619
export async function startSession(req, res, next) {
540620
const session = await unsafeGetSessionById(req.body.sessionId);
541621
const canAccess = await checkCanAccessApp(session?.appId?.toString(), false, res.locals.account);

webapp/src/pages/globals.css

+33-15
Original file line numberDiff line numberDiff line change
@@ -298,26 +298,44 @@ div.bg-gray-200.border.rounded-sm.flex.space-x-1.pl-1 {
298298
}
299299

300300
.gradient-animation {
301-
background: linear-gradient(226deg, #612d89, #4f46e5);
302-
background-size: 400% 400%;
301+
background: linear-gradient(226deg, #612d89, #4f46e5);
302+
background-size: 400% 400%;
303303

304-
-webkit-animation: left-frame-gradient 8s ease infinite;
305-
-moz-animation: left-frame-gradient 8s ease infinite;
306-
animation: left-frame-gradient 8s ease infinite;
304+
-webkit-animation: left-frame-gradient 8s ease infinite;
305+
-moz-animation: left-frame-gradient 8s ease infinite;
306+
animation: left-frame-gradient 8s ease infinite;
307307
}
308308

309309
@-webkit-keyframes left-frame-gradient {
310-
0%{background-position:94% 0%}
311-
50%{background-position:7% 100%}
312-
100%{background-position:94% 0%}
310+
0% {
311+
background-position: 94% 0%;
312+
}
313+
50% {
314+
background-position: 7% 100%;
315+
}
316+
100% {
317+
background-position: 94% 0%;
318+
}
313319
}
314320
@-moz-keyframes left-frame-gradient {
315-
0%{background-position:94% 0%}
316-
50%{background-position:7% 100%}
317-
100%{background-position:94% 0%}
321+
0% {
322+
background-position: 94% 0%;
323+
}
324+
50% {
325+
background-position: 7% 100%;
326+
}
327+
100% {
328+
background-position: 94% 0%;
329+
}
318330
}
319331
@keyframes left-frame-gradient {
320-
0%{background-position:94% 0%}
321-
50%{background-position:7% 100%}
322-
100%{background-position:94% 0%}
323-
}
332+
0% {
333+
background-position: 94% 0%;
334+
}
335+
50% {
336+
background-position: 7% 100%;
337+
}
338+
100% {
339+
background-position: 94% 0%;
340+
}
341+
}

webapp/src/pages/login.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as API from '@api';
22
import { CubeIcon, EyeIcon, EyeSlashIcon } from '@heroicons/react/24/outline';
3+
import animationData from 'animations/SignInAnimationTransparent.json';
34
import ButtonSpinner from 'components/ButtonSpinner';
45
import ErrorAlert from 'components/ErrorAlert';
56
import InputField from 'components/form/InputField';
67
import InfoAlert from 'components/InfoAlert';
8+
import LeftFrame from 'components/onboarding/LeftFrame';
79
import SuccessAlert from 'components/SuccessAlert';
810
import { useSocketContext } from 'context/socket';
911
import { useThemeContext } from 'context/themecontext';
@@ -15,8 +17,6 @@ import { useRouter } from 'next/router';
1517
import { usePostHog } from 'posthog-js/react';
1618
import { useState } from 'react';
1719
import { useForm } from 'react-hook-form';
18-
import animationData from 'animations/SignInAnimationTransparent.json';
19-
import LeftFrame from 'components/onboarding/LeftFrame';
2020

2121
export interface LoginFormValues {
2222
name: string;

webapp/src/router.ts

+6
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,12 @@ export default function router(server, app) {
379379
teamRouter.post('/forms/session/:sessionId([a-f0-9]{24})/edit', sessionController.editSessionApi);
380380
teamRouter.post('/forms/session/:sessionId([a-f0-9]{24})/start', sessionController.startSession);
381381

382+
//messages
383+
teamRouter.post(
384+
'/session/:sessionId([a-f0-9]{24})/send-message',
385+
sessionController.sendMessage
386+
);
387+
382388
//agents
383389
teamRouter.get('/agents', agentController.agentsPage.bind(null, app));
384390
teamRouter.get('/agents.json', agentController.agentsJson);

webapp/src/socketio.ts

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ export function initSocket(rawHttpServer) {
149149
const socketRequest = socket.request as any;
150150
data.event = data.event || 'message';
151151

152+
log('socket.id "%s" event "message" args: %O', socket.id, data);
153+
152154
if (typeof data.message !== 'object') {
153155
data.message = {
154156
type: 'text',

0 commit comments

Comments
 (0)