@wabot-dev/framework 0.1.0-beta.4 → 0.1.0-beta.40
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/anthropic/AnthropicChatAdapter.js +128 -0
- package/dist/src/addon/chat-bot/deepseek/DeepSeekChatAdapter.js +137 -0
- package/dist/src/addon/chat-bot/google/GoogleChatAdapter.js +120 -0
- package/dist/src/addon/chat-bot/openia/OpenaiChatAdapter.js +111 -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/{channels → addon/chat-controller}/whatsapp/EnvWhatsAppRepository.js +3 -3
- package/dist/src/{channels → addon/chat-controller}/whatsapp/PgWhatsAppRepository.js +1 -1
- package/dist/src/{channels/wabot → addon/chat-controller/whatsapp}/WabotDevConnection.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/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiver.js +4 -7
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiverByDevConnection.js +4 -10
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiverByWebHook.js +6 -12
- 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/addon/money/MoneyDto.js +22 -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 +31 -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 +45 -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/auth/Auth.js +24 -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 +51 -0
- package/dist/src/core/validation/validators/validateIsOptional.js +5 -0
- package/dist/src/core/validation/validators/validateModel.js +36 -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 +2 -4
- package/dist/src/{channels → feature}/http/HttpServerProvider.js +2 -2
- 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/money/Money.js +61 -0
- package/dist/src/feature/rest-controller/injection-tokens.js +4 -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 +88 -0
- package/dist/src/{channels → feature}/socket/SocketServerProvider.js +2 -2
- package/dist/src/index.d.ts +848 -483
- package/dist/src/index.js +122 -76
- package/package.json +9 -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/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/wabot → addon/chat-controller/whatsapp}/WabotDevSocketContracts.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}/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,128 @@
|
|
|
1
|
+
import { Logger } from '../../../core/logger/Logger.js';
|
|
2
|
+
import { Anthropic } from '@anthropic-ai/sdk';
|
|
3
|
+
|
|
4
|
+
class AnthropicChatAdapter {
|
|
5
|
+
env;
|
|
6
|
+
anthropic;
|
|
7
|
+
logger = new Logger('wabot:anthropic-chat-adapter');
|
|
8
|
+
constructor(env) {
|
|
9
|
+
this.env = env;
|
|
10
|
+
const apiKey = this.env.requireString('ANTHROPIC_API_KEY');
|
|
11
|
+
this.anthropic = new Anthropic({ apiKey });
|
|
12
|
+
}
|
|
13
|
+
async nextItem(req) {
|
|
14
|
+
const tools = req.tools.map((x) => this.mapTool(x));
|
|
15
|
+
const messages = this.mapChatItems(req.prevItems);
|
|
16
|
+
const request = {
|
|
17
|
+
model: req.model,
|
|
18
|
+
max_tokens: 4096,
|
|
19
|
+
system: req.systemPrompt,
|
|
20
|
+
messages,
|
|
21
|
+
tools: tools.length > 0 ? tools : undefined,
|
|
22
|
+
};
|
|
23
|
+
this.logger.debug(`Call Claude API with Request: ${JSON.stringify(request)}`);
|
|
24
|
+
const response = await this.anthropic.messages.create(request);
|
|
25
|
+
return this.mapResponse(response);
|
|
26
|
+
}
|
|
27
|
+
mapChatItems(chatItems) {
|
|
28
|
+
const messages = [];
|
|
29
|
+
for (const chatItem of chatItems) {
|
|
30
|
+
switch (chatItem.type) {
|
|
31
|
+
case 'humanMessage':
|
|
32
|
+
messages.push(this.mapHumanMessage(chatItem.humanMessage));
|
|
33
|
+
break;
|
|
34
|
+
case 'botMessage':
|
|
35
|
+
messages.push(this.mapBotMessage(chatItem.botMessage));
|
|
36
|
+
break;
|
|
37
|
+
case 'functionCall':
|
|
38
|
+
messages.push(...this.mapFunctionCall(chatItem.functionCall));
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return messages;
|
|
43
|
+
}
|
|
44
|
+
mapHumanMessage(item) {
|
|
45
|
+
if (!item.text) {
|
|
46
|
+
throw new Error('User message content is empty');
|
|
47
|
+
}
|
|
48
|
+
return { role: 'user', content: item.text };
|
|
49
|
+
}
|
|
50
|
+
mapBotMessage(item) {
|
|
51
|
+
if (!item.text) {
|
|
52
|
+
throw new Error('Assistant message content is empty');
|
|
53
|
+
}
|
|
54
|
+
return { role: 'assistant', content: item.text };
|
|
55
|
+
}
|
|
56
|
+
mapFunctionCall(item) {
|
|
57
|
+
return [
|
|
58
|
+
{
|
|
59
|
+
role: 'assistant',
|
|
60
|
+
content: [
|
|
61
|
+
{
|
|
62
|
+
type: 'tool_use',
|
|
63
|
+
id: item.id,
|
|
64
|
+
name: item.name,
|
|
65
|
+
input: JSON.parse(item.arguments || '{}'),
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
role: 'user',
|
|
71
|
+
content: [
|
|
72
|
+
{
|
|
73
|
+
type: 'tool_result',
|
|
74
|
+
tool_use_id: item.id,
|
|
75
|
+
content: item.result || 'No result',
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
mapTool(tool) {
|
|
82
|
+
return {
|
|
83
|
+
name: tool.name,
|
|
84
|
+
description: tool.description,
|
|
85
|
+
input_schema: {
|
|
86
|
+
type: 'object',
|
|
87
|
+
properties: tool.parameters.reduce((prev, param) => ({
|
|
88
|
+
...prev,
|
|
89
|
+
[param.name]: { type: param.type, description: param.description },
|
|
90
|
+
}), {}),
|
|
91
|
+
required: tool.parameters.map((param) => param.name),
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
mapResponse(response) {
|
|
96
|
+
let chatItem;
|
|
97
|
+
const content = response.content[0];
|
|
98
|
+
if (content.type === 'text') {
|
|
99
|
+
chatItem = { type: 'botMessage', botMessage: { text: content.text } };
|
|
100
|
+
}
|
|
101
|
+
else if (content.type === 'tool_use') {
|
|
102
|
+
chatItem = {
|
|
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
|
+
let usage;
|
|
115
|
+
if (response.usage) {
|
|
116
|
+
usage = {
|
|
117
|
+
inputTokens: response.usage.input_tokens,
|
|
118
|
+
outputTokens: response.usage.output_tokens,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
throw new Error('Unable to found usage info');
|
|
123
|
+
}
|
|
124
|
+
return { chatItem, usage };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { AnthropicChatAdapter };
|
|
@@ -0,0 +1,137 @@
|
|
|
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
|
+
let chatItem;
|
|
106
|
+
const { tool_calls: responseFunctionCall, content: responseText } = response.choices?.[0]?.message ?? {};
|
|
107
|
+
if (responseText) {
|
|
108
|
+
chatItem = { type: 'botMessage', botMessage: { text: responseText } };
|
|
109
|
+
}
|
|
110
|
+
else if (responseFunctionCall && responseFunctionCall[0]?.type === 'function') {
|
|
111
|
+
chatItem = {
|
|
112
|
+
type: 'functionCall',
|
|
113
|
+
functionCall: {
|
|
114
|
+
id: responseFunctionCall[0].id,
|
|
115
|
+
name: responseFunctionCall[0].function.name,
|
|
116
|
+
arguments: responseFunctionCall[0].function.arguments,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
throw new Error('Not supported DeepSeek Response');
|
|
122
|
+
}
|
|
123
|
+
let usage;
|
|
124
|
+
if (response.usage) {
|
|
125
|
+
usage = {
|
|
126
|
+
inputTokens: response.usage.prompt_tokens,
|
|
127
|
+
outputTokens: response.usage.completion_tokens,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
throw new Error('Unable to found usage info');
|
|
132
|
+
}
|
|
133
|
+
return { chatItem, usage };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export { DeepSeekChatAdapter };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Logger } from '../../../core/logger/Logger.js';
|
|
2
|
+
import { GoogleGenAI } from '@google/genai';
|
|
3
|
+
|
|
4
|
+
class GoogleChatAdapter {
|
|
5
|
+
env;
|
|
6
|
+
genai;
|
|
7
|
+
logger = new Logger('wabot:google-chat-adapter');
|
|
8
|
+
constructor(env) {
|
|
9
|
+
this.env = env;
|
|
10
|
+
const apiKey = this.env.requireString('GOOGLE_API_KEY');
|
|
11
|
+
this.genai = new GoogleGenAI({ apiKey });
|
|
12
|
+
}
|
|
13
|
+
async nextItem(req) {
|
|
14
|
+
const contents = this.buildContents(req.prevItems, req.systemPrompt);
|
|
15
|
+
const tools = req.tools.length > 0 ? req.tools.map(this.mapTool) : undefined;
|
|
16
|
+
const request = { model: req.model, contents, tools };
|
|
17
|
+
this.logger.debug(`Call Gemini API with Request: ${JSON.stringify(request)}`);
|
|
18
|
+
const response = await this.genai.models.generateContent(request);
|
|
19
|
+
return this.mapResponse(response);
|
|
20
|
+
}
|
|
21
|
+
buildContents(chatItems, systemPrompt) {
|
|
22
|
+
const contents = [];
|
|
23
|
+
if (systemPrompt) {
|
|
24
|
+
contents.push({ role: 'user', parts: [{ text: systemPrompt }] }, { role: 'model', parts: [{ text: 'I understand. I will follow these instructions.' }] });
|
|
25
|
+
}
|
|
26
|
+
chatItems.forEach((chatItem) => {
|
|
27
|
+
switch (chatItem.type) {
|
|
28
|
+
case 'humanMessage':
|
|
29
|
+
this.validateMessageContent(chatItem.humanMessage.text, 'User');
|
|
30
|
+
contents.push({ role: 'user', parts: [{ text: chatItem.humanMessage.text }] });
|
|
31
|
+
break;
|
|
32
|
+
case 'botMessage':
|
|
33
|
+
this.validateMessageContent(chatItem.botMessage.text, 'Assistant');
|
|
34
|
+
contents.push({ role: 'model', parts: [{ text: chatItem.botMessage.text }] });
|
|
35
|
+
break;
|
|
36
|
+
case 'functionCall':
|
|
37
|
+
contents.push(...this.mapFunctionCall(chatItem.functionCall));
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return contents;
|
|
42
|
+
}
|
|
43
|
+
validateMessageContent(text, messageType) {
|
|
44
|
+
if (!text) {
|
|
45
|
+
throw new Error(`${messageType} message content is empty`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
mapFunctionCall(item) {
|
|
49
|
+
const args = JSON.parse(item.arguments || '{}');
|
|
50
|
+
return [
|
|
51
|
+
{
|
|
52
|
+
role: 'model',
|
|
53
|
+
parts: [{ functionCall: { name: item.name, args } }],
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
role: 'user',
|
|
57
|
+
parts: [
|
|
58
|
+
{
|
|
59
|
+
functionResponse: { name: item.name, response: { result: item.result || 'No result' } },
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
mapTool = (tool) => ({
|
|
66
|
+
functionDeclarations: [
|
|
67
|
+
{
|
|
68
|
+
name: tool.name,
|
|
69
|
+
description: tool.description,
|
|
70
|
+
parameters: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: tool.parameters.reduce((acc, param) => ({
|
|
73
|
+
...acc,
|
|
74
|
+
[param.name]: { type: param.type, description: param.description },
|
|
75
|
+
}), {}),
|
|
76
|
+
required: tool.parameters.map((param) => param.name),
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
mapResponse(response) {
|
|
82
|
+
this.validateResponse(response);
|
|
83
|
+
const part = response.response.candidates[0].content.parts[0];
|
|
84
|
+
const chatItem = part.text
|
|
85
|
+
? { type: 'botMessage', botMessage: { text: part.text } }
|
|
86
|
+
: part.functionCall
|
|
87
|
+
? {
|
|
88
|
+
type: 'functionCall',
|
|
89
|
+
functionCall: {
|
|
90
|
+
id: `gemini_${Date.now()}`,
|
|
91
|
+
name: part.functionCall.name,
|
|
92
|
+
arguments: JSON.stringify(part.functionCall.args || {}),
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
: (() => {
|
|
96
|
+
throw new Error('Not supported Gemini Response');
|
|
97
|
+
})();
|
|
98
|
+
const usage = {
|
|
99
|
+
inputTokens: response.response.usageMetadata?.promptTokenCount || 0,
|
|
100
|
+
outputTokens: response.response.usageMetadata?.candidatesTokenCount || 0,
|
|
101
|
+
};
|
|
102
|
+
if (!response.response.usageMetadata) {
|
|
103
|
+
throw new Error('Unable to found usage info');
|
|
104
|
+
}
|
|
105
|
+
return { chatItem, usage };
|
|
106
|
+
}
|
|
107
|
+
validateResponse(response) {
|
|
108
|
+
if (!response.response) {
|
|
109
|
+
throw new Error('Invalid Gemini response structure');
|
|
110
|
+
}
|
|
111
|
+
if (!response.response.candidates?.[0]) {
|
|
112
|
+
throw new Error('No candidates in Gemini response');
|
|
113
|
+
}
|
|
114
|
+
if (!response.response.candidates[0].content?.parts?.[0]) {
|
|
115
|
+
throw new Error('No parts in Gemini response');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { GoogleChatAdapter };
|
|
@@ -0,0 +1,111 @@
|
|
|
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 chatItem;
|
|
81
|
+
if (response.output_text) {
|
|
82
|
+
chatItem = { type: 'botMessage', botMessage: { text: response.output_text } };
|
|
83
|
+
}
|
|
84
|
+
else if (response.output && response.output[0]?.type == 'function_call') {
|
|
85
|
+
chatItem = {
|
|
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
|
+
let usage;
|
|
98
|
+
if (response.usage) {
|
|
99
|
+
usage = {
|
|
100
|
+
inputTokens: response.usage.input_tokens,
|
|
101
|
+
outputTokens: response.usage.output_tokens,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
throw new Error('Unable to found usage info');
|
|
106
|
+
}
|
|
107
|
+
return { chatItem, usage };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
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
|
|