opencode-dingtalk 0.2.0

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.
@@ -0,0 +1,103 @@
1
+ /**
2
+ * ConnectionManager - 简化版连接管理器
3
+ *
4
+ * 原版依赖 dingtalk-stream 的 DWClient,现已移除
5
+ * 保留状态管理功能,用于追踪连接状态
6
+ */
7
+ import { EventEmitter } from 'events';
8
+ // ============ Type Definitions ============
9
+ export var ConnectionState;
10
+ (function (ConnectionState) {
11
+ ConnectionState["CONNECTING"] = "CONNECTING";
12
+ ConnectionState["CONNECTED"] = "CONNECTED";
13
+ ConnectionState["DISCONNECTED"] = "DISCONNECTED";
14
+ ConnectionState["RECONNECTING"] = "RECONNECTING";
15
+ })(ConnectionState || (ConnectionState = {}));
16
+ const DEFAULT_CONFIG = {
17
+ reconnectTimeoutMs: 30000,
18
+ maxReconnectAttempts: 5,
19
+ watchdogIntervalMs: 15000,
20
+ };
21
+ // ============ ConnectionManager ============
22
+ export class ConnectionManager extends EventEmitter {
23
+ _instanceId;
24
+ _state = ConnectionState.DISCONNECTED;
25
+ _metrics;
26
+ _config;
27
+ _reconnectTimer = null;
28
+ constructor(instanceId, config = {}) {
29
+ super();
30
+ this._instanceId = instanceId;
31
+ this._config = { ...DEFAULT_CONFIG, ...config };
32
+ this._metrics = this._createEmptyMetrics();
33
+ }
34
+ get state() {
35
+ return this._state;
36
+ }
37
+ get metrics() {
38
+ return { ...this._metrics };
39
+ }
40
+ get config() {
41
+ return { ...this._config };
42
+ }
43
+ getState() {
44
+ return this._state;
45
+ }
46
+ getMetrics() {
47
+ return { ...this._metrics };
48
+ }
49
+ updateConfig(config) {
50
+ this._config = { ...this._config, ...config };
51
+ }
52
+ get isConnected() {
53
+ return this._state === ConnectionState.CONNECTED;
54
+ }
55
+ start() {
56
+ if (this._state === ConnectionState.CONNECTED) {
57
+ return;
58
+ }
59
+ this._setState(ConnectionState.CONNECTING);
60
+ this._setState(ConnectionState.CONNECTED);
61
+ this._metrics.connectedAt = Date.now();
62
+ this.emit('connected');
63
+ }
64
+ stop() {
65
+ this._cleanupReconnect();
66
+ if (this._metrics.connectedAt) {
67
+ this._metrics.totalConnectedMs += Date.now() - this._metrics.connectedAt;
68
+ this._metrics.connectedAt = null;
69
+ }
70
+ this._setState(ConnectionState.DISCONNECTED);
71
+ this.emit('disconnected');
72
+ }
73
+ _setState(newState) {
74
+ if (this._state === newState)
75
+ return;
76
+ const oldState = this._state;
77
+ this._state = newState;
78
+ this.emit('stateChange', oldState, newState);
79
+ }
80
+ _createEmptyMetrics() {
81
+ return {
82
+ connectedAt: null,
83
+ lastDisconnectedAt: null,
84
+ totalConnectedMs: 0,
85
+ reconnectAttempts: 0,
86
+ successfulReconnects: 0,
87
+ failedReconnects: 0,
88
+ lastError: null,
89
+ lastErrorAt: null,
90
+ currentReconnectCycle: 0,
91
+ };
92
+ }
93
+ _cleanupReconnect() {
94
+ if (this._reconnectTimer) {
95
+ clearTimeout(this._reconnectTimer);
96
+ this._reconnectTimer = null;
97
+ }
98
+ }
99
+ cleanup() {
100
+ this.stop();
101
+ this.removeAllListeners();
102
+ }
103
+ }