@wabot-dev/framework 0.4.2 → 0.4.4

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.
@@ -54,12 +54,12 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
54
54
  this.auth.assign(authInfo);
55
55
  }
56
56
  }
57
- this.callBack({
57
+ await this.callBack({
58
58
  chatConnection,
59
59
  message: {
60
60
  text: trimmedInput,
61
61
  },
62
- reply: (message) => {
62
+ reply: async (message) => {
63
63
  console.log(`\n[${message.senderName}]: ${message.text}\n`);
64
64
  this.rl.prompt();
65
65
  if (this.auth.isAssigned()) {
@@ -48,7 +48,7 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
48
48
  constructor(auth) {
49
49
  this.auth = auth;
50
50
  }
51
- onMessage(message, socket) {
51
+ async onMessage(message, socket) {
52
52
  if (!channel.callBack)
53
53
  return;
54
54
  const trimmedInput = message.text.trim();
@@ -60,13 +60,13 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
60
60
  chatType: 'PRIVATE',
61
61
  channelName: SocketChannel_1.name,
62
62
  };
63
- channel.callBack({
63
+ await channel.callBack({
64
64
  chatConnection,
65
65
  message: {
66
66
  text: message.text,
67
67
  senderName: message.senderName,
68
68
  },
69
- reply: (message) => {
69
+ reply: async (message) => {
70
70
  socket.emit('message', message);
71
71
  },
72
72
  injectInstances: [
@@ -80,7 +80,7 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
80
80
  onSocketEvent('message'),
81
81
  __metadata("design:type", Function),
82
82
  __metadata("design:paramtypes", [SocketChannelReceivedMessage, Socket]),
83
- __metadata("design:returntype", void 0)
83
+ __metadata("design:returntype", Promise)
84
84
  ], SocketChannelController.prototype, "onMessage", null);
85
85
  SocketChannelController = __decorate([
86
86
  socketController(channel.config.namespace),
@@ -21,14 +21,16 @@ let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
21
21
  chatType: ctx.message.chat.type === 'private' ? 'PRIVATE' : 'GROUP',
22
22
  channelName: TelegramChannel_1.name,
23
23
  };
24
- callback({
24
+ await callback({
25
25
  chatConnection,
26
26
  message: {
27
27
  senderName: ctx.from.first_name,
28
28
  text: ctx.message.text,
29
29
  },
30
- reply: (replyMessage) => {
31
- replyMessage.text && ctx.reply(replyMessage.text);
30
+ reply: async (replyMessage) => {
31
+ if (!replyMessage.text)
32
+ return;
33
+ await ctx.reply(replyMessage.text);
32
34
  },
33
35
  });
34
36
  });
@@ -20,11 +20,11 @@ let WhatsAppChannel = class WhatsAppChannel {
20
20
  to: this.config.number,
21
21
  listener: async (message) => {
22
22
  try {
23
- callback({
23
+ await callback({
24
24
  chatConnection: message.chatConnection,
25
25
  message: message.message,
26
- reply: (replyMessage) => {
27
- this.sender.sendWhatsApp({
26
+ reply: async (replyMessage) => {
27
+ await this.sender.sendWhatsApp({
28
28
  from: this.config.number,
29
29
  to: message.chatConnection.id,
30
30
  message: replyMessage,
@@ -24,6 +24,15 @@ class Chat extends Entity {
24
24
  }
25
25
  return false;
26
26
  }
27
+ getConnectionByChannel(channelName) {
28
+ return this.data.connections.find((a) => a.channelName === channelName) ?? null;
29
+ }
30
+ addConnection(connection) {
31
+ if (this.hasConnection(connection)) {
32
+ throw new Error('Connection already exists');
33
+ }
34
+ this.data.connections.push(connection);
35
+ }
27
36
  hasAssociation(type, id) {
28
37
  return this.data.associations?.some((a) => a.type === type && a.id === id) ?? false;
29
38
  }
@@ -21,7 +21,7 @@ let ChatBot = class ChatBot {
21
21
  humanMessage: message,
22
22
  });
23
23
  await this.memory.create(newChatItem);
24
- this.processLoop(callback);
24
+ await this.processLoop(callback);
25
25
  }
26
26
  async processLoop(callback) {
27
27
  const prevItems = await this.memory.findLastItems(10);
@@ -57,13 +57,13 @@ let ChatBot = class ChatBot {
57
57
  const newChatItem = new ChatItem(newItemData);
58
58
  await this.memory.create(newChatItem);
59
59
  if (newItemData.type === 'botMessage') {
60
- callback(newChatItem.botMessage);
60
+ await callback(newChatItem.botMessage);
61
61
  }
62
62
  }
63
63
  if (newItemsData.length == 0 || newItemsData[newItemsData.length - 1].type === 'botMessage') {
64
64
  return;
65
65
  }
66
- this.processLoop(callback);
66
+ await this.processLoop(callback);
67
67
  }
68
68
  };
69
69
  ChatBot = __decorate([
@@ -597,6 +597,12 @@ declare class Chat extends Entity<IChatData> {
597
597
  id: string;
598
598
  }[];
599
599
  hasConnection(connection: IChatConnection): boolean;
600
+ getConnectionByChannel(channelName: string): {
601
+ chatType: "GROUP" | "PRIVATE";
602
+ channelName: string;
603
+ id: string;
604
+ } | null;
605
+ addConnection(connection: IChatConnection): void;
600
606
  hasAssociation(type: string, id: string): boolean;
601
607
  getAssociationsByType(type: string): {
602
608
  type: string;
@@ -758,6 +764,8 @@ interface IChatMessage {
758
764
  senderName?: string;
759
765
  text?: string;
760
766
  images?: IChatMessageImage[];
767
+ object?: object;
768
+ metadata?: Record<string, string>;
761
769
  }
762
770
 
763
771
  interface IFunctionCall {
@@ -834,7 +842,7 @@ declare class ChatMemory implements IChatMemory {
834
842
  }
835
843
 
836
844
  interface IChatBot {
837
- sendMessage(message: IChatMessage, callback: (message: IChatMessage) => void): void | Promise<void>;
845
+ sendMessage(message: IChatMessage, callback: (message: IChatMessage) => Promise<void>): void | Promise<void>;
838
846
  }
839
847
 
840
848
  declare class ChatBot implements IChatBot {
@@ -842,8 +850,8 @@ declare class ChatBot implements IChatBot {
842
850
  private adapter;
843
851
  private mindset;
844
852
  constructor(memory: ChatMemory, adapter: ChatAdapter, mindset: MindsetOperator);
845
- sendMessage(message: IChatMessage, callback: (message: IChatMessage) => void): Promise<void>;
846
- protected processLoop(callback: (message: IChatMessage) => void): Promise<void>;
853
+ sendMessage(message: IChatMessage, callback: (message: IChatMessage) => Promise<void>): Promise<void>;
854
+ protected processLoop(callback: (message: IChatMessage) => Promise<void>): Promise<void>;
847
855
  }
848
856
 
849
857
  interface IChatRepository {
@@ -885,7 +893,7 @@ interface IChatControllerMetadata {
885
893
 
886
894
  interface IReceivedMessage {
887
895
  message: IChatMessage;
888
- reply: (message: IChatMessage) => void;
896
+ reply: (message: IChatMessage) => Promise<Record<string, string> | void>;
889
897
  }
890
898
 
891
899
  interface IChannelMessage extends IReceivedMessage {
@@ -894,7 +902,7 @@ interface IChannelMessage extends IReceivedMessage {
894
902
  }
895
903
 
896
904
  interface IChatChannel {
897
- listen(callback: (received: IChannelMessage) => void): void;
905
+ listen(callback: (received: IChannelMessage) => Promise<void>): void;
898
906
  connect(): void;
899
907
  }
900
908
 
@@ -1505,7 +1513,7 @@ declare class CmdChannel implements IChatChannel {
1505
1513
  private rl;
1506
1514
  private callBack;
1507
1515
  constructor(auth: Auth<any>);
1508
- listen(callback: (message: IChannelMessage) => void): void;
1516
+ listen(callback: (message: IChannelMessage) => Promise<void>): void;
1509
1517
  connect(): void;
1510
1518
  }
1511
1519
  declare function writeJsonToFile<T>(filename: string, data: T): void;
@@ -1540,7 +1548,7 @@ declare class SocketChannel implements IChatChannel {
1540
1548
  private controller;
1541
1549
  constructor(config: SocketChannelConfig);
1542
1550
  private configController;
1543
- listen(callback: (message: IChannelMessage) => void): void;
1551
+ listen(callback: (message: IChannelMessage) => Promise<void>): void;
1544
1552
  connect(): void;
1545
1553
  }
1546
1554
 
@@ -1558,7 +1566,7 @@ declare class TelegramChannel implements IChatChannel {
1558
1566
  private config;
1559
1567
  private bot;
1560
1568
  constructor(config: TelegramChannelConfig);
1561
- listen(callback: (message: IChannelMessage) => void): void;
1569
+ listen(callback: (message: IChannelMessage) => Promise<void>): void;
1562
1570
  connect(): void;
1563
1571
  }
1564
1572
 
@@ -1723,7 +1731,7 @@ declare class WhatsAppChannel implements IChatChannel {
1723
1731
  private receiver;
1724
1732
  private logger;
1725
1733
  constructor(config: WhatsappChannelConfig, sender: WhatsAppSender, receiver: WhatsAppReceiver);
1726
- listen(callback: (message: IChannelMessage) => void): void;
1734
+ listen(callback: (message: IChannelMessage) => Promise<void>): void;
1727
1735
  connect(): void;
1728
1736
  }
1729
1737
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",
@@ -47,7 +47,7 @@
47
47
  "@types/jsonwebtoken": "^9.0.10",
48
48
  "@types/node": "22.14.1",
49
49
  "@types/pg": "^8.11.14",
50
- "@yucacodes/ts": "0.0.6",
50
+ "@yucacodes/ts": "0.0.7",
51
51
  "big.js": "^7.0.1",
52
52
  "body-parser": "^2.2.0",
53
53
  "cron-parser": "^5.4.0",