@openclaw/feishu 2026.5.2-beta.2 → 2026.5.3-beta.1

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