@actagent/feishu 2026.6.2
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 +11 -0
- package/actagent.plugin.json +224 -0
- package/api.ts +33 -0
- package/channel-entry.ts +21 -0
- package/channel-plugin-api.ts +2 -0
- package/contract-api.ts +17 -0
- package/index.ts +83 -0
- package/legacy-state-migrations-api.ts +2 -0
- package/npm-shrinkwrap.json +539 -0
- package/package.json +64 -0
- package/runtime-api.ts +58 -0
- package/runtime-setter-api.ts +3 -0
- package/secret-contract-api.ts +6 -0
- package/security-contract-api.ts +2 -0
- package/session-key-api.ts +2 -0
- package/setup-api.ts +4 -0
- package/setup-entry.test.ts +33 -0
- package/setup-entry.ts +25 -0
- 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 +113 -0
- package/src/accounts.test.ts +481 -0
- package/src/accounts.ts +380 -0
- package/src/agent-config.ts +22 -0
- package/src/app-registration.test.ts +62 -0
- package/src/app-registration.ts +355 -0
- package/src/approval-auth.test.ts +25 -0
- package/src/approval-auth.ts +26 -0
- package/src/async.test.ts +68 -0
- package/src/async.ts +109 -0
- package/src/audio-preflight.runtime.ts +10 -0
- package/src/bitable.test.ts +174 -0
- package/src/bitable.ts +781 -0
- package/src/bot-content.ts +488 -0
- package/src/bot-group-name.test.ts +148 -0
- package/src/bot-runtime-api.ts +13 -0
- package/src/bot-sender-name.test.ts +68 -0
- package/src/bot-sender-name.ts +137 -0
- package/src/bot.broadcast.test.ts +643 -0
- package/src/bot.card-action.test.ts +647 -0
- package/src/bot.checkBotMentioned.test.ts +266 -0
- package/src/bot.helpers.test.ts +136 -0
- package/src/bot.stripBotMention.test.ts +127 -0
- package/src/bot.test.ts +3817 -0
- package/src/bot.ts +1788 -0
- package/src/card-action.ts +515 -0
- package/src/card-interaction.test.ts +132 -0
- package/src/card-interaction.ts +160 -0
- package/src/card-test-helpers.ts +55 -0
- package/src/card-ux-approval.ts +66 -0
- package/src/card-ux-launcher.test.ts +126 -0
- package/src/card-ux-launcher.ts +136 -0
- package/src/card-ux-shared.ts +34 -0
- package/src/channel-runtime-api.ts +17 -0
- package/src/channel.runtime.ts +48 -0
- package/src/channel.test.ts +1337 -0
- package/src/channel.ts +1401 -0
- package/src/chat-schema.ts +30 -0
- package/src/chat.test.ts +295 -0
- package/src/chat.ts +198 -0
- package/src/client-timeout.ts +44 -0
- package/src/client.test.ts +463 -0
- package/src/client.ts +263 -0
- package/src/comment-dispatcher-runtime-api.ts +7 -0
- package/src/comment-dispatcher.test.ts +186 -0
- package/src/comment-dispatcher.ts +108 -0
- package/src/comment-handler-runtime-api.ts +4 -0
- package/src/comment-handler.test.ts +588 -0
- package/src/comment-handler.ts +304 -0
- package/src/comment-reaction.test.ts +139 -0
- package/src/comment-reaction.ts +260 -0
- package/src/comment-shared.test.ts +184 -0
- package/src/comment-shared.ts +405 -0
- package/src/comment-target.ts +45 -0
- package/src/config-schema.test.ts +327 -0
- package/src/config-schema.ts +338 -0
- package/src/conversation-id.test.ts +19 -0
- package/src/conversation-id.ts +199 -0
- package/src/dedup-migrations.test.ts +90 -0
- package/src/dedup-migrations.ts +103 -0
- package/src/dedup.test.ts +95 -0
- package/src/dedup.ts +304 -0
- package/src/dedupe-key.ts +68 -0
- package/src/directory.static.ts +62 -0
- package/src/directory.test.ts +142 -0
- package/src/directory.ts +125 -0
- package/src/doc-schema.ts +183 -0
- package/src/doctor.test.ts +382 -0
- package/src/doctor.ts +876 -0
- package/src/docx-batch-insert.test.ts +117 -0
- package/src/docx-batch-insert.ts +223 -0
- package/src/docx-color-text.ts +154 -0
- package/src/docx-table-ops.test.ts +54 -0
- package/src/docx-table-ops.ts +316 -0
- package/src/docx-types.ts +39 -0
- package/src/docx.account-selection.test.ts +96 -0
- package/src/docx.test.ts +706 -0
- package/src/docx.ts +1598 -0
- package/src/drive-schema.ts +93 -0
- package/src/drive.test.ts +1240 -0
- package/src/drive.ts +830 -0
- package/src/dynamic-agent.test.ts +156 -0
- package/src/dynamic-agent.ts +144 -0
- package/src/event-types.ts +46 -0
- package/src/external-keys.test.ts +21 -0
- package/src/external-keys.ts +20 -0
- package/src/lifecycle.test-support.ts +223 -0
- package/src/media.test.ts +956 -0
- package/src/media.ts +1106 -0
- package/src/mention-target.types.ts +6 -0
- package/src/mention.ts +115 -0
- package/src/message-action-contract.ts +14 -0
- package/src/monitor-state-runtime-api.ts +8 -0
- package/src/monitor-transport-runtime-api.ts +11 -0
- package/src/monitor.account.ts +501 -0
- package/src/monitor.acp-init-failure.lifecycle.test-support.ts +215 -0
- package/src/monitor.bot-identity.ts +87 -0
- package/src/monitor.bot-menu-handler.ts +164 -0
- package/src/monitor.bot-menu.lifecycle.test-support.ts +221 -0
- package/src/monitor.bot-menu.test.ts +200 -0
- package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +265 -0
- package/src/monitor.card-action.lifecycle.test-support.ts +418 -0
- package/src/monitor.cleanup.test.ts +384 -0
- package/src/monitor.comment-notice-handler.ts +106 -0
- package/src/monitor.comment.test.ts +968 -0
- package/src/monitor.comment.ts +1386 -0
- package/src/monitor.lifecycle.test.ts +5 -0
- package/src/monitor.message-handler.ts +346 -0
- package/src/monitor.reaction.test.ts +770 -0
- package/src/monitor.startup.test.ts +232 -0
- package/src/monitor.startup.ts +76 -0
- package/src/monitor.state.defaults.test.ts +47 -0
- package/src/monitor.state.ts +171 -0
- package/src/monitor.synthetic-error.ts +19 -0
- package/src/monitor.test-mocks.ts +47 -0
- package/src/monitor.transport.ts +451 -0
- package/src/monitor.ts +104 -0
- package/src/monitor.webhook-e2e.test.ts +284 -0
- package/src/monitor.webhook-security.test.ts +394 -0
- package/src/monitor.webhook.test-helpers.ts +138 -0
- package/src/outbound-runtime-api.ts +2 -0
- package/src/outbound.test.ts +1255 -0
- package/src/outbound.ts +742 -0
- package/src/perm-schema.ts +53 -0
- package/src/perm.ts +171 -0
- package/src/pins.ts +109 -0
- package/src/policy.test.ts +224 -0
- package/src/policy.ts +322 -0
- package/src/post.test.ts +106 -0
- package/src/post.ts +276 -0
- package/src/presentation-card.ts +204 -0
- package/src/probe.test.ts +310 -0
- package/src/probe.ts +181 -0
- package/src/processing-claims.ts +60 -0
- package/src/qr-terminal.ts +2 -0
- package/src/reactions.ts +124 -0
- package/src/reasoning-preview.test.ts +114 -0
- package/src/reasoning-preview.ts +29 -0
- package/src/reply-dispatcher-runtime-api.ts +8 -0
- package/src/reply-dispatcher.test.ts +2009 -0
- package/src/reply-dispatcher.ts +865 -0
- package/src/runtime.ts +10 -0
- package/src/secret-contract.ts +146 -0
- package/src/secret-input.ts +2 -0
- package/src/security-audit-shared.ts +70 -0
- package/src/security-audit.test.ts +60 -0
- package/src/security-audit.ts +2 -0
- package/src/send-result.ts +81 -0
- package/src/send-target.test.ts +87 -0
- package/src/send-target.ts +36 -0
- package/src/send.reply-fallback.test.ts +418 -0
- package/src/send.test.ts +661 -0
- package/src/send.ts +860 -0
- package/src/sequential-key.test.ts +73 -0
- package/src/sequential-key.ts +29 -0
- package/src/sequential-queue.test.ts +184 -0
- package/src/sequential-queue.ts +90 -0
- package/src/session-conversation.ts +42 -0
- package/src/session-route.ts +49 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-surface.test.ts +485 -0
- package/src/setup-surface.ts +620 -0
- package/src/streaming-card.test.ts +549 -0
- package/src/streaming-card.ts +611 -0
- package/src/subagent-hooks.test.ts +632 -0
- package/src/subagent-hooks.ts +414 -0
- package/src/targets.ts +98 -0
- package/src/test-support/lifecycle-test-support.ts +459 -0
- package/src/thread-bindings.test.ts +181 -0
- package/src/thread-bindings.ts +332 -0
- package/src/tool-account-routing.test.ts +419 -0
- package/src/tool-account.test.ts +45 -0
- package/src/tool-account.ts +98 -0
- package/src/tool-factory-test-harness.ts +83 -0
- package/src/tool-result.test.ts +33 -0
- package/src/tool-result.ts +17 -0
- package/src/tools-config.test.ts +52 -0
- package/src/tools-config.ts +29 -0
- package/src/types.ts +111 -0
- package/src/typing.test.ts +145 -0
- package/src/typing.ts +215 -0
- package/src/wiki-schema.ts +70 -0
- package/src/wiki.ts +271 -0
- package/subagent-hooks-api.ts +22 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
// Feishu plugin module implements app registration behavior.
|
|
2
|
+
import { finiteSecondsToTimerSafeMilliseconds } from "actagent/plugin-sdk/number-runtime";
|
|
3
|
+
/**
|
|
4
|
+
* Feishu app registration via OAuth device-code flow.
|
|
5
|
+
*
|
|
6
|
+
* Migrated from feishu-plugin-cli's `feishu-auth.ts` and `install-prompts.ts`.
|
|
7
|
+
* Replaces axios with native fetch, removes inquirer/ora/chalk in favor of
|
|
8
|
+
* the actagent WizardPrompter surface.
|
|
9
|
+
*/
|
|
10
|
+
import { fetchWithSsrFGuard } from "actagent/plugin-sdk/ssrf-runtime";
|
|
11
|
+
import { renderQrTerminal } from "./qr-terminal.js";
|
|
12
|
+
import type { FeishuDomain } from "./types.js";
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Constants
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
const FEISHU_ACCOUNTS_URL = "https://accounts.feishu.cn";
|
|
19
|
+
const LARK_ACCOUNTS_URL = "https://accounts.larksuite.com";
|
|
20
|
+
|
|
21
|
+
const REGISTRATION_PATH = "/oauth/v1/app/registration";
|
|
22
|
+
|
|
23
|
+
const REQUEST_TIMEOUT_MS = 10_000;
|
|
24
|
+
const DEFAULT_REGISTRATION_POLL_INTERVAL_SECONDS = 5;
|
|
25
|
+
const DEFAULT_REGISTRATION_EXPIRE_SECONDS = 600;
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Types
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
export interface AppRegistrationResult {
|
|
32
|
+
appId: string;
|
|
33
|
+
appSecret: string;
|
|
34
|
+
domain: FeishuDomain;
|
|
35
|
+
openId?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface InitResponse {
|
|
39
|
+
nonce: string;
|
|
40
|
+
supported_auth_methods: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface BeginResult {
|
|
44
|
+
deviceCode: string;
|
|
45
|
+
qrUrl: string;
|
|
46
|
+
userCode: string;
|
|
47
|
+
interval: number;
|
|
48
|
+
expireIn: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface RawBeginResponse {
|
|
52
|
+
device_code: string;
|
|
53
|
+
verification_uri: string;
|
|
54
|
+
user_code: string;
|
|
55
|
+
verification_uri_complete: string;
|
|
56
|
+
interval: number;
|
|
57
|
+
expire_in: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface PollResponse {
|
|
61
|
+
client_id?: string;
|
|
62
|
+
client_secret?: string;
|
|
63
|
+
user_info?: {
|
|
64
|
+
open_id?: string;
|
|
65
|
+
tenant_brand?: "feishu" | "lark";
|
|
66
|
+
};
|
|
67
|
+
error?: string;
|
|
68
|
+
error_description?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type PollOutcome =
|
|
72
|
+
| { status: "success"; result: AppRegistrationResult }
|
|
73
|
+
| { status: "access_denied" }
|
|
74
|
+
| { status: "expired" }
|
|
75
|
+
| { status: "timeout" }
|
|
76
|
+
| { status: "error"; message: string };
|
|
77
|
+
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// Helpers
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
function accountsBaseUrl(domain: FeishuDomain): string {
|
|
83
|
+
return domain === "lark" ? LARK_ACCOUNTS_URL : FEISHU_ACCOUNTS_URL;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function postRegistration<T>(baseUrl: string, body: Record<string, string>): Promise<T> {
|
|
87
|
+
return await fetchFeishuJson<T>({
|
|
88
|
+
url: `${baseUrl}${REGISTRATION_PATH}`,
|
|
89
|
+
init: {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
92
|
+
body: new URLSearchParams(body).toString(),
|
|
93
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
94
|
+
},
|
|
95
|
+
auditContext: "feishu.app-registration.post",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function fetchFeishuJson<T>(params: {
|
|
100
|
+
url: string;
|
|
101
|
+
init: RequestInit;
|
|
102
|
+
auditContext: string;
|
|
103
|
+
}): Promise<T> {
|
|
104
|
+
const { response, release } = await fetchWithSsrFGuard({
|
|
105
|
+
url: params.url,
|
|
106
|
+
init: params.init,
|
|
107
|
+
policy: { allowedHostnames: [new URL(params.url).hostname] },
|
|
108
|
+
auditContext: params.auditContext,
|
|
109
|
+
});
|
|
110
|
+
try {
|
|
111
|
+
// Registration poll returns 4xx for pending/error states with a JSON body.
|
|
112
|
+
return (await response.json()) as T;
|
|
113
|
+
} finally {
|
|
114
|
+
await release();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
// Public API
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Step 1: Initialize registration and verify the environment supports
|
|
124
|
+
* `client_secret` auth.
|
|
125
|
+
*
|
|
126
|
+
* @throws If the environment does not support `client_secret`.
|
|
127
|
+
*/
|
|
128
|
+
export async function initAppRegistration(domain: FeishuDomain = "feishu"): Promise<void> {
|
|
129
|
+
const baseUrl = accountsBaseUrl(domain);
|
|
130
|
+
const res = await postRegistration<InitResponse>(baseUrl, { action: "init" });
|
|
131
|
+
|
|
132
|
+
if (!res.supported_auth_methods?.includes("client_secret")) {
|
|
133
|
+
throw new Error("Current environment does not support client_secret auth method");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Step 2: Begin the device-code flow. Returns a device code and a QR URL
|
|
139
|
+
* that the user should scan with Feishu/Lark mobile app.
|
|
140
|
+
*/
|
|
141
|
+
export async function beginAppRegistration(domain: FeishuDomain = "feishu"): Promise<BeginResult> {
|
|
142
|
+
const baseUrl = accountsBaseUrl(domain);
|
|
143
|
+
const res = await postRegistration<RawBeginResponse>(baseUrl, {
|
|
144
|
+
action: "begin",
|
|
145
|
+
archetype: "PersonalAgent",
|
|
146
|
+
auth_method: "client_secret",
|
|
147
|
+
request_user_info: "open_id",
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const qrUrl = new URL(res.verification_uri_complete);
|
|
151
|
+
qrUrl.searchParams.set("from", "oc_onboard");
|
|
152
|
+
qrUrl.searchParams.set("tp", "ob_cli_app");
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
deviceCode: res.device_code,
|
|
156
|
+
qrUrl: qrUrl.toString(),
|
|
157
|
+
userCode: res.user_code,
|
|
158
|
+
interval:
|
|
159
|
+
finiteSecondsToTimerSafeMilliseconds(res.interval) === undefined
|
|
160
|
+
? DEFAULT_REGISTRATION_POLL_INTERVAL_SECONDS
|
|
161
|
+
: res.interval,
|
|
162
|
+
expireIn:
|
|
163
|
+
finiteSecondsToTimerSafeMilliseconds(res.expire_in) === undefined
|
|
164
|
+
? DEFAULT_REGISTRATION_EXPIRE_SECONDS
|
|
165
|
+
: res.expire_in,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Step 3: Poll for authorization result until success, denial, expiry, or
|
|
171
|
+
* timeout. Automatically handles domain switching when `tenant_brand` is
|
|
172
|
+
* detected as "lark".
|
|
173
|
+
*/
|
|
174
|
+
export async function pollAppRegistration(params: {
|
|
175
|
+
deviceCode: string;
|
|
176
|
+
interval: number;
|
|
177
|
+
expireIn: number;
|
|
178
|
+
initialDomain?: FeishuDomain;
|
|
179
|
+
abortSignal?: AbortSignal;
|
|
180
|
+
/** Registration type parameter. The CLI bot QR flow uses "ob_cli_app". */
|
|
181
|
+
tp?: string;
|
|
182
|
+
}): Promise<PollOutcome> {
|
|
183
|
+
const { deviceCode, expireIn, initialDomain = "feishu", abortSignal, tp } = params;
|
|
184
|
+
let currentInterval = params.interval;
|
|
185
|
+
let domain: FeishuDomain = initialDomain;
|
|
186
|
+
let domainSwitched = false;
|
|
187
|
+
|
|
188
|
+
const expireInMs =
|
|
189
|
+
finiteSecondsToTimerSafeMilliseconds(expireIn) ??
|
|
190
|
+
finiteSecondsToTimerSafeMilliseconds(DEFAULT_REGISTRATION_EXPIRE_SECONDS) ??
|
|
191
|
+
REQUEST_TIMEOUT_MS;
|
|
192
|
+
const deadline = Date.now() + expireInMs;
|
|
193
|
+
|
|
194
|
+
while (Date.now() < deadline) {
|
|
195
|
+
if (abortSignal?.aborted) {
|
|
196
|
+
return { status: "timeout" };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const baseUrl = accountsBaseUrl(domain);
|
|
200
|
+
|
|
201
|
+
let pollRes: PollResponse;
|
|
202
|
+
try {
|
|
203
|
+
pollRes = await postRegistration<PollResponse>(baseUrl, {
|
|
204
|
+
action: "poll",
|
|
205
|
+
device_code: deviceCode,
|
|
206
|
+
...(tp ? { tp } : {}),
|
|
207
|
+
});
|
|
208
|
+
} catch {
|
|
209
|
+
// Transient network error — keep polling.
|
|
210
|
+
await sleepRegistrationPollInterval(currentInterval);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Domain auto-detection: switch to lark if tenant_brand says so.
|
|
215
|
+
if (pollRes.user_info?.tenant_brand) {
|
|
216
|
+
const isLark = pollRes.user_info.tenant_brand === "lark";
|
|
217
|
+
if (!domainSwitched && isLark) {
|
|
218
|
+
domain = "lark";
|
|
219
|
+
domainSwitched = true;
|
|
220
|
+
// Retry poll immediately with the correct domain.
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Success.
|
|
226
|
+
if (pollRes.client_id && pollRes.client_secret) {
|
|
227
|
+
return {
|
|
228
|
+
status: "success",
|
|
229
|
+
result: {
|
|
230
|
+
appId: pollRes.client_id,
|
|
231
|
+
appSecret: pollRes.client_secret,
|
|
232
|
+
domain,
|
|
233
|
+
openId: pollRes.user_info?.open_id,
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Error handling.
|
|
239
|
+
if (pollRes.error) {
|
|
240
|
+
if (pollRes.error === "authorization_pending") {
|
|
241
|
+
// Continue waiting.
|
|
242
|
+
} else if (pollRes.error === "slow_down") {
|
|
243
|
+
currentInterval += 5;
|
|
244
|
+
} else if (pollRes.error === "access_denied") {
|
|
245
|
+
return { status: "access_denied" };
|
|
246
|
+
} else if (pollRes.error === "expired_token") {
|
|
247
|
+
return { status: "expired" };
|
|
248
|
+
} else {
|
|
249
|
+
return {
|
|
250
|
+
status: "error",
|
|
251
|
+
message: `${pollRes.error}: ${pollRes.error_description ?? "unknown"}`,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
await sleepRegistrationPollInterval(currentInterval);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return { status: "timeout" };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Print QR code directly to stdout.
|
|
264
|
+
*
|
|
265
|
+
* QR codes must be printed without any surrounding box/border decoration,
|
|
266
|
+
* otherwise the pattern is corrupted and cannot be scanned.
|
|
267
|
+
*/
|
|
268
|
+
export async function printQrCode(url: string): Promise<void> {
|
|
269
|
+
const output = await renderQrTerminal(url);
|
|
270
|
+
process.stdout.write(output.endsWith("\n") ? output : `${output}\n`);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Fetch the app owner's open_id using the application.v6.application.get API.
|
|
275
|
+
*
|
|
276
|
+
* Used during setup to auto-populate security policy allowlists.
|
|
277
|
+
* Returns undefined on any failure (fail-open).
|
|
278
|
+
*/
|
|
279
|
+
export async function getAppOwnerOpenId(params: {
|
|
280
|
+
appId: string;
|
|
281
|
+
appSecret: string;
|
|
282
|
+
domain?: FeishuDomain;
|
|
283
|
+
}): Promise<string | undefined> {
|
|
284
|
+
const baseUrl =
|
|
285
|
+
params.domain === "lark" ? "https://open.larksuite.com" : "https://open.feishu.cn";
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
// First, get a tenant_access_token.
|
|
289
|
+
const tokenData = await fetchFeishuJson<{
|
|
290
|
+
code?: number;
|
|
291
|
+
tenant_access_token?: string;
|
|
292
|
+
}>({
|
|
293
|
+
url: `${baseUrl}/open-apis/auth/v3/tenant_access_token/internal`,
|
|
294
|
+
init: {
|
|
295
|
+
method: "POST",
|
|
296
|
+
headers: { "Content-Type": "application/json" },
|
|
297
|
+
body: JSON.stringify({ app_id: params.appId, app_secret: params.appSecret }),
|
|
298
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
299
|
+
},
|
|
300
|
+
auditContext: "feishu.app-registration.owner-token",
|
|
301
|
+
});
|
|
302
|
+
if (!tokenData.tenant_access_token) {
|
|
303
|
+
return undefined;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Query app info for the owner's open_id.
|
|
307
|
+
const appData = await fetchFeishuJson<{
|
|
308
|
+
code?: number;
|
|
309
|
+
data?: {
|
|
310
|
+
app?: {
|
|
311
|
+
owner?: { owner_id?: string; owner_type?: number; type?: number };
|
|
312
|
+
creator_id?: string;
|
|
313
|
+
};
|
|
314
|
+
};
|
|
315
|
+
}>({
|
|
316
|
+
url: `${baseUrl}/open-apis/application/v6/applications/${params.appId}?user_id_type=open_id`,
|
|
317
|
+
init: {
|
|
318
|
+
method: "GET",
|
|
319
|
+
headers: {
|
|
320
|
+
Authorization: `Bearer ${tokenData.tenant_access_token}`,
|
|
321
|
+
"Content-Type": "application/json",
|
|
322
|
+
},
|
|
323
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
324
|
+
},
|
|
325
|
+
auditContext: "feishu.app-registration.owner-app",
|
|
326
|
+
});
|
|
327
|
+
if (appData.code !== 0) {
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const app = appData.data?.app;
|
|
332
|
+
const owner = app?.owner;
|
|
333
|
+
const ownerType = owner?.owner_type ?? owner?.type;
|
|
334
|
+
// owner_type=2 means enterprise member; use owner_id. Otherwise fallback to creator_id.
|
|
335
|
+
return ownerType === 2 && owner?.owner_id
|
|
336
|
+
? owner.owner_id
|
|
337
|
+
: (app?.creator_id ?? owner?.owner_id);
|
|
338
|
+
} catch {
|
|
339
|
+
return undefined;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function sleep(ms: number): Promise<void> {
|
|
344
|
+
return new Promise((resolve) => {
|
|
345
|
+
setTimeout(resolve, ms);
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function sleepRegistrationPollInterval(intervalSeconds: number): Promise<void> {
|
|
350
|
+
const intervalMs =
|
|
351
|
+
finiteSecondsToTimerSafeMilliseconds(intervalSeconds) ??
|
|
352
|
+
finiteSecondsToTimerSafeMilliseconds(DEFAULT_REGISTRATION_POLL_INTERVAL_SECONDS) ??
|
|
353
|
+
REQUEST_TIMEOUT_MS;
|
|
354
|
+
return sleep(intervalMs);
|
|
355
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Feishu tests cover approval auth plugin behavior.
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { feishuApprovalAuth } from "./approval-auth.js";
|
|
4
|
+
|
|
5
|
+
describe("feishuApprovalAuth", () => {
|
|
6
|
+
it("authorizes open_id approvers and ignores user_id-only allowlists", () => {
|
|
7
|
+
expect(
|
|
8
|
+
feishuApprovalAuth.authorizeActorAction({
|
|
9
|
+
cfg: { channels: { feishu: { allowFrom: ["ou_owner"] } } },
|
|
10
|
+
senderId: "ou_owner",
|
|
11
|
+
action: "approve",
|
|
12
|
+
approvalKind: "exec",
|
|
13
|
+
}),
|
|
14
|
+
).toEqual({ authorized: true });
|
|
15
|
+
|
|
16
|
+
expect(
|
|
17
|
+
feishuApprovalAuth.authorizeActorAction({
|
|
18
|
+
cfg: { channels: { feishu: { allowFrom: ["user_123"] } } },
|
|
19
|
+
senderId: "ou_attacker",
|
|
20
|
+
action: "approve",
|
|
21
|
+
approvalKind: "exec",
|
|
22
|
+
}),
|
|
23
|
+
).toEqual({ authorized: true });
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Feishu plugin module implements approval auth behavior.
|
|
2
|
+
import {
|
|
3
|
+
createResolvedApproverActionAuthAdapter,
|
|
4
|
+
resolveApprovalApprovers,
|
|
5
|
+
} from "actagent/plugin-sdk/approval-auth-runtime";
|
|
6
|
+
import { normalizeOptionalLowercaseString } from "actagent/plugin-sdk/string-coerce-runtime";
|
|
7
|
+
import { resolveFeishuAccount } from "./accounts.js";
|
|
8
|
+
import { normalizeFeishuTarget } from "./targets.js";
|
|
9
|
+
|
|
10
|
+
function normalizeFeishuApproverId(value: string | number): string | undefined {
|
|
11
|
+
const normalized = normalizeFeishuTarget(String(value));
|
|
12
|
+
const trimmed = normalizeOptionalLowercaseString(normalized);
|
|
13
|
+
return trimmed?.startsWith("ou_") ? trimmed : undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const feishuApprovalAuth = createResolvedApproverActionAuthAdapter({
|
|
17
|
+
channelLabel: "Feishu",
|
|
18
|
+
resolveApprovers: ({ cfg, accountId }) => {
|
|
19
|
+
const account = resolveFeishuAccount({ cfg, accountId }).config;
|
|
20
|
+
return resolveApprovalApprovers({
|
|
21
|
+
allowFrom: account.allowFrom,
|
|
22
|
+
normalizeApprover: normalizeFeishuApproverId,
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
normalizeSenderId: (value) => normalizeFeishuApproverId(value),
|
|
26
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Feishu tests cover async plugin behavior.
|
|
2
|
+
import { MAX_TIMER_TIMEOUT_MS } from "actagent/plugin-sdk/number-runtime";
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { raceWithTimeoutAndAbort, waitForAbortableDelay } from "./async.js";
|
|
5
|
+
|
|
6
|
+
afterEach(() => {
|
|
7
|
+
vi.useRealTimers();
|
|
8
|
+
vi.restoreAllMocks();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe("raceWithTimeoutAndAbort", () => {
|
|
12
|
+
it("normalizes oversized timeouts before arming the watchdog", async () => {
|
|
13
|
+
const timeoutSpy = vi
|
|
14
|
+
.spyOn(globalThis, "setTimeout")
|
|
15
|
+
.mockReturnValue(1 as unknown as ReturnType<typeof setTimeout>);
|
|
16
|
+
vi.spyOn(globalThis, "clearTimeout").mockImplementation(() => undefined);
|
|
17
|
+
|
|
18
|
+
await raceWithTimeoutAndAbort(Promise.resolve("ok"), {
|
|
19
|
+
timeoutMs: Number.MAX_SAFE_INTEGER,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("waitForAbortableDelay", () => {
|
|
27
|
+
it("resolves false immediately when already aborted", async () => {
|
|
28
|
+
vi.useFakeTimers();
|
|
29
|
+
const abortController = new AbortController();
|
|
30
|
+
abortController.abort();
|
|
31
|
+
|
|
32
|
+
await expect(waitForAbortableDelay(60_000, abortController.signal)).resolves.toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("resolves false immediately when aborted during backoff", async () => {
|
|
36
|
+
vi.useFakeTimers();
|
|
37
|
+
const abortController = new AbortController();
|
|
38
|
+
|
|
39
|
+
const delay = waitForAbortableDelay(60_000, abortController.signal);
|
|
40
|
+
abortController.abort();
|
|
41
|
+
|
|
42
|
+
await expect(delay).resolves.toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("resolves true after the full delay when not aborted", async () => {
|
|
46
|
+
vi.useFakeTimers();
|
|
47
|
+
|
|
48
|
+
const delay = waitForAbortableDelay(500);
|
|
49
|
+
await vi.advanceTimersByTimeAsync(500);
|
|
50
|
+
|
|
51
|
+
await expect(delay).resolves.toBe(true);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("normalizes oversized delays before arming the timer", async () => {
|
|
55
|
+
const timeoutSpy = vi
|
|
56
|
+
.spyOn(globalThis, "setTimeout")
|
|
57
|
+
.mockImplementation((callback: () => void) => {
|
|
58
|
+
queueMicrotask(callback);
|
|
59
|
+
return 1 as unknown as ReturnType<typeof setTimeout>;
|
|
60
|
+
});
|
|
61
|
+
vi.spyOn(globalThis, "clearTimeout").mockImplementation(() => undefined);
|
|
62
|
+
|
|
63
|
+
const delay = waitForAbortableDelay(Number.MAX_SAFE_INTEGER);
|
|
64
|
+
|
|
65
|
+
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
|
|
66
|
+
await expect(delay).resolves.toBe(true);
|
|
67
|
+
});
|
|
68
|
+
});
|
package/src/async.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Feishu plugin module implements async behavior.
|
|
2
|
+
import { resolveTimerTimeoutMs } from "actagent/plugin-sdk/number-runtime";
|
|
3
|
+
|
|
4
|
+
const RACE_TIMEOUT = Symbol("race-timeout");
|
|
5
|
+
const RACE_ABORT = Symbol("race-abort");
|
|
6
|
+
|
|
7
|
+
type RaceWithTimeoutAndAbortResult<T> =
|
|
8
|
+
| { status: "resolved"; value: T }
|
|
9
|
+
| { status: "timeout" }
|
|
10
|
+
| { status: "aborted" };
|
|
11
|
+
|
|
12
|
+
export async function raceWithTimeoutAndAbort<T>(
|
|
13
|
+
promise: Promise<T>,
|
|
14
|
+
options: {
|
|
15
|
+
timeoutMs?: number;
|
|
16
|
+
abortSignal?: AbortSignal;
|
|
17
|
+
} = {},
|
|
18
|
+
): Promise<RaceWithTimeoutAndAbortResult<T>> {
|
|
19
|
+
if (options.abortSignal?.aborted) {
|
|
20
|
+
return { status: "aborted" };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (options.timeoutMs === undefined && !options.abortSignal) {
|
|
24
|
+
return { status: "resolved", value: await promise };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let timeoutHandle: ReturnType<typeof setTimeout> | undefined;
|
|
28
|
+
let abortHandler: (() => void) | undefined;
|
|
29
|
+
const contenders: Array<Promise<T | typeof RACE_TIMEOUT | typeof RACE_ABORT>> = [promise];
|
|
30
|
+
|
|
31
|
+
if (options.timeoutMs !== undefined) {
|
|
32
|
+
const timeoutMs = resolveTimerTimeoutMs(options.timeoutMs, 1);
|
|
33
|
+
contenders.push(
|
|
34
|
+
new Promise((resolve) => {
|
|
35
|
+
timeoutHandle = setTimeout(() => resolve(RACE_TIMEOUT), timeoutMs);
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (options.abortSignal) {
|
|
41
|
+
contenders.push(
|
|
42
|
+
new Promise((resolve) => {
|
|
43
|
+
abortHandler = () => resolve(RACE_ABORT);
|
|
44
|
+
options.abortSignal?.addEventListener("abort", abortHandler, { once: true });
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const result = await Promise.race(contenders);
|
|
51
|
+
if (result === RACE_TIMEOUT) {
|
|
52
|
+
return { status: "timeout" };
|
|
53
|
+
}
|
|
54
|
+
if (result === RACE_ABORT) {
|
|
55
|
+
return { status: "aborted" };
|
|
56
|
+
}
|
|
57
|
+
return { status: "resolved", value: result };
|
|
58
|
+
} finally {
|
|
59
|
+
if (timeoutHandle) {
|
|
60
|
+
clearTimeout(timeoutHandle);
|
|
61
|
+
}
|
|
62
|
+
if (abortHandler) {
|
|
63
|
+
options.abortSignal?.removeEventListener("abort", abortHandler);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function waitForAbortableDelay(
|
|
69
|
+
delayMs: number,
|
|
70
|
+
abortSignal?: AbortSignal,
|
|
71
|
+
): Promise<boolean> {
|
|
72
|
+
if (abortSignal?.aborted) {
|
|
73
|
+
return Promise.resolve(false);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return new Promise((resolve) => {
|
|
77
|
+
let settled = false;
|
|
78
|
+
|
|
79
|
+
const finish = (value: boolean) => {
|
|
80
|
+
if (settled) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
settled = true;
|
|
84
|
+
if (timer) {
|
|
85
|
+
clearTimeout(timer);
|
|
86
|
+
}
|
|
87
|
+
if (handleAbort) {
|
|
88
|
+
abortSignal?.removeEventListener("abort", handleAbort);
|
|
89
|
+
}
|
|
90
|
+
resolve(value);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const handleAbort: (() => void) | undefined = () => {
|
|
94
|
+
finish(false);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
abortSignal?.addEventListener("abort", handleAbort, { once: true });
|
|
98
|
+
if (abortSignal?.aborted) {
|
|
99
|
+
finish(false);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const timer: ReturnType<typeof setTimeout> | undefined = setTimeout(
|
|
104
|
+
() => finish(true),
|
|
105
|
+
resolveTimerTimeoutMs(delayMs, 1),
|
|
106
|
+
);
|
|
107
|
+
timer.unref?.();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Feishu plugin module implements audio preflight behavior.
|
|
2
|
+
import { transcribeFirstAudio as transcribeFirstAudioImpl } from "actagent/plugin-sdk/media-runtime";
|
|
3
|
+
|
|
4
|
+
type TranscribeFirstAudio = typeof import("actagent/plugin-sdk/media-runtime").transcribeFirstAudio;
|
|
5
|
+
|
|
6
|
+
export async function transcribeFirstAudio(
|
|
7
|
+
...args: Parameters<TranscribeFirstAudio>
|
|
8
|
+
): ReturnType<TranscribeFirstAudio> {
|
|
9
|
+
return await transcribeFirstAudioImpl(...args);
|
|
10
|
+
}
|