alemonjs 2.1.89 → 2.1.91

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.
@@ -11,6 +11,11 @@ import { deviceId } from '../common/cbp/runtime.js';
11
11
  import { createWSConnector } from '../common/cbp/ws-connector.js';
12
12
  import { createEventEnvelope, normalizeInboundMessage, isNormalizedApiRequest, toLegacyApiData, isNormalizedActionRequest, toLegacyActionData, createApiResponseEnvelope, createActionResponseEnvelope } from '../common/cbp/normalize.js';
13
13
 
14
+ const notifyTransportReady = (transport) => {
15
+ if (typeof process.send === 'function') {
16
+ process.send({ type: 'transport_ready', protocolVersion: 'v2', transport });
17
+ }
18
+ };
14
19
  const dispatchLegacyActionHandlers = (actionReplys, replyAction, input) => {
15
20
  const normalized = normalizeInboundMessage(input);
16
21
  if (!isNormalizedActionRequest(normalized)) {
@@ -73,6 +78,7 @@ const cbpPlatformDirect = (sockPath, open) => {
73
78
  channel.send(msg);
74
79
  }
75
80
  pendingQueue.length = 0;
81
+ notifyTransportReady('direct');
76
82
  open();
77
83
  logger.debug({
78
84
  code: ResultCode.Ok,
@@ -141,6 +147,7 @@ const cbpPlatformIPC = (open, existingActionReplys, existingApiReplys) => {
141
147
  });
142
148
  }
143
149
  });
150
+ notifyTransportReady('ipc');
144
151
  open();
145
152
  logger.debug({
146
153
  code: ResultCode.Ok,
@@ -207,7 +214,10 @@ const cbpPlatform = (url, options = {
207
214
  url: currentURL,
208
215
  role: 'platform',
209
216
  globalKey: 'chatbotPlatform',
210
- onOpen: open,
217
+ onOpen: () => {
218
+ notifyTransportReady('ws');
219
+ open();
220
+ },
211
221
  onMessage: (messageStr) => {
212
222
  try {
213
223
  const data = flattedJSON.parse(messageStr);
@@ -2,6 +2,9 @@ import { logger } from '../common/logger.js';
2
2
 
3
3
  const definePlatform = (options) => {
4
4
  const platformName = options.name || process.env.platform || 'unknown';
5
+ if (global.__platform_bootstrap_loaded) {
6
+ return options.main;
7
+ }
5
8
  const mainProcess = () => {
6
9
  ['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
7
10
  process?.on?.(sig, () => {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,85 @@
1
+ import { logger } from './common/logger.js';
2
+
3
+ let runtimeStarted = false;
4
+ let stopping = false;
5
+ const normalizeError = (error) => {
6
+ if (error instanceof Error) {
7
+ return {
8
+ message: error.message,
9
+ stack: error.stack
10
+ };
11
+ }
12
+ return {
13
+ message: typeof error === 'string' ? error : 'Unknown platform bootstrap error'
14
+ };
15
+ };
16
+ const notifyParent = (message) => {
17
+ if (typeof process.send === 'function') {
18
+ process.send(message);
19
+ }
20
+ };
21
+ const shutdown = (reason) => {
22
+ if (stopping) {
23
+ return;
24
+ }
25
+ stopping = true;
26
+ logger.info?.(`[platform-bootstrap][${reason}] 收到信号,正在关闭...`);
27
+ setImmediate(() => process.exit(0));
28
+ };
29
+ const startRuntime = async () => {
30
+ if (runtimeStarted) {
31
+ return;
32
+ }
33
+ runtimeStarted = true;
34
+ global.__platform_bootstrap_loaded = true;
35
+ try {
36
+ const entryPath = String(process.env.__ALEMON_PLATFORM_ENTRY ?? '').trim();
37
+ if (!entryPath) {
38
+ throw new Error('Missing __ALEMON_PLATFORM_ENTRY');
39
+ }
40
+ const importPath = entryPath.startsWith('file://') ? entryPath : `file://${entryPath}`;
41
+ const mod = await import(importPath);
42
+ const run = typeof mod.default === 'function' ? mod.default : typeof mod.main === 'function' ? mod.main : null;
43
+ if (typeof run !== 'function') {
44
+ throw new Error('Platform entry must export a callable default or main function');
45
+ }
46
+ await run();
47
+ notifyParent({ type: 'app_ready', protocolVersion: 'v2' });
48
+ }
49
+ catch (error) {
50
+ notifyParent({
51
+ type: 'boot_error',
52
+ protocolVersion: 'v2',
53
+ stage: 'app',
54
+ error: normalizeError(error)
55
+ });
56
+ logger.error?.('[platform-bootstrap] 启动失败', error);
57
+ process.exitCode = 1;
58
+ setImmediate(() => process.exit(1));
59
+ }
60
+ };
61
+ process.on('unhandledRejection', (reason) => {
62
+ logger.error?.('[platform-bootstrap][unhandledRejection]', reason);
63
+ });
64
+ process.on('uncaughtException', (error) => {
65
+ logger.error?.('[platform-bootstrap][uncaughtException]', error);
66
+ });
67
+ ['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
68
+ process?.on?.(sig, () => shutdown(sig));
69
+ });
70
+ process?.on?.('exit', code => {
71
+ logger.info?.(`[platform-bootstrap][exit] 进程退出,code=${code}`);
72
+ });
73
+ process.on('message', msg => {
74
+ try {
75
+ const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
76
+ if (data?.type === 'start') {
77
+ void startRuntime();
78
+ }
79
+ else if (data?.type === 'stop') {
80
+ shutdown('stop');
81
+ }
82
+ }
83
+ catch { }
84
+ });
85
+ notifyParent({ type: 'ready', protocolVersion: 'v2' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alemonjs",
3
- "version": "2.1.89",
3
+ "version": "2.1.91",
4
4
  "description": "bot script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",