@kodelyth/feishu 2026.5.42 → 2026.6.2

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 (192) hide show
  1. package/klaw.plugin.json +1712 -47
  2. package/package.json +19 -6
  3. package/api.ts +0 -32
  4. package/channel-entry.ts +0 -20
  5. package/channel-plugin-api.ts +0 -1
  6. package/contract-api.ts +0 -16
  7. package/index.ts +0 -82
  8. package/runtime-api.ts +0 -52
  9. package/secret-contract-api.ts +0 -5
  10. package/security-contract-api.ts +0 -1
  11. package/session-key-api.ts +0 -1
  12. package/setup-api.ts +0 -3
  13. package/setup-entry.test.ts +0 -19
  14. package/setup-entry.ts +0 -13
  15. package/src/accounts.test.ts +0 -480
  16. package/src/accounts.ts +0 -333
  17. package/src/agent-config.ts +0 -21
  18. package/src/app-registration.ts +0 -331
  19. package/src/approval-auth.test.ts +0 -24
  20. package/src/approval-auth.ts +0 -25
  21. package/src/async.test.ts +0 -35
  22. package/src/async.ts +0 -104
  23. package/src/audio-preflight.runtime.ts +0 -9
  24. package/src/bitable.test.ts +0 -136
  25. package/src/bitable.ts +0 -762
  26. package/src/bot-content.ts +0 -485
  27. package/src/bot-group-name.test.ts +0 -116
  28. package/src/bot-runtime-api.ts +0 -12
  29. package/src/bot-sender-name.ts +0 -125
  30. package/src/bot.broadcast.test.ts +0 -523
  31. package/src/bot.card-action.test.ts +0 -552
  32. package/src/bot.checkBotMentioned.test.ts +0 -265
  33. package/src/bot.helpers.test.ts +0 -135
  34. package/src/bot.stripBotMention.test.ts +0 -126
  35. package/src/bot.test.ts +0 -3671
  36. package/src/bot.ts +0 -1703
  37. package/src/card-action.ts +0 -447
  38. package/src/card-interaction.test.ts +0 -131
  39. package/src/card-interaction.ts +0 -159
  40. package/src/card-test-helpers.ts +0 -54
  41. package/src/card-ux-approval.ts +0 -65
  42. package/src/card-ux-launcher.test.ts +0 -106
  43. package/src/card-ux-launcher.ts +0 -121
  44. package/src/card-ux-shared.ts +0 -33
  45. package/src/channel-runtime-api.ts +0 -16
  46. package/src/channel.runtime.ts +0 -47
  47. package/src/channel.test.ts +0 -1151
  48. package/src/channel.ts +0 -1423
  49. package/src/chat-schema.ts +0 -25
  50. package/src/chat.test.ts +0 -240
  51. package/src/chat.ts +0 -188
  52. package/src/client-timeout.ts +0 -42
  53. package/src/client.test.ts +0 -447
  54. package/src/client.ts +0 -262
  55. package/src/comment-dispatcher-runtime-api.ts +0 -6
  56. package/src/comment-dispatcher.test.ts +0 -185
  57. package/src/comment-dispatcher.ts +0 -107
  58. package/src/comment-handler-runtime-api.ts +0 -3
  59. package/src/comment-handler.test.ts +0 -592
  60. package/src/comment-handler.ts +0 -303
  61. package/src/comment-reaction.test.ts +0 -138
  62. package/src/comment-reaction.ts +0 -259
  63. package/src/comment-shared.test.ts +0 -183
  64. package/src/comment-shared.ts +0 -406
  65. package/src/comment-target.ts +0 -44
  66. package/src/config-schema.test.ts +0 -326
  67. package/src/config-schema.ts +0 -335
  68. package/src/conversation-id.test.ts +0 -18
  69. package/src/conversation-id.ts +0 -199
  70. package/src/dedup-runtime-api.ts +0 -1
  71. package/src/dedup.ts +0 -141
  72. package/src/dedupe-key.ts +0 -72
  73. package/src/directory.static.ts +0 -61
  74. package/src/directory.test.ts +0 -141
  75. package/src/directory.ts +0 -124
  76. package/src/doc-schema.ts +0 -182
  77. package/src/docx-batch-insert.test.ts +0 -116
  78. package/src/docx-batch-insert.ts +0 -223
  79. package/src/docx-color-text.ts +0 -154
  80. package/src/docx-table-ops.test.ts +0 -53
  81. package/src/docx-table-ops.ts +0 -316
  82. package/src/docx-types.ts +0 -38
  83. package/src/docx.account-selection.test.ts +0 -95
  84. package/src/docx.test.ts +0 -701
  85. package/src/docx.ts +0 -1596
  86. package/src/drive-schema.ts +0 -92
  87. package/src/drive.test.ts +0 -1237
  88. package/src/drive.ts +0 -829
  89. package/src/dynamic-agent.test.ts +0 -155
  90. package/src/dynamic-agent.ts +0 -143
  91. package/src/event-types.ts +0 -45
  92. package/src/external-keys.test.ts +0 -20
  93. package/src/external-keys.ts +0 -19
  94. package/src/lifecycle.test-support.ts +0 -220
  95. package/src/media.test.ts +0 -955
  96. package/src/media.ts +0 -1105
  97. package/src/mention-target.types.ts +0 -5
  98. package/src/mention.ts +0 -114
  99. package/src/message-action-contract.ts +0 -13
  100. package/src/monitor-state-runtime-api.ts +0 -7
  101. package/src/monitor-transport-runtime-api.ts +0 -10
  102. package/src/monitor.account.ts +0 -492
  103. package/src/monitor.acp-init-failure.lifecycle.test-support.ts +0 -219
  104. package/src/monitor.bot-identity.ts +0 -86
  105. package/src/monitor.bot-menu-handler.ts +0 -165
  106. package/src/monitor.bot-menu.lifecycle.test-support.ts +0 -224
  107. package/src/monitor.bot-menu.test.ts +0 -188
  108. package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +0 -264
  109. package/src/monitor.card-action.lifecycle.test-support.ts +0 -421
  110. package/src/monitor.cleanup.test.ts +0 -383
  111. package/src/monitor.comment-notice-handler.ts +0 -105
  112. package/src/monitor.comment.test.ts +0 -967
  113. package/src/monitor.comment.ts +0 -1386
  114. package/src/monitor.lifecycle.test.ts +0 -4
  115. package/src/monitor.message-handler.ts +0 -350
  116. package/src/monitor.reaction.lifecycle.test-support.ts +0 -68
  117. package/src/monitor.reaction.test.ts +0 -739
  118. package/src/monitor.startup.test.ts +0 -213
  119. package/src/monitor.startup.ts +0 -74
  120. package/src/monitor.state.defaults.test.ts +0 -46
  121. package/src/monitor.state.ts +0 -170
  122. package/src/monitor.synthetic-error.ts +0 -18
  123. package/src/monitor.test-mocks.ts +0 -46
  124. package/src/monitor.transport.ts +0 -451
  125. package/src/monitor.ts +0 -100
  126. package/src/monitor.webhook-e2e.test.ts +0 -279
  127. package/src/monitor.webhook-security.test.ts +0 -389
  128. package/src/monitor.webhook.test-helpers.ts +0 -116
  129. package/src/outbound-runtime-api.ts +0 -1
  130. package/src/outbound.test.ts +0 -1118
  131. package/src/outbound.ts +0 -785
  132. package/src/perm-schema.ts +0 -52
  133. package/src/perm.ts +0 -170
  134. package/src/pins.ts +0 -108
  135. package/src/policy.test.ts +0 -223
  136. package/src/policy.ts +0 -318
  137. package/src/post.test.ts +0 -105
  138. package/src/post.ts +0 -275
  139. package/src/probe.test.ts +0 -283
  140. package/src/probe.ts +0 -166
  141. package/src/processing-claims.ts +0 -59
  142. package/src/qr-terminal.ts +0 -1
  143. package/src/reactions.ts +0 -123
  144. package/src/reasoning-preview.test.ts +0 -113
  145. package/src/reasoning-preview.ts +0 -28
  146. package/src/reply-dispatcher-runtime-api.ts +0 -7
  147. package/src/reply-dispatcher.test.ts +0 -1513
  148. package/src/reply-dispatcher.ts +0 -748
  149. package/src/runtime.ts +0 -9
  150. package/src/secret-contract.ts +0 -145
  151. package/src/secret-input.ts +0 -1
  152. package/src/security-audit-shared.ts +0 -69
  153. package/src/security-audit.test.ts +0 -59
  154. package/src/security-audit.ts +0 -1
  155. package/src/send-result.ts +0 -80
  156. package/src/send-target.test.ts +0 -86
  157. package/src/send-target.ts +0 -35
  158. package/src/send.reply-fallback.test.ts +0 -417
  159. package/src/send.test.ts +0 -621
  160. package/src/send.ts +0 -861
  161. package/src/sequential-key.test.ts +0 -72
  162. package/src/sequential-key.ts +0 -25
  163. package/src/sequential-queue.test.ts +0 -165
  164. package/src/sequential-queue.ts +0 -86
  165. package/src/session-conversation.ts +0 -42
  166. package/src/session-route.ts +0 -48
  167. package/src/setup-core.ts +0 -51
  168. package/src/setup-surface.test.ts +0 -484
  169. package/src/setup-surface.ts +0 -618
  170. package/src/streaming-card.test.ts +0 -397
  171. package/src/streaming-card.ts +0 -571
  172. package/src/subagent-hooks.test.ts +0 -627
  173. package/src/subagent-hooks.ts +0 -413
  174. package/src/targets.ts +0 -97
  175. package/src/test-support/lifecycle-test-support.ts +0 -454
  176. package/src/thread-bindings.test.ts +0 -180
  177. package/src/thread-bindings.ts +0 -331
  178. package/src/tool-account-routing.test.ts +0 -250
  179. package/src/tool-account.test.ts +0 -44
  180. package/src/tool-account.ts +0 -93
  181. package/src/tool-factory-test-harness.ts +0 -79
  182. package/src/tool-result.test.ts +0 -32
  183. package/src/tool-result.ts +0 -16
  184. package/src/tools-config.test.ts +0 -21
  185. package/src/tools-config.ts +0 -22
  186. package/src/types.ts +0 -106
  187. package/src/typing.test.ts +0 -144
  188. package/src/typing.ts +0 -214
  189. package/src/wiki-schema.ts +0 -69
  190. package/src/wiki.ts +0 -270
  191. package/subagent-hooks-api.ts +0 -31
  192. package/tsconfig.json +0 -16
@@ -1,383 +0,0 @@
1
- import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
2
- import { botNames, botOpenIds, stopFeishuMonitorState, wsClients } from "./monitor.state.js";
3
- import type { ResolvedFeishuAccount } from "./types.js";
4
-
5
- const createFeishuWSClientMock = vi.hoisted(() => vi.fn());
6
-
7
- vi.mock("./client.js", () => ({
8
- createFeishuWSClient: createFeishuWSClientMock,
9
- }));
10
-
11
- import { monitorWebSocket } from "./monitor.transport.js";
12
-
13
- type MockWsClient = {
14
- start: ReturnType<typeof vi.fn>;
15
- close: ReturnType<typeof vi.fn>;
16
- };
17
-
18
- function createAccount(accountId: string): ResolvedFeishuAccount {
19
- return {
20
- accountId,
21
- enabled: true,
22
- configured: true,
23
- appId: `cli_${accountId}`,
24
- appSecret: `secret_${accountId}`, // pragma: allowlist secret
25
- domain: "feishu",
26
- config: {
27
- enabled: true,
28
- connectionMode: "websocket",
29
- },
30
- } as ResolvedFeishuAccount;
31
- }
32
-
33
- function createWsClient(): MockWsClient {
34
- return {
35
- start: vi.fn(),
36
- close: vi.fn(),
37
- };
38
- }
39
-
40
- function firstRuntimeError(runtime: { error: ReturnType<typeof vi.fn> }): string {
41
- return String(runtime.error.mock.calls[0]?.[0] ?? "");
42
- }
43
-
44
- function firstWsCallbacks(): { onError?: (err: Error) => void } {
45
- const callbacks = createFeishuWSClientMock.mock.calls[0]?.[1];
46
- if (!callbacks || typeof callbacks !== "object") {
47
- throw new Error("expected Feishu websocket callbacks");
48
- }
49
- return callbacks as { onError?: (err: Error) => void };
50
- }
51
-
52
- afterEach(() => {
53
- vi.useRealTimers();
54
- stopFeishuMonitorState();
55
- vi.clearAllMocks();
56
- });
57
-
58
- afterAll(() => {
59
- vi.doUnmock("./client.js");
60
- vi.resetModules();
61
- });
62
-
63
- describe("feishu websocket cleanup", () => {
64
- it("closes the websocket client when the monitor aborts", async () => {
65
- const wsClient = createWsClient();
66
- createFeishuWSClientMock.mockReturnValue(wsClient);
67
-
68
- const abortController = new AbortController();
69
- const accountId = "alpha";
70
-
71
- botOpenIds.set(accountId, "ou_alpha");
72
- botNames.set(accountId, "Alpha");
73
-
74
- const monitorPromise = monitorWebSocket({
75
- account: createAccount(accountId),
76
- accountId,
77
- runtime: {
78
- log: vi.fn(),
79
- error: vi.fn(),
80
- exit: vi.fn(),
81
- },
82
- abortSignal: abortController.signal,
83
- eventDispatcher: {} as never,
84
- });
85
-
86
- await vi.waitFor(() => {
87
- expect(wsClient.start).toHaveBeenCalledTimes(1);
88
- expect(wsClients.get(accountId)).toBe(wsClient);
89
- });
90
-
91
- abortController.abort();
92
- await monitorPromise;
93
-
94
- expect(wsClient.close).toHaveBeenCalledTimes(1);
95
- expect(wsClients.has(accountId)).toBe(false);
96
- expect(botOpenIds.has(accountId)).toBe(false);
97
- expect(botNames.has(accountId)).toBe(false);
98
- });
99
-
100
- it("retries with backoff after websocket start rejects", async () => {
101
- vi.useFakeTimers();
102
- const failedClient = createWsClient();
103
- failedClient.start.mockRejectedValueOnce(
104
- new Error("connect failed\nAuthorization: Bearer token_abc appSecret=secret_abc"),
105
- );
106
- const recoveredClient = createWsClient();
107
- createFeishuWSClientMock
108
- .mockResolvedValueOnce(failedClient)
109
- .mockResolvedValueOnce(recoveredClient);
110
-
111
- const abortController = new AbortController();
112
- const runtime = {
113
- log: vi.fn(),
114
- error: vi.fn(),
115
- exit: vi.fn(),
116
- };
117
- const accountId = "retry";
118
-
119
- const monitorPromise = monitorWebSocket({
120
- account: createAccount(accountId),
121
- accountId,
122
- runtime,
123
- abortSignal: abortController.signal,
124
- eventDispatcher: {} as never,
125
- });
126
-
127
- await vi.waitFor(() => {
128
- expect(failedClient.start).toHaveBeenCalledTimes(1);
129
- expect(failedClient.close).toHaveBeenCalledTimes(1);
130
- expect(wsClients.has(accountId)).toBe(false);
131
- });
132
-
133
- await vi.advanceTimersByTimeAsync(1_000);
134
-
135
- await vi.waitFor(() => {
136
- expect(recoveredClient.start).toHaveBeenCalledTimes(1);
137
- expect(wsClients.get(accountId)).toBe(recoveredClient);
138
- });
139
-
140
- abortController.abort();
141
- await monitorPromise;
142
-
143
- expect(createFeishuWSClientMock).toHaveBeenCalledTimes(2);
144
- expect(recoveredClient.close).toHaveBeenCalledTimes(1);
145
- expect(runtime.error).toHaveBeenCalledTimes(1);
146
- const errorMessage = firstRuntimeError(runtime);
147
- expect(errorMessage).toContain("WebSocket start failed, retrying in 1000ms");
148
- expect(errorMessage).not.toContain("\n");
149
- expect(errorMessage).not.toContain("token_abc");
150
- expect(errorMessage).not.toContain("secret_abc");
151
- expect(errorMessage).toContain("Authorization: Bearer [redacted]");
152
- expect(errorMessage).toContain("appSecret=[redacted]");
153
- });
154
-
155
- it("recreates the websocket client after sdk reconnect exhaustion", async () => {
156
- vi.useFakeTimers();
157
- const exhaustedClient = createWsClient();
158
- const recoveredClient = createWsClient();
159
- createFeishuWSClientMock
160
- .mockResolvedValueOnce(exhaustedClient)
161
- .mockResolvedValueOnce(recoveredClient);
162
-
163
- const abortController = new AbortController();
164
- const runtime = {
165
- log: vi.fn(),
166
- error: vi.fn(),
167
- exit: vi.fn(),
168
- };
169
- const accountId = "exhausted";
170
- botOpenIds.set(accountId, "ou_exhausted");
171
- botNames.set(accountId, "Exhausted");
172
-
173
- const monitorPromise = monitorWebSocket({
174
- account: createAccount(accountId),
175
- accountId,
176
- runtime,
177
- abortSignal: abortController.signal,
178
- eventDispatcher: {} as never,
179
- });
180
-
181
- await vi.waitFor(() => {
182
- expect(exhaustedClient.start).toHaveBeenCalledTimes(1);
183
- expect(wsClients.get(accountId)).toBe(exhaustedClient);
184
- });
185
-
186
- firstWsCallbacks().onError?.(
187
- new Error("WebSocket reconnect exhausted after 3 attempts\nBearer token_abc"),
188
- );
189
-
190
- await vi.waitFor(() => {
191
- expect(exhaustedClient.close).toHaveBeenCalledTimes(1);
192
- expect(wsClients.has(accountId)).toBe(false);
193
- });
194
- expect(botOpenIds.get(accountId)).toBe("ou_exhausted");
195
- expect(botNames.get(accountId)).toBe("Exhausted");
196
-
197
- await vi.advanceTimersByTimeAsync(1_000);
198
-
199
- await vi.waitFor(() => {
200
- expect(recoveredClient.start).toHaveBeenCalledTimes(1);
201
- expect(wsClients.get(accountId)).toBe(recoveredClient);
202
- });
203
-
204
- abortController.abort();
205
- await monitorPromise;
206
-
207
- expect(createFeishuWSClientMock).toHaveBeenCalledTimes(2);
208
- expect(recoveredClient.close).toHaveBeenCalledTimes(1);
209
- expect(botOpenIds.has(accountId)).toBe(false);
210
- expect(botNames.has(accountId)).toBe(false);
211
- const errorMessage = firstRuntimeError(runtime);
212
- expect(errorMessage).toContain("WebSocket connection ended, recreating client in 1000ms");
213
- expect(errorMessage).toContain("Bearer [redacted]");
214
- expect(errorMessage).not.toContain("\n");
215
- expect(errorMessage).not.toContain("token_abc");
216
- });
217
-
218
- it("keeps the websocket client alive after recoverable sdk callback errors", async () => {
219
- vi.useFakeTimers();
220
- const wsClient = createWsClient();
221
- createFeishuWSClientMock.mockResolvedValueOnce(wsClient);
222
-
223
- const abortController = new AbortController();
224
- const runtime = {
225
- log: vi.fn(),
226
- error: vi.fn(),
227
- exit: vi.fn(),
228
- };
229
- const accountId = "recoverable-callback";
230
-
231
- const monitorPromise = monitorWebSocket({
232
- account: createAccount(accountId),
233
- accountId,
234
- runtime,
235
- abortSignal: abortController.signal,
236
- eventDispatcher: {} as never,
237
- });
238
-
239
- await vi.waitFor(() => {
240
- expect(wsClient.start).toHaveBeenCalledTimes(1);
241
- expect(wsClients.get(accountId)).toBe(wsClient);
242
- });
243
-
244
- firstWsCallbacks().onError?.(new Error("temporary callback failure\nBearer token_abc"));
245
-
246
- await vi.advanceTimersByTimeAsync(1_000);
247
-
248
- expect(createFeishuWSClientMock).toHaveBeenCalledTimes(1);
249
- expect(wsClient.close).not.toHaveBeenCalled();
250
- expect(wsClients.get(accountId)).toBe(wsClient);
251
- const errorMessage = firstRuntimeError(runtime);
252
- expect(errorMessage).toContain("WebSocket SDK reported recoverable error");
253
- expect(errorMessage).toContain("Bearer [redacted]");
254
- expect(errorMessage).not.toContain("\n");
255
- expect(errorMessage).not.toContain("token_abc");
256
-
257
- abortController.abort();
258
- await monitorPromise;
259
-
260
- expect(createFeishuWSClientMock).toHaveBeenCalledTimes(1);
261
- expect(wsClient.close).toHaveBeenCalledTimes(1);
262
- });
263
-
264
- it("clears identity without recreating a websocket when aborted during reconnect backoff", async () => {
265
- vi.useFakeTimers();
266
- const exhaustedClient = createWsClient();
267
- createFeishuWSClientMock.mockResolvedValueOnce(exhaustedClient);
268
-
269
- const abortController = new AbortController();
270
- const accountId = "abort-backoff";
271
- botOpenIds.set(accountId, "ou_abort");
272
- botNames.set(accountId, "Abort");
273
-
274
- const monitorPromise = monitorWebSocket({
275
- account: createAccount(accountId),
276
- accountId,
277
- runtime: {
278
- log: vi.fn(),
279
- error: vi.fn(),
280
- exit: vi.fn(),
281
- },
282
- abortSignal: abortController.signal,
283
- eventDispatcher: {} as never,
284
- });
285
-
286
- await vi.waitFor(() => {
287
- expect(exhaustedClient.start).toHaveBeenCalledTimes(1);
288
- });
289
-
290
- firstWsCallbacks().onError?.(new Error("WebSocket reconnect exhausted after 3 attempts"));
291
-
292
- await vi.waitFor(() => {
293
- expect(exhaustedClient.close).toHaveBeenCalledTimes(1);
294
- });
295
-
296
- abortController.abort();
297
- await monitorPromise;
298
-
299
- expect(createFeishuWSClientMock).toHaveBeenCalledTimes(1);
300
- expect(wsClients.has(accountId)).toBe(false);
301
- expect(botOpenIds.has(accountId)).toBe(false);
302
- expect(botNames.has(accountId)).toBe(false);
303
- });
304
-
305
- it("redacts websocket close errors during abort cleanup", async () => {
306
- const wsClient = createWsClient();
307
- wsClient.close.mockImplementationOnce(() => {
308
- throw new Error("close failed\naccess_token=secret_token");
309
- });
310
- createFeishuWSClientMock.mockReturnValue(wsClient);
311
-
312
- const abortController = new AbortController();
313
- const runtime = {
314
- log: vi.fn(),
315
- error: vi.fn(),
316
- exit: vi.fn(),
317
- };
318
-
319
- const monitorPromise = monitorWebSocket({
320
- account: createAccount("close-error"),
321
- accountId: "close-error",
322
- runtime,
323
- abortSignal: abortController.signal,
324
- eventDispatcher: {} as never,
325
- });
326
-
327
- await vi.waitFor(() => {
328
- expect(wsClient.start).toHaveBeenCalledTimes(1);
329
- });
330
-
331
- abortController.abort();
332
- await monitorPromise;
333
-
334
- const errorMessage = firstRuntimeError(runtime);
335
- expect(errorMessage).toContain("error closing WebSocket client");
336
- expect(errorMessage).toContain("access_token=[redacted]");
337
- expect(errorMessage).not.toContain("\n");
338
- expect(errorMessage).not.toContain("secret_token");
339
- });
340
-
341
- it("closes targeted websocket clients during stop cleanup", () => {
342
- const alphaClient = createWsClient();
343
- const betaClient = createWsClient();
344
-
345
- wsClients.set("alpha", alphaClient as never);
346
- wsClients.set("beta", betaClient as never);
347
- botOpenIds.set("alpha", "ou_alpha");
348
- botOpenIds.set("beta", "ou_beta");
349
- botNames.set("alpha", "Alpha");
350
- botNames.set("beta", "Beta");
351
-
352
- stopFeishuMonitorState("alpha");
353
-
354
- expect(alphaClient.close).toHaveBeenCalledTimes(1);
355
- expect(betaClient.close).not.toHaveBeenCalled();
356
- expect(wsClients.has("alpha")).toBe(false);
357
- expect(wsClients.has("beta")).toBe(true);
358
- expect(botOpenIds.has("alpha")).toBe(false);
359
- expect(botOpenIds.has("beta")).toBe(true);
360
- expect(botNames.has("alpha")).toBe(false);
361
- expect(botNames.has("beta")).toBe(true);
362
- });
363
-
364
- it("closes all websocket clients during global stop cleanup", () => {
365
- const alphaClient = createWsClient();
366
- const betaClient = createWsClient();
367
-
368
- wsClients.set("alpha", alphaClient as never);
369
- wsClients.set("beta", betaClient as never);
370
- botOpenIds.set("alpha", "ou_alpha");
371
- botOpenIds.set("beta", "ou_beta");
372
- botNames.set("alpha", "Alpha");
373
- botNames.set("beta", "Beta");
374
-
375
- stopFeishuMonitorState();
376
-
377
- expect(alphaClient.close).toHaveBeenCalledTimes(1);
378
- expect(betaClient.close).toHaveBeenCalledTimes(1);
379
- expect(wsClients.size).toBe(0);
380
- expect(botOpenIds.size).toBe(0);
381
- expect(botNames.size).toBe(0);
382
- });
383
- });
@@ -1,105 +0,0 @@
1
- import type { ClawdbotConfig, RuntimeEnv } from "../runtime-api.js";
2
- import { handleFeishuCommentEvent } from "./comment-handler.js";
3
- import {
4
- claimUnprocessedFeishuMessage,
5
- recordProcessedFeishuMessage,
6
- releaseFeishuMessageProcessing,
7
- } from "./dedup.js";
8
- import { parseFeishuDriveCommentNoticeEventPayload } from "./monitor.comment.js";
9
- import { botOpenIds } from "./monitor.state.js";
10
- import { isFeishuRetryableSyntheticEventError } from "./monitor.synthetic-error.js";
11
- import { createSequentialQueue } from "./sequential-queue.js";
12
-
13
- function buildCommentNoticeQueueKey(event: {
14
- notice_meta?: {
15
- file_type?: string;
16
- file_token?: string;
17
- };
18
- }): string {
19
- const fileType = event.notice_meta?.file_type?.trim() || "unknown";
20
- const fileToken = event.notice_meta?.file_token?.trim() || "unknown";
21
- return `comment-doc:${fileType}:${fileToken}`;
22
- }
23
-
24
- export function createFeishuDriveCommentNoticeHandler(params: {
25
- cfg: ClawdbotConfig;
26
- accountId: string;
27
- runtime?: RuntimeEnv;
28
- fireAndForget?: boolean;
29
- getBotOpenId?: (accountId: string) => string | undefined;
30
- }): (data: unknown) => Promise<void> {
31
- const { cfg, accountId, runtime, fireAndForget } = params;
32
- const log = runtime?.log ?? console.log;
33
- const error = runtime?.error ?? console.error;
34
- const enqueue = createSequentialQueue();
35
- const getBotOpenId = params.getBotOpenId ?? ((id) => botOpenIds.get(id));
36
-
37
- const runFeishuHandler = async (task: () => Promise<void>) => {
38
- const promise = task().catch((err) => {
39
- error(`feishu[${accountId}]: error handling drive comment notice: ${String(err)}`);
40
- });
41
- if (!fireAndForget) {
42
- await promise;
43
- }
44
- };
45
-
46
- return async (data: unknown) => {
47
- await runFeishuHandler(async () => {
48
- const event = parseFeishuDriveCommentNoticeEventPayload(data);
49
- if (!event) {
50
- error(`feishu[${accountId}]: ignoring malformed drive comment notice payload`);
51
- return;
52
- }
53
- const eventId = event.event_id?.trim();
54
- const syntheticMessageId = eventId ? `drive-comment:${eventId}` : undefined;
55
- if (syntheticMessageId) {
56
- const claim = await claimUnprocessedFeishuMessage({
57
- messageId: syntheticMessageId,
58
- namespace: accountId,
59
- log,
60
- });
61
- if (claim === "duplicate") {
62
- log(`feishu[${accountId}]: dropping duplicate comment event ${syntheticMessageId}`);
63
- return;
64
- }
65
- if (claim === "inflight") {
66
- log(`feishu[${accountId}]: dropping in-flight comment event ${syntheticMessageId}`);
67
- return;
68
- }
69
- }
70
- log(
71
- `feishu[${accountId}]: received drive comment notice ` +
72
- `event=${event.event_id ?? "unknown"} ` +
73
- `type=${event.notice_meta?.notice_type ?? "unknown"} ` +
74
- `file=${event.notice_meta?.file_type ?? "unknown"}:${event.notice_meta?.file_token ?? "unknown"} ` +
75
- `comment=${event.comment_id ?? "unknown"} ` +
76
- `reply=${event.reply_id ?? "none"} ` +
77
- `from=${event.notice_meta?.from_user_id?.open_id ?? "unknown"} ` +
78
- `mentioned=${event.is_mentioned === true ? "yes" : "no"}`,
79
- );
80
- try {
81
- await enqueue(buildCommentNoticeQueueKey(event), async () => {
82
- await handleFeishuCommentEvent({
83
- cfg,
84
- accountId,
85
- event,
86
- botOpenId: getBotOpenId(accountId),
87
- runtime,
88
- });
89
- });
90
- if (syntheticMessageId) {
91
- await recordProcessedFeishuMessage(syntheticMessageId, accountId, log);
92
- }
93
- } catch (err) {
94
- if (syntheticMessageId && !isFeishuRetryableSyntheticEventError(err)) {
95
- await recordProcessedFeishuMessage(syntheticMessageId, accountId, log);
96
- }
97
- throw err;
98
- } finally {
99
- if (syntheticMessageId) {
100
- releaseFeishuMessageProcessing(syntheticMessageId, accountId);
101
- }
102
- }
103
- });
104
- };
105
- }