@openclaw/feishu 2026.2.25 → 2026.3.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 (73) hide show
  1. package/index.ts +2 -0
  2. package/package.json +2 -1
  3. package/skills/feishu-doc/SKILL.md +109 -3
  4. package/src/accounts.test.ts +161 -0
  5. package/src/accounts.ts +76 -8
  6. package/src/async.ts +62 -0
  7. package/src/bitable.ts +189 -215
  8. package/src/bot.card-action.test.ts +63 -0
  9. package/src/bot.checkBotMentioned.test.ts +56 -1
  10. package/src/bot.test.ts +1271 -56
  11. package/src/bot.ts +499 -215
  12. package/src/card-action.ts +79 -0
  13. package/src/channel.ts +26 -4
  14. package/src/chat-schema.ts +24 -0
  15. package/src/chat.test.ts +89 -0
  16. package/src/chat.ts +130 -0
  17. package/src/client.test.ts +121 -0
  18. package/src/client.ts +13 -0
  19. package/src/config-schema.test.ts +101 -1
  20. package/src/config-schema.ts +66 -11
  21. package/src/dedup.ts +47 -1
  22. package/src/doc-schema.ts +135 -0
  23. package/src/docx-batch-insert.ts +190 -0
  24. package/src/docx-color-text.ts +149 -0
  25. package/src/docx-table-ops.ts +298 -0
  26. package/src/docx.account-selection.test.ts +70 -0
  27. package/src/docx.test.ts +331 -9
  28. package/src/docx.ts +996 -72
  29. package/src/drive.ts +38 -33
  30. package/src/media.test.ts +227 -7
  31. package/src/media.ts +52 -11
  32. package/src/mention.ts +1 -1
  33. package/src/monitor.account.ts +534 -0
  34. package/src/monitor.reaction.test.ts +578 -0
  35. package/src/monitor.startup.test.ts +203 -0
  36. package/src/monitor.startup.ts +51 -0
  37. package/src/monitor.state.defaults.test.ts +46 -0
  38. package/src/monitor.state.ts +152 -0
  39. package/src/monitor.test-mocks.ts +12 -0
  40. package/src/monitor.transport.ts +163 -0
  41. package/src/monitor.ts +44 -346
  42. package/src/monitor.webhook-security.test.ts +53 -10
  43. package/src/onboarding.status.test.ts +25 -0
  44. package/src/onboarding.ts +144 -52
  45. package/src/outbound.test.ts +181 -0
  46. package/src/outbound.ts +94 -7
  47. package/src/perm.ts +37 -30
  48. package/src/policy.test.ts +56 -1
  49. package/src/policy.ts +5 -1
  50. package/src/post.test.ts +105 -0
  51. package/src/post.ts +274 -0
  52. package/src/probe.test.ts +271 -0
  53. package/src/probe.ts +131 -19
  54. package/src/reply-dispatcher.test.ts +300 -0
  55. package/src/reply-dispatcher.ts +159 -46
  56. package/src/secret-input.ts +19 -0
  57. package/src/send-target.test.ts +74 -0
  58. package/src/send-target.ts +6 -2
  59. package/src/send.reply-fallback.test.ts +105 -0
  60. package/src/send.test.ts +168 -0
  61. package/src/send.ts +143 -18
  62. package/src/streaming-card.ts +131 -43
  63. package/src/targets.test.ts +55 -1
  64. package/src/targets.ts +32 -7
  65. package/src/tool-account-routing.test.ts +129 -0
  66. package/src/tool-account.ts +70 -0
  67. package/src/tool-factory-test-harness.ts +76 -0
  68. package/src/tools-config.test.ts +21 -0
  69. package/src/tools-config.ts +2 -1
  70. package/src/types.ts +10 -1
  71. package/src/typing.test.ts +144 -0
  72. package/src/typing.ts +140 -10
  73. package/src/wiki.ts +55 -50
package/src/docx.test.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { promises as fs } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
1
4
  import { beforeEach, describe, expect, it, vi } from "vitest";
2
5
 
3
6
  const createFeishuClientMock = vi.hoisted(() => vi.fn());
@@ -21,9 +24,14 @@ import { registerFeishuDocTools } from "./docx.js";
21
24
 
22
25
  describe("feishu_doc image fetch hardening", () => {
23
26
  const convertMock = vi.hoisted(() => vi.fn());
27
+ const documentCreateMock = vi.hoisted(() => vi.fn());
24
28
  const blockListMock = vi.hoisted(() => vi.fn());
25
29
  const blockChildrenCreateMock = vi.hoisted(() => vi.fn());
30
+ const blockChildrenGetMock = vi.hoisted(() => vi.fn());
31
+ const blockChildrenBatchDeleteMock = vi.hoisted(() => vi.fn());
32
+ const blockDescendantCreateMock = vi.hoisted(() => vi.fn());
26
33
  const driveUploadAllMock = vi.hoisted(() => vi.fn());
34
+ const permissionMemberCreateMock = vi.hoisted(() => vi.fn());
27
35
  const blockPatchMock = vi.hoisted(() => vi.fn());
28
36
  const scopeListMock = vi.hoisted(() => vi.fn());
29
37
 
@@ -34,6 +42,7 @@ describe("feishu_doc image fetch hardening", () => {
34
42
  docx: {
35
43
  document: {
36
44
  convert: convertMock,
45
+ create: documentCreateMock,
37
46
  },
38
47
  documentBlock: {
39
48
  list: blockListMock,
@@ -41,12 +50,20 @@ describe("feishu_doc image fetch hardening", () => {
41
50
  },
42
51
  documentBlockChildren: {
43
52
  create: blockChildrenCreateMock,
53
+ get: blockChildrenGetMock,
54
+ batchDelete: blockChildrenBatchDeleteMock,
55
+ },
56
+ documentBlockDescendant: {
57
+ create: blockDescendantCreateMock,
44
58
  },
45
59
  },
46
60
  drive: {
47
61
  media: {
48
62
  uploadAll: driveUploadAllMock,
49
63
  },
64
+ permissionMember: {
65
+ create: permissionMemberCreateMock,
66
+ },
50
67
  },
51
68
  application: {
52
69
  scope: {
@@ -77,17 +94,27 @@ describe("feishu_doc image fetch hardening", () => {
77
94
  },
78
95
  });
79
96
 
97
+ blockChildrenGetMock.mockResolvedValue({
98
+ code: 0,
99
+ data: { items: [{ block_id: "placeholder_block_1" }] },
100
+ });
101
+ blockChildrenBatchDeleteMock.mockResolvedValue({ code: 0 });
102
+ // write/append use Descendant API; return image block so processImages runs
103
+ blockDescendantCreateMock.mockResolvedValue({
104
+ code: 0,
105
+ data: { children: [{ block_type: 27, block_id: "img_block_1" }] },
106
+ });
80
107
  driveUploadAllMock.mockResolvedValue({ file_token: "token_1" });
108
+ documentCreateMock.mockResolvedValue({
109
+ code: 0,
110
+ data: { document: { document_id: "doc_created", title: "Created Doc" } },
111
+ });
112
+ permissionMemberCreateMock.mockResolvedValue({ code: 0 });
81
113
  blockPatchMock.mockResolvedValue({ code: 0 });
82
114
  scopeListMock.mockResolvedValue({ code: 0, data: { scopes: [] } });
83
115
  });
84
116
 
85
- it("skips image upload when markdown image URL is blocked", async () => {
86
- const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
87
- fetchRemoteMediaMock.mockRejectedValueOnce(
88
- new Error("Blocked: resolves to private/internal IP address"),
89
- );
90
-
117
+ function resolveFeishuDocTool(context: Record<string, unknown> = {}) {
91
118
  const registerTool = vi.fn();
92
119
  registerFeishuDocTools({
93
120
  config: {
@@ -102,10 +129,162 @@ describe("feishu_doc image fetch hardening", () => {
102
129
  registerTool,
103
130
  } as any);
104
131
 
105
- const feishuDocTool = registerTool.mock.calls
132
+ const tool = registerTool.mock.calls
106
133
  .map((call) => call[0])
107
- .find((tool) => tool.name === "feishu_doc");
108
- expect(feishuDocTool).toBeDefined();
134
+ .map((candidate) => (typeof candidate === "function" ? candidate(context) : candidate))
135
+ .find((candidate) => candidate.name === "feishu_doc");
136
+ expect(tool).toBeDefined();
137
+ return tool as { execute: (callId: string, params: Record<string, unknown>) => Promise<any> };
138
+ }
139
+
140
+ it("inserts blocks sequentially to preserve document order", async () => {
141
+ const blocks = [
142
+ { block_type: 3, block_id: "h1" },
143
+ { block_type: 2, block_id: "t1" },
144
+ { block_type: 3, block_id: "h2" },
145
+ ];
146
+ convertMock.mockResolvedValue({
147
+ code: 0,
148
+ data: {
149
+ blocks,
150
+ first_level_block_ids: ["h1", "t1", "h2"],
151
+ },
152
+ });
153
+
154
+ blockListMock.mockResolvedValue({ code: 0, data: { items: [] } });
155
+
156
+ blockDescendantCreateMock.mockResolvedValueOnce({
157
+ code: 0,
158
+ data: { children: [{ block_type: 3, block_id: "h1" }] },
159
+ });
160
+
161
+ const feishuDocTool = resolveFeishuDocTool();
162
+
163
+ const result = await feishuDocTool.execute("tool-call", {
164
+ action: "append",
165
+ doc_token: "doc_1",
166
+ content: "plain text body",
167
+ });
168
+
169
+ expect(blockDescendantCreateMock).toHaveBeenCalledTimes(1);
170
+ const call = blockDescendantCreateMock.mock.calls[0]?.[0];
171
+ expect(call?.data.children_id).toEqual(["h1", "t1", "h2"]);
172
+ expect(call?.data.descendants).toBeDefined();
173
+ expect(call?.data.descendants.length).toBeGreaterThanOrEqual(3);
174
+
175
+ expect(result.details.blocks_added).toBe(3);
176
+ });
177
+
178
+ it("falls back to size-based convert chunking for long no-heading markdown", async () => {
179
+ let successChunkCount = 0;
180
+ convertMock.mockImplementation(async ({ data }) => {
181
+ const content = data.content as string;
182
+ if (content.length > 280) {
183
+ return { code: 999, msg: "content too large" };
184
+ }
185
+ successChunkCount++;
186
+ const blockId = `b_${successChunkCount}`;
187
+ return {
188
+ code: 0,
189
+ data: {
190
+ blocks: [{ block_type: 2, block_id: blockId }],
191
+ first_level_block_ids: [blockId],
192
+ },
193
+ };
194
+ });
195
+
196
+ blockDescendantCreateMock.mockImplementation(async ({ data }) => ({
197
+ code: 0,
198
+ data: {
199
+ children: (data.children_id as string[]).map((id) => ({
200
+ block_id: id,
201
+ })),
202
+ },
203
+ }));
204
+
205
+ const feishuDocTool = resolveFeishuDocTool();
206
+
207
+ const longMarkdown = Array.from(
208
+ { length: 120 },
209
+ (_, i) => `line ${i} with enough content to trigger fallback chunking`,
210
+ ).join("\n");
211
+
212
+ const result = await feishuDocTool.execute("tool-call", {
213
+ action: "append",
214
+ doc_token: "doc_1",
215
+ content: longMarkdown,
216
+ });
217
+
218
+ expect(convertMock.mock.calls.length).toBeGreaterThan(1);
219
+ expect(successChunkCount).toBeGreaterThan(1);
220
+ expect(result.details.blocks_added).toBe(successChunkCount);
221
+ });
222
+
223
+ it("keeps fenced code blocks balanced when size fallback split is needed", async () => {
224
+ const convertedChunks: string[] = [];
225
+ let successChunkCount = 0;
226
+ let failFirstConvert = true;
227
+ convertMock.mockImplementation(async ({ data }) => {
228
+ const content = data.content as string;
229
+ convertedChunks.push(content);
230
+ if (failFirstConvert) {
231
+ failFirstConvert = false;
232
+ return { code: 999, msg: "content too large" };
233
+ }
234
+ successChunkCount++;
235
+ const blockId = `c_${successChunkCount}`;
236
+ return {
237
+ code: 0,
238
+ data: {
239
+ blocks: [{ block_type: 2, block_id: blockId }],
240
+ first_level_block_ids: [blockId],
241
+ },
242
+ };
243
+ });
244
+
245
+ blockChildrenCreateMock.mockImplementation(async ({ data }) => ({
246
+ code: 0,
247
+ data: { children: data.children },
248
+ }));
249
+
250
+ const feishuDocTool = resolveFeishuDocTool();
251
+
252
+ const fencedMarkdown = [
253
+ "## Section",
254
+ "```ts",
255
+ "const alpha = 1;",
256
+ "const beta = 2;",
257
+ "const gamma = alpha + beta;",
258
+ "console.log(gamma);",
259
+ "```",
260
+ "",
261
+ "Tail paragraph one with enough text to exceed API limits when combined. ".repeat(8),
262
+ "Tail paragraph two with enough text to exceed API limits when combined. ".repeat(8),
263
+ "Tail paragraph three with enough text to exceed API limits when combined. ".repeat(8),
264
+ ].join("\n");
265
+
266
+ const result = await feishuDocTool.execute("tool-call", {
267
+ action: "append",
268
+ doc_token: "doc_1",
269
+ content: fencedMarkdown,
270
+ });
271
+
272
+ expect(convertMock.mock.calls.length).toBeGreaterThan(1);
273
+ expect(successChunkCount).toBeGreaterThan(1);
274
+ for (const chunk of convertedChunks) {
275
+ const fenceCount = chunk.match(/```/g)?.length ?? 0;
276
+ expect(fenceCount % 2).toBe(0);
277
+ }
278
+ expect(result.details.blocks_added).toBe(successChunkCount);
279
+ });
280
+
281
+ it("skips image upload when markdown image URL is blocked", async () => {
282
+ const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
283
+ fetchRemoteMediaMock.mockRejectedValueOnce(
284
+ new Error("Blocked: resolves to private/internal IP address"),
285
+ );
286
+
287
+ const feishuDocTool = resolveFeishuDocTool();
109
288
 
110
289
  const result = await feishuDocTool.execute("tool-call", {
111
290
  action: "write",
@@ -120,4 +299,147 @@ describe("feishu_doc image fetch hardening", () => {
120
299
  expect(consoleErrorSpy).toHaveBeenCalled();
121
300
  consoleErrorSpy.mockRestore();
122
301
  });
302
+
303
+ it("create grants permission only to trusted Feishu requester", async () => {
304
+ const feishuDocTool = resolveFeishuDocTool({
305
+ messageChannel: "feishu",
306
+ requesterSenderId: "ou_123",
307
+ });
308
+
309
+ const result = await feishuDocTool.execute("tool-call", {
310
+ action: "create",
311
+ title: "Demo",
312
+ });
313
+
314
+ expect(result.details.document_id).toBe("doc_created");
315
+ expect(result.details.requester_permission_added).toBe(true);
316
+ expect(result.details.requester_open_id).toBe("ou_123");
317
+ expect(result.details.requester_perm_type).toBe("edit");
318
+ expect(permissionMemberCreateMock).toHaveBeenCalledWith(
319
+ expect.objectContaining({
320
+ data: expect.objectContaining({
321
+ member_type: "openid",
322
+ member_id: "ou_123",
323
+ perm: "edit",
324
+ }),
325
+ }),
326
+ );
327
+ });
328
+
329
+ it("create skips requester grant when trusted requester identity is unavailable", async () => {
330
+ const feishuDocTool = resolveFeishuDocTool({
331
+ messageChannel: "feishu",
332
+ });
333
+
334
+ const result = await feishuDocTool.execute("tool-call", {
335
+ action: "create",
336
+ title: "Demo",
337
+ });
338
+
339
+ expect(permissionMemberCreateMock).not.toHaveBeenCalled();
340
+ expect(result.details.requester_permission_added).toBe(false);
341
+ expect(result.details.requester_permission_skipped_reason).toContain("trusted requester");
342
+ });
343
+
344
+ it("create never grants permissions when grant_to_requester is false", async () => {
345
+ const feishuDocTool = resolveFeishuDocTool({
346
+ messageChannel: "feishu",
347
+ requesterSenderId: "ou_123",
348
+ });
349
+
350
+ const result = await feishuDocTool.execute("tool-call", {
351
+ action: "create",
352
+ title: "Demo",
353
+ grant_to_requester: false,
354
+ });
355
+
356
+ expect(permissionMemberCreateMock).not.toHaveBeenCalled();
357
+ expect(result.details.requester_permission_added).toBeUndefined();
358
+ });
359
+
360
+ it("returns an error when create response omits document_id", async () => {
361
+ documentCreateMock.mockResolvedValueOnce({
362
+ code: 0,
363
+ data: { document: { title: "Created Doc" } },
364
+ });
365
+
366
+ const feishuDocTool = resolveFeishuDocTool();
367
+
368
+ const result = await feishuDocTool.execute("tool-call", {
369
+ action: "create",
370
+ title: "Demo",
371
+ });
372
+
373
+ expect(result.details.error).toContain("no document_id");
374
+ });
375
+
376
+ it("uploads local file to doc via upload_file action", async () => {
377
+ blockChildrenCreateMock.mockResolvedValueOnce({
378
+ code: 0,
379
+ data: {
380
+ children: [{ block_type: 23, block_id: "file_block_1" }],
381
+ },
382
+ });
383
+
384
+ const localPath = join(tmpdir(), `feishu-docx-upload-${Date.now()}.txt`);
385
+ await fs.writeFile(localPath, "hello from local file", "utf8");
386
+
387
+ const feishuDocTool = resolveFeishuDocTool();
388
+
389
+ const result = await feishuDocTool.execute("tool-call", {
390
+ action: "upload_file",
391
+ doc_token: "doc_1",
392
+ file_path: localPath,
393
+ filename: "test-local.txt",
394
+ });
395
+
396
+ expect(result.details.success).toBe(true);
397
+ expect(result.details.file_token).toBe("token_1");
398
+ expect(result.details.file_name).toBe("test-local.txt");
399
+
400
+ expect(driveUploadAllMock).toHaveBeenCalledWith(
401
+ expect.objectContaining({
402
+ data: expect.objectContaining({
403
+ parent_type: "docx_file",
404
+ parent_node: "doc_1",
405
+ file_name: "test-local.txt",
406
+ }),
407
+ }),
408
+ );
409
+
410
+ await fs.unlink(localPath);
411
+ });
412
+
413
+ it("returns an error when upload_file cannot list placeholder siblings", async () => {
414
+ blockChildrenCreateMock.mockResolvedValueOnce({
415
+ code: 0,
416
+ data: {
417
+ children: [{ block_type: 23, block_id: "file_block_1" }],
418
+ },
419
+ });
420
+ blockChildrenGetMock.mockResolvedValueOnce({
421
+ code: 999,
422
+ msg: "list failed",
423
+ data: { items: [] },
424
+ });
425
+
426
+ const localPath = join(tmpdir(), `feishu-docx-upload-fail-${Date.now()}.txt`);
427
+ await fs.writeFile(localPath, "hello from local file", "utf8");
428
+
429
+ try {
430
+ const feishuDocTool = resolveFeishuDocTool();
431
+
432
+ const result = await feishuDocTool.execute("tool-call", {
433
+ action: "upload_file",
434
+ doc_token: "doc_1",
435
+ file_path: localPath,
436
+ filename: "test-local.txt",
437
+ });
438
+
439
+ expect(result.details.error).toBe("list failed");
440
+ expect(driveUploadAllMock).not.toHaveBeenCalled();
441
+ } finally {
442
+ await fs.unlink(localPath);
443
+ }
444
+ });
123
445
  });