wexa-chat 0.2.1 → 0.2.3

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/README.md CHANGED
@@ -121,7 +121,7 @@ export type InitOptions = {
121
121
  - `searchByParticipantPair({ organizationId, a, b })`
122
122
 
123
123
  - Messages
124
- - `sendMessage({ organizationId, conversationId, senderModel, senderId, text, source, kind?, parentMessageId?, rootThreadId? })`
124
+ - `sendMessage({ organizationId, conversationId, senderModel, senderId, text, source, kind?, parentMessageId?, rootThreadId?, connectorIds? })`
125
125
  - `listMessages({ organizationId, conversationId, limit?, cursor? })`
126
126
  - `listMessagesWithSenders({ organizationId, conversationId, limit?, cursor?, populateSenders: true, populateOptions? })`
127
127
  - `markRead({ organizationId, conversationId, participantModel, participantId, messageId })`
@@ -209,9 +209,9 @@ The method returns messages sorted by creation time (newest first), making it id
209
209
 
210
210
  The type `MessageWithSender` is exported from the package for proper TypeScript support.
211
211
 
212
- ### Message Source
212
+ ### Sending messages and connectors
213
213
 
214
- Messages now include a required `source` field (array) to indicate one or more origins for a message. The model enforces `source` values via an enum of strings:
214
+ Messages include a required `source` field (array) to indicate one or more origins for a message. Valid values are enforced by an enum of strings:
215
215
 
216
216
  ```ts
217
217
  export const sourceType = {
@@ -224,8 +224,35 @@ export const sourceType = {
224
224
  export type SourceType = (typeof sourceType)[keyof typeof sourceType];
225
225
  ```
226
226
 
227
- When sending a message via `sendMessage`, you must provide `source: SourceType[]`.
228
- For internal server-generated messages, use `[sourceType.CORE]`.
227
+ When sending a message via `sendMessage`, you must provide `source: SourceType[]`. For internal server‑generated messages, use `[sourceType.CORE]`.
228
+
229
+ If you include external connectors in the `source` (e.g., `linkedin`, `whatsapp`), you can optionally pass `connectorIds` to perform the external send and to persist identifiers for quick follow‑ups:
230
+
231
+ ```ts
232
+ await chat.services.messages.sendMessage({
233
+ organizationId: 'org-123',
234
+ conversationId: 'conv-456',
235
+ senderModel: 'User',
236
+ senderId: 'user-1',
237
+ text: 'Hello from Wexa',
238
+ source: [sourceType.CORE, sourceType.LINKEDIN, sourceType.WHATSAPP],
239
+ connectorIds: {
240
+ linkedin: { connectorId: 'ln-connector-1', contactId: 'https://linkedin.com/in/someone' },
241
+ whatsapp: { connectorId: 'wa-connector-1', contactId: '+1 234 567 8901' },
242
+ },
243
+ });
244
+ ```
245
+
246
+ Under the hood (`src/services/messages.service.ts`):
247
+
248
+ - LinkedIn: posts to the LinkedIn connector with `{ linkedin_url, text }`.
249
+ - WhatsApp: posts to the WhatsApp connector with `{ phone_numbers, text }` (phone is sanitized to remove spaces and a leading `+`).
250
+ - Each external task runs concurrently. A successful task's `source` remains in the stored message. Failures are collected into a `failed_source` array attached to the returned message instance at runtime: `message.failed_source: Array<{ source: string; message: string }>`.
251
+ - The conversation document is updated with last message metadata and, when applicable, with connector identifiers:
252
+ - `lastLinkedInId: string` — set from `connectorIds.linkedin.contactId` when `source` includes `linkedin`.
253
+ - `lastWhatsAppId: string` — set from a sanitized `connectorIds.whatsapp.contactId` when `source` includes `whatsapp`.
254
+
255
+ This allows subsequent UI actions to prefill the last known contact handles for LinkedIn and WhatsApp.
229
256
 
230
257
  ## Models
231
258
 
package/dist/index.d.cts CHANGED
@@ -259,6 +259,10 @@ interface IConversation extends Document {
259
259
  lastMessagePreview?: string;
260
260
  lastMessageSenderId?: string;
261
261
  lastMessageSenderModel?: string;
262
+ lastLinkedInId?: string;
263
+ lastWhatsAppId?: string;
264
+ linkedinChatId?: string;
265
+ whatsappChatId?: string;
262
266
  metadata?: Record<string, any>;
263
267
  createdAt: Date;
264
268
  updatedAt: Date;
package/dist/index.d.ts CHANGED
@@ -259,6 +259,10 @@ interface IConversation extends Document {
259
259
  lastMessagePreview?: string;
260
260
  lastMessageSenderId?: string;
261
261
  lastMessageSenderModel?: string;
262
+ lastLinkedInId?: string;
263
+ lastWhatsAppId?: string;
264
+ linkedinChatId?: string;
265
+ whatsappChatId?: string;
262
266
  metadata?: Record<string, any>;
263
267
  createdAt: Date;
264
268
  updatedAt: Date;
package/dist/index.js CHANGED
@@ -70,6 +70,10 @@ function createConversationModel(options, conn) {
70
70
  lastMessagePreview: String,
71
71
  lastMessageSenderId: String,
72
72
  lastMessageSenderModel: String,
73
+ lastLinkedInId: String,
74
+ lastWhatsAppId: String,
75
+ linkedinChatId: String,
76
+ whatsappChatId: String,
73
77
  metadata: Schema.Types.Mixed
74
78
  },
75
79
  { timestamps: true }
@@ -549,8 +553,11 @@ function createMessagesService(models, hooks = {}) {
549
553
  const success_source = ["core"];
550
554
  const failed_source = [];
551
555
  const tasks = [];
556
+ let lastLinkedInId;
557
+ let lastWhatsAppId;
552
558
  if (Array.isArray(source) && source.includes("linkedin")) {
553
559
  const LinkedinConnector = connectorIds && connectorIds.linkedin;
560
+ lastLinkedInId = LinkedinConnector == null ? void 0 : LinkedinConnector.connectorId;
554
561
  if (LinkedinConnector) {
555
562
  tasks.push({
556
563
  name: "linkedin",
@@ -568,6 +575,7 @@ function createMessagesService(models, hooks = {}) {
568
575
  }
569
576
  if (Array.isArray(source) && source.includes("whatsapp")) {
570
577
  const whatsappConnector = connectorIds && connectorIds.whatsapp;
578
+ lastWhatsAppId = whatsappConnector == null ? void 0 : whatsappConnector.connectorId;
571
579
  if (whatsappConnector) {
572
580
  tasks.push({
573
581
  name: "whatsapp",
@@ -593,14 +601,17 @@ function createMessagesService(models, hooks = {}) {
593
601
  if (res.status === "fulfilled") {
594
602
  success_source.push(name);
595
603
  } else {
596
- const err = res.reason.response.data.detail || res.reason.response.data || "Unknown error";
604
+ const err = success_source;
605
+ res.reason.response.data.detail || res.reason.response.data || "Unknown error";
597
606
  failed_source.push({
598
607
  source: name,
599
608
  message: typeof err === "string" ? err : JSON.stringify(err)
600
609
  });
601
610
  }
602
611
  });
612
+ console.log("results", results);
603
613
  }
614
+ console.log("success_source", success_source);
604
615
  const message = new Message({
605
616
  organizationId,
606
617
  conversationId,
@@ -613,13 +624,16 @@ function createMessagesService(models, hooks = {}) {
613
624
  rootThreadId: rootThreadId || parentMessageId
614
625
  });
615
626
  await message.save();
627
+ const conversationUpdate = {
628
+ lastMessageAt: message.createdAt,
629
+ lastMessagePreview: text.substring(0, 100),
630
+ lastMessageSenderId: senderId,
631
+ lastMessageSenderModel: senderModel
632
+ };
633
+ if (lastLinkedInId) conversationUpdate.lastLinkedInId = lastLinkedInId;
634
+ if (lastWhatsAppId) conversationUpdate.lastWhatsAppId = lastWhatsAppId;
616
635
  await Conversation.findByIdAndUpdate(conversationId, {
617
- $set: {
618
- lastMessageAt: message.createdAt,
619
- lastMessagePreview: text.substring(0, 100),
620
- lastMessageSenderId: senderId,
621
- lastMessageSenderModel: senderModel
622
- }
636
+ $set: conversationUpdate
623
637
  });
624
638
  if (onMessageCreated) {
625
639
  onMessageCreated(message);