@superbenxxxh/feishu 2.0.0 → 3.0.0

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/src/wiki.ts ADDED
@@ -0,0 +1,218 @@
1
+ import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
2
+ import { createFeishuClient } from "./client.js";
3
+ import type { FeishuConfig } from "./types.js";
4
+ import type * as Lark from "@larksuiteoapi/node-sdk";
5
+ import { FeishuWikiSchema, type FeishuWikiParams } from "./wiki-schema.js";
6
+ import { resolveToolsConfig } from "./tools-config.js";
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
+ type ObjType = "doc" | "sheet" | "mindnote" | "bitable" | "file" | "docx" | "slides";
18
+
19
+ // ============ Actions ============
20
+
21
+ const WIKI_ACCESS_HINT =
22
+ "To grant wiki access: Open wiki space → Settings → Members → Add the bot. " +
23
+ "See: https://open.feishu.cn/document/server-docs/docs/wiki-v2/wiki-qa#a40ad4ca";
24
+
25
+ async function listSpaces(client: Lark.Client) {
26
+ const res = await client.wiki.space.list({});
27
+ if (res.code !== 0) throw new Error(res.msg);
28
+
29
+ const spaces =
30
+ res.data?.items?.map((s) => ({
31
+ space_id: s.space_id,
32
+ name: s.name,
33
+ description: s.description,
34
+ visibility: s.visibility,
35
+ })) ?? [];
36
+
37
+ return {
38
+ spaces,
39
+ ...(spaces.length === 0 && { hint: WIKI_ACCESS_HINT }),
40
+ };
41
+ }
42
+
43
+ async function listNodes(client: Lark.Client, spaceId: string, parentNodeToken?: string) {
44
+ const res = await client.wiki.spaceNode.list({
45
+ path: { space_id: spaceId },
46
+ params: { parent_node_token: parentNodeToken },
47
+ });
48
+ if (res.code !== 0) throw new Error(res.msg);
49
+
50
+ return {
51
+ nodes:
52
+ res.data?.items?.map((n) => ({
53
+ node_token: n.node_token,
54
+ obj_token: n.obj_token,
55
+ obj_type: n.obj_type,
56
+ title: n.title,
57
+ has_child: n.has_child,
58
+ })) ?? [],
59
+ };
60
+ }
61
+
62
+ async function getNode(client: Lark.Client, token: string) {
63
+ const res = await client.wiki.space.getNode({
64
+ params: { token },
65
+ });
66
+ if (res.code !== 0) throw new Error(res.msg);
67
+
68
+ const node = res.data?.node;
69
+ return {
70
+ node_token: node?.node_token,
71
+ space_id: node?.space_id,
72
+ obj_token: node?.obj_token,
73
+ obj_type: node?.obj_type,
74
+ title: node?.title,
75
+ parent_node_token: node?.parent_node_token,
76
+ has_child: node?.has_child,
77
+ creator: node?.creator,
78
+ create_time: node?.node_create_time,
79
+ };
80
+ }
81
+
82
+ async function createNode(
83
+ client: Lark.Client,
84
+ spaceId: string,
85
+ title: string,
86
+ objType?: string,
87
+ parentNodeToken?: string,
88
+ ) {
89
+ const res = await client.wiki.spaceNode.create({
90
+ path: { space_id: spaceId },
91
+ data: {
92
+ obj_type: (objType as ObjType) || "docx",
93
+ node_type: "origin" as const,
94
+ title,
95
+ parent_node_token: parentNodeToken,
96
+ },
97
+ });
98
+ if (res.code !== 0) throw new Error(res.msg);
99
+
100
+ const node = res.data?.node;
101
+ return {
102
+ node_token: node?.node_token,
103
+ obj_token: node?.obj_token,
104
+ obj_type: node?.obj_type,
105
+ title: node?.title,
106
+ };
107
+ }
108
+
109
+ async function moveNode(
110
+ client: Lark.Client,
111
+ spaceId: string,
112
+ nodeToken: string,
113
+ targetSpaceId?: string,
114
+ targetParentToken?: string,
115
+ ) {
116
+ const res = await client.wiki.spaceNode.move({
117
+ path: { space_id: spaceId, node_token: nodeToken },
118
+ data: {
119
+ target_space_id: targetSpaceId || spaceId,
120
+ target_parent_token: targetParentToken,
121
+ },
122
+ });
123
+ if (res.code !== 0) throw new Error(res.msg);
124
+
125
+ return {
126
+ success: true,
127
+ node_token: res.data?.node?.node_token,
128
+ };
129
+ }
130
+
131
+ async function renameNode(
132
+ client: Lark.Client,
133
+ spaceId: string,
134
+ nodeToken: string,
135
+ title: string,
136
+ ) {
137
+ const res = await client.wiki.spaceNode.updateTitle({
138
+ path: { space_id: spaceId, node_token: nodeToken },
139
+ data: { title },
140
+ });
141
+ if (res.code !== 0) throw new Error(res.msg);
142
+
143
+ return {
144
+ success: true,
145
+ node_token: nodeToken,
146
+ title,
147
+ };
148
+ }
149
+
150
+ // ============ Tool Registration ============
151
+
152
+ export function registerFeishuWikiTools(api: OpenClawPluginApi) {
153
+ const feishuCfg = api.config?.channels?.feishu as FeishuConfig | undefined;
154
+ if (!feishuCfg?.appId || !feishuCfg?.appSecret) {
155
+ api.logger.debug?.("feishu_wiki: Feishu credentials not configured, skipping wiki tools");
156
+ return;
157
+ }
158
+
159
+ const toolsCfg = resolveToolsConfig(feishuCfg.tools);
160
+ if (!toolsCfg.wiki) {
161
+ api.logger.debug?.("feishu_wiki: wiki tool disabled in config");
162
+ return;
163
+ }
164
+
165
+ const getClient = () => createFeishuClient(feishuCfg);
166
+
167
+ api.registerTool(
168
+ {
169
+ name: "feishu_wiki",
170
+ label: "Feishu Wiki",
171
+ description:
172
+ "Feishu knowledge base operations. Actions: spaces, nodes, get, create, move, rename",
173
+ parameters: FeishuWikiSchema,
174
+ async execute(_toolCallId, params) {
175
+ const p = params as FeishuWikiParams;
176
+ try {
177
+ const client = getClient();
178
+ switch (p.action) {
179
+ case "spaces":
180
+ return json(await listSpaces(client));
181
+ case "nodes":
182
+ return json(await listNodes(client, p.space_id, p.parent_node_token));
183
+ case "get":
184
+ return json(await getNode(client, p.token));
185
+ case "search":
186
+ return json({
187
+ error:
188
+ "Search is not available. Use feishu_wiki with action: 'nodes' to browse or action: 'get' to lookup by token.",
189
+ });
190
+ case "create":
191
+ return json(
192
+ await createNode(client, p.space_id, p.title, p.obj_type, p.parent_node_token),
193
+ );
194
+ case "move":
195
+ return json(
196
+ await moveNode(
197
+ client,
198
+ p.space_id,
199
+ p.node_token,
200
+ p.target_space_id,
201
+ p.target_parent_token,
202
+ ),
203
+ );
204
+ case "rename":
205
+ return json(await renameNode(client, p.space_id, p.node_token, p.title));
206
+ default:
207
+ return json({ error: `Unknown action: ${(p as any).action}` });
208
+ }
209
+ } catch (err) {
210
+ return json({ error: err instanceof Error ? err.message : String(err) });
211
+ }
212
+ },
213
+ },
214
+ { name: "feishu_wiki" },
215
+ );
216
+
217
+ api.logger.info?.(`feishu_wiki: Registered feishu_wiki tool`);
218
+ }