@wabot-dev/framework 0.0.6 → 0.0.7

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.
Files changed (36) hide show
  1. package/dist/src/ai/openia/OpenaiChatBotAdapter.js +10 -8
  2. package/dist/src/channels/cmd/@cmd.js +1 -0
  3. package/dist/src/channels/cmd/CmdChannel.js +32 -12
  4. package/dist/src/channels/telegram/@telegram.js +1 -0
  5. package/dist/src/channels/telegram/TelegramChannel.js +27 -14
  6. package/dist/src/chatbot/ChatBot.js +7 -5
  7. package/dist/src/chatbot/ChatBotAdapter.js +12 -4
  8. package/dist/src/chatbot/metadata/@chatBot.js +3 -0
  9. package/dist/src/chatbot/metadata/ChatBotMetadataStore.js +3 -0
  10. package/dist/src/controller/channel/ChatResolver.js +9 -10
  11. package/dist/src/controller/channel/UserResolver.js +23 -0
  12. package/dist/src/core/IMessageContext.js +15 -0
  13. package/dist/src/{chatbot → core}/chat/Chat.js +1 -1
  14. package/dist/src/{chatbot → core}/chat/repository/ram/RamChatRepository.js +1 -0
  15. package/dist/src/core/user/User.js +42 -0
  16. package/dist/src/core/user/repository/IUserRepository.js +19 -0
  17. package/dist/src/core/user/repository/ram/RamUserRepository.js +28 -0
  18. package/dist/src/index.d.ts +249 -146
  19. package/dist/src/index.js +25 -13
  20. package/dist/src/mindset/IMindset.js +3 -3
  21. package/dist/src/mindset/metadata/params/@isOptional.js +1 -1
  22. package/dist/src/pre-made/modules/authentication/AuthenticationModule.js +99 -0
  23. package/dist/src/pre-made/modules/authentication/requests/SendOneTimePasswordRequest.js +25 -0
  24. package/dist/src/pre-made/modules/authentication/requests/ValidateOneTimePasswordRequest.js +25 -0
  25. package/dist/src/pre-made/modules/register-user/RegisterUserModule.js +58 -0
  26. package/dist/src/pre-made/modules/register-user/requests/RegisterUserWithEmailRequest.js +25 -0
  27. package/dist/src/pre-made/services/EmailService.js +13 -0
  28. package/dist/src/pre-made/services/OtpService.js +14 -0
  29. package/dist/src/server/prepareChatContainer.js +14 -10
  30. package/dist/src/server/runChannel.js +12 -4
  31. package/dist/src/server/runServer.js +15 -11
  32. package/package.json +8 -3
  33. package/dist/src/context/IContext.js +0 -10
  34. /package/dist/src/{chatbot → core}/chat/repository/IChatMemory.js +0 -0
  35. /package/dist/src/{chatbot → core}/chat/repository/IChatRepository.js +0 -0
  36. /package/dist/src/{chatbot → core}/chat/repository/ram/RamChatMemory.js +0 -0
@@ -1,15 +1,17 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
2
  import { injectable } from '../../injection/index.js';
3
3
  import { OpenAI } from 'openai';
4
- import '../../chatbot/chat/repository/ram/RamChatRepository.js';
5
- import '../../chatbot/chat/repository/IChatRepository.js';
4
+ import 'reflect-metadata';
5
+ import '../../mindset/metadata/MindsetMetadataStore.js';
6
+ import { MindsetOperator } from '../../mindset/MindsetOperator.js';
6
7
  import 'uuid';
7
8
  import '../../chatbot/metadata/ChatBotMetadataStore.js';
8
9
  import '../../chatbot/ChatBot.js';
9
10
  import { ChatBotAdapter } from '../../chatbot/ChatBotAdapter.js';
10
- import 'reflect-metadata';
11
- import '../../mindset/metadata/MindsetMetadataStore.js';
12
- import { MindsetOperator } from '../../mindset/MindsetOperator.js';
11
+ import '../../core/chat/repository/ram/RamChatRepository.js';
12
+ import '../../core/chat/repository/IChatRepository.js';
13
+ import '../../core/user/repository/ram/RamUserRepository.js';
14
+ import '../../core/user/repository/IUserRepository.js';
13
15
 
14
16
  let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
15
17
  openai = new OpenAI();
@@ -48,7 +50,7 @@ let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
48
50
  mapChatItems(chatItems) {
49
51
  const openIaInput = [];
50
52
  for (const item of chatItems) {
51
- if (item.type === 'USER_MESSAGE') {
53
+ if (item.type === 'CONNECTION_MESSAGE') {
52
54
  if (!item.content.text) {
53
55
  throw new Error('System message content is empty');
54
56
  }
@@ -63,13 +65,13 @@ let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
63
65
  if (item.type === 'FUNCTION_CALL') {
64
66
  openIaInput.push({
65
67
  type: 'function_call',
66
- call_id: item.content.foreignId,
68
+ call_id: item.content.id,
67
69
  name: item.content.name,
68
70
  arguments: JSON.stringify(item.content.arguments),
69
71
  });
70
72
  openIaInput.push({
71
73
  type: 'function_call_output',
72
- call_id: item.content.foreignId,
74
+ call_id: item.content.id,
73
75
  output: item.content.result,
74
76
  });
75
77
  }
@@ -1,4 +1,5 @@
1
1
  import '../../controller/channel/ChatResolver.js';
2
+ import '../../controller/channel/UserResolver.js';
2
3
  import { container } from '../../injection/index.js';
3
4
  import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
4
5
  import { CmdChannel } from './CmdChannel.js';
@@ -1,19 +1,29 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
2
  import { ChatResolver } from '../../controller/channel/ChatResolver.js';
3
+ import { UserResolver } from '../../controller/channel/UserResolver.js';
3
4
  import { injectable } from '../../injection/index.js';
4
5
  import '../../controller/metadata/ControllerMetadataStore.js';
6
+ import { v4 } from 'uuid';
7
+ import '../../core/chat/repository/ram/RamChatRepository.js';
8
+ import '../../core/chat/repository/IChatRepository.js';
9
+ import '../../core/user/repository/ram/RamUserRepository.js';
10
+ import '../../core/user/repository/IUserRepository.js';
5
11
  import * as readline from 'readline';
6
12
 
7
13
  var CmdChannel_1;
14
+ const chatId = v4();
15
+ const userId = v4();
8
16
  let CmdChannel = CmdChannel_1 = class CmdChannel {
9
17
  chatResolver;
18
+ userResolver;
10
19
  rl = readline.createInterface({
11
20
  input: process.stdin,
12
21
  output: process.stdout,
13
22
  });
14
23
  callBack = null;
15
- constructor(chatResolver) {
24
+ constructor(chatResolver, userResolver) {
16
25
  this.chatResolver = chatResolver;
26
+ this.userResolver = userResolver;
17
27
  }
18
28
  listen(callback) {
19
29
  this.callBack = callback;
@@ -21,29 +31,38 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
21
31
  connect() {
22
32
  this.rl.on('line', async (input) => {
23
33
  const trimmedInput = input.trim();
34
+ if (!trimmedInput) {
35
+ this.rl.prompt();
36
+ return;
37
+ }
24
38
  if (trimmedInput.toLowerCase() === 'exit') {
25
39
  this.rl.close();
26
40
  return;
27
41
  }
28
- const origin = {
29
- chatId: 'cmd',
42
+ const chatConnection = {
43
+ id: chatId,
30
44
  chatType: 'PRIVATE',
31
- channelType: CmdChannel_1,
45
+ channelName: CmdChannel_1.name,
46
+ };
47
+ const chat = await this.chatResolver.resolve(chatConnection);
48
+ const userConnection = {
49
+ id: userId,
50
+ channelName: CmdChannel_1.name,
32
51
  };
33
- const chat = await this.chatResolver.resolve(origin);
52
+ const user = await this.userResolver.resolve(userConnection);
34
53
  if (!this.callBack)
35
54
  return;
36
55
  this.callBack({
37
- chatId: chat.getId(),
38
- origin,
56
+ chat,
57
+ user,
39
58
  message: {
59
+ chatConnection,
60
+ userConnection,
40
61
  text: trimmedInput,
41
- sender: {
42
- shortName: 'Cmd',
43
- },
62
+ senderName: 'cmd',
44
63
  },
45
64
  reply: (message) => {
46
- console.log(`\n[${message.sender.shortName}]: ${message.text}\n`);
65
+ console.log(`\n[${message.senderName}]: ${message.text}\n`);
47
66
  this.rl.prompt();
48
67
  },
49
68
  });
@@ -52,7 +71,8 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
52
71
  };
53
72
  CmdChannel = CmdChannel_1 = __decorate([
54
73
  injectable(),
55
- __metadata("design:paramtypes", [ChatResolver])
74
+ __metadata("design:paramtypes", [ChatResolver,
75
+ UserResolver])
56
76
  ], CmdChannel);
57
77
 
58
78
  export { CmdChannel };
@@ -1,5 +1,6 @@
1
1
  import { container } from '../../injection/index.js';
2
2
  import '../../controller/channel/ChatResolver.js';
3
+ import '../../controller/channel/UserResolver.js';
3
4
  import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
4
5
  import { TelegramChannel } from './TelegramChannel.js';
5
6
  import { TelegramChannelConfig } from './TelegramChannelConfig.js';
@@ -1,17 +1,25 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
- import { Bot } from 'grammy';
3
2
  import { ChatResolver } from '../../controller/channel/ChatResolver.js';
4
- import { TelegramChannelConfig } from './TelegramChannelConfig.js';
3
+ import { UserResolver } from '../../controller/channel/UserResolver.js';
5
4
  import { injectable } from '../../injection/index.js';
5
+ import '../../controller/metadata/ControllerMetadataStore.js';
6
+ import { Bot } from 'grammy';
7
+ import '../../core/chat/repository/ram/RamChatRepository.js';
8
+ import '../../core/chat/repository/IChatRepository.js';
9
+ import '../../core/user/repository/ram/RamUserRepository.js';
10
+ import '../../core/user/repository/IUserRepository.js';
11
+ import { TelegramChannelConfig } from './TelegramChannelConfig.js';
6
12
 
7
13
  var TelegramChannel_1;
8
14
  let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
9
15
  config;
10
16
  chatResolver;
17
+ userResolver;
11
18
  bot;
12
- constructor(config, chatResolver) {
19
+ constructor(config, chatResolver, userResolver) {
13
20
  this.config = config;
14
21
  this.chatResolver = chatResolver;
22
+ this.userResolver = userResolver;
15
23
  this.bot = new Bot(this.config.botToken);
16
24
  }
17
25
  listen(callback) {
@@ -19,21 +27,25 @@ let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
19
27
  if (!ctx.message) {
20
28
  return;
21
29
  }
22
- const origin = {
23
- chatId: ctx.message.chat.id.toString(),
30
+ const chatConnection = {
31
+ id: ctx.message.chat.id.toString(),
24
32
  chatType: ctx.message.chat.type === 'private' ? 'PRIVATE' : 'GROUP',
25
- channelType: TelegramChannel_1,
33
+ channelName: TelegramChannel_1.name,
34
+ };
35
+ const chat = await this.chatResolver.resolve(chatConnection);
36
+ const userConnection = {
37
+ id: ctx.from.id.toString(),
38
+ channelName: TelegramChannel_1.name,
26
39
  };
27
- const chat = await this.chatResolver.resolve(origin);
40
+ const user = await this.userResolver.resolve(userConnection);
28
41
  callback({
29
- chatId: chat.getId(),
30
- origin,
42
+ chat,
43
+ user,
31
44
  message: {
45
+ chatConnection,
46
+ userConnection,
47
+ senderName: ctx.from.first_name,
32
48
  text: ctx.message.text,
33
- sender: {
34
- shortName: ctx.from.first_name,
35
- senderId: ctx.from.id != null ? ctx.from.id.toString() : undefined,
36
- },
37
49
  },
38
50
  reply: (replyMessage) => {
39
51
  replyMessage.text && ctx.reply(replyMessage.text);
@@ -48,7 +60,8 @@ let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
48
60
  TelegramChannel = TelegramChannel_1 = __decorate([
49
61
  injectable(),
50
62
  __metadata("design:paramtypes", [TelegramChannelConfig,
51
- ChatResolver])
63
+ ChatResolver,
64
+ UserResolver])
52
65
  ], TelegramChannel);
53
66
 
54
67
  export { TelegramChannel };
@@ -1,10 +1,12 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
+ import { ChatMemory } from '../core/chat/repository/IChatMemory.js';
3
+ import '../core/chat/repository/ram/RamChatRepository.js';
4
+ import '../core/chat/repository/IChatRepository.js';
5
+ import '../core/user/repository/ram/RamUserRepository.js';
6
+ import '../core/user/repository/IUserRepository.js';
7
+ import { injectable } from '../injection/index.js';
2
8
  import { v4 } from 'uuid';
3
9
  import { ChatBotAdapter } from './ChatBotAdapter.js';
4
- import './chat/repository/ram/RamChatRepository.js';
5
- import { ChatMemory } from './chat/repository/IChatMemory.js';
6
- import './chat/repository/IChatRepository.js';
7
- import { injectable } from '../injection/index.js';
8
10
 
9
11
  let ChatBot = class ChatBot {
10
12
  memory;
@@ -18,7 +20,7 @@ let ChatBot = class ChatBot {
18
20
  const newChatItem = {
19
21
  id: v4(),
20
22
  createdAt: new Date(),
21
- type: 'USER_MESSAGE',
23
+ type: 'CONNECTION_MESSAGE',
22
24
  content: message,
23
25
  };
24
26
  await this.memory.saveItem(newChatItem);
@@ -1,4 +1,12 @@
1
+ import 'reflect-metadata';
2
+ import '../injection/index.js';
3
+ import '../mindset/metadata/MindsetMetadataStore.js';
4
+ import '../mindset/MindsetOperator.js';
1
5
  import { v4 } from 'uuid';
6
+ import '../core/chat/repository/ram/RamChatRepository.js';
7
+ import '../core/chat/repository/IChatRepository.js';
8
+ import '../core/user/repository/ram/RamUserRepository.js';
9
+ import '../core/user/repository/IUserRepository.js';
2
10
 
3
11
  class ChatBotAdapter {
4
12
  mindset;
@@ -42,26 +50,26 @@ class ChatBotAdapter {
42
50
  return systemPrompt;
43
51
  }
44
52
  async buildBotMessageItem(text) {
45
- const shortName = (await this.mindset.identity()).name;
53
+ const senderName = (await this.mindset.identity()).name;
46
54
  const newBotMessage = {
47
55
  id: v4(),
48
56
  createdAt: new Date(),
49
57
  type: 'BOT_MESSAGE',
50
58
  content: {
51
- sender: { shortName },
59
+ senderName,
52
60
  text,
53
61
  },
54
62
  };
55
63
  return newBotMessage;
56
64
  }
57
- async buildFunctionCallItem(foreignId, functionName, functionArguments) {
65
+ async buildFunctionCallItem(id, functionName, functionArguments) {
58
66
  const functionResult = await this.mindset.callFunction(functionName, functionArguments);
59
67
  const newFunctionCall = {
60
68
  id: v4(),
61
69
  createdAt: new Date(),
62
70
  type: 'FUNCTION_CALL',
63
71
  content: {
64
- foreignId,
72
+ id,
65
73
  name: functionName,
66
74
  arguments: functionArguments,
67
75
  result: functionResult,
@@ -1,4 +1,7 @@
1
+ import 'reflect-metadata';
1
2
  import { container, inject } from '../../injection/index.js';
3
+ import '../../mindset/metadata/MindsetMetadataStore.js';
4
+ import '../../mindset/MindsetOperator.js';
2
5
  import { v4 } from 'uuid';
3
6
  import { ChatBotMetadataStore } from './ChatBotMetadataStore.js';
4
7
 
@@ -1,5 +1,8 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { singleton } from '../../injection/index.js';
3
+ import 'reflect-metadata';
4
+ import '../../mindset/metadata/MindsetMetadataStore.js';
5
+ import '../../mindset/MindsetOperator.js';
3
6
 
4
7
  let ChatBotMetadataStore = class ChatBotMetadataStore {
5
8
  chatBots = [];
@@ -1,22 +1,21 @@
1
1
  import { __decorate, __metadata } from 'tslib';
2
- import '../../chatbot/chat/repository/ram/RamChatRepository.js';
3
- import { ChatRepository } from '../../chatbot/chat/repository/IChatRepository.js';
4
- import { Chat } from '../../chatbot/chat/Chat.js';
2
+ import '../../core/chat/repository/ram/RamChatRepository.js';
3
+ import { ChatRepository } from '../../core/chat/repository/IChatRepository.js';
4
+ import { Chat } from '../../core/chat/Chat.js';
5
+ import '../../core/user/repository/ram/RamUserRepository.js';
6
+ import '../../core/user/repository/IUserRepository.js';
5
7
  import { injectable } from '../../injection/index.js';
6
- import 'uuid';
7
- import '../../chatbot/metadata/ChatBotMetadataStore.js';
8
- import '../../chatbot/ChatBot.js';
9
8
 
10
9
  let ChatResolver = class ChatResolver {
11
10
  chatRepository;
12
11
  constructor(chatRepository) {
13
12
  this.chatRepository = chatRepository;
14
13
  }
15
- async resolve(origin) {
16
- if (origin.chatType === 'GROUP') {
17
- return this.resolveGroupChat({ channelType: origin.channelType.name, chatId: origin.chatId });
14
+ async resolve(connection) {
15
+ if (connection.chatType === 'GROUP') {
16
+ return this.resolveGroupChat(connection);
18
17
  }
19
- return this.resolvePrivateChat({ channelType: origin.channelType.name, chatId: origin.chatId });
18
+ return this.resolvePrivateChat(connection);
20
19
  }
21
20
  async resolveGroupChat(connection) {
22
21
  let chat = await this.chatRepository.findByConnection(connection);
@@ -0,0 +1,23 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import '../../core/chat/repository/ram/RamChatRepository.js';
3
+ import '../../core/chat/repository/IChatRepository.js';
4
+ import '../../core/user/repository/ram/RamUserRepository.js';
5
+ import { UserRepository } from '../../core/user/repository/IUserRepository.js';
6
+ import { singleton } from '../../injection/index.js';
7
+
8
+ let UserResolver = class UserResolver {
9
+ userRepository;
10
+ constructor(userRepository) {
11
+ this.userRepository = userRepository;
12
+ }
13
+ async resolve(connection) {
14
+ let user = await this.userRepository.findByConnection(connection);
15
+ return user;
16
+ }
17
+ };
18
+ UserResolver = __decorate([
19
+ singleton(),
20
+ __metadata("design:paramtypes", [UserRepository])
21
+ ], UserResolver);
22
+
23
+ export { UserResolver };
@@ -0,0 +1,15 @@
1
+ import './user/repository/ram/RamUserRepository.js';
2
+ import './user/repository/IUserRepository.js';
3
+
4
+ class MessageContext {
5
+ message;
6
+ chat;
7
+ user;
8
+ constructor(message, chat, user) {
9
+ this.message = message;
10
+ this.chat = chat;
11
+ this.user = user;
12
+ }
13
+ }
14
+
15
+ export { MessageContext };
@@ -11,7 +11,7 @@ class Chat {
11
11
  }
12
12
  hasConnection(connection) {
13
13
  for (const con of this.data.connections) {
14
- if (con.channelType === connection.channelType && con.chatId === connection.chatId) {
14
+ if (con.channelName === connection.channelName && con.id === connection.id) {
15
15
  return true;
16
16
  }
17
17
  }
@@ -1,5 +1,6 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { v4 } from 'uuid';
3
+ import '../IChatRepository.js';
3
4
  import { RamChatMemory } from './RamChatMemory.js';
4
5
  import { singleton } from '../../../../injection/index.js';
5
6
 
@@ -0,0 +1,42 @@
1
+ class User {
2
+ data;
3
+ constructor(data) {
4
+ this.data = data;
5
+ }
6
+ getId() {
7
+ if (!this.data.id) {
8
+ throw new Error('User have not ID');
9
+ }
10
+ return this.data.id;
11
+ }
12
+ hasConnection(connection) {
13
+ for (const con of this.data.connections) {
14
+ if (con.channelName === connection.channelName && con.id === connection.id) {
15
+ return true;
16
+ }
17
+ }
18
+ return false;
19
+ }
20
+ wasCreated() {
21
+ return !!this.data.createdAt || !!this.data.id;
22
+ }
23
+ validate() {
24
+ if (!this.data.id) {
25
+ throw new Error('User ID is required');
26
+ }
27
+ if (!this.data.createdAt) {
28
+ throw new Error('User createdAt is required');
29
+ }
30
+ }
31
+ getValue(key) {
32
+ return this.data.keyValueData[key];
33
+ }
34
+ setValue(key, value) {
35
+ this.data.keyValueData[key] = value;
36
+ }
37
+ addConnection(connection) {
38
+ this.data.connections.push(connection);
39
+ }
40
+ }
41
+
42
+ export { User };
@@ -0,0 +1,19 @@
1
+ import { __decorate } from 'tslib';
2
+ import { singleton } from '../../../injection/index.js';
3
+
4
+ let UserRepository = class UserRepository {
5
+ create(chat) {
6
+ throw new Error('Method not implemented.');
7
+ }
8
+ update(chat) {
9
+ throw new Error('Method not implemented.');
10
+ }
11
+ findByConnection(query) {
12
+ throw new Error('Method not implemented.');
13
+ }
14
+ };
15
+ UserRepository = __decorate([
16
+ singleton()
17
+ ], UserRepository);
18
+
19
+ export { UserRepository };
@@ -0,0 +1,28 @@
1
+ import { __decorate } from 'tslib';
2
+ import { v4 } from 'uuid';
3
+ import '../IUserRepository.js';
4
+ import { singleton } from '../../../../injection/index.js';
5
+
6
+ let RamUserRepository = class RamUserRepository {
7
+ items = [];
8
+ async create(chat) {
9
+ if (chat.wasCreated()) {
10
+ throw new Error('User already created');
11
+ }
12
+ chat['data'].id = v4();
13
+ chat['data'].createdAt = new Date();
14
+ chat.validate();
15
+ this.items.push(chat);
16
+ }
17
+ async findByConnection(query) {
18
+ return this.items.find((item) => item.hasConnection(query)) ?? null;
19
+ }
20
+ async update(chat) {
21
+ // TODO
22
+ }
23
+ };
24
+ RamUserRepository = __decorate([
25
+ singleton()
26
+ ], RamUserRepository);
27
+
28
+ export { RamUserRepository };