@openclaw/feishu 2026.3.13 → 2026.5.2-beta.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/api.ts +31 -0
- package/channel-entry.ts +20 -0
- package/channel-plugin-api.ts +1 -0
- package/contract-api.ts +16 -0
- package/index.ts +70 -53
- package/openclaw.plugin.json +1827 -4
- package/package.json +32 -7
- package/runtime-api.ts +55 -0
- package/secret-contract-api.ts +5 -0
- package/security-contract-api.ts +1 -0
- package/session-key-api.ts +1 -0
- package/setup-api.ts +3 -0
- package/setup-entry.test.ts +14 -0
- package/setup-entry.ts +13 -0
- package/src/accounts.test.ts +95 -7
- package/src/accounts.ts +199 -117
- package/src/app-registration.ts +331 -0
- package/src/approval-auth.test.ts +24 -0
- package/src/approval-auth.ts +25 -0
- package/src/async.test.ts +35 -0
- package/src/async.ts +43 -1
- package/src/audio-preflight.runtime.ts +9 -0
- package/src/bitable.test.ts +131 -0
- package/src/bitable.ts +59 -22
- package/src/bot-content.ts +474 -0
- package/src/bot-group-name.test.ts +108 -0
- package/src/bot-runtime-api.ts +12 -0
- package/src/bot-sender-name.ts +125 -0
- package/src/bot.broadcast.test.ts +463 -0
- package/src/bot.card-action.test.ts +519 -5
- package/src/bot.checkBotMentioned.test.ts +92 -20
- package/src/bot.helpers.test.ts +118 -0
- package/src/bot.stripBotMention.test.ts +13 -21
- package/src/bot.test.ts +1334 -401
- package/src/bot.ts +778 -775
- package/src/card-action.ts +408 -40
- package/src/card-interaction.test.ts +129 -0
- package/src/card-interaction.ts +159 -0
- package/src/card-test-helpers.ts +47 -0
- package/src/card-ux-approval.ts +65 -0
- package/src/card-ux-launcher.test.ts +99 -0
- package/src/card-ux-launcher.ts +121 -0
- package/src/card-ux-shared.ts +33 -0
- package/src/channel-runtime-api.ts +16 -0
- package/src/channel.runtime.ts +47 -0
- package/src/channel.test.ts +914 -3
- package/src/channel.ts +1253 -309
- package/src/chat-schema.ts +5 -4
- package/src/chat.test.ts +135 -28
- package/src/chat.ts +68 -10
- package/src/client.test.ts +212 -103
- package/src/client.ts +115 -21
- package/src/comment-dispatcher-runtime-api.ts +6 -0
- package/src/comment-dispatcher.test.ts +169 -0
- package/src/comment-dispatcher.ts +107 -0
- package/src/comment-handler-runtime-api.ts +3 -0
- package/src/comment-handler.test.ts +486 -0
- package/src/comment-handler.ts +309 -0
- package/src/comment-reaction.test.ts +166 -0
- package/src/comment-reaction.ts +259 -0
- package/src/comment-shared.test.ts +182 -0
- package/src/comment-shared.ts +406 -0
- package/src/comment-target.ts +44 -0
- package/src/config-schema.test.ts +63 -1
- package/src/config-schema.ts +31 -4
- package/src/conversation-id.test.ts +18 -0
- package/src/conversation-id.ts +199 -0
- package/src/dedup-runtime-api.ts +1 -0
- package/src/dedup.ts +33 -95
- package/src/directory.static.ts +61 -0
- package/src/directory.test.ts +116 -20
- package/src/directory.ts +60 -92
- package/src/doc-schema.ts +1 -1
- package/src/docx-batch-insert.test.ts +39 -38
- package/src/docx-batch-insert.ts +55 -19
- package/src/docx-color-text.ts +9 -4
- package/src/docx-table-ops.test.ts +53 -0
- package/src/docx-table-ops.ts +52 -34
- package/src/docx-types.ts +38 -0
- package/src/docx.account-selection.test.ts +12 -3
- package/src/docx.test.ts +314 -74
- package/src/docx.ts +278 -122
- package/src/drive-schema.ts +47 -1
- package/src/drive.test.ts +1219 -0
- package/src/drive.ts +614 -13
- package/src/dynamic-agent.ts +10 -4
- package/src/event-types.ts +45 -0
- package/src/external-keys.ts +1 -1
- package/src/lifecycle.test-support.ts +220 -0
- package/src/media.test.ts +403 -26
- package/src/media.ts +509 -132
- package/src/mention-target.types.ts +5 -0
- package/src/mention.ts +32 -51
- package/src/message-action-contract.ts +13 -0
- package/src/monitor-state-runtime-api.ts +7 -0
- package/src/monitor-transport-runtime-api.ts +7 -0
- package/src/monitor.account.ts +218 -312
- package/src/monitor.acp-init-failure.lifecycle.test-support.ts +219 -0
- package/src/monitor.bot-identity.ts +86 -0
- package/src/monitor.bot-menu-handler.ts +165 -0
- package/src/monitor.bot-menu.lifecycle.test-support.ts +224 -0
- package/src/monitor.bot-menu.test.ts +178 -0
- package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +264 -0
- package/src/monitor.card-action.lifecycle.test-support.ts +373 -0
- package/src/monitor.cleanup.test.ts +376 -0
- package/src/monitor.comment-notice-handler.ts +105 -0
- package/src/monitor.comment.test.ts +937 -0
- package/src/monitor.comment.ts +1386 -0
- package/src/monitor.lifecycle.test.ts +4 -0
- package/src/monitor.message-handler.ts +339 -0
- package/src/monitor.reaction.lifecycle.test-support.ts +68 -0
- package/src/monitor.reaction.test.ts +108 -48
- package/src/monitor.startup.test.ts +11 -9
- package/src/monitor.startup.ts +26 -16
- package/src/monitor.state.ts +20 -5
- package/src/monitor.synthetic-error.ts +18 -0
- package/src/monitor.test-mocks.ts +2 -2
- package/src/monitor.transport.ts +220 -60
- package/src/monitor.ts +15 -10
- package/src/monitor.webhook-e2e.test.ts +65 -7
- package/src/monitor.webhook-security.test.ts +122 -0
- package/src/monitor.webhook.test-helpers.ts +44 -26
- package/src/outbound-runtime-api.ts +1 -0
- package/src/outbound.test.ts +616 -37
- package/src/outbound.ts +623 -81
- package/src/perm-schema.ts +1 -1
- package/src/perm.ts +1 -7
- package/src/pins.ts +108 -0
- package/src/policy.test.ts +297 -117
- package/src/policy.ts +142 -29
- package/src/post.ts +7 -6
- package/src/probe.test.ts +14 -9
- package/src/probe.ts +26 -16
- package/src/processing-claims.ts +59 -0
- package/src/qr-terminal.ts +1 -0
- package/src/reactions.ts +4 -34
- package/src/reasoning-preview.test.ts +59 -0
- package/src/reasoning-preview.ts +20 -0
- package/src/reply-dispatcher-runtime-api.ts +7 -0
- package/src/reply-dispatcher.test.ts +660 -29
- package/src/reply-dispatcher.ts +407 -154
- package/src/runtime.ts +6 -3
- package/src/secret-contract.ts +145 -0
- package/src/secret-input.ts +1 -13
- package/src/security-audit-shared.ts +69 -0
- package/src/security-audit.test.ts +61 -0
- package/src/security-audit.ts +1 -0
- package/src/send-result.ts +1 -1
- package/src/send-target.test.ts +9 -3
- package/src/send-target.ts +10 -4
- package/src/send.reply-fallback.test.ts +105 -2
- package/src/send.test.ts +386 -4
- package/src/send.ts +414 -95
- package/src/sequential-key.test.ts +72 -0
- package/src/sequential-key.ts +28 -0
- package/src/sequential-queue.test.ts +92 -0
- package/src/sequential-queue.ts +16 -0
- package/src/session-conversation.ts +42 -0
- package/src/session-route.ts +48 -0
- package/src/setup-core.ts +51 -0
- package/src/{onboarding.test.ts → setup-surface.test.ts} +52 -21
- package/src/setup-surface.ts +581 -0
- package/src/streaming-card.test.ts +138 -2
- package/src/streaming-card.ts +134 -18
- package/src/subagent-hooks.test.ts +603 -0
- package/src/subagent-hooks.ts +397 -0
- package/src/targets.ts +3 -13
- package/src/test-support/lifecycle-test-support.ts +453 -0
- package/src/thread-bindings.test.ts +143 -0
- package/src/thread-bindings.ts +330 -0
- package/src/tool-account-routing.test.ts +66 -8
- package/src/tool-account.test.ts +44 -0
- package/src/tool-account.ts +40 -17
- package/src/tool-factory-test-harness.ts +11 -8
- package/src/tool-result.ts +3 -1
- package/src/tools-config.ts +1 -1
- package/src/types.ts +16 -15
- package/src/typing.ts +10 -6
- package/src/wiki-schema.ts +1 -1
- package/src/wiki.ts +1 -7
- package/subagent-hooks-api.ts +31 -0
- package/tsconfig.json +16 -0
- package/src/feishu-command-handler.ts +0 -59
- package/src/onboarding.status.test.ts +0 -25
- package/src/onboarding.ts +0 -489
- package/src/send-message.ts +0 -71
- package/src/targets.test.ts +0 -70
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feishu app registration via OAuth device-code flow.
|
|
3
|
+
*
|
|
4
|
+
* Migrated from feishu-plugin-cli's `feishu-auth.ts` and `install-prompts.ts`.
|
|
5
|
+
* Replaces axios with native fetch, removes inquirer/ora/chalk in favor of
|
|
6
|
+
* the openclaw WizardPrompter surface.
|
|
7
|
+
*/
|
|
8
|
+
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
9
|
+
import { renderQrTerminal } from "./qr-terminal.js";
|
|
10
|
+
import type { FeishuDomain } from "./types.js";
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Constants
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
const FEISHU_ACCOUNTS_URL = "https://accounts.feishu.cn";
|
|
17
|
+
const LARK_ACCOUNTS_URL = "https://accounts.larksuite.com";
|
|
18
|
+
|
|
19
|
+
const REGISTRATION_PATH = "/oauth/v1/app/registration";
|
|
20
|
+
|
|
21
|
+
const REQUEST_TIMEOUT_MS = 10_000;
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Types
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
export interface AppRegistrationResult {
|
|
28
|
+
appId: string;
|
|
29
|
+
appSecret: string;
|
|
30
|
+
domain: FeishuDomain;
|
|
31
|
+
openId?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface InitResponse {
|
|
35
|
+
nonce: string;
|
|
36
|
+
supported_auth_methods: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface BeginResult {
|
|
40
|
+
deviceCode: string;
|
|
41
|
+
qrUrl: string;
|
|
42
|
+
userCode: string;
|
|
43
|
+
interval: number;
|
|
44
|
+
expireIn: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface RawBeginResponse {
|
|
48
|
+
device_code: string;
|
|
49
|
+
verification_uri: string;
|
|
50
|
+
user_code: string;
|
|
51
|
+
verification_uri_complete: string;
|
|
52
|
+
interval: number;
|
|
53
|
+
expire_in: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface PollResponse {
|
|
57
|
+
client_id?: string;
|
|
58
|
+
client_secret?: string;
|
|
59
|
+
user_info?: {
|
|
60
|
+
open_id?: string;
|
|
61
|
+
tenant_brand?: "feishu" | "lark";
|
|
62
|
+
};
|
|
63
|
+
error?: string;
|
|
64
|
+
error_description?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type PollOutcome =
|
|
68
|
+
| { status: "success"; result: AppRegistrationResult }
|
|
69
|
+
| { status: "access_denied" }
|
|
70
|
+
| { status: "expired" }
|
|
71
|
+
| { status: "timeout" }
|
|
72
|
+
| { status: "error"; message: string };
|
|
73
|
+
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
// Helpers
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
|
|
78
|
+
function accountsBaseUrl(domain: FeishuDomain): string {
|
|
79
|
+
return domain === "lark" ? LARK_ACCOUNTS_URL : FEISHU_ACCOUNTS_URL;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function postRegistration<T>(baseUrl: string, body: Record<string, string>): Promise<T> {
|
|
83
|
+
return await fetchFeishuJson<T>({
|
|
84
|
+
url: `${baseUrl}${REGISTRATION_PATH}`,
|
|
85
|
+
init: {
|
|
86
|
+
method: "POST",
|
|
87
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
88
|
+
body: new URLSearchParams(body).toString(),
|
|
89
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
90
|
+
},
|
|
91
|
+
auditContext: "feishu.app-registration.post",
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function fetchFeishuJson<T>(params: {
|
|
96
|
+
url: string;
|
|
97
|
+
init: RequestInit;
|
|
98
|
+
auditContext: string;
|
|
99
|
+
}): Promise<T> {
|
|
100
|
+
const { response, release } = await fetchWithSsrFGuard({
|
|
101
|
+
url: params.url,
|
|
102
|
+
init: params.init,
|
|
103
|
+
policy: { allowedHostnames: [new URL(params.url).hostname] },
|
|
104
|
+
auditContext: params.auditContext,
|
|
105
|
+
});
|
|
106
|
+
try {
|
|
107
|
+
// Registration poll returns 4xx for pending/error states with a JSON body.
|
|
108
|
+
return (await response.json()) as T;
|
|
109
|
+
} finally {
|
|
110
|
+
await release();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// Public API
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Step 1: Initialize registration and verify the environment supports
|
|
120
|
+
* `client_secret` auth.
|
|
121
|
+
*
|
|
122
|
+
* @throws If the environment does not support `client_secret`.
|
|
123
|
+
*/
|
|
124
|
+
export async function initAppRegistration(domain: FeishuDomain = "feishu"): Promise<void> {
|
|
125
|
+
const baseUrl = accountsBaseUrl(domain);
|
|
126
|
+
const res = await postRegistration<InitResponse>(baseUrl, { action: "init" });
|
|
127
|
+
|
|
128
|
+
if (!res.supported_auth_methods?.includes("client_secret")) {
|
|
129
|
+
throw new Error("Current environment does not support client_secret auth method");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Step 2: Begin the device-code flow. Returns a device code and a QR URL
|
|
135
|
+
* that the user should scan with Feishu/Lark mobile app.
|
|
136
|
+
*/
|
|
137
|
+
export async function beginAppRegistration(domain: FeishuDomain = "feishu"): Promise<BeginResult> {
|
|
138
|
+
const baseUrl = accountsBaseUrl(domain);
|
|
139
|
+
const res = await postRegistration<RawBeginResponse>(baseUrl, {
|
|
140
|
+
action: "begin",
|
|
141
|
+
archetype: "PersonalAgent",
|
|
142
|
+
auth_method: "client_secret",
|
|
143
|
+
request_user_info: "open_id",
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const qrUrl = new URL(res.verification_uri_complete);
|
|
147
|
+
qrUrl.searchParams.set("from", "oc_onboard");
|
|
148
|
+
qrUrl.searchParams.set("tp", "ob_cli_app");
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
deviceCode: res.device_code,
|
|
152
|
+
qrUrl: qrUrl.toString(),
|
|
153
|
+
userCode: res.user_code,
|
|
154
|
+
interval: res.interval || 5,
|
|
155
|
+
expireIn: res.expire_in || 600,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Step 3: Poll for authorization result until success, denial, expiry, or
|
|
161
|
+
* timeout. Automatically handles domain switching when `tenant_brand` is
|
|
162
|
+
* detected as "lark".
|
|
163
|
+
*/
|
|
164
|
+
export async function pollAppRegistration(params: {
|
|
165
|
+
deviceCode: string;
|
|
166
|
+
interval: number;
|
|
167
|
+
expireIn: number;
|
|
168
|
+
initialDomain?: FeishuDomain;
|
|
169
|
+
abortSignal?: AbortSignal;
|
|
170
|
+
/** Registration type parameter: "ob_user" for user mode, "ob_app" for bot mode. */
|
|
171
|
+
tp?: string;
|
|
172
|
+
}): Promise<PollOutcome> {
|
|
173
|
+
const { deviceCode, expireIn, initialDomain = "feishu", abortSignal, tp } = params;
|
|
174
|
+
let currentInterval = params.interval;
|
|
175
|
+
let domain: FeishuDomain = initialDomain;
|
|
176
|
+
let domainSwitched = false;
|
|
177
|
+
|
|
178
|
+
const deadline = Date.now() + expireIn * 1000;
|
|
179
|
+
|
|
180
|
+
while (Date.now() < deadline) {
|
|
181
|
+
if (abortSignal?.aborted) {
|
|
182
|
+
return { status: "timeout" };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const baseUrl = accountsBaseUrl(domain);
|
|
186
|
+
|
|
187
|
+
let pollRes: PollResponse;
|
|
188
|
+
try {
|
|
189
|
+
pollRes = await postRegistration<PollResponse>(baseUrl, {
|
|
190
|
+
action: "poll",
|
|
191
|
+
device_code: deviceCode,
|
|
192
|
+
...(tp ? { tp } : {}),
|
|
193
|
+
});
|
|
194
|
+
} catch {
|
|
195
|
+
// Transient network error — keep polling.
|
|
196
|
+
await sleep(currentInterval * 1000);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Domain auto-detection: switch to lark if tenant_brand says so.
|
|
201
|
+
if (pollRes.user_info?.tenant_brand) {
|
|
202
|
+
const isLark = pollRes.user_info.tenant_brand === "lark";
|
|
203
|
+
if (!domainSwitched && isLark) {
|
|
204
|
+
domain = "lark";
|
|
205
|
+
domainSwitched = true;
|
|
206
|
+
// Retry poll immediately with the correct domain.
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Success.
|
|
212
|
+
if (pollRes.client_id && pollRes.client_secret) {
|
|
213
|
+
return {
|
|
214
|
+
status: "success",
|
|
215
|
+
result: {
|
|
216
|
+
appId: pollRes.client_id,
|
|
217
|
+
appSecret: pollRes.client_secret,
|
|
218
|
+
domain,
|
|
219
|
+
openId: pollRes.user_info?.open_id,
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Error handling.
|
|
225
|
+
if (pollRes.error) {
|
|
226
|
+
if (pollRes.error === "authorization_pending") {
|
|
227
|
+
// Continue waiting.
|
|
228
|
+
} else if (pollRes.error === "slow_down") {
|
|
229
|
+
currentInterval += 5;
|
|
230
|
+
} else if (pollRes.error === "access_denied") {
|
|
231
|
+
return { status: "access_denied" };
|
|
232
|
+
} else if (pollRes.error === "expired_token") {
|
|
233
|
+
return { status: "expired" };
|
|
234
|
+
} else {
|
|
235
|
+
return {
|
|
236
|
+
status: "error",
|
|
237
|
+
message: `${pollRes.error}: ${pollRes.error_description ?? "unknown"}`,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
await sleep(currentInterval * 1000);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return { status: "timeout" };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Print QR code directly to stdout.
|
|
250
|
+
*
|
|
251
|
+
* QR codes must be printed without any surrounding box/border decoration,
|
|
252
|
+
* otherwise the pattern is corrupted and cannot be scanned.
|
|
253
|
+
*/
|
|
254
|
+
export async function printQrCode(url: string): Promise<void> {
|
|
255
|
+
const output = await renderQrTerminal(url, { small: true });
|
|
256
|
+
process.stdout.write(output.endsWith("\n") ? output : `${output}\n`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Fetch the app owner's open_id using the application.v6.application.get API.
|
|
261
|
+
*
|
|
262
|
+
* Used during setup to auto-populate security policy allowlists.
|
|
263
|
+
* Returns undefined on any failure (fail-open).
|
|
264
|
+
*/
|
|
265
|
+
export async function getAppOwnerOpenId(params: {
|
|
266
|
+
appId: string;
|
|
267
|
+
appSecret: string;
|
|
268
|
+
domain?: FeishuDomain;
|
|
269
|
+
}): Promise<string | undefined> {
|
|
270
|
+
const baseUrl =
|
|
271
|
+
params.domain === "lark" ? "https://open.larksuite.com" : "https://open.feishu.cn";
|
|
272
|
+
|
|
273
|
+
try {
|
|
274
|
+
// First, get a tenant_access_token.
|
|
275
|
+
const tokenData = await fetchFeishuJson<{
|
|
276
|
+
code?: number;
|
|
277
|
+
tenant_access_token?: string;
|
|
278
|
+
}>({
|
|
279
|
+
url: `${baseUrl}/open-apis/auth/v3/tenant_access_token/internal`,
|
|
280
|
+
init: {
|
|
281
|
+
method: "POST",
|
|
282
|
+
headers: { "Content-Type": "application/json" },
|
|
283
|
+
body: JSON.stringify({ app_id: params.appId, app_secret: params.appSecret }),
|
|
284
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
285
|
+
},
|
|
286
|
+
auditContext: "feishu.app-registration.owner-token",
|
|
287
|
+
});
|
|
288
|
+
if (!tokenData.tenant_access_token) {
|
|
289
|
+
return undefined;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Query app info for the owner's open_id.
|
|
293
|
+
const appData = await fetchFeishuJson<{
|
|
294
|
+
code?: number;
|
|
295
|
+
data?: {
|
|
296
|
+
app?: {
|
|
297
|
+
owner?: { owner_id?: string; owner_type?: number; type?: number };
|
|
298
|
+
creator_id?: string;
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
}>({
|
|
302
|
+
url: `${baseUrl}/open-apis/application/v6/applications/${params.appId}?user_id_type=open_id`,
|
|
303
|
+
init: {
|
|
304
|
+
method: "GET",
|
|
305
|
+
headers: {
|
|
306
|
+
Authorization: `Bearer ${tokenData.tenant_access_token}`,
|
|
307
|
+
"Content-Type": "application/json",
|
|
308
|
+
},
|
|
309
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
310
|
+
},
|
|
311
|
+
auditContext: "feishu.app-registration.owner-app",
|
|
312
|
+
});
|
|
313
|
+
if (appData.code !== 0) {
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const app = appData.data?.app;
|
|
318
|
+
const owner = app?.owner;
|
|
319
|
+
const ownerType = owner?.owner_type ?? owner?.type;
|
|
320
|
+
// owner_type=2 means enterprise member; use owner_id. Otherwise fallback to creator_id.
|
|
321
|
+
return ownerType === 2 && owner?.owner_id
|
|
322
|
+
? owner.owner_id
|
|
323
|
+
: (app?.creator_id ?? owner?.owner_id);
|
|
324
|
+
} catch {
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function sleep(ms: number): Promise<void> {
|
|
330
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
331
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { feishuApprovalAuth } from "./approval-auth.js";
|
|
3
|
+
|
|
4
|
+
describe("feishuApprovalAuth", () => {
|
|
5
|
+
it("authorizes open_id approvers and ignores user_id-only allowlists", () => {
|
|
6
|
+
expect(
|
|
7
|
+
feishuApprovalAuth.authorizeActorAction({
|
|
8
|
+
cfg: { channels: { feishu: { allowFrom: ["ou_owner"] } } },
|
|
9
|
+
senderId: "ou_owner",
|
|
10
|
+
action: "approve",
|
|
11
|
+
approvalKind: "exec",
|
|
12
|
+
}),
|
|
13
|
+
).toEqual({ authorized: true });
|
|
14
|
+
|
|
15
|
+
expect(
|
|
16
|
+
feishuApprovalAuth.authorizeActorAction({
|
|
17
|
+
cfg: { channels: { feishu: { allowFrom: ["user_123"] } } },
|
|
18
|
+
senderId: "ou_attacker",
|
|
19
|
+
action: "approve",
|
|
20
|
+
approvalKind: "exec",
|
|
21
|
+
}),
|
|
22
|
+
).toEqual({ authorized: true });
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createResolvedApproverActionAuthAdapter,
|
|
3
|
+
resolveApprovalApprovers,
|
|
4
|
+
} from "openclaw/plugin-sdk/approval-auth-runtime";
|
|
5
|
+
import { normalizeOptionalLowercaseString } from "openclaw/plugin-sdk/text-runtime";
|
|
6
|
+
import { resolveFeishuAccount } from "./accounts.js";
|
|
7
|
+
import { normalizeFeishuTarget } from "./targets.js";
|
|
8
|
+
|
|
9
|
+
function normalizeFeishuApproverId(value: string | number): string | undefined {
|
|
10
|
+
const normalized = normalizeFeishuTarget(String(value));
|
|
11
|
+
const trimmed = normalizeOptionalLowercaseString(normalized);
|
|
12
|
+
return trimmed?.startsWith("ou_") ? trimmed : undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const feishuApprovalAuth = createResolvedApproverActionAuthAdapter({
|
|
16
|
+
channelLabel: "Feishu",
|
|
17
|
+
resolveApprovers: ({ cfg, accountId }) => {
|
|
18
|
+
const account = resolveFeishuAccount({ cfg, accountId }).config;
|
|
19
|
+
return resolveApprovalApprovers({
|
|
20
|
+
allowFrom: account.allowFrom,
|
|
21
|
+
normalizeApprover: normalizeFeishuApproverId,
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
normalizeSenderId: (value) => normalizeFeishuApproverId(value),
|
|
25
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { waitForAbortableDelay } from "./async.js";
|
|
3
|
+
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
vi.useRealTimers();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe("waitForAbortableDelay", () => {
|
|
9
|
+
it("resolves false immediately when already aborted", async () => {
|
|
10
|
+
vi.useFakeTimers();
|
|
11
|
+
const abortController = new AbortController();
|
|
12
|
+
abortController.abort();
|
|
13
|
+
|
|
14
|
+
await expect(waitForAbortableDelay(60_000, abortController.signal)).resolves.toBe(false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("resolves false immediately when aborted during backoff", async () => {
|
|
18
|
+
vi.useFakeTimers();
|
|
19
|
+
const abortController = new AbortController();
|
|
20
|
+
|
|
21
|
+
const delay = waitForAbortableDelay(60_000, abortController.signal);
|
|
22
|
+
abortController.abort();
|
|
23
|
+
|
|
24
|
+
await expect(delay).resolves.toBe(false);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("resolves true after the full delay when not aborted", async () => {
|
|
28
|
+
vi.useFakeTimers();
|
|
29
|
+
|
|
30
|
+
const delay = waitForAbortableDelay(500);
|
|
31
|
+
await vi.advanceTimersByTimeAsync(500);
|
|
32
|
+
|
|
33
|
+
await expect(delay).resolves.toBe(true);
|
|
34
|
+
});
|
|
35
|
+
});
|
package/src/async.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const RACE_TIMEOUT = Symbol("race-timeout");
|
|
2
2
|
const RACE_ABORT = Symbol("race-abort");
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
type RaceWithTimeoutAndAbortResult<T> =
|
|
5
5
|
| { status: "resolved"; value: T }
|
|
6
6
|
| { status: "timeout" }
|
|
7
7
|
| { status: "aborted" };
|
|
@@ -60,3 +60,45 @@ export async function raceWithTimeoutAndAbort<T>(
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
+
|
|
64
|
+
export function waitForAbortableDelay(
|
|
65
|
+
delayMs: number,
|
|
66
|
+
abortSignal?: AbortSignal,
|
|
67
|
+
): Promise<boolean> {
|
|
68
|
+
if (abortSignal?.aborted) {
|
|
69
|
+
return Promise.resolve(false);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return new Promise((resolve) => {
|
|
73
|
+
let settled = false;
|
|
74
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
75
|
+
let handleAbort: (() => void) | undefined;
|
|
76
|
+
|
|
77
|
+
const finish = (value: boolean) => {
|
|
78
|
+
if (settled) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
settled = true;
|
|
82
|
+
if (timer) {
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
}
|
|
85
|
+
if (handleAbort) {
|
|
86
|
+
abortSignal?.removeEventListener("abort", handleAbort);
|
|
87
|
+
}
|
|
88
|
+
resolve(value);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
handleAbort = () => {
|
|
92
|
+
finish(false);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
abortSignal?.addEventListener("abort", handleAbort, { once: true });
|
|
96
|
+
if (abortSignal?.aborted) {
|
|
97
|
+
finish(false);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
timer = setTimeout(() => finish(true), delayMs);
|
|
102
|
+
timer.unref?.();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { transcribeFirstAudio as transcribeFirstAudioImpl } from "openclaw/plugin-sdk/media-runtime";
|
|
2
|
+
|
|
3
|
+
type TranscribeFirstAudio = typeof import("openclaw/plugin-sdk/media-runtime").transcribeFirstAudio;
|
|
4
|
+
|
|
5
|
+
export async function transcribeFirstAudio(
|
|
6
|
+
...args: Parameters<TranscribeFirstAudio>
|
|
7
|
+
): ReturnType<TranscribeFirstAudio> {
|
|
8
|
+
return await transcribeFirstAudioImpl(...args);
|
|
9
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
2
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import type { OpenClawPluginApi } from "../runtime-api.js";
|
|
4
|
+
import { createToolFactoryHarness } from "./tool-factory-test-harness.js";
|
|
5
|
+
|
|
6
|
+
const createFeishuClientMock = vi.hoisted(() => vi.fn());
|
|
7
|
+
|
|
8
|
+
vi.mock("./client.js", () => ({
|
|
9
|
+
createFeishuClient: createFeishuClientMock,
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
import { registerFeishuBitableTools } from "./bitable.js";
|
|
13
|
+
|
|
14
|
+
type MockRecord = {
|
|
15
|
+
record_id?: string;
|
|
16
|
+
fields?: Record<string, unknown>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function createConfig(): OpenClawPluginApi["config"] {
|
|
20
|
+
return {
|
|
21
|
+
channels: {
|
|
22
|
+
feishu: {
|
|
23
|
+
enabled: true,
|
|
24
|
+
accounts: {
|
|
25
|
+
default: {
|
|
26
|
+
appId: "cli_default",
|
|
27
|
+
appSecret: "secret_default", // pragma: allowlist secret
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
} as OpenClawPluginApi["config"];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function createBitableClient(records: MockRecord[]) {
|
|
36
|
+
const batchDelete = vi.fn(async () => ({ code: 0 }));
|
|
37
|
+
const client = {
|
|
38
|
+
bitable: {
|
|
39
|
+
app: {
|
|
40
|
+
create: vi.fn(async () => ({
|
|
41
|
+
code: 0,
|
|
42
|
+
data: {
|
|
43
|
+
app: {
|
|
44
|
+
app_token: "app_token",
|
|
45
|
+
name: "Project Tracker",
|
|
46
|
+
url: "https://example.feishu.cn/base/app_token",
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
})),
|
|
50
|
+
},
|
|
51
|
+
appTable: {
|
|
52
|
+
list: vi.fn(async () => ({
|
|
53
|
+
code: 0,
|
|
54
|
+
data: { items: [{ table_id: "tbl_main", name: "Table 1" }] },
|
|
55
|
+
})),
|
|
56
|
+
},
|
|
57
|
+
appTableField: {
|
|
58
|
+
list: vi.fn(async () => ({ code: 0, data: { items: [] } })),
|
|
59
|
+
update: vi.fn(async () => ({ code: 0 })),
|
|
60
|
+
delete: vi.fn(async () => ({ code: 0 })),
|
|
61
|
+
},
|
|
62
|
+
appTableRecord: {
|
|
63
|
+
list: vi.fn(async () => ({ code: 0, data: { items: records } })),
|
|
64
|
+
batchDelete,
|
|
65
|
+
delete: vi.fn(async () => ({ code: 0 })),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
} as unknown as Lark.Client;
|
|
69
|
+
|
|
70
|
+
return { batchDelete, client };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe("feishu bitable create app cleanup", () => {
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
createFeishuClientMock.mockReset();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("deletes placeholder rows whose fields contain only default empty values", async () => {
|
|
79
|
+
const { batchDelete, client } = createBitableClient([
|
|
80
|
+
{ record_id: "rec_missing_fields" },
|
|
81
|
+
{ record_id: "rec_empty_fields", fields: {} },
|
|
82
|
+
{
|
|
83
|
+
record_id: "rec_empty_defaults",
|
|
84
|
+
fields: {
|
|
85
|
+
Name: "",
|
|
86
|
+
Status: [],
|
|
87
|
+
Attachments: [],
|
|
88
|
+
Started: null,
|
|
89
|
+
EmptyObject: {},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
record_id: "rec_empty_rich_text",
|
|
94
|
+
fields: { Notes: [{ type: "text", text: "" }] },
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
record_id: "rec_empty_nested",
|
|
98
|
+
fields: { Notes: { value: "", segments: [{ type: "text", text: "" }] } },
|
|
99
|
+
},
|
|
100
|
+
{ record_id: "rec_text", fields: { Name: "Milestone" } },
|
|
101
|
+
{ record_id: "rec_number", fields: { Estimate: 0 } },
|
|
102
|
+
{ record_id: "rec_boolean", fields: { Done: false } },
|
|
103
|
+
{ record_id: "rec_link", fields: { Link: { text: "", link: "https://example.com" } } },
|
|
104
|
+
{ record_id: "rec_attachment", fields: { Attachments: [{ file_token: "boxcn_token" }] } },
|
|
105
|
+
{ record_id: "rec_user", fields: { Assignee: [{ id: "ou_1", name: "" }] } },
|
|
106
|
+
{ record_id: "rec_location", fields: { Location: { name: "", location: "116,39" } } },
|
|
107
|
+
]);
|
|
108
|
+
createFeishuClientMock.mockReturnValue(client);
|
|
109
|
+
|
|
110
|
+
const { api, resolveTool } = createToolFactoryHarness(createConfig());
|
|
111
|
+
registerFeishuBitableTools(api);
|
|
112
|
+
|
|
113
|
+
const result = await resolveTool("feishu_bitable_create_app").execute("call", {
|
|
114
|
+
name: "Project Tracker",
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expect(result.details.cleaned_placeholder_rows).toBe(5);
|
|
118
|
+
expect(batchDelete).toHaveBeenCalledWith({
|
|
119
|
+
path: { app_token: "app_token", table_id: "tbl_main" },
|
|
120
|
+
data: {
|
|
121
|
+
records: [
|
|
122
|
+
"rec_missing_fields",
|
|
123
|
+
"rec_empty_fields",
|
|
124
|
+
"rec_empty_defaults",
|
|
125
|
+
"rec_empty_rich_text",
|
|
126
|
+
"rec_empty_nested",
|
|
127
|
+
],
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
});
|