@wabot-dev/framework 0.1.0-beta.6 → 0.1.0-beta.60
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 +53 -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 +35 -0
- package/dist/src/addon/auth/api-key/PgApiKeyRepository.js +28 -0
- package/dist/src/addon/auth/api-key/RemoteApiKeyRepository.js +42 -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 +23 -0
- package/dist/src/{channels → addon/chat-controller}/cmd/CmdChannel.js +4 -26
- package/dist/src/{channels → addon/chat-controller}/socket/@socket.js +9 -4
- package/dist/src/{channels → addon/chat-controller}/socket/SocketChannel.js +5 -23
- package/dist/src/{channels → addon/chat-controller}/socket/SocketChannelConfig.js +1 -1
- package/dist/src/{channels → addon/chat-controller}/telegram/@telegram.js +9 -4
- package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannel.js +4 -22
- package/dist/src/addon/chat-controller/whatsapp/@whatsApp.js +25 -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 +20 -41
- 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 +26 -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 +60 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppWabotProxyConnection.js +45 -0
- package/dist/src/addon/mindset/html/HtmlModule.js +70 -0
- package/dist/src/core/auth/Auth.js +24 -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/validateIsOptional.js +5 -0
- package/dist/src/core/validation/core/validateModel.js +36 -0
- package/dist/src/core/validation/metadata/@isArray.js +18 -0
- package/dist/src/core/validation/metadata/@isModel.js +18 -0
- package/dist/src/core/validation/metadata/@isOptional.js +17 -0
- package/dist/src/core/validation/metadata/ValidationMetadataStore.js +98 -0
- package/dist/src/core/validation/modelInfo.js +9 -0
- package/dist/src/core/validation/validate.js +11 -0
- package/dist/src/core/validation/validators/is-boolean/@isBoolean.js +17 -0
- package/dist/src/core/validation/validators/is-boolean/validateIsBoolean.js +12 -0
- package/dist/src/core/validation/validators/is-date/@isDate.js +17 -0
- package/dist/src/core/validation/validators/is-date/validateIsDate.js +16 -0
- package/dist/src/core/validation/validators/is-not-empty/@isNotEmpty.js +17 -0
- package/dist/src/core/validation/validators/is-not-empty/validateIsNotEmpty.js +12 -0
- package/dist/src/core/validation/validators/is-number/@isNumber.js +17 -0
- package/dist/src/core/validation/validators/is-number/validateIsNumber.js +12 -0
- package/dist/src/core/validation/validators/is-present/@isPresent.js +17 -0
- package/dist/src/core/validation/validators/is-present/validateIsPresent.js +12 -0
- package/dist/src/core/validation/validators/is-string/@isString.js +17 -0
- package/dist/src/core/validation/validators/is-string/validateIsString.js +12 -0
- package/dist/src/core/validation/validators/max/@max.js +18 -0
- package/dist/src/core/validation/validators/max/validateMax.js +17 -0
- package/dist/src/core/validation/validators/min/@min.js +18 -0
- package/dist/src/core/validation/validators/min/validateMin.js +17 -0
- 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 +69 -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 +3 -0
- package/dist/src/{mindset → feature/mindset}/MindsetOperator.js +69 -42
- 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 +30 -9
- package/dist/src/{repository → feature}/pg/PgRepositoryBase.js +2 -2
- package/dist/src/feature/rest-controller/injection-tokens.js +4 -0
- package/dist/src/feature/rest-controller/metadata/@middleware.js +16 -0
- 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/feature/rest-controller/metadata/@restController.js +15 -0
- package/dist/src/feature/rest-controller/metadata/RestControllerMetadataStore.js +50 -0
- package/dist/src/feature/rest-controller/metadata/methodDecorator.js +19 -0
- 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 +1202 -575
- package/dist/src/index.js +145 -76
- package/package.json +10 -4
- package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +0 -106
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +0 -88
- package/dist/src/channels/cmd/@cmd.js +0 -18
- 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/metadata/params/@isOptional.js +0 -21
- 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/server/prepareChatContainer.js +0 -43
- package/dist/src/server/runChannel.js +0 -27
- package/dist/src/server/runServer.js +0 -40
- /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/{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,27 @@
|
|
|
1
|
+
import { Entity } from '../../core/entity/Entity.js';
|
|
2
|
+
import { CustomError } from '../../core/error/CustomError.js';
|
|
3
|
+
|
|
4
|
+
class Job extends Entity {
|
|
5
|
+
get commandName() {
|
|
6
|
+
return this.data.commandName;
|
|
7
|
+
}
|
|
8
|
+
hasFinished() {
|
|
9
|
+
return this.data.successAt != null || this.data.failedAt != null;
|
|
10
|
+
}
|
|
11
|
+
setAsStarted() {
|
|
12
|
+
this.data.startedAt = new Date().getTime();
|
|
13
|
+
}
|
|
14
|
+
setAsSuccess() {
|
|
15
|
+
this.data.successAt = new Date().getTime();
|
|
16
|
+
}
|
|
17
|
+
setAsFailed(error) {
|
|
18
|
+
this.data.failedAt = new Date().getTime();
|
|
19
|
+
this.data.error = {
|
|
20
|
+
message: error.message,
|
|
21
|
+
stack: error.stack,
|
|
22
|
+
info: error instanceof CustomError ? error.info : undefined,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { Job };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { singleton } from '../../core/injection/index.js';
|
|
3
|
+
|
|
4
|
+
let JobRepository = class JobRepository {
|
|
5
|
+
find(id) {
|
|
6
|
+
throw new Error('Method not implemented.');
|
|
7
|
+
}
|
|
8
|
+
findOrThrow(id) {
|
|
9
|
+
throw new Error('Method not implemented.');
|
|
10
|
+
}
|
|
11
|
+
findByIds(ids) {
|
|
12
|
+
throw new Error('Method not implemented.');
|
|
13
|
+
}
|
|
14
|
+
findAll(id) {
|
|
15
|
+
throw new Error('Method not implemented.');
|
|
16
|
+
}
|
|
17
|
+
create(item) {
|
|
18
|
+
throw new Error('Method not implemented.');
|
|
19
|
+
}
|
|
20
|
+
update(item) {
|
|
21
|
+
throw new Error('Method not implemented.');
|
|
22
|
+
}
|
|
23
|
+
delete(item) {
|
|
24
|
+
throw new Error('Method not implemented.');
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
JobRepository = __decorate([
|
|
28
|
+
singleton()
|
|
29
|
+
], JobRepository);
|
|
30
|
+
|
|
31
|
+
export { JobRepository };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { CommandMetadataStore } from './CommandMetadataStore.js';
|
|
3
|
+
import { JobRepository } from './JobRepository.js';
|
|
4
|
+
import { singleton, container } from '../../core/injection/index.js';
|
|
5
|
+
import { Logger } from '../../core/logger/Logger.js';
|
|
6
|
+
|
|
7
|
+
let JobRunner = class JobRunner {
|
|
8
|
+
jobRepository;
|
|
9
|
+
handlerContainer;
|
|
10
|
+
logger = new Logger('wabot:job-runner');
|
|
11
|
+
constructor(jobRepository, handlerContainer) {
|
|
12
|
+
this.jobRepository = jobRepository;
|
|
13
|
+
this.handlerContainer = handlerContainer;
|
|
14
|
+
}
|
|
15
|
+
async run(job) {
|
|
16
|
+
try {
|
|
17
|
+
const { commandName, commandData } = job['data'];
|
|
18
|
+
const handlerConstructor = this.handlerContainer.getHandlerForCommandName(commandName);
|
|
19
|
+
if (!handlerConstructor) {
|
|
20
|
+
throw new Error(`Not found handler for command '${commandName}'`);
|
|
21
|
+
}
|
|
22
|
+
const handler = container.resolve(handlerConstructor);
|
|
23
|
+
const commandConstructor = this.handlerContainer.getCommandForCommandName(commandName);
|
|
24
|
+
if (!commandConstructor) {
|
|
25
|
+
throw new Error(`Not found class for command name '${commandName}'`);
|
|
26
|
+
}
|
|
27
|
+
job.setAsStarted();
|
|
28
|
+
await this.jobRepository.update(job);
|
|
29
|
+
const command = new commandConstructor(commandData);
|
|
30
|
+
await handler.handle(command);
|
|
31
|
+
job.setAsSuccess();
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
this.logger.error(e);
|
|
35
|
+
job.setAsFailed(e instanceof Error ? e : new Error('Invalid Job error'));
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
await this.jobRepository.update(job);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
JobRunner = __decorate([
|
|
43
|
+
singleton(),
|
|
44
|
+
__metadata("design:paramtypes", [JobRepository,
|
|
45
|
+
CommandMetadataStore])
|
|
46
|
+
], JobRunner);
|
|
47
|
+
|
|
48
|
+
export { JobRunner };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { singleton } from '../../core/injection/index.js';
|
|
3
|
+
import { Logger } from '../../core/logger/Logger.js';
|
|
4
|
+
|
|
5
|
+
let JobsEventsHub = class JobsEventsHub {
|
|
6
|
+
logger = new Logger('wabot:jobs-events-hub');
|
|
7
|
+
jobsEventsListener = null;
|
|
8
|
+
notifyJobCreated(job) {
|
|
9
|
+
const timer = setTimeout(async () => {
|
|
10
|
+
if (!this.jobsEventsListener) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
await this.jobsEventsListener({
|
|
15
|
+
jobId: job.id,
|
|
16
|
+
commandName: job.commandName,
|
|
17
|
+
type: 'created',
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
this.logger.error(err);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
clearTimeout(timer);
|
|
25
|
+
}
|
|
26
|
+
}, 1000);
|
|
27
|
+
}
|
|
28
|
+
listenJobsEvents(listener) {
|
|
29
|
+
this.jobsEventsListener = listener;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
JobsEventsHub = __decorate([
|
|
33
|
+
singleton()
|
|
34
|
+
], JobsEventsHub);
|
|
35
|
+
|
|
36
|
+
export { JobsEventsHub };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { JobsEventsHub } from './JobsEventsHub.js';
|
|
2
|
+
import { JobRunner } from './JobRunner.js';
|
|
3
|
+
import { CommandMetadataStore } from './CommandMetadataStore.js';
|
|
4
|
+
import { JobRepository } from './JobRepository.js';
|
|
5
|
+
import { container } from '../../core/injection/index.js';
|
|
6
|
+
|
|
7
|
+
function runAsyncCommandHandlers(handlers) {
|
|
8
|
+
const eventsHub = container.resolve(JobsEventsHub);
|
|
9
|
+
const jobRunner = container.resolve(JobRunner);
|
|
10
|
+
const jobRepository = container.resolve(JobRepository);
|
|
11
|
+
const commandsHandlersContainer = container.resolve(CommandMetadataStore);
|
|
12
|
+
const handledCommands = handlers
|
|
13
|
+
.map((x) => commandsHandlersContainer.getCommandNameForHandler(x))
|
|
14
|
+
.filter((x) => x)
|
|
15
|
+
.map((x) => x);
|
|
16
|
+
eventsHub.listenJobsEvents(async (event) => {
|
|
17
|
+
try {
|
|
18
|
+
const job = await jobRepository.findOrThrow(event.jobId);
|
|
19
|
+
if (handledCommands.includes(job.commandName)) {
|
|
20
|
+
jobRunner.run(job);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
console.error(e);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { runAsyncCommandHandlers };
|
|
@@ -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,69 @@
|
|
|
1
|
+
import { container } from '../../core/injection/index.js';
|
|
2
|
+
import { Chat } from '../chat-bot/Chat.js';
|
|
3
|
+
import { ChatBot } from '../chat-bot/ChatBot.js';
|
|
4
|
+
import { ChatMemory } from '../chat-bot/ChatMemory.js';
|
|
5
|
+
import { ChatRepository } from '../chat-bot/ChatRepository.js';
|
|
6
|
+
import 'uuid';
|
|
7
|
+
import { ChatBotMetadataStore } from '../chat-bot/metadata/ChatBotMetadataStore.js';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import '../mindset/metadata/MindsetMetadataStore.js';
|
|
10
|
+
import { Mindset } from '../mindset/IMindset.js';
|
|
11
|
+
import '../mindset/MindsetOperator.js';
|
|
12
|
+
import { ChatResolver } from './ChatResolver.js';
|
|
13
|
+
import { ControllerMetadataStore } from './metadata/ControllerMetadataStore.js';
|
|
14
|
+
import { Container } from '../../core/injection/Container.js';
|
|
15
|
+
|
|
16
|
+
async function prepareChatContainer(container, messageContext, mindsetCtor) {
|
|
17
|
+
const chatContainer = container.createChildContainer();
|
|
18
|
+
chatContainer.register(Container, { useValue: chatContainer });
|
|
19
|
+
chatContainer.registerInstance(Chat, messageContext.chat);
|
|
20
|
+
const chatRepository = container.resolve(ChatRepository);
|
|
21
|
+
const chatMemory = await chatRepository.findMemory(messageContext.chat.id);
|
|
22
|
+
if (!chatMemory) {
|
|
23
|
+
throw new Error('Not found Chat Memory for Chat with Id=' + messageContext.chat.id);
|
|
24
|
+
}
|
|
25
|
+
chatContainer.registerInstance(ChatMemory, chatMemory);
|
|
26
|
+
const chatBotMetadataStore = container.resolve(ChatBotMetadataStore);
|
|
27
|
+
const chatBots = chatBotMetadataStore.getChatBotsMetadata();
|
|
28
|
+
for (const chatBotMetadata of chatBots) {
|
|
29
|
+
chatContainer.beforeResolution(chatBotMetadata.constructor, (a, b) => {
|
|
30
|
+
const subContainer = chatContainer.createChildContainer();
|
|
31
|
+
subContainer.register(Mindset, { useClass: chatBotMetadata.mindsetConstructor });
|
|
32
|
+
const chatBot = subContainer.resolve(ChatBot);
|
|
33
|
+
chatContainer.register(chatBotMetadata.injectionToken, { useValue: chatBot });
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return chatContainer;
|
|
37
|
+
}
|
|
38
|
+
function runChatControllers(controllers) {
|
|
39
|
+
const metadataStore = container.resolve(ControllerMetadataStore);
|
|
40
|
+
const chatResolver = container.resolve(ChatResolver);
|
|
41
|
+
for (const controllerCtor of controllers) {
|
|
42
|
+
const chatControllerMetadata = metadataStore.getChatControllerMetadata(controllerCtor);
|
|
43
|
+
if (!chatControllerMetadata) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
for (const channelMetadata of chatControllerMetadata.channels) {
|
|
47
|
+
const channelContainer = container.createChildContainer();
|
|
48
|
+
if (channelMetadata.channelConfig) {
|
|
49
|
+
channelContainer.register(channelMetadata.channelConfig.constructor, {
|
|
50
|
+
useValue: channelMetadata.channelConfig,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const channel = channelContainer.resolve(channelMetadata.channelConstructor);
|
|
54
|
+
channel.listen(async (channelMessage) => {
|
|
55
|
+
const chat = await chatResolver.resolve(channelMessage.chatConnection);
|
|
56
|
+
const chatContainer = await prepareChatContainer(channelContainer, {
|
|
57
|
+
chat,
|
|
58
|
+
message: channelMessage.message,
|
|
59
|
+
reply: channelMessage.reply,
|
|
60
|
+
});
|
|
61
|
+
const chatController = chatContainer.resolve(channelMetadata.controllerConstructor);
|
|
62
|
+
chatController[channelMetadata.functionName](channelMessage);
|
|
63
|
+
});
|
|
64
|
+
channel.connect();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
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;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import { injectable } from '
|
|
2
|
+
import { injectable } from '../../core/injection/index.js';
|
|
3
3
|
import { Mindset } from './IMindset.js';
|
|
4
4
|
import { MindsetMetadataStore } from './metadata/MindsetMetadataStore.js';
|
|
5
|
-
import { Container } from '
|
|
5
|
+
import { Container } from '../../core/injection/Container.js';
|
|
6
6
|
|
|
7
7
|
let MindsetOperator = class MindsetOperator {
|
|
8
8
|
mindset;
|
|
@@ -22,64 +22,69 @@ let MindsetOperator = class MindsetOperator {
|
|
|
22
22
|
limits() {
|
|
23
23
|
return this.mindset.limits();
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
.map((module) => module.functions)
|
|
28
|
-
.flat()
|
|
29
|
-
.find((fn) => fn.name === name);
|
|
30
|
-
if (!fnMetadata) {
|
|
31
|
-
throw new Error(`Function ${name} not found`);
|
|
32
|
-
}
|
|
33
|
-
const paramsObj = JSON.parse(params);
|
|
34
|
-
const module = this.container.resolve(fnMetadata.moduleConstructor);
|
|
35
|
-
try {
|
|
36
|
-
const response = await module[name](paramsObj);
|
|
37
|
-
if (!response) {
|
|
38
|
-
return 'success';
|
|
39
|
-
}
|
|
40
|
-
return response.toString();
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
return `Error: ${error}`;
|
|
44
|
-
}
|
|
25
|
+
llms() {
|
|
26
|
+
return this.mindset.llms();
|
|
45
27
|
}
|
|
46
|
-
async
|
|
28
|
+
async systemPrompt() {
|
|
29
|
+
let [identity, skills, limits] = await Promise.all([
|
|
30
|
+
this.identity(),
|
|
31
|
+
this.skills(),
|
|
32
|
+
this.limits(),
|
|
33
|
+
]);
|
|
34
|
+
const language = identity.language.replaceAll('#', ' ');
|
|
35
|
+
const name = identity.name.replaceAll('#', ' ');
|
|
36
|
+
const age = identity.age ? identity.age.toString().replaceAll('#', ' ') : null;
|
|
37
|
+
const personality = identity.personality ? identity.personality.replaceAll('#', ' ') : null;
|
|
38
|
+
skills = skills.replaceAll('#', ' ');
|
|
39
|
+
limits = limits.replaceAll('#', ' ');
|
|
40
|
+
const systemPrompt = `
|
|
41
|
+
# System Instructions
|
|
42
|
+
you should act as a assistant.
|
|
43
|
+
your main language is ${language}.
|
|
44
|
+
your name is ${name}.
|
|
45
|
+
${age ? 'you are ' + age + ' years old.' : ''}
|
|
46
|
+
|
|
47
|
+
${personality ? '## Personality (in your main language) \n' + personality : ''}
|
|
48
|
+
|
|
49
|
+
## Skills (in your main language)
|
|
50
|
+
${skills}
|
|
51
|
+
|
|
52
|
+
## System limitations (in your main language)
|
|
53
|
+
${limits}
|
|
54
|
+
|
|
55
|
+
## Chat memory
|
|
56
|
+
Next you will receive a chat history,
|
|
57
|
+
you should use this information to answer the user.
|
|
58
|
+
`;
|
|
59
|
+
return systemPrompt;
|
|
60
|
+
}
|
|
61
|
+
tools() {
|
|
47
62
|
return this.metadata.modules
|
|
48
|
-
.map((module) => module.functions.map((fn) => this.
|
|
63
|
+
.map((module) => module.functions.map((fn) => this.tool(fn, module)))
|
|
49
64
|
.flat();
|
|
50
65
|
}
|
|
51
|
-
|
|
66
|
+
tool(fn, module) {
|
|
52
67
|
const description = fn.config.description.replaceAll('#', ' ');
|
|
53
68
|
return {
|
|
54
|
-
|
|
69
|
+
language: module.config.language ?? 'english',
|
|
55
70
|
name: fn.name,
|
|
56
71
|
description,
|
|
57
|
-
parameters:
|
|
58
|
-
type: 'object',
|
|
59
|
-
properties: fn.params.reduce((prev, param) => ({
|
|
60
|
-
...prev,
|
|
61
|
-
[param.name]: this.toolParam(param),
|
|
62
|
-
}), {}),
|
|
63
|
-
required: fn.params.filter((param) => !param.config.optional).map((param) => param.name)
|
|
64
|
-
},
|
|
72
|
+
parameters: fn.params.map((param) => this.toolParameter(param)),
|
|
65
73
|
};
|
|
66
74
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
description: `
|
|
75
|
+
toolParameter(param) {
|
|
76
|
+
let description = `
|
|
70
77
|
### description (in your main language)
|
|
71
78
|
${param.config.description.replaceAll('#', ' ')}
|
|
72
|
-
|
|
73
|
-
};
|
|
79
|
+
`;
|
|
74
80
|
const type = (() => {
|
|
75
81
|
if (param.type === Number)
|
|
76
82
|
return 'number';
|
|
77
83
|
if (param.type === String)
|
|
78
84
|
return 'string';
|
|
79
85
|
if (param.type === Date) {
|
|
80
|
-
|
|
86
|
+
description = `${description}
|
|
81
87
|
### format: ISO 8681 - YYYY-MM-DDTHH:mm:ssZ
|
|
82
|
-
${addons.description}
|
|
83
88
|
`;
|
|
84
89
|
return 'string';
|
|
85
90
|
}
|
|
@@ -87,9 +92,31 @@ let MindsetOperator = class MindsetOperator {
|
|
|
87
92
|
})();
|
|
88
93
|
return {
|
|
89
94
|
type,
|
|
90
|
-
|
|
95
|
+
name: param.name,
|
|
96
|
+
description,
|
|
91
97
|
};
|
|
92
98
|
}
|
|
99
|
+
async callFunction(name, params) {
|
|
100
|
+
const fnMetadata = this.metadata.modules
|
|
101
|
+
.map((module) => module.functions)
|
|
102
|
+
.flat()
|
|
103
|
+
.find((fn) => fn.name === name);
|
|
104
|
+
if (!fnMetadata) {
|
|
105
|
+
throw new Error(`Function ${name} not found`);
|
|
106
|
+
}
|
|
107
|
+
const paramsObj = JSON.parse(params);
|
|
108
|
+
const module = this.container.resolve(fnMetadata.moduleConstructor);
|
|
109
|
+
try {
|
|
110
|
+
const response = await module[name](paramsObj);
|
|
111
|
+
if (!response) {
|
|
112
|
+
return 'success';
|
|
113
|
+
}
|
|
114
|
+
return response.toString();
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
return `Error: ${error}`;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
93
120
|
};
|
|
94
121
|
MindsetOperator = __decorate([
|
|
95
122
|
injectable(),
|
|
@@ -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
|
|