@openclaw-plugins/feishu-plus 0.1.7

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.
@@ -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>;
package/src/drive.ts ADDED
@@ -0,0 +1,207 @@
1
+ import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
2
+ import { createFeishuClient } from "./client.js";
3
+ import { listEnabledFeishuAccounts } from "./accounts.js";
4
+ import type * as Lark from "@larksuiteoapi/node-sdk";
5
+ import { FeishuDriveSchema, type FeishuDriveParams } from "./drive-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
+ // ============ Actions ============
18
+
19
+ async function getRootFolderToken(client: Lark.Client): Promise<string> {
20
+ // Use generic HTTP client to call the root folder meta API
21
+ // as it's not directly exposed in the SDK
22
+ const domain = (client as any).domain ?? "https://open.feishu.cn";
23
+ const res = (await (client as any).httpInstance.get(
24
+ `${domain}/open-apis/drive/explorer/v2/root_folder/meta`,
25
+ )) as { code: number; msg?: string; data?: { token?: string } };
26
+ if (res.code !== 0) throw new Error(res.msg ?? "Failed to get root folder");
27
+ const token = res.data?.token;
28
+ if (!token) throw new Error("Root folder token not found");
29
+ return token;
30
+ }
31
+
32
+ async function listFolder(client: Lark.Client, folderToken?: string) {
33
+ // Filter out invalid folder_token values (empty, "0", etc.)
34
+ const validFolderToken = folderToken && folderToken !== "0" ? folderToken : undefined;
35
+ const res = await client.drive.file.list({
36
+ params: validFolderToken ? { folder_token: validFolderToken } : {},
37
+ });
38
+ if (res.code !== 0) throw new Error(res.msg);
39
+
40
+ return {
41
+ files:
42
+ res.data?.files?.map((f) => ({
43
+ token: f.token,
44
+ name: f.name,
45
+ type: f.type,
46
+ url: f.url,
47
+ created_time: f.created_time,
48
+ modified_time: f.modified_time,
49
+ owner_id: f.owner_id,
50
+ })) ?? [],
51
+ next_page_token: res.data?.next_page_token,
52
+ };
53
+ }
54
+
55
+ async function getFileInfo(client: Lark.Client, fileToken: string, folderToken?: string) {
56
+ // Use list with folder_token to find file info
57
+ const res = await client.drive.file.list({
58
+ params: folderToken ? { folder_token: folderToken } : {},
59
+ });
60
+ if (res.code !== 0) throw new Error(res.msg);
61
+
62
+ const file = res.data?.files?.find((f) => f.token === fileToken);
63
+ if (!file) {
64
+ throw new Error(`File not found: ${fileToken}`);
65
+ }
66
+
67
+ return {
68
+ token: file.token,
69
+ name: file.name,
70
+ type: file.type,
71
+ url: file.url,
72
+ created_time: file.created_time,
73
+ modified_time: file.modified_time,
74
+ owner_id: file.owner_id,
75
+ };
76
+ }
77
+
78
+ async function createFolder(client: Lark.Client, name: string, folderToken?: string) {
79
+ // Feishu supports using folder_token="0" as the root folder.
80
+ // We *try* to resolve the real root token (explorer API), but fall back to "0"
81
+ // because some tenants/apps return 400 for that explorer endpoint.
82
+ let effectiveToken = folderToken && folderToken !== "0" ? folderToken : "0";
83
+ if (effectiveToken === "0") {
84
+ try {
85
+ effectiveToken = await getRootFolderToken(client);
86
+ } catch {
87
+ // ignore and keep "0"
88
+ }
89
+ }
90
+
91
+ const res = await client.drive.file.createFolder({
92
+ data: {
93
+ name,
94
+ folder_token: effectiveToken,
95
+ },
96
+ });
97
+ if (res.code !== 0) throw new Error(res.msg);
98
+
99
+ return {
100
+ token: res.data?.token,
101
+ url: res.data?.url,
102
+ };
103
+ }
104
+
105
+ async function moveFile(
106
+ client: Lark.Client,
107
+ fileToken: string,
108
+ type: string,
109
+ folderToken: string,
110
+ ) {
111
+ const res = await client.drive.file.move({
112
+ path: { file_token: fileToken },
113
+ data: {
114
+ type: type as "doc" | "docx" | "sheet" | "bitable" | "folder" | "file" | "mindnote" | "slides",
115
+ folder_token: folderToken,
116
+ },
117
+ });
118
+ if (res.code !== 0) throw new Error(res.msg);
119
+
120
+ return {
121
+ success: true,
122
+ task_id: res.data?.task_id,
123
+ };
124
+ }
125
+
126
+ async function deleteFile(client: Lark.Client, fileToken: string, type: string) {
127
+ const res = await client.drive.file.delete({
128
+ path: { file_token: fileToken },
129
+ params: {
130
+ type: type as
131
+ | "doc"
132
+ | "docx"
133
+ | "sheet"
134
+ | "bitable"
135
+ | "folder"
136
+ | "file"
137
+ | "mindnote"
138
+ | "slides"
139
+ | "shortcut",
140
+ },
141
+ });
142
+ if (res.code !== 0) throw new Error(res.msg);
143
+
144
+ return {
145
+ success: true,
146
+ task_id: res.data?.task_id,
147
+ };
148
+ }
149
+
150
+ // ============ Tool Registration ============
151
+
152
+ export function registerFeishuDriveTools(api: OpenClawPluginApi) {
153
+ if (!api.config) {
154
+ api.logger.debug?.("feishu_drive: No config available, skipping drive tools");
155
+ return;
156
+ }
157
+
158
+ const accounts = listEnabledFeishuAccounts(api.config);
159
+ if (accounts.length === 0) {
160
+ api.logger.debug?.("feishu_drive: No Feishu accounts configured, skipping drive tools");
161
+ return;
162
+ }
163
+
164
+ const firstAccount = accounts[0];
165
+ const toolsCfg = resolveToolsConfig(firstAccount.config.tools);
166
+ if (!toolsCfg.drive) {
167
+ api.logger.debug?.("feishu_drive: drive tool disabled in config");
168
+ return;
169
+ }
170
+
171
+ const getClient = () => createFeishuClient(firstAccount);
172
+
173
+ api.registerTool(
174
+ {
175
+ name: "feishu_drive",
176
+ label: "Feishu Drive",
177
+ description:
178
+ "Feishu cloud storage operations. Actions: list, info, create_folder, move, delete",
179
+ parameters: FeishuDriveSchema,
180
+ async execute(_toolCallId, params) {
181
+ const p = params as FeishuDriveParams;
182
+ try {
183
+ const client = getClient();
184
+ switch (p.action) {
185
+ case "list":
186
+ return json(await listFolder(client, p.folder_token));
187
+ case "info":
188
+ return json(await getFileInfo(client, p.file_token));
189
+ case "create_folder":
190
+ return json(await createFolder(client, p.name, p.folder_token));
191
+ case "move":
192
+ return json(await moveFile(client, p.file_token, p.type, p.folder_token));
193
+ case "delete":
194
+ return json(await deleteFile(client, p.file_token, p.type));
195
+ default:
196
+ return json({ error: `Unknown action: ${(p as any).action}` });
197
+ }
198
+ } catch (err) {
199
+ return json({ error: err instanceof Error ? err.message : String(err) });
200
+ }
201
+ },
202
+ },
203
+ { name: "feishu_drive" },
204
+ );
205
+
206
+ api.logger.info?.(`feishu_drive: Registered feishu_drive tool`);
207
+ }
@@ -0,0 +1,131 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import os from "node:os";
4
+ import type { OpenClawConfig, PluginRuntime } from "openclaw/plugin-sdk";
5
+ import type { DynamicAgentCreationConfig } from "./types.js";
6
+
7
+ export type MaybeCreateDynamicAgentResult = {
8
+ created: boolean;
9
+ updatedCfg: OpenClawConfig;
10
+ agentId?: string;
11
+ };
12
+
13
+ /**
14
+ * Check if a dynamic agent should be created for a DM user and create it if needed.
15
+ * This creates a unique agent instance with its own workspace for each DM user.
16
+ */
17
+ export async function maybeCreateDynamicAgent(params: {
18
+ cfg: OpenClawConfig;
19
+ runtime: PluginRuntime;
20
+ senderOpenId: string;
21
+ dynamicCfg: DynamicAgentCreationConfig;
22
+ log: (msg: string) => void;
23
+ }): Promise<MaybeCreateDynamicAgentResult> {
24
+ const { cfg, runtime, senderOpenId, dynamicCfg, log } = params;
25
+
26
+ // Check if there's already a binding for this user
27
+ const existingBindings = cfg.bindings ?? [];
28
+ const hasBinding = existingBindings.some(
29
+ (b) =>
30
+ b.match?.channel === "feishu" &&
31
+ b.match?.peer?.kind === "dm" &&
32
+ b.match?.peer?.id === senderOpenId,
33
+ );
34
+
35
+ if (hasBinding) {
36
+ return { created: false, updatedCfg: cfg };
37
+ }
38
+
39
+ // Check maxAgents limit if configured
40
+ if (dynamicCfg.maxAgents !== undefined) {
41
+ const feishuAgentCount = (cfg.agents?.list ?? []).filter((a) =>
42
+ a.id.startsWith("feishu-"),
43
+ ).length;
44
+ if (feishuAgentCount >= dynamicCfg.maxAgents) {
45
+ log(
46
+ `feishu: maxAgents limit (${dynamicCfg.maxAgents}) reached, not creating agent for ${senderOpenId}`,
47
+ );
48
+ return { created: false, updatedCfg: cfg };
49
+ }
50
+ }
51
+
52
+ // Use full OpenID as agent ID suffix (OpenID format: ou_xxx is already filesystem-safe)
53
+ const agentId = `feishu-${senderOpenId}`;
54
+
55
+ // Check if agent already exists (but binding was missing)
56
+ const existingAgent = (cfg.agents?.list ?? []).find((a) => a.id === agentId);
57
+ if (existingAgent) {
58
+ // Agent exists but binding doesn't - just add the binding
59
+ log(`feishu: agent "${agentId}" exists, adding missing binding for ${senderOpenId}`);
60
+
61
+ const updatedCfg: OpenClawConfig = {
62
+ ...cfg,
63
+ bindings: [
64
+ ...existingBindings,
65
+ {
66
+ agentId,
67
+ match: {
68
+ channel: "feishu",
69
+ peer: { kind: "dm", id: senderOpenId },
70
+ },
71
+ },
72
+ ],
73
+ };
74
+
75
+ await runtime.config.writeConfigFile(updatedCfg);
76
+ return { created: true, updatedCfg, agentId };
77
+ }
78
+
79
+ // Resolve path templates with substitutions
80
+ const workspaceTemplate = dynamicCfg.workspaceTemplate ?? "~/.openclaw/workspace-{agentId}";
81
+ const agentDirTemplate = dynamicCfg.agentDirTemplate ?? "~/.openclaw/agents/{agentId}/agent";
82
+
83
+ const workspace = resolveUserPath(
84
+ workspaceTemplate.replace("{userId}", senderOpenId).replace("{agentId}", agentId),
85
+ );
86
+ const agentDir = resolveUserPath(
87
+ agentDirTemplate.replace("{userId}", senderOpenId).replace("{agentId}", agentId),
88
+ );
89
+
90
+ log(`feishu: creating dynamic agent "${agentId}" for user ${senderOpenId}`);
91
+ log(` workspace: ${workspace}`);
92
+ log(` agentDir: ${agentDir}`);
93
+
94
+ // Create directories
95
+ await fs.promises.mkdir(workspace, { recursive: true });
96
+ await fs.promises.mkdir(agentDir, { recursive: true });
97
+
98
+ // Update configuration with new agent and binding
99
+ const updatedCfg: OpenClawConfig = {
100
+ ...cfg,
101
+ agents: {
102
+ ...cfg.agents,
103
+ list: [...(cfg.agents?.list ?? []), { id: agentId, workspace, agentDir }],
104
+ },
105
+ bindings: [
106
+ ...existingBindings,
107
+ {
108
+ agentId,
109
+ match: {
110
+ channel: "feishu",
111
+ peer: { kind: "dm", id: senderOpenId },
112
+ },
113
+ },
114
+ ],
115
+ };
116
+
117
+ // Write updated config using PluginRuntime API
118
+ await runtime.config.writeConfigFile(updatedCfg);
119
+
120
+ return { created: true, updatedCfg, agentId };
121
+ }
122
+
123
+ /**
124
+ * Resolve a path that may start with ~ to the user's home directory.
125
+ */
126
+ function resolveUserPath(p: string): string {
127
+ if (p.startsWith("~/")) {
128
+ return path.join(os.homedir(), p.slice(2));
129
+ }
130
+ return p;
131
+ }