@futdevpro/nts-dynamo 1.14.23 → 1.14.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@futdevpro/nts-dynamo",
3
- "version": "01.14.23",
3
+ "version": "01.14.24",
4
4
  "description": "Dynamic NodeTS (NodeJS-Typescript), MongoDB Backend System Framework by Future Development Program Ltd.",
5
5
  "DyBu_settings": {
6
6
  "packageType": "server-package",
@@ -68,6 +68,13 @@
68
68
  "typings": "./build/_modules/ai/_modules/open-ai/index.d.ts"
69
69
  },
70
70
 
71
+ "./assistant": {
72
+ "default": "./build/_modules/assistant/index.js",
73
+ "module": "./build/_modules/assistant/index.js",
74
+ "types": "./build/_modules/assistant/index.d.ts",
75
+ "typings": "./build/_modules/assistant/index.d.ts"
76
+ },
77
+
71
78
  "./bot": {
72
79
  "default": "./build/_modules/bot/index.js",
73
80
  "module": "./build/_modules/bot/index.js",
@@ -188,6 +195,10 @@
188
195
  "build/_modules/ai/_modules/open-ai/index.d.ts"
189
196
  ],
190
197
 
198
+ "assistant": [
199
+ "build/_modules/assistant/index.d.ts"
200
+ ],
201
+
191
202
  "bot": [
192
203
  "build/_modules/bot/index.d.ts"
193
204
  ],
@@ -0,0 +1,359 @@
1
+ # Dynamo NTS Messaging Module [DyNTS-MSG]
2
+
3
+ TODO: The Entire Messaging system is AI generated, therefore we need to clean it up, and remove the unnecessary elements
4
+
5
+ ## Overview
6
+
7
+ The DyNTS Messaging module provides the **backend implementation** for the unified messaging system. It handles all server-side operations including data persistence, business logic, real-time events, and API endpoints. The module integrates with bot and assistant modules to provide AI-powered messaging capabilities.
8
+
9
+ ## Import
10
+
11
+ ```typescript
12
+ import * from '@futdevpro/dynamo-nts/messaging';
13
+ ```
14
+
15
+ ## Key Features
16
+
17
+ - **Data Services**: CRUD operations for messages and conversations
18
+ - **Control Services**: Business logic orchestration and validation
19
+ - **Event Handling**: Real-time socket communication
20
+ - **API Endpoints**: RESTful HTTP endpoints for all messaging operations
21
+ - **Bot Integration**: Seamless integration with AI bot modules
22
+ - **Assistant Integration**: Support for AI assistant workflows
23
+ - **Authentication**: User authentication and authorization
24
+ - **Error Handling**: Comprehensive error handling and logging
25
+ - **Real-time Events**: Socket-based live updates
26
+ - **Agent Process Tracking**: AI reasoning and tool usage recording
27
+
28
+ ## Architecture
29
+
30
+ The messaging module follows a layered architecture:
31
+
32
+ ```
33
+ ┌─────────────────────────────────────┐
34
+ │ HTTP Controller (API) │
35
+ └────────────────┬──────────────────┘
36
+
37
+ ┌────────────────▼──────────────────┐
38
+ │ Control Services │
39
+ │ • Main Control │
40
+ │ • Integration Control │
41
+ │ • Events Service │
42
+ └────────────────┬──────────────────┘
43
+
44
+ ┌────────────────▼──────────────────┐
45
+ │ Data Services │
46
+ │ • Message Data Service │
47
+ │ • Conversation Data Service │
48
+ └────────────────┬──────────────────┘
49
+
50
+ ┌────────────────▼──────────────────┐
51
+ │ Database Layer │
52
+ └─────────────────────────────────┘
53
+ ```
54
+
55
+ ## Services
56
+
57
+ ### Data Services
58
+
59
+ #### `DyNTS_Msg_Message_DataService`
60
+
61
+ Handles all message data operations:
62
+
63
+ ```typescript
64
+ class DyNTS_Msg_Message_DataService {
65
+ async saveData(message: DyFM_Msg_Message): Promise<void>;
66
+ async getDataById(messageId: string): Promise<DyFM_Msg_Message>;
67
+ async markAsRead(messageId: string, userId: string): Promise<void>;
68
+ async deleteData(messageId: string): Promise<void>;
69
+ }
70
+ ```
71
+
72
+ **Usage Example:**
73
+ ```typescript
74
+ const messageService = new DyNTS_Msg_Message_DataService({
75
+ issuer: 'user-id',
76
+ });
77
+
78
+ const message = await messageService.getDataById('msg-123');
79
+ await messageService.markAsRead('msg-123', 'user-id');
80
+ ```
81
+
82
+ #### `DyNTS_Msg_Conversation_DataService`
83
+
84
+ Handles all conversation data operations:
85
+
86
+ ```typescript
87
+ class DyNTS_Msg_Conversation_DataService {
88
+ async saveData(conversation: DyFM_Msg_Conversation): Promise<void>;
89
+ async getDataById(conversationId: string): Promise<DyFM_Msg_Conversation>;
90
+ async updateLastMessage(conversationId: string, messageId: string, content: string): Promise<void>;
91
+ async addParticipant(conversationId: string, participant: DyFM_Msg_Participant): Promise<void>;
92
+ async removeParticipant(conversationId: string, userId: string): Promise<void>;
93
+ }
94
+ ```
95
+
96
+ ### Control Services
97
+
98
+ #### `DyNTS_Msg_Main_ControlService`
99
+
100
+ Singleton service for orchestration and business logic:
101
+
102
+ ```typescript
103
+ class DyNTS_Msg_Main_ControlService extends DyNTS_SingletonService {
104
+ static getInstance(): DyNTS_Msg_Main_ControlService;
105
+
106
+ // Messages
107
+ async sendMessage(conversationId: string, messageData: Partial<DyFM_Msg_Message>, senderId: string, issuer: string): Promise<DyFM_Msg_Message>;
108
+ async editMessage(messageId: string, content: string, userId: string, issuer: string): Promise<DyFM_Msg_Message>;
109
+ async deleteMessage(messageId: string, userId: string, issuer: string): Promise<void>;
110
+ async markMessagesAsRead(messageIds: string[], userId: string, issuer: string): Promise<{ success: boolean; count: number }>;
111
+
112
+ // Reactions
113
+ async addReaction(messageId: string, emoji: string, userId: string, issuer: string): Promise<DyFM_Msg_Message>;
114
+ async removeReaction(messageId: string, emoji: string, userId: string, issuer: string): Promise<DyFM_Msg_Message>;
115
+
116
+ // Conversations
117
+ async createConversation(conversationData: Partial<DyFM_Msg_Conversation>, creatorId: string, issuer: string): Promise<DyFM_Msg_Conversation>;
118
+ async updateConversation(conversationId: string, updateData: Partial<DyFM_Msg_Conversation>, userId: string, issuer: string): Promise<DyFM_Msg_Conversation>;
119
+ async deleteConversation(conversationId: string, userId: string, issuer: string): Promise<void>;
120
+
121
+ // Participants
122
+ async addParticipant(conversationId: string, newUserId: string, role: DyFM_Msg_ParticipantRole, requesterId: string, issuer: string): Promise<void>;
123
+ async removeParticipant(conversationId: string, userIdToRemove: string, requesterId: string, issuer: string): Promise<void>;
124
+ }
125
+ ```
126
+
127
+ **Usage Example:**
128
+ ```typescript
129
+ const controlService = DyNTS_Msg_Main_ControlService.getInstance();
130
+
131
+ // Send a message
132
+ const message = await controlService.sendMessage(
133
+ 'conv-123',
134
+ { content: 'Hello!' },
135
+ 'user-456',
136
+ 'user-456'
137
+ );
138
+
139
+ // Add reaction
140
+ await controlService.addReaction(
141
+ 'msg-789',
142
+ '👍',
143
+ 'user-456',
144
+ 'user-456'
145
+ );
146
+ ```
147
+
148
+ #### `DyNTS_Msg_Integration_ControlService`
149
+
150
+ Handles integration with bot and assistant modules:
151
+
152
+ ```typescript
153
+ class DyNTS_Msg_Integration_ControlService extends DyNTS_SingletonService {
154
+ static getInstance(): DyNTS_Msg_Integration_ControlService;
155
+
156
+ // Bot integration
157
+ async syncBotMessage<T>(botMessage: DyNTS_Bot_Message<T>, conversationId: string, issuer: string): Promise<DyFM_Msg_Message>;
158
+
159
+ // Assistant integration
160
+ async syncAssistantMessage(content: string, conversationId: string, aiProvider: string, aiModel: string, senderId: string, issuer: string): Promise<DyFM_Msg_Message>;
161
+
162
+ // Find or create bot conversation
163
+ async findOrCreateBotConversation(channelId: string, platformSource: string, issuer: string): Promise<DyFM_Msg_Conversation>;
164
+ }
165
+ ```
166
+
167
+ #### `DyNTS_Msg_Events_Service`
168
+
169
+ Handles real-time socket events:
170
+
171
+ ```typescript
172
+ class DyNTS_Msg_Events_Service extends DyNTS_SingletonService {
173
+ static getInstance(): DyNTS_Msg_Events_Service;
174
+
175
+ // Event emission
176
+ emitMessageSent(message: DyFM_Msg_Message, conversationId: string): void;
177
+ emitMessageUpdated(message: DyFM_Msg_Message, conversationId: string): void;
178
+ emitMessageDeleted(messageId: string, conversationId: string): void;
179
+ emitMessageRead(messageId: string, userId: string, conversationId: string): void;
180
+ emitTypingIndicator(userId: string, conversationId: string, isTyping: boolean): void;
181
+ emitConversationCreated(conversation: DyFM_Msg_Conversation, participantIds: string[]): void;
182
+ emitConversationUpdated(conversation: DyFM_Msg_Conversation): void;
183
+ emitConversationDeleted(conversationId: string): void;
184
+ emitParticipantAdded(conversationId: string, userId: string): void;
185
+ emitParticipantRemoved(conversationId: string, userId: string): void;
186
+ emitReactionAdded(messageId: string, reaction: DyFM_Msg_Reaction): void;
187
+ emitReactionRemoved(messageId: string, userId: string, emoji: string): void;
188
+ }
189
+ ```
190
+
191
+ ### Controllers
192
+
193
+ #### `DyNTS_Msg_Controller`
194
+
195
+ HTTP API controller for messaging endpoints:
196
+
197
+ **Endpoints:**
198
+ - `POST /api/messaging/messages` - Send a message
199
+ - `PUT /api/messaging/messages/:id` - Edit a message
200
+ - `DELETE /api/messaging/messages/:id` - Delete a message
201
+ - `POST /api/messaging/messages/:id/read` - Mark messages as read
202
+ - `POST /api/messaging/messages/:id/reactions` - Add reaction
203
+ - `DELETE /api/messaging/messages/:id/reactions` - Remove reaction
204
+ - `GET /api/messaging/conversations` - Get conversations
205
+ - `POST /api/messaging/conversations` - Create conversation
206
+ - `PUT /api/messaging/conversations/:id` - Update conversation
207
+ - `DELETE /api/messaging/conversations/:id` - Delete conversation
208
+ - `POST /api/messaging/conversations/:id/participants` - Add participant
209
+ - `DELETE /api/messaging/conversations/:id/participants/:userId` - Remove participant
210
+
211
+ ## Configuration
212
+
213
+ ### Global Settings
214
+
215
+ Configure messaging settings in your global configuration:
216
+
217
+ ```typescript
218
+ export const DyNTS_global_settings = {
219
+ messaging_settings: {
220
+ enableRealtimeEvents: true,
221
+ maxMessagesPerLoad: 50,
222
+ enableThreading: true,
223
+ enableReactions: true,
224
+ enableAttachments: true,
225
+ enableMentions: true,
226
+ enableReadReceipts: true,
227
+ enableTypingIndicators: true,
228
+ integration: {
229
+ syncBotMessages: true,
230
+ syncAssistantMessages: true,
231
+ enableAgentProcessViz: true
232
+ },
233
+ retry: {
234
+ maxRetries: 3,
235
+ retryDelay: 1000
236
+ }
237
+ }
238
+ };
239
+ ```
240
+
241
+ ## Routing
242
+
243
+ Add the messaging routing module to your application:
244
+
245
+ ```typescript
246
+ import { DyNTS_getMessagingRoutingModule } from '@futdevpro/dynamo-nts/messaging';
247
+
248
+ const messagingRoutes = DyNTS_getMessagingRoutingModule(
249
+ DyNTS_RouteSecurity.authenticated
250
+ );
251
+
252
+ // Add to your routing configuration
253
+ ```
254
+
255
+ ## Integration
256
+
257
+ ### Bot Integration
258
+
259
+ The messaging system integrates with the bot module to sync messages:
260
+
261
+ ```typescript
262
+ import { DyNTS_Msg_Integration_ControlService } from '@futdevpro/dynamo-nts/messaging';
263
+ import { DyNTS_Bot_Message } from '@futdevpro/dynamo-nts/bot';
264
+
265
+ const integrationService = DyNTS_Msg_Integration_ControlService.getInstance();
266
+
267
+ // Sync bot message to messaging system
268
+ const message = await integrationService.syncBotMessage(
269
+ botMessage,
270
+ conversationId,
271
+ 'bot-service'
272
+ );
273
+ ```
274
+
275
+ ### Assistant Integration
276
+
277
+ Sync AI assistant responses:
278
+
279
+ ```typescript
280
+ // Sync AI assistant message
281
+ const aiMessage = await integrationService.syncAssistantMessage(
282
+ 'AI response text',
283
+ 'conv-123',
284
+ 'openai',
285
+ 'gpt-4',
286
+ 'ai-assistant-id',
287
+ 'ai-service'
288
+ );
289
+ ```
290
+
291
+ ## Usage Examples
292
+
293
+ ### Sending a Message
294
+
295
+ ```typescript
296
+ import { DyNTS_Msg_Main_ControlService } from '@futdevpro/dynamo-nts/messaging';
297
+
298
+ const controlService = DyNTS_Msg_Main_ControlService.getInstance();
299
+
300
+ const message = await controlService.sendMessage(
301
+ 'conv-123',
302
+ {
303
+ content: 'Hello from the backend!',
304
+ type: DyFM_Msg_Type.text
305
+ },
306
+ 'user-456',
307
+ 'backend-service'
308
+ );
309
+ ```
310
+
311
+ ### Creating a Conversation
312
+
313
+ ```typescript
314
+ const conversation = await controlService.createConversation(
315
+ {
316
+ type: DyFM_Msg_ConversationType.direct,
317
+ participants: [
318
+ { userId: 'user-123', role: DyFM_Msg_ParticipantRole.member, joinedAt: new Date() },
319
+ { userId: 'user-456', role: DyFM_Msg_ParticipantRole.member, joinedAt: new Date() }
320
+ ]
321
+ },
322
+ 'user-123',
323
+ 'backend-service'
324
+ );
325
+ ```
326
+
327
+ ### Adding a Reaction
328
+
329
+ ```typescript
330
+ const message = await controlService.addReaction(
331
+ 'msg-123',
332
+ '👍',
333
+ 'user-456',
334
+ 'user-456'
335
+ );
336
+ ```
337
+
338
+ ## Error Handling
339
+
340
+ The module uses comprehensive error handling:
341
+
342
+ ```typescript
343
+ try {
344
+ await controlService.sendMessage(...);
345
+ } catch (error) {
346
+ if (error instanceof DyFM_Error) {
347
+ console.error('Error code:', error.errorCode);
348
+ console.error('User message:', error.userMessage);
349
+ console.error('Technical message:', error.technicalMessage);
350
+ }
351
+ }
352
+ ```
353
+
354
+ ## Related Documentation
355
+
356
+ - [Dynamo FSM Messaging Module](../dynamo-fsm/messaging/README.md)
357
+ - [Dynamo NGX Messaging Module](../dynamo-ngx/messaging/README.md)
358
+ - [Bot Module Integration](./bot/README.md)
359
+ - [Assistant Module Integration](./assistant/README.md)