@wabot-dev/framework 0.1.0-beta.9 → 0.2.0-beta.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.
- package/dist/src/addon/async/pg/PgJobRepository.js +26 -0
- package/dist/src/addon/auth/api-key/@apiKeyConnectionGuard.js +16 -0
- package/dist/src/addon/auth/api-key/@apiKeyGuard.js +17 -0
- package/dist/src/addon/auth/api-key/ApiKey.js +45 -0
- package/dist/src/addon/auth/api-key/ApiKeyConnectionGuardMiddleware.js +57 -0
- package/dist/src/addon/auth/api-key/ApiKeyGuardMiddleware.js +45 -0
- package/dist/src/addon/auth/api-key/ApiKeyRepository.js +22 -0
- package/dist/src/addon/auth/api-key/PgApiKeyRepository.js +53 -0
- package/dist/src/addon/auth/api-key/RemoteApiKeyRepository.js +62 -0
- package/dist/src/addon/auth/jwt/@jwtConnectionGuard.js +16 -0
- package/dist/src/addon/auth/jwt/@jwtGuard.js +17 -0
- package/dist/src/addon/auth/jwt/Jwt.js +53 -0
- package/dist/src/addon/auth/jwt/JwtAccessAndRefreshTokenDto.js +20 -0
- package/dist/src/addon/auth/jwt/JwtConfig.js +28 -0
- package/dist/src/addon/auth/jwt/JwtConnectionGuardMiddleware.js +57 -0
- package/dist/src/addon/auth/jwt/JwtGuardMiddleware.js +45 -0
- package/dist/src/addon/auth/jwt/JwtRefreshToken.js +56 -0
- package/dist/src/addon/auth/jwt/JwtRefreshTokenRepository.js +25 -0
- package/dist/src/addon/auth/jwt/JwtSigner.js +36 -0
- package/dist/src/addon/auth/jwt/JwtTokenDto.js +22 -0
- package/dist/src/addon/auth/jwt/PgJwtRefreshTokenRepository.js +21 -0
- package/dist/src/addon/chat-bot/anthropic/AnthropicChatAdapter.js +135 -0
- package/dist/src/addon/chat-bot/deepseek/DeepSeekChatAdapter.js +137 -0
- package/dist/src/addon/chat-bot/google/GoogleChatAdapter.js +128 -0
- package/dist/src/addon/chat-bot/openia/OpenaiChatAdapter.js +117 -0
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatMemory.js +6 -4
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatRepository.js +6 -5
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatRepository.js +2 -2
- package/dist/src/addon/chat-bot/wabot/WabotChatAdapter.js +41 -0
- package/dist/src/addon/chat-controller/cmd/@cmd.js +24 -0
- package/dist/src/addon/chat-controller/cmd/CmdChannel.js +91 -0
- package/dist/src/{channels → addon/chat-controller}/socket/@socket.js +10 -4
- package/dist/src/{channels → addon/chat-controller}/socket/SocketChannel.js +9 -23
- package/dist/src/{channels → addon/chat-controller}/socket/SocketChannelConfig.js +1 -1
- package/dist/src/{channels → addon/chat-controller}/telegram/@telegram.js +10 -4
- package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannel.js +4 -22
- package/dist/src/addon/chat-controller/whatsapp/@whatsApp.js +26 -0
- package/dist/src/{channels → addon/chat-controller}/whatsapp/EnvWhatsAppRepository.js +3 -3
- package/dist/src/{channels → addon/chat-controller}/whatsapp/PgWhatsAppRepository.js +2 -2
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsApp.js +2 -4
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannel.js +6 -18
- package/dist/src/addon/chat-controller/whatsapp/WhatsAppReceiver.js +10 -0
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSender.js +15 -39
- package/dist/src/addon/chat-controller/whatsapp/cloud-api/WhatsAppReceiverByCloudApi.js +97 -0
- package/dist/src/{channels/whatsapp → addon/chat-controller/whatsapp/cloud-api}/WhatsAppSenderByCloudApi.js +27 -20
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppProxyContracts.js +5 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppReceiverByWabotProxy.js +65 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppSenderByWabotProxy.js +61 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppWabotProxyConnection.js +45 -0
- package/dist/src/{pre-made/module → addon/mindset}/html/HtmlModule.js +7 -7
- package/dist/src/core/auth/Auth.js +33 -0
- package/dist/src/core/{Persistent.js → entity/Entity.js} +24 -12
- package/dist/src/core/env/Env.js +39 -0
- package/dist/src/core/error/CustomError.js +15 -0
- package/dist/src/core/injection/index.js +4 -0
- package/dist/src/core/mapper/Mapper.js +42 -0
- package/dist/src/core/password/Password.js +30 -0
- package/dist/src/core/random/Random.js +65 -0
- package/dist/src/core/storable/Storable.js +8 -0
- package/dist/src/core/validation/core/validateArray.js +51 -0
- package/dist/src/core/validation/core/validateModel.js +36 -0
- package/dist/src/{validation/metadata/@isNumber.js → core/validation/metadata/@isArray.js} +5 -4
- package/dist/src/{validation/metadata/@isDate.js → core/validation/metadata/@isModel.js} +5 -4
- package/dist/src/{validation → core/validation}/metadata/@isOptional.js +1 -1
- package/dist/src/core/validation/metadata/ValidationMetadataStore.js +98 -0
- package/dist/src/core/validation/modelInfo.js +9 -0
- package/dist/src/{validation/validateModel2.js → core/validation/validate.js} +3 -3
- package/dist/src/{validation/metadata → core/validation/validators/is-boolean}/@isBoolean.js +3 -3
- package/dist/src/core/validation/validators/is-date/@isDate.js +17 -0
- package/dist/src/core/validation/validators/is-in/@isIn.js +18 -0
- package/dist/src/core/validation/validators/is-in/validateIsIn.js +12 -0
- package/dist/src/{validation/metadata → core/validation/validators/is-not-empty}/@isNotEmpty.js +3 -3
- package/dist/src/core/validation/validators/is-number/@isNumber.js +17 -0
- package/dist/src/{validation/metadata → core/validation/validators/is-present}/@isPresent.js +3 -3
- package/dist/src/{validation/metadata → core/validation/validators/is-string}/@isString.js +3 -3
- package/dist/src/{validation/metadata → core/validation/validators/max}/@max.js +3 -3
- package/dist/src/{validation/metadata → core/validation/validators/min}/@min.js +3 -3
- package/dist/src/feature/async/@command.js +11 -0
- package/dist/src/feature/async/@commandHandler.js +12 -0
- package/dist/src/feature/async/Async.js +38 -0
- package/dist/src/feature/async/Command.js +9 -0
- package/dist/src/feature/async/CommandMetadataStore.js +38 -0
- package/dist/src/feature/async/Job.js +27 -0
- package/dist/src/feature/async/JobRepository.js +31 -0
- package/dist/src/feature/async/JobRunner.js +48 -0
- package/dist/src/feature/async/JobsEventsHub.js +36 -0
- package/dist/src/feature/async/runCommandHandlers.js +29 -0
- package/dist/src/{core/chat → feature/chat-bot}/Chat.js +2 -2
- package/dist/src/feature/chat-bot/ChatAdapter.js +7 -0
- package/dist/src/feature/chat-bot/ChatBot.js +73 -0
- package/dist/src/feature/chat-bot/ChatItem.js +24 -0
- package/dist/src/feature/chat-bot/ChatMemory.js +10 -0
- package/dist/src/{core/chat/repository/IChatRepository.js → feature/chat-bot/ChatRepository.js} +2 -8
- package/dist/src/feature/chat-bot/IChatItem.js +3 -0
- package/dist/src/{chatbot → feature/chat-bot}/metadata/@chatBot.js +1 -1
- package/dist/src/{chatbot → feature/chat-bot}/metadata/ChatBotMetadataStore.js +1 -1
- package/dist/src/{controller/channel → feature/chat-controller}/ChatResolver.js +6 -4
- package/dist/src/{controller → feature/chat-controller}/metadata/ControllerMetadataStore.js +1 -1
- package/dist/src/{controller → feature/chat-controller}/metadata/controller/@chatController.js +1 -1
- package/dist/src/feature/chat-controller/runChatControllers.js +83 -0
- package/dist/src/{channels → feature}/express/ExpressProvider.js +2 -4
- package/dist/src/{channels → feature}/http/HttpServerProvider.js +2 -2
- package/dist/src/{mindset → feature/mindset}/IMindset.js +6 -0
- package/dist/src/feature/mindset/MindsetOperator.js +180 -0
- package/dist/src/{mindset → feature/mindset}/metadata/MindsetMetadataStore.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/functions/@mindsetFunction.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/mindsets/@mindset.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/modules/@mindsetModule.js +1 -2
- package/dist/src/{mindset → feature/mindset}/metadata/params/@param.js +1 -1
- package/dist/src/feature/money/Money.js +61 -0
- package/dist/src/feature/money/MoneyDto.js +22 -0
- package/dist/src/{repository → feature}/pg/PgCrudRepository.js +24 -10
- package/dist/src/{repository → feature}/pg/PgRepositoryBase.js +2 -2
- package/dist/src/feature/rest-controller/injection-tokens.js +4 -0
- package/dist/src/{rest-controller/metadata/@post.js → feature/rest-controller/metadata/@middleware.js} +5 -8
- package/dist/src/feature/rest-controller/metadata/@onDelete.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onGet.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onPost.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onPut.js +7 -0
- package/dist/src/{rest-controller → feature/rest-controller}/metadata/@restController.js +2 -2
- package/dist/src/{rest-controller → feature/rest-controller}/metadata/RestControllerMetadataStore.js +14 -1
- package/dist/src/{rest-controller/metadata/@get.js → feature/rest-controller/metadata/methodDecorator.js} +5 -5
- package/dist/src/feature/rest-controller/runRestControllers.js +103 -0
- package/dist/src/{channels → feature}/socket/SocketServerProvider.js +2 -2
- package/dist/src/feature/socket-controller/metadata/@connectionMiddleware.js +16 -0
- package/dist/src/feature/socket-controller/metadata/@socketConnection.js +18 -0
- package/dist/src/feature/socket-controller/metadata/@socketController.js +15 -0
- package/dist/src/feature/socket-controller/metadata/@socketEvent.js +18 -0
- package/dist/src/feature/socket-controller/metadata/SocketControllerMetadataStore.js +65 -0
- package/dist/src/feature/socket-controller/runSocketControllers.js +99 -0
- package/dist/src/index.d.ts +1219 -718
- package/dist/src/index.js +147 -102
- package/package.json +8 -2
- package/dist/src/_virtual/_commonjsHelpers.js +0 -5
- package/dist/src/_virtual/cjs.js +0 -7
- package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +0 -107
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +0 -88
- package/dist/src/channels/cmd/@cmd.js +0 -18
- package/dist/src/channels/cmd/CmdChannel.js +0 -73
- package/dist/src/channels/wabot/WabotDevConnection.js +0 -57
- package/dist/src/channels/wabot/WabotDevSocketContracts.js +0 -10
- package/dist/src/channels/whatsapp/@whatsapp.js +0 -20
- package/dist/src/channels/whatsapp/WhatsAppReceiver.js +0 -59
- package/dist/src/channels/whatsapp/WhatsAppReceiverByDevConnection.js +0 -32
- package/dist/src/channels/whatsapp/WhatsAppReceiverByWebHook.js +0 -63
- package/dist/src/channels/whatsapp/WhatsAppSenderByDevConnection.js +0 -61
- package/dist/src/chatbot/ChatBot.js +0 -51
- package/dist/src/chatbot/ChatBotAdapter.js +0 -72
- package/dist/src/controller/channel/UserResolver.js +0 -21
- package/dist/src/core/IMessageContext.js +0 -12
- package/dist/src/core/chat/ChatItem.js +0 -15
- package/dist/src/core/chat/repository/IChatMemory.js +0 -10
- package/dist/src/core/user/IUserRepository.js +0 -19
- package/dist/src/core/user/User.js +0 -26
- package/dist/src/env/WabotEnv.js +0 -27
- package/dist/src/injection/index.js +0 -4
- package/dist/src/mindset/MindsetOperator.js +0 -101
- package/dist/src/node_modules/@selderee/plugin-htmlparser2/lib/hp2-builder.js +0 -90
- package/dist/src/node_modules/deepmerge/dist/cjs.js +0 -142
- package/dist/src/node_modules/dom-serializer/lib/esm/foreignNames.js +0 -102
- package/dist/src/node_modules/dom-serializer/lib/esm/index.js +0 -186
- package/dist/src/node_modules/domelementtype/lib/esm/index.js +0 -53
- package/dist/src/node_modules/domhandler/lib/esm/index.js +0 -148
- package/dist/src/node_modules/domhandler/lib/esm/node.js +0 -334
- package/dist/src/node_modules/entities/lib/esm/decode.js +0 -458
- package/dist/src/node_modules/entities/lib/esm/decode_codepoint.js +0 -62
- package/dist/src/node_modules/entities/lib/esm/escape.js +0 -99
- package/dist/src/node_modules/entities/lib/esm/generated/decode-data-html.js +0 -8
- package/dist/src/node_modules/entities/lib/esm/generated/decode-data-xml.js +0 -8
- package/dist/src/node_modules/html-to-text/lib/html-to-text.js +0 -2147
- package/dist/src/node_modules/htmlparser2/lib/esm/Parser.js +0 -491
- package/dist/src/node_modules/htmlparser2/lib/esm/Tokenizer.js +0 -928
- package/dist/src/node_modules/htmlparser2/lib/esm/index.js +0 -18
- package/dist/src/node_modules/leac/lib/leac.js +0 -3
- package/dist/src/node_modules/parseley/lib/parseley.js +0 -270
- package/dist/src/node_modules/peberminta/lib/core.js +0 -171
- package/dist/src/node_modules/selderee/lib/selderee.js +0 -380
- package/dist/src/pre-made/module/authentication/AuthenticationModule.js +0 -97
- package/dist/src/pre-made/module/authentication/requests/SendOneTimePasswordRequest.js +0 -25
- package/dist/src/pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js +0 -25
- package/dist/src/pre-made/module/register-user/RegisterUserModule.js +0 -56
- package/dist/src/pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js +0 -25
- package/dist/src/pre-made/repository/user/pg/PgUserRepository.js +0 -33
- package/dist/src/pre-made/repository/user/ram/RamUserRepository.js +0 -27
- package/dist/src/pre-made/service/EmailService.js +0 -13
- package/dist/src/pre-made/service/OtpService.js +0 -14
- package/dist/src/rest-controller/runRestControllers.js +0 -74
- package/dist/src/server/prepareChatContainer.js +0 -43
- package/dist/src/server/runChannel.js +0 -27
- package/dist/src/server/runServer.js +0 -40
- package/dist/src/validation/metadata/@validable.js +0 -14
- package/dist/src/validation/metadata/ValidationMetadataStore.js +0 -55
- package/dist/src/validation/validators/validateModel.js +0 -47
- /package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatMemory.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannelConfig.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannelConfig.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppRepository.js +0 -0
- /package/dist/src/{injection → core/injection}/Container.js +0 -0
- /package/dist/src/{logger → core/logger}/Logger.js +0 -0
- /package/dist/src/{validation/validators → core/validation/core}/validateIsOptional.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-boolean}/validateIsBoolean.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-date}/validateIsDate.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-not-empty}/validateIsNotEmpty.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-number}/validateIsNumber.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-present}/validateIsPresent.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-string}/validateIsString.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/max}/validateMax.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/min}/validateMin.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/functions/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/mindsets/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/modules/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/params/decoratorNames.js +0 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import 'reflect-metadata';
|
|
3
|
+
import { injectable } from '../../core/injection/index.js';
|
|
4
|
+
import '../mindset/metadata/MindsetMetadataStore.js';
|
|
5
|
+
import { MindsetOperator } from '../mindset/MindsetOperator.js';
|
|
6
|
+
import { ChatAdapter } from './ChatAdapter.js';
|
|
7
|
+
import { ChatItem } from './ChatItem.js';
|
|
8
|
+
import { ChatMemory } from './ChatMemory.js';
|
|
9
|
+
|
|
10
|
+
let ChatBot = class ChatBot {
|
|
11
|
+
memory;
|
|
12
|
+
adapter;
|
|
13
|
+
mindset;
|
|
14
|
+
constructor(memory, adapter, mindset) {
|
|
15
|
+
this.memory = memory;
|
|
16
|
+
this.adapter = adapter;
|
|
17
|
+
this.mindset = mindset;
|
|
18
|
+
}
|
|
19
|
+
async sendMessage(message, callback) {
|
|
20
|
+
const newChatItem = new ChatItem({
|
|
21
|
+
type: 'humanMessage',
|
|
22
|
+
humanMessage: message,
|
|
23
|
+
});
|
|
24
|
+
await this.memory.create(newChatItem);
|
|
25
|
+
this.processLoop(callback);
|
|
26
|
+
}
|
|
27
|
+
async processLoop(callback) {
|
|
28
|
+
const prevItems = await this.memory.findLastItems(10);
|
|
29
|
+
if (prevItems.length === 0) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const lastChatItem = prevItems[prevItems.length - 1];
|
|
33
|
+
if (lastChatItem.type === 'botMessage') {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const systemPrompt = await this.mindset.systemPrompt();
|
|
37
|
+
const tools = this.mindset.tools();
|
|
38
|
+
const identity = await this.mindset.identity();
|
|
39
|
+
const llms = await this.mindset.llms();
|
|
40
|
+
if (llms.length === 0) {
|
|
41
|
+
throw new Error(`Invalid ${this.mindset.constructor.name} - llms not found`);
|
|
42
|
+
}
|
|
43
|
+
const llm = llms[0];
|
|
44
|
+
const { chatItem: newItemData } = await this.adapter.nextItem({
|
|
45
|
+
model: llm.model,
|
|
46
|
+
provider: llm.provider,
|
|
47
|
+
systemPrompt,
|
|
48
|
+
tools,
|
|
49
|
+
prevItems: prevItems.map((x) => x.getData()),
|
|
50
|
+
});
|
|
51
|
+
if (newItemData.type === 'functionCall') {
|
|
52
|
+
newItemData.functionCall.result = await this.mindset.callFunction(newItemData.functionCall.name, newItemData.functionCall.arguments ?? '{}');
|
|
53
|
+
}
|
|
54
|
+
else if (newItemData.type === 'botMessage') {
|
|
55
|
+
newItemData.botMessage.senderName = identity.name;
|
|
56
|
+
}
|
|
57
|
+
const newChatItem = new ChatItem(newItemData);
|
|
58
|
+
await this.memory.create(newChatItem);
|
|
59
|
+
if (newChatItem.type === 'botMessage') {
|
|
60
|
+
callback(newChatItem.botMessage);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
this.processLoop(callback);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
ChatBot = __decorate([
|
|
67
|
+
injectable(),
|
|
68
|
+
__metadata("design:paramtypes", [ChatMemory,
|
|
69
|
+
ChatAdapter,
|
|
70
|
+
MindsetOperator])
|
|
71
|
+
], ChatBot);
|
|
72
|
+
|
|
73
|
+
export { ChatBot };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Entity } from '../../core/entity/Entity.js';
|
|
2
|
+
|
|
3
|
+
class ChatItem extends Entity {
|
|
4
|
+
get type() {
|
|
5
|
+
return this.data.type;
|
|
6
|
+
}
|
|
7
|
+
get botMessage() {
|
|
8
|
+
return this.data.botMessage;
|
|
9
|
+
}
|
|
10
|
+
get functionCall() {
|
|
11
|
+
return this.data.functionCall;
|
|
12
|
+
}
|
|
13
|
+
setFunctionResult(result) {
|
|
14
|
+
if (this.data.type !== 'functionCall') {
|
|
15
|
+
throw new Error('The chat item is not functionCall type');
|
|
16
|
+
}
|
|
17
|
+
this.data.functionCall.result = result;
|
|
18
|
+
}
|
|
19
|
+
getData() {
|
|
20
|
+
return this.data;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { ChatItem };
|
package/dist/src/{core/chat/repository/IChatRepository.js → feature/chat-bot/ChatRepository.js}
RENAMED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import { singleton } from '../../../injection/index.js';
|
|
3
|
-
|
|
4
|
-
let ChatRepository = class ChatRepository {
|
|
1
|
+
class ChatRepository {
|
|
5
2
|
create(chat) {
|
|
6
3
|
throw new Error('Method not implemented.');
|
|
7
4
|
}
|
|
@@ -11,9 +8,6 @@ let ChatRepository = class ChatRepository {
|
|
|
11
8
|
findMemory(chatId) {
|
|
12
9
|
throw new Error('Method not implemented.');
|
|
13
10
|
}
|
|
14
|
-
}
|
|
15
|
-
ChatRepository = __decorate([
|
|
16
|
-
singleton()
|
|
17
|
-
], ChatRepository);
|
|
11
|
+
}
|
|
18
12
|
|
|
19
13
|
export { ChatRepository };
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import '
|
|
5
|
-
import { injectable } from '../../injection/index.js';
|
|
2
|
+
import { Chat } from '../chat-bot/Chat.js';
|
|
3
|
+
import '../chat-bot/ChatBot.js';
|
|
4
|
+
import { ChatRepository } from '../chat-bot/ChatRepository.js';
|
|
5
|
+
import { injectable } from '../../core/injection/index.js';
|
|
6
|
+
import 'uuid';
|
|
7
|
+
import '../chat-bot/metadata/ChatBotMetadataStore.js';
|
|
6
8
|
|
|
7
9
|
let ChatResolver = class ChatResolver {
|
|
8
10
|
chatRepository;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Auth } from '../../core/auth/Auth.js';
|
|
2
|
+
import { container } from '../../core/injection/index.js';
|
|
3
|
+
import { Chat } from '../chat-bot/Chat.js';
|
|
4
|
+
import { ChatBot } from '../chat-bot/ChatBot.js';
|
|
5
|
+
import { ChatMemory } from '../chat-bot/ChatMemory.js';
|
|
6
|
+
import { ChatRepository } from '../chat-bot/ChatRepository.js';
|
|
7
|
+
import 'uuid';
|
|
8
|
+
import { ChatBotMetadataStore } from '../chat-bot/metadata/ChatBotMetadataStore.js';
|
|
9
|
+
import 'reflect-metadata';
|
|
10
|
+
import '../mindset/metadata/MindsetMetadataStore.js';
|
|
11
|
+
import { Mindset } from '../mindset/IMindset.js';
|
|
12
|
+
import '../mindset/MindsetOperator.js';
|
|
13
|
+
import { ChatResolver } from './ChatResolver.js';
|
|
14
|
+
import { ControllerMetadataStore } from './metadata/ControllerMetadataStore.js';
|
|
15
|
+
import { Container } from '../../core/injection/Container.js';
|
|
16
|
+
|
|
17
|
+
async function prepareChatContainer(container, messageContext, mindsetCtor) {
|
|
18
|
+
const chatContainer = container.createChildContainer();
|
|
19
|
+
chatContainer.register(Container, { useValue: chatContainer });
|
|
20
|
+
chatContainer.registerInstance(Chat, messageContext.chat);
|
|
21
|
+
const chatRepository = container.resolve(ChatRepository);
|
|
22
|
+
const chatMemory = await chatRepository.findMemory(messageContext.chat.id);
|
|
23
|
+
if (!chatMemory) {
|
|
24
|
+
throw new Error('Not found Chat Memory for Chat with Id=' + messageContext.chat.id);
|
|
25
|
+
}
|
|
26
|
+
chatContainer.registerInstance(ChatMemory, chatMemory);
|
|
27
|
+
if (messageContext.authInfo) {
|
|
28
|
+
const auth = chatContainer.resolve(Auth);
|
|
29
|
+
auth.assign(messageContext.authInfo);
|
|
30
|
+
}
|
|
31
|
+
const chatBotMetadataStore = container.resolve(ChatBotMetadataStore);
|
|
32
|
+
const chatBots = chatBotMetadataStore.getChatBotsMetadata();
|
|
33
|
+
for (const chatBotMetadata of chatBots) {
|
|
34
|
+
chatContainer.beforeResolution(chatBotMetadata.constructor, (a, b) => {
|
|
35
|
+
const subContainer = chatContainer.createChildContainer();
|
|
36
|
+
subContainer.register(Mindset, { useClass: chatBotMetadata.mindsetConstructor });
|
|
37
|
+
const chatBot = subContainer.resolve(ChatBot);
|
|
38
|
+
chatContainer.register(chatBotMetadata.injectionToken, { useValue: chatBot });
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return chatContainer;
|
|
42
|
+
}
|
|
43
|
+
function runChatControllers(controllers) {
|
|
44
|
+
const metadataStore = container.resolve(ControllerMetadataStore);
|
|
45
|
+
const chatResolver = container.resolve(ChatResolver);
|
|
46
|
+
for (const controllerCtor of controllers) {
|
|
47
|
+
const chatControllerMetadata = metadataStore.getChatControllerMetadata(controllerCtor);
|
|
48
|
+
if (!chatControllerMetadata) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
for (const channelMetadata of chatControllerMetadata.channels) {
|
|
52
|
+
const channelContainer = container.createChildContainer();
|
|
53
|
+
if (channelMetadata.channelConfig) {
|
|
54
|
+
channelContainer.register(channelMetadata.channelConfig.constructor, {
|
|
55
|
+
useValue: channelMetadata.channelConfig,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const channel = channelContainer.resolve(channelMetadata.channelConstructor);
|
|
59
|
+
channel.listen(async (channelMessage) => {
|
|
60
|
+
const chat = await chatResolver.resolve(channelMessage.chatConnection);
|
|
61
|
+
const chatContainer = await prepareChatContainer(channelContainer, {
|
|
62
|
+
chat,
|
|
63
|
+
...channelMessage,
|
|
64
|
+
});
|
|
65
|
+
const chatController = chatContainer.resolve(channelMetadata.controllerConstructor);
|
|
66
|
+
const receivedMessage = {
|
|
67
|
+
message: channelMessage.message,
|
|
68
|
+
reply: (message) => {
|
|
69
|
+
channelMessage.reply(message);
|
|
70
|
+
if (channelMessage.setAuthInfo) {
|
|
71
|
+
const auth = chatContainer.resolve(Auth);
|
|
72
|
+
channelMessage.setAuthInfo(auth['authInfo'] || undefined);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
chatController[channelMetadata.functionName](receivedMessage);
|
|
77
|
+
});
|
|
78
|
+
channel.connect();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { runChatControllers };
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
2
|
import { HttpServerProvider } from '../http/HttpServerProvider.js';
|
|
3
3
|
import express from 'express';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import { Logger } from '../../logger/Logger.js';
|
|
4
|
+
import { Logger } from '../../core/logger/Logger.js';
|
|
5
|
+
import { singleton } from '../../core/injection/index.js';
|
|
7
6
|
|
|
8
7
|
let ExpressProvider = class ExpressProvider {
|
|
9
8
|
httpServerProvider;
|
|
@@ -23,7 +22,6 @@ let ExpressProvider = class ExpressProvider {
|
|
|
23
22
|
}
|
|
24
23
|
createExpress() {
|
|
25
24
|
const expressApp = express();
|
|
26
|
-
expressApp.use(bodyParser.json());
|
|
27
25
|
expressApp.use((req, res, next) => {
|
|
28
26
|
const start = process.hrtime();
|
|
29
27
|
res.on('finish', () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
|
-
import {
|
|
2
|
+
import { singleton } from '../../core/injection/index.js';
|
|
3
|
+
import { Logger } from '../../core/logger/Logger.js';
|
|
3
4
|
import { Server } from 'http';
|
|
4
|
-
import { singleton } from 'tsyringe';
|
|
5
5
|
|
|
6
6
|
let HttpServerProvider = class HttpServerProvider {
|
|
7
7
|
server = null;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { injectable } from '../../core/injection/index.js';
|
|
3
|
+
import { Mindset } from './IMindset.js';
|
|
4
|
+
import { MindsetMetadataStore } from './metadata/MindsetMetadataStore.js';
|
|
5
|
+
import { Container } from '../../core/injection/Container.js';
|
|
6
|
+
|
|
7
|
+
let MindsetOperator = class MindsetOperator {
|
|
8
|
+
mindset;
|
|
9
|
+
container;
|
|
10
|
+
metadata;
|
|
11
|
+
constructor(mindset, container, metadataStore) {
|
|
12
|
+
this.mindset = mindset;
|
|
13
|
+
this.container = container;
|
|
14
|
+
this.metadata = metadataStore.getMindsetMetadata(this.mindset.constructor);
|
|
15
|
+
}
|
|
16
|
+
identity() {
|
|
17
|
+
return this.mindset.identity();
|
|
18
|
+
}
|
|
19
|
+
skills() {
|
|
20
|
+
return this.mindset.skills();
|
|
21
|
+
}
|
|
22
|
+
limits() {
|
|
23
|
+
return this.mindset.limits();
|
|
24
|
+
}
|
|
25
|
+
workflow() {
|
|
26
|
+
return this.mindset.workflow();
|
|
27
|
+
}
|
|
28
|
+
llms() {
|
|
29
|
+
return this.mindset.llms();
|
|
30
|
+
}
|
|
31
|
+
async systemPrompt() {
|
|
32
|
+
let [identity, skills, limits, workflow] = await Promise.all([
|
|
33
|
+
this.identity(),
|
|
34
|
+
this.skills(),
|
|
35
|
+
this.limits(),
|
|
36
|
+
this.workflow(),
|
|
37
|
+
]);
|
|
38
|
+
const language = identity.language.replaceAll('#', ' ');
|
|
39
|
+
const name = identity.name.replaceAll('#', ' ');
|
|
40
|
+
const age = identity.age ? identity.age.toString().replaceAll('#', ' ') : null;
|
|
41
|
+
const personality = identity.personality ? identity.personality.replaceAll('#', ' ') : null;
|
|
42
|
+
skills = skills.replaceAll('#', ' ');
|
|
43
|
+
limits = limits.replaceAll('#', ' ');
|
|
44
|
+
workflow = workflow.replaceAll('#', ' ');
|
|
45
|
+
const systemPrompt = `
|
|
46
|
+
# System Instructions
|
|
47
|
+
you should act as a assistant.
|
|
48
|
+
your main language is ${language}.
|
|
49
|
+
your name is ${name}.
|
|
50
|
+
${age ? 'you are ' + age + ' years old.' : ''}
|
|
51
|
+
|
|
52
|
+
${personality ? '## Personality (in your main language) \n' + personality : ''}
|
|
53
|
+
|
|
54
|
+
## Skills (in your main language)
|
|
55
|
+
${skills}
|
|
56
|
+
|
|
57
|
+
## Workflow (in your main language)
|
|
58
|
+
${workflow}
|
|
59
|
+
|
|
60
|
+
## System limitations (in your main language)
|
|
61
|
+
${limits}
|
|
62
|
+
|
|
63
|
+
## Chat memory
|
|
64
|
+
Next you will receive a chat history,
|
|
65
|
+
you should use this information to answer the user.
|
|
66
|
+
`;
|
|
67
|
+
return systemPrompt;
|
|
68
|
+
}
|
|
69
|
+
tools() {
|
|
70
|
+
return this.metadata.modules
|
|
71
|
+
.map((module) => module.functions.map((fn) => this.tool(fn, module)))
|
|
72
|
+
.flat();
|
|
73
|
+
}
|
|
74
|
+
tool(fn, module) {
|
|
75
|
+
const description = fn.config.description.replaceAll('#', ' ');
|
|
76
|
+
return {
|
|
77
|
+
language: module.config.language ?? 'english',
|
|
78
|
+
name: fn.name,
|
|
79
|
+
description,
|
|
80
|
+
parameters: fn.params.map((param) => this.toolParameter(param)),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
toolParameter(param) {
|
|
84
|
+
let description = `
|
|
85
|
+
### description (in your main language)
|
|
86
|
+
${param.config.description.replaceAll('#', ' ')}
|
|
87
|
+
`;
|
|
88
|
+
const type = (() => {
|
|
89
|
+
if (param.type === Number)
|
|
90
|
+
return 'number';
|
|
91
|
+
if (param.type === String)
|
|
92
|
+
return 'string';
|
|
93
|
+
if (param.type === Date) {
|
|
94
|
+
description = `${description}
|
|
95
|
+
### format: ISO 8681 - YYYY-MM-DDTHH:mm:ssZ
|
|
96
|
+
`;
|
|
97
|
+
return 'string';
|
|
98
|
+
}
|
|
99
|
+
throw new Error(`Unsupported type`);
|
|
100
|
+
})();
|
|
101
|
+
return {
|
|
102
|
+
type,
|
|
103
|
+
name: param.name,
|
|
104
|
+
description,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async callFunction(name, params) {
|
|
108
|
+
const fnMetadata = this.metadata.modules
|
|
109
|
+
.map((module) => module.functions)
|
|
110
|
+
.flat()
|
|
111
|
+
.find((fn) => fn.name === name);
|
|
112
|
+
if (!fnMetadata) {
|
|
113
|
+
throw new Error(`Function ${name} not found`);
|
|
114
|
+
}
|
|
115
|
+
const paramsObj = JSON.parse(params);
|
|
116
|
+
const module = this.container.resolve(fnMetadata.moduleConstructor);
|
|
117
|
+
try {
|
|
118
|
+
const response = await module[name](paramsObj);
|
|
119
|
+
if (!response) {
|
|
120
|
+
return 'success';
|
|
121
|
+
}
|
|
122
|
+
return await this.functionResponseToString(response);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
return await this.functionErrorToString(error);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async functionResponseToString(response) {
|
|
129
|
+
if (response instanceof Response) {
|
|
130
|
+
const contentType = response.headers.get('Content-Type') || '';
|
|
131
|
+
let body;
|
|
132
|
+
try {
|
|
133
|
+
body = contentType.includes('application/json')
|
|
134
|
+
? await response.json()
|
|
135
|
+
: await response.text();
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
body = { message: response.ok ? 'OK' : 'Unable to parse error body' };
|
|
139
|
+
}
|
|
140
|
+
return JSON.stringify({
|
|
141
|
+
httpCode: response.status,
|
|
142
|
+
body: typeof body === 'object' ? body : { message: body },
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
const type = typeof response;
|
|
146
|
+
if (type === 'string') {
|
|
147
|
+
return response;
|
|
148
|
+
}
|
|
149
|
+
else if (type === 'boolean' || type === 'number') {
|
|
150
|
+
return `${response}`;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
return JSON.stringify(response);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async functionErrorToString(error) {
|
|
157
|
+
if (error instanceof Response) {
|
|
158
|
+
return await this.functionResponseToString(error);
|
|
159
|
+
}
|
|
160
|
+
if (error?.response && typeof error.response === 'object' && error.response.status) {
|
|
161
|
+
const { status, data } = error.response;
|
|
162
|
+
return JSON.stringify({
|
|
163
|
+
httpCode: status,
|
|
164
|
+
body: typeof data === 'object' ? data : { message: data?.toString?.() || 'Unknown error' },
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
if (error?.message) {
|
|
168
|
+
return error.message;
|
|
169
|
+
}
|
|
170
|
+
return 'Unknown error occurred';
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
MindsetOperator = __decorate([
|
|
174
|
+
injectable(),
|
|
175
|
+
__metadata("design:paramtypes", [Mindset,
|
|
176
|
+
Container,
|
|
177
|
+
MindsetMetadataStore])
|
|
178
|
+
], MindsetOperator);
|
|
179
|
+
|
|
180
|
+
export { MindsetOperator };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
|
-
import { singleton } from '
|
|
2
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
3
3
|
import { MINDSET_FUNCTION_DECORATION_FUNCTION } from './functions/decoratorNames.js';
|
|
4
4
|
import { MINDSET_DECORATION_MINDSET } from './mindsets/decoratorNames.js';
|
|
5
5
|
import { MINDSET_MODULE_DECORATION_MODULE } from './modules/decoratorNames.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import { container } from '
|
|
2
|
+
import { container } from '../../../../core/injection/index.js';
|
|
3
3
|
import { MindsetMetadataStore } from '../MindsetMetadataStore.js';
|
|
4
4
|
import { MINDSET_FUNCTION_DECORATION_FUNCTION } from './decoratorNames.js';
|
|
5
5
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import { container, injectable } from '
|
|
2
|
+
import { container, injectable } from '../../../../core/injection/index.js';
|
|
3
3
|
import { MindsetMetadataStore } from '../MindsetMetadataStore.js';
|
|
4
4
|
import { MINDSET_DECORATION_MINDSET } from './decoratorNames.js';
|
|
5
5
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import { container, injectable } from '../../../injection/index.js';
|
|
1
|
+
import { container, injectable } from '../../../../core/injection/index.js';
|
|
3
2
|
import { MindsetMetadataStore } from '../MindsetMetadataStore.js';
|
|
4
3
|
import { MINDSET_MODULE_DECORATION_MODULE } from './decoratorNames.js';
|
|
5
4
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
import { MindsetMetadataStore } from '../MindsetMetadataStore.js';
|
|
3
|
-
import { container } from '
|
|
3
|
+
import { container } from '../../../../core/injection/index.js';
|
|
4
4
|
import { PARAM_DECORATION_PARAM } from './decoratorNames.js';
|
|
5
5
|
|
|
6
6
|
function param(config) {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Storable } from '../../core/storable/Storable.js';
|
|
2
|
+
import { Big } from 'big.js';
|
|
3
|
+
|
|
4
|
+
Big.strict = true;
|
|
5
|
+
class Money extends Storable {
|
|
6
|
+
getData() {
|
|
7
|
+
return { ...this.data };
|
|
8
|
+
}
|
|
9
|
+
get amount() {
|
|
10
|
+
return Big(this.data.amount);
|
|
11
|
+
}
|
|
12
|
+
get currency() {
|
|
13
|
+
return this.data.currency;
|
|
14
|
+
}
|
|
15
|
+
plus(money) {
|
|
16
|
+
this.validateEqualCurrencies(this.data.currency, money.data.currency);
|
|
17
|
+
return new Money({
|
|
18
|
+
amount: this.amount.plus(money.amount).toFixed(6),
|
|
19
|
+
currency: this.currency,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
minus(money) {
|
|
23
|
+
this.validateEqualCurrencies(this.data.currency, money.data.currency);
|
|
24
|
+
return new Money({
|
|
25
|
+
amount: this.amount.minus(money.amount).toFixed(6),
|
|
26
|
+
currency: this.currency,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
times(value) {
|
|
30
|
+
return new Money({
|
|
31
|
+
amount: this.amount.times(new Big(String(value))).toFixed(6),
|
|
32
|
+
currency: this.currency,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
div(value) {
|
|
36
|
+
return new Money({
|
|
37
|
+
amount: this.amount.div(new Big(String(value))).toFixed(6),
|
|
38
|
+
currency: this.currency,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
negative() {
|
|
42
|
+
return new Money({
|
|
43
|
+
amount: new Big('0').minus(this.amount).toFixed(6),
|
|
44
|
+
currency: this.currency,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
isGretterThan(money) {
|
|
48
|
+
this.validateEqualCurrencies(this.data.currency, money.data.currency);
|
|
49
|
+
return this.amount.gt(money.amount);
|
|
50
|
+
}
|
|
51
|
+
validateEqualCurrencies(left, right) {
|
|
52
|
+
if (left !== right) {
|
|
53
|
+
throw new Error(`Is not posible operate with diferent currencies: '${left}' and '${right}'`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static zero(currency) {
|
|
57
|
+
return new Money({ currency, amount: '0' });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { Money };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import '../../core/injection/index.js';
|
|
3
|
+
import '../../core/validation/metadata/ValidationMetadataStore.js';
|
|
4
|
+
import { isNotEmpty } from '../../core/validation/validators/is-not-empty/@isNotEmpty.js';
|
|
5
|
+
import { isString } from '../../core/validation/validators/is-string/@isString.js';
|
|
6
|
+
|
|
7
|
+
class MoneyDto {
|
|
8
|
+
amount = '';
|
|
9
|
+
currency = '';
|
|
10
|
+
}
|
|
11
|
+
__decorate([
|
|
12
|
+
isString(),
|
|
13
|
+
isNotEmpty(),
|
|
14
|
+
__metadata("design:type", String)
|
|
15
|
+
], MoneyDto.prototype, "amount", void 0);
|
|
16
|
+
__decorate([
|
|
17
|
+
isString(),
|
|
18
|
+
isNotEmpty(),
|
|
19
|
+
__metadata("design:type", String)
|
|
20
|
+
], MoneyDto.prototype, "currency", void 0);
|
|
21
|
+
|
|
22
|
+
export { MoneyDto };
|