@milerliu/feishu 0.1.5
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.
- package/LICENSE +21 -0
- package/README.md +363 -0
- package/index.ts +55 -0
- package/openclaw.plugin.json +10 -0
- package/package.json +61 -0
- package/src/accounts.ts +53 -0
- package/src/bot.ts +685 -0
- package/src/channel.ts +224 -0
- package/src/client.ts +66 -0
- package/src/config-schema.ts +107 -0
- package/src/directory.ts +159 -0
- package/src/docx.ts +711 -0
- package/src/media.ts +515 -0
- package/src/mention.ts +121 -0
- package/src/monitor.ts +151 -0
- package/src/onboarding.ts +358 -0
- package/src/outbound.ts +40 -0
- package/src/policy.ts +92 -0
- package/src/probe.ts +46 -0
- package/src/reactions.ts +157 -0
- package/src/reply-dispatcher.ts +179 -0
- package/src/runtime.ts +14 -0
- package/src/send.ts +475 -0
- package/src/targets.ts +58 -0
- package/src/types.ts +55 -0
- package/src/typing.ts +73 -0
package/src/docx.ts
ADDED
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
|
3
|
+
import { createFeishuClient } from "./client.js";
|
|
4
|
+
import type { FeishuConfig } from "./types.js";
|
|
5
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
6
|
+
import { Readable } from "stream";
|
|
7
|
+
|
|
8
|
+
// ============ Helpers ============
|
|
9
|
+
|
|
10
|
+
function json(data: unknown) {
|
|
11
|
+
return {
|
|
12
|
+
content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }],
|
|
13
|
+
details: data,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function extractBlockPreview(block: any): string {
|
|
18
|
+
const elements =
|
|
19
|
+
block.text?.elements ??
|
|
20
|
+
block.heading1?.elements ??
|
|
21
|
+
block.heading2?.elements ??
|
|
22
|
+
block.heading3?.elements ??
|
|
23
|
+
[];
|
|
24
|
+
return elements
|
|
25
|
+
.filter((e: any) => e.text_run)
|
|
26
|
+
.map((e: any) => e.text_run.content)
|
|
27
|
+
.join("")
|
|
28
|
+
.slice(0, 50);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Extract image URLs from markdown content */
|
|
32
|
+
function extractImageUrls(markdown: string): string[] {
|
|
33
|
+
const regex = /!\[[^\]]*\]\(([^)]+)\)/g;
|
|
34
|
+
const urls: string[] = [];
|
|
35
|
+
let match;
|
|
36
|
+
while ((match = regex.exec(markdown)) !== null) {
|
|
37
|
+
const url = match[1].trim();
|
|
38
|
+
// Only collect http(s) URLs, not file paths
|
|
39
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
40
|
+
urls.push(url);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return urls;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const BLOCK_TYPE_NAMES: Record<number, string> = {
|
|
47
|
+
1: "Page",
|
|
48
|
+
2: "Text",
|
|
49
|
+
3: "Heading1",
|
|
50
|
+
4: "Heading2",
|
|
51
|
+
5: "Heading3",
|
|
52
|
+
12: "Bullet",
|
|
53
|
+
13: "Ordered",
|
|
54
|
+
14: "Code",
|
|
55
|
+
15: "Quote",
|
|
56
|
+
17: "Todo",
|
|
57
|
+
18: "Bitable",
|
|
58
|
+
21: "Diagram",
|
|
59
|
+
22: "Divider",
|
|
60
|
+
23: "File",
|
|
61
|
+
27: "Image",
|
|
62
|
+
30: "Sheet",
|
|
63
|
+
31: "Table",
|
|
64
|
+
32: "TableCell",
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Block types that cannot be created via documentBlockChildren.create API
|
|
68
|
+
const UNSUPPORTED_CREATE_TYPES = new Set([
|
|
69
|
+
31, // Table - must use different API or workaround
|
|
70
|
+
32, // TableCell - child of Table
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
/** Clean blocks for insertion (remove unsupported types and read-only fields) */
|
|
74
|
+
function cleanBlocksForInsert(blocks: any[]): { cleaned: any[]; skipped: string[] } {
|
|
75
|
+
const skipped: string[] = [];
|
|
76
|
+
const cleaned = blocks
|
|
77
|
+
.filter((block) => {
|
|
78
|
+
if (UNSUPPORTED_CREATE_TYPES.has(block.block_type)) {
|
|
79
|
+
const typeName = BLOCK_TYPE_NAMES[block.block_type] || `type_${block.block_type}`;
|
|
80
|
+
skipped.push(typeName);
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
})
|
|
85
|
+
.map((block) => {
|
|
86
|
+
// Remove any read-only fields that might slip through
|
|
87
|
+
if (block.block_type === 31 && block.table?.merge_info) {
|
|
88
|
+
const { merge_info, ...tableRest } = block.table;
|
|
89
|
+
return { ...block, table: tableRest };
|
|
90
|
+
}
|
|
91
|
+
return block;
|
|
92
|
+
});
|
|
93
|
+
return { cleaned, skipped };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ============ Core Functions ============
|
|
97
|
+
|
|
98
|
+
/** Convert markdown to Feishu blocks using the Convert API */
|
|
99
|
+
async function convertMarkdown(client: Lark.Client, markdown: string) {
|
|
100
|
+
const res = await client.docx.document.convert({
|
|
101
|
+
data: { content_type: "markdown", content: markdown },
|
|
102
|
+
});
|
|
103
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
104
|
+
return {
|
|
105
|
+
blocks: res.data?.blocks ?? [],
|
|
106
|
+
firstLevelBlockIds: res.data?.first_level_block_ids ?? [],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Insert blocks as children of a parent block */
|
|
111
|
+
async function insertBlocks(
|
|
112
|
+
client: Lark.Client,
|
|
113
|
+
docToken: string,
|
|
114
|
+
blocks: any[],
|
|
115
|
+
parentBlockId?: string,
|
|
116
|
+
): Promise<{ children: any[]; skipped: string[] }> {
|
|
117
|
+
const { cleaned, skipped } = cleanBlocksForInsert(blocks);
|
|
118
|
+
const blockId = parentBlockId ?? docToken;
|
|
119
|
+
|
|
120
|
+
if (cleaned.length === 0) {
|
|
121
|
+
return { children: [], skipped };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const res = await client.docx.documentBlockChildren.create({
|
|
125
|
+
path: { document_id: docToken, block_id: blockId },
|
|
126
|
+
data: { children: cleaned },
|
|
127
|
+
});
|
|
128
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
129
|
+
return { children: res.data?.children ?? [], skipped };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Delete all child blocks from a parent */
|
|
133
|
+
async function clearDocumentContent(client: Lark.Client, docToken: string) {
|
|
134
|
+
const existing = await client.docx.documentBlock.list({
|
|
135
|
+
path: { document_id: docToken },
|
|
136
|
+
});
|
|
137
|
+
if (existing.code !== 0) throw new Error(existing.msg);
|
|
138
|
+
|
|
139
|
+
const childIds =
|
|
140
|
+
existing.data?.items
|
|
141
|
+
?.filter((b) => b.parent_id === docToken && b.block_type !== 1)
|
|
142
|
+
.map((b) => b.block_id) ?? [];
|
|
143
|
+
|
|
144
|
+
if (childIds.length > 0) {
|
|
145
|
+
const res = await client.docx.documentBlockChildren.batchDelete({
|
|
146
|
+
path: { document_id: docToken, block_id: docToken },
|
|
147
|
+
data: { start_index: 0, end_index: childIds.length },
|
|
148
|
+
});
|
|
149
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return childIds.length;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Upload image to Feishu drive for docx */
|
|
156
|
+
async function uploadImageToDocx(
|
|
157
|
+
client: Lark.Client,
|
|
158
|
+
blockId: string,
|
|
159
|
+
imageBuffer: Buffer,
|
|
160
|
+
fileName: string,
|
|
161
|
+
): Promise<string> {
|
|
162
|
+
const res = await client.drive.media.uploadAll({
|
|
163
|
+
data: {
|
|
164
|
+
file_name: fileName,
|
|
165
|
+
parent_type: "docx_image",
|
|
166
|
+
parent_node: blockId,
|
|
167
|
+
size: imageBuffer.length,
|
|
168
|
+
file: Readable.from(imageBuffer) as any,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const fileToken = res?.file_token;
|
|
173
|
+
if (!fileToken) {
|
|
174
|
+
throw new Error("Image upload failed: no file_token returned");
|
|
175
|
+
}
|
|
176
|
+
return fileToken;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Download image from URL */
|
|
180
|
+
async function downloadImage(url: string): Promise<Buffer> {
|
|
181
|
+
const response = await fetch(url);
|
|
182
|
+
if (!response.ok) {
|
|
183
|
+
throw new Error(`Failed to download image: ${response.status} ${response.statusText}`);
|
|
184
|
+
}
|
|
185
|
+
return Buffer.from(await response.arrayBuffer());
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Process images in markdown: download from URL, upload to Feishu, update blocks */
|
|
189
|
+
async function processImages(
|
|
190
|
+
client: Lark.Client,
|
|
191
|
+
docToken: string,
|
|
192
|
+
markdown: string,
|
|
193
|
+
insertedBlocks: any[],
|
|
194
|
+
): Promise<number> {
|
|
195
|
+
const imageUrls = extractImageUrls(markdown);
|
|
196
|
+
if (imageUrls.length === 0) return 0;
|
|
197
|
+
|
|
198
|
+
// Find Image blocks (block_type 27)
|
|
199
|
+
const imageBlocks = insertedBlocks.filter((b) => b.block_type === 27);
|
|
200
|
+
|
|
201
|
+
let processed = 0;
|
|
202
|
+
for (let i = 0; i < Math.min(imageUrls.length, imageBlocks.length); i++) {
|
|
203
|
+
const url = imageUrls[i];
|
|
204
|
+
const blockId = imageBlocks[i].block_id;
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
// Download image from URL
|
|
208
|
+
const buffer = await downloadImage(url);
|
|
209
|
+
|
|
210
|
+
// Generate filename from URL
|
|
211
|
+
const urlPath = new URL(url).pathname;
|
|
212
|
+
const fileName = urlPath.split("/").pop() || `image_${i}.png`;
|
|
213
|
+
|
|
214
|
+
// Upload to Feishu
|
|
215
|
+
const fileToken = await uploadImageToDocx(client, blockId, buffer, fileName);
|
|
216
|
+
|
|
217
|
+
// Update the image block
|
|
218
|
+
await client.docx.documentBlock.patch({
|
|
219
|
+
path: { document_id: docToken, block_id: blockId },
|
|
220
|
+
data: {
|
|
221
|
+
replace_image: { token: fileToken },
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
processed++;
|
|
226
|
+
} catch (err) {
|
|
227
|
+
// Log but continue processing other images
|
|
228
|
+
console.error(`Failed to process image ${url}:`, err);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return processed;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ============ Actions ============
|
|
236
|
+
|
|
237
|
+
// Block types that are NOT included in rawContent (plain text) output
|
|
238
|
+
const STRUCTURED_BLOCK_TYPES = new Set([14, 18, 21, 23, 27, 30, 31, 32]);
|
|
239
|
+
// 14=Code, 18=Bitable, 21=Diagram, 23=File, 27=Image, 30=Sheet, 31=Table, 32=TableCell
|
|
240
|
+
|
|
241
|
+
async function readDoc(client: Lark.Client, docToken: string) {
|
|
242
|
+
const [contentRes, infoRes, blocksRes] = await Promise.all([
|
|
243
|
+
client.docx.document.rawContent({ path: { document_id: docToken } }),
|
|
244
|
+
client.docx.document.get({ path: { document_id: docToken } }),
|
|
245
|
+
client.docx.documentBlock.list({ path: { document_id: docToken } }),
|
|
246
|
+
]);
|
|
247
|
+
|
|
248
|
+
if (contentRes.code !== 0) throw new Error(contentRes.msg);
|
|
249
|
+
|
|
250
|
+
const blocks = blocksRes.data?.items ?? [];
|
|
251
|
+
const blockCounts: Record<string, number> = {};
|
|
252
|
+
const structuredTypes: string[] = [];
|
|
253
|
+
|
|
254
|
+
for (const b of blocks) {
|
|
255
|
+
const type = b.block_type ?? 0;
|
|
256
|
+
const name = BLOCK_TYPE_NAMES[type] || `type_${type}`;
|
|
257
|
+
blockCounts[name] = (blockCounts[name] || 0) + 1;
|
|
258
|
+
|
|
259
|
+
// Track structured types that need list_blocks to read
|
|
260
|
+
if (STRUCTURED_BLOCK_TYPES.has(type) && !structuredTypes.includes(name)) {
|
|
261
|
+
structuredTypes.push(name);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Build hint if there are structured blocks
|
|
266
|
+
let hint: string | undefined;
|
|
267
|
+
if (structuredTypes.length > 0) {
|
|
268
|
+
hint = `This document contains ${structuredTypes.join(", ")} which are NOT included in the plain text above. Use feishu_doc_list_blocks to get full content.`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
title: infoRes.data?.document?.title,
|
|
273
|
+
content: contentRes.data?.content,
|
|
274
|
+
revision_id: infoRes.data?.document?.revision_id,
|
|
275
|
+
block_count: blocks.length,
|
|
276
|
+
block_types: blockCounts,
|
|
277
|
+
...(hint && { hint }),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async function createDoc(client: Lark.Client, title: string, folderToken?: string) {
|
|
282
|
+
const res = await client.docx.document.create({
|
|
283
|
+
data: { title, folder_token: folderToken },
|
|
284
|
+
});
|
|
285
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
286
|
+
const doc = res.data?.document;
|
|
287
|
+
return {
|
|
288
|
+
document_id: doc?.document_id,
|
|
289
|
+
title: doc?.title,
|
|
290
|
+
url: `https://feishu.cn/docx/${doc?.document_id}`,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async function writeDoc(client: Lark.Client, docToken: string, markdown: string) {
|
|
295
|
+
// 1. Clear existing content
|
|
296
|
+
const deleted = await clearDocumentContent(client, docToken);
|
|
297
|
+
|
|
298
|
+
// 2. Convert markdown to blocks
|
|
299
|
+
const { blocks } = await convertMarkdown(client, markdown);
|
|
300
|
+
if (blocks.length === 0) {
|
|
301
|
+
return { success: true, blocks_deleted: deleted, blocks_added: 0, images_processed: 0 };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// 3. Insert new blocks (unsupported types like Table are filtered)
|
|
305
|
+
const { children: inserted, skipped } = await insertBlocks(client, docToken, blocks);
|
|
306
|
+
|
|
307
|
+
// 4. Process images
|
|
308
|
+
const imagesProcessed = await processImages(client, docToken, markdown, inserted);
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
success: true,
|
|
312
|
+
blocks_deleted: deleted,
|
|
313
|
+
blocks_added: inserted.length,
|
|
314
|
+
images_processed: imagesProcessed,
|
|
315
|
+
...(skipped.length > 0 && {
|
|
316
|
+
warning: `Skipped unsupported block types: ${skipped.join(", ")}. Tables are not supported via this API.`,
|
|
317
|
+
}),
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async function appendDoc(client: Lark.Client, docToken: string, markdown: string) {
|
|
322
|
+
// 1. Convert markdown to blocks
|
|
323
|
+
const { blocks } = await convertMarkdown(client, markdown);
|
|
324
|
+
if (blocks.length === 0) {
|
|
325
|
+
throw new Error("Content is empty");
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// 2. Insert blocks (unsupported types like Table are filtered)
|
|
329
|
+
const { children: inserted, skipped } = await insertBlocks(client, docToken, blocks);
|
|
330
|
+
|
|
331
|
+
// 3. Process images
|
|
332
|
+
const imagesProcessed = await processImages(client, docToken, markdown, inserted);
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
success: true,
|
|
336
|
+
blocks_added: inserted.length,
|
|
337
|
+
images_processed: imagesProcessed,
|
|
338
|
+
block_ids: inserted.map((b: any) => b.block_id),
|
|
339
|
+
...(skipped.length > 0 && {
|
|
340
|
+
warning: `Skipped unsupported block types: ${skipped.join(", ")}. Tables are not supported via this API.`,
|
|
341
|
+
}),
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async function updateBlock(
|
|
346
|
+
client: Lark.Client,
|
|
347
|
+
docToken: string,
|
|
348
|
+
blockId: string,
|
|
349
|
+
content: string,
|
|
350
|
+
) {
|
|
351
|
+
const blockInfo = await client.docx.documentBlock.get({
|
|
352
|
+
path: { document_id: docToken, block_id: blockId },
|
|
353
|
+
});
|
|
354
|
+
if (blockInfo.code !== 0) throw new Error(blockInfo.msg);
|
|
355
|
+
|
|
356
|
+
const res = await client.docx.documentBlock.patch({
|
|
357
|
+
path: { document_id: docToken, block_id: blockId },
|
|
358
|
+
data: {
|
|
359
|
+
update_text_elements: {
|
|
360
|
+
elements: [{ text_run: { content } }],
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
365
|
+
|
|
366
|
+
return { success: true, block_id: blockId };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async function deleteBlock(client: Lark.Client, docToken: string, blockId: string) {
|
|
370
|
+
const blockInfo = await client.docx.documentBlock.get({
|
|
371
|
+
path: { document_id: docToken, block_id: blockId },
|
|
372
|
+
});
|
|
373
|
+
if (blockInfo.code !== 0) throw new Error(blockInfo.msg);
|
|
374
|
+
|
|
375
|
+
const parentId = blockInfo.data?.block?.parent_id ?? docToken;
|
|
376
|
+
|
|
377
|
+
const children = await client.docx.documentBlockChildren.get({
|
|
378
|
+
path: { document_id: docToken, block_id: parentId },
|
|
379
|
+
});
|
|
380
|
+
if (children.code !== 0) throw new Error(children.msg);
|
|
381
|
+
|
|
382
|
+
const items = children.data?.items ?? [];
|
|
383
|
+
const index = items.findIndex((item: any) => item.block_id === blockId);
|
|
384
|
+
if (index === -1) throw new Error("Block not found");
|
|
385
|
+
|
|
386
|
+
const res = await client.docx.documentBlockChildren.batchDelete({
|
|
387
|
+
path: { document_id: docToken, block_id: parentId },
|
|
388
|
+
data: { start_index: index, end_index: index + 1 },
|
|
389
|
+
});
|
|
390
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
391
|
+
|
|
392
|
+
return { success: true, deleted_block_id: blockId };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async function listBlocks(client: Lark.Client, docToken: string) {
|
|
396
|
+
const res = await client.docx.documentBlock.list({
|
|
397
|
+
path: { document_id: docToken },
|
|
398
|
+
});
|
|
399
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
400
|
+
|
|
401
|
+
// Return full block data for agent to parse
|
|
402
|
+
return {
|
|
403
|
+
blocks: res.data?.items ?? [],
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async function getBlock(client: Lark.Client, docToken: string, blockId: string) {
|
|
408
|
+
const res = await client.docx.documentBlock.get({
|
|
409
|
+
path: { document_id: docToken, block_id: blockId },
|
|
410
|
+
});
|
|
411
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
412
|
+
|
|
413
|
+
return {
|
|
414
|
+
block: res.data?.block,
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
async function listFolder(client: Lark.Client, folderToken: string) {
|
|
419
|
+
const res = await client.drive.file.list({
|
|
420
|
+
params: { folder_token: folderToken },
|
|
421
|
+
});
|
|
422
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
423
|
+
|
|
424
|
+
return {
|
|
425
|
+
files: res.data?.files?.map((f) => ({
|
|
426
|
+
token: f.token,
|
|
427
|
+
name: f.name,
|
|
428
|
+
type: f.type,
|
|
429
|
+
url: f.url,
|
|
430
|
+
})),
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
async function listAppScopes(client: Lark.Client) {
|
|
435
|
+
const res = await client.application.scope.list({});
|
|
436
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
437
|
+
|
|
438
|
+
const scopes = res.data?.scopes ?? [];
|
|
439
|
+
const granted = scopes.filter((s) => s.grant_status === 1);
|
|
440
|
+
const pending = scopes.filter((s) => s.grant_status !== 1);
|
|
441
|
+
|
|
442
|
+
return {
|
|
443
|
+
granted: granted.map((s) => ({ name: s.scope_name, type: s.scope_type })),
|
|
444
|
+
pending: pending.map((s) => ({ name: s.scope_name, type: s.scope_type })),
|
|
445
|
+
summary: `${granted.length} granted, ${pending.length} pending`,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// ============ Schemas ============
|
|
450
|
+
|
|
451
|
+
const DocTokenSchema = Type.Object({
|
|
452
|
+
doc_token: Type.String({ description: "Document token (extract from URL /docx/XXX)" }),
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
const CreateDocSchema = Type.Object({
|
|
456
|
+
title: Type.String({ description: "Document title" }),
|
|
457
|
+
folder_token: Type.Optional(Type.String({ description: "Target folder token (optional)" })),
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
const WriteDocSchema = Type.Object({
|
|
461
|
+
doc_token: Type.String({ description: "Document token" }),
|
|
462
|
+
content: Type.String({
|
|
463
|
+
description: "Markdown content to write (replaces entire document content)",
|
|
464
|
+
}),
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
const AppendDocSchema = Type.Object({
|
|
468
|
+
doc_token: Type.String({ description: "Document token" }),
|
|
469
|
+
content: Type.String({ description: "Markdown content to append to end of document" }),
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
const UpdateBlockSchema = Type.Object({
|
|
473
|
+
doc_token: Type.String({ description: "Document token" }),
|
|
474
|
+
block_id: Type.String({ description: "Block ID (get from list_blocks)" }),
|
|
475
|
+
content: Type.String({ description: "New text content" }),
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
const DeleteBlockSchema = Type.Object({
|
|
479
|
+
doc_token: Type.String({ description: "Document token" }),
|
|
480
|
+
block_id: Type.String({ description: "Block ID" }),
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
const GetBlockSchema = Type.Object({
|
|
484
|
+
doc_token: Type.String({ description: "Document token" }),
|
|
485
|
+
block_id: Type.String({ description: "Block ID (from list_blocks)" }),
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
const FolderTokenSchema = Type.Object({
|
|
489
|
+
folder_token: Type.String({ description: "Folder token" }),
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
// ============ Tool Registration ============
|
|
493
|
+
|
|
494
|
+
export function registerFeishuDocTools(api: OpenClawPluginApi) {
|
|
495
|
+
const feishuCfg = api.config?.channels?.feishu as FeishuConfig | undefined;
|
|
496
|
+
if (!feishuCfg?.appId || !feishuCfg?.appSecret) {
|
|
497
|
+
api.logger.debug?.("feishu_doc: Feishu credentials not configured, skipping doc tools");
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const getClient = () => createFeishuClient(feishuCfg);
|
|
502
|
+
|
|
503
|
+
// Tool 1: feishu_doc_read
|
|
504
|
+
api.registerTool(
|
|
505
|
+
{
|
|
506
|
+
name: "feishu_doc_read",
|
|
507
|
+
label: "Feishu Doc Read",
|
|
508
|
+
description: "Read plain text content and metadata from a Feishu document",
|
|
509
|
+
parameters: DocTokenSchema,
|
|
510
|
+
async execute(_toolCallId, params) {
|
|
511
|
+
const { doc_token } = params as { doc_token: string };
|
|
512
|
+
try {
|
|
513
|
+
const result = await readDoc(getClient(), doc_token);
|
|
514
|
+
return json(result);
|
|
515
|
+
} catch (err) {
|
|
516
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
517
|
+
}
|
|
518
|
+
},
|
|
519
|
+
},
|
|
520
|
+
{ name: "feishu_doc_read" },
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
// Tool 2: feishu_doc_create
|
|
524
|
+
api.registerTool(
|
|
525
|
+
{
|
|
526
|
+
name: "feishu_doc_create",
|
|
527
|
+
label: "Feishu Doc Create",
|
|
528
|
+
description: "Create a new empty Feishu document",
|
|
529
|
+
parameters: CreateDocSchema,
|
|
530
|
+
async execute(_toolCallId, params) {
|
|
531
|
+
const { title, folder_token } = params as { title: string; folder_token?: string };
|
|
532
|
+
try {
|
|
533
|
+
const result = await createDoc(getClient(), title, folder_token);
|
|
534
|
+
return json(result);
|
|
535
|
+
} catch (err) {
|
|
536
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
537
|
+
}
|
|
538
|
+
},
|
|
539
|
+
},
|
|
540
|
+
{ name: "feishu_doc_create" },
|
|
541
|
+
);
|
|
542
|
+
|
|
543
|
+
// Tool 3: feishu_doc_write (NEW)
|
|
544
|
+
api.registerTool(
|
|
545
|
+
{
|
|
546
|
+
name: "feishu_doc_write",
|
|
547
|
+
label: "Feishu Doc Write",
|
|
548
|
+
description:
|
|
549
|
+
"Write markdown content to a Feishu document (replaces all content). Supports headings, lists, code blocks, quotes, links, images, and text styling. Note: tables are not supported.",
|
|
550
|
+
parameters: WriteDocSchema,
|
|
551
|
+
async execute(_toolCallId, params) {
|
|
552
|
+
const { doc_token, content } = params as { doc_token: string; content: string };
|
|
553
|
+
try {
|
|
554
|
+
const result = await writeDoc(getClient(), doc_token, content);
|
|
555
|
+
return json(result);
|
|
556
|
+
} catch (err) {
|
|
557
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
},
|
|
561
|
+
{ name: "feishu_doc_write" },
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
// Tool 4: feishu_doc_append
|
|
565
|
+
api.registerTool(
|
|
566
|
+
{
|
|
567
|
+
name: "feishu_doc_append",
|
|
568
|
+
label: "Feishu Doc Append",
|
|
569
|
+
description:
|
|
570
|
+
"Append markdown content to the end of a Feishu document. Supports same markdown syntax as write.",
|
|
571
|
+
parameters: AppendDocSchema,
|
|
572
|
+
async execute(_toolCallId, params) {
|
|
573
|
+
const { doc_token, content } = params as { doc_token: string; content: string };
|
|
574
|
+
try {
|
|
575
|
+
const result = await appendDoc(getClient(), doc_token, content);
|
|
576
|
+
return json(result);
|
|
577
|
+
} catch (err) {
|
|
578
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
579
|
+
}
|
|
580
|
+
},
|
|
581
|
+
},
|
|
582
|
+
{ name: "feishu_doc_append" },
|
|
583
|
+
);
|
|
584
|
+
|
|
585
|
+
// Tool 5: feishu_doc_update_block
|
|
586
|
+
api.registerTool(
|
|
587
|
+
{
|
|
588
|
+
name: "feishu_doc_update_block",
|
|
589
|
+
label: "Feishu Doc Update Block",
|
|
590
|
+
description: "Update the text content of a specific block in a Feishu document",
|
|
591
|
+
parameters: UpdateBlockSchema,
|
|
592
|
+
async execute(_toolCallId, params) {
|
|
593
|
+
const { doc_token, block_id, content } = params as {
|
|
594
|
+
doc_token: string;
|
|
595
|
+
block_id: string;
|
|
596
|
+
content: string;
|
|
597
|
+
};
|
|
598
|
+
try {
|
|
599
|
+
const result = await updateBlock(getClient(), doc_token, block_id, content);
|
|
600
|
+
return json(result);
|
|
601
|
+
} catch (err) {
|
|
602
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
},
|
|
606
|
+
{ name: "feishu_doc_update_block" },
|
|
607
|
+
);
|
|
608
|
+
|
|
609
|
+
// Tool 6: feishu_doc_delete_block
|
|
610
|
+
api.registerTool(
|
|
611
|
+
{
|
|
612
|
+
name: "feishu_doc_delete_block",
|
|
613
|
+
label: "Feishu Doc Delete Block",
|
|
614
|
+
description: "Delete a specific block from a Feishu document",
|
|
615
|
+
parameters: DeleteBlockSchema,
|
|
616
|
+
async execute(_toolCallId, params) {
|
|
617
|
+
const { doc_token, block_id } = params as { doc_token: string; block_id: string };
|
|
618
|
+
try {
|
|
619
|
+
const result = await deleteBlock(getClient(), doc_token, block_id);
|
|
620
|
+
return json(result);
|
|
621
|
+
} catch (err) {
|
|
622
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
623
|
+
}
|
|
624
|
+
},
|
|
625
|
+
},
|
|
626
|
+
{ name: "feishu_doc_delete_block" },
|
|
627
|
+
);
|
|
628
|
+
|
|
629
|
+
// Tool 7: feishu_doc_list_blocks
|
|
630
|
+
api.registerTool(
|
|
631
|
+
{
|
|
632
|
+
name: "feishu_doc_list_blocks",
|
|
633
|
+
label: "Feishu Doc List Blocks",
|
|
634
|
+
description:
|
|
635
|
+
"List all blocks in a Feishu document with full content. Use this to read structured content like tables. Returns block_id for use with update/delete/get_block.",
|
|
636
|
+
parameters: DocTokenSchema,
|
|
637
|
+
async execute(_toolCallId, params) {
|
|
638
|
+
const { doc_token } = params as { doc_token: string };
|
|
639
|
+
try {
|
|
640
|
+
const result = await listBlocks(getClient(), doc_token);
|
|
641
|
+
return json(result);
|
|
642
|
+
} catch (err) {
|
|
643
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
},
|
|
647
|
+
{ name: "feishu_doc_list_blocks" },
|
|
648
|
+
);
|
|
649
|
+
|
|
650
|
+
// Tool 8: feishu_doc_get_block
|
|
651
|
+
api.registerTool(
|
|
652
|
+
{
|
|
653
|
+
name: "feishu_doc_get_block",
|
|
654
|
+
label: "Feishu Doc Get Block",
|
|
655
|
+
description: "Get detailed content of a specific block by ID (from list_blocks)",
|
|
656
|
+
parameters: GetBlockSchema,
|
|
657
|
+
async execute(_toolCallId, params) {
|
|
658
|
+
const { doc_token, block_id } = params as { doc_token: string; block_id: string };
|
|
659
|
+
try {
|
|
660
|
+
const result = await getBlock(getClient(), doc_token, block_id);
|
|
661
|
+
return json(result);
|
|
662
|
+
} catch (err) {
|
|
663
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
664
|
+
}
|
|
665
|
+
},
|
|
666
|
+
},
|
|
667
|
+
{ name: "feishu_doc_get_block" },
|
|
668
|
+
);
|
|
669
|
+
|
|
670
|
+
// Tool 9: feishu_folder_list
|
|
671
|
+
api.registerTool(
|
|
672
|
+
{
|
|
673
|
+
name: "feishu_folder_list",
|
|
674
|
+
label: "Feishu Folder List",
|
|
675
|
+
description: "List documents and subfolders in a Feishu folder",
|
|
676
|
+
parameters: FolderTokenSchema,
|
|
677
|
+
async execute(_toolCallId, params) {
|
|
678
|
+
const { folder_token } = params as { folder_token: string };
|
|
679
|
+
try {
|
|
680
|
+
const result = await listFolder(getClient(), folder_token);
|
|
681
|
+
return json(result);
|
|
682
|
+
} catch (err) {
|
|
683
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
684
|
+
}
|
|
685
|
+
},
|
|
686
|
+
},
|
|
687
|
+
{ name: "feishu_folder_list" },
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
// Tool 10: feishu_app_scopes
|
|
691
|
+
api.registerTool(
|
|
692
|
+
{
|
|
693
|
+
name: "feishu_app_scopes",
|
|
694
|
+
label: "Feishu App Scopes",
|
|
695
|
+
description:
|
|
696
|
+
"List current app permissions (scopes). Use to debug permission issues or check available capabilities.",
|
|
697
|
+
parameters: Type.Object({}),
|
|
698
|
+
async execute() {
|
|
699
|
+
try {
|
|
700
|
+
const result = await listAppScopes(getClient());
|
|
701
|
+
return json(result);
|
|
702
|
+
} catch (err) {
|
|
703
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
704
|
+
}
|
|
705
|
+
},
|
|
706
|
+
},
|
|
707
|
+
{ name: "feishu_app_scopes" },
|
|
708
|
+
);
|
|
709
|
+
|
|
710
|
+
api.logger.info?.(`feishu_doc: Registered 10 document tools`);
|
|
711
|
+
}
|