@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,380 @@
|
|
|
1
|
+
import { parse1, normalize, compareSpecificity, serialize } from '../../parseley/lib/parseley.js';
|
|
2
|
+
|
|
3
|
+
class DecisionTree {
|
|
4
|
+
constructor(input) {
|
|
5
|
+
this.branches = weave(toAstTerminalPairs(input));
|
|
6
|
+
}
|
|
7
|
+
build(builder) {
|
|
8
|
+
return builder(this.branches);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function toAstTerminalPairs(array) {
|
|
12
|
+
const len = array.length;
|
|
13
|
+
const results = new Array(len);
|
|
14
|
+
for (let i = 0; i < len; i++) {
|
|
15
|
+
const [selectorString, val] = array[i];
|
|
16
|
+
const ast = preprocess(parse1(selectorString));
|
|
17
|
+
results[i] = {
|
|
18
|
+
ast: ast,
|
|
19
|
+
terminal: {
|
|
20
|
+
type: 'terminal',
|
|
21
|
+
valueContainer: { index: i, value: val, specificity: ast.specificity }
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return results;
|
|
26
|
+
}
|
|
27
|
+
function preprocess(ast) {
|
|
28
|
+
reduceSelectorVariants(ast);
|
|
29
|
+
normalize(ast);
|
|
30
|
+
return ast;
|
|
31
|
+
}
|
|
32
|
+
function reduceSelectorVariants(ast) {
|
|
33
|
+
const newList = [];
|
|
34
|
+
ast.list.forEach(sel => {
|
|
35
|
+
switch (sel.type) {
|
|
36
|
+
case 'class':
|
|
37
|
+
newList.push({
|
|
38
|
+
matcher: '~=',
|
|
39
|
+
modifier: null,
|
|
40
|
+
name: 'class',
|
|
41
|
+
namespace: null,
|
|
42
|
+
specificity: sel.specificity,
|
|
43
|
+
type: 'attrValue',
|
|
44
|
+
value: sel.name,
|
|
45
|
+
});
|
|
46
|
+
break;
|
|
47
|
+
case 'id':
|
|
48
|
+
newList.push({
|
|
49
|
+
matcher: '=',
|
|
50
|
+
modifier: null,
|
|
51
|
+
name: 'id',
|
|
52
|
+
namespace: null,
|
|
53
|
+
specificity: sel.specificity,
|
|
54
|
+
type: 'attrValue',
|
|
55
|
+
value: sel.name,
|
|
56
|
+
});
|
|
57
|
+
break;
|
|
58
|
+
case 'combinator':
|
|
59
|
+
reduceSelectorVariants(sel.left);
|
|
60
|
+
newList.push(sel);
|
|
61
|
+
break;
|
|
62
|
+
case 'universal':
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
newList.push(sel);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
ast.list = newList;
|
|
70
|
+
}
|
|
71
|
+
function weave(items) {
|
|
72
|
+
const branches = [];
|
|
73
|
+
while (items.length) {
|
|
74
|
+
const topKind = findTopKey(items, (sel) => true, getSelectorKind);
|
|
75
|
+
const { matches, nonmatches, empty } = breakByKind(items, topKind);
|
|
76
|
+
items = nonmatches;
|
|
77
|
+
if (matches.length) {
|
|
78
|
+
branches.push(branchOfKind(topKind, matches));
|
|
79
|
+
}
|
|
80
|
+
if (empty.length) {
|
|
81
|
+
branches.push(...terminate(empty));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return branches;
|
|
85
|
+
}
|
|
86
|
+
function terminate(items) {
|
|
87
|
+
const results = [];
|
|
88
|
+
for (const item of items) {
|
|
89
|
+
const terminal = item.terminal;
|
|
90
|
+
if (terminal.type === 'terminal') {
|
|
91
|
+
results.push(terminal);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const { matches, rest } = partition(terminal.cont, (node) => node.type === 'terminal');
|
|
95
|
+
matches.forEach((node) => results.push(node));
|
|
96
|
+
if (rest.length) {
|
|
97
|
+
terminal.cont = rest;
|
|
98
|
+
results.push(terminal);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return results;
|
|
103
|
+
}
|
|
104
|
+
function breakByKind(items, selectedKind) {
|
|
105
|
+
const matches = [];
|
|
106
|
+
const nonmatches = [];
|
|
107
|
+
const empty = [];
|
|
108
|
+
for (const item of items) {
|
|
109
|
+
const simpsels = item.ast.list;
|
|
110
|
+
if (simpsels.length) {
|
|
111
|
+
const isMatch = simpsels.some(node => getSelectorKind(node) === selectedKind);
|
|
112
|
+
(isMatch ? matches : nonmatches).push(item);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
empty.push(item);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return { matches, nonmatches, empty };
|
|
119
|
+
}
|
|
120
|
+
function getSelectorKind(sel) {
|
|
121
|
+
switch (sel.type) {
|
|
122
|
+
case 'attrPresence':
|
|
123
|
+
return `attrPresence ${sel.name}`;
|
|
124
|
+
case 'attrValue':
|
|
125
|
+
return `attrValue ${sel.name}`;
|
|
126
|
+
case 'combinator':
|
|
127
|
+
return `combinator ${sel.combinator}`;
|
|
128
|
+
default:
|
|
129
|
+
return sel.type;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function branchOfKind(kind, items) {
|
|
133
|
+
if (kind === 'tag') {
|
|
134
|
+
return tagNameBranch(items);
|
|
135
|
+
}
|
|
136
|
+
if (kind.startsWith('attrValue ')) {
|
|
137
|
+
return attrValueBranch(kind.substring(10), items);
|
|
138
|
+
}
|
|
139
|
+
if (kind.startsWith('attrPresence ')) {
|
|
140
|
+
return attrPresenceBranch(kind.substring(13), items);
|
|
141
|
+
}
|
|
142
|
+
if (kind === 'combinator >') {
|
|
143
|
+
return combinatorBranch('>', items);
|
|
144
|
+
}
|
|
145
|
+
if (kind === 'combinator +') {
|
|
146
|
+
return combinatorBranch('+', items);
|
|
147
|
+
}
|
|
148
|
+
throw new Error(`Unsupported selector kind: ${kind}`);
|
|
149
|
+
}
|
|
150
|
+
function tagNameBranch(items) {
|
|
151
|
+
const groups = spliceAndGroup(items, (x) => x.type === 'tag', (x) => x.name);
|
|
152
|
+
const variants = Object.entries(groups).map(([name, group]) => ({
|
|
153
|
+
type: 'variant',
|
|
154
|
+
value: name,
|
|
155
|
+
cont: weave(group.items)
|
|
156
|
+
}));
|
|
157
|
+
return {
|
|
158
|
+
type: 'tagName',
|
|
159
|
+
variants: variants
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function attrPresenceBranch(name, items) {
|
|
163
|
+
for (const item of items) {
|
|
164
|
+
spliceSimpleSelector(item, (x) => (x.type === 'attrPresence') && (x.name === name));
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
type: 'attrPresence',
|
|
168
|
+
name: name,
|
|
169
|
+
cont: weave(items)
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function attrValueBranch(name, items) {
|
|
173
|
+
const groups = spliceAndGroup(items, (x) => (x.type === 'attrValue') && (x.name === name), (x) => `${x.matcher} ${x.modifier || ''} ${x.value}`);
|
|
174
|
+
const matchers = [];
|
|
175
|
+
for (const group of Object.values(groups)) {
|
|
176
|
+
const sel = group.oneSimpleSelector;
|
|
177
|
+
const predicate = getAttrPredicate(sel);
|
|
178
|
+
const continuation = weave(group.items);
|
|
179
|
+
matchers.push({
|
|
180
|
+
type: 'matcher',
|
|
181
|
+
matcher: sel.matcher,
|
|
182
|
+
modifier: sel.modifier,
|
|
183
|
+
value: sel.value,
|
|
184
|
+
predicate: predicate,
|
|
185
|
+
cont: continuation
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
type: 'attrValue',
|
|
190
|
+
name: name,
|
|
191
|
+
matchers: matchers
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function getAttrPredicate(sel) {
|
|
195
|
+
if (sel.modifier === 'i') {
|
|
196
|
+
const expected = sel.value.toLowerCase();
|
|
197
|
+
switch (sel.matcher) {
|
|
198
|
+
case '=':
|
|
199
|
+
return (actual) => expected === actual.toLowerCase();
|
|
200
|
+
case '~=':
|
|
201
|
+
return (actual) => actual.toLowerCase().split(/[ \t]+/).includes(expected);
|
|
202
|
+
case '^=':
|
|
203
|
+
return (actual) => actual.toLowerCase().startsWith(expected);
|
|
204
|
+
case '$=':
|
|
205
|
+
return (actual) => actual.toLowerCase().endsWith(expected);
|
|
206
|
+
case '*=':
|
|
207
|
+
return (actual) => actual.toLowerCase().includes(expected);
|
|
208
|
+
case '|=':
|
|
209
|
+
return (actual) => {
|
|
210
|
+
const lower = actual.toLowerCase();
|
|
211
|
+
return (expected === lower) || (lower.startsWith(expected) && lower[expected.length] === '-');
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
const expected = sel.value;
|
|
217
|
+
switch (sel.matcher) {
|
|
218
|
+
case '=':
|
|
219
|
+
return (actual) => expected === actual;
|
|
220
|
+
case '~=':
|
|
221
|
+
return (actual) => actual.split(/[ \t]+/).includes(expected);
|
|
222
|
+
case '^=':
|
|
223
|
+
return (actual) => actual.startsWith(expected);
|
|
224
|
+
case '$=':
|
|
225
|
+
return (actual) => actual.endsWith(expected);
|
|
226
|
+
case '*=':
|
|
227
|
+
return (actual) => actual.includes(expected);
|
|
228
|
+
case '|=':
|
|
229
|
+
return (actual) => (expected === actual) || (actual.startsWith(expected) && actual[expected.length] === '-');
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function combinatorBranch(combinator, items) {
|
|
234
|
+
const groups = spliceAndGroup(items, (x) => (x.type === 'combinator') && (x.combinator === combinator), (x) => serialize(x.left));
|
|
235
|
+
const leftItems = [];
|
|
236
|
+
for (const group of Object.values(groups)) {
|
|
237
|
+
const rightCont = weave(group.items);
|
|
238
|
+
const leftAst = group.oneSimpleSelector.left;
|
|
239
|
+
leftItems.push({
|
|
240
|
+
ast: leftAst,
|
|
241
|
+
terminal: { type: 'popElement', cont: rightCont }
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
return {
|
|
245
|
+
type: 'pushElement',
|
|
246
|
+
combinator: combinator,
|
|
247
|
+
cont: weave(leftItems)
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function spliceAndGroup(items, predicate, keyCallback) {
|
|
251
|
+
const groups = {};
|
|
252
|
+
while (items.length) {
|
|
253
|
+
const bestKey = findTopKey(items, predicate, keyCallback);
|
|
254
|
+
const bestKeyPredicate = (sel) => predicate(sel) && keyCallback(sel) === bestKey;
|
|
255
|
+
const hasBestKeyPredicate = (item) => item.ast.list.some(bestKeyPredicate);
|
|
256
|
+
const { matches, rest } = partition1(items, hasBestKeyPredicate);
|
|
257
|
+
let oneSimpleSelector = null;
|
|
258
|
+
for (const item of matches) {
|
|
259
|
+
const splicedNode = spliceSimpleSelector(item, bestKeyPredicate);
|
|
260
|
+
if (!oneSimpleSelector) {
|
|
261
|
+
oneSimpleSelector = splicedNode;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (oneSimpleSelector == null) {
|
|
265
|
+
throw new Error('No simple selector is found.');
|
|
266
|
+
}
|
|
267
|
+
groups[bestKey] = { oneSimpleSelector: oneSimpleSelector, items: matches };
|
|
268
|
+
items = rest;
|
|
269
|
+
}
|
|
270
|
+
return groups;
|
|
271
|
+
}
|
|
272
|
+
function spliceSimpleSelector(item, predicate) {
|
|
273
|
+
const simpsels = item.ast.list;
|
|
274
|
+
const matches = new Array(simpsels.length);
|
|
275
|
+
let firstIndex = -1;
|
|
276
|
+
for (let i = simpsels.length; i-- > 0;) {
|
|
277
|
+
if (predicate(simpsels[i])) {
|
|
278
|
+
matches[i] = true;
|
|
279
|
+
firstIndex = i;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (firstIndex == -1) {
|
|
283
|
+
throw new Error(`Couldn't find the required simple selector.`);
|
|
284
|
+
}
|
|
285
|
+
const result = simpsels[firstIndex];
|
|
286
|
+
item.ast.list = simpsels.filter((sel, i) => !matches[i]);
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
function findTopKey(items, predicate, keyCallback) {
|
|
290
|
+
const candidates = {};
|
|
291
|
+
for (const item of items) {
|
|
292
|
+
const candidates1 = {};
|
|
293
|
+
for (const node of item.ast.list.filter(predicate)) {
|
|
294
|
+
candidates1[keyCallback(node)] = true;
|
|
295
|
+
}
|
|
296
|
+
for (const key of Object.keys(candidates1)) {
|
|
297
|
+
if (candidates[key]) {
|
|
298
|
+
candidates[key]++;
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
candidates[key] = 1;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
let topKind = '';
|
|
306
|
+
let topCounter = 0;
|
|
307
|
+
for (const entry of Object.entries(candidates)) {
|
|
308
|
+
if (entry[1] > topCounter) {
|
|
309
|
+
topKind = entry[0];
|
|
310
|
+
topCounter = entry[1];
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return topKind;
|
|
314
|
+
}
|
|
315
|
+
function partition(src, predicate) {
|
|
316
|
+
const matches = [];
|
|
317
|
+
const rest = [];
|
|
318
|
+
for (const x of src) {
|
|
319
|
+
if (predicate(x)) {
|
|
320
|
+
matches.push(x);
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
rest.push(x);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return { matches, rest };
|
|
327
|
+
}
|
|
328
|
+
function partition1(src, predicate) {
|
|
329
|
+
const matches = [];
|
|
330
|
+
const rest = [];
|
|
331
|
+
for (const x of src) {
|
|
332
|
+
if (predicate(x)) {
|
|
333
|
+
matches.push(x);
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
rest.push(x);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return { matches, rest };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
class Picker {
|
|
343
|
+
constructor(f) {
|
|
344
|
+
this.f = f;
|
|
345
|
+
}
|
|
346
|
+
pickAll(el) {
|
|
347
|
+
return this.f(el);
|
|
348
|
+
}
|
|
349
|
+
pick1(el, preferFirst = false) {
|
|
350
|
+
const results = this.f(el);
|
|
351
|
+
const len = results.length;
|
|
352
|
+
if (len === 0) {
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
if (len === 1) {
|
|
356
|
+
return results[0].value;
|
|
357
|
+
}
|
|
358
|
+
const comparator = (preferFirst)
|
|
359
|
+
? comparatorPreferFirst
|
|
360
|
+
: comparatorPreferLast;
|
|
361
|
+
let result = results[0];
|
|
362
|
+
for (let i = 1; i < len; i++) {
|
|
363
|
+
const next = results[i];
|
|
364
|
+
if (comparator(result, next)) {
|
|
365
|
+
result = next;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return result.value;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function comparatorPreferFirst(acc, next) {
|
|
372
|
+
const diff = compareSpecificity(next.specificity, acc.specificity);
|
|
373
|
+
return diff > 0 || (diff === 0 && next.index < acc.index);
|
|
374
|
+
}
|
|
375
|
+
function comparatorPreferLast(acc, next) {
|
|
376
|
+
const diff = compareSpecificity(next.specificity, acc.specificity);
|
|
377
|
+
return diff > 0 || (diff === 0 && next.index > acc.index);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export { DecisionTree, Picker };
|
|
@@ -2,8 +2,6 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import 'reflect-metadata';
|
|
3
3
|
import '../../../../injection/index.js';
|
|
4
4
|
import '../../../../mindset/metadata/MindsetMetadataStore.js';
|
|
5
|
-
import '../../../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../../../core/user/IUserRepository.js';
|
|
7
5
|
import { param } from '../../../../mindset/metadata/params/@param.js';
|
|
8
6
|
import '../../../../mindset/MindsetOperator.js';
|
|
9
7
|
|
|
@@ -2,8 +2,6 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import 'reflect-metadata';
|
|
3
3
|
import '../../../../injection/index.js';
|
|
4
4
|
import '../../../../mindset/metadata/MindsetMetadataStore.js';
|
|
5
|
-
import '../../../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../../../core/user/IUserRepository.js';
|
|
7
5
|
import { param } from '../../../../mindset/metadata/params/@param.js';
|
|
8
6
|
import '../../../../mindset/MindsetOperator.js';
|
|
9
7
|
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { mindsetFunction } from '../../../mindset/metadata/functions/@mindsetFunction.js';
|
|
3
|
+
import 'reflect-metadata';
|
|
4
|
+
import '../../../injection/index.js';
|
|
5
|
+
import '../../../mindset/metadata/MindsetMetadataStore.js';
|
|
6
|
+
import { mindsetModule } from '../../../mindset/metadata/modules/@mindsetModule.js';
|
|
7
|
+
import { param } from '../../../mindset/metadata/params/@param.js';
|
|
8
|
+
import '../../../mindset/MindsetOperator.js';
|
|
9
|
+
import { convert } from '../../../node_modules/html-to-text/lib/html-to-text.js';
|
|
10
|
+
|
|
11
|
+
function toPascalCaseFunctionName(title) {
|
|
12
|
+
const cleaned = title.replace(/[^a-zA-Z0-9]+/g, ' ');
|
|
13
|
+
const words = cleaned
|
|
14
|
+
.trim()
|
|
15
|
+
.split(/\s+/)
|
|
16
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
|
|
17
|
+
return words.join('');
|
|
18
|
+
}
|
|
19
|
+
async function fetchRawHtml(url) {
|
|
20
|
+
try {
|
|
21
|
+
const response = await fetch(url);
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
24
|
+
}
|
|
25
|
+
const html = await response.text();
|
|
26
|
+
return html;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
throw new Error(`Failed to fetch HTML from ${url}: ${error.message}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
class KeywordsReq {
|
|
33
|
+
keywords = '';
|
|
34
|
+
}
|
|
35
|
+
__decorate([
|
|
36
|
+
param({
|
|
37
|
+
description: 'Keywords separated by space',
|
|
38
|
+
}),
|
|
39
|
+
__metadata("design:type", String)
|
|
40
|
+
], KeywordsReq.prototype, "keywords", void 0);
|
|
41
|
+
function HtmlModule(options) {
|
|
42
|
+
var _a;
|
|
43
|
+
const standaricedTitle = toPascalCaseFunctionName(options.title);
|
|
44
|
+
let documentText = null;
|
|
45
|
+
let HtmlModuleIntern = class HtmlModuleIntern {
|
|
46
|
+
async [_a = `searchInTheDocument${standaricedTitle}`](req) {
|
|
47
|
+
if (!documentText) {
|
|
48
|
+
const rawHtml = await fetchRawHtml(options.url);
|
|
49
|
+
documentText = convert(rawHtml);
|
|
50
|
+
}
|
|
51
|
+
return documentText;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
__decorate([
|
|
55
|
+
mindsetFunction({
|
|
56
|
+
description: `Search information using keywords in the document "${options.title}". Priorize this info.`,
|
|
57
|
+
}),
|
|
58
|
+
__metadata("design:type", Function),
|
|
59
|
+
__metadata("design:paramtypes", [KeywordsReq]),
|
|
60
|
+
__metadata("design:returntype", Promise)
|
|
61
|
+
], HtmlModuleIntern.prototype, _a, null);
|
|
62
|
+
HtmlModuleIntern = __decorate([
|
|
63
|
+
mindsetModule({
|
|
64
|
+
description: `Search information using keywords in the document ${options.title}`,
|
|
65
|
+
})
|
|
66
|
+
], HtmlModuleIntern);
|
|
67
|
+
return HtmlModuleIntern;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { HtmlModule };
|
|
@@ -2,8 +2,6 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import 'reflect-metadata';
|
|
3
3
|
import '../../../../injection/index.js';
|
|
4
4
|
import '../../../../mindset/metadata/MindsetMetadataStore.js';
|
|
5
|
-
import '../../../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../../../core/user/IUserRepository.js';
|
|
7
5
|
import { param } from '../../../../mindset/metadata/params/@param.js';
|
|
8
6
|
import '../../../../mindset/MindsetOperator.js';
|
|
9
7
|
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import { ChatItem } from '../../../../core/chat/ChatItem.js';
|
|
2
1
|
import '../../../../core/chat/repository/IChatRepository.js';
|
|
2
|
+
import { ChatItem } from '../../../../core/chat/ChatItem.js';
|
|
3
3
|
import '../../../../core/user/IUserRepository.js';
|
|
4
4
|
import { PgCrudRepository } from '../../../../repository/pg/PgCrudRepository.js';
|
|
5
|
-
import 'fs';
|
|
6
|
-
import 'path';
|
|
7
|
-
import 'sqlite';
|
|
8
|
-
import 'sqlite3';
|
|
9
|
-
import 'uuid';
|
|
10
|
-
import 'pg';
|
|
11
5
|
|
|
12
6
|
class PgChatMemory extends PgCrudRepository {
|
|
13
7
|
chatId;
|
|
@@ -6,11 +6,6 @@ import { singleton } from '../../../../injection/index.js';
|
|
|
6
6
|
import { Pool } from 'pg';
|
|
7
7
|
import { PgChatMemory } from './PgChatMemory.js';
|
|
8
8
|
import { PgCrudRepository } from '../../../../repository/pg/PgCrudRepository.js';
|
|
9
|
-
import 'fs';
|
|
10
|
-
import 'path';
|
|
11
|
-
import 'sqlite';
|
|
12
|
-
import 'sqlite3';
|
|
13
|
-
import 'uuid';
|
|
14
9
|
|
|
15
10
|
let PgChatRepository = class PgChatRepository extends PgCrudRepository {
|
|
16
11
|
constructor(pool) {
|
|
@@ -4,11 +4,6 @@ import { User } from '../../../../core/user/User.js';
|
|
|
4
4
|
import '../../../../core/user/IUserRepository.js';
|
|
5
5
|
import { singleton } from '../../../../injection/index.js';
|
|
6
6
|
import { PgCrudRepository } from '../../../../repository/pg/PgCrudRepository.js';
|
|
7
|
-
import 'fs';
|
|
8
|
-
import 'path';
|
|
9
|
-
import 'sqlite';
|
|
10
|
-
import 'sqlite3';
|
|
11
|
-
import 'uuid';
|
|
12
7
|
import { Pool } from 'pg';
|
|
13
8
|
|
|
14
9
|
let PgUserRepository = class PgUserRepository extends PgCrudRepository {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { container } from '../../injection/index.js';
|
|
2
|
+
import { RestControllerMetadataStore } from './RestControllerMetadataStore.js';
|
|
3
|
+
|
|
4
|
+
function get(config) {
|
|
5
|
+
return function (target, propertyKey) {
|
|
6
|
+
const functionName = propertyKey.toString();
|
|
7
|
+
const paramsTypes = Reflect.getMetadata('design:paramtypes', target, functionName);
|
|
8
|
+
const store = container.resolve(RestControllerMetadataStore);
|
|
9
|
+
store.saveEndPointMetadata({
|
|
10
|
+
controllerConstructor: target.constructor,
|
|
11
|
+
functionName,
|
|
12
|
+
method: 'get',
|
|
13
|
+
path: config?.path,
|
|
14
|
+
paramsTypes,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { get };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { container } from '../../injection/index.js';
|
|
2
|
+
import { RestControllerMetadataStore } from './RestControllerMetadataStore.js';
|
|
3
|
+
|
|
4
|
+
function post(config) {
|
|
5
|
+
return function (target, propertyKey) {
|
|
6
|
+
const functionName = propertyKey.toString();
|
|
7
|
+
const paramsTypes = Reflect.getMetadata('design:paramtypes', target, functionName);
|
|
8
|
+
const store = container.resolve(RestControllerMetadataStore);
|
|
9
|
+
store.saveEndPointMetadata({
|
|
10
|
+
controllerConstructor: target.constructor,
|
|
11
|
+
functionName,
|
|
12
|
+
method: 'post',
|
|
13
|
+
path: config?.path,
|
|
14
|
+
paramsTypes,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { post };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { container, injectable } from '../../injection/index.js';
|
|
2
|
+
import { RestControllerMetadataStore } from './RestControllerMetadataStore.js';
|
|
3
|
+
|
|
4
|
+
function restController(config) {
|
|
5
|
+
return function (target) {
|
|
6
|
+
const metaDataStore = container.resolve(RestControllerMetadataStore);
|
|
7
|
+
metaDataStore.saveControllerMetadata({
|
|
8
|
+
controllerConstructor: target,
|
|
9
|
+
path: config.path,
|
|
10
|
+
});
|
|
11
|
+
injectable()(target);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { restController };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { singleton } from 'tsyringe';
|
|
3
|
+
|
|
4
|
+
let RestControllerMetadataStore = class RestControllerMetadataStore {
|
|
5
|
+
endPoints = new Map();
|
|
6
|
+
restControllers = new Map();
|
|
7
|
+
saveControllerMetadata(controllerMetadata) {
|
|
8
|
+
this.restControllers.set(controllerMetadata.controllerConstructor, controllerMetadata);
|
|
9
|
+
}
|
|
10
|
+
saveEndPointMetadata(endPointMetadata) {
|
|
11
|
+
let controllerEndPoints = this.endPoints.get(endPointMetadata.controllerConstructor);
|
|
12
|
+
if (!controllerEndPoints) {
|
|
13
|
+
this.endPoints.set(endPointMetadata.controllerConstructor, (controllerEndPoints = new Map()));
|
|
14
|
+
}
|
|
15
|
+
controllerEndPoints.set(endPointMetadata.functionName, endPointMetadata);
|
|
16
|
+
}
|
|
17
|
+
getControllerEndPointsInfo(controllerConstructor) {
|
|
18
|
+
const controller = this.restControllers.get(controllerConstructor);
|
|
19
|
+
if (!controller) {
|
|
20
|
+
throw new Error(`${controllerConstructor.name} should be decorated with @restController`);
|
|
21
|
+
}
|
|
22
|
+
const endPoints = this.endPoints.get(controllerConstructor);
|
|
23
|
+
if (!endPoints?.size) {
|
|
24
|
+
// TODO: Warning
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
return [...endPoints.values()].map((endPoint) => ({
|
|
28
|
+
...endPoint,
|
|
29
|
+
controller: this.restControllers.get(endPoint.controllerConstructor),
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
RestControllerMetadataStore = __decorate([
|
|
34
|
+
singleton()
|
|
35
|
+
], RestControllerMetadataStore);
|
|
36
|
+
|
|
37
|
+
export { RestControllerMetadataStore };
|