@wabot-dev/framework 0.0.16 → 0.0.50
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/_virtual/_commonjsHelpers.js +5 -0
- package/dist/src/_virtual/cjs.js +7 -0
- package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +107 -0
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +9 -7
- package/dist/src/channels/cmd/@cmd.js +0 -4
- package/dist/src/channels/cmd/CmdChannel.js +0 -4
- package/dist/src/channels/express/ExpressProvider.js +46 -0
- package/dist/src/channels/http/HttpServerProvider.js +31 -0
- package/dist/src/channels/socket/@socket.js +20 -0
- package/dist/src/channels/socket/SocketChannel.js +79 -0
- package/dist/src/channels/socket/SocketChannelConfig.js +15 -0
- package/dist/src/channels/socket/SocketServerProvider.js +50 -0
- package/dist/src/channels/telegram/@telegram.js +0 -4
- package/dist/src/channels/telegram/TelegramChannel.js +0 -4
- package/dist/src/channels/wabot/WabotDevConnection.js +57 -0
- package/dist/src/channels/wabot/WabotDevSocketContracts.js +10 -0
- package/dist/src/channels/whatsapp/@whatsapp.js +0 -4
- package/dist/src/channels/whatsapp/EnvWhatsAppRepository.js +49 -0
- package/dist/src/channels/whatsapp/PgWhatsAppRepository.js +41 -0
- package/dist/src/channels/whatsapp/WhatsApp.js +32 -0
- package/dist/src/channels/whatsapp/WhatsAppChannel.js +33 -32
- package/dist/src/channels/whatsapp/{WhatsAppConnection.js → WhatsAppReceiver.js} +4 -6
- package/dist/src/channels/whatsapp/WhatsAppReceiverByDevConnection.js +32 -0
- package/dist/src/channels/whatsapp/WhatsAppReceiverByWebHook.js +63 -0
- package/dist/src/channels/whatsapp/WhatsAppRepository.js +10 -0
- package/dist/src/channels/whatsapp/WhatsAppSender.js +99 -0
- package/dist/src/channels/whatsapp/WhatsAppSenderByCloudApi.js +124 -0
- package/dist/src/channels/whatsapp/WhatsAppSenderByDevConnection.js +61 -0
- package/dist/src/chatbot/ChatBotAdapter.js +1 -5
- package/dist/src/chatbot/metadata/@chatBot.js +0 -5
- package/dist/src/chatbot/metadata/ChatBotMetadataStore.js +0 -5
- package/dist/src/controller/metadata/ControllerMetadataStore.js +0 -2
- package/dist/src/controller/metadata/controller/@chatController.js +0 -2
- package/dist/src/core/IMessageContext.js +0 -2
- package/dist/src/env/WabotEnv.js +6 -0
- package/dist/src/index.d.ts +454 -163
- package/dist/src/index.js +56 -21
- package/dist/src/mindset/MindsetOperator.js +1 -3
- package/dist/src/mindset/metadata/MindsetMetadataStore.js +0 -2
- package/dist/src/mindset/metadata/mindsets/@mindset.js +0 -2
- package/dist/src/mindset/metadata/modules/@mindsetModule.js +0 -2
- package/dist/src/node_modules/@selderee/plugin-htmlparser2/lib/hp2-builder.js +90 -0
- package/dist/src/node_modules/deepmerge/dist/cjs.js +142 -0
- package/dist/src/node_modules/dom-serializer/lib/esm/foreignNames.js +102 -0
- package/dist/src/node_modules/dom-serializer/lib/esm/index.js +186 -0
- package/dist/src/node_modules/domelementtype/lib/esm/index.js +53 -0
- package/dist/src/node_modules/domhandler/lib/esm/index.js +148 -0
- package/dist/src/node_modules/domhandler/lib/esm/node.js +334 -0
- package/dist/src/node_modules/entities/lib/esm/decode.js +458 -0
- package/dist/src/node_modules/entities/lib/esm/decode_codepoint.js +62 -0
- package/dist/src/node_modules/entities/lib/esm/escape.js +99 -0
- package/dist/src/node_modules/entities/lib/esm/generated/decode-data-html.js +8 -0
- package/dist/src/node_modules/entities/lib/esm/generated/decode-data-xml.js +8 -0
- package/dist/src/node_modules/html-to-text/lib/html-to-text.js +2147 -0
- package/dist/src/node_modules/htmlparser2/lib/esm/Parser.js +491 -0
- package/dist/src/node_modules/htmlparser2/lib/esm/Tokenizer.js +928 -0
- package/dist/src/node_modules/htmlparser2/lib/esm/index.js +18 -0
- package/dist/src/node_modules/leac/lib/leac.js +3 -0
- package/dist/src/node_modules/parseley/lib/parseley.js +270 -0
- package/dist/src/node_modules/peberminta/lib/core.js +171 -0
- package/dist/src/node_modules/selderee/lib/selderee.js +380 -0
- package/dist/src/pre-made/module/authentication/requests/SendOneTimePasswordRequest.js +0 -2
- package/dist/src/pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js +0 -2
- package/dist/src/pre-made/module/html/HtmlModule.js +70 -0
- package/dist/src/pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js +0 -2
- package/dist/src/pre-made/repository/chat/pg/PgChatMemory.js +1 -7
- package/dist/src/pre-made/repository/chat/pg/PgChatRepository.js +0 -5
- package/dist/src/pre-made/repository/user/pg/PgUserRepository.js +0 -5
- package/dist/src/repository/pg/PgCrudRepository.js +0 -1
- package/dist/src/rest-controller/metadata/@get.js +19 -0
- package/dist/src/rest-controller/metadata/@post.js +19 -0
- package/dist/src/rest-controller/metadata/@restController.js +15 -0
- package/dist/src/rest-controller/metadata/RestControllerMetadataStore.js +37 -0
- package/dist/src/rest-controller/runRestControllers.js +74 -0
- package/dist/src/server/prepareChatContainer.js +4 -4
- package/dist/src/server/runChannel.js +2 -10
- package/dist/src/server/runServer.js +0 -43
- package/dist/src/validation/metadata/@isBoolean.js +17 -0
- package/dist/src/validation/metadata/@isDate.js +17 -0
- package/dist/src/validation/metadata/@isNotEmpty.js +17 -0
- package/dist/src/validation/metadata/@isNumber.js +17 -0
- package/dist/src/validation/metadata/@isOptional.js +17 -0
- package/dist/src/validation/metadata/@isPresent.js +17 -0
- package/dist/src/validation/metadata/@isString.js +17 -0
- package/dist/src/validation/metadata/@max.js +18 -0
- package/dist/src/validation/metadata/@min.js +18 -0
- package/dist/src/validation/metadata/@validable.js +14 -0
- package/dist/src/validation/metadata/ValidationMetadataStore.js +55 -0
- package/dist/src/validation/validateModel2.js +11 -0
- package/dist/src/validation/validators/validateIsBoolean.js +12 -0
- package/dist/src/validation/validators/validateIsDate.js +16 -0
- package/dist/src/validation/validators/validateIsNotEmpty.js +12 -0
- package/dist/src/validation/validators/validateIsNumber.js +12 -0
- package/dist/src/validation/validators/validateIsOptional.js +5 -0
- package/dist/src/validation/validators/validateIsPresent.js +12 -0
- package/dist/src/validation/validators/validateIsString.js +12 -0
- package/dist/src/validation/validators/validateMax.js +17 -0
- package/dist/src/validation/validators/validateMin.js +17 -0
- package/dist/src/validation/validators/validateModel.js +47 -0
- package/package.json +4 -6
- package/dist/src/channels/whatsapp/WhatsAppDevConnection.js +0 -61
- package/dist/src/channels/whatsapp/WhatsAppProdConnection.js +0 -36
- package/dist/src/channels/whatsapp/whatsAppDevSocketContracts.js +0 -9
- package/dist/src/controller/express/Express.js +0 -5
- package/dist/src/controller/socket.io/SocketIO.js +0 -5
- package/dist/src/mindset/metadata/params/@isOptional.js +0 -21
- package/dist/src/pre-made/repository/chat/sqlite/SqliteChatMemory.js +0 -23
- package/dist/src/pre-made/repository/chat/sqlite/SqliteChatRepository.js +0 -30
- package/dist/src/pre-made/repository/user/sqlite/SqliteUserRepository.js +0 -23
- package/dist/src/repository/sqlite/SqliteCrudRepository.js +0 -85
- package/dist/src/repository/sqlite/SqlitePersistentMapper.js +0 -17
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { getDefaultExportFromCjs } from './_commonjsHelpers.js';
|
|
2
|
+
import { __require as requireCjs } from '../node_modules/deepmerge/dist/cjs.js';
|
|
3
|
+
|
|
4
|
+
var cjsExports = requireCjs();
|
|
5
|
+
var merge = /*@__PURE__*/getDefaultExportFromCjs(cjsExports);
|
|
6
|
+
|
|
7
|
+
export { merge as default };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { injectable } from '../../injection/index.js';
|
|
3
|
+
import { OpenAI } from 'openai';
|
|
4
|
+
import 'uuid';
|
|
5
|
+
import '../../chatbot/metadata/ChatBotMetadataStore.js';
|
|
6
|
+
import '../../chatbot/ChatBot.js';
|
|
7
|
+
import { ChatBotAdapter } from '../../chatbot/ChatBotAdapter.js';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import '../../mindset/metadata/MindsetMetadataStore.js';
|
|
10
|
+
import { MindsetOperator } from '../../mindset/MindsetOperator.js';
|
|
11
|
+
|
|
12
|
+
let DeepSeekChatBotAdapter = class DeepSeekChatBotAdapter extends ChatBotAdapter {
|
|
13
|
+
deepSeek;
|
|
14
|
+
model;
|
|
15
|
+
constructor(mindset) {
|
|
16
|
+
super(mindset);
|
|
17
|
+
const model = process.env.DEEPSEEK_CHAT_MODEL;
|
|
18
|
+
const apiKey = process.env.DEEPSEEK_API_KEY;
|
|
19
|
+
const baseURL = process.env.DEEPSEEK_BASE_URL;
|
|
20
|
+
this.validateEnvVariables([model, apiKey, baseURL]);
|
|
21
|
+
this.model = model || 'deepseek-chat';
|
|
22
|
+
this.deepSeek = new OpenAI({
|
|
23
|
+
apiKey: apiKey,
|
|
24
|
+
baseURL: baseURL,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
validateEnvVariables(envVariables) {
|
|
28
|
+
envVariables.forEach((envVariable) => {
|
|
29
|
+
if (!envVariable) {
|
|
30
|
+
throw new Error('Missing environment variable');
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async generateNextChatItem(chatItems) {
|
|
35
|
+
const systemPrompt = await this.systemPrompt();
|
|
36
|
+
const tools = (await this.mindset.allFunctionsDescriptors()).map((fn) => {
|
|
37
|
+
const parameters = { ...fn.parameters, additionalProperties: false, type: 'object' };
|
|
38
|
+
return {
|
|
39
|
+
type: 'function',
|
|
40
|
+
function: { name: fn.name, description: fn.description, parameters, strict: true },
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
const response = await this.deepSeek.chat.completions.create({
|
|
44
|
+
model: this.model,
|
|
45
|
+
messages: [{ role: 'system', content: systemPrompt }, ...this.mapChatItems(chatItems)],
|
|
46
|
+
tools: tools,
|
|
47
|
+
tool_choice: 'auto',
|
|
48
|
+
});
|
|
49
|
+
let newChatItem;
|
|
50
|
+
const { tool_calls: responseFunctionCall, content: responseText } = response.choices?.[0]?.message ?? {};
|
|
51
|
+
if (responseText) {
|
|
52
|
+
newChatItem = await this.buildBotMessageItem(responseText);
|
|
53
|
+
}
|
|
54
|
+
else if (responseFunctionCall && responseFunctionCall[0]?.type == 'function') {
|
|
55
|
+
newChatItem = await this.buildFunctionCallItem(responseFunctionCall[0].id, responseFunctionCall[0].function.name, responseFunctionCall[0].function.arguments);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
throw new Error('Not supported DeepSeek Response');
|
|
59
|
+
}
|
|
60
|
+
return newChatItem;
|
|
61
|
+
}
|
|
62
|
+
mapChatItems(chatItems) {
|
|
63
|
+
const deepSeekInput = [];
|
|
64
|
+
for (const item of chatItems) {
|
|
65
|
+
const itemData = item.getData();
|
|
66
|
+
if (itemData.type === 'CONNECTION_MESSAGE') {
|
|
67
|
+
if (!itemData.content.text) {
|
|
68
|
+
throw new Error('System message content is empty');
|
|
69
|
+
}
|
|
70
|
+
deepSeekInput.push({ role: 'user', content: itemData.content.text });
|
|
71
|
+
}
|
|
72
|
+
else if (itemData.type === 'BOT_MESSAGE') {
|
|
73
|
+
if (!itemData.content.text) {
|
|
74
|
+
throw new Error('System message content is empty');
|
|
75
|
+
}
|
|
76
|
+
deepSeekInput.push({ role: 'assistant', content: itemData.content.text });
|
|
77
|
+
}
|
|
78
|
+
if (itemData.type === 'FUNCTION_CALL') {
|
|
79
|
+
deepSeekInput.push({
|
|
80
|
+
role: 'assistant',
|
|
81
|
+
tool_calls: [
|
|
82
|
+
{
|
|
83
|
+
id: itemData.content.id,
|
|
84
|
+
type: 'function',
|
|
85
|
+
function: {
|
|
86
|
+
name: itemData.content.name,
|
|
87
|
+
arguments: JSON.stringify(itemData.content.arguments),
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
});
|
|
92
|
+
deepSeekInput.push({
|
|
93
|
+
role: 'tool',
|
|
94
|
+
tool_call_id: itemData.content.id,
|
|
95
|
+
content: itemData.content.result,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return deepSeekInput;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
DeepSeekChatBotAdapter = __decorate([
|
|
103
|
+
injectable(),
|
|
104
|
+
__metadata("design:paramtypes", [MindsetOperator])
|
|
105
|
+
], DeepSeekChatBotAdapter);
|
|
106
|
+
|
|
107
|
+
export { DeepSeekChatBotAdapter };
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
2
|
import { injectable } from '../../injection/index.js';
|
|
3
3
|
import { OpenAI } from 'openai';
|
|
4
|
-
import 'reflect-metadata';
|
|
5
|
-
import '../../mindset/metadata/MindsetMetadataStore.js';
|
|
6
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
7
|
-
import '../../core/user/IUserRepository.js';
|
|
8
|
-
import { MindsetOperator } from '../../mindset/MindsetOperator.js';
|
|
9
4
|
import 'uuid';
|
|
10
5
|
import '../../chatbot/metadata/ChatBotMetadataStore.js';
|
|
11
6
|
import '../../chatbot/ChatBot.js';
|
|
12
7
|
import { ChatBotAdapter } from '../../chatbot/ChatBotAdapter.js';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import '../../mindset/metadata/MindsetMetadataStore.js';
|
|
10
|
+
import { MindsetOperator } from '../../mindset/MindsetOperator.js';
|
|
11
|
+
import { Logger } from '../../logger/Logger.js';
|
|
13
12
|
|
|
14
13
|
let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
|
|
15
14
|
openai = new OpenAI();
|
|
16
15
|
model;
|
|
16
|
+
logger = new Logger('wabot:openai-chat-bot-adapter');
|
|
17
17
|
constructor(mindset) {
|
|
18
18
|
super(mindset);
|
|
19
19
|
const model = process.env.OPENAI_CHAT_MODEL;
|
|
@@ -28,11 +28,13 @@ let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
|
|
|
28
28
|
const parameters = { ...fn.parameters, additionalProperties: false, type: 'object' };
|
|
29
29
|
return { ...fn, type: 'function', parameters, strict: true };
|
|
30
30
|
});
|
|
31
|
-
const
|
|
31
|
+
const request = {
|
|
32
32
|
model: this.model,
|
|
33
33
|
input: [{ role: 'system', content: systemPrompt }, ...this.mapChatItems(chatItems)],
|
|
34
34
|
tools,
|
|
35
|
-
}
|
|
35
|
+
};
|
|
36
|
+
this.logger.debug(`Call Api with Request: ${JSON.stringify(request)}`);
|
|
37
|
+
const response = await this.openai.responses.create(request);
|
|
36
38
|
let newChatItem;
|
|
37
39
|
if (response.output_text) {
|
|
38
40
|
newChatItem = await this.buildBotMessageItem(response.output_text);
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import '../../controller/channel/ChatResolver.js';
|
|
2
2
|
import '../../controller/channel/UserResolver.js';
|
|
3
3
|
import { container } from '../../injection/index.js';
|
|
4
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
5
|
-
import '../../core/user/IUserRepository.js';
|
|
6
4
|
import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
|
|
7
|
-
import 'express';
|
|
8
|
-
import 'socket.io';
|
|
9
5
|
import { CmdChannel } from './CmdChannel.js';
|
|
10
6
|
|
|
11
7
|
function cmd() {
|
|
@@ -2,11 +2,7 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
3
3
|
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
4
4
|
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../core/user/IUserRepository.js';
|
|
7
5
|
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
8
|
-
import 'express';
|
|
9
|
-
import 'socket.io';
|
|
10
6
|
import * as readline from 'readline';
|
|
11
7
|
|
|
12
8
|
var CmdChannel_1;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { HttpServerProvider } from '../http/HttpServerProvider.js';
|
|
3
|
+
import express from 'express';
|
|
4
|
+
import { singleton } from 'tsyringe';
|
|
5
|
+
import bodyParser from 'body-parser';
|
|
6
|
+
import { Logger } from '../../logger/Logger.js';
|
|
7
|
+
|
|
8
|
+
let ExpressProvider = class ExpressProvider {
|
|
9
|
+
httpServerProvider;
|
|
10
|
+
expressApp = null;
|
|
11
|
+
logger = new Logger('wabot:express');
|
|
12
|
+
constructor(httpServerProvider) {
|
|
13
|
+
this.httpServerProvider = httpServerProvider;
|
|
14
|
+
}
|
|
15
|
+
getExpress() {
|
|
16
|
+
if (!this.expressApp) {
|
|
17
|
+
this.expressApp = this.createExpress();
|
|
18
|
+
}
|
|
19
|
+
return this.expressApp;
|
|
20
|
+
}
|
|
21
|
+
listen() {
|
|
22
|
+
this.httpServerProvider.listen();
|
|
23
|
+
}
|
|
24
|
+
createExpress() {
|
|
25
|
+
const expressApp = express();
|
|
26
|
+
expressApp.use(bodyParser.json());
|
|
27
|
+
expressApp.use((req, res, next) => {
|
|
28
|
+
const start = process.hrtime();
|
|
29
|
+
res.on('finish', () => {
|
|
30
|
+
const [seconds, nanoseconds] = process.hrtime(start);
|
|
31
|
+
const ms = (seconds * 1000 + nanoseconds / 1e6).toFixed(2);
|
|
32
|
+
this.logger.trace(`${req.method} ${req.originalUrl} ${res.statusCode} - ${ms}ms`);
|
|
33
|
+
});
|
|
34
|
+
next();
|
|
35
|
+
});
|
|
36
|
+
const httpServer = this.httpServerProvider.getHttpServer();
|
|
37
|
+
httpServer.on('request', expressApp);
|
|
38
|
+
return expressApp;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
ExpressProvider = __decorate([
|
|
42
|
+
singleton(),
|
|
43
|
+
__metadata("design:paramtypes", [HttpServerProvider])
|
|
44
|
+
], ExpressProvider);
|
|
45
|
+
|
|
46
|
+
export { ExpressProvider };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { Logger } from '../../logger/Logger.js';
|
|
3
|
+
import { Server } from 'http';
|
|
4
|
+
import { singleton } from 'tsyringe';
|
|
5
|
+
|
|
6
|
+
let HttpServerProvider = class HttpServerProvider {
|
|
7
|
+
server = null;
|
|
8
|
+
listening = false;
|
|
9
|
+
logger = new Logger('wabot:http');
|
|
10
|
+
getHttpServer() {
|
|
11
|
+
if (!this.server) {
|
|
12
|
+
this.server = new Server();
|
|
13
|
+
}
|
|
14
|
+
return this.server;
|
|
15
|
+
}
|
|
16
|
+
listen() {
|
|
17
|
+
if (!this.server || this.listening) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.listening = true;
|
|
21
|
+
const PORT = process.env.PORT || 3000;
|
|
22
|
+
this.server.listen(PORT, () => {
|
|
23
|
+
this.logger.info(`server listenig on port ${PORT}`);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
HttpServerProvider = __decorate([
|
|
28
|
+
singleton()
|
|
29
|
+
], HttpServerProvider);
|
|
30
|
+
|
|
31
|
+
export { HttpServerProvider };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import '../../controller/channel/ChatResolver.js';
|
|
2
|
+
import '../../controller/channel/UserResolver.js';
|
|
3
|
+
import { container } from '../../injection/index.js';
|
|
4
|
+
import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
|
|
5
|
+
import { SocketChannel } from './SocketChannel.js';
|
|
6
|
+
import { SocketChannelConfig } from './SocketChannelConfig.js';
|
|
7
|
+
|
|
8
|
+
function socket(config) {
|
|
9
|
+
return function (target, propertyKey) {
|
|
10
|
+
const store = container.resolve(ControllerMetadataStore);
|
|
11
|
+
store.saveChannelMetadata({
|
|
12
|
+
channelConstructor: SocketChannel,
|
|
13
|
+
functionName: propertyKey.toString(),
|
|
14
|
+
controllerConstructor: target.constructor,
|
|
15
|
+
channelConfig: new SocketChannelConfig(config.channel),
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { socket };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { SocketChannelConfig } from './SocketChannelConfig.js';
|
|
3
|
+
import { injectable } from '../../injection/index.js';
|
|
4
|
+
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
5
|
+
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
6
|
+
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
7
|
+
import { SocketServerProvider } from './SocketServerProvider.js';
|
|
8
|
+
|
|
9
|
+
var SocketChannel_1;
|
|
10
|
+
let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
11
|
+
config;
|
|
12
|
+
socketServerProvider;
|
|
13
|
+
chatResolver;
|
|
14
|
+
userResolver;
|
|
15
|
+
callBack = null;
|
|
16
|
+
server;
|
|
17
|
+
constructor(config, socketServerProvider, chatResolver, userResolver) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
this.socketServerProvider = socketServerProvider;
|
|
20
|
+
this.chatResolver = chatResolver;
|
|
21
|
+
this.userResolver = userResolver;
|
|
22
|
+
this.server = this.socketServerProvider.getSocketServer();
|
|
23
|
+
}
|
|
24
|
+
listen(callback) {
|
|
25
|
+
this.callBack = callback;
|
|
26
|
+
}
|
|
27
|
+
connect() {
|
|
28
|
+
this.server.on('connection', (socket) => {
|
|
29
|
+
socket.on(this.config.channel, async (message) => {
|
|
30
|
+
const trimmedInput = message.text.trim();
|
|
31
|
+
if (!trimmedInput) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (!message.chatId || !message.userId || !message.senderName) {
|
|
35
|
+
socket.emit(this.config.channel, {
|
|
36
|
+
error: 'Invalid message format. chatId, userId, and senderName are required.',
|
|
37
|
+
});
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const chatConnection = {
|
|
41
|
+
id: message.chatId,
|
|
42
|
+
chatType: 'PRIVATE',
|
|
43
|
+
channelName: SocketChannel_1.name,
|
|
44
|
+
};
|
|
45
|
+
const chat = await this.chatResolver.resolve(chatConnection);
|
|
46
|
+
const userConnection = {
|
|
47
|
+
id: message.userId,
|
|
48
|
+
channelName: SocketChannel_1.name,
|
|
49
|
+
};
|
|
50
|
+
const user = await this.userResolver.resolve(userConnection);
|
|
51
|
+
if (!this.callBack)
|
|
52
|
+
return;
|
|
53
|
+
this.callBack({
|
|
54
|
+
chat,
|
|
55
|
+
user,
|
|
56
|
+
message: {
|
|
57
|
+
chatConnection,
|
|
58
|
+
userConnection,
|
|
59
|
+
text: trimmedInput,
|
|
60
|
+
senderName: message.senderName,
|
|
61
|
+
},
|
|
62
|
+
reply: (message) => {
|
|
63
|
+
socket.emit(this.config.channel, message);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
this.socketServerProvider.listen();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
SocketChannel = SocketChannel_1 = __decorate([
|
|
72
|
+
injectable(),
|
|
73
|
+
__metadata("design:paramtypes", [SocketChannelConfig,
|
|
74
|
+
SocketServerProvider,
|
|
75
|
+
ChatResolver,
|
|
76
|
+
UserResolver])
|
|
77
|
+
], SocketChannel);
|
|
78
|
+
|
|
79
|
+
export { SocketChannel };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { injectable } from '../../injection/index.js';
|
|
3
|
+
|
|
4
|
+
let SocketChannelConfig = class SocketChannelConfig {
|
|
5
|
+
channel;
|
|
6
|
+
constructor(channel) {
|
|
7
|
+
this.channel = channel;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
SocketChannelConfig = __decorate([
|
|
11
|
+
injectable(),
|
|
12
|
+
__metadata("design:paramtypes", [String])
|
|
13
|
+
], SocketChannelConfig);
|
|
14
|
+
|
|
15
|
+
export { SocketChannelConfig };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { Logger } from '../../logger/Logger.js';
|
|
3
|
+
import { HttpServerProvider } from '../http/HttpServerProvider.js';
|
|
4
|
+
import { Server } from 'socket.io';
|
|
5
|
+
import { singleton } from 'tsyringe';
|
|
6
|
+
|
|
7
|
+
let SocketServerProvider = class SocketServerProvider {
|
|
8
|
+
httpServerProvider;
|
|
9
|
+
socketServer = null;
|
|
10
|
+
logger = new Logger('wabot:socket');
|
|
11
|
+
constructor(httpServerProvider) {
|
|
12
|
+
this.httpServerProvider = httpServerProvider;
|
|
13
|
+
}
|
|
14
|
+
getSocketServer() {
|
|
15
|
+
if (!this.socketServer) {
|
|
16
|
+
this.socketServer = this.createSocketServer();
|
|
17
|
+
}
|
|
18
|
+
return this.socketServer;
|
|
19
|
+
}
|
|
20
|
+
listen() {
|
|
21
|
+
this.httpServerProvider.listen();
|
|
22
|
+
}
|
|
23
|
+
createSocketServer() {
|
|
24
|
+
const httpServer = this.httpServerProvider.getHttpServer();
|
|
25
|
+
const socketServer = new Server(httpServer, {
|
|
26
|
+
cors: {
|
|
27
|
+
origin: '*',
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
socketServer.on('connection', (socket) => {
|
|
31
|
+
this.logger.trace(`socket:${socket.id} connection`);
|
|
32
|
+
socket.onAny((event) => {
|
|
33
|
+
this.logger.trace(`socket:${socket.id} emmits ${event}`);
|
|
34
|
+
});
|
|
35
|
+
socket.on('disconnect', (reason) => {
|
|
36
|
+
this.logger.trace(`socket:${socket.id} disconnect with reason: ${reason}`);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
httpServer.on('listening', () => {
|
|
40
|
+
this.logger.info(`socket server listening`);
|
|
41
|
+
});
|
|
42
|
+
return socketServer;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
SocketServerProvider = __decorate([
|
|
46
|
+
singleton(),
|
|
47
|
+
__metadata("design:paramtypes", [HttpServerProvider])
|
|
48
|
+
], SocketServerProvider);
|
|
49
|
+
|
|
50
|
+
export { SocketServerProvider };
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { container } from '../../injection/index.js';
|
|
2
2
|
import '../../controller/channel/ChatResolver.js';
|
|
3
3
|
import '../../controller/channel/UserResolver.js';
|
|
4
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
5
|
-
import '../../core/user/IUserRepository.js';
|
|
6
4
|
import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
|
|
7
|
-
import 'express';
|
|
8
|
-
import 'socket.io';
|
|
9
5
|
import { TelegramChannel } from './TelegramChannel.js';
|
|
10
6
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
11
7
|
|
|
@@ -2,11 +2,7 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
3
3
|
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
4
4
|
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../core/user/IUserRepository.js';
|
|
7
5
|
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
8
|
-
import 'express';
|
|
9
|
-
import 'socket.io';
|
|
10
6
|
import { Bot } from 'grammy';
|
|
11
7
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
12
8
|
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { singleton } from '../../injection/index.js';
|
|
3
|
+
import { Logger } from '../../logger/Logger.js';
|
|
4
|
+
import { io } from 'socket.io-client';
|
|
5
|
+
import { devEmitEvent } from './WabotDevSocketContracts.js';
|
|
6
|
+
|
|
7
|
+
var WabotDevConnection_1;
|
|
8
|
+
let WabotDevConnection = WabotDevConnection_1 = class WabotDevConnection {
|
|
9
|
+
devProxy;
|
|
10
|
+
devProxySocket = null;
|
|
11
|
+
devToken;
|
|
12
|
+
logger = new Logger('wabot:dev-connection');
|
|
13
|
+
static isTokenAvailable() {
|
|
14
|
+
return !!process.env.WABOT_DEV_TOKEN;
|
|
15
|
+
}
|
|
16
|
+
constructor() {
|
|
17
|
+
if (!WabotDevConnection_1.isTokenAvailable()) {
|
|
18
|
+
throw new Error('WABOT_DEV_TOKEN is not set in environment variables');
|
|
19
|
+
}
|
|
20
|
+
this.devToken = process.env.WABOT_DEV_TOKEN;
|
|
21
|
+
this.devProxy = process.env.WABOT_DEV_PROXY ?? 'https://proxy.wabot.dev';
|
|
22
|
+
}
|
|
23
|
+
async getSocket() {
|
|
24
|
+
if (this.devProxySocket) {
|
|
25
|
+
return this.devProxySocket;
|
|
26
|
+
}
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const devProxySocket = io(this.devProxy, { autoConnect: false });
|
|
29
|
+
devProxySocket.on('connect', async () => {
|
|
30
|
+
try {
|
|
31
|
+
const req = {
|
|
32
|
+
token: this.devToken,
|
|
33
|
+
};
|
|
34
|
+
this.logger.debug('dev connection request');
|
|
35
|
+
const ack = await devProxySocket.emitWithAck(devEmitEvent.DEV_CONNECTION, req);
|
|
36
|
+
if (ack != 'OK') {
|
|
37
|
+
return reject(new Error('Dev connection failed'));
|
|
38
|
+
}
|
|
39
|
+
this.logger.debug('success dev connection');
|
|
40
|
+
this.devProxySocket = devProxySocket;
|
|
41
|
+
resolve(devProxySocket);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
this.logger.error(err);
|
|
45
|
+
reject(err);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
devProxySocket.connect();
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
WabotDevConnection = WabotDevConnection_1 = __decorate([
|
|
53
|
+
singleton(),
|
|
54
|
+
__metadata("design:paramtypes", [])
|
|
55
|
+
], WabotDevConnection);
|
|
56
|
+
|
|
57
|
+
export { WabotDevConnection };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const devListentEvent = {
|
|
2
|
+
DEV_WATSAPP_WEBHOOK: 'dev-whatsapp-webhook',
|
|
3
|
+
};
|
|
4
|
+
const devEmitEvent = {
|
|
5
|
+
DEV_CONNECTION: 'dev-connection',
|
|
6
|
+
DEV_SEND_WHATSAPP: 'dev-send-whatsapp',
|
|
7
|
+
DEV_SEND_WHATSAPP_TEMPLATE: 'dev-send-whatsapp-template',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export { devEmitEvent, devListentEvent };
|
|
@@ -2,11 +2,7 @@ import { container } from '../../injection/index.js';
|
|
|
2
2
|
import { WhatsappChannelConfig } from './WhatsAppChannelConfig.js';
|
|
3
3
|
import '../../controller/channel/ChatResolver.js';
|
|
4
4
|
import '../../controller/channel/UserResolver.js';
|
|
5
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../core/user/IUserRepository.js';
|
|
7
5
|
import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
|
|
8
|
-
import 'express';
|
|
9
|
-
import 'socket.io';
|
|
10
6
|
import { WhatsAppChannel } from './WhatsAppChannel.js';
|
|
11
7
|
|
|
12
8
|
function whatsapp(config) {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { singleton } from '../../injection/index.js';
|
|
3
|
+
import { WhatsApp } from './WhatsApp.js';
|
|
4
|
+
import { WabotEnv } from '../../env/WabotEnv.js';
|
|
5
|
+
|
|
6
|
+
let EnvWhatsAppRepository = class EnvWhatsAppRepository {
|
|
7
|
+
env;
|
|
8
|
+
constructor(env) {
|
|
9
|
+
this.env = env;
|
|
10
|
+
}
|
|
11
|
+
async findBySlug(slug) {
|
|
12
|
+
const whatsApp = this.getFromEnvVars();
|
|
13
|
+
if (whatsApp.getSlug() !== slug) {
|
|
14
|
+
throw new Error(`Not found WhatsApp configuration in env variables for the required slug '${slug}'`);
|
|
15
|
+
}
|
|
16
|
+
return whatsApp;
|
|
17
|
+
}
|
|
18
|
+
async findByBusinessNumber(number) {
|
|
19
|
+
const whatsApp = this.getFromEnvVars();
|
|
20
|
+
if (!whatsApp.getBussinessNumber(number)) {
|
|
21
|
+
throw new Error(`Not found WhatsApp configuration in env variables for the required number '${number}'`);
|
|
22
|
+
}
|
|
23
|
+
return whatsApp;
|
|
24
|
+
}
|
|
25
|
+
getFromEnvVars() {
|
|
26
|
+
const whatsApp = new WhatsApp({
|
|
27
|
+
verifyToken: this.env.requireString('WHATSAPP_HOOK_TOKEN'),
|
|
28
|
+
slug: this.env.requireString('WHATSAPP_HOOK_SLUG'),
|
|
29
|
+
appSecret: this.env.requireString('WHATSAPP_APP_SECRET'),
|
|
30
|
+
businessAccount: {
|
|
31
|
+
id: this.env.requireString('WHATSAPP_ACCOUNT_ID'),
|
|
32
|
+
},
|
|
33
|
+
accessToken: this.env.requireString('WHATSAPP_ACCESS_TOKEN'),
|
|
34
|
+
businessNumbers: [
|
|
35
|
+
{
|
|
36
|
+
id: this.env.requireString('WHATSAPP_BUSINESS_NUMBER_ID'),
|
|
37
|
+
number: this.env.requireString('WHATSAPP_BUSINESS_NUMBER'),
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
return whatsApp;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
EnvWhatsAppRepository = __decorate([
|
|
45
|
+
singleton(),
|
|
46
|
+
__metadata("design:paramtypes", [WabotEnv])
|
|
47
|
+
], EnvWhatsAppRepository);
|
|
48
|
+
|
|
49
|
+
export { EnvWhatsAppRepository };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { Pool } from 'pg';
|
|
3
|
+
import { WhatsApp } from './WhatsApp.js';
|
|
4
|
+
import { singleton } from '../../injection/index.js';
|
|
5
|
+
import { PgCrudRepository } from '../../repository/pg/PgCrudRepository.js';
|
|
6
|
+
|
|
7
|
+
let PgWhatsAppRepository = class PgWhatsAppRepository extends PgCrudRepository {
|
|
8
|
+
constructor(pool) {
|
|
9
|
+
super(pool, {
|
|
10
|
+
table: 'whatsapp',
|
|
11
|
+
constructor: WhatsApp,
|
|
12
|
+
schema: 'wabot',
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async findBySlug(slug) {
|
|
16
|
+
const sql = `
|
|
17
|
+
SELECT ${this.columns}
|
|
18
|
+
FROM ${this.table}
|
|
19
|
+
WHERE data @> $1
|
|
20
|
+
LIMIT 1
|
|
21
|
+
`;
|
|
22
|
+
const items = await this.query(sql, [JSON.stringify({ slug })]);
|
|
23
|
+
return items.at(0) ?? null;
|
|
24
|
+
}
|
|
25
|
+
async findByBusinessNumber(number) {
|
|
26
|
+
const sql = `
|
|
27
|
+
SELECT ${this.columns}
|
|
28
|
+
FROM ${this.table}
|
|
29
|
+
WHERE data->'businessNumbers' @> $1
|
|
30
|
+
LIMIT 1
|
|
31
|
+
`;
|
|
32
|
+
const items = await this.query(sql, [JSON.stringify([{ number }])]);
|
|
33
|
+
return items.at(0) ?? null;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
PgWhatsAppRepository = __decorate([
|
|
37
|
+
singleton(),
|
|
38
|
+
__metadata("design:paramtypes", [Pool])
|
|
39
|
+
], PgWhatsAppRepository);
|
|
40
|
+
|
|
41
|
+
export { PgWhatsAppRepository };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import '../../core/chat/repository/IChatRepository.js';
|
|
2
|
+
import { Persistent } from '../../core/Persistent.js';
|
|
3
|
+
import '../../core/user/IUserRepository.js';
|
|
4
|
+
|
|
5
|
+
class WhatsApp extends Persistent {
|
|
6
|
+
getVerifyToken() {
|
|
7
|
+
return this.data.verifyToken;
|
|
8
|
+
}
|
|
9
|
+
getSlug() {
|
|
10
|
+
return this.data.slug;
|
|
11
|
+
}
|
|
12
|
+
getBusinessAccount() {
|
|
13
|
+
return this.data.businessAccount;
|
|
14
|
+
}
|
|
15
|
+
getAppSecret() {
|
|
16
|
+
return this.data.appSecret;
|
|
17
|
+
}
|
|
18
|
+
getBusinessNumbers() {
|
|
19
|
+
return this.data.businessNumbers;
|
|
20
|
+
}
|
|
21
|
+
getAccessToken() {
|
|
22
|
+
return this.data.accessToken;
|
|
23
|
+
}
|
|
24
|
+
hasBusinessNumber(number) {
|
|
25
|
+
return this.data.businessNumbers.some((x) => x.number === number);
|
|
26
|
+
}
|
|
27
|
+
getBussinessNumber(number) {
|
|
28
|
+
return this.data.businessNumbers.find((x) => x.number === number) ?? null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { WhatsApp };
|