wexa-chat 0.1.6 → 0.1.8
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/dist/{index.d.mts → index.d.cts} +20 -22
- package/dist/index.d.ts +20 -22
- package/dist/index.js +67 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +67 -23
- package/dist/index.mjs.map +1 -1
- package/package.json +35 -40
- package/src/config/env.ts +0 -40
- package/src/index.ts +0 -142
- package/src/models/Conversation.model.ts +0 -92
- package/src/models/Message.model.ts +0 -102
- package/src/models/connection.ts +0 -34
- package/src/models/index.ts +0 -4
- package/src/models/indexes.ts +0 -18
- package/src/services/conversations.service.ts +0 -121
- package/src/services/index.ts +0 -3
- package/src/services/messages.service.ts +0 -167
- package/src/services/search/index.ts +0 -2
- package/src/services/search/search.conversations.ts +0 -122
- package/src/services/search/search.messages.ts +0 -125
- package/src/transport/index.ts +0 -63
- package/src/transport/localSubs.ts +0 -102
- package/src/transport/redis.ts +0 -129
- package/src/transport/socket.ts +0 -143
- package/src/types/actor.ts +0 -5
- package/src/types/dto.ts +0 -53
- package/src/types/events.ts +0 -8
- package/src/types/index.ts +0 -8
- package/src/types/init.ts +0 -57
- package/src/utils/ids.ts +0 -55
- package/src/utils/index.ts +0 -7
- package/src/utils/pagination.ts +0 -75
- package/src/utils/validators.ts +0 -146
|
@@ -166,19 +166,19 @@ type SearchConversationsArgs = {
|
|
|
166
166
|
cursor?: string;
|
|
167
167
|
};
|
|
168
168
|
|
|
169
|
-
interface IParticipant {
|
|
170
|
-
entityModel: string;
|
|
171
|
-
entityId: string;
|
|
172
|
-
role: string;
|
|
173
|
-
}
|
|
174
169
|
interface IConversation extends Document {
|
|
175
170
|
organizationId: string;
|
|
176
|
-
participants:
|
|
177
|
-
|
|
171
|
+
participants: Array<{
|
|
172
|
+
entityModel: string;
|
|
173
|
+
entityId: string;
|
|
174
|
+
role: string;
|
|
175
|
+
}>;
|
|
176
|
+
lastMessageId?: string;
|
|
177
|
+
lastMessageAt?: Date;
|
|
178
178
|
lastMessagePreview?: string;
|
|
179
179
|
lastMessageSenderId?: string;
|
|
180
180
|
lastMessageSenderModel?: string;
|
|
181
|
-
metadata?: Record<string,
|
|
181
|
+
metadata?: Record<string, any>;
|
|
182
182
|
createdAt: Date;
|
|
183
183
|
updatedAt: Date;
|
|
184
184
|
}
|
|
@@ -187,27 +187,24 @@ interface IConversation extends Document {
|
|
|
187
187
|
*/
|
|
188
188
|
declare function createConversationModel(options: InitOptions, conn?: Connection): Model<IConversation>;
|
|
189
189
|
|
|
190
|
-
/**
|
|
191
|
-
* Message document interface
|
|
192
|
-
*/
|
|
193
190
|
interface IMessage extends Document {
|
|
194
|
-
organizationId:
|
|
195
|
-
conversationId:
|
|
191
|
+
organizationId: string;
|
|
192
|
+
conversationId: string;
|
|
196
193
|
senderModel: string;
|
|
197
|
-
senderId:
|
|
198
|
-
kind: 'text' | 'system';
|
|
194
|
+
senderId: string;
|
|
199
195
|
text: string;
|
|
200
|
-
|
|
201
|
-
|
|
196
|
+
kind: 'text' | 'system';
|
|
197
|
+
parentMessageId?: string;
|
|
198
|
+
rootThreadId?: string;
|
|
202
199
|
editedAt?: Date;
|
|
203
200
|
deletedAt?: Date;
|
|
204
201
|
createdAt: Date;
|
|
205
202
|
updatedAt: Date;
|
|
206
203
|
}
|
|
207
204
|
/**
|
|
208
|
-
* Create Message model
|
|
205
|
+
* Create (or retrieve) Message model safely
|
|
209
206
|
*/
|
|
210
|
-
declare function createMessageModel(options: InitOptions): Model<IMessage>;
|
|
207
|
+
declare function createMessageModel(options: InitOptions, conn?: Connection): Model<IMessage>;
|
|
211
208
|
|
|
212
209
|
/**
|
|
213
210
|
* Pagination result interface
|
|
@@ -450,6 +447,7 @@ declare function validatePagination(limit?: number, cursor?: string): {
|
|
|
450
447
|
cursor: string | undefined;
|
|
451
448
|
};
|
|
452
449
|
|
|
450
|
+
declare const modelRegistry: Map<string, mongoose.Model<any, {}, {}, {}, any, any>>;
|
|
453
451
|
/**
|
|
454
452
|
* Create Mongoose models with applied configuration
|
|
455
453
|
* @param mongooseInstance Mongoose instance
|
|
@@ -457,10 +455,10 @@ declare function validatePagination(limit?: number, cursor?: string): {
|
|
|
457
455
|
* @returns Object containing Conversation and Message models
|
|
458
456
|
*/
|
|
459
457
|
declare function createModels(mongooseInstance: typeof mongoose, options: InitOptions): {
|
|
460
|
-
Conversation: mongoose.Model<IConversation, {}, {}, {}, mongoose.Document<unknown, {}, IConversation> & IConversation & {
|
|
458
|
+
Conversation: mongoose.Model<any, {}, {}, {}, any, any> | mongoose.Model<IConversation, {}, {}, {}, mongoose.Document<unknown, {}, IConversation> & IConversation & {
|
|
461
459
|
_id: mongoose.Types.ObjectId;
|
|
462
460
|
}, any>;
|
|
463
|
-
Message: mongoose.Model<IMessage, {}, {}, {}, mongoose.Document<unknown, {}, IMessage> & IMessage & {
|
|
461
|
+
Message: mongoose.Model<any, {}, {}, {}, any, any> | mongoose.Model<IMessage, {}, {}, {}, mongoose.Document<unknown, {}, IMessage> & IMessage & {
|
|
464
462
|
_id: mongoose.Types.ObjectId;
|
|
465
463
|
}, any>;
|
|
466
464
|
};
|
|
@@ -526,4 +524,4 @@ interface ChatServices {
|
|
|
526
524
|
*/
|
|
527
525
|
declare function initChat(mongooseInstance: typeof mongoose, options: InitOptions, server?: Server): Promise<ChatServices>;
|
|
528
526
|
|
|
529
|
-
export { type Actor, type ChatServices, type ConversationReadEvent, type ConversationsService, type CreateConversationArgs, type EnvironmentConfig, type IConversation, type IMessage, type
|
|
527
|
+
export { type Actor, type ChatServices, type ConversationReadEvent, type ConversationsService, type CreateConversationArgs, type EnvironmentConfig, type IConversation, type IMessage, type InitOptions, type ListMessagesArgs, type MarkReadArgs, type MessageCreatedEvent, type MessageServiceHooks, type MessagesService, type PaginatedResult, type PresenceEvent, type RedisConfig, type SearchConversationsArgs, type SearchMessagesArgs, type SendMessageArgs, type SocketConfig$1 as SocketConfig, type Transport, type TransportConfig, type TypingEvent, createConversationId, createConversationModel, createConversationsService, createCursor, createMessageId, createMessageModel, createMessagesService, createModels, createPaginatedResponse, createThreadId, createTransport, ensureIndexes, generateId, getRedisConfig, initChat, modelRegistry, parseCursor, searchByParticipantPair, searchConversations, searchMessages, validateCreateConversationArgs, validateInitOptions, validateMarkReadArgs, validatePagination, validateSendMessageArgs };
|
package/dist/index.d.ts
CHANGED
|
@@ -166,19 +166,19 @@ type SearchConversationsArgs = {
|
|
|
166
166
|
cursor?: string;
|
|
167
167
|
};
|
|
168
168
|
|
|
169
|
-
interface IParticipant {
|
|
170
|
-
entityModel: string;
|
|
171
|
-
entityId: string;
|
|
172
|
-
role: string;
|
|
173
|
-
}
|
|
174
169
|
interface IConversation extends Document {
|
|
175
170
|
organizationId: string;
|
|
176
|
-
participants:
|
|
177
|
-
|
|
171
|
+
participants: Array<{
|
|
172
|
+
entityModel: string;
|
|
173
|
+
entityId: string;
|
|
174
|
+
role: string;
|
|
175
|
+
}>;
|
|
176
|
+
lastMessageId?: string;
|
|
177
|
+
lastMessageAt?: Date;
|
|
178
178
|
lastMessagePreview?: string;
|
|
179
179
|
lastMessageSenderId?: string;
|
|
180
180
|
lastMessageSenderModel?: string;
|
|
181
|
-
metadata?: Record<string,
|
|
181
|
+
metadata?: Record<string, any>;
|
|
182
182
|
createdAt: Date;
|
|
183
183
|
updatedAt: Date;
|
|
184
184
|
}
|
|
@@ -187,27 +187,24 @@ interface IConversation extends Document {
|
|
|
187
187
|
*/
|
|
188
188
|
declare function createConversationModel(options: InitOptions, conn?: Connection): Model<IConversation>;
|
|
189
189
|
|
|
190
|
-
/**
|
|
191
|
-
* Message document interface
|
|
192
|
-
*/
|
|
193
190
|
interface IMessage extends Document {
|
|
194
|
-
organizationId:
|
|
195
|
-
conversationId:
|
|
191
|
+
organizationId: string;
|
|
192
|
+
conversationId: string;
|
|
196
193
|
senderModel: string;
|
|
197
|
-
senderId:
|
|
198
|
-
kind: 'text' | 'system';
|
|
194
|
+
senderId: string;
|
|
199
195
|
text: string;
|
|
200
|
-
|
|
201
|
-
|
|
196
|
+
kind: 'text' | 'system';
|
|
197
|
+
parentMessageId?: string;
|
|
198
|
+
rootThreadId?: string;
|
|
202
199
|
editedAt?: Date;
|
|
203
200
|
deletedAt?: Date;
|
|
204
201
|
createdAt: Date;
|
|
205
202
|
updatedAt: Date;
|
|
206
203
|
}
|
|
207
204
|
/**
|
|
208
|
-
* Create Message model
|
|
205
|
+
* Create (or retrieve) Message model safely
|
|
209
206
|
*/
|
|
210
|
-
declare function createMessageModel(options: InitOptions): Model<IMessage>;
|
|
207
|
+
declare function createMessageModel(options: InitOptions, conn?: Connection): Model<IMessage>;
|
|
211
208
|
|
|
212
209
|
/**
|
|
213
210
|
* Pagination result interface
|
|
@@ -450,6 +447,7 @@ declare function validatePagination(limit?: number, cursor?: string): {
|
|
|
450
447
|
cursor: string | undefined;
|
|
451
448
|
};
|
|
452
449
|
|
|
450
|
+
declare const modelRegistry: Map<string, mongoose.Model<any, {}, {}, {}, any, any>>;
|
|
453
451
|
/**
|
|
454
452
|
* Create Mongoose models with applied configuration
|
|
455
453
|
* @param mongooseInstance Mongoose instance
|
|
@@ -457,10 +455,10 @@ declare function validatePagination(limit?: number, cursor?: string): {
|
|
|
457
455
|
* @returns Object containing Conversation and Message models
|
|
458
456
|
*/
|
|
459
457
|
declare function createModels(mongooseInstance: typeof mongoose, options: InitOptions): {
|
|
460
|
-
Conversation: mongoose.Model<IConversation, {}, {}, {}, mongoose.Document<unknown, {}, IConversation> & IConversation & {
|
|
458
|
+
Conversation: mongoose.Model<any, {}, {}, {}, any, any> | mongoose.Model<IConversation, {}, {}, {}, mongoose.Document<unknown, {}, IConversation> & IConversation & {
|
|
461
459
|
_id: mongoose.Types.ObjectId;
|
|
462
460
|
}, any>;
|
|
463
|
-
Message: mongoose.Model<IMessage, {}, {}, {}, mongoose.Document<unknown, {}, IMessage> & IMessage & {
|
|
461
|
+
Message: mongoose.Model<any, {}, {}, {}, any, any> | mongoose.Model<IMessage, {}, {}, {}, mongoose.Document<unknown, {}, IMessage> & IMessage & {
|
|
464
462
|
_id: mongoose.Types.ObjectId;
|
|
465
463
|
}, any>;
|
|
466
464
|
};
|
|
@@ -526,4 +524,4 @@ interface ChatServices {
|
|
|
526
524
|
*/
|
|
527
525
|
declare function initChat(mongooseInstance: typeof mongoose, options: InitOptions, server?: Server): Promise<ChatServices>;
|
|
528
526
|
|
|
529
|
-
export { type Actor, type ChatServices, type ConversationReadEvent, type ConversationsService, type CreateConversationArgs, type EnvironmentConfig, type IConversation, type IMessage, type
|
|
527
|
+
export { type Actor, type ChatServices, type ConversationReadEvent, type ConversationsService, type CreateConversationArgs, type EnvironmentConfig, type IConversation, type IMessage, type InitOptions, type ListMessagesArgs, type MarkReadArgs, type MessageCreatedEvent, type MessageServiceHooks, type MessagesService, type PaginatedResult, type PresenceEvent, type RedisConfig, type SearchConversationsArgs, type SearchMessagesArgs, type SendMessageArgs, type SocketConfig$1 as SocketConfig, type Transport, type TransportConfig, type TypingEvent, createConversationId, createConversationModel, createConversationsService, createCursor, createMessageId, createMessageModel, createMessagesService, createModels, createPaginatedResponse, createThreadId, createTransport, ensureIndexes, generateId, getRedisConfig, initChat, modelRegistry, parseCursor, searchByParticipantPair, searchConversations, searchMessages, validateCreateConversationArgs, validateInitOptions, validateMarkReadArgs, validatePagination, validateSendMessageArgs };
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,17 @@ var Redis__default = /*#__PURE__*/_interopDefault(Redis);
|
|
|
11
11
|
|
|
12
12
|
// src/models/Conversation.model.ts
|
|
13
13
|
function createConversationModel(options, conn = mongoose__default.default.connection) {
|
|
14
|
+
const connectionId = conn.id || conn.name || "default";
|
|
15
|
+
const registryKey = `${connectionId}:Conversation`;
|
|
16
|
+
if (modelRegistry.has(registryKey)) {
|
|
17
|
+
return modelRegistry.get(registryKey);
|
|
18
|
+
}
|
|
19
|
+
const name = "Conversation";
|
|
20
|
+
if (conn.models[name]) {
|
|
21
|
+
const existingModel = conn.models[name];
|
|
22
|
+
modelRegistry.set(registryKey, existingModel);
|
|
23
|
+
return existingModel;
|
|
24
|
+
}
|
|
14
25
|
const ParticipantSchema = new mongoose.Schema(
|
|
15
26
|
{
|
|
16
27
|
entityModel: {
|
|
@@ -41,10 +52,16 @@ function createConversationModel(options, conn = mongoose__default.default.conne
|
|
|
41
52
|
type: [ParticipantSchema],
|
|
42
53
|
required: true,
|
|
43
54
|
validate: {
|
|
44
|
-
validator: (participants)
|
|
45
|
-
|
|
55
|
+
validator: function(participants) {
|
|
56
|
+
return participants.length === 2;
|
|
57
|
+
},
|
|
58
|
+
message: "Conversations must have exactly 2 participants"
|
|
46
59
|
}
|
|
47
60
|
},
|
|
61
|
+
lastMessageId: {
|
|
62
|
+
type: String,
|
|
63
|
+
ref: "Message"
|
|
64
|
+
},
|
|
48
65
|
lastMessageAt: {
|
|
49
66
|
type: Date,
|
|
50
67
|
index: true
|
|
@@ -64,21 +81,31 @@ function createConversationModel(options, conn = mongoose__default.default.conne
|
|
|
64
81
|
organizationId: 1,
|
|
65
82
|
lastMessageAt: -1
|
|
66
83
|
});
|
|
67
|
-
const
|
|
68
|
-
|
|
84
|
+
const model = conn.model(name, ConversationSchema);
|
|
85
|
+
modelRegistry.set(registryKey, model);
|
|
86
|
+
return model;
|
|
69
87
|
}
|
|
70
|
-
function createMessageModel(options) {
|
|
88
|
+
function createMessageModel(options, conn = mongoose__default.default.connection) {
|
|
89
|
+
const connectionId = conn.id || conn.name || "default";
|
|
90
|
+
const registryKey = `${connectionId}:Message`;
|
|
91
|
+
if (modelRegistry.has(registryKey)) {
|
|
92
|
+
return modelRegistry.get(registryKey);
|
|
93
|
+
}
|
|
94
|
+
const name = "Message";
|
|
95
|
+
if (conn.models[name]) {
|
|
96
|
+
const existingModel = conn.models[name];
|
|
97
|
+
modelRegistry.set(registryKey, existingModel);
|
|
98
|
+
return existingModel;
|
|
99
|
+
}
|
|
71
100
|
const MessageSchema = new mongoose.Schema(
|
|
72
101
|
{
|
|
73
102
|
organizationId: {
|
|
74
|
-
type:
|
|
75
|
-
ref: "Organization",
|
|
103
|
+
type: String,
|
|
76
104
|
required: true,
|
|
77
105
|
index: true
|
|
78
106
|
},
|
|
79
107
|
conversationId: {
|
|
80
|
-
type:
|
|
81
|
-
ref: "Conversation",
|
|
108
|
+
type: String,
|
|
82
109
|
required: true,
|
|
83
110
|
index: true
|
|
84
111
|
},
|
|
@@ -88,8 +115,7 @@ function createMessageModel(options) {
|
|
|
88
115
|
required: true
|
|
89
116
|
},
|
|
90
117
|
senderId: {
|
|
91
|
-
type:
|
|
92
|
-
refPath: "senderModel",
|
|
118
|
+
type: String,
|
|
93
119
|
required: true,
|
|
94
120
|
index: true
|
|
95
121
|
},
|
|
@@ -104,11 +130,11 @@ function createMessageModel(options) {
|
|
|
104
130
|
trim: true
|
|
105
131
|
},
|
|
106
132
|
parentMessageId: {
|
|
107
|
-
type:
|
|
133
|
+
type: String,
|
|
108
134
|
ref: "Message"
|
|
109
135
|
},
|
|
110
136
|
rootThreadId: {
|
|
111
|
-
type:
|
|
137
|
+
type: String,
|
|
112
138
|
ref: "Message"
|
|
113
139
|
},
|
|
114
140
|
editedAt: {
|
|
@@ -134,13 +160,26 @@ function createMessageModel(options) {
|
|
|
134
160
|
}
|
|
135
161
|
);
|
|
136
162
|
}
|
|
137
|
-
|
|
163
|
+
const model = conn.model(name, MessageSchema);
|
|
164
|
+
modelRegistry.set(registryKey, model);
|
|
165
|
+
return model;
|
|
138
166
|
}
|
|
139
167
|
|
|
140
168
|
// src/models/connection.ts
|
|
169
|
+
var modelRegistry = /* @__PURE__ */ new Map();
|
|
141
170
|
function createModels(mongooseInstance, options) {
|
|
142
|
-
const
|
|
143
|
-
const
|
|
171
|
+
const conn = mongooseInstance.connection;
|
|
172
|
+
const connectionId = conn.id || conn.name || "default";
|
|
173
|
+
const registryKey = `${connectionId}:Conversation`;
|
|
174
|
+
const messageRegistryKey = `${connectionId}:Message`;
|
|
175
|
+
const Conversation = modelRegistry.get(registryKey) || createConversationModel(options, conn);
|
|
176
|
+
const Message = modelRegistry.get(messageRegistryKey) || createMessageModel(options, conn);
|
|
177
|
+
if (!modelRegistry.has(registryKey)) {
|
|
178
|
+
modelRegistry.set(registryKey, Conversation);
|
|
179
|
+
}
|
|
180
|
+
if (!modelRegistry.has(messageRegistryKey)) {
|
|
181
|
+
modelRegistry.set(messageRegistryKey, Message);
|
|
182
|
+
}
|
|
144
183
|
return {
|
|
145
184
|
Conversation,
|
|
146
185
|
Message
|
|
@@ -636,8 +675,7 @@ function createRedisClient(config) {
|
|
|
636
675
|
}
|
|
637
676
|
}
|
|
638
677
|
async function publishToConversation(client, conversationId, payload) {
|
|
639
|
-
if (!client)
|
|
640
|
-
return 0;
|
|
678
|
+
if (!client) return 0;
|
|
641
679
|
try {
|
|
642
680
|
const channel = `conv:${conversationId}`;
|
|
643
681
|
const message = JSON.stringify(payload);
|
|
@@ -648,8 +686,7 @@ async function publishToConversation(client, conversationId, payload) {
|
|
|
648
686
|
}
|
|
649
687
|
}
|
|
650
688
|
function startRedisListener(client, onEvent) {
|
|
651
|
-
if (!client)
|
|
652
|
-
return null;
|
|
689
|
+
if (!client) return null;
|
|
653
690
|
try {
|
|
654
691
|
const subClient = client.duplicate();
|
|
655
692
|
subClient.psubscribe("conv:*");
|
|
@@ -919,7 +956,12 @@ function validatePagination(limit, cursor) {
|
|
|
919
956
|
}
|
|
920
957
|
|
|
921
958
|
// src/index.ts
|
|
959
|
+
var initializedInstances = /* @__PURE__ */ new WeakMap();
|
|
922
960
|
async function initChat(mongooseInstance, options, server) {
|
|
961
|
+
const conn = mongooseInstance.connection;
|
|
962
|
+
if (initializedInstances.has(conn)) {
|
|
963
|
+
return initializedInstances.get(conn);
|
|
964
|
+
}
|
|
923
965
|
const models = createModels(mongooseInstance, options);
|
|
924
966
|
await ensureIndexes(models);
|
|
925
967
|
const transport = createTransport({
|
|
@@ -987,7 +1029,9 @@ async function initChat(mongooseInstance, options, server) {
|
|
|
987
1029
|
}
|
|
988
1030
|
})
|
|
989
1031
|
};
|
|
990
|
-
|
|
1032
|
+
const chatServices = { models, services, transport };
|
|
1033
|
+
initializedInstances.set(conn, chatServices);
|
|
1034
|
+
return chatServices;
|
|
991
1035
|
}
|
|
992
1036
|
|
|
993
1037
|
exports.createConversationId = createConversationId;
|
|
@@ -1005,6 +1049,7 @@ exports.ensureIndexes = ensureIndexes;
|
|
|
1005
1049
|
exports.generateId = generateId;
|
|
1006
1050
|
exports.getRedisConfig = getRedisConfig;
|
|
1007
1051
|
exports.initChat = initChat;
|
|
1052
|
+
exports.modelRegistry = modelRegistry;
|
|
1008
1053
|
exports.parseCursor = parseCursor;
|
|
1009
1054
|
exports.searchByParticipantPair = searchByParticipantPair;
|
|
1010
1055
|
exports.searchConversations = searchConversations;
|
|
@@ -1014,5 +1059,5 @@ exports.validateInitOptions = validateInitOptions;
|
|
|
1014
1059
|
exports.validateMarkReadArgs = validateMarkReadArgs;
|
|
1015
1060
|
exports.validatePagination = validatePagination;
|
|
1016
1061
|
exports.validateSendMessageArgs = validateSendMessageArgs;
|
|
1017
|
-
//# sourceMappingURL=
|
|
1062
|
+
//# sourceMappingURL=index.js.map
|
|
1018
1063
|
//# sourceMappingURL=index.js.map
|