alemonjs 2.1.0-alpha.0 → 2.1.0-alpha.10
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/lib/app/event-processor-event.js +1 -1
- package/lib/app/event-processor-middleware.js +1 -1
- package/lib/app/event-processor.js +1 -1
- package/lib/app/hook-use-api.d.ts +9 -2
- package/lib/app/hook-use-api.js +37 -19
- package/lib/app/load.js +10 -14
- package/lib/app/message-api.d.ts +4 -4
- package/lib/app/message-format.d.ts +1 -1
- package/lib/app/message-format.js +1 -1
- package/lib/app/store.js +4 -3
- package/lib/cbp/actions.js +9 -21
- package/lib/cbp/api.js +36 -0
- package/lib/cbp/config.js +8 -2
- package/lib/cbp/connect.d.ts +6 -2
- package/lib/cbp/connect.js +208 -27
- package/lib/cbp/index.js +54 -8
- package/lib/core/utils.d.ts +77 -0
- package/lib/core/utils.js +162 -0
- package/lib/global.d.ts +8 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -2
- package/lib/main.js +1 -1
- package/lib/typing/actions.d.ts +1 -1
- package/lib/typing/apis.d.ts +18 -0
- package/lib/typing/client/index.d.ts +1 -1
- package/lib/typing/event/base/user.d.ts +5 -17
- package/lib/typing/message/button.d.ts +1 -1
- package/lib/typing/message/image.d.ts +1 -1
- package/lib/utils.d.ts +43 -0
- package/lib/utils.js +108 -0
- package/package.json +5 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isAsyncFunction } from 'util/types';
|
|
2
2
|
import { useState } from './hook-use-state.js';
|
|
3
|
-
import { showErrorModule } from '
|
|
3
|
+
import { showErrorModule } from '../core/utils.js';
|
|
4
4
|
import { Response } from './store.js';
|
|
5
5
|
import { useMessage } from './hook-use-api.js';
|
|
6
6
|
import { EventMessageText } from '../core/variable.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isAsyncFunction } from 'util/types';
|
|
2
2
|
import { useState } from './hook-use-state.js';
|
|
3
|
-
import { showErrorModule } from '
|
|
3
|
+
import { showErrorModule } from '../core/utils.js';
|
|
4
4
|
import { Middleware } from './store.js';
|
|
5
5
|
import { useMessage } from './hook-use-api.js';
|
|
6
6
|
import { EventMessageText } from '../core/variable.js';
|
|
@@ -2,7 +2,7 @@ import { getConfigValue } from '../core/config.js';
|
|
|
2
2
|
import { processor_repeated_clear_time_min, processor_repeated_clear_time_max, processor_repeated_event_time, processor_repeated_user_time, processor_repeated_clear_size } from '../core/variable.js';
|
|
3
3
|
import { expendCycle } from './event-processor-cycle.js';
|
|
4
4
|
import { ProcessorEventAutoClearMap, ProcessorEventUserAudoClearMap } from './store.js';
|
|
5
|
-
import { createHash } from '
|
|
5
|
+
import { createHash } from '../core/utils.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* 过滤掉重复消息
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventKeys, Events } from '../typing/event/map.js';
|
|
2
|
-
import { Result } from '
|
|
2
|
+
import { Result } from '../core/utils.js';
|
|
3
3
|
import '../global.js';
|
|
4
4
|
import { DataEnums } from '../typing/message/index.js';
|
|
5
5
|
|
|
@@ -91,5 +91,12 @@ declare const onSelects: <T extends EventKeys[] | EventKeys>(values: T) => T;
|
|
|
91
91
|
* @deprecated
|
|
92
92
|
*/
|
|
93
93
|
declare const createSelects: <T extends EventKeys[] | EventKeys>(values: T) => T;
|
|
94
|
+
/**
|
|
95
|
+
* 使用客户端
|
|
96
|
+
* @param event
|
|
97
|
+
* @param _ApiClass
|
|
98
|
+
* @returns
|
|
99
|
+
*/
|
|
100
|
+
declare const useClient: <T extends object>(event: any, _ApiClass: new (...args: any[]) => T) => T[];
|
|
94
101
|
|
|
95
|
-
export { createSelects, onSelects, unChildren, useChannel, useMenber, useMention, useMessage, useSend, useSends };
|
|
102
|
+
export { createSelects, onSelects, unChildren, useChannel, useClient, useMenber, useMention, useMessage, useSend, useSends };
|
package/lib/app/hook-use-api.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ResultCode } from '../core/code.js';
|
|
2
2
|
import { ChildrenApp } from './store.js';
|
|
3
|
-
import { createResult } from '
|
|
3
|
+
import { createResult } from '../core/utils.js';
|
|
4
4
|
import { sendAction } from '../cbp/actions.js';
|
|
5
|
+
import { sendAPI } from '../cbp/api.js';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* 使用提及。
|
|
@@ -22,20 +23,16 @@ const useMention = (event) => {
|
|
|
22
23
|
find: async (options) => {
|
|
23
24
|
try {
|
|
24
25
|
if (!res) {
|
|
25
|
-
const
|
|
26
|
+
const results = await sendAction({
|
|
26
27
|
action: 'mention.get',
|
|
27
28
|
payload: {
|
|
28
29
|
event
|
|
29
30
|
}
|
|
30
31
|
});
|
|
31
|
-
|
|
32
|
-
if (
|
|
33
|
-
|
|
32
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
33
|
+
if (result) {
|
|
34
|
+
res = result.data;
|
|
34
35
|
}
|
|
35
|
-
if (result.code !== ResultCode.Ok) {
|
|
36
|
-
return createResult(result.code, result.message, null);
|
|
37
|
-
}
|
|
38
|
-
res = result.data;
|
|
39
36
|
}
|
|
40
37
|
}
|
|
41
38
|
catch (err) {
|
|
@@ -70,20 +67,16 @@ const useMention = (event) => {
|
|
|
70
67
|
}) => {
|
|
71
68
|
try {
|
|
72
69
|
if (!res) {
|
|
73
|
-
const
|
|
70
|
+
const results = await sendAction({
|
|
74
71
|
action: 'mention.get',
|
|
75
72
|
payload: {
|
|
76
73
|
event
|
|
77
74
|
}
|
|
78
75
|
});
|
|
79
|
-
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
if (result.code !== ResultCode.Ok) {
|
|
84
|
-
return createResult(result.code, result.message, null);
|
|
76
|
+
const result = results.find(item => item.code === ResultCode.Ok);
|
|
77
|
+
if (result) {
|
|
78
|
+
res = result.data;
|
|
85
79
|
}
|
|
86
|
-
res = result.data;
|
|
87
80
|
}
|
|
88
81
|
}
|
|
89
82
|
catch (err) {
|
|
@@ -140,7 +133,9 @@ const useMessage = (event) => {
|
|
|
140
133
|
*/
|
|
141
134
|
const send = async (val) => {
|
|
142
135
|
if (!val || val.length === 0) {
|
|
143
|
-
return [
|
|
136
|
+
return [
|
|
137
|
+
createResult(ResultCode.FailParams, 'Invalid val: val must be a non-empty array', null)
|
|
138
|
+
];
|
|
144
139
|
}
|
|
145
140
|
const result = await sendAction({
|
|
146
141
|
action: 'message.send',
|
|
@@ -345,5 +340,28 @@ global.onSelects = onSelects;
|
|
|
345
340
|
* @deprecated
|
|
346
341
|
*/
|
|
347
342
|
const createSelects = onSelects;
|
|
343
|
+
/**
|
|
344
|
+
* 使用客户端
|
|
345
|
+
* @param event
|
|
346
|
+
* @param _ApiClass
|
|
347
|
+
* @returns
|
|
348
|
+
*/
|
|
349
|
+
const useClient = (event, _ApiClass) => {
|
|
350
|
+
const client = new Proxy({}, {
|
|
351
|
+
get(_target, prop) {
|
|
352
|
+
return (...args) => {
|
|
353
|
+
return sendAPI({
|
|
354
|
+
action: 'client.api',
|
|
355
|
+
payload: {
|
|
356
|
+
event,
|
|
357
|
+
key: String(prop),
|
|
358
|
+
params: args
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
return [client];
|
|
365
|
+
};
|
|
348
366
|
|
|
349
|
-
export { createSelects, onSelects, unChildren, useChannel, useMenber, useMention, useMessage, useSend, useSends };
|
|
367
|
+
export { createSelects, onSelects, unChildren, useChannel, useClient, useMenber, useMention, useMessage, useSend, useSends };
|
package/lib/app/load.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { dirname, join } from 'path';
|
|
2
2
|
import { existsSync } from 'fs';
|
|
3
|
-
import { showErrorModule, getRecursiveDirFiles, createEventName } from '
|
|
3
|
+
import { showErrorModule, getRecursiveDirFiles, createEventName } from '../core/utils.js';
|
|
4
4
|
import { createRequire } from 'module';
|
|
5
5
|
import { ChildrenApp } from './store.js';
|
|
6
6
|
import { ResultCode } from '../core/code.js';
|
|
@@ -51,7 +51,7 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
51
51
|
// 卸载
|
|
52
52
|
App.un();
|
|
53
53
|
try {
|
|
54
|
-
await app
|
|
54
|
+
app?.unMounted && (await app.unMounted(e));
|
|
55
55
|
}
|
|
56
56
|
catch (e) {
|
|
57
57
|
// 卸载周期出意外,不需要进行卸载
|
|
@@ -59,15 +59,13 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
61
|
// onCreated 创建
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
62
|
+
try {
|
|
63
|
+
app?.onCreated && (await app?.onCreated());
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
unMounted(e);
|
|
67
|
+
// 出错了,结束后续的操作。
|
|
68
|
+
return;
|
|
71
69
|
}
|
|
72
70
|
// onMounted 加载
|
|
73
71
|
try {
|
|
@@ -119,9 +117,7 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
119
117
|
App.pushMiddleware(mwData);
|
|
120
118
|
App.on();
|
|
121
119
|
try {
|
|
122
|
-
|
|
123
|
-
await app?.onMounted({ response: resData, middleware: mwData });
|
|
124
|
-
}
|
|
120
|
+
app?.onMounted && (await app.onMounted({ response: resData, middleware: mwData }));
|
|
125
121
|
}
|
|
126
122
|
catch (e) {
|
|
127
123
|
unMounted(e);
|
package/lib/app/message-api.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Result } from './utils.js';
|
|
2
1
|
import { OnDataFormatFunc } from '../typing/event/index.js';
|
|
3
|
-
import '../
|
|
2
|
+
import { Result } from '../core/utils.js';
|
|
4
3
|
import { DataEnums } from '../typing/message/index.js';
|
|
4
|
+
import '../global.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 创建数据格式。
|
|
@@ -16,13 +16,13 @@ declare const format: OnDataFormatFunc;
|
|
|
16
16
|
* @param {DataEnums[]} data - 要发送的数据。
|
|
17
17
|
* @throws {Error} - 如果 channel_id 无效或发送失败,抛出错误。
|
|
18
18
|
*/
|
|
19
|
-
declare const sendToChannel: (channel_id: string, data: DataEnums[]) => Promise<Result
|
|
19
|
+
declare const sendToChannel: (channel_id: string, data: DataEnums[]) => Promise<Result[]>;
|
|
20
20
|
/**
|
|
21
21
|
* 向指定用户发送消息。
|
|
22
22
|
* @param {string} user_id - 目标用户的 ID。
|
|
23
23
|
* @param {DataEnums[]} data - 要发送的数据。
|
|
24
24
|
* @throws {Error} - 如果 user_id 无效或发送失败,抛出错误。
|
|
25
25
|
*/
|
|
26
|
-
declare const sendToUser: (user_id: string, data: DataEnums[]) => Promise<Result
|
|
26
|
+
declare const sendToUser: (user_id: string, data: DataEnums[]) => Promise<Result[]>;
|
|
27
27
|
|
|
28
28
|
export { format, sendToChannel, sendToUser };
|
|
@@ -39,7 +39,7 @@ declare const ImageFile: (val: DataImageFile["value"]) => DataImageFile;
|
|
|
39
39
|
* @returns
|
|
40
40
|
*/
|
|
41
41
|
declare const Image: {
|
|
42
|
-
(val:
|
|
42
|
+
(val: Buffer): DataImage;
|
|
43
43
|
url: (val: DataImageURL["value"]) => DataImageURL;
|
|
44
44
|
file: (val: DataImageFile["value"]) => DataImageFile;
|
|
45
45
|
};
|
package/lib/app/store.js
CHANGED
|
@@ -15,6 +15,8 @@ import log4js from 'log4js';
|
|
|
15
15
|
const createLogger = () => {
|
|
16
16
|
const logDir = process.env?.LOG_PATH ?? `./logs/${process.env.LOG_NAME ?? ''}`;
|
|
17
17
|
mkdirSync(logDir, { recursive: true });
|
|
18
|
+
// 当环境被设置为 development 时。被视为 trace
|
|
19
|
+
const level = process.env.NODE_ENV === 'development' ? 'trace' : 'info';
|
|
18
20
|
log4js.configure({
|
|
19
21
|
appenders: {
|
|
20
22
|
console: {
|
|
@@ -48,13 +50,12 @@ const createLogger = () => {
|
|
|
48
50
|
}
|
|
49
51
|
},
|
|
50
52
|
categories: {
|
|
51
|
-
default: { appenders: ['console'], level:
|
|
52
|
-
// 记录级别
|
|
53
|
+
default: { appenders: ['console'], level: level },
|
|
53
54
|
command: { appenders: ['console', 'command'], level: 'info' },
|
|
54
55
|
error: { appenders: ['console', 'command', 'error'], level: 'warn' }
|
|
55
56
|
}
|
|
56
57
|
});
|
|
57
|
-
const defaultLogger = log4js.getLogger('
|
|
58
|
+
const defaultLogger = log4js.getLogger('default');
|
|
58
59
|
const commandLogger = log4js.getLogger('command');
|
|
59
60
|
const errorLogger = log4js.getLogger('error');
|
|
60
61
|
return {
|
package/lib/cbp/actions.js
CHANGED
|
@@ -1,20 +1,5 @@
|
|
|
1
1
|
import { ResultCode } from '../core/code.js';
|
|
2
|
-
import '../
|
|
3
|
-
import '../app/event-middleware.js';
|
|
4
|
-
import '../app/event-processor.js';
|
|
5
|
-
import '../app/event-response.js';
|
|
6
|
-
import '../app/hook-use-api.js';
|
|
7
|
-
import '../typing/event/actions.js';
|
|
8
|
-
import 'fs';
|
|
9
|
-
import 'path';
|
|
10
|
-
import 'yaml';
|
|
11
|
-
import 'node:fs';
|
|
12
|
-
import 'log4js';
|
|
13
|
-
import '../app/load.js';
|
|
14
|
-
import '../app/message-api.js';
|
|
15
|
-
import '../app/message-format.js';
|
|
16
|
-
import { createResult } from '../app/utils.js';
|
|
17
|
-
import 'ws';
|
|
2
|
+
import { createResult } from '../core/utils.js';
|
|
18
3
|
import { generateUniqueId, actionResolves, deviceId, actionTimeouts, timeoutTime } from './config.js';
|
|
19
4
|
|
|
20
5
|
/**
|
|
@@ -26,20 +11,23 @@ const sendAction = (data) => {
|
|
|
26
11
|
return new Promise(resolve => {
|
|
27
12
|
actionResolves.set(actionId, resolve);
|
|
28
13
|
// 设置唯一标识符
|
|
29
|
-
data.
|
|
14
|
+
data.actionId = actionId;
|
|
30
15
|
// 设置设备 ID
|
|
31
16
|
data.DeviceId = deviceId;
|
|
17
|
+
// 发送消息
|
|
32
18
|
global.chatbotClient.send(JSON.stringify(data));
|
|
33
19
|
// 12 秒后超时
|
|
34
20
|
const timeout = setTimeout(() => {
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
21
|
+
// 被清理了
|
|
22
|
+
if (!actionResolves.has(actionId) || !actionTimeouts.has(actionId)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
39
25
|
// 删除回调
|
|
40
26
|
actionResolves.delete(actionId);
|
|
41
27
|
// 删除超时器
|
|
42
28
|
actionTimeouts.delete(actionId);
|
|
29
|
+
// 不会当错误进行处理。而是传入错误码
|
|
30
|
+
resolve([createResult(ResultCode.Fail, '行为超时', null)]);
|
|
43
31
|
}, timeoutTime);
|
|
44
32
|
actionTimeouts.set(actionId, timeout);
|
|
45
33
|
});
|
package/lib/cbp/api.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ResultCode } from '../core/code.js';
|
|
2
|
+
import { createResult } from '../core/utils.js';
|
|
3
|
+
import { generateUniqueId, apiResolves, deviceId, apiTimeouts, timeoutTime } from './config.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 发送行为
|
|
7
|
+
* @param data
|
|
8
|
+
*/
|
|
9
|
+
const sendAPI = (data) => {
|
|
10
|
+
const ApiId = generateUniqueId();
|
|
11
|
+
return new Promise(resolve => {
|
|
12
|
+
apiResolves.set(ApiId, resolve);
|
|
13
|
+
// 设置唯一标识符
|
|
14
|
+
data.apiId = ApiId;
|
|
15
|
+
// 设置设备 ID
|
|
16
|
+
data.DeviceId = deviceId;
|
|
17
|
+
// 发送消息
|
|
18
|
+
global.chatbotClient.send(JSON.stringify(data));
|
|
19
|
+
// 12 秒后超时
|
|
20
|
+
const timeout = setTimeout(() => {
|
|
21
|
+
// 被清理了
|
|
22
|
+
if (!apiResolves.has(ApiId) || !apiTimeouts.has(ApiId)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// 删除回调
|
|
26
|
+
apiResolves.delete(ApiId);
|
|
27
|
+
// 删除超时器
|
|
28
|
+
apiTimeouts.delete(ApiId);
|
|
29
|
+
// 不会当错误进行处理。而是传入错误码
|
|
30
|
+
resolve([createResult(ResultCode.Fail, '接口超时', null)]);
|
|
31
|
+
}, timeoutTime);
|
|
32
|
+
apiTimeouts.set(ApiId, timeout);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export { sendAPI };
|
package/lib/cbp/config.js
CHANGED
|
@@ -15,8 +15,12 @@ const DEVICE_ID_HEADER = 'x-device-id';
|
|
|
15
15
|
const FULL_RECEIVE_HEADER = 'x-full-receive';
|
|
16
16
|
// 行为回调
|
|
17
17
|
const actionResolves = new Map();
|
|
18
|
+
// 接口回调
|
|
19
|
+
const apiResolves = new Map();
|
|
18
20
|
// 超时器
|
|
19
21
|
const actionTimeouts = new Map();
|
|
22
|
+
// 接口超时器
|
|
23
|
+
const apiTimeouts = new Map();
|
|
20
24
|
// 分配绑定记录
|
|
21
25
|
const childrenBind = new Map();
|
|
22
26
|
// 生成唯一标识符
|
|
@@ -26,6 +30,8 @@ const generateUniqueId = () => {
|
|
|
26
30
|
// 超时时间
|
|
27
31
|
const timeoutTime = 1000 * 12; // 12秒
|
|
28
32
|
// 失败重连
|
|
29
|
-
const reconnectInterval =
|
|
33
|
+
const reconnectInterval = 1000 * 6; // 6秒
|
|
34
|
+
// 心跳间隔
|
|
35
|
+
const HEARTBEAT_INTERVAL = 1000 * 18; // 18秒
|
|
30
36
|
|
|
31
|
-
export { DEVICE_ID_HEADER, FULL_RECEIVE_HEADER, USER_AGENT_HEADER, actionResolves, actionTimeouts, childrenBind, childrenClient, deviceId, fullClient, generateUniqueId, platformClient, reconnectInterval, timeoutTime };
|
|
37
|
+
export { DEVICE_ID_HEADER, FULL_RECEIVE_HEADER, HEARTBEAT_INTERVAL, USER_AGENT_HEADER, actionResolves, actionTimeouts, apiResolves, apiTimeouts, childrenBind, childrenClient, deviceId, fullClient, generateUniqueId, platformClient, reconnectInterval, timeoutTime };
|
package/lib/cbp/connect.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Actions } from '../typing/actions.js';
|
|
2
2
|
import { EventsEnum } from '../typing/event/map.js';
|
|
3
|
+
import { Result } from '../core/utils.js';
|
|
3
4
|
import '../global.js';
|
|
5
|
+
import { Apis } from '../typing/apis.js';
|
|
4
6
|
|
|
5
7
|
type CBPClientOptions = {
|
|
6
8
|
open?: () => void;
|
|
@@ -12,12 +14,14 @@ type CBPClientOptions = {
|
|
|
12
14
|
* @param onopen
|
|
13
15
|
*/
|
|
14
16
|
declare const cbpClient: (url: string, options?: CBPClientOptions) => void;
|
|
15
|
-
type
|
|
17
|
+
type ActionReplyFunc = (data: Actions, consume: (payload: Result[]) => void) => void;
|
|
18
|
+
type ApiReplyFunc = (data: Apis, consume: (payload: Result[]) => void) => void;
|
|
16
19
|
declare const cbpPlatform: (url: string, options?: {
|
|
17
20
|
open: () => void;
|
|
18
21
|
}) => {
|
|
19
22
|
send: (data: EventsEnum) => void;
|
|
20
|
-
onactions: (reply:
|
|
23
|
+
onactions: (reply: ActionReplyFunc) => void;
|
|
24
|
+
onapis: (reply: ApiReplyFunc) => void;
|
|
21
25
|
};
|
|
22
26
|
|
|
23
27
|
export { cbpClient, cbpPlatform };
|