lightclawbot 1.1.2 → 1.2.0-beta.1
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/README.md +0 -1
- package/dist/index.js +38 -31
- package/dist/setup-entry.js +32 -0
- package/dist/src/channel.js +166 -253
- package/dist/src/channel.setup.js +16 -0
- package/dist/src/config.js +31 -124
- package/dist/src/dedup.js +0 -1
- package/dist/src/download-tool.js +0 -1
- package/dist/src/file-storage.js +2 -2
- package/dist/src/gateway.js +224 -178
- package/dist/src/history/cron-utils.js +0 -1
- package/dist/src/history/index.js +0 -1
- package/dist/src/history/message-parser.js +0 -1
- package/dist/src/history/session-reader.js +3 -1
- package/dist/src/history/session-store.js +0 -1
- package/dist/src/history/text-processing.js +0 -1
- package/dist/src/history/types.js +0 -1
- package/dist/src/inbound.js +165 -234
- package/dist/src/media.js +0 -1
- package/dist/src/messaging.js +65 -0
- package/dist/src/outbound.js +129 -45
- package/dist/src/runtime.js +9 -11
- package/dist/src/setup-core.js +27 -0
- package/dist/src/shared.js +141 -0
- package/dist/src/socket/handlers.js +72 -49
- package/dist/src/socket/index.js +0 -1
- package/dist/src/socket/native-socket.js +385 -0
- package/dist/src/socket/registry.js +0 -1
- package/dist/src/socket/reliable-emitter.js +188 -50
- package/dist/src/streaming/delta-tracker.js +140 -0
- package/dist/src/streaming/index.js +12 -0
- package/dist/src/streaming/stream-reply-sink.js +387 -0
- package/dist/src/streaming/types.js +4 -0
- package/dist/src/tools.js +13 -0
- package/dist/src/types.js +22 -2
- package/dist/src/upload-tool.js +0 -1
- package/dist/src/utils/account.js +73 -0
- package/dist/src/utils/common.js +48 -0
- package/dist/src/utils/index.js +2 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/constants.js +1 -0
- package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/permessage-deflate.js +6 -6
- package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/websocket-server.js +10 -6
- package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/websocket.js +19 -14
- package/node_modules/{engine.io-client/node_modules/ws → ws}/package.json +4 -3
- package/node_modules/ws/wrapper.mjs +21 -0
- package/openclaw.plugin.json +0 -7
- package/package.json +8 -5
- package/dist/index.d.ts +0 -24
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/public/data/scripts/manifest.json +0 -11
- package/dist/public/data/scripts/preflight.9af62b39.sh +0 -191
- package/dist/public/data/scripts/preflight.sh +0 -191
- package/dist/src/channel.d.ts +0 -11
- package/dist/src/channel.d.ts.map +0 -1
- package/dist/src/channel.js.map +0 -1
- package/dist/src/config.d.ts +0 -129
- package/dist/src/config.d.ts.map +0 -1
- package/dist/src/config.js.map +0 -1
- package/dist/src/dedup.d.ts +0 -12
- package/dist/src/dedup.d.ts.map +0 -1
- package/dist/src/dedup.js.map +0 -1
- package/dist/src/download-tool.d.ts +0 -32
- package/dist/src/download-tool.d.ts.map +0 -1
- package/dist/src/download-tool.js.map +0 -1
- package/dist/src/file-storage.d.ts +0 -74
- package/dist/src/file-storage.d.ts.map +0 -1
- package/dist/src/file-storage.js.map +0 -1
- package/dist/src/format-urls.d.ts +0 -26
- package/dist/src/format-urls.d.ts.map +0 -1
- package/dist/src/format-urls.js +0 -53
- package/dist/src/format-urls.js.map +0 -1
- package/dist/src/gateway.d.ts +0 -16
- package/dist/src/gateway.d.ts.map +0 -1
- package/dist/src/gateway.js.map +0 -1
- package/dist/src/history/cron-utils.d.ts +0 -76
- package/dist/src/history/cron-utils.d.ts.map +0 -1
- package/dist/src/history/cron-utils.js.map +0 -1
- package/dist/src/history/index.d.ts +0 -20
- package/dist/src/history/index.d.ts.map +0 -1
- package/dist/src/history/index.js.map +0 -1
- package/dist/src/history/message-parser.d.ts +0 -52
- package/dist/src/history/message-parser.d.ts.map +0 -1
- package/dist/src/history/message-parser.js.map +0 -1
- package/dist/src/history/session-reader.d.ts +0 -58
- package/dist/src/history/session-reader.d.ts.map +0 -1
- package/dist/src/history/session-reader.js.map +0 -1
- package/dist/src/history/session-store.d.ts +0 -42
- package/dist/src/history/session-store.d.ts.map +0 -1
- package/dist/src/history/session-store.js.map +0 -1
- package/dist/src/history/text-processing.d.ts +0 -41
- package/dist/src/history/text-processing.d.ts.map +0 -1
- package/dist/src/history/text-processing.js.map +0 -1
- package/dist/src/history/types.d.ts +0 -134
- package/dist/src/history/types.d.ts.map +0 -1
- package/dist/src/history/types.js.map +0 -1
- package/dist/src/inbound.d.ts +0 -18
- package/dist/src/inbound.d.ts.map +0 -1
- package/dist/src/inbound.js.map +0 -1
- package/dist/src/media.d.ts +0 -12
- package/dist/src/media.d.ts.map +0 -1
- package/dist/src/media.js.map +0 -1
- package/dist/src/outbound.d.ts +0 -28
- package/dist/src/outbound.d.ts.map +0 -1
- package/dist/src/outbound.js.map +0 -1
- package/dist/src/runtime.d.ts +0 -4
- package/dist/src/runtime.d.ts.map +0 -1
- package/dist/src/runtime.js.map +0 -1
- package/dist/src/socket/handlers.d.ts +0 -26
- package/dist/src/socket/handlers.d.ts.map +0 -1
- package/dist/src/socket/handlers.js.map +0 -1
- package/dist/src/socket/index.d.ts +0 -11
- package/dist/src/socket/index.d.ts.map +0 -1
- package/dist/src/socket/index.js.map +0 -1
- package/dist/src/socket/registry.d.ts +0 -59
- package/dist/src/socket/registry.d.ts.map +0 -1
- package/dist/src/socket/registry.js.map +0 -1
- package/dist/src/socket/reliable-emitter.d.ts +0 -79
- package/dist/src/socket/reliable-emitter.d.ts.map +0 -1
- package/dist/src/socket/reliable-emitter.js.map +0 -1
- package/dist/src/types.d.ts +0 -112
- package/dist/src/types.d.ts.map +0 -1
- package/dist/src/types.js.map +0 -1
- package/dist/src/upload-tool.d.ts +0 -27
- package/dist/src/upload-tool.d.ts.map +0 -1
- package/dist/src/upload-tool.js.map +0 -1
- package/node_modules/@socket.io/component-emitter/LICENSE +0 -24
- package/node_modules/@socket.io/component-emitter/Readme.md +0 -79
- package/node_modules/@socket.io/component-emitter/lib/cjs/index.d.ts +0 -179
- package/node_modules/@socket.io/component-emitter/lib/cjs/index.js +0 -176
- package/node_modules/@socket.io/component-emitter/lib/cjs/package.json +0 -4
- package/node_modules/@socket.io/component-emitter/lib/esm/index.d.ts +0 -179
- package/node_modules/@socket.io/component-emitter/lib/esm/index.js +0 -169
- package/node_modules/@socket.io/component-emitter/lib/esm/package.json +0 -4
- package/node_modules/@socket.io/component-emitter/package.json +0 -28
- package/node_modules/debug/LICENSE +0 -20
- package/node_modules/debug/README.md +0 -481
- package/node_modules/debug/package.json +0 -64
- package/node_modules/debug/src/browser.js +0 -272
- package/node_modules/debug/src/common.js +0 -292
- package/node_modules/debug/src/index.js +0 -10
- package/node_modules/debug/src/node.js +0 -263
- package/node_modules/engine.io-client/LICENSE +0 -22
- package/node_modules/engine.io-client/README.md +0 -331
- package/node_modules/engine.io-client/build/cjs/browser-entrypoint.d.ts +0 -3
- package/node_modules/engine.io-client/build/cjs/browser-entrypoint.js +0 -4
- package/node_modules/engine.io-client/build/cjs/contrib/has-cors.d.ts +0 -1
- package/node_modules/engine.io-client/build/cjs/contrib/has-cors.js +0 -14
- package/node_modules/engine.io-client/build/cjs/contrib/parseqs.d.ts +0 -15
- package/node_modules/engine.io-client/build/cjs/contrib/parseqs.js +0 -38
- package/node_modules/engine.io-client/build/cjs/contrib/parseuri.d.ts +0 -1
- package/node_modules/engine.io-client/build/cjs/contrib/parseuri.js +0 -67
- package/node_modules/engine.io-client/build/cjs/globals.d.ts +0 -4
- package/node_modules/engine.io-client/build/cjs/globals.js +0 -26
- package/node_modules/engine.io-client/build/cjs/globals.node.d.ts +0 -21
- package/node_modules/engine.io-client/build/cjs/globals.node.js +0 -97
- package/node_modules/engine.io-client/build/cjs/index.d.ts +0 -15
- package/node_modules/engine.io-client/build/cjs/index.js +0 -32
- package/node_modules/engine.io-client/build/cjs/package.json +0 -10
- package/node_modules/engine.io-client/build/cjs/socket.d.ts +0 -482
- package/node_modules/engine.io-client/build/cjs/socket.js +0 -765
- package/node_modules/engine.io-client/build/cjs/transport.d.ts +0 -106
- package/node_modules/engine.io-client/build/cjs/transport.js +0 -153
- package/node_modules/engine.io-client/build/cjs/transports/index.d.ts +0 -8
- package/node_modules/engine.io-client/build/cjs/transports/index.js +0 -11
- package/node_modules/engine.io-client/build/cjs/transports/polling-fetch.d.ts +0 -15
- package/node_modules/engine.io-client/build/cjs/transports/polling-fetch.js +0 -60
- package/node_modules/engine.io-client/build/cjs/transports/polling-xhr.d.ts +0 -108
- package/node_modules/engine.io-client/build/cjs/transports/polling-xhr.js +0 -285
- package/node_modules/engine.io-client/build/cjs/transports/polling-xhr.node.d.ts +0 -11
- package/node_modules/engine.io-client/build/cjs/transports/polling-xhr.node.js +0 -44
- package/node_modules/engine.io-client/build/cjs/transports/polling.d.ts +0 -52
- package/node_modules/engine.io-client/build/cjs/transports/polling.js +0 -165
- package/node_modules/engine.io-client/build/cjs/transports/websocket.d.ts +0 -36
- package/node_modules/engine.io-client/build/cjs/transports/websocket.js +0 -136
- package/node_modules/engine.io-client/build/cjs/transports/websocket.node.d.ts +0 -14
- package/node_modules/engine.io-client/build/cjs/transports/websocket.node.js +0 -68
- package/node_modules/engine.io-client/build/cjs/transports/webtransport.d.ts +0 -18
- package/node_modules/engine.io-client/build/cjs/transports/webtransport.js +0 -94
- package/node_modules/engine.io-client/build/cjs/util.d.ts +0 -7
- package/node_modules/engine.io-client/build/cjs/util.js +0 -65
- package/node_modules/engine.io-client/build/esm/browser-entrypoint.d.ts +0 -3
- package/node_modules/engine.io-client/build/esm/browser-entrypoint.js +0 -2
- package/node_modules/engine.io-client/build/esm/contrib/has-cors.d.ts +0 -1
- package/node_modules/engine.io-client/build/esm/contrib/has-cors.js +0 -11
- package/node_modules/engine.io-client/build/esm/contrib/parseqs.d.ts +0 -15
- package/node_modules/engine.io-client/build/esm/contrib/parseqs.js +0 -34
- package/node_modules/engine.io-client/build/esm/contrib/parseuri.d.ts +0 -1
- package/node_modules/engine.io-client/build/esm/contrib/parseuri.js +0 -64
- package/node_modules/engine.io-client/build/esm/globals.d.ts +0 -4
- package/node_modules/engine.io-client/build/esm/globals.js +0 -22
- package/node_modules/engine.io-client/build/esm/globals.node.d.ts +0 -21
- package/node_modules/engine.io-client/build/esm/globals.node.js +0 -91
- package/node_modules/engine.io-client/build/esm/index.d.ts +0 -15
- package/node_modules/engine.io-client/build/esm/index.js +0 -15
- package/node_modules/engine.io-client/build/esm/package.json +0 -10
- package/node_modules/engine.io-client/build/esm/socket.d.ts +0 -482
- package/node_modules/engine.io-client/build/esm/socket.js +0 -727
- package/node_modules/engine.io-client/build/esm/transport.d.ts +0 -106
- package/node_modules/engine.io-client/build/esm/transport.js +0 -142
- package/node_modules/engine.io-client/build/esm/transports/index.d.ts +0 -8
- package/node_modules/engine.io-client/build/esm/transports/index.js +0 -8
- package/node_modules/engine.io-client/build/esm/transports/polling-fetch.d.ts +0 -15
- package/node_modules/engine.io-client/build/esm/transports/polling-fetch.js +0 -56
- package/node_modules/engine.io-client/build/esm/transports/polling-xhr.d.ts +0 -108
- package/node_modules/engine.io-client/build/esm/transports/polling-xhr.js +0 -271
- package/node_modules/engine.io-client/build/esm/transports/polling-xhr.node.d.ts +0 -11
- package/node_modules/engine.io-client/build/esm/transports/polling-xhr.node.js +0 -17
- package/node_modules/engine.io-client/build/esm/transports/polling.d.ts +0 -52
- package/node_modules/engine.io-client/build/esm/transports/polling.js +0 -145
- package/node_modules/engine.io-client/build/esm/transports/websocket.d.ts +0 -36
- package/node_modules/engine.io-client/build/esm/transports/websocket.js +0 -125
- package/node_modules/engine.io-client/build/esm/transports/websocket.node.d.ts +0 -14
- package/node_modules/engine.io-client/build/esm/transports/websocket.node.js +0 -41
- package/node_modules/engine.io-client/build/esm/transports/webtransport.d.ts +0 -18
- package/node_modules/engine.io-client/build/esm/transports/webtransport.js +0 -80
- package/node_modules/engine.io-client/build/esm/util.d.ts +0 -7
- package/node_modules/engine.io-client/build/esm/util.js +0 -59
- package/node_modules/engine.io-client/build/esm-debug/browser-entrypoint.d.ts +0 -3
- package/node_modules/engine.io-client/build/esm-debug/browser-entrypoint.js +0 -2
- package/node_modules/engine.io-client/build/esm-debug/contrib/has-cors.d.ts +0 -1
- package/node_modules/engine.io-client/build/esm-debug/contrib/has-cors.js +0 -11
- package/node_modules/engine.io-client/build/esm-debug/contrib/parseqs.d.ts +0 -15
- package/node_modules/engine.io-client/build/esm-debug/contrib/parseqs.js +0 -34
- package/node_modules/engine.io-client/build/esm-debug/contrib/parseuri.d.ts +0 -1
- package/node_modules/engine.io-client/build/esm-debug/contrib/parseuri.js +0 -64
- package/node_modules/engine.io-client/build/esm-debug/globals.d.ts +0 -4
- package/node_modules/engine.io-client/build/esm-debug/globals.js +0 -22
- package/node_modules/engine.io-client/build/esm-debug/globals.node.d.ts +0 -21
- package/node_modules/engine.io-client/build/esm-debug/globals.node.js +0 -91
- package/node_modules/engine.io-client/build/esm-debug/index.d.ts +0 -15
- package/node_modules/engine.io-client/build/esm-debug/index.js +0 -15
- package/node_modules/engine.io-client/build/esm-debug/package.json +0 -10
- package/node_modules/engine.io-client/build/esm-debug/socket.d.ts +0 -482
- package/node_modules/engine.io-client/build/esm-debug/socket.js +0 -756
- package/node_modules/engine.io-client/build/esm-debug/transport.d.ts +0 -106
- package/node_modules/engine.io-client/build/esm-debug/transport.js +0 -145
- package/node_modules/engine.io-client/build/esm-debug/transports/index.d.ts +0 -8
- package/node_modules/engine.io-client/build/esm-debug/transports/index.js +0 -8
- package/node_modules/engine.io-client/build/esm-debug/transports/polling-fetch.d.ts +0 -15
- package/node_modules/engine.io-client/build/esm-debug/transports/polling-fetch.js +0 -56
- package/node_modules/engine.io-client/build/esm-debug/transports/polling-xhr.d.ts +0 -108
- package/node_modules/engine.io-client/build/esm-debug/transports/polling-xhr.js +0 -276
- package/node_modules/engine.io-client/build/esm-debug/transports/polling-xhr.node.d.ts +0 -11
- package/node_modules/engine.io-client/build/esm-debug/transports/polling-xhr.node.js +0 -17
- package/node_modules/engine.io-client/build/esm-debug/transports/polling.d.ts +0 -52
- package/node_modules/engine.io-client/build/esm-debug/transports/polling.js +0 -158
- package/node_modules/engine.io-client/build/esm-debug/transports/websocket.d.ts +0 -36
- package/node_modules/engine.io-client/build/esm-debug/transports/websocket.js +0 -128
- package/node_modules/engine.io-client/build/esm-debug/transports/websocket.node.d.ts +0 -14
- package/node_modules/engine.io-client/build/esm-debug/transports/websocket.node.js +0 -41
- package/node_modules/engine.io-client/build/esm-debug/transports/webtransport.d.ts +0 -18
- package/node_modules/engine.io-client/build/esm-debug/transports/webtransport.js +0 -87
- package/node_modules/engine.io-client/build/esm-debug/util.d.ts +0 -7
- package/node_modules/engine.io-client/build/esm-debug/util.js +0 -59
- package/node_modules/engine.io-client/dist/engine.io.esm.min.js +0 -7
- package/node_modules/engine.io-client/dist/engine.io.esm.min.js.map +0 -1
- package/node_modules/engine.io-client/dist/engine.io.js +0 -3064
- package/node_modules/engine.io-client/dist/engine.io.js.map +0 -1
- package/node_modules/engine.io-client/dist/engine.io.min.js +0 -7
- package/node_modules/engine.io-client/dist/engine.io.min.js.map +0 -1
- package/node_modules/engine.io-client/node_modules/ws/index.js +0 -13
- package/node_modules/engine.io-client/node_modules/ws/wrapper.mjs +0 -8
- package/node_modules/engine.io-client/package.json +0 -95
- package/node_modules/engine.io-parser/LICENSE +0 -22
- package/node_modules/engine.io-parser/Readme.md +0 -158
- package/node_modules/engine.io-parser/build/cjs/commons.d.ts +0 -14
- package/node_modules/engine.io-parser/build/cjs/commons.js +0 -19
- package/node_modules/engine.io-parser/build/cjs/contrib/base64-arraybuffer.d.ts +0 -2
- package/node_modules/engine.io-parser/build/cjs/contrib/base64-arraybuffer.js +0 -48
- package/node_modules/engine.io-parser/build/cjs/decodePacket.browser.d.ts +0 -2
- package/node_modules/engine.io-parser/build/cjs/decodePacket.browser.js +0 -66
- package/node_modules/engine.io-parser/build/cjs/decodePacket.d.ts +0 -2
- package/node_modules/engine.io-parser/build/cjs/decodePacket.js +0 -59
- package/node_modules/engine.io-parser/build/cjs/encodePacket.browser.d.ts +0 -4
- package/node_modules/engine.io-parser/build/cjs/encodePacket.browser.js +0 -72
- package/node_modules/engine.io-parser/build/cjs/encodePacket.d.ts +0 -3
- package/node_modules/engine.io-parser/build/cjs/encodePacket.js +0 -38
- package/node_modules/engine.io-parser/build/cjs/index.d.ts +0 -9
- package/node_modules/engine.io-parser/build/cjs/index.js +0 -164
- package/node_modules/engine.io-parser/build/cjs/package.json +0 -8
- package/node_modules/engine.io-parser/build/esm/commons.d.ts +0 -14
- package/node_modules/engine.io-parser/build/esm/commons.js +0 -14
- package/node_modules/engine.io-parser/build/esm/contrib/base64-arraybuffer.d.ts +0 -2
- package/node_modules/engine.io-parser/build/esm/contrib/base64-arraybuffer.js +0 -43
- package/node_modules/engine.io-parser/build/esm/decodePacket.browser.d.ts +0 -2
- package/node_modules/engine.io-parser/build/esm/decodePacket.browser.js +0 -62
- package/node_modules/engine.io-parser/build/esm/decodePacket.d.ts +0 -2
- package/node_modules/engine.io-parser/build/esm/decodePacket.js +0 -55
- package/node_modules/engine.io-parser/build/esm/encodePacket.browser.d.ts +0 -4
- package/node_modules/engine.io-parser/build/esm/encodePacket.browser.js +0 -68
- package/node_modules/engine.io-parser/build/esm/encodePacket.d.ts +0 -3
- package/node_modules/engine.io-parser/build/esm/encodePacket.js +0 -33
- package/node_modules/engine.io-parser/build/esm/index.d.ts +0 -9
- package/node_modules/engine.io-parser/build/esm/index.js +0 -156
- package/node_modules/engine.io-parser/build/esm/package.json +0 -8
- package/node_modules/engine.io-parser/package.json +0 -46
- package/node_modules/ms/index.js +0 -162
- package/node_modules/ms/license.md +0 -21
- package/node_modules/ms/package.json +0 -38
- package/node_modules/ms/readme.md +0 -59
- package/node_modules/socket.io-client/LICENSE +0 -21
- package/node_modules/socket.io-client/README.md +0 -29
- package/node_modules/socket.io-client/build/cjs/browser-entrypoint.d.ts +0 -2
- package/node_modules/socket.io-client/build/cjs/browser-entrypoint.js +0 -4
- package/node_modules/socket.io-client/build/cjs/contrib/backo2.d.ts +0 -12
- package/node_modules/socket.io-client/build/cjs/contrib/backo2.js +0 -69
- package/node_modules/socket.io-client/build/cjs/index.d.ts +0 -29
- package/node_modules/socket.io-client/build/cjs/index.js +0 -76
- package/node_modules/socket.io-client/build/cjs/manager.d.ts +0 -295
- package/node_modules/socket.io-client/build/cjs/manager.js +0 -416
- package/node_modules/socket.io-client/build/cjs/on.d.ts +0 -2
- package/node_modules/socket.io-client/build/cjs/on.js +0 -9
- package/node_modules/socket.io-client/build/cjs/socket.d.ts +0 -593
- package/node_modules/socket.io-client/build/cjs/socket.js +0 -909
- package/node_modules/socket.io-client/build/cjs/url.d.ts +0 -33
- package/node_modules/socket.io-client/build/cjs/url.js +0 -69
- package/node_modules/socket.io-client/build/esm/browser-entrypoint.d.ts +0 -2
- package/node_modules/socket.io-client/build/esm/browser-entrypoint.js +0 -2
- package/node_modules/socket.io-client/build/esm/contrib/backo2.d.ts +0 -12
- package/node_modules/socket.io-client/build/esm/contrib/backo2.js +0 -66
- package/node_modules/socket.io-client/build/esm/index.d.ts +0 -29
- package/node_modules/socket.io-client/build/esm/index.js +0 -58
- package/node_modules/socket.io-client/build/esm/manager.d.ts +0 -295
- package/node_modules/socket.io-client/build/esm/manager.js +0 -367
- package/node_modules/socket.io-client/build/esm/on.d.ts +0 -2
- package/node_modules/socket.io-client/build/esm/on.js +0 -6
- package/node_modules/socket.io-client/build/esm/package.json +0 -5
- package/node_modules/socket.io-client/build/esm/socket.d.ts +0 -593
- package/node_modules/socket.io-client/build/esm/socket.js +0 -880
- package/node_modules/socket.io-client/build/esm/url.d.ts +0 -33
- package/node_modules/socket.io-client/build/esm/url.js +0 -59
- package/node_modules/socket.io-client/build/esm-debug/browser-entrypoint.d.ts +0 -2
- package/node_modules/socket.io-client/build/esm-debug/browser-entrypoint.js +0 -2
- package/node_modules/socket.io-client/build/esm-debug/contrib/backo2.d.ts +0 -12
- package/node_modules/socket.io-client/build/esm-debug/contrib/backo2.js +0 -66
- package/node_modules/socket.io-client/build/esm-debug/index.d.ts +0 -29
- package/node_modules/socket.io-client/build/esm-debug/index.js +0 -62
- package/node_modules/socket.io-client/build/esm-debug/manager.d.ts +0 -295
- package/node_modules/socket.io-client/build/esm-debug/manager.js +0 -386
- package/node_modules/socket.io-client/build/esm-debug/on.d.ts +0 -2
- package/node_modules/socket.io-client/build/esm-debug/on.js +0 -6
- package/node_modules/socket.io-client/build/esm-debug/package.json +0 -5
- package/node_modules/socket.io-client/build/esm-debug/socket.d.ts +0 -593
- package/node_modules/socket.io-client/build/esm-debug/socket.js +0 -902
- package/node_modules/socket.io-client/build/esm-debug/url.d.ts +0 -33
- package/node_modules/socket.io-client/build/esm-debug/url.js +0 -63
- package/node_modules/socket.io-client/dist/socket.io.esm.min.js +0 -7
- package/node_modules/socket.io-client/dist/socket.io.esm.min.js.map +0 -1
- package/node_modules/socket.io-client/dist/socket.io.js +0 -4955
- package/node_modules/socket.io-client/dist/socket.io.js.map +0 -1
- package/node_modules/socket.io-client/dist/socket.io.min.js +0 -7
- package/node_modules/socket.io-client/dist/socket.io.min.js.map +0 -1
- package/node_modules/socket.io-client/dist/socket.io.msgpack.min.js +0 -7
- package/node_modules/socket.io-client/dist/socket.io.msgpack.min.js.map +0 -1
- package/node_modules/socket.io-client/package.json +0 -101
- package/node_modules/socket.io-parser/LICENSE +0 -20
- package/node_modules/socket.io-parser/Readme.md +0 -81
- package/node_modules/socket.io-parser/build/cjs/binary.d.ts +0 -20
- package/node_modules/socket.io-parser/build/cjs/binary.js +0 -87
- package/node_modules/socket.io-parser/build/cjs/index.d.ts +0 -101
- package/node_modules/socket.io-parser/build/cjs/index.js +0 -367
- package/node_modules/socket.io-parser/build/cjs/is-binary.d.ts +0 -7
- package/node_modules/socket.io-parser/build/cjs/is-binary.js +0 -54
- package/node_modules/socket.io-parser/build/cjs/package.json +0 -3
- package/node_modules/socket.io-parser/build/esm/binary.d.ts +0 -20
- package/node_modules/socket.io-parser/build/esm/binary.js +0 -83
- package/node_modules/socket.io-parser/build/esm/index.d.ts +0 -101
- package/node_modules/socket.io-parser/build/esm/index.js +0 -356
- package/node_modules/socket.io-parser/build/esm/is-binary.d.ts +0 -7
- package/node_modules/socket.io-parser/build/esm/is-binary.js +0 -50
- package/node_modules/socket.io-parser/build/esm/package.json +0 -3
- package/node_modules/socket.io-parser/build/esm-debug/binary.d.ts +0 -20
- package/node_modules/socket.io-parser/build/esm-debug/binary.js +0 -83
- package/node_modules/socket.io-parser/build/esm-debug/index.d.ts +0 -101
- package/node_modules/socket.io-parser/build/esm-debug/index.js +0 -361
- package/node_modules/socket.io-parser/build/esm-debug/is-binary.d.ts +0 -7
- package/node_modules/socket.io-parser/build/esm-debug/is-binary.js +0 -50
- package/node_modules/socket.io-parser/build/esm-debug/package.json +0 -3
- package/node_modules/socket.io-parser/package.json +0 -44
- package/node_modules/xmlhttprequest-ssl/LICENSE +0 -22
- package/node_modules/xmlhttprequest-ssl/README.md +0 -67
- package/node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js +0 -689
- package/node_modules/xmlhttprequest-ssl/package.json +0 -40
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/LICENSE +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/README.md +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/browser.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/buffer-util.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/event-target.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/extension.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/limiter.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/receiver.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/sender.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/stream.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/subprotocol.js +0 -0
- /package/node_modules/{engine.io-client/node_modules/ws → ws}/lib/validation.js +0 -0
|
@@ -2,30 +2,47 @@
|
|
|
2
2
|
* LightClaw — Socket.IO 事件处理器
|
|
3
3
|
*
|
|
4
4
|
* 将 socket 事件监听(message:private、history:request、sessions:request)
|
|
5
|
-
* 从 gateway
|
|
5
|
+
* 从 gateway 主逻辑中解耦,使 gateway.ts 只负责连接管理,本模块专注事件处理。
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* 事件处理流程:
|
|
8
|
+
* 1. message:private — 接收用户私信,过滤无效消息后入队等待 AI 处理
|
|
9
|
+
* 2. history:request — 接收历史消息拉取请求,查询本地存储后返回
|
|
10
|
+
* 3. sessions:request — 接收会话列表请求,返回所有已存在的会话
|
|
11
|
+
*
|
|
12
|
+
* 所有出站 socket.emit 通过 ReliableEmitter 实现 ACK 确认 + 自动重试,
|
|
13
|
+
* 保证消息在网络抖动时不丢失。
|
|
8
14
|
*/
|
|
9
|
-
import { CHANNEL_KEY, EVENT_MESSAGE_PRIVATE, EVENT_HISTORY_REQUEST, EVENT_HISTORY_RESPONSE,
|
|
10
|
-
import { isDuplicate, debounceHistoryRequest, generateMsgId } from
|
|
11
|
-
import {
|
|
12
|
-
import { readSessionHistoryWithCron
|
|
15
|
+
import { CHANNEL_KEY, EVENT_MESSAGE_PRIVATE, EVENT_HISTORY_REQUEST, EVENT_HISTORY_RESPONSE, DEFAULT_HISTORY_LIMIT, DEFAULT_AGENT_ID, } from '../config.js';
|
|
16
|
+
import { isDuplicate, debounceHistoryRequest, generateMsgId } from '../dedup.js';
|
|
17
|
+
import { getLightclawRuntime } from '../runtime.js';
|
|
18
|
+
import { readSessionHistoryWithCron } from '../history/index.js';
|
|
13
19
|
/**
|
|
14
|
-
* 绑定所有 Socket.IO
|
|
20
|
+
* 绑定所有 Socket.IO 事件监听器到指定 socket 实例。
|
|
21
|
+
*
|
|
22
|
+
* 该函数在 gateway 建立连接后调用一次,后续事件由各监听器自动处理。
|
|
23
|
+
* 函数本身无副作用,不持有状态,所有状态通过 deps 注入。
|
|
24
|
+
*
|
|
25
|
+
* @param socket - 已连接的 Socket.IO 客户端实例
|
|
26
|
+
* @param deps - 处理事件所需的外部依赖(账户信息、队列、日志等)
|
|
15
27
|
*/
|
|
16
28
|
export function bindSocketHandlers(socket, deps) {
|
|
17
|
-
const { account, botClientId, log,
|
|
18
|
-
//
|
|
29
|
+
const { account, botClientId, log, handleMessage, onEvent, reliableEmitter } = deps;
|
|
30
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
31
|
+
// 事件:接收用户私信(message:private)
|
|
32
|
+
// 职责:对消息做多层过滤,将合法消息放入处理队列
|
|
33
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
19
34
|
socket.on(EVENT_MESSAGE_PRIVATE, (data, ack) => {
|
|
35
|
+
// 立即回复 ACK,告知服务端消息已收到,避免服务端重发
|
|
20
36
|
ack?.();
|
|
37
|
+
// {"msgId":"91647ae9-c123-48e4-9d80-26f2e9a91ac9","from":"100013456706","to":"lhins-jem9a0br","content":"今天天气怎么样","files":[],"timestamp":1776077511044}
|
|
21
38
|
log?.info(`[${CHANNEL_KEY}] Received private message: ${JSON.stringify(data)},botClientId:${botClientId}`);
|
|
22
|
-
//
|
|
39
|
+
// ① 回环防御:过滤 bot 自身发出的消息,防止自问自答死循环
|
|
23
40
|
if (data.from === botClientId)
|
|
24
41
|
return;
|
|
25
|
-
//
|
|
26
|
-
if (data.kind && data.kind !==
|
|
42
|
+
// ② 跳过控制消息(如 typing 状态、stream 信号等),只处理 kind=text 的真实用户消息
|
|
43
|
+
if (data.kind && data.kind !== 'text')
|
|
27
44
|
return;
|
|
28
|
-
//
|
|
45
|
+
// ③ 内容校验:消息既无文字内容也无附件文件时,直接丢弃
|
|
29
46
|
const hasContent = data.content?.trim();
|
|
30
47
|
const hasFiles = data.files && data.files.length > 0;
|
|
31
48
|
if (!hasContent && !hasFiles)
|
|
@@ -33,19 +50,25 @@ export function bindSocketHandlers(socket, deps) {
|
|
|
33
50
|
// 去重
|
|
34
51
|
if (isDuplicate(data.msgId))
|
|
35
52
|
return;
|
|
36
|
-
//
|
|
53
|
+
// 通知框架收到入站事件,更新 lastEventAt 时间戳,防止 stale-socket 检测误判断线
|
|
37
54
|
onEvent?.();
|
|
38
|
-
log?.info(`[${CHANNEL_KEY}] Message from ${data.from}: "${(data.content ||
|
|
39
|
-
|
|
55
|
+
log?.info(`[${CHANNEL_KEY}] Message from Agent: ${data.agentId}, ${data.from}: "${(data.content || '').slice(0, 60)}" files=${data.files?.length ?? 0}`);
|
|
56
|
+
// /stop 及自然语言 abort 不做旁路,统一交给 openclaw fast-abort。
|
|
57
|
+
handleMessage({
|
|
40
58
|
senderId: data.from,
|
|
41
|
-
text: data.content ||
|
|
59
|
+
text: data.content || '',
|
|
42
60
|
messageId: data.msgId,
|
|
43
61
|
files: data.files ?? [],
|
|
44
62
|
timestamp: data.timestamp,
|
|
63
|
+
agentId: data.agentId, // 透传前端指定的 agentId
|
|
45
64
|
});
|
|
46
65
|
});
|
|
47
|
-
//
|
|
66
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
67
|
+
// 事件:历史消息请求(history:request)
|
|
68
|
+
// 职责:根据用户 ID 解析 sessionKey,从本地存储读取历史消息后返回给客户端
|
|
69
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
48
70
|
socket.on(EVENT_HISTORY_REQUEST, (data, ack) => {
|
|
71
|
+
// 立即回复 ACK,告知服务端请求已收到
|
|
49
72
|
ack?.();
|
|
50
73
|
if (!data?.from) {
|
|
51
74
|
log?.warn(`[${CHANNEL_KEY}] History request missing userId, ignoring`);
|
|
@@ -64,67 +87,67 @@ export function bindSocketHandlers(socket, deps) {
|
|
|
64
87
|
// 通知框架收到入站事件(更新 lastEventAt,防止 stale-socket 误判)
|
|
65
88
|
onEvent?.();
|
|
66
89
|
try {
|
|
67
|
-
|
|
90
|
+
// 获取插件运行时,读取最新配置(支持热更新,不缓存旧配置)
|
|
91
|
+
const pluginRuntime = getLightclawRuntime();
|
|
68
92
|
const currentCfg = pluginRuntime.config.loadConfig();
|
|
69
|
-
|
|
93
|
+
// 通过路由解析器获取该用户对应的 Agent 路由信息
|
|
94
|
+
// resolveAgentRoute 会综合账户配置、用户 ID、渠道等信息计算出 sessionKey
|
|
95
|
+
const baseRoute = pluginRuntime.channel.routing.resolveAgentRoute({
|
|
70
96
|
cfg: currentCfg,
|
|
71
97
|
channel: CHANNEL_KEY,
|
|
72
98
|
accountId: account.accountId,
|
|
73
|
-
peer: { kind:
|
|
99
|
+
peer: { kind: 'direct', id: data.from },
|
|
100
|
+
});
|
|
101
|
+
// agentId 合法性校验:防止前端传入任意 agentId 访问其他 Agent 的数据
|
|
102
|
+
const currentCfgTyped = currentCfg;
|
|
103
|
+
const validAgentIds = currentCfgTyped.agents?.list?.map((a) => a.id) ?? [DEFAULT_AGENT_ID];
|
|
104
|
+
log?.info(`[${CHANNEL_KEY}], 当前合法的agentId为:${validAgentIds}`);
|
|
105
|
+
const resolvedAgentId = data.agentId && validAgentIds.includes(data.agentId) ? data.agentId : DEFAULT_AGENT_ID;
|
|
106
|
+
log?.info(`[${CHANNEL_KEY}], 当前agentId为:${resolvedAgentId}`);
|
|
107
|
+
// 始终用 effectiveAgentId 调用 buildAgentSessionKey 生成 sessionKey:
|
|
108
|
+
// - 前端传了合法 agentId → 用它;否则降级到 baseRoute.agentId(框架路由解析出的默认值)
|
|
109
|
+
const sessionKey = pluginRuntime.channel.routing.buildAgentSessionKey({
|
|
110
|
+
agentId: resolvedAgentId,
|
|
111
|
+
channel: CHANNEL_KEY,
|
|
112
|
+
accountId: baseRoute.accountId,
|
|
113
|
+
peer: { kind: 'direct', id: data.from },
|
|
114
|
+
dmScope: 'per-channel-peer',
|
|
74
115
|
});
|
|
75
|
-
|
|
116
|
+
log?.info(`[${CHANNEL_KEY}] userId=${data.from},当前的sessionKey=${sessionKey}`);
|
|
117
|
+
// 从本地持久化存储中读取该 session 的历史消息
|
|
118
|
+
// includeCron: true 表示同时读取定时任务产生的消息(如每日推送)
|
|
76
119
|
const messages = readSessionHistoryWithCron(sessionKey, {
|
|
77
120
|
limit: data.limit ?? DEFAULT_HISTORY_LIMIT,
|
|
78
121
|
chatOnly: data.chatOnly ?? true,
|
|
79
122
|
includeCron: true,
|
|
123
|
+
agentId: resolvedAgentId,
|
|
80
124
|
});
|
|
81
125
|
log?.info(`[${CHANNEL_KEY}] History request: userId=${data.from} sessionKey=${sessionKey} found=${messages.length}`);
|
|
126
|
+
// 过滤掉内容为空的消息(既无文字也无附件),避免客户端渲染空气泡
|
|
82
127
|
const historyMsgId = generateMsgId();
|
|
83
128
|
reliableEmitter.emitWithAck(EVENT_HISTORY_RESPONSE, {
|
|
84
129
|
msgId: historyMsgId,
|
|
85
130
|
from: botClientId,
|
|
86
131
|
to: data.from,
|
|
87
132
|
sessionKey,
|
|
88
|
-
messages: messages.filter(msg => !!msg.content.trim() || (msg.files && msg.files.length > 0)),
|
|
133
|
+
messages: messages.filter((msg) => !!msg.content.trim() || (msg.files && msg.files.length > 0)),
|
|
134
|
+
agentId: resolvedAgentId,
|
|
89
135
|
}, historyMsgId);
|
|
90
136
|
}
|
|
91
137
|
catch (err) {
|
|
138
|
+
// 查询失败时返回空列表 + 错误信息,客户端可据此展示错误提示
|
|
92
139
|
log?.error(`[${CHANNEL_KEY}] History request error: ${err}`);
|
|
93
140
|
const errorMsgId = generateMsgId();
|
|
94
141
|
reliableEmitter.emitWithAck(EVENT_HISTORY_RESPONSE, {
|
|
95
142
|
msgId: errorMsgId,
|
|
96
143
|
from: botClientId,
|
|
97
144
|
to: data.from,
|
|
98
|
-
sessionKey:
|
|
145
|
+
sessionKey: '',
|
|
99
146
|
messages: [],
|
|
100
147
|
error: err instanceof Error ? err.message : String(err),
|
|
148
|
+
agentId: data.agentId || DEFAULT_AGENT_ID,
|
|
101
149
|
}, errorMsgId);
|
|
102
150
|
}
|
|
103
151
|
});
|
|
104
152
|
});
|
|
105
|
-
// ---- 会话列表请求 ----
|
|
106
|
-
socket.on(EVENT_SESSIONS_REQUEST, (data) => {
|
|
107
|
-
// 通知框架收到入站事件(更新 lastEventAt,防止 stale-socket 误判)
|
|
108
|
-
onEvent?.();
|
|
109
|
-
try {
|
|
110
|
-
const sessions = listSessions();
|
|
111
|
-
const sessionsMsgId = generateMsgId();
|
|
112
|
-
reliableEmitter.emitWithAck(EVENT_SESSIONS_RESPONSE, {
|
|
113
|
-
requestId: data.requestId,
|
|
114
|
-
sessions,
|
|
115
|
-
msgId: sessionsMsgId,
|
|
116
|
-
}, sessionsMsgId);
|
|
117
|
-
}
|
|
118
|
-
catch (err) {
|
|
119
|
-
log?.error(`[${CHANNEL_KEY}] Sessions list error: ${err}`);
|
|
120
|
-
const sessionsErrMsgId = generateMsgId();
|
|
121
|
-
reliableEmitter.emitWithAck(EVENT_SESSIONS_RESPONSE, {
|
|
122
|
-
requestId: data.requestId,
|
|
123
|
-
sessions: [],
|
|
124
|
-
error: err instanceof Error ? err.message : String(err),
|
|
125
|
-
msgId: sessionsErrMsgId,
|
|
126
|
-
}, sessionsErrMsgId);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
153
|
}
|
|
130
|
-
//# sourceMappingURL=handlers.js.map
|
package/dist/src/socket/index.js
CHANGED
|
@@ -6,4 +6,3 @@
|
|
|
6
6
|
export { bindSocketHandlers } from "./handlers.js";
|
|
7
7
|
export { registerSocket, unregisterSocket, getSocket, hasEntry, getBotClientId, getReliableEmitter, bufferMessage, flushPendingMessages, getPendingCount, } from "./registry.js";
|
|
8
8
|
export { ReliableEmitter } from "./reliable-emitter.js";
|
|
9
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LightClaw — 原生 WebSocket 封装层(NativeSocketClient)
|
|
3
|
+
*
|
|
4
|
+
* 功能:
|
|
5
|
+
* 1. 基于原生 WebSocket 实现连接管理
|
|
6
|
+
* 2. 基于事件名的消息分发(on / emit)
|
|
7
|
+
* 3. 指数退避自动重连
|
|
8
|
+
* 4. timeout().emit() ACK 链式调用
|
|
9
|
+
*
|
|
10
|
+
* 消息协议(与服务端约定):
|
|
11
|
+
* 上行:{ event: string; data?: unknown; msgId?: string }
|
|
12
|
+
* 下行:{ event: string; data?: unknown }
|
|
13
|
+
* ACK :{ event: 'message:ack'; data: { msgId: string; relatedMsgId: string; ... } }
|
|
14
|
+
*/
|
|
15
|
+
import WebSocket from 'ws';
|
|
16
|
+
import { SOCKET_RECONNECTION_DELAY, SOCKET_RECONNECTION_DELAY_MAX, SOCKET_RECONNECTION_ATTEMPTS } from '../config.js';
|
|
17
|
+
// ============================================================
|
|
18
|
+
// NativeSocketClient 实现
|
|
19
|
+
// ============================================================
|
|
20
|
+
/**
|
|
21
|
+
* 基于 ws 库的 WebSocket 封装,接口与原 socket.io-client 保持兼容。
|
|
22
|
+
*
|
|
23
|
+
* 使用方式:
|
|
24
|
+
* ```ts
|
|
25
|
+
* // 认证信息请直接拼接到 URL 上(例如 ?ticket=xxx),本类不再处理 auth headers
|
|
26
|
+
* const socket = new NativeSocketClient(`${WS_URL}?ticket=xxx`, { path: '/claw-socket' });
|
|
27
|
+
* socket.on('connect', () => { ... });
|
|
28
|
+
* socket.on('message:private', (data) => { ... });
|
|
29
|
+
* socket.timeout(5000).emit('message:private', data, (err) => { ... });
|
|
30
|
+
* socket.disconnect();
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export class NativeSocketClient {
|
|
34
|
+
url;
|
|
35
|
+
options;
|
|
36
|
+
// ----------------------------------------------------------
|
|
37
|
+
// 连接状态
|
|
38
|
+
// ----------------------------------------------------------
|
|
39
|
+
_connected = false;
|
|
40
|
+
_id = undefined;
|
|
41
|
+
_ws = null;
|
|
42
|
+
/** 是否为主动断开(主动断开时不触发自动重连) */
|
|
43
|
+
_manualDisconnect = false;
|
|
44
|
+
// ----------------------------------------------------------
|
|
45
|
+
// 重连状态
|
|
46
|
+
// ----------------------------------------------------------
|
|
47
|
+
_reconnectAttempts = 0;
|
|
48
|
+
_reconnectTimer = null;
|
|
49
|
+
// ----------------------------------------------------------
|
|
50
|
+
// 事件监听器映射表
|
|
51
|
+
// ----------------------------------------------------------
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
+
_listeners = new Map();
|
|
54
|
+
// ----------------------------------------------------------
|
|
55
|
+
// ACK 挂起队列
|
|
56
|
+
// ----------------------------------------------------------
|
|
57
|
+
_pendingAcks = new Map();
|
|
58
|
+
_ackCounter = 0;
|
|
59
|
+
// ----------------------------------------------------------
|
|
60
|
+
// 构造函数
|
|
61
|
+
// ----------------------------------------------------------
|
|
62
|
+
/** 日志对象(可选)。调用方通常传入 GatewayContext.log,用于打印连接/重连/错误等诊断信息。 */
|
|
63
|
+
log;
|
|
64
|
+
/** 日志前缀,便于在多实例场景下区分日志来源。 */
|
|
65
|
+
logPrefix;
|
|
66
|
+
/**
|
|
67
|
+
* @param url WebSocket 服务端地址(ws:// 或 wss://),认证所需的 ticket 等参数请直接拼到 URL query 上
|
|
68
|
+
* @param options 连接选项
|
|
69
|
+
* - path: 连接路径(如 /claw-socket),会附加到 URL
|
|
70
|
+
* - log: 日志对象(可选),用于打印连接生命周期与异常信息
|
|
71
|
+
* - logPrefix: 日志前缀(可选),默认 `[NativeSocket]`
|
|
72
|
+
*/
|
|
73
|
+
constructor(url, options = {}) {
|
|
74
|
+
this.url = url;
|
|
75
|
+
this.options = options;
|
|
76
|
+
this.log = options.log;
|
|
77
|
+
this.logPrefix = options.logPrefix ?? '[NativeSocket]';
|
|
78
|
+
this._connect();
|
|
79
|
+
}
|
|
80
|
+
// ----------------------------------------------------------
|
|
81
|
+
// NativeSocket 接口实现
|
|
82
|
+
// ----------------------------------------------------------
|
|
83
|
+
get connected() {
|
|
84
|
+
return this._connected;
|
|
85
|
+
}
|
|
86
|
+
get id() {
|
|
87
|
+
return this._id;
|
|
88
|
+
}
|
|
89
|
+
on(event, listener) {
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
+
let listeners = this._listeners.get(event);
|
|
92
|
+
if (!listeners) {
|
|
93
|
+
listeners = [];
|
|
94
|
+
this._listeners.set(event, listeners);
|
|
95
|
+
}
|
|
96
|
+
listeners.push(listener);
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
off(event, listener) {
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
+
const listeners = this._listeners.get(event);
|
|
102
|
+
if (!listeners)
|
|
103
|
+
return this;
|
|
104
|
+
const idx = listeners.indexOf(listener);
|
|
105
|
+
if (idx !== -1)
|
|
106
|
+
listeners.splice(idx, 1);
|
|
107
|
+
if (listeners.length === 0)
|
|
108
|
+
this._listeners.delete(event);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
removeAllListeners() {
|
|
112
|
+
this._listeners.clear();
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
emit(event, ...args) {
|
|
116
|
+
if (!this._connected || !this._ws)
|
|
117
|
+
return this;
|
|
118
|
+
const data = args.length === 1 ? args[0] : args;
|
|
119
|
+
try {
|
|
120
|
+
this._ws.send(JSON.stringify({ event, data }));
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// 发送失败时静默处理,连接断开会由 onclose 触发后续流程
|
|
124
|
+
}
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
timeout(ms) {
|
|
128
|
+
return {
|
|
129
|
+
emit: (event, data, callback, msgId) => {
|
|
130
|
+
// 未连接时立即回调错误
|
|
131
|
+
if (!this._connected || !this._ws) {
|
|
132
|
+
callback(new Error('not connected'));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
// ACK 匹配链路说明:
|
|
136
|
+
// 1. 调用方(ReliableEmitter.doEmit)在 data 中注入:
|
|
137
|
+
// - msgId:业务层消息 ID,同一条消息所有重试相同
|
|
138
|
+
// - idempotencyKey:emitId,每次 emitWithAck 调用唯一,仅用于服务端去重
|
|
139
|
+
// 2. 服务端 ACK 通过 relatedMsgId 回传上行消息的 msgId
|
|
140
|
+
// 3. 此处以 data.msgId 作为 pendingKey 注册到 _pendingAcks
|
|
141
|
+
// 4. _handleMessage 用 ACK 的 relatedMsgId(= msgId)查找 _pendingAcks,命中回调
|
|
142
|
+
// 因此 pendingKey 必须与上行 data.msgId 保持一致,而非 idempotencyKey。
|
|
143
|
+
const dataObj = data;
|
|
144
|
+
const pendingKey = dataObj?.msgId ??
|
|
145
|
+
`_ack_${Date.now().toString(36)}_${(this._ackCounter++).toString(36)}`;
|
|
146
|
+
// 同一条消息重试时 msgId 相同,新的回调覆盖旧的(旧定时器需先清理,防止泄漏)
|
|
147
|
+
const existing = this._pendingAcks.get(pendingKey);
|
|
148
|
+
if (existing) {
|
|
149
|
+
clearTimeout(existing.timer);
|
|
150
|
+
}
|
|
151
|
+
// 启动超时计时器
|
|
152
|
+
const timer = setTimeout(() => {
|
|
153
|
+
this._pendingAcks.delete(pendingKey);
|
|
154
|
+
callback(new Error('timeout'));
|
|
155
|
+
}, ms);
|
|
156
|
+
// 注册 ACK 挂起记录,以 pendingKey(= msgId)为 key
|
|
157
|
+
this._pendingAcks.set(pendingKey, { timer, callback });
|
|
158
|
+
// 发送消息(data.msgId 供服务端回传 relatedMsgId,data.idempotencyKey 供服务端去重)
|
|
159
|
+
try {
|
|
160
|
+
this._ws.send(JSON.stringify({ event, data }));
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
// 发送失败时立即清理并回调错误
|
|
164
|
+
clearTimeout(timer);
|
|
165
|
+
this._pendingAcks.delete(pendingKey);
|
|
166
|
+
callback(new Error('send failed'));
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
disconnect() {
|
|
172
|
+
this.log?.info(`${this.logPrefix} disconnect() called (wasConnected=${this._connected})`);
|
|
173
|
+
this._manualDisconnect = true;
|
|
174
|
+
this._clearReconnectTimer();
|
|
175
|
+
const wasConnected = this._connected;
|
|
176
|
+
this._closeWs();
|
|
177
|
+
// 主动断开时也需要通知上层,与 socket.io 行为保持一致
|
|
178
|
+
if (wasConnected) {
|
|
179
|
+
this._dispatch('disconnect');
|
|
180
|
+
}
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
connect() {
|
|
184
|
+
// 手动触发重连(用于服务端主动断开后的手动重连场景)
|
|
185
|
+
// 若当前已有活跃连接(CONNECTING 或 OPEN),直接返回,避免连接泄漏
|
|
186
|
+
if (this._ws && (this._ws.readyState === WebSocket.CONNECTING || this._ws.readyState === WebSocket.OPEN)) {
|
|
187
|
+
this.log?.debug?.(`${this.logPrefix} connect() skipped, already ${this._ws.readyState === WebSocket.OPEN ? 'OPEN' : 'CONNECTING'}`);
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
this.log?.info(`${this.logPrefix} connect() called, manually reconnecting`);
|
|
191
|
+
this._manualDisconnect = false;
|
|
192
|
+
this._reconnectAttempts = 0;
|
|
193
|
+
this._clearReconnectTimer();
|
|
194
|
+
this._connect();
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
// ----------------------------------------------------------
|
|
198
|
+
// 内部:建立 WebSocket 连接
|
|
199
|
+
// ----------------------------------------------------------
|
|
200
|
+
_connect() {
|
|
201
|
+
// 构建完整 URL(仅拼接 path,认证通过 URL query 参数完成,无需 headers)
|
|
202
|
+
const fullUrl = this._buildUrl();
|
|
203
|
+
this.log?.info(`${this.logPrefix} Connecting to ${fullUrl}`);
|
|
204
|
+
try {
|
|
205
|
+
const ws = new WebSocket(fullUrl);
|
|
206
|
+
this._ws = ws;
|
|
207
|
+
ws.onopen = () => {
|
|
208
|
+
this._reconnectAttempts = 0;
|
|
209
|
+
this._connected = true;
|
|
210
|
+
this.log?.info(`${this.logPrefix} WebSocket opened`);
|
|
211
|
+
this._dispatch('connect');
|
|
212
|
+
};
|
|
213
|
+
ws.onmessage = (event) => {
|
|
214
|
+
this._handleMessage(event.data);
|
|
215
|
+
};
|
|
216
|
+
ws.onclose = (event) => {
|
|
217
|
+
const wasConnected = this._connected;
|
|
218
|
+
this._connected = false;
|
|
219
|
+
this._id = undefined;
|
|
220
|
+
this._ws = null;
|
|
221
|
+
this.log?.info(`${this.logPrefix} WebSocket closed (code=${event.code}, reason="${event.reason}", wasClean=${event.wasClean}, wasConnected=${wasConnected})`);
|
|
222
|
+
// 立即 flush 所有挂起的 ACK,以断开错误立即回调,而非等待超时
|
|
223
|
+
this._flushPendingAcks(new Error('disconnect'));
|
|
224
|
+
if (wasConnected) {
|
|
225
|
+
this._dispatch('disconnect');
|
|
226
|
+
}
|
|
227
|
+
// 非主动断开时触发自动重连
|
|
228
|
+
if (!this._manualDisconnect) {
|
|
229
|
+
this._scheduleReconnect();
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
ws.onerror = (event) => {
|
|
233
|
+
const detail = event.error?.stack || event.message || 'unknown';
|
|
234
|
+
this.log?.error(`${this.logPrefix} WebSocket error: ${detail}`);
|
|
235
|
+
// onerror 后必然触发 onclose,此处只派发 connect_error 事件
|
|
236
|
+
this._dispatch('connect_error', new Error('WebSocket error'));
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
catch (err) {
|
|
240
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
241
|
+
this.log?.error(`${this.logPrefix} Failed to construct WebSocket: ${message}`);
|
|
242
|
+
this._dispatch('connect_error', err instanceof Error ? err : new Error(String(err)));
|
|
243
|
+
if (!this._manualDisconnect) {
|
|
244
|
+
this._scheduleReconnect();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// ----------------------------------------------------------
|
|
249
|
+
// 内部:消息处理
|
|
250
|
+
// ----------------------------------------------------------
|
|
251
|
+
_handleMessage(raw) {
|
|
252
|
+
let msg;
|
|
253
|
+
try {
|
|
254
|
+
msg = JSON.parse(raw);
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
this.log?.warn(`${this.logPrefix} Received non-JSON message, ignored`);
|
|
258
|
+
return; // 忽略非 JSON 消息
|
|
259
|
+
}
|
|
260
|
+
if (!msg.event) {
|
|
261
|
+
this.log?.warn(`${this.logPrefix} Received message without event field, ignored`);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
// 处理 ACK 回包
|
|
265
|
+
if (msg.event === 'message:ack') {
|
|
266
|
+
this.log?.info(`${this.logPrefix} Received message ACK ${JSON.stringify(msg)}`);
|
|
267
|
+
// 服务端通过 relatedMsgId 回传上行消息的 msgId,用于匹配挂起的 ACK 记录
|
|
268
|
+
// pendingKey = data.msgId(注册时),relatedMsgId = msgId(服务端回传),两者一致
|
|
269
|
+
const ackData = msg.data;
|
|
270
|
+
const relatedMsgId = ackData?.relatedMsgId;
|
|
271
|
+
if (relatedMsgId) {
|
|
272
|
+
const pending = this._pendingAcks.get(relatedMsgId);
|
|
273
|
+
if (pending) {
|
|
274
|
+
clearTimeout(pending.timer);
|
|
275
|
+
this._pendingAcks.delete(relatedMsgId);
|
|
276
|
+
pending.callback(null);
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
this.log?.debug?.(`${this.logPrefix} Received ACK for unknown relatedMsgId=${relatedMsgId}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
// 处理服务端握手消息(获取 socket id)
|
|
285
|
+
if (msg.event === '__handshake__') {
|
|
286
|
+
const handshakeData = msg.data;
|
|
287
|
+
if (handshakeData?.id) {
|
|
288
|
+
this._id = handshakeData.id;
|
|
289
|
+
this.log?.info(`${this.logPrefix} Handshake received, socket id=${this._id}`);
|
|
290
|
+
}
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
// 分发业务事件
|
|
294
|
+
this.log?.debug?.(`${this.logPrefix} Dispatch event: ${msg.event}`);
|
|
295
|
+
this._dispatch(msg.event, msg.data);
|
|
296
|
+
}
|
|
297
|
+
// ----------------------------------------------------------
|
|
298
|
+
// 内部:事件分发
|
|
299
|
+
// ----------------------------------------------------------
|
|
300
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
301
|
+
_dispatch(event, ...args) {
|
|
302
|
+
const listeners = this._listeners.get(event);
|
|
303
|
+
if (!listeners)
|
|
304
|
+
return;
|
|
305
|
+
// 遍历前浅拷贝,防止 listener 内部调用 off() 修改原数组导致遍历异常
|
|
306
|
+
for (const listener of [...listeners]) {
|
|
307
|
+
try {
|
|
308
|
+
listener(...args);
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
// 单个监听器异常不影响其他监听器
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
// ----------------------------------------------------------
|
|
316
|
+
// 内部:自动重连
|
|
317
|
+
// ----------------------------------------------------------
|
|
318
|
+
_scheduleReconnect() {
|
|
319
|
+
// 超过最大重连次数时停止重连
|
|
320
|
+
if (SOCKET_RECONNECTION_ATTEMPTS !== Infinity && this._reconnectAttempts >= SOCKET_RECONNECTION_ATTEMPTS) {
|
|
321
|
+
this.log?.error(`${this.logPrefix} Max reconnection attempts (${SOCKET_RECONNECTION_ATTEMPTS}) reached, giving up`);
|
|
322
|
+
this._dispatch('connect_error', new Error(`Max reconnection attempts (${SOCKET_RECONNECTION_ATTEMPTS}) reached`));
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
// 指数退避延迟:delay = min(base * 2^attempts, max)
|
|
326
|
+
const delay = Math.min(SOCKET_RECONNECTION_DELAY * Math.pow(2, this._reconnectAttempts), SOCKET_RECONNECTION_DELAY_MAX);
|
|
327
|
+
this._reconnectAttempts++;
|
|
328
|
+
this.log?.warn(`${this.logPrefix} Scheduling reconnect attempt #${this._reconnectAttempts} in ${delay}ms`);
|
|
329
|
+
this._reconnectTimer = setTimeout(() => {
|
|
330
|
+
this._reconnectTimer = null;
|
|
331
|
+
if (!this._manualDisconnect) {
|
|
332
|
+
this._connect();
|
|
333
|
+
}
|
|
334
|
+
}, delay);
|
|
335
|
+
}
|
|
336
|
+
_clearReconnectTimer() {
|
|
337
|
+
if (this._reconnectTimer !== null) {
|
|
338
|
+
clearTimeout(this._reconnectTimer);
|
|
339
|
+
this._reconnectTimer = null;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/** 立即清空所有挂起的 ACK,以指定错误回调 */
|
|
343
|
+
_flushPendingAcks(err) {
|
|
344
|
+
for (const [, pending] of this._pendingAcks) {
|
|
345
|
+
clearTimeout(pending.timer);
|
|
346
|
+
pending.callback(err);
|
|
347
|
+
}
|
|
348
|
+
this._pendingAcks.clear();
|
|
349
|
+
}
|
|
350
|
+
// ----------------------------------------------------------
|
|
351
|
+
// 内部:关闭 WebSocket
|
|
352
|
+
// ----------------------------------------------------------
|
|
353
|
+
_closeWs() {
|
|
354
|
+
if (this._ws) {
|
|
355
|
+
// 移除所有回调,防止 onclose 触发重连
|
|
356
|
+
this._ws.onopen = null;
|
|
357
|
+
this._ws.onmessage = null;
|
|
358
|
+
this._ws.onclose = null;
|
|
359
|
+
this._ws.onerror = null;
|
|
360
|
+
try {
|
|
361
|
+
this._ws.close();
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
// 忽略关闭时的异常
|
|
365
|
+
}
|
|
366
|
+
this._ws = null;
|
|
367
|
+
}
|
|
368
|
+
this._connected = false;
|
|
369
|
+
this._id = undefined;
|
|
370
|
+
// onclose 被置 null 后不会自动触发,需手动 flush 挂起的 ACK
|
|
371
|
+
this._flushPendingAcks(new Error('disconnect'));
|
|
372
|
+
}
|
|
373
|
+
// ----------------------------------------------------------
|
|
374
|
+
// 内部:构建完整 URL
|
|
375
|
+
// ----------------------------------------------------------
|
|
376
|
+
_buildUrl() {
|
|
377
|
+
let base = this.url;
|
|
378
|
+
// 拼接 path(如 /claw-socket)
|
|
379
|
+
if (this.options.path) {
|
|
380
|
+
// 确保 base 末尾无斜杠,path 开头有斜杠
|
|
381
|
+
base = base.replace(/\/$/, '') + this.options.path;
|
|
382
|
+
}
|
|
383
|
+
return base;
|
|
384
|
+
}
|
|
385
|
+
}
|