@myclaw163/clawclaw-cli 0.6.76 → 0.6.78

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 (209) hide show
  1. package/README.md +387 -387
  2. package/bin/clawclaw-cli.mjs +3 -3
  3. package/package.json +48 -48
  4. package/personas//347/220/206/346/231/272/346/270/251/345/222/214.md +23 -23
  5. package/personas//350/200/201/350/260/213/346/267/261/347/256/227.md +22 -22
  6. package/personas//350/257/232/346/201/263/347/233/264/347/216/207.md +22 -22
  7. package/personas//350/275/273/346/235/276/346/264/273/346/263/274.md +22 -22
  8. package/personas//351/207/216/346/200/247/345/217/233/351/200/206.md +23 -23
  9. package/scripts/check-skill-command-surface.mjs +116 -116
  10. package/scripts/find-hide-spots.py +157 -157
  11. package/scripts/postinstall.mjs +20 -20
  12. package/scripts/sync-bundled-skill.mjs +254 -245
  13. package/scripts/sync-bundled-skill.test.mjs +152 -152
  14. package/skills/clawclaw/SKILL.md +248 -248
  15. package/skills/clawclaw/references/CHATTERBOX.md +141 -141
  16. package/skills/clawclaw/references/COMMANDS.md +160 -160
  17. package/skills/clawclaw/references/GAME-MECHANICS.md +188 -188
  18. package/skills/clawclaw/references/HUB.md +48 -48
  19. package/skills/clawclaw/references/KNOWLEDGE.md +42 -42
  20. package/skills/clawclaw/references/STRATEGIES.md +59 -59
  21. package/skills/clawclaw/references/STREAM.md +93 -93
  22. package/skills/clawclaw/references/TACTICS.md +65 -65
  23. package/src/assets/clawclaw-ascii-map.txt +40 -40
  24. package/src/cli.ts +112 -112
  25. package/src/commands/_schema.ts +124 -124
  26. package/src/commands/account.ts +209 -209
  27. package/src/commands/data.test.ts +33 -33
  28. package/src/commands/data.ts +22 -22
  29. package/src/commands/do.test.ts +84 -84
  30. package/src/commands/do.ts +130 -130
  31. package/src/commands/events.test.ts +100 -100
  32. package/src/commands/events.ts +250 -250
  33. package/src/commands/game-map.test.ts +28 -28
  34. package/src/commands/game-start-plan.test.ts +84 -84
  35. package/src/commands/game.ts +1113 -1113
  36. package/src/commands/history-player.test.ts +102 -102
  37. package/src/commands/history.ts +573 -573
  38. package/src/commands/hub.test.ts +96 -96
  39. package/src/commands/hub.ts +234 -234
  40. package/src/commands/knowledge.test.ts +13 -13
  41. package/src/commands/knowledge.ts +139 -139
  42. package/src/commands/load.test.ts +51 -51
  43. package/src/commands/load.ts +13 -13
  44. package/src/commands/meeting-history.test.ts +106 -106
  45. package/src/commands/memory.ts +40 -40
  46. package/src/commands/peek.ts +45 -45
  47. package/src/commands/persona.ts +57 -57
  48. package/src/commands/setup/codex.ts +266 -266
  49. package/src/commands/skill.ts +128 -128
  50. package/src/commands/state.ts +46 -46
  51. package/src/commands/strategy.test.ts +153 -153
  52. package/src/commands/strategy.ts +183 -183
  53. package/src/commands/tts.ts +128 -128
  54. package/src/commands/upgrade.test.ts +82 -82
  55. package/src/commands/upgrade.ts +148 -148
  56. package/src/commands/watch.test.ts +999 -999
  57. package/src/commands/watch.ts +660 -660
  58. package/src/lib/auth.test.ts +86 -86
  59. package/src/lib/auth.ts +223 -223
  60. package/src/lib/command-meta.ts +37 -37
  61. package/src/lib/game-client.ts +403 -403
  62. package/src/lib/game-context.ts +92 -92
  63. package/src/lib/http-keepalive.ts +15 -15
  64. package/src/lib/http-transport.test.ts +42 -42
  65. package/src/lib/http-transport.ts +113 -113
  66. package/src/lib/hub-client.test.ts +56 -56
  67. package/src/lib/hub-client.ts +88 -88
  68. package/src/lib/hub-install.test.ts +98 -98
  69. package/src/lib/hub-install.ts +160 -160
  70. package/src/lib/hub-reminder.ts +78 -78
  71. package/src/lib/hub-unzip.test.ts +69 -69
  72. package/src/lib/hub-unzip.ts +62 -62
  73. package/src/lib/init-command.test.ts +75 -75
  74. package/src/lib/init-command.ts +130 -130
  75. package/src/lib/knowledge-store.test.ts +170 -170
  76. package/src/lib/knowledge-store.ts +369 -369
  77. package/src/lib/load-context.test.ts +52 -52
  78. package/src/lib/load-context.ts +52 -52
  79. package/src/lib/match-state.test.ts +134 -134
  80. package/src/lib/match-state.ts +94 -94
  81. package/src/lib/netease-tts.ts +83 -83
  82. package/src/lib/normalize.ts +42 -42
  83. package/src/lib/persona.test.ts +41 -41
  84. package/src/lib/persona.ts +72 -72
  85. package/src/lib/server-registry.ts +152 -152
  86. package/src/lib/skill-version.test.ts +48 -48
  87. package/src/lib/skill-version.ts +19 -19
  88. package/src/lib/strategy-export.test.ts +240 -240
  89. package/src/lib/strategy-export.ts +247 -247
  90. package/src/lib/tts-keys.ts +7 -7
  91. package/src/lib/tts-speech.test.ts +63 -63
  92. package/src/lib/tts-speech.ts +76 -76
  93. package/src/lib/user-data.test.ts +96 -96
  94. package/src/lib/user-data.ts +400 -400
  95. package/src/lib/workspace-argv.test.ts +49 -49
  96. package/src/lib/workspace-argv.ts +44 -44
  97. package/src/perception/player-history-store.test.ts +87 -87
  98. package/src/perception/player-history-store.ts +194 -194
  99. package/src/pipeline/event-format.test.ts +243 -243
  100. package/src/pipeline/event-format.ts +501 -501
  101. package/src/pipeline/event-hints.ts +195 -195
  102. package/src/pipeline/event-store.test.ts +28 -28
  103. package/src/pipeline/event-store.ts +193 -193
  104. package/src/pipeline/pipeline.ts +35 -35
  105. package/src/pipeline/player-projection.test.ts +168 -168
  106. package/src/pipeline/player-projection.ts +370 -370
  107. package/src/runtime/auto-upgrade.test.ts +66 -66
  108. package/src/runtime/auto-upgrade.ts +31 -31
  109. package/src/runtime/event-daemon.test.ts +209 -209
  110. package/src/runtime/event-daemon.ts +519 -519
  111. package/src/runtime/owner-control.ts +150 -150
  112. package/src/runtime/raw-ws-log.test.ts +33 -33
  113. package/src/runtime/raw-ws-log.ts +32 -32
  114. package/src/runtime/runtime-logger.ts +107 -107
  115. package/src/runtime/ws-client.test.ts +125 -125
  116. package/src/runtime/ws-client.ts +287 -287
  117. package/src/sdk/action.ts +166 -166
  118. package/src/sdk/index.ts +110 -110
  119. package/src/sdk/types.ts +161 -161
  120. package/src/strategies/avoid-lone.ts +12 -12
  121. package/src/strategies/avoid-players.knowledge.md +19 -19
  122. package/src/strategies/avoid-players.ts +16 -16
  123. package/src/strategies/corpse-patrol.ts +23 -23
  124. package/src/strategies/crab-sabotage.ts +22 -22
  125. package/src/strategies/custom-module.test.ts +270 -270
  126. package/src/strategies/find-player.ts +17 -17
  127. package/src/strategies/game-utils.test.ts +242 -242
  128. package/src/strategies/game-utils.ts +846 -846
  129. package/src/strategies/goals/anchor-linger.ts +77 -77
  130. package/src/strategies/goals/avoid-lone-top.ts +168 -168
  131. package/src/strategies/goals/avoid-players-top.test.ts +83 -83
  132. package/src/strategies/goals/avoid-players-top.ts +121 -121
  133. package/src/strategies/goals/conversation-goal.ts +51 -51
  134. package/src/strategies/goals/corpse-patrol-top.ts +113 -113
  135. package/src/strategies/goals/crab-octopus-reflexes.ts +101 -101
  136. package/src/strategies/goals/crab-sabotage-top.ts +197 -197
  137. package/src/strategies/goals/emergency-hunt-goal.ts +28 -28
  138. package/src/strategies/goals/find-player-top.ts +93 -93
  139. package/src/strategies/goals/flee-players-goal.ts +53 -53
  140. package/src/strategies/goals/follow-companion-goal.ts +106 -106
  141. package/src/strategies/goals/goal-manager.ts +41 -41
  142. package/src/strategies/goals/goal-root-strategy.ts +49 -49
  143. package/src/strategies/goals/goal.ts +28 -28
  144. package/src/strategies/goals/hide-top.ts +197 -197
  145. package/src/strategies/goals/keep-away-goal.ts +221 -221
  146. package/src/strategies/goals/kill-frenzy-top.ts +80 -80
  147. package/src/strategies/goals/kill-lone-top.ts +160 -160
  148. package/src/strategies/goals/kill-target-goal.ts +59 -59
  149. package/src/strategies/goals/kill-target-top.ts +109 -109
  150. package/src/strategies/goals/leaf-goal.ts +27 -27
  151. package/src/strategies/goals/linger-corpse-goal.ts +35 -35
  152. package/src/strategies/goals/lone-kill-core.ts +82 -82
  153. package/src/strategies/goals/lone-kill-goal.ts +24 -24
  154. package/src/strategies/goals/lone-kill-task-top.test.ts +85 -85
  155. package/src/strategies/goals/lone-kill-task-top.ts +133 -133
  156. package/src/strategies/goals/move-room-goal.ts +60 -60
  157. package/src/strategies/goals/normal-shrimp-top.test.ts +80 -80
  158. package/src/strategies/goals/normal-shrimp-top.ts +242 -242
  159. package/src/strategies/goals/paradise-fish-top.test.ts +126 -126
  160. package/src/strategies/goals/paradise-fish-top.ts +224 -224
  161. package/src/strategies/goals/patrol-top.ts +57 -57
  162. package/src/strategies/goals/report-patrol-top.ts +80 -80
  163. package/src/strategies/goals/safe-task-goal.ts +102 -102
  164. package/src/strategies/goals/social-task-top.ts +161 -161
  165. package/src/strategies/goals/task-kill-report-top.ts +163 -163
  166. package/src/strategies/goals/task-only-top.ts +57 -57
  167. package/src/strategies/goals/task-or-patrol-goal.ts +41 -41
  168. package/src/strategies/goals/task-report-top.ts +57 -57
  169. package/src/strategies/goals/wander-task-goal.ts +33 -33
  170. package/src/strategies/goals/warrior-shrimp-top.test.ts +87 -87
  171. package/src/strategies/goals/warrior-shrimp-top.ts +267 -267
  172. package/src/strategies/greeting.ts +53 -53
  173. package/src/strategies/hide-spots.ts +59 -59
  174. package/src/strategies/hide.ts +24 -24
  175. package/src/strategies/kill-frenzy.ts +13 -13
  176. package/src/strategies/kill-lone.knowledge.md +17 -17
  177. package/src/strategies/kill-lone.ts +14 -14
  178. package/src/strategies/kill-target.ts +19 -19
  179. package/src/strategies/loader.test.ts +678 -678
  180. package/src/strategies/loader.ts +181 -181
  181. package/src/strategies/lone-kill-task.ts +22 -22
  182. package/src/strategies/meeting-gate.test.ts +59 -59
  183. package/src/strategies/meeting-gate.ts +23 -23
  184. package/src/strategies/move-room.ts +16 -16
  185. package/src/strategies/new-events-backfill.ts +98 -98
  186. package/src/strategies/off-route-points.ts +105 -105
  187. package/src/strategies/paradise-fish.knowledge.md +19 -19
  188. package/src/strategies/paradise-fish.ts +26 -26
  189. package/src/strategies/pathfind/distance-field.ts +150 -150
  190. package/src/strategies/pathfind/escape-planner.test.ts +197 -197
  191. package/src/strategies/pathfind/escape-planner.ts +355 -355
  192. package/src/strategies/pathfind/walkable-grid.ts +117 -117
  193. package/src/strategies/patrol.ts +12 -12
  194. package/src/strategies/player-targets.ts +13 -13
  195. package/src/strategies/report-patrol.ts +12 -12
  196. package/src/strategies/shrimp-memory.knowledge.md +19 -19
  197. package/src/strategies/shrimp-memory.ts +26 -26
  198. package/src/strategies/social-task.test.ts +28 -28
  199. package/src/strategies/social-task.ts +50 -50
  200. package/src/strategies/spawn.ts +82 -82
  201. package/src/strategies/speech-module.ts +123 -123
  202. package/src/strategies/strategy-loop.test.ts +15 -15
  203. package/src/strategies/strategy-loop.ts +776 -776
  204. package/src/strategies/task-kill-report.ts +18 -18
  205. package/src/strategies/task-only.ts +12 -12
  206. package/src/strategies/task-report.ts +23 -23
  207. package/src/strategies/types.ts +109 -109
  208. package/src/strategies/warrior-memory.knowledge.md +21 -21
  209. package/src/strategies/warrior-memory.ts +17 -17
@@ -1,287 +1,287 @@
1
- // src/runtime/ws-client.ts
2
- // Internal WebSocket client — NOT exported from index.ts
3
- // Used only by runtime listeners internally.
4
-
5
- import WebSocket from 'ws';
6
- import { WsConnectionState } from '../sdk/types.js';
7
- import { appendRawWsMessage } from './raw-ws-log.js';
8
-
9
- export interface WsClientConfig {
10
- url: string;
11
- apiKey: string;
12
- reconnectMaxMs?: number;
13
- /** Heartbeat interval in ms (default: 15000). Set to 0 to disable. */
14
- heartbeatIntervalMs?: number;
15
- /** Diagnostic-only raw server frame log path. */
16
- rawMessageLogPath?: string;
17
- }
18
-
19
- export interface WsCloseInfo {
20
- code?: number;
21
- reason: string;
22
- wasClean?: boolean;
23
- }
24
-
25
- /** Heartbeat interval — matches old auto_play.ts */
26
- const DEFAULT_HEARTBEAT_INTERVAL_MS = 15000;
27
-
28
- function wsMessageToString(data: WebSocket.Data): string {
29
- if (typeof data === 'string') return data;
30
- if (Buffer.isBuffer(data)) return data.toString('utf8');
31
- if (data instanceof ArrayBuffer) return Buffer.from(data).toString('utf8');
32
- return Buffer.concat(data).toString('utf8');
33
- }
34
-
35
- export class WsClient {
36
- private config: WsClientConfig;
37
- private ws: WebSocket | null = null;
38
- private _state: WsConnectionState = WsConnectionState.Disconnected;
39
- private shouldReconnect = false;
40
- private reconnectAttempt = 0;
41
- private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
42
- private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
43
-
44
- // Type-safe event handlers (new)
45
- private typedHandlers = new Map<string, Set<(data: Record<string, any>) => void>>();
46
- private disconnectHandlers: ((info: WsCloseInfo) => void)[] = [];
47
- private stateChangeHandlers: ((state: WsConnectionState) => void)[] = [];
48
- /** High-water mark for tick-based dedup of state.new_events (align with agones) */
49
- private lastEventTick = -1;
50
-
51
- constructor(config: WsClientConfig) {
52
- this.config = config;
53
- }
54
-
55
- get connected(): boolean {
56
- return this._state === WsConnectionState.Connected;
57
- }
58
-
59
- get state(): WsConnectionState {
60
- return this._state;
61
- }
62
-
63
- private setState(newState: WsConnectionState): void {
64
- if (this._state === newState) return;
65
- this._state = newState;
66
- for (const handler of this.stateChangeHandlers) {
67
- handler(newState);
68
- }
69
- }
70
-
71
- connect(): Promise<void> {
72
- this.shouldReconnect = true;
73
- this.reconnectAttempt = 0;
74
- this.setState(WsConnectionState.Connecting);
75
- return this._openSocket();
76
- }
77
-
78
- private _openSocket(): Promise<void> {
79
- return new Promise((resolve, reject) => {
80
- const ws = new WebSocket(this.config.url, {
81
- headers: { Authorization: `Bearer ${this.config.apiKey}` },
82
- });
83
- this.ws = ws;
84
-
85
- ws.onopen = () => {
86
- this.setState(WsConnectionState.Connected);
87
- this.reconnectAttempt = 0;
88
- this._startHeartbeat();
89
- resolve();
90
- };
91
-
92
- ws.onerror = (err) => {
93
- reject(err);
94
- };
95
-
96
- ws.onmessage = (e) => {
97
- const data = wsMessageToString(e.data);
98
- if (this.config.rawMessageLogPath) {
99
- appendRawWsMessage(this.config.rawMessageLogPath, data);
100
- }
101
- if (data === 'pong') return;
102
- try {
103
- const msg = JSON.parse(data);
104
- this.handleMessage(msg);
105
- } catch {
106
- // ignore malformed messages
107
- }
108
- };
109
-
110
- ws.onclose = (e: { code?: number; reason?: string; wasClean?: boolean }) => {
111
- this._stopHeartbeat();
112
- const info: WsCloseInfo = {
113
- code: typeof e.code === 'number' ? e.code : undefined,
114
- reason: e.reason ?? '',
115
- wasClean: typeof e.wasClean === 'boolean' ? e.wasClean : undefined,
116
- };
117
- for (const handler of this.disconnectHandlers) {
118
- handler(info);
119
- }
120
- if (this.shouldReconnect) {
121
- this.setState(WsConnectionState.Reconnecting);
122
- this._scheduleReconnect();
123
- } else {
124
- this.setState(WsConnectionState.Disconnected);
125
- }
126
- };
127
- });
128
- }
129
-
130
- /**
131
- * Central message handler: handles event_batch, state, and single events.
132
- * Normalizes event format before dispatching.
133
- */
134
- private handleMessage(msg: any): void {
135
- // event_batch: unwrap and dispatch each event individually
136
- if (msg.type === 'event_batch' && Array.isArray(msg.data)) {
137
- for (const event of msg.data) {
138
- this.dispatchEvent(event);
139
- }
140
- return;
141
- }
142
- // state: always dispatch the full snapshot as _state for runtime feed tracking
143
- // (current_speaker, sub_phase, vote progress, etc. update without necessarily
144
- // shipping a new event). Process new_events with tick-based dedup if present.
145
- if (msg.type === 'state' && msg.data) {
146
- if (Array.isArray(msg.data.new_events)) {
147
- const threshold = this.lastEventTick;
148
- let maxTick = threshold;
149
- for (const event of msg.data.new_events) {
150
- const evtTick: number = event.tick ?? -1;
151
- if (evtTick <= threshold) continue; // already processed
152
- if (evtTick > maxTick) maxTick = evtTick;
153
- this.dispatchEvent(event);
154
- }
155
- this.lastEventTick = maxTick;
156
- }
157
- this.dispatchEvent({ type: '_state', data: msg.data });
158
- return;
159
- }
160
- // All other messages are dispatched as events
161
- this.dispatchEvent(msg);
162
- }
163
-
164
- /**
165
- * Normalize event format and dispatch to typed + legacy handlers.
166
- *
167
- * Server sends two formats:
168
- * - { type, field1, field2 }
169
- * - { type, data: { field1, field2 } }
170
- * Normalize to consistent data object for typed handlers.
171
- */
172
- private dispatchEvent(raw: any): void {
173
- const type: string = raw.type;
174
- if (!type) return;
175
-
176
- // Normalize: extract data payload
177
- const { type: _t, data, ...rest } = raw;
178
- const normalizedData: Record<string, any> =
179
- data && typeof data === 'object' && !Array.isArray(data) ? data : rest;
180
-
181
- // Typed handlers (new API)
182
- const handlers = this.typedHandlers.get(type);
183
- if (handlers) {
184
- for (const handler of handlers) {
185
- handler(normalizedData);
186
- }
187
- }
188
-
189
- // Catch-all `*` handlers
190
- const wildcardHandlers = this.typedHandlers.get('*');
191
- if (wildcardHandlers) {
192
- for (const handler of wildcardHandlers) {
193
- handler({ type, ...normalizedData });
194
- }
195
- }
196
- }
197
-
198
- // ─── Public API ───
199
-
200
- /**
201
- * Register a handler for a specific event type.
202
- * Handler receives normalized data (always a flat object, never nested under .data).
203
- * Returns an unsubscribe function.
204
- */
205
- on(eventType: string, handler: (data: Record<string, any>) => void): () => void {
206
- let set = this.typedHandlers.get(eventType);
207
- if (!set) {
208
- set = new Set();
209
- this.typedHandlers.set(eventType, set);
210
- }
211
- set.add(handler);
212
- return () => { set!.delete(handler); };
213
- }
214
-
215
- /** Send an action via WebSocket. */
216
- send(action: { toJSON(): Record<string, any> }): void {
217
- if (this._state !== WsConnectionState.Connected || !this.ws) return;
218
- this.ws.send(JSON.stringify({ type: 'action', data: action.toJSON() }));
219
- }
220
-
221
- /** Register a handler for state changes. */
222
- onStateChange(handler: (state: WsConnectionState) => void): void {
223
- this.stateChangeHandlers.push(handler);
224
- }
225
-
226
- onDisconnect(handler: (info: WsCloseInfo) => void): () => void {
227
- this.disconnectHandlers.push(handler);
228
- return () => {
229
- const idx = this.disconnectHandlers.indexOf(handler);
230
- if (idx >= 0) this.disconnectHandlers.splice(idx, 1);
231
- };
232
- }
233
-
234
- /** Clear all typed event handlers (used when replacing WsClient). */
235
- clearHandlers(): void {
236
- this.typedHandlers.clear();
237
- this.stateChangeHandlers = [];
238
- this.disconnectHandlers = [];
239
- }
240
-
241
- disconnect(): void {
242
- this.shouldReconnect = false;
243
- this._stopHeartbeat();
244
- if (this.reconnectTimer !== null) {
245
- clearTimeout(this.reconnectTimer);
246
- this.reconnectTimer = null;
247
- }
248
- if (this.ws) {
249
- this.ws.onclose = null;
250
- this.ws.close();
251
- this.ws = null;
252
- }
253
- this.setState(WsConnectionState.Disconnected);
254
- }
255
-
256
- // ─── Heartbeat ───
257
-
258
- private _startHeartbeat(): void {
259
- this._stopHeartbeat();
260
- const intervalMs = this.config.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
261
- if (intervalMs <= 0) return;
262
- this.heartbeatTimer = setInterval(() => {
263
- if (this.ws && this._state === WsConnectionState.Connected) {
264
- this.ws.send('ping');
265
- }
266
- }, intervalMs);
267
- }
268
-
269
- private _stopHeartbeat(): void {
270
- if (this.heartbeatTimer !== null) {
271
- clearInterval(this.heartbeatTimer);
272
- this.heartbeatTimer = null;
273
- }
274
- }
275
-
276
- private _scheduleReconnect(): void {
277
- const maxMs = this.config.reconnectMaxMs ?? 30000;
278
- const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempt), maxMs);
279
- this.reconnectAttempt++;
280
- this.reconnectTimer = setTimeout(() => {
281
- if (this.shouldReconnect) {
282
- this.setState(WsConnectionState.Connecting);
283
- this._openSocket().catch(() => {});
284
- }
285
- }, delay);
286
- }
287
- }
1
+ // src/runtime/ws-client.ts
2
+ // Internal WebSocket client — NOT exported from index.ts
3
+ // Used only by runtime listeners internally.
4
+
5
+ import WebSocket from 'ws';
6
+ import { WsConnectionState } from '../sdk/types.js';
7
+ import { appendRawWsMessage } from './raw-ws-log.js';
8
+
9
+ export interface WsClientConfig {
10
+ url: string;
11
+ apiKey: string;
12
+ reconnectMaxMs?: number;
13
+ /** Heartbeat interval in ms (default: 15000). Set to 0 to disable. */
14
+ heartbeatIntervalMs?: number;
15
+ /** Diagnostic-only raw server frame log path. */
16
+ rawMessageLogPath?: string;
17
+ }
18
+
19
+ export interface WsCloseInfo {
20
+ code?: number;
21
+ reason: string;
22
+ wasClean?: boolean;
23
+ }
24
+
25
+ /** Heartbeat interval — matches old auto_play.ts */
26
+ const DEFAULT_HEARTBEAT_INTERVAL_MS = 15000;
27
+
28
+ function wsMessageToString(data: WebSocket.Data): string {
29
+ if (typeof data === 'string') return data;
30
+ if (Buffer.isBuffer(data)) return data.toString('utf8');
31
+ if (data instanceof ArrayBuffer) return Buffer.from(data).toString('utf8');
32
+ return Buffer.concat(data).toString('utf8');
33
+ }
34
+
35
+ export class WsClient {
36
+ private config: WsClientConfig;
37
+ private ws: WebSocket | null = null;
38
+ private _state: WsConnectionState = WsConnectionState.Disconnected;
39
+ private shouldReconnect = false;
40
+ private reconnectAttempt = 0;
41
+ private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
42
+ private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
43
+
44
+ // Type-safe event handlers (new)
45
+ private typedHandlers = new Map<string, Set<(data: Record<string, any>) => void>>();
46
+ private disconnectHandlers: ((info: WsCloseInfo) => void)[] = [];
47
+ private stateChangeHandlers: ((state: WsConnectionState) => void)[] = [];
48
+ /** High-water mark for tick-based dedup of state.new_events (align with agones) */
49
+ private lastEventTick = -1;
50
+
51
+ constructor(config: WsClientConfig) {
52
+ this.config = config;
53
+ }
54
+
55
+ get connected(): boolean {
56
+ return this._state === WsConnectionState.Connected;
57
+ }
58
+
59
+ get state(): WsConnectionState {
60
+ return this._state;
61
+ }
62
+
63
+ private setState(newState: WsConnectionState): void {
64
+ if (this._state === newState) return;
65
+ this._state = newState;
66
+ for (const handler of this.stateChangeHandlers) {
67
+ handler(newState);
68
+ }
69
+ }
70
+
71
+ connect(): Promise<void> {
72
+ this.shouldReconnect = true;
73
+ this.reconnectAttempt = 0;
74
+ this.setState(WsConnectionState.Connecting);
75
+ return this._openSocket();
76
+ }
77
+
78
+ private _openSocket(): Promise<void> {
79
+ return new Promise((resolve, reject) => {
80
+ const ws = new WebSocket(this.config.url, {
81
+ headers: { Authorization: `Bearer ${this.config.apiKey}` },
82
+ });
83
+ this.ws = ws;
84
+
85
+ ws.onopen = () => {
86
+ this.setState(WsConnectionState.Connected);
87
+ this.reconnectAttempt = 0;
88
+ this._startHeartbeat();
89
+ resolve();
90
+ };
91
+
92
+ ws.onerror = (err) => {
93
+ reject(err);
94
+ };
95
+
96
+ ws.onmessage = (e) => {
97
+ const data = wsMessageToString(e.data);
98
+ if (this.config.rawMessageLogPath) {
99
+ appendRawWsMessage(this.config.rawMessageLogPath, data);
100
+ }
101
+ if (data === 'pong') return;
102
+ try {
103
+ const msg = JSON.parse(data);
104
+ this.handleMessage(msg);
105
+ } catch {
106
+ // ignore malformed messages
107
+ }
108
+ };
109
+
110
+ ws.onclose = (e: { code?: number; reason?: string; wasClean?: boolean }) => {
111
+ this._stopHeartbeat();
112
+ const info: WsCloseInfo = {
113
+ code: typeof e.code === 'number' ? e.code : undefined,
114
+ reason: e.reason ?? '',
115
+ wasClean: typeof e.wasClean === 'boolean' ? e.wasClean : undefined,
116
+ };
117
+ for (const handler of this.disconnectHandlers) {
118
+ handler(info);
119
+ }
120
+ if (this.shouldReconnect) {
121
+ this.setState(WsConnectionState.Reconnecting);
122
+ this._scheduleReconnect();
123
+ } else {
124
+ this.setState(WsConnectionState.Disconnected);
125
+ }
126
+ };
127
+ });
128
+ }
129
+
130
+ /**
131
+ * Central message handler: handles event_batch, state, and single events.
132
+ * Normalizes event format before dispatching.
133
+ */
134
+ private handleMessage(msg: any): void {
135
+ // event_batch: unwrap and dispatch each event individually
136
+ if (msg.type === 'event_batch' && Array.isArray(msg.data)) {
137
+ for (const event of msg.data) {
138
+ this.dispatchEvent(event);
139
+ }
140
+ return;
141
+ }
142
+ // state: always dispatch the full snapshot as _state for runtime feed tracking
143
+ // (current_speaker, sub_phase, vote progress, etc. update without necessarily
144
+ // shipping a new event). Process new_events with tick-based dedup if present.
145
+ if (msg.type === 'state' && msg.data) {
146
+ if (Array.isArray(msg.data.new_events)) {
147
+ const threshold = this.lastEventTick;
148
+ let maxTick = threshold;
149
+ for (const event of msg.data.new_events) {
150
+ const evtTick: number = event.tick ?? -1;
151
+ if (evtTick <= threshold) continue; // already processed
152
+ if (evtTick > maxTick) maxTick = evtTick;
153
+ this.dispatchEvent(event);
154
+ }
155
+ this.lastEventTick = maxTick;
156
+ }
157
+ this.dispatchEvent({ type: '_state', data: msg.data });
158
+ return;
159
+ }
160
+ // All other messages are dispatched as events
161
+ this.dispatchEvent(msg);
162
+ }
163
+
164
+ /**
165
+ * Normalize event format and dispatch to typed + legacy handlers.
166
+ *
167
+ * Server sends two formats:
168
+ * - { type, field1, field2 }
169
+ * - { type, data: { field1, field2 } }
170
+ * Normalize to consistent data object for typed handlers.
171
+ */
172
+ private dispatchEvent(raw: any): void {
173
+ const type: string = raw.type;
174
+ if (!type) return;
175
+
176
+ // Normalize: extract data payload
177
+ const { type: _t, data, ...rest } = raw;
178
+ const normalizedData: Record<string, any> =
179
+ data && typeof data === 'object' && !Array.isArray(data) ? data : rest;
180
+
181
+ // Typed handlers (new API)
182
+ const handlers = this.typedHandlers.get(type);
183
+ if (handlers) {
184
+ for (const handler of handlers) {
185
+ handler(normalizedData);
186
+ }
187
+ }
188
+
189
+ // Catch-all `*` handlers
190
+ const wildcardHandlers = this.typedHandlers.get('*');
191
+ if (wildcardHandlers) {
192
+ for (const handler of wildcardHandlers) {
193
+ handler({ type, ...normalizedData });
194
+ }
195
+ }
196
+ }
197
+
198
+ // ─── Public API ───
199
+
200
+ /**
201
+ * Register a handler for a specific event type.
202
+ * Handler receives normalized data (always a flat object, never nested under .data).
203
+ * Returns an unsubscribe function.
204
+ */
205
+ on(eventType: string, handler: (data: Record<string, any>) => void): () => void {
206
+ let set = this.typedHandlers.get(eventType);
207
+ if (!set) {
208
+ set = new Set();
209
+ this.typedHandlers.set(eventType, set);
210
+ }
211
+ set.add(handler);
212
+ return () => { set!.delete(handler); };
213
+ }
214
+
215
+ /** Send an action via WebSocket. */
216
+ send(action: { toJSON(): Record<string, any> }): void {
217
+ if (this._state !== WsConnectionState.Connected || !this.ws) return;
218
+ this.ws.send(JSON.stringify({ type: 'action', data: action.toJSON() }));
219
+ }
220
+
221
+ /** Register a handler for state changes. */
222
+ onStateChange(handler: (state: WsConnectionState) => void): void {
223
+ this.stateChangeHandlers.push(handler);
224
+ }
225
+
226
+ onDisconnect(handler: (info: WsCloseInfo) => void): () => void {
227
+ this.disconnectHandlers.push(handler);
228
+ return () => {
229
+ const idx = this.disconnectHandlers.indexOf(handler);
230
+ if (idx >= 0) this.disconnectHandlers.splice(idx, 1);
231
+ };
232
+ }
233
+
234
+ /** Clear all typed event handlers (used when replacing WsClient). */
235
+ clearHandlers(): void {
236
+ this.typedHandlers.clear();
237
+ this.stateChangeHandlers = [];
238
+ this.disconnectHandlers = [];
239
+ }
240
+
241
+ disconnect(): void {
242
+ this.shouldReconnect = false;
243
+ this._stopHeartbeat();
244
+ if (this.reconnectTimer !== null) {
245
+ clearTimeout(this.reconnectTimer);
246
+ this.reconnectTimer = null;
247
+ }
248
+ if (this.ws) {
249
+ this.ws.onclose = null;
250
+ this.ws.close();
251
+ this.ws = null;
252
+ }
253
+ this.setState(WsConnectionState.Disconnected);
254
+ }
255
+
256
+ // ─── Heartbeat ───
257
+
258
+ private _startHeartbeat(): void {
259
+ this._stopHeartbeat();
260
+ const intervalMs = this.config.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
261
+ if (intervalMs <= 0) return;
262
+ this.heartbeatTimer = setInterval(() => {
263
+ if (this.ws && this._state === WsConnectionState.Connected) {
264
+ this.ws.send('ping');
265
+ }
266
+ }, intervalMs);
267
+ }
268
+
269
+ private _stopHeartbeat(): void {
270
+ if (this.heartbeatTimer !== null) {
271
+ clearInterval(this.heartbeatTimer);
272
+ this.heartbeatTimer = null;
273
+ }
274
+ }
275
+
276
+ private _scheduleReconnect(): void {
277
+ const maxMs = this.config.reconnectMaxMs ?? 30000;
278
+ const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempt), maxMs);
279
+ this.reconnectAttempt++;
280
+ this.reconnectTimer = setTimeout(() => {
281
+ if (this.shouldReconnect) {
282
+ this.setState(WsConnectionState.Connecting);
283
+ this._openSocket().catch(() => {});
284
+ }
285
+ }, delay);
286
+ }
287
+ }