@wabot-dev/framework 0.0.1

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 (127) hide show
  1. package/LICENSE +21 -0
  2. package/dist/src/_virtual/_commonjsHelpers.js +5 -0
  3. package/dist/src/_virtual/main.js +7 -0
  4. package/dist/src/_virtual/main2.js +3 -0
  5. package/dist/src/ai/index.d.ts +1 -0
  6. package/dist/src/ai/openia/OpenaiChatBotAdapter.d.ts +9 -0
  7. package/dist/src/ai/openia/OpenaiChatBotAdapter.js +85 -0
  8. package/dist/src/ai/openia/index.d.ts +1 -0
  9. package/dist/src/channels/cmd/CmdChannel.d.ts +9 -0
  10. package/dist/src/channels/cmd/CmdChannel.js +58 -0
  11. package/dist/src/channels/cmd/index.d.ts +1 -0
  12. package/dist/src/channels/index.d.ts +2 -0
  13. package/dist/src/channels/telegram/@telegram.d.ts +2 -0
  14. package/dist/src/channels/telegram/@telegram.js +19 -0
  15. package/dist/src/channels/telegram/TelegramChannel.d.ts +12 -0
  16. package/dist/src/channels/telegram/TelegramChannel.js +54 -0
  17. package/dist/src/channels/telegram/TelegramChannelConfig.d.ts +7 -0
  18. package/dist/src/channels/telegram/TelegramChannelConfig.js +8 -0
  19. package/dist/src/channels/telegram/index.d.ts +3 -0
  20. package/dist/src/chatbot/ChatBot.d.ts +11 -0
  21. package/dist/src/chatbot/ChatBot.js +51 -0
  22. package/dist/src/chatbot/ChatBotAdapter.d.ts +26 -0
  23. package/dist/src/chatbot/ChatBotAdapter.js +74 -0
  24. package/dist/src/chatbot/IChatBot.d.ts +4 -0
  25. package/dist/src/chatbot/chat/Chat.d.ts +23 -0
  26. package/dist/src/chatbot/chat/Chat.js +55 -0
  27. package/dist/src/chatbot/chat/IChatFunctionCall.d.ts +6 -0
  28. package/dist/src/chatbot/chat/IChatItem.d.ts +19 -0
  29. package/dist/src/chatbot/chat/index.d.ts +4 -0
  30. package/dist/src/chatbot/chat/repository/IChatMemory.d.ts +9 -0
  31. package/dist/src/chatbot/chat/repository/IChatMemory.js +10 -0
  32. package/dist/src/chatbot/chat/repository/IChatRepository.d.ts +12 -0
  33. package/dist/src/chatbot/chat/repository/IChatRepository.js +19 -0
  34. package/dist/src/chatbot/chat/repository/index.d.ts +3 -0
  35. package/dist/src/chatbot/chat/repository/ram/RamChatMemory.d.ts +8 -0
  36. package/dist/src/chatbot/chat/repository/ram/RamChatMemory.js +14 -0
  37. package/dist/src/chatbot/chat/repository/ram/RamChatRepository.d.ts +11 -0
  38. package/dist/src/chatbot/chat/repository/ram/RamChatRepository.js +41 -0
  39. package/dist/src/chatbot/chat/repository/ram/index.d.ts +2 -0
  40. package/dist/src/chatbot/index.d.ts +5 -0
  41. package/dist/src/chatbot/metadata/@chatBot.d.ts +3 -0
  42. package/dist/src/chatbot/metadata/@chatBot.js +18 -0
  43. package/dist/src/chatbot/metadata/ChatBotMetadataStore.d.ts +6 -0
  44. package/dist/src/chatbot/metadata/ChatBotMetadataStore.js +17 -0
  45. package/dist/src/chatbot/metadata/IChatBotMetadata.d.ts +7 -0
  46. package/dist/src/chatbot/metadata/index.d.ts +3 -0
  47. package/dist/src/context/IContext.d.ts +18 -0
  48. package/dist/src/context/IContext.js +10 -0
  49. package/dist/src/context/index.d.ts +1 -0
  50. package/dist/src/controller/channel/ChatResolver.d.ts +9 -0
  51. package/dist/src/controller/channel/ChatResolver.js +43 -0
  52. package/dist/src/controller/channel/IChatChannel.d.ts +5 -0
  53. package/dist/src/controller/channel/IMessageContext.d.ts +10 -0
  54. package/dist/src/controller/channel/index.d.ts +3 -0
  55. package/dist/src/controller/index.d.ts +2 -0
  56. package/dist/src/controller/metadata/ControllerMetadataStore.d.ts +12 -0
  57. package/dist/src/controller/metadata/ControllerMetadataStore.js +42 -0
  58. package/dist/src/controller/metadata/channel/IChannelMetadata.d.ts +8 -0
  59. package/dist/src/controller/metadata/channel/index.d.ts +1 -0
  60. package/dist/src/controller/metadata/controller/@chatController.d.ts +3 -0
  61. package/dist/src/controller/metadata/controller/@chatController.js +14 -0
  62. package/dist/src/controller/metadata/controller/IChatControllerConfig.d.ts +2 -0
  63. package/dist/src/controller/metadata/controller/IChatControllerMetadata.d.ts +3 -0
  64. package/dist/src/controller/metadata/controller/index.d.ts +3 -0
  65. package/dist/src/controller/metadata/index.d.ts +3 -0
  66. package/dist/src/index.d.ts +10 -0
  67. package/dist/src/index.js +34 -0
  68. package/dist/src/injection/Container.d.ts +18 -0
  69. package/dist/src/injection/Container.js +43 -0
  70. package/dist/src/injection/index.d.ts +3 -0
  71. package/dist/src/injection/index.js +4 -0
  72. package/dist/src/message/IChatDocument.d.ts +2 -0
  73. package/dist/src/message/IChatImage.d.ts +2 -0
  74. package/dist/src/message/IChatMessage.d.ts +9 -0
  75. package/dist/src/message/IChatSender.d.ts +4 -0
  76. package/dist/src/message/index.d.ts +4 -0
  77. package/dist/src/mindset/IMindset.d.ts +17 -0
  78. package/dist/src/mindset/IMindset.js +13 -0
  79. package/dist/src/mindset/MindsetOperator.d.ts +25 -0
  80. package/dist/src/mindset/MindsetOperator.js +101 -0
  81. package/dist/src/mindset/index.d.ts +3 -0
  82. package/dist/src/mindset/metadata/IMindsetMetadata.d.ts +28 -0
  83. package/dist/src/mindset/metadata/MindsetMetadataStore.d.ts +19 -0
  84. package/dist/src/mindset/metadata/MindsetMetadataStore.js +134 -0
  85. package/dist/src/mindset/metadata/functions/@mindsetFunction.d.ts +3 -0
  86. package/dist/src/mindset/metadata/functions/@mindsetFunction.js +21 -0
  87. package/dist/src/mindset/metadata/functions/IMindsetFunctionConfig.d.ts +3 -0
  88. package/dist/src/mindset/metadata/functions/IMindsetFunctionDecoration.d.ts +7 -0
  89. package/dist/src/mindset/metadata/functions/decoratorNames.d.ts +1 -0
  90. package/dist/src/mindset/metadata/functions/decoratorNames.js +3 -0
  91. package/dist/src/mindset/metadata/functions/index.d.ts +4 -0
  92. package/dist/src/mindset/metadata/index.d.ts +6 -0
  93. package/dist/src/mindset/metadata/mindsets/@mindset.d.ts +5 -0
  94. package/dist/src/mindset/metadata/mindsets/@mindset.js +18 -0
  95. package/dist/src/mindset/metadata/mindsets/IMindsetConfig.d.ts +4 -0
  96. package/dist/src/mindset/metadata/mindsets/IMindsetDecoration.d.ts +6 -0
  97. package/dist/src/mindset/metadata/mindsets/decoratorNames.d.ts +1 -0
  98. package/dist/src/mindset/metadata/mindsets/decoratorNames.js +3 -0
  99. package/dist/src/mindset/metadata/mindsets/index.d.ts +3 -0
  100. package/dist/src/mindset/metadata/modules/@mindsetModule.d.ts +4 -0
  101. package/dist/src/mindset/metadata/modules/@mindsetModule.js +18 -0
  102. package/dist/src/mindset/metadata/modules/IMindsetModuleConfig.d.ts +3 -0
  103. package/dist/src/mindset/metadata/modules/IMindsetModuleDecoration.d.ts +5 -0
  104. package/dist/src/mindset/metadata/modules/decoratorNames.d.ts +1 -0
  105. package/dist/src/mindset/metadata/modules/decoratorNames.js +3 -0
  106. package/dist/src/mindset/metadata/modules/index.d.ts +4 -0
  107. package/dist/src/mindset/metadata/params/@isOptional.d.ts +2 -0
  108. package/dist/src/mindset/metadata/params/@isOptional.js +21 -0
  109. package/dist/src/mindset/metadata/params/@param.d.ts +3 -0
  110. package/dist/src/mindset/metadata/params/@param.js +21 -0
  111. package/dist/src/mindset/metadata/params/IParamConfig.d.ts +4 -0
  112. package/dist/src/mindset/metadata/params/IParamDecoration.d.ts +7 -0
  113. package/dist/src/mindset/metadata/params/decoratorNames.d.ts +2 -0
  114. package/dist/src/mindset/metadata/params/decoratorNames.js +4 -0
  115. package/dist/src/mindset/metadata/params/index.d.ts +3 -0
  116. package/dist/src/node_modules/dotenv/lib/main.js +376 -0
  117. package/dist/src/node_modules/dotenv/package.json.js +5 -0
  118. package/dist/src/server/index.d.ts +3 -0
  119. package/dist/src/server/prepareChatContainer.d.ts +5 -0
  120. package/dist/src/server/prepareChatContainer.js +41 -0
  121. package/dist/src/server/runChannel.d.ts +12 -0
  122. package/dist/src/server/runChannel.js +29 -0
  123. package/dist/src/server/runServer.d.ts +8 -0
  124. package/dist/src/server/runServer.js +42 -0
  125. package/dist/src/shared/IConstructor.d.ts +1 -0
  126. package/dist/src/shared/index.d.ts +1 -0
  127. package/package.json +47 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Wabot
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,5 @@
1
+ function getDefaultExportFromCjs (x) {
2
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
3
+ }
4
+
5
+ export { getDefaultExportFromCjs };
@@ -0,0 +1,7 @@
1
+ import { getDefaultExportFromCjs } from './_commonjsHelpers.js';
2
+ import { __require as requireMain } from '../node_modules/dotenv/lib/main.js';
3
+
4
+ var mainExports = requireMain();
5
+ var dotenv = /*@__PURE__*/getDefaultExportFromCjs(mainExports);
6
+
7
+ export { dotenv as default };
@@ -0,0 +1,3 @@
1
+ var main = {exports: {}};
2
+
3
+ export { main as __module };
@@ -0,0 +1 @@
1
+ export * from './openia';
@@ -0,0 +1,9 @@
1
+ import { ChatBotAdapter, IChatItem } from '@/chatbot';
2
+ import { MindsetOperator } from '@/mindset';
3
+ export declare class OpenaiChatBotAdapter extends ChatBotAdapter {
4
+ private openai;
5
+ private model;
6
+ constructor(mindset: MindsetOperator);
7
+ generateNextChatItem(chatItems: IChatItem[]): Promise<IChatItem>;
8
+ private mapChatItems;
9
+ }
@@ -0,0 +1,85 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { injectable } from '../../injection/index.js';
3
+ import { OpenAI } from 'openai';
4
+ import '../../chatbot/chat/repository/ram/RamChatRepository.js';
5
+ import '../../chatbot/chat/repository/IChatRepository.js';
6
+ import 'uuid';
7
+ import '../../chatbot/metadata/ChatBotMetadataStore.js';
8
+ import '../../chatbot/ChatBot.js';
9
+ import { ChatBotAdapter } from '../../chatbot/ChatBotAdapter.js';
10
+ import 'reflect-metadata';
11
+ import '../../mindset/metadata/MindsetMetadataStore.js';
12
+ import { MindsetOperator } from '../../mindset/MindsetOperator.js';
13
+
14
+ let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
15
+ openai = new OpenAI();
16
+ model;
17
+ constructor(mindset) {
18
+ super(mindset);
19
+ const model = process.env.OPENAI_CHAT_MODEL;
20
+ if (!model) {
21
+ throw new Error(`OPENAI_CHAT_MODEL env variable is required`);
22
+ }
23
+ this.model = model;
24
+ }
25
+ async generateNextChatItem(chatItems) {
26
+ const systemPrompt = await this.systemPrompt();
27
+ const tools = (await this.mindset.allFunctionsDescriptors()).map((fn) => {
28
+ const parameters = { ...fn.parameters, additionalProperties: false, type: 'object' };
29
+ return { ...fn, type: 'function', parameters, strict: true };
30
+ });
31
+ const response = await this.openai.responses.create({
32
+ model: this.model,
33
+ input: [{ role: 'system', content: systemPrompt }, ...this.mapChatItems(chatItems)],
34
+ tools,
35
+ });
36
+ let newChatItem;
37
+ if (response.output_text) {
38
+ newChatItem = await this.buildBotMessageItem(response.output_text);
39
+ }
40
+ else if (response.output && response.output[0]?.type == 'function_call') {
41
+ newChatItem = await this.buildFunctionCallItem(response.output[0].call_id, response.output[0].name, response.output[0].arguments);
42
+ }
43
+ else {
44
+ throw new Error('Not supported OpenIA Response');
45
+ }
46
+ return newChatItem;
47
+ }
48
+ mapChatItems(chatItems) {
49
+ const openIaInput = [];
50
+ for (const item of chatItems) {
51
+ if (item.type === 'USER_MESSAGE') {
52
+ if (!item.content.text) {
53
+ throw new Error('System message content is empty');
54
+ }
55
+ openIaInput.push({ role: 'user', content: item.content.text });
56
+ }
57
+ else if (item.type === 'BOT_MESSAGE') {
58
+ if (!item.content.text) {
59
+ throw new Error('System message content is empty');
60
+ }
61
+ openIaInput.push({ role: 'assistant', content: item.content.text });
62
+ }
63
+ if (item.type === 'FUNCTION_CALL') {
64
+ openIaInput.push({
65
+ type: 'function_call',
66
+ call_id: item.content.foreignId,
67
+ name: item.content.name,
68
+ arguments: JSON.stringify(item.content.arguments),
69
+ });
70
+ openIaInput.push({
71
+ type: 'function_call_output',
72
+ call_id: item.content.foreignId,
73
+ output: item.content.result,
74
+ });
75
+ }
76
+ }
77
+ return openIaInput;
78
+ }
79
+ };
80
+ OpenaiChatBotAdapter = __decorate([
81
+ injectable(),
82
+ __metadata("design:paramtypes", [MindsetOperator])
83
+ ], OpenaiChatBotAdapter);
84
+
85
+ export { OpenaiChatBotAdapter };
@@ -0,0 +1 @@
1
+ export * from './OpenaiChatBotAdapter';
@@ -0,0 +1,9 @@
1
+ import { ChatResolver, IChatChannel, IMessageContext } from '@/controller';
2
+ export declare class CmdChannel implements IChatChannel {
3
+ private chatResolver;
4
+ private rl;
5
+ private callBack;
6
+ constructor(chatResolver: ChatResolver);
7
+ listen(callback: (message: IMessageContext) => void): void;
8
+ connect(): void;
9
+ }
@@ -0,0 +1,58 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { ChatResolver } from '../../controller/channel/ChatResolver.js';
3
+ import { injectable } from '../../injection/index.js';
4
+ import '../../controller/metadata/ControllerMetadataStore.js';
5
+ import * as readline from 'readline';
6
+
7
+ var CmdChannel_1;
8
+ let CmdChannel = CmdChannel_1 = class CmdChannel {
9
+ chatResolver;
10
+ rl = readline.createInterface({
11
+ input: process.stdin,
12
+ output: process.stdout,
13
+ });
14
+ callBack = null;
15
+ constructor(chatResolver) {
16
+ this.chatResolver = chatResolver;
17
+ }
18
+ listen(callback) {
19
+ this.callBack = callback;
20
+ }
21
+ connect() {
22
+ this.rl.on('line', async (input) => {
23
+ const trimmedInput = input.trim();
24
+ if (trimmedInput.toLowerCase() === 'exit') {
25
+ this.rl.close();
26
+ return;
27
+ }
28
+ const origin = {
29
+ chatId: 'cmd',
30
+ chatType: 'PRIVATE',
31
+ channelType: CmdChannel_1,
32
+ };
33
+ const chat = await this.chatResolver.resolve(origin);
34
+ if (!this.callBack)
35
+ return;
36
+ this.callBack({
37
+ chatId: chat.getId(),
38
+ origin,
39
+ message: {
40
+ text: trimmedInput,
41
+ sender: {
42
+ shortName: 'Cmd',
43
+ },
44
+ },
45
+ reply: (message) => {
46
+ console.log(`\n[${message.sender.shortName}]: ${message.text}\n`);
47
+ this.rl.prompt();
48
+ },
49
+ });
50
+ });
51
+ }
52
+ };
53
+ CmdChannel = CmdChannel_1 = __decorate([
54
+ injectable(),
55
+ __metadata("design:paramtypes", [ChatResolver])
56
+ ], CmdChannel);
57
+
58
+ export { CmdChannel };
@@ -0,0 +1 @@
1
+ export * from './CmdChannel';
@@ -0,0 +1,2 @@
1
+ export * from './cmd';
2
+ export * from './telegram';
@@ -0,0 +1,2 @@
1
+ import { ITelegramChannelConfig } from './TelegramChannelConfig';
2
+ export declare function telegram(config: ITelegramChannelConfig): (target: object, propertyKey: string | symbol) => void;
@@ -0,0 +1,19 @@
1
+ import { container } from '../../injection/index.js';
2
+ import '../../controller/channel/ChatResolver.js';
3
+ import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
4
+ import { TelegramChannel } from './TelegramChannel.js';
5
+ import { TelegramChannelConfig } from './TelegramChannelConfig.js';
6
+
7
+ function telegram(config) {
8
+ return function (target, propertyKey) {
9
+ const store = container.resolve(ControllerMetadataStore);
10
+ store.saveChannelMetadata({
11
+ channelConstructor: TelegramChannel,
12
+ functionName: propertyKey.toString(),
13
+ controllerConstructor: target.constructor,
14
+ channelConfig: new TelegramChannelConfig(config.botToken),
15
+ });
16
+ };
17
+ }
18
+
19
+ export { telegram };
@@ -0,0 +1,12 @@
1
+ import { IChatChannel } from '../../controller/channel/IChatChannel';
2
+ import { ChatResolver } from '../../controller/channel/ChatResolver';
3
+ import { IMessageContext } from '../../controller/channel/IMessageContext';
4
+ import { TelegramChannelConfig } from './TelegramChannelConfig';
5
+ export declare class TelegramChannel implements IChatChannel {
6
+ private config;
7
+ private chatResolver;
8
+ private bot;
9
+ constructor(config: TelegramChannelConfig, chatResolver: ChatResolver);
10
+ listen(callback: (message: IMessageContext) => void): void;
11
+ connect(): void;
12
+ }
@@ -0,0 +1,54 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { Bot } from 'grammy';
3
+ import { ChatResolver } from '../../controller/channel/ChatResolver.js';
4
+ import { TelegramChannelConfig } from './TelegramChannelConfig.js';
5
+ import { injectable } from '../../injection/index.js';
6
+
7
+ var TelegramChannel_1;
8
+ let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
9
+ config;
10
+ chatResolver;
11
+ bot;
12
+ constructor(config, chatResolver) {
13
+ this.config = config;
14
+ this.chatResolver = chatResolver;
15
+ this.bot = new Bot(this.config.botToken);
16
+ }
17
+ listen(callback) {
18
+ this.bot.on('message', async (ctx) => {
19
+ if (!ctx.message) {
20
+ return;
21
+ }
22
+ const origin = {
23
+ chatId: ctx.message.chat.id.toString(),
24
+ chatType: ctx.message.chat.type === 'private' ? 'PRIVATE' : 'GROUP',
25
+ channelType: TelegramChannel_1,
26
+ };
27
+ const chat = await this.chatResolver.resolve(origin);
28
+ callback({
29
+ chatId: chat.getId(),
30
+ origin,
31
+ message: {
32
+ 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
+ },
38
+ reply: (replyMessage) => {
39
+ replyMessage.text && ctx.reply(replyMessage.text);
40
+ },
41
+ });
42
+ });
43
+ }
44
+ connect() {
45
+ this.bot.start();
46
+ }
47
+ };
48
+ TelegramChannel = TelegramChannel_1 = __decorate([
49
+ injectable(),
50
+ __metadata("design:paramtypes", [TelegramChannelConfig,
51
+ ChatResolver])
52
+ ], TelegramChannel);
53
+
54
+ export { TelegramChannel };
@@ -0,0 +1,7 @@
1
+ export interface ITelegramChannelConfig {
2
+ botToken: string;
3
+ }
4
+ export declare class TelegramChannelConfig implements ITelegramChannelConfig {
5
+ botToken: string;
6
+ constructor(botToken: string);
7
+ }
@@ -0,0 +1,8 @@
1
+ class TelegramChannelConfig {
2
+ botToken;
3
+ constructor(botToken) {
4
+ this.botToken = botToken;
5
+ }
6
+ }
7
+
8
+ export { TelegramChannelConfig };
@@ -0,0 +1,3 @@
1
+ export * from './@telegram';
2
+ export * from './TelegramChannelConfig';
3
+ export * from './TelegramChannel';
@@ -0,0 +1,11 @@
1
+ import { IChatMessage } from '@/message';
2
+ import { ChatBotAdapter } from './ChatBotAdapter';
3
+ import { IChatBot } from './IChatBot';
4
+ import { ChatMemory } from './chat';
5
+ export declare class ChatBot implements IChatBot {
6
+ private memory;
7
+ private adapter;
8
+ constructor(memory: ChatMemory, adapter: ChatBotAdapter);
9
+ sendMessage(message: IChatMessage, callback: (message: IChatMessage) => void): Promise<void>;
10
+ protected processLoop(callback: (message: IChatMessage) => void): Promise<void>;
11
+ }
@@ -0,0 +1,51 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { v4 } from 'uuid';
3
+ 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
+
9
+ let ChatBot = class ChatBot {
10
+ memory;
11
+ adapter;
12
+ constructor(memory, adapter) {
13
+ this.memory = memory;
14
+ this.adapter = adapter;
15
+ this.memory = memory;
16
+ }
17
+ async sendMessage(message, callback) {
18
+ const newChatItem = {
19
+ id: v4(),
20
+ createdAt: new Date(),
21
+ type: 'USER_MESSAGE',
22
+ content: message,
23
+ };
24
+ await this.memory.saveItem(newChatItem);
25
+ this.processLoop(callback);
26
+ }
27
+ async processLoop(callback) {
28
+ const prevChatItems = await this.memory.findLastItems(10);
29
+ if (prevChatItems.length === 0) {
30
+ return;
31
+ }
32
+ const lastChatItem = prevChatItems[prevChatItems.length - 1];
33
+ if (lastChatItem.type === 'BOT_MESSAGE') {
34
+ return;
35
+ }
36
+ const newChatItem = await this.adapter.generateNextChatItem(prevChatItems);
37
+ await this.memory.saveItem(newChatItem);
38
+ if (newChatItem.type === 'BOT_MESSAGE') {
39
+ callback(newChatItem.content);
40
+ return;
41
+ }
42
+ this.processLoop(callback);
43
+ }
44
+ };
45
+ ChatBot = __decorate([
46
+ injectable(),
47
+ __metadata("design:paramtypes", [ChatMemory,
48
+ ChatBotAdapter])
49
+ ], ChatBot);
50
+
51
+ export { ChatBot };
@@ -0,0 +1,26 @@
1
+ import { MindsetOperator } from '@/mindset/MindsetOperator';
2
+ import { IChatItem } from './chat/IChatItem';
3
+ export interface IChatBotAdapter {
4
+ generateNextChatItem(chatItems: IChatItem[]): Promise<IChatItem>;
5
+ }
6
+ export declare class ChatBotAdapter implements IChatBotAdapter {
7
+ protected mindset: MindsetOperator;
8
+ constructor(mindset: MindsetOperator);
9
+ generateNextChatItem(chatItems: IChatItem[]): Promise<IChatItem>;
10
+ protected systemPrompt(): Promise<string>;
11
+ protected buildBotMessageItem(text: string): Promise<{
12
+ id: string;
13
+ createdAt: Date;
14
+ } & import("./chat").ISystemMessageItem>;
15
+ protected buildFunctionCallItem(foreignId: string, functionName: string, functionArguments: string): Promise<{
16
+ readonly id: string;
17
+ readonly createdAt: Date;
18
+ readonly type: "FUNCTION_CALL";
19
+ readonly content: {
20
+ readonly foreignId: string;
21
+ readonly name: string;
22
+ readonly arguments: string;
23
+ readonly result: string;
24
+ };
25
+ }>;
26
+ }
@@ -0,0 +1,74 @@
1
+ import { v4 } from 'uuid';
2
+
3
+ class ChatBotAdapter {
4
+ mindset;
5
+ constructor(mindset) {
6
+ this.mindset = mindset;
7
+ }
8
+ generateNextChatItem(chatItems) {
9
+ throw new Error('Not implemented');
10
+ }
11
+ async systemPrompt() {
12
+ let [identity, skills, limits] = await Promise.all([
13
+ this.mindset.identity(),
14
+ this.mindset.skills(),
15
+ this.mindset.limits(),
16
+ ]);
17
+ const language = identity.language.replaceAll('#', ' ');
18
+ const name = identity.name.replaceAll('#', ' ');
19
+ const age = identity.age ? identity.age.toString().replaceAll('#', ' ') : null;
20
+ const personality = identity.personality ? identity.personality.replaceAll('#', ' ') : null;
21
+ skills = skills.replaceAll('#', ' ');
22
+ limits = limits.replaceAll('#', ' ');
23
+ const systemPrompt = `
24
+ # System Instructions
25
+ you should act as a assistant.
26
+ your main language is ${language}.
27
+ your name is ${name}.
28
+ ${age ? 'you are ' + age + ' years old.' : ''}
29
+
30
+ ${personality ? '## Personality (in your main language) \n' + personality : ''}
31
+
32
+ ## Skills (in your main language)
33
+ ${skills}
34
+
35
+ ## System limitations (in your main language)
36
+ ${limits}
37
+
38
+ ## Chat memory
39
+ Next you will receive a chat history,
40
+ you should use this information to answer the user.
41
+ `;
42
+ return systemPrompt;
43
+ }
44
+ async buildBotMessageItem(text) {
45
+ const shortName = (await this.mindset.identity()).name;
46
+ const newBotMessage = {
47
+ id: v4(),
48
+ createdAt: new Date(),
49
+ type: 'BOT_MESSAGE',
50
+ content: {
51
+ sender: { shortName },
52
+ text,
53
+ },
54
+ };
55
+ return newBotMessage;
56
+ }
57
+ async buildFunctionCallItem(foreignId, functionName, functionArguments) {
58
+ const functionResult = await this.mindset.callFunction(functionName, functionArguments);
59
+ const newFunctionCall = {
60
+ id: v4(),
61
+ createdAt: new Date(),
62
+ type: 'FUNCTION_CALL',
63
+ content: {
64
+ foreignId,
65
+ name: functionName,
66
+ arguments: functionArguments,
67
+ result: functionResult,
68
+ },
69
+ };
70
+ return newFunctionCall;
71
+ }
72
+ }
73
+
74
+ export { ChatBotAdapter };
@@ -0,0 +1,4 @@
1
+ import { IChatMessage } from '@/message';
2
+ export interface IChatBot {
3
+ sendMessage(message: IChatMessage, callback: (message: IChatMessage) => void): void | Promise<void>;
4
+ }
@@ -0,0 +1,23 @@
1
+ export type IChatType = 'PRIVATE' | 'GROUP';
2
+ export interface IChatConnection {
3
+ channelType: string;
4
+ chatId: string;
5
+ }
6
+ export interface IChatData {
7
+ id?: string;
8
+ createdAt?: Date;
9
+ type: IChatType;
10
+ connections: IChatConnection[];
11
+ }
12
+ export declare class Chat {
13
+ private data;
14
+ constructor(data: IChatData);
15
+ isPrivate(): boolean;
16
+ isGroup(): boolean;
17
+ hasConnection(connection: IChatConnection): boolean;
18
+ private validatePrivateChat;
19
+ private validateGroupChat;
20
+ getId(): string;
21
+ wasCreated(): boolean;
22
+ validate(): void;
23
+ }
@@ -0,0 +1,55 @@
1
+ class Chat {
2
+ data;
3
+ constructor(data) {
4
+ this.data = data;
5
+ }
6
+ isPrivate() {
7
+ return this.data.type === 'PRIVATE';
8
+ }
9
+ isGroup() {
10
+ return this.data.type === 'GROUP';
11
+ }
12
+ hasConnection(connection) {
13
+ for (const con of this.data.connections) {
14
+ if (con.channelType === connection.channelType && con.chatId === connection.chatId) {
15
+ return true;
16
+ }
17
+ }
18
+ return false;
19
+ }
20
+ validatePrivateChat() {
21
+ if (this.data.connections.length < 1) {
22
+ throw new Error('PRIVATE chat should have one or more connections');
23
+ }
24
+ }
25
+ validateGroupChat() {
26
+ if (this.data.connections.length != 1) {
27
+ throw new Error('GROUP chat should have exactly one connection');
28
+ }
29
+ }
30
+ getId() {
31
+ if (!this.data.id) {
32
+ throw new Error('Chat ID is required');
33
+ }
34
+ return this.data.id;
35
+ }
36
+ wasCreated() {
37
+ return !!this.data.createdAt || !!this.data.id;
38
+ }
39
+ validate() {
40
+ if (!this.data.id) {
41
+ throw new Error('Chat ID is required');
42
+ }
43
+ if (!this.data.createdAt) {
44
+ throw new Error('Chat createdAt is required');
45
+ }
46
+ if (this.data.type === 'PRIVATE') {
47
+ this.validatePrivateChat();
48
+ }
49
+ else if (this.data.type === 'GROUP') {
50
+ this.validateGroupChat();
51
+ }
52
+ }
53
+ }
54
+
55
+ export { Chat };
@@ -0,0 +1,6 @@
1
+ export interface IChatFunctionCall {
2
+ foreignId: string;
3
+ name: string;
4
+ arguments?: string;
5
+ result: string;
6
+ }
@@ -0,0 +1,19 @@
1
+ import { IChatMessage } from '../../message/IChatMessage';
2
+ import { IChatFunctionCall } from './IChatFunctionCall';
3
+ export type ISystemMessageItem = {
4
+ type: 'BOT_MESSAGE';
5
+ content: IChatMessage;
6
+ };
7
+ export type IUserMessageItem = {
8
+ type: 'USER_MESSAGE';
9
+ content: IChatMessage;
10
+ };
11
+ export type ISystemFunctionCallItem = {
12
+ type: 'FUNCTION_CALL';
13
+ content: IChatFunctionCall;
14
+ };
15
+ export type IChatItem = {
16
+ id: string;
17
+ createdAt: Date;
18
+ } & (ISystemMessageItem | IUserMessageItem | ISystemFunctionCallItem);
19
+ export type IChatItemType = IChatItem['type'];
@@ -0,0 +1,4 @@
1
+ export * from './repository';
2
+ export * from './Chat';
3
+ export * from './IChatFunctionCall';
4
+ export * from './IChatItem';
@@ -0,0 +1,9 @@
1
+ import { IChatItem } from '../IChatItem';
2
+ export interface IChatMemory {
3
+ findLastItems(count: number): Promise<IChatItem[]>;
4
+ saveItem(item: IChatItem): Promise<void>;
5
+ }
6
+ export declare class ChatMemory implements IChatMemory {
7
+ findLastItems(count: number): Promise<IChatItem[]>;
8
+ saveItem(item: IChatItem): Promise<void>;
9
+ }
@@ -0,0 +1,10 @@
1
+ class ChatMemory {
2
+ findLastItems(count) {
3
+ throw new Error('Method not implemented.');
4
+ }
5
+ saveItem(item) {
6
+ throw new Error('Method not implemented.');
7
+ }
8
+ }
9
+
10
+ export { ChatMemory };
@@ -0,0 +1,12 @@
1
+ import { Chat, IChatConnection } from '../Chat';
2
+ import { IChatMemory } from './IChatMemory';
3
+ export interface IChatRepository {
4
+ create(chat: Chat): Promise<void>;
5
+ findByConnection(query: IChatConnection): Promise<Chat | null>;
6
+ findMemory(chatId: string): Promise<IChatMemory | null>;
7
+ }
8
+ export declare class ChatRepository implements IChatRepository {
9
+ create(chat: Chat): Promise<void>;
10
+ findByConnection(query: IChatConnection): Promise<Chat | null>;
11
+ findMemory(chatId: string): Promise<IChatMemory | null>;
12
+ }