@wabot-dev/framework 0.1.0-beta.3 → 0.1.0-beta.31
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/chat-bot/claude/ClaudeChatAdapter.js +117 -0
- package/dist/src/addon/chat-bot/deepseek/DeepSeekChatAdapter.js +125 -0
- package/dist/src/addon/chat-bot/openia/OpenaiChatAdapter.js +101 -0
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatMemory.js +5 -4
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatRepository.js +5 -5
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatRepository.js +2 -2
- package/dist/src/addon/chat-controller/cmd/@cmd.js +22 -0
- package/dist/src/{channels → addon/chat-controller}/cmd/CmdChannel.js +4 -26
- package/dist/src/{channels → addon/chat-controller}/socket/@socket.js +8 -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 +8 -4
- package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannel.js +4 -22
- package/dist/src/{channels → addon/chat-controller}/whatsapp/@whatsapp.js +8 -4
- package/dist/src/addon/chat-controller/whatsapp/EnvWhatsAppRepository.js +49 -0
- package/dist/src/{channels/whatsapp/WhatsAppRepository.js → addon/chat-controller/whatsapp/PgWhatsAppRepository.js} +5 -5
- package/dist/src/{channels/wabot → addon/chat-controller/whatsapp}/WabotDevConnection.js +2 -2
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsApp.js +5 -4
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannel.js +6 -18
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiver.js +4 -7
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiverByDevConnection.js +3 -9
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiverByWebHook.js +2 -9
- package/dist/src/{core/chat/repository/IChatMemory.js → addon/chat-controller/whatsapp/WhatsAppRepository.js} +4 -4
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSender.js +39 -10
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSenderByCloudApi.js +10 -13
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSenderByDevConnection.js +13 -15
- package/dist/src/addon/mindset/html/HtmlModule.js +70 -0
- package/dist/src/{repository → addon/repository}/pg/PgCrudRepository.js +23 -2
- package/dist/src/{repository → addon/repository}/pg/PgRepositoryBase.js +2 -2
- package/dist/src/addon/rest-controller/jwt/@jwtGuard.js +17 -0
- package/dist/src/addon/rest-controller/jwt/Jwt.js +36 -0
- package/dist/src/addon/rest-controller/jwt/JwtAccessAndRefreshTokenDto.js +20 -0
- package/dist/src/addon/rest-controller/jwt/JwtConfig.js +28 -0
- package/dist/src/addon/rest-controller/jwt/JwtGuardMiddleware.js +50 -0
- package/dist/src/addon/rest-controller/jwt/JwtRefreshToken.js +9 -0
- package/dist/src/addon/rest-controller/jwt/JwtRefreshTokenRepository.js +21 -0
- package/dist/src/addon/rest-controller/jwt/JwtSigner.js +48 -0
- package/dist/src/addon/rest-controller/jwt/JwtTokenDto.js +22 -0
- package/dist/src/addon/validation/is-boolean/@isBoolean.js +17 -0
- package/dist/src/addon/validation/is-boolean/validateIsBoolean.js +12 -0
- package/dist/src/addon/validation/is-date/@isDate.js +17 -0
- package/dist/src/addon/validation/is-date/validateIsDate.js +16 -0
- package/dist/src/addon/validation/is-not-empty/@isNotEmpty.js +17 -0
- package/dist/src/addon/validation/is-not-empty/validateIsNotEmpty.js +12 -0
- package/dist/src/addon/validation/is-number/@isNumber.js +17 -0
- package/dist/src/addon/validation/is-number/validateIsNumber.js +12 -0
- package/dist/src/addon/validation/is-present/@isPresent.js +17 -0
- package/dist/src/addon/validation/is-present/validateIsPresent.js +12 -0
- package/dist/src/addon/validation/is-string/@isString.js +17 -0
- package/dist/src/addon/validation/is-string/validateIsString.js +12 -0
- package/dist/src/addon/validation/max/@max.js +18 -0
- package/dist/src/addon/validation/max/validateMax.js +17 -0
- package/dist/src/addon/validation/min/@min.js +18 -0
- package/dist/src/addon/validation/min/validateMin.js +17 -0
- package/dist/src/core/{Persistent.js → entity/Entity.js} +24 -9
- 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/random/Random.js +37 -0
- package/dist/src/core/storable/Storable.js +8 -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/validateArray.js +41 -0
- package/dist/src/core/validation/validators/validateIsOptional.js +5 -0
- package/dist/src/core/validation/validators/validateModel.js +34 -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 +24 -0
- package/dist/src/feature/async/JobRepository.js +31 -0
- package/dist/src/feature/async/JobRunner.js +45 -0
- package/dist/src/feature/async/JobsEventsHub.js +24 -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 +50 -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 +5 -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 +1 -1
- package/dist/src/{channels → feature}/http/HttpServerProvider.js +1 -1
- package/dist/src/{mindset → feature/mindset}/MindsetOperator.js +66 -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/rest-controller/auth/Auth.js +24 -0
- package/dist/src/feature/rest-controller/metadata/@get.js +19 -0
- package/dist/src/feature/rest-controller/metadata/@middleware.js +16 -0
- package/dist/src/feature/rest-controller/metadata/@post.js +19 -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/runRestControllers.js +75 -0
- package/dist/src/{channels → feature}/socket/SocketServerProvider.js +1 -1
- package/dist/src/index.d.ts +805 -475
- package/dist/src/index.js +118 -74
- package/package.json +6 -3
- package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +0 -106
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +0 -84
- package/dist/src/channels/cmd/@cmd.js +0 -18
- 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/user/IUserRepository.js +0 -19
- package/dist/src/core/user/User.js +0 -26
- package/dist/src/env/WabotEnv.js +0 -21
- 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/wabot → addon/chat-controller/whatsapp}/WabotDevSocketContracts.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannelConfig.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}/IMindset.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,26 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { Pool } from 'pg';
|
|
3
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
4
|
+
import { PgCrudRepository } from '../../repository/pg/PgCrudRepository.js';
|
|
5
|
+
import '../../../feature/async/CommandMetadataStore.js';
|
|
6
|
+
import '../../../feature/async/Async.js';
|
|
7
|
+
import { Job } from '../../../feature/async/Job.js';
|
|
8
|
+
import '../../../feature/async/JobRepository.js';
|
|
9
|
+
import '../../../feature/async/JobRunner.js';
|
|
10
|
+
import '../../../feature/async/JobsEventsHub.js';
|
|
11
|
+
|
|
12
|
+
let PgJobRepository = class PgJobRepository extends PgCrudRepository {
|
|
13
|
+
constructor(pool) {
|
|
14
|
+
super(pool, {
|
|
15
|
+
schema: 'wabot',
|
|
16
|
+
table: 'job',
|
|
17
|
+
constructor: Job,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
PgJobRepository = __decorate([
|
|
22
|
+
singleton(),
|
|
23
|
+
__metadata("design:paramtypes", [Pool])
|
|
24
|
+
], PgJobRepository);
|
|
25
|
+
|
|
26
|
+
export { PgJobRepository };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Logger } from '../../../core/logger/Logger.js';
|
|
2
|
+
import { Anthropic } from '@anthropic-ai/sdk';
|
|
3
|
+
|
|
4
|
+
class ClaudeChatAdapter {
|
|
5
|
+
anthropic;
|
|
6
|
+
logger = new Logger('wabot:claude-chat-adapter');
|
|
7
|
+
constructor() {
|
|
8
|
+
const apiKey = process.env.CLAUDE_API_KEY;
|
|
9
|
+
if (!apiKey) {
|
|
10
|
+
throw new Error('CLAUDE_API_KEY env variable is required');
|
|
11
|
+
}
|
|
12
|
+
this.anthropic = new Anthropic({ apiKey });
|
|
13
|
+
}
|
|
14
|
+
async nextItem(req) {
|
|
15
|
+
const tools = req.tools.map((x) => this.mapTool(x));
|
|
16
|
+
const messages = this.mapChatItems(req.prevItems);
|
|
17
|
+
const request = {
|
|
18
|
+
model: req.model,
|
|
19
|
+
max_tokens: 4096,
|
|
20
|
+
system: req.systemPrompt,
|
|
21
|
+
messages,
|
|
22
|
+
tools: tools.length > 0 ? tools : undefined,
|
|
23
|
+
};
|
|
24
|
+
this.logger.debug(`Call Claude API with Request: ${JSON.stringify(request)}`);
|
|
25
|
+
const response = await this.anthropic.messages.create(request);
|
|
26
|
+
return this.mapResponse(response);
|
|
27
|
+
}
|
|
28
|
+
mapChatItems(chatItems) {
|
|
29
|
+
const messages = [];
|
|
30
|
+
for (const chatItem of chatItems) {
|
|
31
|
+
switch (chatItem.type) {
|
|
32
|
+
case 'humanMessage':
|
|
33
|
+
messages.push(this.mapHumanMessage(chatItem.humanMessage));
|
|
34
|
+
break;
|
|
35
|
+
case 'botMessage':
|
|
36
|
+
messages.push(this.mapBotMessage(chatItem.botMessage));
|
|
37
|
+
break;
|
|
38
|
+
case 'functionCall':
|
|
39
|
+
messages.push(...this.mapFunctionCall(chatItem.functionCall));
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return messages;
|
|
44
|
+
}
|
|
45
|
+
mapHumanMessage(item) {
|
|
46
|
+
if (!item.text) {
|
|
47
|
+
throw new Error('User message content is empty');
|
|
48
|
+
}
|
|
49
|
+
return { role: 'user', content: item.text };
|
|
50
|
+
}
|
|
51
|
+
mapBotMessage(item) {
|
|
52
|
+
if (!item.text) {
|
|
53
|
+
throw new Error('Assistant message content is empty');
|
|
54
|
+
}
|
|
55
|
+
return { role: 'assistant', content: item.text };
|
|
56
|
+
}
|
|
57
|
+
mapFunctionCall(item) {
|
|
58
|
+
return [
|
|
59
|
+
{
|
|
60
|
+
role: 'assistant',
|
|
61
|
+
content: [
|
|
62
|
+
{
|
|
63
|
+
type: 'tool_use',
|
|
64
|
+
id: item.id,
|
|
65
|
+
name: item.name,
|
|
66
|
+
input: JSON.parse(item.arguments || '{}'),
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
role: 'user',
|
|
72
|
+
content: [
|
|
73
|
+
{
|
|
74
|
+
type: 'tool_result',
|
|
75
|
+
tool_use_id: item.id,
|
|
76
|
+
content: item.result || 'No result',
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
mapTool(tool) {
|
|
83
|
+
return {
|
|
84
|
+
name: tool.name,
|
|
85
|
+
description: tool.description,
|
|
86
|
+
input_schema: {
|
|
87
|
+
type: 'object',
|
|
88
|
+
properties: tool.parameters.reduce((prev, param) => ({
|
|
89
|
+
...prev,
|
|
90
|
+
[param.name]: { type: param.type, description: param.description },
|
|
91
|
+
}), {}),
|
|
92
|
+
required: tool.parameters.map((param) => param.name),
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
mapResponse(response) {
|
|
97
|
+
const content = response.content[0];
|
|
98
|
+
if (content.type === 'text') {
|
|
99
|
+
return { type: 'botMessage', botMessage: { text: content.text } };
|
|
100
|
+
}
|
|
101
|
+
else if (content.type === 'tool_use') {
|
|
102
|
+
return {
|
|
103
|
+
type: 'functionCall',
|
|
104
|
+
functionCall: {
|
|
105
|
+
id: content.id,
|
|
106
|
+
name: content.name,
|
|
107
|
+
arguments: JSON.stringify(content.input),
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
throw new Error('Not supported Claude Response');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { ClaudeChatAdapter };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Logger } from '../../../core/logger/Logger.js';
|
|
2
|
+
import { OpenAI } from 'openai';
|
|
3
|
+
|
|
4
|
+
class DeepSeekChatAdapter {
|
|
5
|
+
deepSeek;
|
|
6
|
+
logger = new Logger('wabot:deepseek-chat-adapter');
|
|
7
|
+
constructor() {
|
|
8
|
+
const apiKey = process.env.DEEPSEEK_API_KEY;
|
|
9
|
+
const baseURL = process.env.DEEPSEEK_BASE_URL;
|
|
10
|
+
if (!apiKey) {
|
|
11
|
+
throw new Error('DEEPSEEK_API_KEY env variable is required');
|
|
12
|
+
}
|
|
13
|
+
if (!baseURL) {
|
|
14
|
+
throw new Error('DEEPSEEK_BASE_URL env variable is required');
|
|
15
|
+
}
|
|
16
|
+
this.deepSeek = new OpenAI({
|
|
17
|
+
apiKey,
|
|
18
|
+
baseURL,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
async nextItem(req) {
|
|
22
|
+
const deepSeekInput = [];
|
|
23
|
+
deepSeekInput.push({ role: 'system', content: req.systemPrompt });
|
|
24
|
+
deepSeekInput.push(...this.mapChatItems(req.prevItems));
|
|
25
|
+
const tools = req.tools.map((x) => this.mapTool(x));
|
|
26
|
+
const response = await this.deepSeek.chat.completions.create({
|
|
27
|
+
model: req.model,
|
|
28
|
+
messages: deepSeekInput,
|
|
29
|
+
tools,
|
|
30
|
+
tool_choice: 'auto',
|
|
31
|
+
});
|
|
32
|
+
return this.mapResponse(response);
|
|
33
|
+
}
|
|
34
|
+
mapChatItems(chatItems) {
|
|
35
|
+
const deepSeekInput = [];
|
|
36
|
+
for (const chatItem of chatItems) {
|
|
37
|
+
switch (chatItem.type) {
|
|
38
|
+
case 'humanMessage':
|
|
39
|
+
deepSeekInput.push(this.mapHumanMessage(chatItem.humanMessage));
|
|
40
|
+
break;
|
|
41
|
+
case 'botMessage':
|
|
42
|
+
deepSeekInput.push(this.mapBotMessage(chatItem.botMessage));
|
|
43
|
+
break;
|
|
44
|
+
case 'functionCall':
|
|
45
|
+
deepSeekInput.push(...this.mapFunctionCall(chatItem.functionCall));
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return deepSeekInput;
|
|
50
|
+
}
|
|
51
|
+
mapHumanMessage(item) {
|
|
52
|
+
if (!item.text) {
|
|
53
|
+
throw new Error('User message content is empty');
|
|
54
|
+
}
|
|
55
|
+
return { role: 'user', content: item.text };
|
|
56
|
+
}
|
|
57
|
+
mapBotMessage(item) {
|
|
58
|
+
if (!item.text) {
|
|
59
|
+
throw new Error('Assistant message content is empty');
|
|
60
|
+
}
|
|
61
|
+
return { role: 'assistant', content: item.text };
|
|
62
|
+
}
|
|
63
|
+
mapFunctionCall(item) {
|
|
64
|
+
return [
|
|
65
|
+
{
|
|
66
|
+
role: 'assistant',
|
|
67
|
+
tool_calls: [
|
|
68
|
+
{
|
|
69
|
+
id: item.id,
|
|
70
|
+
type: 'function',
|
|
71
|
+
function: {
|
|
72
|
+
name: item.name,
|
|
73
|
+
arguments: item.arguments || '{}',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
role: 'tool',
|
|
80
|
+
tool_call_id: item.id,
|
|
81
|
+
content: item.result ?? 'No result',
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
}
|
|
85
|
+
mapTool(tool) {
|
|
86
|
+
return {
|
|
87
|
+
type: 'function',
|
|
88
|
+
function: {
|
|
89
|
+
name: tool.name,
|
|
90
|
+
description: tool.description,
|
|
91
|
+
parameters: {
|
|
92
|
+
type: 'object',
|
|
93
|
+
properties: tool.parameters.reduce((prev, param) => ({
|
|
94
|
+
...prev,
|
|
95
|
+
[param.name]: { type: param.type, description: param.description },
|
|
96
|
+
}), {}),
|
|
97
|
+
required: tool.parameters.map((param) => param.name),
|
|
98
|
+
additionalProperties: false,
|
|
99
|
+
},
|
|
100
|
+
strict: true,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
mapResponse(response) {
|
|
105
|
+
const { tool_calls: responseFunctionCall, content: responseText } = response.choices?.[0]?.message ?? {};
|
|
106
|
+
if (responseText) {
|
|
107
|
+
return { type: 'botMessage', botMessage: { text: responseText } };
|
|
108
|
+
}
|
|
109
|
+
else if (responseFunctionCall && responseFunctionCall[0]?.type === 'function') {
|
|
110
|
+
return {
|
|
111
|
+
type: 'functionCall',
|
|
112
|
+
functionCall: {
|
|
113
|
+
id: responseFunctionCall[0].id,
|
|
114
|
+
name: responseFunctionCall[0].function.name,
|
|
115
|
+
arguments: responseFunctionCall[0].function.arguments,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
throw new Error('Not supported DeepSeek Response');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export { DeepSeekChatAdapter };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Logger } from '../../../core/logger/Logger.js';
|
|
2
|
+
import { OpenAI } from 'openai';
|
|
3
|
+
|
|
4
|
+
class OpenaiChatAdapter {
|
|
5
|
+
openai = new OpenAI();
|
|
6
|
+
logger = new Logger('wabot:openai-chat-adapter');
|
|
7
|
+
async nextItem(req) {
|
|
8
|
+
const openIaInput = [];
|
|
9
|
+
openIaInput.push({ role: 'system', content: req.systemPrompt });
|
|
10
|
+
openIaInput.push(...this.mapChatItems(req.prevItems));
|
|
11
|
+
const tools = req.tools.map((x) => this.mapTool(x));
|
|
12
|
+
const response = await this.openai.responses.create({
|
|
13
|
+
model: req.model,
|
|
14
|
+
input: openIaInput,
|
|
15
|
+
tools,
|
|
16
|
+
});
|
|
17
|
+
return this.mapResponse(response);
|
|
18
|
+
}
|
|
19
|
+
mapChatItems(chatItems) {
|
|
20
|
+
const openIaInput = [];
|
|
21
|
+
for (const chatItem of chatItems) {
|
|
22
|
+
switch (chatItem.type) {
|
|
23
|
+
case 'humanMessage':
|
|
24
|
+
openIaInput.push(this.mapConectionMessage(chatItem.humanMessage));
|
|
25
|
+
break;
|
|
26
|
+
case 'botMessage':
|
|
27
|
+
openIaInput.push(this.mapBotMessage(chatItem.botMessage));
|
|
28
|
+
break;
|
|
29
|
+
case 'functionCall':
|
|
30
|
+
openIaInput.push(...this.mapFunctionCall(chatItem.functionCall));
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return openIaInput;
|
|
35
|
+
}
|
|
36
|
+
mapConectionMessage(item) {
|
|
37
|
+
if (!item.text) {
|
|
38
|
+
throw new Error('System message content is empty');
|
|
39
|
+
}
|
|
40
|
+
return { role: 'user', content: item.text };
|
|
41
|
+
}
|
|
42
|
+
mapBotMessage(item) {
|
|
43
|
+
if (!item.text) {
|
|
44
|
+
throw new Error('System message content is empty');
|
|
45
|
+
}
|
|
46
|
+
return { role: 'assistant', content: item.text };
|
|
47
|
+
}
|
|
48
|
+
mapFunctionCall(item) {
|
|
49
|
+
return [
|
|
50
|
+
{
|
|
51
|
+
type: 'function_call',
|
|
52
|
+
call_id: item.id,
|
|
53
|
+
name: item.name,
|
|
54
|
+
arguments: JSON.stringify(item.arguments),
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
type: 'function_call_output',
|
|
58
|
+
call_id: item.id,
|
|
59
|
+
output: item.result ?? 'Not result',
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
mapTool(tool) {
|
|
64
|
+
return {
|
|
65
|
+
type: 'function',
|
|
66
|
+
name: tool.name,
|
|
67
|
+
description: tool.description,
|
|
68
|
+
parameters: {
|
|
69
|
+
type: 'object',
|
|
70
|
+
properties: tool.parameters.reduce((prev, param) => ({
|
|
71
|
+
...prev,
|
|
72
|
+
[param.name]: { type: param.type, description: param.description },
|
|
73
|
+
}), {}),
|
|
74
|
+
required: tool.parameters.map((param) => param.name),
|
|
75
|
+
},
|
|
76
|
+
strict: true,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
mapResponse(response) {
|
|
80
|
+
let newItem;
|
|
81
|
+
if (response.output_text) {
|
|
82
|
+
newItem = { type: 'botMessage', botMessage: { text: response.output_text } };
|
|
83
|
+
}
|
|
84
|
+
else if (response.output && response.output[0]?.type == 'function_call') {
|
|
85
|
+
newItem = {
|
|
86
|
+
type: 'functionCall',
|
|
87
|
+
functionCall: {
|
|
88
|
+
id: response.output[0].call_id,
|
|
89
|
+
name: response.output[0].name,
|
|
90
|
+
arguments: response.output[0].arguments,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
throw new Error('Not supported OpenIA Response');
|
|
96
|
+
}
|
|
97
|
+
return newItem;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { OpenaiChatAdapter };
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import { ChatItem } from '
|
|
3
|
-
import '
|
|
4
|
-
import
|
|
1
|
+
import { PgCrudRepository } from '../../repository/pg/PgCrudRepository.js';
|
|
2
|
+
import { ChatItem } from '../../../feature/chat-bot/ChatItem.js';
|
|
3
|
+
import '../../../core/injection/index.js';
|
|
4
|
+
import 'uuid';
|
|
5
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
5
6
|
|
|
6
7
|
class PgChatMemory extends PgCrudRepository {
|
|
7
8
|
chatId;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import '../../../../core/chat/repository/IChatRepository.js';
|
|
3
|
-
import { Chat } from '../../../../core/chat/Chat.js';
|
|
4
|
-
import '../../../../core/user/IUserRepository.js';
|
|
5
|
-
import { singleton } from '../../../../injection/index.js';
|
|
6
2
|
import { Pool } from 'pg';
|
|
7
3
|
import { PgChatMemory } from './PgChatMemory.js';
|
|
8
|
-
import {
|
|
4
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
5
|
+
import { PgCrudRepository } from '../../repository/pg/PgCrudRepository.js';
|
|
6
|
+
import { Chat } from '../../../feature/chat-bot/Chat.js';
|
|
7
|
+
import 'uuid';
|
|
8
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
9
9
|
|
|
10
10
|
let PgChatRepository = class PgChatRepository extends PgCrudRepository {
|
|
11
11
|
constructor(pool) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
2
|
import { v4 } from 'uuid';
|
|
3
3
|
import { RamChatMemory } from './RamChatMemory.js';
|
|
4
|
-
import { singleton } from '
|
|
4
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
5
5
|
|
|
6
6
|
let RamChatRepository = class RamChatRepository {
|
|
7
7
|
items = [];
|
|
@@ -16,7 +16,7 @@ let RamChatRepository = class RamChatRepository {
|
|
|
16
16
|
this.items.push(chat);
|
|
17
17
|
const memory = {
|
|
18
18
|
memory: new RamChatMemory(),
|
|
19
|
-
chatId: chat.
|
|
19
|
+
chatId: chat.id,
|
|
20
20
|
};
|
|
21
21
|
this.memories.push(memory);
|
|
22
22
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
|
|
3
|
+
import '../../../feature/chat-controller/ChatResolver.js';
|
|
4
|
+
import 'uuid';
|
|
5
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
6
|
+
import 'reflect-metadata';
|
|
7
|
+
import '../../../feature/mindset/metadata/MindsetMetadataStore.js';
|
|
8
|
+
import '../../../feature/mindset/MindsetOperator.js';
|
|
9
|
+
import { CmdChannel } from './CmdChannel.js';
|
|
10
|
+
|
|
11
|
+
function cmd() {
|
|
12
|
+
return function (target, propertyKey) {
|
|
13
|
+
const store = container.resolve(ControllerMetadataStore);
|
|
14
|
+
store.saveChannelMetadata({
|
|
15
|
+
channelConstructor: CmdChannel,
|
|
16
|
+
functionName: propertyKey.toString(),
|
|
17
|
+
controllerConstructor: target.constructor,
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { cmd };
|
|
@@ -1,25 +1,15 @@
|
|
|
1
|
-
import { __decorate
|
|
2
|
-
import {
|
|
3
|
-
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
4
|
-
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
6
3
|
import * as readline from 'readline';
|
|
7
4
|
|
|
8
5
|
var CmdChannel_1;
|
|
9
6
|
const chatId = 'cmd';
|
|
10
|
-
const userId = 'cmd';
|
|
11
7
|
let CmdChannel = CmdChannel_1 = class CmdChannel {
|
|
12
|
-
chatResolver;
|
|
13
|
-
userResolver;
|
|
14
8
|
rl = readline.createInterface({
|
|
15
9
|
input: process.stdin,
|
|
16
10
|
output: process.stdout,
|
|
17
11
|
});
|
|
18
12
|
callBack = null;
|
|
19
|
-
constructor(chatResolver, userResolver) {
|
|
20
|
-
this.chatResolver = chatResolver;
|
|
21
|
-
this.userResolver = userResolver;
|
|
22
|
-
}
|
|
23
13
|
listen(callback) {
|
|
24
14
|
this.callBack = callback;
|
|
25
15
|
}
|
|
@@ -39,22 +29,12 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
|
|
|
39
29
|
chatType: 'PRIVATE',
|
|
40
30
|
channelName: CmdChannel_1.name,
|
|
41
31
|
};
|
|
42
|
-
const chat = await this.chatResolver.resolve(chatConnection);
|
|
43
|
-
const userConnection = {
|
|
44
|
-
id: userId,
|
|
45
|
-
channelName: CmdChannel_1.name,
|
|
46
|
-
};
|
|
47
|
-
const user = await this.userResolver.resolve(userConnection);
|
|
48
32
|
if (!this.callBack)
|
|
49
33
|
return;
|
|
50
34
|
this.callBack({
|
|
51
|
-
|
|
52
|
-
user,
|
|
35
|
+
chatConnection,
|
|
53
36
|
message: {
|
|
54
|
-
chatConnection,
|
|
55
|
-
userConnection,
|
|
56
37
|
text: trimmedInput,
|
|
57
|
-
senderName: 'cmd',
|
|
58
38
|
},
|
|
59
39
|
reply: (message) => {
|
|
60
40
|
console.log(`\n[${message.senderName}]: ${message.text}\n`);
|
|
@@ -65,9 +45,7 @@ let CmdChannel = CmdChannel_1 = class CmdChannel {
|
|
|
65
45
|
}
|
|
66
46
|
};
|
|
67
47
|
CmdChannel = CmdChannel_1 = __decorate([
|
|
68
|
-
injectable()
|
|
69
|
-
__metadata("design:paramtypes", [ChatResolver,
|
|
70
|
-
UserResolver])
|
|
48
|
+
injectable()
|
|
71
49
|
], CmdChannel);
|
|
72
50
|
|
|
73
51
|
export { CmdChannel };
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import '
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
|
|
3
|
+
import '../../../feature/chat-controller/ChatResolver.js';
|
|
4
|
+
import 'uuid';
|
|
5
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
6
|
+
import 'reflect-metadata';
|
|
7
|
+
import '../../../feature/mindset/metadata/MindsetMetadataStore.js';
|
|
8
|
+
import '../../../feature/mindset/MindsetOperator.js';
|
|
5
9
|
import { SocketChannel } from './SocketChannel.js';
|
|
6
10
|
import { SocketChannelConfig } from './SocketChannelConfig.js';
|
|
7
11
|
|
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
3
|
+
import { SocketServerProvider } from '../../../feature/socket/SocketServerProvider.js';
|
|
2
4
|
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
5
|
|
|
9
6
|
var SocketChannel_1;
|
|
10
7
|
let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
11
8
|
config;
|
|
12
9
|
socketServerProvider;
|
|
13
|
-
chatResolver;
|
|
14
|
-
userResolver;
|
|
15
10
|
callBack = null;
|
|
16
11
|
server;
|
|
17
|
-
constructor(config, socketServerProvider
|
|
12
|
+
constructor(config, socketServerProvider) {
|
|
18
13
|
this.config = config;
|
|
19
14
|
this.socketServerProvider = socketServerProvider;
|
|
20
|
-
this.chatResolver = chatResolver;
|
|
21
|
-
this.userResolver = userResolver;
|
|
22
15
|
this.server = this.socketServerProvider.getSocketServer();
|
|
23
16
|
}
|
|
24
17
|
listen(callback) {
|
|
@@ -42,20 +35,11 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
|
42
35
|
chatType: 'PRIVATE',
|
|
43
36
|
channelName: SocketChannel_1.name,
|
|
44
37
|
};
|
|
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
38
|
if (!this.callBack)
|
|
52
39
|
return;
|
|
53
40
|
this.callBack({
|
|
54
|
-
|
|
55
|
-
user,
|
|
41
|
+
chatConnection,
|
|
56
42
|
message: {
|
|
57
|
-
chatConnection,
|
|
58
|
-
userConnection,
|
|
59
43
|
text: trimmedInput,
|
|
60
44
|
senderName: message.senderName,
|
|
61
45
|
},
|
|
@@ -71,9 +55,7 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
|
71
55
|
SocketChannel = SocketChannel_1 = __decorate([
|
|
72
56
|
injectable(),
|
|
73
57
|
__metadata("design:paramtypes", [SocketChannelConfig,
|
|
74
|
-
SocketServerProvider
|
|
75
|
-
ChatResolver,
|
|
76
|
-
UserResolver])
|
|
58
|
+
SocketServerProvider])
|
|
77
59
|
], SocketChannel);
|
|
78
60
|
|
|
79
61
|
export { SocketChannel };
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { container } from '
|
|
2
|
-
import '
|
|
3
|
-
import '
|
|
4
|
-
import
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
|
|
3
|
+
import '../../../feature/chat-controller/ChatResolver.js';
|
|
4
|
+
import 'uuid';
|
|
5
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
6
|
+
import 'reflect-metadata';
|
|
7
|
+
import '../../../feature/mindset/metadata/MindsetMetadataStore.js';
|
|
8
|
+
import '../../../feature/mindset/MindsetOperator.js';
|
|
5
9
|
import { TelegramChannel } from './TelegramChannel.js';
|
|
6
10
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
7
11
|
|
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
3
|
-
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
4
|
-
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
6
2
|
import { Bot } from 'grammy';
|
|
7
3
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
4
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
8
5
|
|
|
9
6
|
var TelegramChannel_1;
|
|
10
7
|
let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
|
|
11
8
|
config;
|
|
12
|
-
chatResolver;
|
|
13
|
-
userResolver;
|
|
14
9
|
bot;
|
|
15
|
-
constructor(config
|
|
10
|
+
constructor(config) {
|
|
16
11
|
this.config = config;
|
|
17
|
-
this.chatResolver = chatResolver;
|
|
18
|
-
this.userResolver = userResolver;
|
|
19
12
|
this.bot = new Bot(this.config.botToken);
|
|
20
13
|
}
|
|
21
14
|
listen(callback) {
|
|
@@ -28,18 +21,9 @@ let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
|
|
|
28
21
|
chatType: ctx.message.chat.type === 'private' ? 'PRIVATE' : 'GROUP',
|
|
29
22
|
channelName: TelegramChannel_1.name,
|
|
30
23
|
};
|
|
31
|
-
const chat = await this.chatResolver.resolve(chatConnection);
|
|
32
|
-
const userConnection = {
|
|
33
|
-
id: ctx.from.id.toString(),
|
|
34
|
-
channelName: TelegramChannel_1.name,
|
|
35
|
-
};
|
|
36
|
-
const user = await this.userResolver.resolve(userConnection);
|
|
37
24
|
callback({
|
|
38
|
-
|
|
39
|
-
user,
|
|
25
|
+
chatConnection,
|
|
40
26
|
message: {
|
|
41
|
-
chatConnection,
|
|
42
|
-
userConnection,
|
|
43
27
|
senderName: ctx.from.first_name,
|
|
44
28
|
text: ctx.message.text,
|
|
45
29
|
},
|
|
@@ -55,9 +39,7 @@ let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
|
|
|
55
39
|
};
|
|
56
40
|
TelegramChannel = TelegramChannel_1 = __decorate([
|
|
57
41
|
injectable(),
|
|
58
|
-
__metadata("design:paramtypes", [TelegramChannelConfig
|
|
59
|
-
ChatResolver,
|
|
60
|
-
UserResolver])
|
|
42
|
+
__metadata("design:paramtypes", [TelegramChannelConfig])
|
|
61
43
|
], TelegramChannel);
|
|
62
44
|
|
|
63
45
|
export { TelegramChannel };
|