@nextclaw/channel-plugin-feishu 0.2.13 → 0.2.15
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/README.md +3 -1
- package/index.ts +65 -0
- package/openclaw.plugin.json +3 -7
- package/package.json +32 -9
- package/skills/feishu-doc/SKILL.md +211 -0
- package/skills/feishu-doc/references/block-types.md +103 -0
- package/skills/feishu-drive/SKILL.md +97 -0
- package/skills/feishu-perm/SKILL.md +119 -0
- package/skills/feishu-wiki/SKILL.md +111 -0
- package/src/accounts.test.ts +371 -0
- package/src/accounts.ts +244 -0
- package/src/async.ts +62 -0
- package/src/bitable.ts +725 -0
- package/src/bot.card-action.test.ts +63 -0
- package/src/bot.checkBotMentioned.test.ts +193 -0
- package/src/bot.stripBotMention.test.ts +134 -0
- package/src/bot.test.ts +2107 -0
- package/src/bot.ts +1556 -0
- package/src/card-action.ts +79 -0
- package/src/channel.test.ts +48 -0
- package/src/channel.ts +369 -0
- package/src/chat-schema.ts +24 -0
- package/src/chat.test.ts +89 -0
- package/src/chat.ts +130 -0
- package/src/client.test.ts +324 -0
- package/src/client.ts +196 -0
- package/src/config-schema.test.ts +247 -0
- package/src/config-schema.ts +306 -0
- package/src/dedup.ts +203 -0
- package/src/directory.test.ts +40 -0
- package/src/directory.ts +156 -0
- package/src/doc-schema.ts +182 -0
- package/src/docx-batch-insert.test.ts +90 -0
- package/src/docx-batch-insert.ts +187 -0
- package/src/docx-color-text.ts +149 -0
- package/src/docx-table-ops.ts +298 -0
- package/src/docx.account-selection.test.ts +70 -0
- package/src/docx.test.ts +445 -0
- package/src/docx.ts +1460 -0
- package/src/drive-schema.ts +46 -0
- package/src/drive.ts +228 -0
- package/src/dynamic-agent.ts +131 -0
- package/src/external-keys.test.ts +20 -0
- package/src/external-keys.ts +19 -0
- package/src/feishu-command-handler.ts +59 -0
- package/src/media.test.ts +523 -0
- package/src/media.ts +484 -0
- package/src/mention.ts +133 -0
- package/src/monitor.account.ts +562 -0
- package/src/monitor.reaction.test.ts +653 -0
- package/src/monitor.startup.test.ts +190 -0
- package/src/monitor.startup.ts +64 -0
- package/src/monitor.state.defaults.test.ts +46 -0
- package/src/monitor.state.ts +155 -0
- package/src/monitor.test-mocks.ts +45 -0
- package/src/monitor.transport.ts +264 -0
- package/src/monitor.ts +95 -0
- package/src/monitor.webhook-e2e.test.ts +214 -0
- package/src/monitor.webhook-security.test.ts +142 -0
- package/src/monitor.webhook.test-helpers.ts +98 -0
- package/src/nextclaw-sdk/account-id.ts +31 -0
- package/src/nextclaw-sdk/compat.ts +8 -0
- package/src/nextclaw-sdk/core-channel.ts +296 -0
- package/src/nextclaw-sdk/core-pairing.ts +224 -0
- package/src/nextclaw-sdk/core.ts +26 -0
- package/src/nextclaw-sdk/dedupe.ts +246 -0
- package/src/nextclaw-sdk/feishu.ts +77 -0
- package/src/nextclaw-sdk/history.ts +127 -0
- package/src/nextclaw-sdk/network-body.ts +245 -0
- package/src/nextclaw-sdk/network-fetch.ts +129 -0
- package/src/nextclaw-sdk/network-webhook.ts +182 -0
- package/src/nextclaw-sdk/network.ts +13 -0
- package/src/nextclaw-sdk/runtime-store.ts +26 -0
- package/src/nextclaw-sdk/secrets-config.ts +109 -0
- package/src/nextclaw-sdk/secrets-core.ts +170 -0
- package/src/nextclaw-sdk/secrets-prompt.ts +305 -0
- package/src/nextclaw-sdk/secrets.ts +18 -0
- package/src/nextclaw-sdk/types.ts +300 -0
- package/src/onboarding.status.test.ts +25 -0
- package/src/onboarding.test.ts +143 -0
- package/src/onboarding.ts +489 -0
- package/src/outbound.test.ts +356 -0
- package/src/outbound.ts +176 -0
- package/src/perm-schema.ts +52 -0
- package/src/perm.ts +176 -0
- package/src/policy.test.ts +154 -0
- package/src/policy.ts +123 -0
- package/src/post.test.ts +105 -0
- package/src/post.ts +274 -0
- package/src/probe.test.ts +270 -0
- package/src/probe.ts +156 -0
- package/src/reactions.ts +153 -0
- package/src/reply-dispatcher.test.ts +513 -0
- package/src/reply-dispatcher.ts +397 -0
- package/src/runtime.ts +6 -0
- package/src/secret-input.ts +13 -0
- package/src/send-message.ts +71 -0
- package/src/send-result.ts +29 -0
- package/src/send-target.test.ts +74 -0
- package/src/send-target.ts +29 -0
- package/src/send.reply-fallback.test.ts +189 -0
- package/src/send.test.ts +168 -0
- package/src/send.ts +481 -0
- package/src/streaming-card.test.ts +54 -0
- package/src/streaming-card.ts +374 -0
- package/src/targets.test.ts +70 -0
- package/src/targets.ts +107 -0
- package/src/tool-account-routing.test.ts +129 -0
- package/src/tool-account.ts +70 -0
- package/src/tool-factory-test-harness.ts +76 -0
- package/src/tool-result.test.ts +32 -0
- package/src/tool-result.ts +14 -0
- package/src/tools-config.test.ts +21 -0
- package/src/tools-config.ts +22 -0
- package/src/types.ts +103 -0
- package/src/typing.test.ts +144 -0
- package/src/typing.ts +210 -0
- package/src/wiki-schema.ts +55 -0
- package/src/wiki.ts +233 -0
- package/index.js +0 -27
|
@@ -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,228 @@
|
|
|
1
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
2
|
+
import type { OpenClawPluginApi } from "./nextclaw-sdk/feishu.js";
|
|
3
|
+
import { listEnabledFeishuAccounts } from "./accounts.js";
|
|
4
|
+
import { FeishuDriveSchema, type FeishuDriveParams } from "./drive-schema.js";
|
|
5
|
+
import { createFeishuToolClient, resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js";
|
|
6
|
+
import {
|
|
7
|
+
jsonToolResult,
|
|
8
|
+
toolExecutionErrorResult,
|
|
9
|
+
unknownToolActionResult,
|
|
10
|
+
} from "./tool-result.js";
|
|
11
|
+
|
|
12
|
+
// ============ Actions ============
|
|
13
|
+
|
|
14
|
+
async function getRootFolderToken(client: Lark.Client): Promise<string> {
|
|
15
|
+
// Use generic HTTP client to call the root folder meta API
|
|
16
|
+
// as it's not directly exposed in the SDK
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- accessing internal SDK property
|
|
18
|
+
const domain = (client as any).domain ?? "https://open.feishu.cn";
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- accessing internal SDK property
|
|
20
|
+
const res = (await (client as any).httpInstance.get(
|
|
21
|
+
`${domain}/open-apis/drive/explorer/v2/root_folder/meta`,
|
|
22
|
+
)) as { code: number; msg?: string; data?: { token?: string } };
|
|
23
|
+
if (res.code !== 0) {
|
|
24
|
+
throw new Error(res.msg ?? "Failed to get root folder");
|
|
25
|
+
}
|
|
26
|
+
const token = res.data?.token;
|
|
27
|
+
if (!token) {
|
|
28
|
+
throw new Error("Root folder token not found");
|
|
29
|
+
}
|
|
30
|
+
return token;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function listFolder(client: Lark.Client, folderToken?: string) {
|
|
34
|
+
// Filter out invalid folder_token values (empty, "0", etc.)
|
|
35
|
+
const validFolderToken = folderToken && folderToken !== "0" ? folderToken : undefined;
|
|
36
|
+
const res = await client.drive.file.list({
|
|
37
|
+
params: validFolderToken ? { folder_token: validFolderToken } : {},
|
|
38
|
+
});
|
|
39
|
+
if (res.code !== 0) {
|
|
40
|
+
throw new Error(res.msg);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
files:
|
|
45
|
+
res.data?.files?.map((f) => ({
|
|
46
|
+
token: f.token,
|
|
47
|
+
name: f.name,
|
|
48
|
+
type: f.type,
|
|
49
|
+
url: f.url,
|
|
50
|
+
created_time: f.created_time,
|
|
51
|
+
modified_time: f.modified_time,
|
|
52
|
+
owner_id: f.owner_id,
|
|
53
|
+
})) ?? [],
|
|
54
|
+
next_page_token: res.data?.next_page_token,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function getFileInfo(client: Lark.Client, fileToken: string, folderToken?: string) {
|
|
59
|
+
// Use list with folder_token to find file info
|
|
60
|
+
const res = await client.drive.file.list({
|
|
61
|
+
params: folderToken ? { folder_token: folderToken } : {},
|
|
62
|
+
});
|
|
63
|
+
if (res.code !== 0) {
|
|
64
|
+
throw new Error(res.msg);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const file = res.data?.files?.find((f) => f.token === fileToken);
|
|
68
|
+
if (!file) {
|
|
69
|
+
throw new Error(`File not found: ${fileToken}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
token: file.token,
|
|
74
|
+
name: file.name,
|
|
75
|
+
type: file.type,
|
|
76
|
+
url: file.url,
|
|
77
|
+
created_time: file.created_time,
|
|
78
|
+
modified_time: file.modified_time,
|
|
79
|
+
owner_id: file.owner_id,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function createFolder(client: Lark.Client, name: string, folderToken?: string) {
|
|
84
|
+
// Feishu supports using folder_token="0" as the root folder.
|
|
85
|
+
// We *try* to resolve the real root token (explorer API), but fall back to "0"
|
|
86
|
+
// because some tenants/apps return 400 for that explorer endpoint.
|
|
87
|
+
let effectiveToken = folderToken && folderToken !== "0" ? folderToken : "0";
|
|
88
|
+
if (effectiveToken === "0") {
|
|
89
|
+
try {
|
|
90
|
+
effectiveToken = await getRootFolderToken(client);
|
|
91
|
+
} catch {
|
|
92
|
+
// ignore and keep "0"
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const res = await client.drive.file.createFolder({
|
|
97
|
+
data: {
|
|
98
|
+
name,
|
|
99
|
+
folder_token: effectiveToken,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
if (res.code !== 0) {
|
|
103
|
+
throw new Error(res.msg);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
token: res.data?.token,
|
|
108
|
+
url: res.data?.url,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function moveFile(client: Lark.Client, fileToken: string, type: string, folderToken: string) {
|
|
113
|
+
const res = await client.drive.file.move({
|
|
114
|
+
path: { file_token: fileToken },
|
|
115
|
+
data: {
|
|
116
|
+
type: type as
|
|
117
|
+
| "doc"
|
|
118
|
+
| "docx"
|
|
119
|
+
| "sheet"
|
|
120
|
+
| "bitable"
|
|
121
|
+
| "folder"
|
|
122
|
+
| "file"
|
|
123
|
+
| "mindnote"
|
|
124
|
+
| "slides",
|
|
125
|
+
folder_token: folderToken,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
if (res.code !== 0) {
|
|
129
|
+
throw new Error(res.msg);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
success: true,
|
|
134
|
+
task_id: res.data?.task_id,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function deleteFile(client: Lark.Client, fileToken: string, type: string) {
|
|
139
|
+
const res = await client.drive.file.delete({
|
|
140
|
+
path: { file_token: fileToken },
|
|
141
|
+
params: {
|
|
142
|
+
type: type as
|
|
143
|
+
| "doc"
|
|
144
|
+
| "docx"
|
|
145
|
+
| "sheet"
|
|
146
|
+
| "bitable"
|
|
147
|
+
| "folder"
|
|
148
|
+
| "file"
|
|
149
|
+
| "mindnote"
|
|
150
|
+
| "slides"
|
|
151
|
+
| "shortcut",
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
if (res.code !== 0) {
|
|
155
|
+
throw new Error(res.msg);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
success: true,
|
|
160
|
+
task_id: res.data?.task_id,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ============ Tool Registration ============
|
|
165
|
+
|
|
166
|
+
export function registerFeishuDriveTools(api: OpenClawPluginApi) {
|
|
167
|
+
if (!api.config) {
|
|
168
|
+
api.logger.debug?.("feishu_drive: No config available, skipping drive tools");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const accounts = listEnabledFeishuAccounts(api.config);
|
|
173
|
+
if (accounts.length === 0) {
|
|
174
|
+
api.logger.debug?.("feishu_drive: No Feishu accounts configured, skipping drive tools");
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const toolsCfg = resolveAnyEnabledFeishuToolsConfig(accounts);
|
|
179
|
+
if (!toolsCfg.drive) {
|
|
180
|
+
api.logger.debug?.("feishu_drive: drive tool disabled in config");
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
type FeishuDriveExecuteParams = FeishuDriveParams & { accountId?: string };
|
|
185
|
+
|
|
186
|
+
api.registerTool(
|
|
187
|
+
(ctx) => {
|
|
188
|
+
const defaultAccountId = ctx.agentAccountId;
|
|
189
|
+
return {
|
|
190
|
+
name: "feishu_drive",
|
|
191
|
+
label: "Feishu Drive",
|
|
192
|
+
description:
|
|
193
|
+
"Feishu cloud storage operations. Actions: list, info, create_folder, move, delete",
|
|
194
|
+
parameters: FeishuDriveSchema,
|
|
195
|
+
async execute(_toolCallId, params) {
|
|
196
|
+
const p = params as FeishuDriveExecuteParams;
|
|
197
|
+
try {
|
|
198
|
+
const client = createFeishuToolClient({
|
|
199
|
+
api,
|
|
200
|
+
executeParams: p,
|
|
201
|
+
defaultAccountId,
|
|
202
|
+
});
|
|
203
|
+
switch (p.action) {
|
|
204
|
+
case "list":
|
|
205
|
+
return jsonToolResult(await listFolder(client, p.folder_token));
|
|
206
|
+
case "info":
|
|
207
|
+
return jsonToolResult(await getFileInfo(client, p.file_token));
|
|
208
|
+
case "create_folder":
|
|
209
|
+
return jsonToolResult(await createFolder(client, p.name, p.folder_token));
|
|
210
|
+
case "move":
|
|
211
|
+
return jsonToolResult(await moveFile(client, p.file_token, p.type, p.folder_token));
|
|
212
|
+
case "delete":
|
|
213
|
+
return jsonToolResult(await deleteFile(client, p.file_token, p.type));
|
|
214
|
+
default:
|
|
215
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- exhaustive check fallback
|
|
216
|
+
return unknownToolActionResult((p as { action?: unknown }).action);
|
|
217
|
+
}
|
|
218
|
+
} catch (err) {
|
|
219
|
+
return toolExecutionErrorResult(err);
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
},
|
|
224
|
+
{ name: "feishu_drive" },
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
api.logger.info?.(`feishu_drive: Registered feishu_drive tool`);
|
|
228
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type { OpenClawConfig, PluginRuntime } from "./nextclaw-sdk/feishu.js";
|
|
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 === "direct" &&
|
|
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: "direct", 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: "direct", 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
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { normalizeFeishuExternalKey } from "./external-keys.js";
|
|
3
|
+
|
|
4
|
+
describe("normalizeFeishuExternalKey", () => {
|
|
5
|
+
it("accepts a normal feishu key and trims surrounding spaces", () => {
|
|
6
|
+
expect(normalizeFeishuExternalKey(" img_v3_01abcDEF123 ")).toBe("img_v3_01abcDEF123");
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("rejects traversal and path separator patterns", () => {
|
|
10
|
+
expect(normalizeFeishuExternalKey("../etc/passwd")).toBeUndefined();
|
|
11
|
+
expect(normalizeFeishuExternalKey("a/../../b")).toBeUndefined();
|
|
12
|
+
expect(normalizeFeishuExternalKey("a\\..\\b")).toBeUndefined();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("rejects empty, non-string, and control-char values", () => {
|
|
16
|
+
expect(normalizeFeishuExternalKey(" ")).toBeUndefined();
|
|
17
|
+
expect(normalizeFeishuExternalKey(123)).toBeUndefined();
|
|
18
|
+
expect(normalizeFeishuExternalKey("abc\u0000def")).toBeUndefined();
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const CONTROL_CHARS_RE = /[\u0000-\u001f\u007f]/;
|
|
2
|
+
const MAX_EXTERNAL_KEY_LENGTH = 512;
|
|
3
|
+
|
|
4
|
+
export function normalizeFeishuExternalKey(value: unknown): string | undefined {
|
|
5
|
+
if (typeof value !== "string") {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
const normalized = value.trim();
|
|
9
|
+
if (!normalized || normalized.length > MAX_EXTERNAL_KEY_LENGTH) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
if (CONTROL_CHARS_RE.test(normalized)) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
if (normalized.includes("/") || normalized.includes("\\") || normalized.includes("..")) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return normalized;
|
|
19
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const DEFAULT_RESET_TRIGGERS = ["/new", "/reset"] as const;
|
|
2
|
+
|
|
3
|
+
type FeishuBeforeResetContext = {
|
|
4
|
+
cfg: Record<string, unknown>;
|
|
5
|
+
sessionEntry: Record<string, unknown>;
|
|
6
|
+
previousSessionEntry?: Record<string, unknown>;
|
|
7
|
+
commandSource: string;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type FeishuBeforeResetEvent = {
|
|
12
|
+
type: "command";
|
|
13
|
+
action: "new" | "reset";
|
|
14
|
+
context: FeishuBeforeResetContext;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type FeishuBeforeResetRunner = {
|
|
18
|
+
runBeforeReset: (
|
|
19
|
+
event: FeishuBeforeResetEvent,
|
|
20
|
+
ctx: { agentId: string; sessionKey: string },
|
|
21
|
+
) => Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Handle Feishu command messages and trigger reset hooks.
|
|
26
|
+
*/
|
|
27
|
+
export async function handleFeishuCommand(
|
|
28
|
+
messageText: string,
|
|
29
|
+
sessionKey: string,
|
|
30
|
+
hookRunner: FeishuBeforeResetRunner,
|
|
31
|
+
context: FeishuBeforeResetContext,
|
|
32
|
+
): Promise<boolean> {
|
|
33
|
+
const trimmed = messageText.trim().toLowerCase();
|
|
34
|
+
const isResetCommand = DEFAULT_RESET_TRIGGERS.some(
|
|
35
|
+
(trigger) => trimmed === trigger || trimmed.startsWith(`${trigger} `),
|
|
36
|
+
);
|
|
37
|
+
if (!isResetCommand) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const command = trimmed.split(" ")[0];
|
|
42
|
+
const action: "new" | "reset" = command === "/new" ? "new" : "reset";
|
|
43
|
+
await hookRunner.runBeforeReset(
|
|
44
|
+
{
|
|
45
|
+
type: "command",
|
|
46
|
+
action,
|
|
47
|
+
context: {
|
|
48
|
+
...context,
|
|
49
|
+
commandSource: "feishu",
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
agentId: "main",
|
|
54
|
+
sessionKey,
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return true;
|
|
59
|
+
}
|