@openclaw-plugins/feishu-plus 0.1.7-fork.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.
- package/LICENSE +21 -0
- package/README.md +560 -0
- package/index.ts +74 -0
- package/openclaw.plugin.json +10 -0
- package/package.json +65 -0
- package/skills/feishu-doc/SKILL.md +99 -0
- package/skills/feishu-doc/references/block-types.md +102 -0
- package/skills/feishu-drive/SKILL.md +96 -0
- package/skills/feishu-perm/SKILL.md +90 -0
- package/skills/feishu-wiki/SKILL.md +96 -0
- package/src/accounts.ts +140 -0
- package/src/bitable.ts +441 -0
- package/src/bot.ts +919 -0
- package/src/channel.ts +335 -0
- package/src/client.ts +114 -0
- package/src/config-schema.ts +199 -0
- package/src/directory.ts +165 -0
- package/src/doc-schema.ts +47 -0
- package/src/docx.ts +525 -0
- package/src/drive-schema.ts +46 -0
- package/src/drive.ts +207 -0
- package/src/dynamic-agent.ts +131 -0
- package/src/media.ts +523 -0
- package/src/mention.ts +121 -0
- package/src/monitor.ts +190 -0
- package/src/onboarding.ts +358 -0
- package/src/outbound.ts +40 -0
- package/src/perm-schema.ts +52 -0
- package/src/perm.ts +166 -0
- package/src/policy.ts +92 -0
- package/src/probe.ts +115 -0
- package/src/reactions.ts +160 -0
- package/src/reply-dispatcher.ts +225 -0
- package/src/runtime.ts +14 -0
- package/src/send.ts +492 -0
- package/src/stream.ts +160 -0
- package/src/targets.ts +58 -0
- package/src/tools-config.ts +21 -0
- package/src/types.ts +77 -0
- package/src/typing.ts +75 -0
- package/src/wiki-schema.ts +55 -0
- package/src/wiki.ts +224 -0
package/src/docx.ts
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
|
3
|
+
import { createFeishuClient } from "./client.js";
|
|
4
|
+
import { resolveFeishuAccount, listEnabledFeishuAccounts } from "./accounts.js";
|
|
5
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
6
|
+
import { Readable } from "stream";
|
|
7
|
+
import { FeishuDocSchema, type FeishuDocParams } from "./doc-schema.js";
|
|
8
|
+
import { resolveToolsConfig } from "./tools-config.js";
|
|
9
|
+
|
|
10
|
+
// ============ Helpers ============
|
|
11
|
+
|
|
12
|
+
function json(data: unknown) {
|
|
13
|
+
return {
|
|
14
|
+
content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }],
|
|
15
|
+
details: data,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Reorder blocks based on firstLevelBlockIds from convertMarkdown API.
|
|
21
|
+
* The convertMarkdown API returns blocks as an unordered map, but provides
|
|
22
|
+
* firstLevelBlockIds to indicate the correct document order.
|
|
23
|
+
*/
|
|
24
|
+
function reorderBlocks(blocks: any[], firstLevelBlockIds: string[]): any[] {
|
|
25
|
+
if (!firstLevelBlockIds || firstLevelBlockIds.length === 0) {
|
|
26
|
+
return blocks;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Create a map of block_id to block for quick lookup
|
|
30
|
+
const blockMap = new Map<string, any>();
|
|
31
|
+
for (const block of blocks) {
|
|
32
|
+
if (block.block_id) {
|
|
33
|
+
blockMap.set(block.block_id, block);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Reorder blocks according to firstLevelBlockIds
|
|
38
|
+
const ordered: any[] = [];
|
|
39
|
+
for (const blockId of firstLevelBlockIds) {
|
|
40
|
+
const block = blockMap.get(blockId);
|
|
41
|
+
if (block) {
|
|
42
|
+
ordered.push(block);
|
|
43
|
+
blockMap.delete(blockId);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Append any remaining blocks that weren't in the order list
|
|
48
|
+
// (defensive: shouldn't happen with correct API response)
|
|
49
|
+
for (const block of blocks) {
|
|
50
|
+
if (blockMap.has(block.block_id)) {
|
|
51
|
+
ordered.push(block);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return ordered;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Extract image URLs from markdown content */
|
|
59
|
+
function extractImageUrls(markdown: string): string[] {
|
|
60
|
+
const regex = /!\[[^\]]*\]\(([^)]+)\)/g;
|
|
61
|
+
const urls: string[] = [];
|
|
62
|
+
let match;
|
|
63
|
+
while ((match = regex.exec(markdown)) !== null) {
|
|
64
|
+
const url = match[1].trim();
|
|
65
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
66
|
+
urls.push(url);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return urls;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const BLOCK_TYPE_NAMES: Record<number, string> = {
|
|
73
|
+
1: "Page",
|
|
74
|
+
2: "Text",
|
|
75
|
+
3: "Heading1",
|
|
76
|
+
4: "Heading2",
|
|
77
|
+
5: "Heading3",
|
|
78
|
+
12: "Bullet",
|
|
79
|
+
13: "Ordered",
|
|
80
|
+
14: "Code",
|
|
81
|
+
15: "Quote",
|
|
82
|
+
17: "Todo",
|
|
83
|
+
18: "Bitable",
|
|
84
|
+
21: "Diagram",
|
|
85
|
+
22: "Divider",
|
|
86
|
+
23: "File",
|
|
87
|
+
27: "Image",
|
|
88
|
+
30: "Sheet",
|
|
89
|
+
31: "Table",
|
|
90
|
+
32: "TableCell",
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// Block types that cannot be created via documentBlockChildren.create API
|
|
94
|
+
const UNSUPPORTED_CREATE_TYPES = new Set([31, 32]);
|
|
95
|
+
|
|
96
|
+
/** Clean blocks for insertion (remove unsupported types and read-only fields) */
|
|
97
|
+
function cleanBlocksForInsert(blocks: any[]): { cleaned: any[]; skipped: string[] } {
|
|
98
|
+
const skipped: string[] = [];
|
|
99
|
+
const cleaned = blocks
|
|
100
|
+
.filter((block) => {
|
|
101
|
+
if (UNSUPPORTED_CREATE_TYPES.has(block.block_type)) {
|
|
102
|
+
const typeName = BLOCK_TYPE_NAMES[block.block_type] || `type_${block.block_type}`;
|
|
103
|
+
skipped.push(typeName);
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
})
|
|
108
|
+
.map((block) => {
|
|
109
|
+
if (block.block_type === 31 && block.table?.merge_info) {
|
|
110
|
+
const { merge_info, ...tableRest } = block.table;
|
|
111
|
+
return { ...block, table: tableRest };
|
|
112
|
+
}
|
|
113
|
+
return block;
|
|
114
|
+
});
|
|
115
|
+
return { cleaned, skipped };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ============ Core Functions ============
|
|
119
|
+
|
|
120
|
+
async function convertMarkdown(client: Lark.Client, markdown: string) {
|
|
121
|
+
const res = await client.docx.document.convert({
|
|
122
|
+
data: { content_type: "markdown", content: markdown },
|
|
123
|
+
});
|
|
124
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
125
|
+
return {
|
|
126
|
+
blocks: res.data?.blocks ?? [],
|
|
127
|
+
firstLevelBlockIds: res.data?.first_level_block_ids ?? [],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function insertBlocks(
|
|
132
|
+
client: Lark.Client,
|
|
133
|
+
docToken: string,
|
|
134
|
+
blocks: any[],
|
|
135
|
+
parentBlockId?: string,
|
|
136
|
+
): Promise<{ children: any[]; skipped: string[] }> {
|
|
137
|
+
const { cleaned, skipped } = cleanBlocksForInsert(blocks);
|
|
138
|
+
const blockId = parentBlockId ?? docToken;
|
|
139
|
+
|
|
140
|
+
if (cleaned.length === 0) {
|
|
141
|
+
return { children: [], skipped };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const res = await client.docx.documentBlockChildren.create({
|
|
145
|
+
path: { document_id: docToken, block_id: blockId },
|
|
146
|
+
data: { children: cleaned },
|
|
147
|
+
});
|
|
148
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
149
|
+
return { children: res.data?.children ?? [], skipped };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function clearDocumentContent(client: Lark.Client, docToken: string) {
|
|
153
|
+
const existing = await client.docx.documentBlock.list({
|
|
154
|
+
path: { document_id: docToken },
|
|
155
|
+
});
|
|
156
|
+
if (existing.code !== 0) throw new Error(existing.msg);
|
|
157
|
+
|
|
158
|
+
const childIds =
|
|
159
|
+
existing.data?.items
|
|
160
|
+
?.filter((b) => b.parent_id === docToken && b.block_type !== 1)
|
|
161
|
+
.map((b) => b.block_id) ?? [];
|
|
162
|
+
|
|
163
|
+
if (childIds.length > 0) {
|
|
164
|
+
const res = await client.docx.documentBlockChildren.batchDelete({
|
|
165
|
+
path: { document_id: docToken, block_id: docToken },
|
|
166
|
+
data: { start_index: 0, end_index: childIds.length },
|
|
167
|
+
});
|
|
168
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return childIds.length;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function uploadImageToDocx(
|
|
175
|
+
client: Lark.Client,
|
|
176
|
+
blockId: string,
|
|
177
|
+
imageBuffer: Buffer,
|
|
178
|
+
fileName: string,
|
|
179
|
+
): Promise<string> {
|
|
180
|
+
const res = await client.drive.media.uploadAll({
|
|
181
|
+
data: {
|
|
182
|
+
file_name: fileName,
|
|
183
|
+
parent_type: "docx_image",
|
|
184
|
+
parent_node: blockId,
|
|
185
|
+
size: imageBuffer.length,
|
|
186
|
+
file: Readable.from(imageBuffer) as any,
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const fileToken = res?.file_token;
|
|
191
|
+
if (!fileToken) {
|
|
192
|
+
throw new Error("Image upload failed: no file_token returned");
|
|
193
|
+
}
|
|
194
|
+
return fileToken;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function downloadImage(url: string): Promise<Buffer> {
|
|
198
|
+
const response = await fetch(url);
|
|
199
|
+
if (!response.ok) {
|
|
200
|
+
throw new Error(`Failed to download image: ${response.status} ${response.statusText}`);
|
|
201
|
+
}
|
|
202
|
+
return Buffer.from(await response.arrayBuffer());
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function processImages(
|
|
206
|
+
client: Lark.Client,
|
|
207
|
+
docToken: string,
|
|
208
|
+
markdown: string,
|
|
209
|
+
insertedBlocks: any[],
|
|
210
|
+
): Promise<number> {
|
|
211
|
+
const imageUrls = extractImageUrls(markdown);
|
|
212
|
+
if (imageUrls.length === 0) return 0;
|
|
213
|
+
|
|
214
|
+
const imageBlocks = insertedBlocks.filter((b) => b.block_type === 27);
|
|
215
|
+
|
|
216
|
+
let processed = 0;
|
|
217
|
+
for (let i = 0; i < Math.min(imageUrls.length, imageBlocks.length); i++) {
|
|
218
|
+
const url = imageUrls[i];
|
|
219
|
+
const blockId = imageBlocks[i].block_id;
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
const buffer = await downloadImage(url);
|
|
223
|
+
const urlPath = new URL(url).pathname;
|
|
224
|
+
const fileName = urlPath.split("/").pop() || `image_${i}.png`;
|
|
225
|
+
const fileToken = await uploadImageToDocx(client, blockId, buffer, fileName);
|
|
226
|
+
|
|
227
|
+
await client.docx.documentBlock.patch({
|
|
228
|
+
path: { document_id: docToken, block_id: blockId },
|
|
229
|
+
data: {
|
|
230
|
+
replace_image: { token: fileToken },
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
processed++;
|
|
235
|
+
} catch (err) {
|
|
236
|
+
console.error(`Failed to process image ${url}:`, err);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return processed;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ============ Actions ============
|
|
244
|
+
|
|
245
|
+
const STRUCTURED_BLOCK_TYPES = new Set([14, 18, 21, 23, 27, 30, 31, 32]);
|
|
246
|
+
|
|
247
|
+
async function readDoc(client: Lark.Client, docToken: string) {
|
|
248
|
+
const [contentRes, infoRes, blocksRes] = await Promise.all([
|
|
249
|
+
client.docx.document.rawContent({ path: { document_id: docToken } }),
|
|
250
|
+
client.docx.document.get({ path: { document_id: docToken } }),
|
|
251
|
+
client.docx.documentBlock.list({ path: { document_id: docToken } }),
|
|
252
|
+
]);
|
|
253
|
+
|
|
254
|
+
if (contentRes.code !== 0) throw new Error(contentRes.msg);
|
|
255
|
+
|
|
256
|
+
const blocks = blocksRes.data?.items ?? [];
|
|
257
|
+
const blockCounts: Record<string, number> = {};
|
|
258
|
+
const structuredTypes: string[] = [];
|
|
259
|
+
|
|
260
|
+
for (const b of blocks) {
|
|
261
|
+
const type = b.block_type ?? 0;
|
|
262
|
+
const name = BLOCK_TYPE_NAMES[type] || `type_${type}`;
|
|
263
|
+
blockCounts[name] = (blockCounts[name] || 0) + 1;
|
|
264
|
+
|
|
265
|
+
if (STRUCTURED_BLOCK_TYPES.has(type) && !structuredTypes.includes(name)) {
|
|
266
|
+
structuredTypes.push(name);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
let hint: string | undefined;
|
|
271
|
+
if (structuredTypes.length > 0) {
|
|
272
|
+
hint = `This document contains ${structuredTypes.join(", ")} which are NOT included in the plain text above. Use feishu_doc with action: "list_blocks" to get full content.`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
title: infoRes.data?.document?.title,
|
|
277
|
+
content: contentRes.data?.content,
|
|
278
|
+
revision_id: infoRes.data?.document?.revision_id,
|
|
279
|
+
block_count: blocks.length,
|
|
280
|
+
block_types: blockCounts,
|
|
281
|
+
...(hint && { hint }),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function createDoc(client: Lark.Client, title: string, folderToken?: string) {
|
|
286
|
+
const res = await client.docx.document.create({
|
|
287
|
+
data: { title, folder_token: folderToken },
|
|
288
|
+
});
|
|
289
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
290
|
+
const doc = res.data?.document;
|
|
291
|
+
return {
|
|
292
|
+
document_id: doc?.document_id,
|
|
293
|
+
title: doc?.title,
|
|
294
|
+
url: `https://feishu.cn/docx/${doc?.document_id}`,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async function writeDoc(client: Lark.Client, docToken: string, markdown: string) {
|
|
299
|
+
const deleted = await clearDocumentContent(client, docToken);
|
|
300
|
+
|
|
301
|
+
const { blocks, firstLevelBlockIds } = await convertMarkdown(client, markdown);
|
|
302
|
+
if (blocks.length === 0) {
|
|
303
|
+
return { success: true, blocks_deleted: deleted, blocks_added: 0, images_processed: 0 };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Reorder blocks according to firstLevelBlockIds to ensure correct document structure
|
|
307
|
+
const orderedBlocks = reorderBlocks(blocks, firstLevelBlockIds);
|
|
308
|
+
|
|
309
|
+
const { children: inserted, skipped } = await insertBlocks(client, docToken, orderedBlocks);
|
|
310
|
+
const imagesProcessed = await processImages(client, docToken, markdown, inserted);
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
success: true,
|
|
314
|
+
blocks_deleted: deleted,
|
|
315
|
+
blocks_added: inserted.length,
|
|
316
|
+
images_processed: imagesProcessed,
|
|
317
|
+
...(skipped.length > 0 && {
|
|
318
|
+
warning: `Skipped unsupported block types: ${skipped.join(", ")}. Tables are not supported via this API.`,
|
|
319
|
+
}),
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async function appendDoc(client: Lark.Client, docToken: string, markdown: string) {
|
|
324
|
+
const { blocks, firstLevelBlockIds } = await convertMarkdown(client, markdown);
|
|
325
|
+
if (blocks.length === 0) {
|
|
326
|
+
throw new Error("Content is empty");
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Reorder blocks according to firstLevelBlockIds to ensure correct document structure
|
|
330
|
+
const orderedBlocks = reorderBlocks(blocks, firstLevelBlockIds);
|
|
331
|
+
|
|
332
|
+
const { children: inserted, skipped } = await insertBlocks(client, docToken, orderedBlocks);
|
|
333
|
+
const imagesProcessed = await processImages(client, docToken, markdown, inserted);
|
|
334
|
+
|
|
335
|
+
return {
|
|
336
|
+
success: true,
|
|
337
|
+
blocks_added: inserted.length,
|
|
338
|
+
images_processed: imagesProcessed,
|
|
339
|
+
block_ids: inserted.map((b: any) => b.block_id),
|
|
340
|
+
...(skipped.length > 0 && {
|
|
341
|
+
warning: `Skipped unsupported block types: ${skipped.join(", ")}. Tables are not supported via this API.`,
|
|
342
|
+
}),
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async function updateBlock(
|
|
347
|
+
client: Lark.Client,
|
|
348
|
+
docToken: string,
|
|
349
|
+
blockId: string,
|
|
350
|
+
content: string,
|
|
351
|
+
) {
|
|
352
|
+
const blockInfo = await client.docx.documentBlock.get({
|
|
353
|
+
path: { document_id: docToken, block_id: blockId },
|
|
354
|
+
});
|
|
355
|
+
if (blockInfo.code !== 0) throw new Error(blockInfo.msg);
|
|
356
|
+
|
|
357
|
+
const res = await client.docx.documentBlock.patch({
|
|
358
|
+
path: { document_id: docToken, block_id: blockId },
|
|
359
|
+
data: {
|
|
360
|
+
update_text_elements: {
|
|
361
|
+
elements: [{ text_run: { content } }],
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
});
|
|
365
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
366
|
+
|
|
367
|
+
return { success: true, block_id: blockId };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async function deleteBlock(client: Lark.Client, docToken: string, blockId: string) {
|
|
371
|
+
const blockInfo = await client.docx.documentBlock.get({
|
|
372
|
+
path: { document_id: docToken, block_id: blockId },
|
|
373
|
+
});
|
|
374
|
+
if (blockInfo.code !== 0) throw new Error(blockInfo.msg);
|
|
375
|
+
|
|
376
|
+
const parentId = blockInfo.data?.block?.parent_id ?? docToken;
|
|
377
|
+
|
|
378
|
+
const children = await client.docx.documentBlockChildren.get({
|
|
379
|
+
path: { document_id: docToken, block_id: parentId },
|
|
380
|
+
});
|
|
381
|
+
if (children.code !== 0) throw new Error(children.msg);
|
|
382
|
+
|
|
383
|
+
const items = children.data?.items ?? [];
|
|
384
|
+
const index = items.findIndex((item: any) => item.block_id === blockId);
|
|
385
|
+
if (index === -1) throw new Error("Block not found");
|
|
386
|
+
|
|
387
|
+
const res = await client.docx.documentBlockChildren.batchDelete({
|
|
388
|
+
path: { document_id: docToken, block_id: parentId },
|
|
389
|
+
data: { start_index: index, end_index: index + 1 },
|
|
390
|
+
});
|
|
391
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
392
|
+
|
|
393
|
+
return { success: true, deleted_block_id: blockId };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
async function listBlocks(client: Lark.Client, docToken: string) {
|
|
397
|
+
const res = await client.docx.documentBlock.list({
|
|
398
|
+
path: { document_id: docToken },
|
|
399
|
+
});
|
|
400
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
401
|
+
|
|
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 listAppScopes(client: Lark.Client) {
|
|
419
|
+
const res = await client.application.scope.list({});
|
|
420
|
+
if (res.code !== 0) throw new Error(res.msg);
|
|
421
|
+
|
|
422
|
+
const scopes = res.data?.scopes ?? [];
|
|
423
|
+
const granted = scopes.filter((s) => s.grant_status === 1);
|
|
424
|
+
const pending = scopes.filter((s) => s.grant_status !== 1);
|
|
425
|
+
|
|
426
|
+
return {
|
|
427
|
+
granted: granted.map((s) => ({ name: s.scope_name, type: s.scope_type })),
|
|
428
|
+
pending: pending.map((s) => ({ name: s.scope_name, type: s.scope_type })),
|
|
429
|
+
summary: `${granted.length} granted, ${pending.length} pending`,
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// ============ Tool Registration ============
|
|
434
|
+
|
|
435
|
+
export function registerFeishuDocTools(api: OpenClawPluginApi) {
|
|
436
|
+
if (!api.config) {
|
|
437
|
+
api.logger.debug?.("feishu_doc: No config available, skipping doc tools");
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Check if any account is configured
|
|
442
|
+
const accounts = listEnabledFeishuAccounts(api.config);
|
|
443
|
+
if (accounts.length === 0) {
|
|
444
|
+
api.logger.debug?.("feishu_doc: No Feishu accounts configured, skipping doc tools");
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// Use first account's config for tools configuration
|
|
449
|
+
const firstAccount = accounts[0];
|
|
450
|
+
const toolsCfg = resolveToolsConfig(firstAccount.config.tools);
|
|
451
|
+
|
|
452
|
+
// Helper to get client for the default account
|
|
453
|
+
const getClient = () => createFeishuClient(firstAccount);
|
|
454
|
+
const registered: string[] = [];
|
|
455
|
+
|
|
456
|
+
// Main document tool with action-based dispatch
|
|
457
|
+
if (toolsCfg.doc) {
|
|
458
|
+
api.registerTool(
|
|
459
|
+
{
|
|
460
|
+
name: "feishu_doc",
|
|
461
|
+
label: "Feishu Doc",
|
|
462
|
+
description:
|
|
463
|
+
"Feishu document operations. Actions: read, write, append, create, list_blocks, get_block, update_block, delete_block",
|
|
464
|
+
parameters: FeishuDocSchema,
|
|
465
|
+
async execute(_toolCallId, params) {
|
|
466
|
+
const p = params as FeishuDocParams;
|
|
467
|
+
try {
|
|
468
|
+
const client = getClient();
|
|
469
|
+
switch (p.action) {
|
|
470
|
+
case "read":
|
|
471
|
+
return json(await readDoc(client, p.doc_token));
|
|
472
|
+
case "write":
|
|
473
|
+
return json(await writeDoc(client, p.doc_token, p.content));
|
|
474
|
+
case "append":
|
|
475
|
+
return json(await appendDoc(client, p.doc_token, p.content));
|
|
476
|
+
case "create":
|
|
477
|
+
return json(await createDoc(client, p.title, p.folder_token));
|
|
478
|
+
case "list_blocks":
|
|
479
|
+
return json(await listBlocks(client, p.doc_token));
|
|
480
|
+
case "get_block":
|
|
481
|
+
return json(await getBlock(client, p.doc_token, p.block_id));
|
|
482
|
+
case "update_block":
|
|
483
|
+
return json(await updateBlock(client, p.doc_token, p.block_id, p.content));
|
|
484
|
+
case "delete_block":
|
|
485
|
+
return json(await deleteBlock(client, p.doc_token, p.block_id));
|
|
486
|
+
default:
|
|
487
|
+
return json({ error: `Unknown action: ${(p as any).action}` });
|
|
488
|
+
}
|
|
489
|
+
} catch (err) {
|
|
490
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
491
|
+
}
|
|
492
|
+
},
|
|
493
|
+
},
|
|
494
|
+
{ name: "feishu_doc" },
|
|
495
|
+
);
|
|
496
|
+
registered.push("feishu_doc");
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Keep feishu_app_scopes as independent tool
|
|
500
|
+
if (toolsCfg.scopes) {
|
|
501
|
+
api.registerTool(
|
|
502
|
+
{
|
|
503
|
+
name: "feishu_app_scopes",
|
|
504
|
+
label: "Feishu App Scopes",
|
|
505
|
+
description:
|
|
506
|
+
"List current app permissions (scopes). Use to debug permission issues or check available capabilities.",
|
|
507
|
+
parameters: Type.Object({}),
|
|
508
|
+
async execute() {
|
|
509
|
+
try {
|
|
510
|
+
const result = await listAppScopes(getClient());
|
|
511
|
+
return json(result);
|
|
512
|
+
} catch (err) {
|
|
513
|
+
return json({ error: err instanceof Error ? err.message : String(err) });
|
|
514
|
+
}
|
|
515
|
+
},
|
|
516
|
+
},
|
|
517
|
+
{ name: "feishu_app_scopes" },
|
|
518
|
+
);
|
|
519
|
+
registered.push("feishu_app_scopes");
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (registered.length > 0) {
|
|
523
|
+
api.logger.info?.(`feishu_doc: Registered ${registered.join(", ")}`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Type, type Static } from "@sinclair/typebox";
|
|
2
|
+
|
|
3
|
+
const FileType = Type.Union([
|
|
4
|
+
Type.Literal("doc"),
|
|
5
|
+
Type.Literal("docx"),
|
|
6
|
+
Type.Literal("sheet"),
|
|
7
|
+
Type.Literal("bitable"),
|
|
8
|
+
Type.Literal("folder"),
|
|
9
|
+
Type.Literal("file"),
|
|
10
|
+
Type.Literal("mindnote"),
|
|
11
|
+
Type.Literal("shortcut"),
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
export const FeishuDriveSchema = Type.Union([
|
|
15
|
+
Type.Object({
|
|
16
|
+
action: Type.Literal("list"),
|
|
17
|
+
folder_token: Type.Optional(
|
|
18
|
+
Type.String({ description: "Folder token (optional, omit for root directory)" }),
|
|
19
|
+
),
|
|
20
|
+
}),
|
|
21
|
+
Type.Object({
|
|
22
|
+
action: Type.Literal("info"),
|
|
23
|
+
file_token: Type.String({ description: "File or folder token" }),
|
|
24
|
+
type: FileType,
|
|
25
|
+
}),
|
|
26
|
+
Type.Object({
|
|
27
|
+
action: Type.Literal("create_folder"),
|
|
28
|
+
name: Type.String({ description: "Folder name" }),
|
|
29
|
+
folder_token: Type.Optional(
|
|
30
|
+
Type.String({ description: "Parent folder token (optional, omit for root)" }),
|
|
31
|
+
),
|
|
32
|
+
}),
|
|
33
|
+
Type.Object({
|
|
34
|
+
action: Type.Literal("move"),
|
|
35
|
+
file_token: Type.String({ description: "File token to move" }),
|
|
36
|
+
type: FileType,
|
|
37
|
+
folder_token: Type.String({ description: "Target folder token" }),
|
|
38
|
+
}),
|
|
39
|
+
Type.Object({
|
|
40
|
+
action: Type.Literal("delete"),
|
|
41
|
+
file_token: Type.String({ description: "File token to delete" }),
|
|
42
|
+
type: FileType,
|
|
43
|
+
}),
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
export type FeishuDriveParams = Static<typeof FeishuDriveSchema>;
|