alemonjs 2.1.83 → 2.1.84

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.
Files changed (235) hide show
  1. package/lib/{app → application}/define-children.js +2 -1
  2. package/lib/{app → application/format}/message-api.d.ts +2 -2
  3. package/lib/{app → application/format}/message-api.js +12 -4
  4. package/lib/{app → application/format}/message-format-old.d.ts +1 -1
  5. package/lib/{app → application/format}/message-format.d.ts +1 -2
  6. package/lib/{app → application/format}/message-format.js +0 -2
  7. package/lib/{app/hook-use → application/hooks}/announce.js +4 -3
  8. package/lib/{app/hook-use → application/hooks}/channel.js +4 -3
  9. package/lib/{app/hook-use → application/hooks}/client.d.ts +1 -1
  10. package/lib/{app/hook-use → application/hooks}/client.js +4 -11
  11. package/lib/{app/hook-use → application/hooks}/common.d.ts +7 -7
  12. package/lib/{app/hook-use → application/hooks}/common.js +9 -8
  13. package/lib/{app/hook-use → application/hooks}/event.js +1 -1
  14. package/lib/{app/hook-use → application/hooks}/guild.js +4 -3
  15. package/lib/{app/hook-use → application/hooks}/history.js +4 -3
  16. package/lib/{app/hook-use → application/hooks}/me.js +12 -7
  17. package/lib/{app/hook-use → application/hooks}/media.d.ts +3 -3
  18. package/lib/{app/hook-use → application/hooks}/media.js +4 -3
  19. package/lib/{app/hook-use → application/hooks}/member.js +4 -3
  20. package/lib/{app/hook-use → application/hooks}/mention.js +4 -3
  21. package/lib/{app/hook-use → application/hooks}/message.js +6 -5
  22. package/lib/{app/hook-use → application/hooks}/permission.js +4 -3
  23. package/lib/{app/hook-use → application/hooks}/reaction.js +4 -3
  24. package/lib/{app/hook-use → application/hooks}/request.js +12 -7
  25. package/lib/{app/hook-use → application/hooks}/role.js +4 -3
  26. package/lib/{app/hook-use → application/hooks}/subscribe.js +4 -4
  27. package/lib/{app/hook-use → application/hooks}/user.js +12 -7
  28. package/lib/application/index.d.ts +29 -0
  29. package/lib/application/index.js +58 -0
  30. package/lib/{app → application}/router/dsl.d.ts +1 -1
  31. package/lib/{app → application}/router/dsl.js +13 -9
  32. package/lib/{cbp → application/runtime/cbp}/connects/client.d.ts +1 -1
  33. package/lib/{cbp → application/runtime/cbp}/connects/client.js +43 -50
  34. package/lib/application/runtime/cbp/index.d.ts +3 -0
  35. package/lib/application/runtime/cbp/index.js +3 -0
  36. package/lib/application/runtime/cbp/processor/actions.d.ts +3 -0
  37. package/lib/application/runtime/cbp/processor/actions.js +58 -0
  38. package/lib/application/runtime/cbp/processor/api.d.ts +3 -0
  39. package/lib/application/runtime/cbp/processor/api.js +58 -0
  40. package/lib/application/runtime/cbp/processor/request-registry.d.ts +8 -0
  41. package/lib/application/runtime/cbp/processor/request-registry.js +6 -0
  42. package/lib/application/runtime/client-runtime.d.ts +1 -0
  43. package/lib/application/runtime/client-runtime.js +95 -0
  44. package/lib/{app → application/runtime}/event-error.d.ts +1 -1
  45. package/lib/{app → application/runtime}/event-error.js +1 -1
  46. package/lib/{app → application/runtime}/event-group.d.ts +1 -1
  47. package/lib/{app → application/runtime}/event-middleware.d.ts +1 -1
  48. package/lib/{app → application/runtime}/event-middleware.js +1 -1
  49. package/lib/{app → application/runtime}/event-processor-callHandler.d.ts +1 -1
  50. package/lib/{app → application/runtime}/event-processor-callHandler.js +8 -1
  51. package/lib/{app → application/runtime}/event-processor-cycle.d.ts +1 -1
  52. package/lib/{app → application/runtime}/event-processor-cycleFiles.d.ts +1 -1
  53. package/lib/{app → application/runtime}/event-processor-cycleFiles.js +2 -2
  54. package/lib/{app → application/runtime}/event-processor-cycleRoute.d.ts +1 -1
  55. package/lib/{app → application/runtime}/event-processor-cycleRoute.js +9 -2
  56. package/lib/{app → application/runtime}/event-processor-event.d.ts +1 -1
  57. package/lib/{app → application/runtime}/event-processor-middleware.d.ts +1 -1
  58. package/lib/{app → application/runtime}/event-processor-subscribe.d.ts +1 -1
  59. package/lib/{app → application/runtime}/event-processor-subscribe.js +9 -2
  60. package/lib/{app → application/runtime}/event-processor.d.ts +1 -1
  61. package/lib/{app → application/runtime}/event-processor.js +4 -3
  62. package/lib/{app → application/runtime}/event-response.d.ts +1 -1
  63. package/lib/{app → application/runtime}/event-utils.d.ts +1 -1
  64. package/lib/{app → application/runtime}/event-utils.js +2 -2
  65. package/lib/{app → application/runtime}/hook-event-context.d.ts +2 -2
  66. package/lib/{app → application/runtime}/hook-event-context.js +2 -2
  67. package/lib/application/runtime/http/index.d.ts +4 -0
  68. package/lib/application/runtime/http/index.js +4 -0
  69. package/lib/application/runtime/http/routers/hello.html.d.ts +4 -0
  70. package/lib/application/runtime/http/routers/hello.html.js +376 -0
  71. package/lib/{server → application/runtime/http}/routers/router.js +131 -54
  72. package/lib/{server → application/runtime/http}/routers/utils.d.ts +1 -1
  73. package/lib/{server → application/runtime/http}/routers/utils.js +7 -7
  74. package/lib/{server/main.js → application/runtime/http-server.js} +2 -6
  75. package/lib/{app → application/runtime}/lifecycle-callbacks.d.ts +1 -1
  76. package/lib/{app → application/runtime}/lifecycle-callbacks.js +2 -7
  77. package/lib/{app/load_modules → application/runtime/load-modules}/load.js +2 -2
  78. package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.js +14 -5
  79. package/lib/{app → application/runtime}/schedule-store.d.ts +1 -1
  80. package/lib/{app → application/runtime}/schedule-store.js +1 -1
  81. package/lib/{app → application/runtime}/store.d.ts +14 -19
  82. package/lib/{app → application/runtime}/store.js +8 -103
  83. package/lib/application/schedule.d.ts +17 -0
  84. package/lib/{app/api → application}/schedule.js +1 -1
  85. package/lib/client.d.ts +1 -1
  86. package/lib/client.js +1 -117
  87. package/lib/common/cbp/constants.d.ts +11 -0
  88. package/lib/common/cbp/constants.js +13 -0
  89. package/lib/{cbp/connects/connect.js → common/cbp/heartbeat.js} +3 -6
  90. package/lib/common/cbp/normalize.d.ts +16 -0
  91. package/lib/common/cbp/normalize.js +324 -0
  92. package/lib/common/cbp/runtime.d.ts +2 -0
  93. package/lib/common/cbp/runtime.js +10 -0
  94. package/lib/common/cbp/typings.d.ts +159 -0
  95. package/lib/{cbp/connects/base.js → common/cbp/ws-connector.js} +7 -9
  96. package/lib/{core → common}/config.d.ts +2 -2
  97. package/lib/{core → common}/config.js +5 -4
  98. package/lib/common/identity.d.ts +11 -0
  99. package/lib/common/identity.js +34 -0
  100. package/lib/common/index.d.ts +15 -0
  101. package/lib/common/index.js +14 -0
  102. package/lib/common/logger.d.ts +6 -0
  103. package/lib/common/logger.js +99 -0
  104. package/lib/common/result.d.ts +7 -0
  105. package/lib/common/result.js +19 -0
  106. package/lib/{core → common}/utils.d.ts +3 -18
  107. package/lib/{core → common}/utils.js +6 -49
  108. package/lib/{core → common}/variable.js +1 -1
  109. package/lib/core/cbp/index.d.ts +1 -0
  110. package/lib/core/cbp/index.js +1 -0
  111. package/lib/core/cbp/processor/config.d.ts +8 -0
  112. package/lib/core/cbp/processor/config.js +29 -0
  113. package/lib/{cbp → core/cbp}/routers/hello.html.js +1 -1
  114. package/lib/{cbp → core/cbp}/server/main.js +57 -35
  115. package/lib/{cbp → core/cbp}/server/testone.d.ts +1 -1
  116. package/lib/{cbp → core/cbp}/server/testone.js +8 -2
  117. package/lib/core/index.d.ts +1 -3
  118. package/lib/core/index.js +1 -3
  119. package/lib/core/process/index.d.ts +3 -0
  120. package/lib/{process → core/process}/index.js +0 -1
  121. package/lib/{process → core/process}/ipc-bridge.js +16 -2
  122. package/lib/{process → core/process}/module.js +13 -4
  123. package/lib/{process → core/process}/platform.js +12 -3
  124. package/lib/core/start.d.ts +2 -0
  125. package/lib/core/start.js +76 -0
  126. package/lib/global.d.ts +4 -3
  127. package/lib/index.d.ts +10 -2
  128. package/lib/index.js +74 -54
  129. package/lib/main.d.ts +1 -2
  130. package/lib/main.js +1 -76
  131. package/lib/{cbp/connects/platform.d.ts → platform/cbp-platform.d.ts} +2 -2
  132. package/lib/{cbp/connects/platform.js → platform/cbp-platform.js} +52 -75
  133. package/lib/{app → platform}/define-platform.js +2 -0
  134. package/lib/platform/event-value.d.ts +7 -0
  135. package/lib/platform/event-value.js +5 -0
  136. package/lib/platform/index.d.ts +5 -0
  137. package/lib/platform/index.js +5 -0
  138. package/lib/types/actions.d.ts +20 -1
  139. package/lib/types/apis.d.ts +2 -1
  140. package/lib/types/client/index.d.ts +1 -1
  141. package/lib/types/cycle/index.d.ts +1 -1
  142. package/lib/types/event/index.d.ts +1 -1
  143. package/lib/types/subscribe/index.d.ts +1 -1
  144. package/package.json +22 -1
  145. package/lib/app/api/schedule.d.ts +0 -17
  146. package/lib/app/index.d.ts +0 -25
  147. package/lib/app/index.js +0 -47
  148. package/lib/cbp/index.d.ts +0 -3
  149. package/lib/cbp/index.js +0 -3
  150. package/lib/cbp/processor/actions.d.ts +0 -3
  151. package/lib/cbp/processor/actions.js +0 -48
  152. package/lib/cbp/processor/api.d.ts +0 -3
  153. package/lib/cbp/processor/api.js +0 -48
  154. package/lib/cbp/processor/config.d.ts +0 -29
  155. package/lib/cbp/processor/config.js +0 -52
  156. package/lib/cbp/typings.d.ts +0 -20
  157. package/lib/process/index.d.ts +0 -4
  158. package/lib/server/routers/hello.html.d.ts +0 -2
  159. package/lib/server/routers/hello.html.js +0 -31
  160. /package/lib/{app → application}/define-children.d.ts +0 -0
  161. /package/lib/{app → application}/define-middleware.d.ts +0 -0
  162. /package/lib/{app → application}/define-middleware.js +0 -0
  163. /package/lib/{app → application}/define-response.d.ts +0 -0
  164. /package/lib/{app → application}/define-response.js +0 -0
  165. /package/lib/{app → application}/define-router.d.ts +0 -0
  166. /package/lib/{app → application}/define-router.js +0 -0
  167. /package/lib/{app → application}/expose.d.ts +0 -0
  168. /package/lib/{app → application}/expose.js +0 -0
  169. /package/lib/{app → application/format}/message-format-old.js +0 -0
  170. /package/lib/{app/hook-use → application/hooks}/announce.d.ts +0 -0
  171. /package/lib/{app/hook-use → application/hooks}/channel.d.ts +0 -0
  172. /package/lib/{app/hook-use → application/hooks}/event.d.ts +0 -0
  173. /package/lib/{app/hook-use → application/hooks}/guild.d.ts +0 -0
  174. /package/lib/{app/hook-use → application/hooks}/history.d.ts +0 -0
  175. /package/lib/{app/hook-use → application/hooks}/index.d.ts +0 -0
  176. /package/lib/{app/hook-use → application/hooks}/index.js +0 -0
  177. /package/lib/{app/hook-use → application/hooks}/me.d.ts +0 -0
  178. /package/lib/{app/hook-use → application/hooks}/member.d.ts +0 -0
  179. /package/lib/{app/hook-use → application/hooks}/mention.d.ts +0 -0
  180. /package/lib/{app/hook-use → application/hooks}/message.d.ts +0 -0
  181. /package/lib/{app/hook-use → application/hooks}/permission.d.ts +0 -0
  182. /package/lib/{app/hook-use → application/hooks}/reaction.d.ts +0 -0
  183. /package/lib/{app/hook-use → application/hooks}/request.d.ts +0 -0
  184. /package/lib/{app/hook-use → application/hooks}/role.d.ts +0 -0
  185. /package/lib/{app/hook-use → application/hooks}/route.d.ts +0 -0
  186. /package/lib/{app/hook-use → application/hooks}/route.js +0 -0
  187. /package/lib/{app/hook-use → application/hooks}/subscribe.d.ts +0 -0
  188. /package/lib/{app/hook-use → application/hooks}/user.d.ts +0 -0
  189. /package/lib/{app → application}/router/fallback.d.ts +0 -0
  190. /package/lib/{app → application}/router/fallback.js +0 -0
  191. /package/lib/{app → application}/router/main.d.ts +0 -0
  192. /package/lib/{app → application}/router/main.js +0 -0
  193. /package/lib/{app → application}/router/parser.d.ts +0 -0
  194. /package/lib/{app → application}/router/parser.js +0 -0
  195. /package/lib/{app → application}/router/types.d.ts +0 -0
  196. /package/lib/{app → application}/router/types.js +0 -0
  197. /package/lib/{app → application}/router/validator.d.ts +0 -0
  198. /package/lib/{app → application}/router/validator.js +0 -0
  199. /package/lib/{cbp → application/runtime/cbp}/processor/transport.d.ts +0 -0
  200. /package/lib/{cbp → application/runtime/cbp}/processor/transport.js +0 -0
  201. /package/lib/{app → application/runtime}/event-group.js +0 -0
  202. /package/lib/{app → application/runtime}/event-processor-cycle.js +0 -0
  203. /package/lib/{app → application/runtime}/event-processor-event.js +0 -0
  204. /package/lib/{app → application/runtime}/event-processor-middleware.js +0 -0
  205. /package/lib/{app → application/runtime}/event-response.js +0 -0
  206. /package/lib/{server → application/runtime/http}/routers/middleware.d.ts +0 -0
  207. /package/lib/{server → application/runtime/http}/routers/middleware.js +0 -0
  208. /package/lib/{cbp → application/runtime/http}/routers/router.d.ts +0 -0
  209. /package/lib/{server/main.d.ts → application/runtime/http-server.d.ts} +0 -0
  210. /package/lib/{app/load_modules → application/runtime/load-modules}/index.d.ts +0 -0
  211. /package/lib/{app/load_modules → application/runtime/load-modules}/index.js +0 -0
  212. /package/lib/{app/load_modules → application/runtime/load-modules}/load.d.ts +0 -0
  213. /package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.d.ts +0 -0
  214. /package/lib/{app/config.d.ts → application/runtime/subscribe-status.d.ts} +0 -0
  215. /package/lib/{app/config.js → application/runtime/subscribe-status.js} +0 -0
  216. /package/lib/{app → common}/SinglyLinkedList.d.ts +0 -0
  217. /package/lib/{app → common}/SinglyLinkedList.js +0 -0
  218. /package/lib/{cbp/connects/connect.d.ts → common/cbp/heartbeat.d.ts} +0 -0
  219. /package/lib/{cbp → common/cbp}/typings.js +0 -0
  220. /package/lib/{cbp/connects/base.d.ts → common/cbp/ws-connector.d.ts} +0 -0
  221. /package/lib/{process → common}/direct-channel.d.ts +0 -0
  222. /package/lib/{process → common}/direct-channel.js +0 -0
  223. /package/lib/{core → common}/react.d.ts +0 -0
  224. /package/lib/{core → common}/react.js +0 -0
  225. /package/lib/{core → common}/variable.d.ts +0 -0
  226. /package/lib/{cbp → core/cbp}/routers/hello.html.d.ts +0 -0
  227. /package/lib/{server → core/cbp}/routers/router.d.ts +0 -0
  228. /package/lib/{cbp → core/cbp}/routers/router.js +0 -0
  229. /package/lib/{cbp → core/cbp}/server/main.d.ts +0 -0
  230. /package/lib/{process → core/process}/ipc-bridge.d.ts +0 -0
  231. /package/lib/{process → core/process}/module.d.ts +0 -0
  232. /package/lib/{process → core/process}/platform.d.ts +0 -0
  233. /package/lib/{app → platform}/define-platform.d.ts +0 -0
  234. /package/lib/{app → platform}/event-format.d.ts +0 -0
  235. /package/lib/{app → platform}/event-format.js +0 -0
@@ -1,70 +1,74 @@
1
1
  import * as flattedJSON from 'flatted';
2
- import '../../app/store.js';
2
+ import { onProcessor } from '../../event-processor.js';
3
3
  import 'fs';
4
4
  import 'path';
5
5
  import 'yaml';
6
- import { ResultCode } from '../../core/variable.js';
7
- import '../../app/load_modules/loadChild.js';
8
- import '../../app/define-children.js';
9
- import '../../app/define-response.js';
10
- import '../../app/define-middleware.js';
11
- import '../../app/event-group.js';
12
- import '../../app/event-middleware.js';
13
- import { onProcessor } from '../../app/event-processor.js';
14
- import '../../app/event-response.js';
15
- import { createResult } from '../../core/utils.js';
16
- import '../../app/hook-event-context.js';
17
- import { apiResolves, apiTimeouts, actionResolves, actionTimeouts, FULL_RECEIVE_HEADER } from '../processor/config.js';
6
+ import '../../../../common/logger.js';
7
+ import { ResultCode } from '../../../../common/variable.js';
8
+ import { createResult } from '../../../../common/result.js';
9
+ import '../../../../common/utils.js';
10
+ import { createDirectServer } from '../../../../common/direct-channel.js';
11
+ import { FULL_RECEIVE_HEADER } from '../../../../common/cbp/constants.js';
12
+ import '../../../../common/cbp/runtime.js';
13
+ import { createWSConnector } from '../../../../common/cbp/ws-connector.js';
14
+ import { normalizeInboundMessage } from '../../../../common/cbp/normalize.js';
15
+ import { apiRequestResolves, apiRequestTimeouts, actionRequestResolves, actionRequestTimeouts } from '../processor/request-registry.js';
18
16
  import { setDirectSend } from '../processor/transport.js';
19
- import '../../app/message-format-old.js';
20
- import 'cron';
21
- import '../../app/event-utils.js';
22
- import '../../app/message-api.js';
23
- import { createWSConnector } from './base.js';
24
- import { createDirectServer } from '../../process/direct-channel.js';
25
17
 
26
- const handleParsedMessage = (parsedMessage) => {
27
- if (parsedMessage?.apiId) {
28
- const resolve = apiResolves.get(parsedMessage.apiId);
18
+ const handleInboundMessage = (message) => {
19
+ const normalized = normalizeInboundMessage(message);
20
+ if (!normalized) {
21
+ return;
22
+ }
23
+ if (normalized.kind === 'api.res') {
24
+ const resolve = apiRequestResolves.get(normalized.replyTo);
29
25
  if (resolve) {
30
- apiResolves.delete(parsedMessage.apiId);
31
- const timeout = apiTimeouts.get(parsedMessage.apiId);
26
+ apiRequestResolves.delete(normalized.replyTo);
27
+ const timeout = apiRequestTimeouts.get(normalized.replyTo);
32
28
  if (timeout) {
33
- apiTimeouts.delete(parsedMessage.apiId);
29
+ apiRequestTimeouts.delete(normalized.replyTo);
34
30
  clearTimeout(timeout);
35
31
  }
36
- if (Array.isArray(parsedMessage.payload)) {
37
- resolve(parsedMessage.payload);
32
+ if (Array.isArray(normalized.results)) {
33
+ resolve(normalized.results);
38
34
  }
39
35
  else {
40
36
  resolve([createResult(ResultCode.Fail, '接口处理错误', null)]);
41
37
  }
42
38
  }
43
39
  }
44
- else if (parsedMessage?.actionId) {
45
- const resolve = actionResolves.get(parsedMessage.actionId);
40
+ else if (normalized.kind === 'action.res') {
41
+ const resolve = actionRequestResolves.get(normalized.replyTo);
46
42
  if (resolve) {
47
- actionResolves.delete(parsedMessage.actionId);
48
- const timeout = actionTimeouts.get(parsedMessage.actionId);
43
+ actionRequestResolves.delete(normalized.replyTo);
44
+ const timeout = actionRequestTimeouts.get(normalized.replyTo);
49
45
  if (timeout) {
50
- actionTimeouts.delete(parsedMessage.actionId);
46
+ actionRequestTimeouts.delete(normalized.replyTo);
51
47
  clearTimeout(timeout);
52
48
  }
53
- if (Array.isArray(parsedMessage.payload)) {
54
- resolve(parsedMessage.payload);
49
+ if (Array.isArray(normalized.results)) {
50
+ resolve(normalized.results);
55
51
  }
56
52
  else {
57
53
  resolve([createResult(ResultCode.Fail, '消费处理错误', null)]);
58
54
  }
59
55
  }
60
56
  }
61
- else if (parsedMessage.name) {
62
- onProcessor(parsedMessage.name, parsedMessage, parsedMessage.value);
57
+ else if (normalized.kind === 'event') {
58
+ onProcessor(normalized.eventName, normalized.event, normalized.raw);
59
+ }
60
+ else if (normalized.kind === 'control' && normalized.op === 'sync') {
61
+ const env = normalized.payload?.env;
62
+ if (env && typeof env === 'object') {
63
+ for (const key in env) {
64
+ process.env[key] = String(env[key]);
65
+ }
66
+ }
63
67
  }
64
68
  };
65
69
  const cbpClientDirect = (sockPath, open) => {
66
70
  createDirectServer(sockPath, (data) => {
67
- handleParsedMessage(data);
71
+ handleInboundMessage(data);
68
72
  })
69
73
  .then(channel => {
70
74
  setDirectSend(channel.send);
@@ -89,7 +93,7 @@ const cbpClientIPC = (open) => {
89
93
  try {
90
94
  const msg = typeof message === 'string' ? JSON.parse(message) : message;
91
95
  if (msg?.type === 'ipc:data') {
92
- handleParsedMessage(msg.data);
96
+ handleInboundMessage(msg.data);
93
97
  }
94
98
  }
95
99
  catch (error) {
@@ -128,18 +132,7 @@ const cbpClient = (url, options = {}) => {
128
132
  onMessage: (messageStr) => {
129
133
  try {
130
134
  const parsedMessage = flattedJSON.parse(messageStr);
131
- if (parsedMessage?.activeId) {
132
- if (parsedMessage.active === 'sync') {
133
- const configs = parsedMessage.payload;
134
- const env = configs.env || {};
135
- for (const key in env) {
136
- process.env[key] = env[key];
137
- }
138
- }
139
- }
140
- else {
141
- handleParsedMessage(parsedMessage);
142
- }
135
+ handleInboundMessage(parsedMessage);
143
136
  }
144
137
  catch (error) {
145
138
  logger.error({
@@ -0,0 +1,3 @@
1
+ export { cbpClient } from './connects/client.js';
2
+ export { sendAction } from './processor/actions.js';
3
+ export { sendAPI } from './processor/api.js';
@@ -0,0 +1,3 @@
1
+ export { cbpClient } from './connects/client.js';
2
+ export { sendAction } from './processor/actions.js';
3
+ export { sendAPI } from './processor/api.js';
@@ -0,0 +1,3 @@
1
+ import { Result } from '../../../../common/index.js';
2
+ import type { Actions } from '../../../../types/index.js';
3
+ export declare const sendAction: (data: Actions) => Promise<Result[]>;
@@ -0,0 +1,58 @@
1
+ import * as flattedJSON from 'flatted';
2
+ import 'fs';
3
+ import 'path';
4
+ import 'yaml';
5
+ import '../../../../common/logger.js';
6
+ import { ResultCode } from '../../../../common/variable.js';
7
+ import { createResult } from '../../../../common/result.js';
8
+ import { sanitizeForSerialization } from '../../../../common/utils.js';
9
+ import 'net';
10
+ import 'v8';
11
+ import 'os';
12
+ import { timeoutTime } from '../../../../common/cbp/constants.js';
13
+ import { generateUniqueId, deviceId } from '../../../../common/cbp/runtime.js';
14
+ import 'ws';
15
+ import { createActionRequestEnvelope } from '../../../../common/cbp/normalize.js';
16
+ import { actionRequestResolves, actionRequestTimeouts } from './request-registry.js';
17
+ import { getDirectSend } from './transport.js';
18
+
19
+ const setupActionResolve = (requestId, resolve) => {
20
+ actionRequestResolves.set(requestId, resolve);
21
+ const timeout = setTimeout(() => {
22
+ if (!actionRequestResolves.has(requestId) || !actionRequestTimeouts.has(requestId)) {
23
+ return;
24
+ }
25
+ actionRequestResolves.delete(requestId);
26
+ actionRequestTimeouts.delete(requestId);
27
+ resolve([createResult(ResultCode.Fail, '行为超时', null)]);
28
+ }, timeoutTime);
29
+ actionRequestTimeouts.set(requestId, timeout);
30
+ };
31
+ const sendAction = (data) => {
32
+ const requestId = generateUniqueId();
33
+ return new Promise(resolve => {
34
+ data.actionId = requestId;
35
+ data.DeviceId = deviceId;
36
+ const envelope = createActionRequestEnvelope(data);
37
+ const safeData = sanitizeForSerialization(envelope);
38
+ const directSend = getDirectSend();
39
+ if (directSend) {
40
+ directSend(safeData);
41
+ setupActionResolve(requestId, resolve);
42
+ return;
43
+ }
44
+ if (process.env.__ALEMON_IPC === '1' && typeof process.send === 'function') {
45
+ process.send({ type: 'ipc:data', data: safeData });
46
+ setupActionResolve(requestId, resolve);
47
+ return;
48
+ }
49
+ if (!global.chatbotClient?.send) {
50
+ resolve([createResult(ResultCode.Fail, 'Chatbot client is not available', null)]);
51
+ return;
52
+ }
53
+ global.chatbotClient?.send(flattedJSON.stringify(safeData));
54
+ setupActionResolve(requestId, resolve);
55
+ });
56
+ };
57
+
58
+ export { sendAction };
@@ -0,0 +1,3 @@
1
+ import { Result } from '../../../../common/index.js';
2
+ import type { Apis } from '../../../../types/index.js';
3
+ export declare const sendAPI: (data: Apis) => Promise<Result[]>;
@@ -0,0 +1,58 @@
1
+ import 'fs';
2
+ import 'path';
3
+ import 'yaml';
4
+ import '../../../../common/logger.js';
5
+ import { ResultCode } from '../../../../common/variable.js';
6
+ import { createResult } from '../../../../common/result.js';
7
+ import { sanitizeForSerialization } from '../../../../common/utils.js';
8
+ import 'net';
9
+ import 'v8';
10
+ import 'os';
11
+ import * as flattedJSON from 'flatted';
12
+ import { timeoutTime } from '../../../../common/cbp/constants.js';
13
+ import { generateUniqueId, deviceId } from '../../../../common/cbp/runtime.js';
14
+ import 'ws';
15
+ import { createApiRequestEnvelope } from '../../../../common/cbp/normalize.js';
16
+ import { apiRequestResolves, apiRequestTimeouts } from './request-registry.js';
17
+ import { getDirectSend } from './transport.js';
18
+
19
+ const setupApiResolve = (requestId, resolve) => {
20
+ apiRequestResolves.set(requestId, resolve);
21
+ const timeout = setTimeout(() => {
22
+ if (!apiRequestResolves.has(requestId) || !apiRequestTimeouts.has(requestId)) {
23
+ return;
24
+ }
25
+ apiRequestResolves.delete(requestId);
26
+ apiRequestTimeouts.delete(requestId);
27
+ resolve([createResult(ResultCode.Fail, '接口超时', null)]);
28
+ }, timeoutTime);
29
+ apiRequestTimeouts.set(requestId, timeout);
30
+ };
31
+ const sendAPI = (data) => {
32
+ const requestId = generateUniqueId();
33
+ return new Promise(resolve => {
34
+ data.apiId = requestId;
35
+ data.DeviceId = deviceId;
36
+ const envelope = createApiRequestEnvelope(data);
37
+ const safeData = sanitizeForSerialization(envelope);
38
+ const directSend = getDirectSend();
39
+ if (directSend) {
40
+ directSend(safeData);
41
+ setupApiResolve(requestId, resolve);
42
+ return;
43
+ }
44
+ if (process.env.__ALEMON_IPC === '1' && typeof process.send === 'function') {
45
+ process.send({ type: 'ipc:data', data: safeData });
46
+ setupApiResolve(requestId, resolve);
47
+ return;
48
+ }
49
+ if (!global.chatbotClient?.send) {
50
+ resolve([createResult(ResultCode.Fail, 'Chatbot client is not available', null)]);
51
+ return;
52
+ }
53
+ global.chatbotClient?.send(flattedJSON.stringify(safeData));
54
+ setupApiResolve(requestId, resolve);
55
+ });
56
+ };
57
+
58
+ export { sendAPI };
@@ -0,0 +1,8 @@
1
+ import type { Result } from '../../../../common/index.js';
2
+ type RequestReplyValue = Result[] | PromiseLike<Result[]>;
3
+ type RequestReplyResolve = (value: RequestReplyValue) => void;
4
+ export declare const actionRequestResolves: Map<string, RequestReplyResolve>;
5
+ export declare const apiRequestResolves: Map<string, RequestReplyResolve>;
6
+ export declare const actionRequestTimeouts: Map<string, NodeJS.Timeout>;
7
+ export declare const apiRequestTimeouts: Map<string, NodeJS.Timeout>;
8
+ export {};
@@ -0,0 +1,6 @@
1
+ const actionRequestResolves = new Map();
2
+ const apiRequestResolves = new Map();
3
+ const actionRequestTimeouts = new Map();
4
+ const apiRequestTimeouts = new Map();
5
+
6
+ export { actionRequestResolves, actionRequestTimeouts, apiRequestResolves, apiRequestTimeouts };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,95 @@
1
+ import { cbpClient } from './cbp/connects/client.js';
2
+ import 'flatted';
3
+ import 'fs';
4
+ import 'path';
5
+ import 'yaml';
6
+ import '../../common/logger.js';
7
+ import { defaultPort } from '../../common/variable.js';
8
+ import '../../common/utils.js';
9
+ import 'net';
10
+ import 'v8';
11
+ import 'os';
12
+ import '../../common/cbp/runtime.js';
13
+ import 'ws';
14
+ import { loadModels } from './load-modules/load.js';
15
+ import { createServer } from './http-server.js';
16
+ import { disposeAllRuntimeApps } from './store.js';
17
+ import { scheduleCancelByApp, unregisterAppDir } from './schedule-store.js';
18
+ import { dispatchDisposeAllApps } from './lifecycle-callbacks.js';
19
+
20
+ global.__client_loaded = true;
21
+ let runtimeDisposed = false;
22
+ const disposeRuntime = async () => {
23
+ if (runtimeDisposed) {
24
+ return;
25
+ }
26
+ runtimeDisposed = true;
27
+ await dispatchDisposeAllApps();
28
+ const apps = disposeAllRuntimeApps();
29
+ apps.forEach(app => {
30
+ scheduleCancelByApp(app.name);
31
+ unregisterAppDir(app.name);
32
+ });
33
+ };
34
+ const shutdown = async (reason) => {
35
+ logger.info?.(`[alemonjs][${reason}] 收到信号,正在关闭...`);
36
+ await disposeRuntime();
37
+ process.exit(0);
38
+ };
39
+ const mainServer = () => {
40
+ const port = process.env.serverPort;
41
+ if (!port) {
42
+ return;
43
+ }
44
+ createServer(port, () => {
45
+ const httpURL = `http://127.0.0.1:${port}`;
46
+ logger.info(`应用服务器: ${httpURL}`);
47
+ });
48
+ };
49
+ const main = () => {
50
+ const login = process.env.login ?? '';
51
+ const platform = process.env.platform ?? '';
52
+ const url = process.env.url ?? '';
53
+ const port = process.env.port ?? defaultPort;
54
+ const isFullReceive = process.env.is_full_receive === 'true' || process.env.is_full_receive === '1';
55
+ if (!login && !platform && url) {
56
+ logger.info(`[Connecting to CBP server at ${url}]`);
57
+ cbpClient(url);
58
+ }
59
+ else {
60
+ cbpClient(`http://127.0.0.1:${port}`, { isFullReceive });
61
+ }
62
+ loadModels();
63
+ };
64
+ const mainProcess = () => {
65
+ process.on('unhandledRejection', (reason) => {
66
+ logger.error('[alemonjs][unhandledRejection] 未捕获的 Promise 拒绝:', reason);
67
+ });
68
+ process.on('uncaughtException', (error) => {
69
+ logger.error('[alemonjs][uncaughtException] 未捕获的异常:', error);
70
+ });
71
+ ['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
72
+ process?.on?.(sig, () => void shutdown(sig));
73
+ });
74
+ process?.on?.('exit', code => {
75
+ void disposeRuntime();
76
+ logger.info?.(`[alemonjs][exit] 进程退出,code=${code}`);
77
+ });
78
+ process.on('message', msg => {
79
+ try {
80
+ const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
81
+ if (data?.type === 'start') {
82
+ main();
83
+ mainServer();
84
+ }
85
+ else if (data?.type === 'stop') {
86
+ void shutdown('stop');
87
+ }
88
+ }
89
+ catch { }
90
+ });
91
+ if (process.send) {
92
+ process.send({ type: 'ready' });
93
+ }
94
+ };
95
+ mainProcess();
@@ -1,2 +1,2 @@
1
- import type { EventErrorContext } from '../types';
1
+ import type { EventErrorContext } from '../../types/index.js';
2
2
  export declare const dispatchEventError: (context: EventErrorContext) => Promise<boolean>;
@@ -1,5 +1,5 @@
1
1
  import { getChildrenApp } from './store.js';
2
- import { showErrorModule } from '../core/utils.js';
2
+ import { showErrorModule } from '../../common/utils.js';
3
3
 
4
4
  const dispatchEventError = async (context) => {
5
5
  const app = getChildrenApp(context.appName);
@@ -1,3 +1,3 @@
1
- import { OnGroupFunc } from '../types';
1
+ import { OnGroupFunc } from '../../types/index.js';
2
2
  declare const onGroup: OnGroupFunc;
3
3
  export { onGroup };
@@ -1,3 +1,3 @@
1
- import { OnMiddlewareReversalFunc, OnMiddlewareReversalFuncBack } from '../types';
1
+ import { OnMiddlewareReversalFunc, OnMiddlewareReversalFuncBack } from '../../types/index.js';
2
2
  export declare const onMiddleware: OnMiddlewareReversalFunc;
3
3
  export declare const OnMiddleware: OnMiddlewareReversalFuncBack;
@@ -1,4 +1,4 @@
1
- import { ResultCode } from '../core/variable.js';
1
+ import { ResultCode } from '../../common/variable.js';
2
2
 
3
3
  const onMiddleware = (select, callback) => {
4
4
  if (typeof callback !== 'function') {
@@ -1,4 +1,4 @@
1
- import type { EventErrorPhase } from '../types';
1
+ import type { EventErrorPhase } from '../../types/index.js';
2
2
  export declare const createCallHandler: (valueEvent: any) => (currents: any, nextEvent: any, meta?: {
3
3
  appName?: string;
4
4
  phase?: EventErrorPhase;
@@ -1,7 +1,14 @@
1
1
  import 'fs';
2
2
  import 'path';
3
3
  import 'yaml';
4
- import { showErrorModule } from '../core/utils.js';
4
+ import '../../common/logger.js';
5
+ import { showErrorModule } from '../../common/utils.js';
6
+ import 'net';
7
+ import 'v8';
8
+ import 'os';
9
+ import 'flatted';
10
+ import '../../common/cbp/runtime.js';
11
+ import 'ws';
5
12
  import { withEventContext, finishCurrentTrace } from './hook-event-context.js';
6
13
  import { dispatchEventError } from './event-error.js';
7
14
 
@@ -1,2 +1,2 @@
1
- import { Events, EventKeys } from '../types';
1
+ import { Events, EventKeys } from '../../types/index.js';
2
2
  export declare const expendCycle: <T extends EventKeys>(valueEvent: Events[T], select: T, _config?: any) => void;
@@ -1,4 +1,4 @@
1
- import { Next, Events, EventKeys, FileTreeNode, StoreResponseItem } from '../types';
1
+ import { Next, Events, EventKeys, FileTreeNode, StoreResponseItem } from '../../types/index.js';
2
2
  export declare const clearModuleCache: (path?: string) => void;
3
3
  export declare const createNextStep: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next, files: StoreResponseItem[], callHandler: (currents: any, nextEvent: any) => void) => Next;
4
4
  export declare const createFileTreeStep: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next, root: FileTreeNode, callHandler: (currents: any, nextEvent: any, meta?: {
@@ -1,5 +1,5 @@
1
- import { getCachedRegExp, showErrorModule } from '../core/utils.js';
2
- import { EventMessageText } from '../core/variable.js';
1
+ import { getCachedRegExp, showErrorModule } from '../../common/utils.js';
2
+ import { EventMessageText } from '../../common/variable.js';
3
3
  import { dispatchEventError } from './event-error.js';
4
4
 
5
5
  const moduleCache = new Map();
@@ -1,4 +1,4 @@
1
- import { Next, Events, EventKeys, ResponseRoute } from '../types';
1
+ import { Next, Events, EventKeys, ResponseRoute } from '../../types/index.js';
2
2
  export declare const createRouteProcessChildren: <T extends EventKeys>(valueEvent: Events[T], select: T, nextCycle: Next, callHandler: (currents: any, nextEvent: any, meta?: {
3
3
  appName?: string;
4
4
  phase?: "route";
@@ -1,8 +1,15 @@
1
- import { EventMessageText } from '../core/variable.js';
1
+ import { EventMessageText } from '../../common/variable.js';
2
2
  import 'fs';
3
3
  import 'path';
4
4
  import 'yaml';
5
- import { getCachedRegExp, showErrorModule } from '../core/utils.js';
5
+ import '../../common/logger.js';
6
+ import { getCachedRegExp, showErrorModule } from '../../common/utils.js';
7
+ import 'net';
8
+ import 'v8';
9
+ import 'os';
10
+ import 'flatted';
11
+ import '../../common/cbp/runtime.js';
12
+ import 'ws';
6
13
  import { dispatchEventError } from './event-error.js';
7
14
 
8
15
  const AsyncFunction = (async () => { }).constructor;
@@ -1,2 +1,2 @@
1
- import { Next, Events, EventKeys } from '../types';
1
+ import { Next, Events, EventKeys } from '../../types/index.js';
2
2
  export declare const expendEvent: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next) => void;
@@ -1,2 +1,2 @@
1
- import { Next, Events, EventKeys } from '../types';
1
+ import { Next, Events, EventKeys } from '../../types/index.js';
2
2
  export declare const expendMiddleware: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next) => void;
@@ -1,4 +1,4 @@
1
- import { Next, Events, EventCycleEnum, EventKeys } from '../types';
1
+ import { Next, Events, EventCycleEnum, EventKeys } from '../../types/index.js';
2
2
  export declare const expendSubscribe: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next, choose: EventCycleEnum) => void;
3
3
  export declare const expendSubscribeCreate: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next) => void;
4
4
  export declare const expendSubscribeMount: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next) => void;
@@ -1,9 +1,16 @@
1
1
  import 'fs';
2
2
  import 'path';
3
3
  import 'yaml';
4
- import { showErrorModule } from '../core/utils.js';
4
+ import '../../common/logger.js';
5
+ import { showErrorModule } from '../../common/utils.js';
6
+ import 'net';
7
+ import 'v8';
8
+ import 'os';
9
+ import 'flatted';
10
+ import '../../common/cbp/runtime.js';
11
+ import 'ws';
5
12
  import { getSubscribeList } from './store.js';
6
- import { SubscribeStatus } from './config.js';
13
+ import { SubscribeStatus } from './subscribe-status.js';
7
14
  import { withEventContext, finishCurrentTrace } from './hook-event-context.js';
8
15
  import { dispatchEventError } from './event-error.js';
9
16
 
@@ -1,3 +1,3 @@
1
- import { EventKeys, Events } from '../types';
1
+ import { EventKeys, Events } from '../../types/index.js';
2
2
  export declare const onProcessor: <T extends EventKeys>(name: T, event: Events[T], data?: any) => void;
3
3
  export declare const OnProcessor: <T extends EventKeys>(event: Events[T], name: T) => void;
@@ -1,10 +1,11 @@
1
- import { getConfigValue } from '../core/config.js';
2
- import { processorRepeatedClearTimeMin, processorRepeatedClearTimeMax, processorRepeatedEventTime, processorRepeatedUserTime, processorRepeatedClearSize, processorMaxMapSize } from '../core/variable.js';
1
+ import { getConfigValue } from '../../common/config.js';
2
+ import { processorRepeatedClearTimeMin, processorRepeatedClearTimeMax, processorRepeatedEventTime, processorRepeatedUserTime, processorRepeatedClearSize, processorMaxMapSize } from '../../common/variable.js';
3
3
  import { expendCycle } from './event-processor-cycle.js';
4
4
  import { withProcessorTrace, finishCurrentTrace } from './hook-event-context.js';
5
5
  import { ProcessorEventAutoClearMap, ProcessorEventUserAutoClearMap } from './store.js';
6
- import { getCachedRegExp, matchIn, fastHash } from '../core/utils.js';
6
+ import { getCachedRegExp } from '../../common/utils.js';
7
7
  import { dispatchEventStart } from './lifecycle-callbacks.js';
8
+ import { matchIn, fastHash } from '../../common/identity.js';
8
9
 
9
10
  const filter = ({ Now, store, INTERVAL }, MessageId) => {
10
11
  if (store.has(MessageId)) {
@@ -1,3 +1,3 @@
1
- import { OnResponseReversalFunc, OnResponseReversalFuncBack } from '../types';
1
+ import { OnResponseReversalFunc, OnResponseReversalFuncBack } from '../../types/index.js';
2
2
  export declare const onResponse: OnResponseReversalFunc;
3
3
  export declare const OnResponse: OnResponseReversalFuncBack;
@@ -1,4 +1,4 @@
1
- import { EventKeys } from '../types';
1
+ import { EventKeys } from '../../types/index.js';
2
2
  type BaseMap = {
3
3
  [key: string]: unknown;
4
4
  };
@@ -1,6 +1,6 @@
1
- import { getConfig } from '../core/config.js';
1
+ import { getConfig } from '../../common/config.js';
2
2
  import { StateSubscribe, ChildrenApp, State } from './store.js';
3
- import { ResultCode } from '../core/variable.js';
3
+ import { ResultCode } from '../../common/variable.js';
4
4
 
5
5
  const unChildren = (name = 'main') => {
6
6
  if (!name || typeof name !== 'string') {
@@ -1,5 +1,5 @@
1
- import { Result } from '../core';
2
- import { EventErrorPhase, EventKeys, Events, EventTraceReason } from '../types';
1
+ import { Result } from '../../common/index.js';
2
+ import { EventErrorPhase, EventKeys, Events, EventTraceReason } from '../../types/index.js';
3
3
  export declare const withEventContext: <T extends EventKeys, R>(event: Events[T], next: (...args: boolean[]) => void, runner: () => R, options?: {
4
4
  appName?: string;
5
5
  phase?: EventErrorPhase;
@@ -1,7 +1,7 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { performance } from 'node:perf_hooks';
3
- import { getConfigValue } from '../core/config.js';
4
- import { ResultCode } from '../core/variable.js';
3
+ import { getConfigValue } from '../../common/config.js';
4
+ import { ResultCode } from '../../common/variable.js';
5
5
  import { dispatchEventFinished } from './lifecycle-callbacks.js';
6
6
 
7
7
  const eventStore = new AsyncLocalStorage();
@@ -0,0 +1,4 @@
1
+ export { createServer } from '../http-server.js';
2
+ export { default as MessageRouter } from './routers/router.js';
3
+ export * from './routers/middleware.js';
4
+ export * from './routers/utils.js';
@@ -0,0 +1,4 @@
1
+ export { createServer } from '../http-server.js';
2
+ export { default as MessageRouter } from './routers/router.js';
3
+ export { collectMiddlewares, runMiddlewares } from './routers/middleware.js';
4
+ export { formatPath, getModuelFile, isValidPackageName, safePath } from './routers/utils.js';
@@ -0,0 +1,4 @@
1
+ import type { RuntimeAppRecord } from '../../store.js';
2
+ export declare const renderHelloHtml: (apps: RuntimeAppRecord[]) => string;
3
+ declare const _default: string;
4
+ export default _default;