@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,967 +0,0 @@
1
- import { createNonExitingRuntimeEnv } from "klaw/plugin-sdk/plugin-test-runtime";
2
- import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3
- import type { ClawdbotConfig } from "../runtime-api.js";
4
- import * as dedup from "./dedup.js";
5
- import { createFeishuDriveCommentNoticeHandler } from "./monitor.comment-notice-handler.js";
6
- import {
7
- resolveDriveCommentEventTurn,
8
- type FeishuDriveCommentNoticeEvent,
9
- } from "./monitor.comment.js";
10
-
11
- const handleFeishuCommentEventMock = vi.hoisted(() => vi.fn(async (_params?: unknown) => {}));
12
- const createFeishuClientMock = vi.hoisted(() => vi.fn());
13
-
14
- let lastRuntime = createNonExitingRuntimeEnv();
15
- const TEST_DOC_TOKEN = "ZsJfdxrBFo0RwuxteOLc1Ekvneb";
16
- const TEST_WIKI_TOKEN = "OtYpd5pKOoMeQzxrzkocv9KIn4H";
17
-
18
- vi.mock("./client.js", () => ({
19
- createFeishuClient: createFeishuClientMock,
20
- }));
21
-
22
- vi.mock("./comment-handler.js", () => ({
23
- handleFeishuCommentEvent: handleFeishuCommentEventMock,
24
- }));
25
-
26
- afterAll(() => {
27
- vi.doUnmock("./client.js");
28
- vi.doUnmock("./comment-handler.js");
29
- vi.resetModules();
30
- });
31
-
32
- function buildMonitorConfig(): ClawdbotConfig {
33
- return {
34
- channels: {
35
- feishu: {
36
- enabled: true,
37
- },
38
- },
39
- } as ClawdbotConfig;
40
- }
41
-
42
- function makeDriveCommentEvent(
43
- overrides: Partial<FeishuDriveCommentNoticeEvent> = {},
44
- ): FeishuDriveCommentNoticeEvent {
45
- return {
46
- comment_id: "7623358762119646411",
47
- event_id: "10d9d60b990db39f96a4c2fd357fb877",
48
- is_mentioned: true,
49
- notice_meta: {
50
- file_token: TEST_DOC_TOKEN,
51
- file_type: "docx",
52
- from_user_id: {
53
- open_id: "ou_509d4d7ace4a9addec2312676ffcba9b",
54
- },
55
- notice_type: "add_comment",
56
- to_user_id: {
57
- open_id: "ou_bot",
58
- },
59
- },
60
- reply_id: "7623358762136374451",
61
- timestamp: "1774951528000",
62
- type: "drive.notice.comment_add_v1",
63
- ...overrides,
64
- };
65
- }
66
-
67
- function makeOpenApiClient(params: {
68
- documentTitle?: string;
69
- documentUrl?: string;
70
- isWholeComment?: boolean;
71
- batchCommentId?: string;
72
- quoteText?: string;
73
- rootReplyText?: string;
74
- targetReplyText?: string;
75
- includeTargetReplyInBatch?: boolean;
76
- repliesSequence?: Array<Array<{ reply_id: string; text: string }>>;
77
- }) {
78
- const remainingReplyBatches = [...(params.repliesSequence ?? [])];
79
- return {
80
- request: vi.fn(async (request: { method: "GET" | "POST"; url: string; data: unknown }) => {
81
- if (request.url === "/open-apis/drive/v1/metas/batch_query") {
82
- return {
83
- code: 0,
84
- data: {
85
- metas: [
86
- {
87
- doc_token: TEST_DOC_TOKEN,
88
- title: params.documentTitle ?? "Comment event handling request",
89
- url: params.documentUrl ?? `https://www.larksuite.com/docx/${TEST_DOC_TOKEN}`,
90
- },
91
- ],
92
- },
93
- };
94
- }
95
- if (request.url.includes("/comments/batch_query")) {
96
- return {
97
- code: 0,
98
- data: {
99
- items: [
100
- {
101
- comment_id: params.batchCommentId ?? "7623358762119646411",
102
- is_whole: params.isWholeComment,
103
- quote: params.quoteText ?? "im.message.receive_v1 message trigger implementation",
104
- reply_list: {
105
- replies: [
106
- {
107
- reply_id: "7623358762136374451",
108
- content: {
109
- elements: [
110
- {
111
- type: "text_run",
112
- text_run: {
113
- content:
114
- params.rootReplyText ??
115
- "Also send it to the agent after receiving the comment event",
116
- },
117
- },
118
- ],
119
- },
120
- },
121
- ...(params.includeTargetReplyInBatch
122
- ? [
123
- {
124
- reply_id: "7623359125036043462",
125
- content: {
126
- elements: [
127
- {
128
- type: "text_run",
129
- text_run: {
130
- content:
131
- params.targetReplyText ?? "Please follow up on this comment",
132
- },
133
- },
134
- ],
135
- },
136
- },
137
- ]
138
- : []),
139
- ],
140
- },
141
- },
142
- ],
143
- },
144
- };
145
- }
146
- if (request.url.includes("/replies")) {
147
- const replyBatch = remainingReplyBatches.shift();
148
- const items = replyBatch?.map((reply) => ({
149
- reply_id: reply.reply_id,
150
- content: {
151
- elements: [
152
- {
153
- type: "text_run",
154
- text_run: {
155
- content: reply.text,
156
- },
157
- },
158
- ],
159
- },
160
- })) ?? [
161
- {
162
- reply_id: "7623358762136374451",
163
- content: {
164
- elements: [
165
- {
166
- type: "text_run",
167
- text_run: {
168
- content:
169
- params.rootReplyText ??
170
- "Also send it to the agent after receiving the comment event",
171
- },
172
- },
173
- ],
174
- },
175
- },
176
- {
177
- reply_id: "7623359125036043462",
178
- content: {
179
- elements: [
180
- {
181
- type: "text_run",
182
- text_run: {
183
- content: params.targetReplyText ?? "Please follow up on this comment",
184
- },
185
- },
186
- ],
187
- },
188
- },
189
- ];
190
- return {
191
- code: 0,
192
- data: {
193
- has_more: false,
194
- items,
195
- },
196
- };
197
- }
198
- throw new Error(`unexpected request: ${request.method} ${request.url}`);
199
- }),
200
- };
201
- }
202
-
203
- async function setupCommentMonitorHandler(): Promise<(data: unknown) => Promise<void>> {
204
- lastRuntime = createNonExitingRuntimeEnv();
205
-
206
- return createFeishuDriveCommentNoticeHandler({
207
- cfg: buildMonitorConfig(),
208
- accountId: "default",
209
- runtime: lastRuntime,
210
- fireAndForget: true,
211
- getBotOpenId: () => "ou_bot",
212
- });
213
- }
214
-
215
- function mockCallAt(
216
- mock: { mock: { calls: Array<readonly unknown[]> } },
217
- index: number,
218
- label: string,
219
- ): readonly unknown[] {
220
- const call = mock.mock.calls[index];
221
- if (!call) {
222
- throw new Error(`expected ${label} call`);
223
- }
224
- return call;
225
- }
226
-
227
- describe("resolveDriveCommentEventTurn", () => {
228
- it("builds a real comment-turn prompt for add_comment notices", async () => {
229
- const client = makeOpenApiClient({ includeTargetReplyInBatch: true });
230
-
231
- const turn = await resolveDriveCommentEventTurn({
232
- cfg: buildMonitorConfig(),
233
- accountId: "default",
234
- event: makeDriveCommentEvent(),
235
- botOpenId: "ou_bot",
236
- createClient: () => client as never,
237
- });
238
-
239
- expect(turn?.senderId).toBe("ou_509d4d7ace4a9addec2312676ffcba9b");
240
- expect(turn?.messageId).toBe("drive-comment:10d9d60b990db39f96a4c2fd357fb877");
241
- expect(turn?.fileType).toBe("docx");
242
- expect(turn?.fileToken).toBe(TEST_DOC_TOKEN);
243
- expect(turn?.prompt).toContain('The user added a comment in "Comment event handling request".');
244
- expect(turn?.prompt).toContain(
245
- 'Current user comment text: "Also send it to the agent after receiving the comment event"',
246
- );
247
- expect(turn?.prompt).toContain("Current comment card timeline (primary context");
248
- expect(turn?.prompt).toContain("This is a Feishu document comment thread.");
249
- expect(turn?.prompt).toContain("It is not a Feishu IM chat.");
250
- expect(turn?.prompt).toContain("Use plain text only.");
251
- expect(turn?.prompt).toContain("Do not show reasoning.");
252
- expect(turn?.prompt).toContain("Do not describe your plan.");
253
- expect(turn?.prompt).toContain("Output only the final user-facing reply.");
254
- expect(turn?.prompt).toContain("comment_id: 7623358762119646411");
255
- expect(turn?.prompt).toContain("reply_id: 7623358762136374451");
256
- expect(turn?.prompt).toContain(
257
- "Your final text reply will be posted to the current comment thread automatically.",
258
- );
259
- });
260
-
261
- it("parses bot mentions plus current and referenced document links from comment content", async () => {
262
- const wikiGetNode = vi.fn(async () => ({
263
- code: 0,
264
- data: {
265
- node: {
266
- obj_type: "docx",
267
- obj_token: "doc_ref_1",
268
- },
269
- },
270
- }));
271
- const client = {
272
- request: vi.fn(async (request: { method: "GET" | "POST"; url: string; data: unknown }) => {
273
- if (request.url === "/open-apis/drive/v1/metas/batch_query") {
274
- return {
275
- code: 0,
276
- data: {
277
- metas: [
278
- {
279
- doc_token: TEST_DOC_TOKEN,
280
- title: "Comment event handling request",
281
- url: `https://www.larksuite.com/docx/${TEST_DOC_TOKEN}`,
282
- },
283
- ],
284
- },
285
- };
286
- }
287
- if (request.url.includes("/comments/batch_query")) {
288
- return {
289
- code: 0,
290
- data: {
291
- items: [
292
- {
293
- comment_id: "7623358762119646411",
294
- is_whole: false,
295
- reply_list: {
296
- replies: [
297
- {
298
- reply_id: "7623358762136374451",
299
- user_id: "ou_509d4d7ace4a9addec2312676ffcba9b",
300
- content: {
301
- elements: [
302
- { type: "text_run", text_run: { text: "请 " } },
303
- { type: "person", person: { user_id: "ou_bot" } },
304
- { type: "text_run", text_run: { text: " 总结下 " } },
305
- {
306
- type: "docs_link",
307
- docs_link: {
308
- url: `https://www.larksuite.com/docx/${TEST_DOC_TOKEN}`,
309
- },
310
- },
311
- { type: "text_run", text_run: { text: " 和 " } },
312
- {
313
- type: "docs_link",
314
- docs_link: {
315
- url: `https://www.larksuite.com/wiki/${TEST_WIKI_TOKEN}`,
316
- },
317
- },
318
- ],
319
- },
320
- },
321
- ],
322
- },
323
- },
324
- ],
325
- },
326
- };
327
- }
328
- throw new Error(`unexpected request: ${request.method} ${request.url}`);
329
- }),
330
- wiki: {
331
- space: {
332
- getNode: wikiGetNode,
333
- },
334
- },
335
- };
336
-
337
- const turn = await resolveDriveCommentEventTurn({
338
- cfg: buildMonitorConfig(),
339
- accountId: "default",
340
- event: makeDriveCommentEvent(),
341
- botOpenId: "ou_bot",
342
- createClient: () => client as never,
343
- });
344
-
345
- expect(turn?.targetReplyText).toBe(
346
- `请 总结下 https://www.larksuite.com/docx/${TEST_DOC_TOKEN} 和 https://www.larksuite.com/wiki/${TEST_WIKI_TOKEN}`,
347
- );
348
- expect(turn?.prompt).toContain("Bot routing mention detected in the current user comment.");
349
- expect(turn?.prompt).toContain("Referenced documents from current user comment:");
350
- expect(turn?.prompt).toContain(
351
- `raw_url=https://www.larksuite.com/docx/${TEST_DOC_TOKEN} url_kind=docx`,
352
- );
353
- expect(turn?.prompt).toContain("same_as_current_document=yes");
354
- expect(turn?.prompt).toContain(
355
- `raw_url=https://www.larksuite.com/wiki/${TEST_WIKI_TOKEN} url_kind=wiki ` +
356
- `wiki_node_token=${TEST_WIKI_TOKEN} resolved_type=docx ` +
357
- "resolved_token=doc_ref_1 same_as_current_document=no",
358
- );
359
- expect(wikiGetNode).toHaveBeenCalledWith({
360
- params: {
361
- token: TEST_WIKI_TOKEN,
362
- },
363
- });
364
- });
365
-
366
- it("preserves whole-document comment metadata for downstream delivery mode selection", async () => {
367
- const client = makeOpenApiClient({
368
- includeTargetReplyInBatch: true,
369
- isWholeComment: true,
370
- });
371
-
372
- const turn = await resolveDriveCommentEventTurn({
373
- cfg: buildMonitorConfig(),
374
- accountId: "default",
375
- event: makeDriveCommentEvent(),
376
- botOpenId: "ou_bot",
377
- createClient: () => client as never,
378
- });
379
-
380
- expect(turn?.isWholeComment).toBe(true);
381
- expect(turn?.prompt).toContain("This is a whole-document comment.");
382
- expect(turn?.prompt).toContain("Whole-document comments do not support direct replies.");
383
- });
384
-
385
- it("builds a whole-comment timeline and highlights the nearest bot-authored follow-up", async () => {
386
- const client = {
387
- request: vi.fn(async (request: { method: "GET" | "POST"; url: string; data: unknown }) => {
388
- if (request.url === "/open-apis/drive/v1/metas/batch_query") {
389
- return {
390
- code: 0,
391
- data: {
392
- metas: [
393
- {
394
- doc_token: TEST_DOC_TOKEN,
395
- title: "Comment event handling request",
396
- url: `https://www.larksuite.com/docx/${TEST_DOC_TOKEN}`,
397
- },
398
- ],
399
- },
400
- };
401
- }
402
- if (request.url.includes("/comments/batch_query")) {
403
- return {
404
- code: 0,
405
- data: {
406
- items: [
407
- {
408
- comment_id: "7623358762119646411",
409
- is_whole: true,
410
- reply_list: {
411
- replies: [
412
- {
413
- reply_id: "7623358762136374451",
414
- user_id: "ou_509d4d7ace4a9addec2312676ffcba9b",
415
- create_time: 1775531531,
416
- content: {
417
- elements: [
418
- {
419
- type: "text_run",
420
- text_run: {
421
- text: "请帮我总结这个文档",
422
- },
423
- },
424
- ],
425
- },
426
- },
427
- ],
428
- },
429
- },
430
- ],
431
- },
432
- };
433
- }
434
- if (request.url.includes("/comments?file_type=docx&is_whole=true")) {
435
- return {
436
- code: 0,
437
- data: {
438
- has_more: false,
439
- items: [
440
- {
441
- comment_id: "7623358762119646411",
442
- create_time: 1775531531,
443
- user_id: "ou_509d4d7ace4a9addec2312676ffcba9b",
444
- is_whole: true,
445
- reply_list: {
446
- replies: [
447
- {
448
- reply_id: "reply_a",
449
- user_id: "ou_509d4d7ace4a9addec2312676ffcba9b",
450
- create_time: 1775531531,
451
- content: {
452
- elements: [
453
- {
454
- type: "text_run",
455
- text_run: {
456
- text: "请帮我总结这个文档",
457
- },
458
- },
459
- ],
460
- },
461
- },
462
- ],
463
- },
464
- },
465
- {
466
- comment_id: "comment_bot_followup",
467
- create_time: 1775531540,
468
- user_id: "ou_bot",
469
- is_whole: true,
470
- reply_list: {
471
- replies: [
472
- {
473
- reply_id: "reply_b",
474
- user_id: "ou_bot",
475
- create_time: 1775531540,
476
- content: {
477
- elements: [
478
- {
479
- type: "text_run",
480
- text_run: {
481
- text: "这是刚才的总结结果",
482
- },
483
- },
484
- ],
485
- },
486
- },
487
- ],
488
- },
489
- },
490
- {
491
- comment_id: "comment_other_user",
492
- create_time: 1775531550,
493
- user_id: "ou_other",
494
- is_whole: true,
495
- reply_list: {
496
- replies: [
497
- {
498
- reply_id: "reply_c",
499
- user_id: "ou_other",
500
- create_time: 1775531550,
501
- content: {
502
- elements: [
503
- {
504
- type: "text_run",
505
- text_run: {
506
- text: "另一个 whole comment",
507
- },
508
- },
509
- ],
510
- },
511
- },
512
- ],
513
- },
514
- },
515
- ],
516
- },
517
- };
518
- }
519
- throw new Error(`unexpected request: ${request.method} ${request.url}`);
520
- }),
521
- wiki: {
522
- space: {
523
- getNode: vi.fn(async () => ({ code: 0, data: { node: {} } })),
524
- },
525
- },
526
- };
527
-
528
- const turn = await resolveDriveCommentEventTurn({
529
- cfg: buildMonitorConfig(),
530
- accountId: "default",
531
- event: makeDriveCommentEvent(),
532
- botOpenId: "ou_bot",
533
- createClient: () => client as never,
534
- });
535
-
536
- expect(turn?.isWholeComment).toBe(true);
537
- expect(turn?.prompt).toContain(
538
- "Whole-document comment timeline (primary context for whole-comment follow-ups):",
539
- );
540
- expect(turn?.prompt).toContain("comment_id=7623358762119646411");
541
- expect(turn?.prompt).toContain("comment_id=comment_bot_followup");
542
- expect(turn?.prompt).toContain(
543
- 'Nearest bot-authored whole-comment after the current comment: comment_id=comment_bot_followup text="这是刚才的总结结果"',
544
- );
545
- expect(turn?.prompt).toContain("Document-level session history is auxiliary background only.");
546
- });
547
-
548
- it("treats replies with missing user_id as user-authored even when bot id hints are missing", async () => {
549
- const client = {
550
- request: vi.fn(async (request: { method: "GET" | "POST"; url: string; data: unknown }) => {
551
- if (request.url === "/open-apis/drive/v1/metas/batch_query") {
552
- return {
553
- code: 0,
554
- data: {
555
- metas: [
556
- {
557
- doc_token: TEST_DOC_TOKEN,
558
- title: "Comment event handling request",
559
- url: `https://www.larksuite.com/docx/${TEST_DOC_TOKEN}`,
560
- },
561
- ],
562
- },
563
- };
564
- }
565
- if (request.url.includes("/comments/batch_query")) {
566
- return {
567
- code: 0,
568
- data: {
569
- items: [
570
- {
571
- comment_id: "7623358762119646411",
572
- is_whole: true,
573
- reply_list: {
574
- replies: [
575
- {
576
- reply_id: "reply_missing_user",
577
- create_time: 1775531531,
578
- content: {
579
- elements: [
580
- {
581
- type: "text_run",
582
- text_run: {
583
- text: "reply without user id",
584
- },
585
- },
586
- ],
587
- },
588
- },
589
- ],
590
- },
591
- },
592
- ],
593
- },
594
- };
595
- }
596
- if (request.url.includes("/comments?file_type=docx&is_whole=true")) {
597
- return {
598
- code: 0,
599
- data: {
600
- has_more: false,
601
- items: [
602
- {
603
- comment_id: "7623358762119646411",
604
- create_time: 1775531531,
605
- is_whole: true,
606
- reply_list: {
607
- replies: [
608
- {
609
- reply_id: "reply_missing_user",
610
- create_time: 1775531531,
611
- content: {
612
- elements: [
613
- {
614
- type: "text_run",
615
- text_run: {
616
- text: "reply without user id",
617
- },
618
- },
619
- ],
620
- },
621
- },
622
- ],
623
- },
624
- },
625
- ],
626
- },
627
- };
628
- }
629
- throw new Error(`unexpected request: ${request.method} ${request.url}`);
630
- }),
631
- wiki: {
632
- space: {
633
- getNode: vi.fn(async () => ({ code: 0, data: { node: {} } })),
634
- },
635
- },
636
- };
637
-
638
- const turn = await resolveDriveCommentEventTurn({
639
- cfg: buildMonitorConfig(),
640
- accountId: "default",
641
- event: makeDriveCommentEvent({
642
- reply_id: "reply_missing_user",
643
- }),
644
- botOpenId: "ou_bot",
645
- createClient: () => client as never,
646
- });
647
-
648
- expect(turn?.prompt).toContain(
649
- "comment_id=7623358762119646411 author=user user_id=UNKNOWN current_comment=yes",
650
- );
651
- expect(turn?.prompt).not.toContain(
652
- "author=assistant user_id=UNKNOWN reply_id=reply_missing_user",
653
- );
654
- });
655
-
656
- it("does not trust whole-comment metadata from a mismatched batch_query item", async () => {
657
- const client = makeOpenApiClient({
658
- includeTargetReplyInBatch: true,
659
- isWholeComment: true,
660
- batchCommentId: "different_comment_id",
661
- });
662
-
663
- const turn = await resolveDriveCommentEventTurn({
664
- cfg: buildMonitorConfig(),
665
- accountId: "default",
666
- event: makeDriveCommentEvent(),
667
- botOpenId: "ou_bot",
668
- createClient: () => client as never,
669
- });
670
-
671
- expect(turn?.isWholeComment).toBeUndefined();
672
- expect(turn?.prompt).not.toContain("This is a whole-document comment.");
673
- });
674
-
675
- it("preserves sender user_id for downstream allowlist checks", async () => {
676
- const client = makeOpenApiClient({ includeTargetReplyInBatch: true });
677
-
678
- const turn = await resolveDriveCommentEventTurn({
679
- cfg: buildMonitorConfig(),
680
- accountId: "default",
681
- event: makeDriveCommentEvent({
682
- notice_meta: {
683
- ...makeDriveCommentEvent().notice_meta,
684
- from_user_id: {
685
- open_id: "ou_509d4d7ace4a9addec2312676ffcba9b",
686
- user_id: "on_comment_user_1",
687
- },
688
- },
689
- }),
690
- botOpenId: "ou_bot",
691
- createClient: () => client as never,
692
- });
693
-
694
- expect(turn?.senderId).toBe("ou_509d4d7ace4a9addec2312676ffcba9b");
695
- expect(turn?.senderUserId).toBe("on_comment_user_1");
696
- });
697
-
698
- it("falls back to the replies API to resolve add_reply text", async () => {
699
- const client = makeOpenApiClient({
700
- includeTargetReplyInBatch: false,
701
- targetReplyText: "Please follow up on this comment",
702
- });
703
-
704
- const turn = await resolveDriveCommentEventTurn({
705
- cfg: buildMonitorConfig(),
706
- accountId: "default",
707
- event: makeDriveCommentEvent({
708
- notice_meta: {
709
- ...makeDriveCommentEvent().notice_meta,
710
- notice_type: "add_reply",
711
- },
712
- reply_id: "7623359125036043462",
713
- }),
714
- botOpenId: "ou_bot",
715
- createClient: () => client as never,
716
- });
717
-
718
- expect(turn?.prompt).toContain('The user added a reply in "Comment event handling request".');
719
- expect(turn?.prompt).toContain('Current user comment text: "Please follow up on this comment"');
720
- expect(turn?.prompt).toContain(
721
- 'Original comment text: "Also send it to the agent after receiving the comment event"',
722
- );
723
- expect(turn?.prompt).toContain(`file_token: ${TEST_DOC_TOKEN}`);
724
- expect(turn?.prompt).toContain("Event type: add_reply");
725
- const replyLookup = client.request.mock.calls
726
- .map(([request]) => request)
727
- .find((request) => request.url.includes("/comments/7623358762119646411/replies"));
728
- expect(replyLookup).toEqual({
729
- method: "GET",
730
- url: `/open-apis/drive/v1/files/${TEST_DOC_TOKEN}/comments/7623358762119646411/replies?file_type=docx&page_size=100&user_id_type=open_id`,
731
- data: {},
732
- timeout: 3000,
733
- });
734
- });
735
-
736
- it("retries comment reply lookup when the requested reply is not immediately visible", async () => {
737
- const waitMs = vi.fn(async () => {});
738
- const client = makeOpenApiClient({
739
- includeTargetReplyInBatch: false,
740
- repliesSequence: [
741
- [
742
- {
743
- reply_id: "7623358762136374451",
744
- text: "Also send it to the agent after receiving the comment event",
745
- },
746
- { reply_id: "7623358762999999999", text: "Earlier assistant summary" },
747
- ],
748
- [
749
- {
750
- reply_id: "7623358762136374451",
751
- text: "Also send it to the agent after receiving the comment event",
752
- },
753
- { reply_id: "7623358762999999999", text: "Earlier assistant summary" },
754
- ],
755
- [
756
- {
757
- reply_id: "7623358762136374451",
758
- text: "Also send it to the agent after receiving the comment event",
759
- },
760
- { reply_id: "7623359125999999999", text: "Insert a sentence below this paragraph" },
761
- ],
762
- ],
763
- });
764
-
765
- const turn = await resolveDriveCommentEventTurn({
766
- cfg: buildMonitorConfig(),
767
- accountId: "default",
768
- event: makeDriveCommentEvent({
769
- notice_meta: {
770
- ...makeDriveCommentEvent().notice_meta,
771
- notice_type: "add_reply",
772
- },
773
- reply_id: "7623359125999999999",
774
- }),
775
- botOpenId: "ou_bot",
776
- createClient: () => client as never,
777
- waitMs,
778
- });
779
-
780
- expect(turn?.targetReplyText).toBe("Insert a sentence below this paragraph");
781
- expect(turn?.prompt).toContain("Insert a sentence below this paragraph");
782
- expect(waitMs).toHaveBeenCalledTimes(2);
783
- expect(waitMs).toHaveBeenNthCalledWith(1, 1000);
784
- expect(waitMs).toHaveBeenNthCalledWith(2, 1000);
785
- expect(
786
- client.request.mock.calls.filter(
787
- ([request]: [{ method: string; url: string }]) =>
788
- request.method === "GET" && request.url.includes("/replies"),
789
- ),
790
- ).toHaveLength(3);
791
- });
792
-
793
- it("ignores self-authored comment notices", async () => {
794
- const turn = await resolveDriveCommentEventTurn({
795
- cfg: buildMonitorConfig(),
796
- accountId: "default",
797
- event: makeDriveCommentEvent({
798
- notice_meta: {
799
- ...makeDriveCommentEvent().notice_meta,
800
- from_user_id: { open_id: "ou_bot" },
801
- },
802
- }),
803
- botOpenId: "ou_bot",
804
- createClient: () => makeOpenApiClient({}) as never,
805
- });
806
-
807
- expect(turn).toBeNull();
808
- });
809
-
810
- it("skips comment notices when bot open_id is unavailable", async () => {
811
- const turn = await resolveDriveCommentEventTurn({
812
- cfg: buildMonitorConfig(),
813
- accountId: "default",
814
- event: makeDriveCommentEvent(),
815
- botOpenId: undefined,
816
- createClient: () => makeOpenApiClient({}) as never,
817
- });
818
-
819
- expect(turn).toBeNull();
820
- });
821
- });
822
-
823
- describe("drive.notice.comment_add_v1 monitor handler", () => {
824
- beforeEach(() => {
825
- lastRuntime = createNonExitingRuntimeEnv();
826
- handleFeishuCommentEventMock.mockClear();
827
- createFeishuClientMock.mockReset().mockReturnValue(makeOpenApiClient({}) as never);
828
- vi.spyOn(dedup, "claimUnprocessedFeishuMessage").mockResolvedValue("claimed");
829
- vi.spyOn(dedup, "recordProcessedFeishuMessage").mockResolvedValue(true);
830
- vi.spyOn(dedup, "releaseFeishuMessageProcessing").mockImplementation(() => {});
831
- });
832
-
833
- afterEach(() => {
834
- vi.restoreAllMocks();
835
- });
836
-
837
- it("dispatches comment notices through handleFeishuCommentEvent", async () => {
838
- const onComment = await setupCommentMonitorHandler();
839
-
840
- await onComment(makeDriveCommentEvent());
841
-
842
- expect(handleFeishuCommentEventMock).toHaveBeenCalledTimes(1);
843
- const handleArgs = mockCallAt(handleFeishuCommentEventMock, 0, "Feishu comment handler")[0] as
844
- | {
845
- accountId?: string;
846
- botOpenId?: string;
847
- event?: { comment_id?: string; event_id?: string };
848
- }
849
- | undefined;
850
- expect(handleArgs?.accountId).toBe("default");
851
- expect(handleArgs?.botOpenId).toBe("ou_bot");
852
- expect(handleArgs?.event?.event_id).toBe("10d9d60b990db39f96a4c2fd357fb877");
853
- expect(handleArgs?.event?.comment_id).toBe("7623358762119646411");
854
- });
855
-
856
- it("serializes same-document comment notices before invoking handleFeishuCommentEvent", async () => {
857
- const onComment = await setupCommentMonitorHandler();
858
- let resolveFirst: (() => void) | undefined;
859
- handleFeishuCommentEventMock
860
- .mockImplementationOnce(
861
- () =>
862
- new Promise<void>((resolve) => {
863
- resolveFirst = resolve;
864
- }),
865
- )
866
- .mockImplementationOnce(async () => {});
867
-
868
- await onComment(
869
- makeDriveCommentEvent({
870
- event_id: "evt_1",
871
- reply_id: "reply_1",
872
- }),
873
- );
874
- await vi.waitFor(() => {
875
- expect(handleFeishuCommentEventMock).toHaveBeenCalledTimes(1);
876
- });
877
-
878
- await onComment(
879
- makeDriveCommentEvent({
880
- event_id: "evt_2",
881
- reply_id: "reply_2",
882
- }),
883
- );
884
- await vi.waitFor(() => {
885
- expect(dedup.claimUnprocessedFeishuMessage).toHaveBeenCalledTimes(2);
886
- });
887
-
888
- expect(handleFeishuCommentEventMock).toHaveBeenCalledTimes(1);
889
-
890
- resolveFirst?.();
891
-
892
- await vi.waitFor(() => {
893
- expect(handleFeishuCommentEventMock).toHaveBeenCalledTimes(2);
894
- });
895
- const firstCallArgs = mockCallAt(
896
- handleFeishuCommentEventMock,
897
- 0,
898
- "first Feishu comment handler",
899
- ) as [{ event?: { event_id?: string } }] | undefined;
900
- const secondCallArgs = mockCallAt(
901
- handleFeishuCommentEventMock,
902
- 1,
903
- "second Feishu comment handler",
904
- ) as [{ event?: { event_id?: string } }] | undefined;
905
- const firstCall = firstCallArgs?.[0];
906
- const secondCall = secondCallArgs?.[0];
907
- expect(firstCall?.event?.event_id).toBe("evt_1");
908
- expect(secondCall?.event?.event_id).toBe("evt_2");
909
- });
910
-
911
- it("drops duplicate comment events before dispatch", async () => {
912
- vi.spyOn(dedup, "claimUnprocessedFeishuMessage").mockResolvedValue("duplicate");
913
- const onComment = await setupCommentMonitorHandler();
914
-
915
- await onComment(makeDriveCommentEvent());
916
-
917
- expect(handleFeishuCommentEventMock).not.toHaveBeenCalled();
918
- });
919
-
920
- it("records generic comment-handler failures so replay stays closed", async () => {
921
- const onComment = await setupCommentMonitorHandler();
922
- handleFeishuCommentEventMock.mockRejectedValueOnce(new Error("post-send failure"));
923
-
924
- await onComment(makeDriveCommentEvent());
925
-
926
- await vi.waitFor(() => {
927
- expect(dedup.recordProcessedFeishuMessage).toHaveBeenCalledTimes(1);
928
- expect(dedup.releaseFeishuMessageProcessing).toHaveBeenCalledWith(
929
- "drive-comment:10d9d60b990db39f96a4c2fd357fb877",
930
- "default",
931
- );
932
- expect(lastRuntime?.error).toHaveBeenCalledWith(
933
- "feishu[default]: error handling drive comment notice: Error: post-send failure",
934
- );
935
- });
936
- const [recordedMessageId, recordedNamespace, recordedLogger] = mockCallAt(
937
- dedup.recordProcessedFeishuMessage as ReturnType<typeof vi.fn>,
938
- 0,
939
- "Feishu processed-message record",
940
- );
941
- expect(recordedMessageId).toBe("drive-comment:10d9d60b990db39f96a4c2fd357fb877");
942
- expect(recordedNamespace).toBe("default");
943
- expect(typeof recordedLogger).toBe("function");
944
- });
945
-
946
- it("releases comment replay without recording when failure is explicitly retryable", async () => {
947
- const onComment = await setupCommentMonitorHandler();
948
- handleFeishuCommentEventMock.mockRejectedValueOnce(
949
- Object.assign(new Error("retry me"), {
950
- name: "FeishuRetryableSyntheticEventError",
951
- }),
952
- );
953
-
954
- await onComment(makeDriveCommentEvent());
955
-
956
- await vi.waitFor(() => {
957
- expect(dedup.recordProcessedFeishuMessage).not.toHaveBeenCalled();
958
- expect(dedup.releaseFeishuMessageProcessing).toHaveBeenCalledWith(
959
- "drive-comment:10d9d60b990db39f96a4c2fd357fb877",
960
- "default",
961
- );
962
- expect(lastRuntime?.error).toHaveBeenCalledWith(
963
- "feishu[default]: error handling drive comment notice: FeishuRetryableSyntheticEventError: retry me",
964
- );
965
- });
966
- });
967
- });