@m1heng-clawd/feishu 0.1.18 → 0.1.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m1heng-clawd/feishu",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "type": "module",
5
5
  "description": "OpenClaw Feishu/Lark channel plugin",
6
6
  "scripts": {
@@ -63,7 +63,7 @@
63
63
  "devDependencies": {
64
64
  "@types/node": "^25.0.10",
65
65
  "@vitest/coverage-v8": "^2.1.8",
66
- "openclaw": "2026.3.1",
66
+ "openclaw": "2026.3.24",
67
67
  "tsx": "^4.21.0",
68
68
  "typescript": "^5.7.0",
69
69
  "vitest": "^2.1.8"
package/src/accounts.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ClawdbotConfig } from "openclaw/plugin-sdk";
2
- import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk";
2
+ import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "./sdk-compat.js";
3
3
  import type { FeishuConfig, FeishuAccountConfig, FeishuDomain, ResolvedFeishuAccount } from "./types.js";
4
4
 
5
5
  /**
package/src/bot.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import type { ClawdbotConfig, RuntimeEnv } from "openclaw/plugin-sdk";
2
+ import type { HistoryEntry } from "openclaw/plugin-sdk/feishu";
2
3
  import {
3
4
  buildPendingHistoryContextFromMap,
4
5
  recordPendingHistoryEntryIfEnabled,
5
6
  clearHistoryEntriesIfEnabled,
6
7
  DEFAULT_GROUP_HISTORY_LIMIT,
7
8
  resolveMentionGatingWithBypass,
8
- type HistoryEntry,
9
- } from "openclaw/plugin-sdk";
9
+ } from "./sdk-compat.js";
10
10
  import type { FeishuConfig, FeishuMessageContext, FeishuMediaInfo, ResolvedFeishuAccount } from "./types.js";
11
11
  import { getFeishuRuntime } from "./runtime.js";
12
12
  import { createFeishuClient } from "./client.js";
package/src/channel.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ChannelPlugin, ClawdbotConfig } from "openclaw/plugin-sdk";
2
- import { DEFAULT_ACCOUNT_ID, normalizeAccountId, PAIRING_APPROVED_MESSAGE } from "openclaw/plugin-sdk";
2
+ import { DEFAULT_ACCOUNT_ID, normalizeAccountId, PAIRING_APPROVED_MESSAGE } from "./sdk-compat.js";
3
+ import { feishuSetupWizardWithAccounts } from "./setup-wizard.js";
3
4
  import type { ResolvedFeishuAccount, FeishuConfig } from "./types.js";
4
5
  import {
5
6
  resolveConfiguredFeishuAccountKey,
@@ -20,7 +21,6 @@ import {
20
21
  listFeishuDirectoryPeersLive,
21
22
  listFeishuDirectoryGroupsLive,
22
23
  } from "./directory.js";
23
- import { feishuOnboardingAdapter } from "./onboarding.js";
24
24
 
25
25
  const meta = {
26
26
  id: "feishu",
@@ -269,7 +269,7 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
269
269
  };
270
270
  },
271
271
  },
272
- onboarding: feishuOnboardingAdapter,
272
+ setupWizard: feishuSetupWizardWithAccounts,
273
273
  messaging: {
274
274
  normalizeTarget: normalizeFeishuTarget,
275
275
  targetResolver: {
@@ -1,4 +1,4 @@
1
- import { normalizeAccountId } from "openclaw/plugin-sdk";
1
+ import { normalizeAccountId } from "./sdk-compat.js";
2
2
  import { z } from "zod";
3
3
  export { z };
4
4
 
package/src/monitor.ts CHANGED
@@ -3,9 +3,9 @@ import * as http from "http";
3
3
  import {
4
4
  type ClawdbotConfig,
5
5
  type RuntimeEnv,
6
- type HistoryEntry,
7
- installRequestBodyLimitGuard,
8
6
  } from "openclaw/plugin-sdk";
7
+ import type { HistoryEntry } from "openclaw/plugin-sdk/feishu";
8
+ import { installRequestBodyLimitGuard } from "./sdk-compat.js";
9
9
  import type { ResolvedFeishuAccount } from "./types.js";
10
10
  import { createFeishuWSClient, createEventDispatcher } from "./client.js";
11
11
  import { resolveFeishuAccount, listEnabledFeishuAccounts } from "./accounts.js";
package/src/outbound.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ChannelOutboundAdapter } from "openclaw/plugin-sdk";
1
+ import type { ChannelOutboundAdapter } from "openclaw/plugin-sdk/feishu";
2
2
  import { getFeishuRuntime } from "./runtime.js";
3
3
  import { sendMessageFeishu } from "./send.js";
4
4
  import { sendMediaFeishu } from "./media.js";
package/src/policy.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ChannelGroupContext, GroupToolPolicyConfig } from "openclaw/plugin-sdk";
1
+ import type { ChannelGroupContext, GroupToolPolicyConfig } from "openclaw/plugin-sdk/feishu";
2
2
  import type { FeishuConfig, FeishuGroupConfig } from "./types.js";
3
3
 
4
4
  export type FeishuGroupCommandMentionBypass = "never" | "single_bot" | "always";
@@ -1,11 +1,13 @@
1
1
  import {
2
- createReplyPrefixContext,
3
- createTypingCallbacks,
4
- logTypingFailure,
5
2
  type ClawdbotConfig,
6
3
  type ReplyPayload,
7
4
  type RuntimeEnv,
8
5
  } from "openclaw/plugin-sdk";
6
+ import {
7
+ createReplyPrefixContext,
8
+ createTypingCallbacks,
9
+ logTypingFailure,
10
+ } from "./sdk-compat.js";
9
11
  import { resolveFeishuAccount } from "./accounts.js";
10
12
  import { createFeishuClient } from "./client.js";
11
13
  import { buildMentionedCardContent, type MentionTarget } from "./mention.js";
@@ -0,0 +1,13 @@
1
+ export { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk/account-id";
2
+ export {
3
+ PAIRING_APPROVED_MESSAGE,
4
+ buildPendingHistoryContextFromMap,
5
+ clearHistoryEntriesIfEnabled,
6
+ createReplyPrefixContext,
7
+ DEFAULT_GROUP_HISTORY_LIMIT,
8
+ installRequestBodyLimitGuard,
9
+ logTypingFailure,
10
+ recordPendingHistoryEntryIfEnabled,
11
+ } from "openclaw/plugin-sdk/feishu";
12
+ export { resolveMentionGatingWithBypass } from "openclaw/plugin-sdk/channel-inbound";
13
+ export { createTypingCallbacks, promptAccountId } from "openclaw/plugin-sdk/matrix";
@@ -0,0 +1,171 @@
1
+ import type { ClawdbotConfig, ChannelPlugin } from "openclaw/plugin-sdk";
2
+ import type { ChannelSetupWizard } from "openclaw/plugin-sdk/setup";
3
+ import { feishuSetupWizard } from "openclaw/plugin-sdk/feishu";
4
+ import { DEFAULT_ACCOUNT_ID, normalizeAccountId, promptAccountId } from "./sdk-compat.js";
5
+ import {
6
+ listFeishuAccountIds,
7
+ resolveConfiguredFeishuAccountKey,
8
+ resolveDefaultFeishuAccountId,
9
+ resolveFeishuAccount,
10
+ resolveFeishuCredentials,
11
+ } from "./accounts.js";
12
+ import { probeFeishu } from "./probe.js";
13
+ import type { FeishuConfig } from "./types.js";
14
+
15
+ function patchFeishuAccountConfig(
16
+ cfg: ClawdbotConfig,
17
+ accountId: string,
18
+ patch: Record<string, unknown>,
19
+ ): ClawdbotConfig {
20
+ const feishuCfg = cfg.channels?.feishu as FeishuConfig | undefined;
21
+
22
+ if (accountId === DEFAULT_ACCOUNT_ID) {
23
+ return {
24
+ ...cfg,
25
+ channels: {
26
+ ...cfg.channels,
27
+ feishu: { ...feishuCfg, ...patch },
28
+ },
29
+ };
30
+ }
31
+
32
+ const accountKey = resolveConfiguredFeishuAccountKey(cfg, accountId) ?? accountId;
33
+ return {
34
+ ...cfg,
35
+ channels: {
36
+ ...cfg.channels,
37
+ feishu: {
38
+ ...feishuCfg,
39
+ accounts: {
40
+ ...feishuCfg?.accounts,
41
+ [accountKey]: { ...feishuCfg?.accounts?.[accountKey], ...patch },
42
+ },
43
+ },
44
+ },
45
+ };
46
+ }
47
+
48
+ const accountScopedFinalize: NonNullable<ChannelSetupWizard["finalize"]> = async ({
49
+ cfg,
50
+ accountId,
51
+ prompter,
52
+ }) => {
53
+ const account = resolveFeishuAccount({ cfg, accountId });
54
+ const resolved = resolveFeishuCredentials(account.config);
55
+ const hasConfigCreds = Boolean(account.config?.appId?.trim() && account.config?.appSecret?.trim());
56
+ const accountLabel = accountId === DEFAULT_ACCOUNT_ID ? "default" : accountId;
57
+
58
+ let next = cfg;
59
+ let appId: string | null = null;
60
+ let appSecret: string | null = null;
61
+
62
+ if (!resolved) {
63
+ await prompter.note(
64
+ [
65
+ "1) Go to Feishu Open Platform (open.feishu.cn)",
66
+ "2) Create a self-built app",
67
+ "3) Get App ID and App Secret from Credentials page",
68
+ "4) Enable required permissions: im:message, im:chat, contact:user.base:readonly",
69
+ "5) Publish the app or add it to a test group",
70
+ ].join("\n"),
71
+ `Feishu credentials (${accountLabel})`,
72
+ );
73
+ }
74
+
75
+ if (hasConfigCreds) {
76
+ const keep = await prompter.confirm({
77
+ message: `Feishu credentials already configured for "${accountLabel}". Keep them?`,
78
+ initialValue: true,
79
+ });
80
+ if (!keep) {
81
+ appId = String(await prompter.text({
82
+ message: `Enter Feishu App ID for "${accountLabel}"`,
83
+ validate: (v) => (v?.trim() ? undefined : "Required"),
84
+ })).trim();
85
+ appSecret = String(await prompter.text({
86
+ message: `Enter Feishu App Secret for "${accountLabel}"`,
87
+ validate: (v) => (v?.trim() ? undefined : "Required"),
88
+ })).trim();
89
+ }
90
+ } else {
91
+ appId = String(await prompter.text({
92
+ message: `Enter Feishu App ID for "${accountLabel}"`,
93
+ initialValue: account.config?.appId?.trim() || undefined,
94
+ validate: (v) => (v?.trim() ? undefined : "Required"),
95
+ })).trim();
96
+ appSecret = String(await prompter.text({
97
+ message: `Enter Feishu App Secret for "${accountLabel}"`,
98
+ validate: (v) => (v?.trim() ? undefined : "Required"),
99
+ })).trim();
100
+ }
101
+
102
+ if (appId && appSecret) {
103
+ next = patchFeishuAccountConfig(next, accountId, { enabled: true, appId, appSecret });
104
+ try {
105
+ const testAccount = resolveFeishuAccount({ cfg: next, accountId });
106
+ const probe = await probeFeishu(testAccount);
107
+ if (probe.ok) {
108
+ await prompter.note(
109
+ `Connected as ${probe.botName ?? probe.botOpenId ?? "bot"}`,
110
+ `Feishu connection test (${accountLabel})`,
111
+ );
112
+ } else {
113
+ await prompter.note(
114
+ `Connection failed: ${probe.error ?? "unknown error"}`,
115
+ `Feishu connection test (${accountLabel})`,
116
+ );
117
+ }
118
+ } catch (err) {
119
+ await prompter.note(`Connection test failed: ${String(err)}`, `Feishu connection test (${accountLabel})`);
120
+ }
121
+ } else {
122
+ next = patchFeishuAccountConfig(next, accountId, { enabled: true });
123
+ }
124
+
125
+ const currentDomain = resolveFeishuAccount({ cfg: next, accountId }).config?.domain ?? "feishu";
126
+ const domain = await prompter.select({
127
+ message: `Which Feishu domain? (${accountLabel})`,
128
+ options: [
129
+ { value: "feishu", label: "Feishu (feishu.cn) - China" },
130
+ { value: "lark", label: "Lark (larksuite.com) - International" },
131
+ ],
132
+ initialValue: currentDomain,
133
+ });
134
+ if (domain) {
135
+ next = patchFeishuAccountConfig(next, accountId, { domain });
136
+ }
137
+
138
+ return { cfg: next };
139
+ };
140
+
141
+ export const feishuSetupWizardWithAccounts: NonNullable<ChannelPlugin["setupWizard"]> = {
142
+ ...feishuSetupWizard,
143
+
144
+ resolveShouldPromptAccountIds: ({ cfg }) => listFeishuAccountIds(cfg).length > 1,
145
+
146
+ resolveAccountIdForConfigure: async ({
147
+ cfg,
148
+ prompter,
149
+ accountOverride,
150
+ shouldPromptAccountIds,
151
+ }) => {
152
+ if (accountOverride?.trim()) return normalizeAccountId(accountOverride);
153
+ const defaultId = resolveDefaultFeishuAccountId(cfg);
154
+ if (!shouldPromptAccountIds) return defaultId;
155
+ return promptAccountId({
156
+ cfg,
157
+ prompter,
158
+ label: "Feishu",
159
+ currentId: defaultId,
160
+ listAccountIds: listFeishuAccountIds,
161
+ defaultAccountId: defaultId,
162
+ });
163
+ },
164
+
165
+ finalize: async (params) => {
166
+ if (params.accountId === DEFAULT_ACCOUNT_ID) {
167
+ return feishuSetupWizard.finalize!(params);
168
+ }
169
+ return accountScopedFinalize(params);
170
+ },
171
+ };
package/src/onboarding.ts DELETED
@@ -1,451 +0,0 @@
1
- import type {
2
- ChannelOnboardingAdapter,
3
- ChannelOnboardingDmPolicy,
4
- ClawdbotConfig,
5
- DmPolicy,
6
- WizardPrompter,
7
- } from "openclaw/plugin-sdk";
8
- import {
9
- addWildcardAllowFrom,
10
- DEFAULT_ACCOUNT_ID,
11
- formatDocsLink,
12
- normalizeAccountId,
13
- promptAccountId,
14
- } from "openclaw/plugin-sdk";
15
-
16
- import {
17
- listFeishuAccountIds,
18
- resolveConfiguredFeishuAccountKey,
19
- resolveDefaultFeishuAccountId,
20
- resolveFeishuAccount,
21
- resolveFeishuCredentials,
22
- } from "./accounts.js";
23
- import { probeFeishu } from "./probe.js";
24
- import type { FeishuConfig } from "./types.js";
25
-
26
- const channel = "feishu" as const;
27
- let onboardingDmPolicyAccountId: string | undefined;
28
-
29
- function resolveOnboardingAccountId(cfg: ClawdbotConfig, accountId?: string | null): string {
30
- const raw = accountId?.trim();
31
- if (raw) {
32
- return normalizeAccountId(raw);
33
- }
34
- return resolveDefaultFeishuAccountId(cfg);
35
- }
36
-
37
- function resolveDmPolicyAccountId(cfg: ClawdbotConfig, accountId?: string | null): string {
38
- return resolveOnboardingAccountId(cfg, accountId ?? onboardingDmPolicyAccountId);
39
- }
40
-
41
- function upsertFeishuAccountConfig(
42
- cfg: ClawdbotConfig,
43
- accountId: string,
44
- patch: Partial<FeishuConfig>,
45
- ): ClawdbotConfig {
46
- const normalizedAccountId = normalizeAccountId(accountId);
47
- const feishuCfg = cfg.channels?.feishu as FeishuConfig | undefined;
48
-
49
- if (normalizedAccountId === DEFAULT_ACCOUNT_ID) {
50
- return {
51
- ...cfg,
52
- channels: {
53
- ...cfg.channels,
54
- feishu: {
55
- ...feishuCfg,
56
- ...patch,
57
- },
58
- },
59
- };
60
- }
61
-
62
- const accountKey = resolveConfiguredFeishuAccountKey(cfg, normalizedAccountId) ?? normalizedAccountId;
63
- const existingAccount = feishuCfg?.accounts?.[accountKey];
64
- return {
65
- ...cfg,
66
- channels: {
67
- ...cfg.channels,
68
- feishu: {
69
- ...feishuCfg,
70
- enabled: true,
71
- accounts: {
72
- ...feishuCfg?.accounts,
73
- [accountKey]: {
74
- ...existingAccount,
75
- enabled: existingAccount?.enabled ?? true,
76
- ...patch,
77
- },
78
- },
79
- },
80
- },
81
- };
82
- }
83
-
84
- function setFeishuDmPolicy(
85
- cfg: ClawdbotConfig,
86
- dmPolicy: DmPolicy,
87
- accountId?: string,
88
- ): ClawdbotConfig {
89
- const resolvedAccountId = resolveDmPolicyAccountId(cfg, accountId);
90
- const account = resolveFeishuAccount({ cfg, accountId: resolvedAccountId });
91
- // Feishu channel config does not support "disabled" as a dmPolicy value.
92
- const effectiveDmPolicy = dmPolicy === "disabled" ? "pairing" : dmPolicy;
93
- const allowFrom =
94
- effectiveDmPolicy === "open"
95
- ? addWildcardAllowFrom(account.config.allowFrom)?.map((entry) => String(entry))
96
- : undefined;
97
- return upsertFeishuAccountConfig(cfg, resolvedAccountId, {
98
- dmPolicy: effectiveDmPolicy,
99
- ...(allowFrom ? { allowFrom } : {}),
100
- });
101
- }
102
-
103
- function setFeishuAllowFrom(cfg: ClawdbotConfig, allowFrom: string[], accountId?: string): ClawdbotConfig {
104
- const resolvedAccountId = resolveOnboardingAccountId(cfg, accountId);
105
- return upsertFeishuAccountConfig(cfg, resolvedAccountId, { allowFrom });
106
- }
107
-
108
- function parseAllowFromInput(raw: string): string[] {
109
- return raw
110
- .split(/[\n,;]+/g)
111
- .map((entry) => entry.trim())
112
- .filter(Boolean);
113
- }
114
-
115
- async function promptFeishuAllowFrom(params: {
116
- cfg: ClawdbotConfig;
117
- prompter: WizardPrompter;
118
- accountId?: string;
119
- }): Promise<ClawdbotConfig> {
120
- const accountId = resolveDmPolicyAccountId(params.cfg, params.accountId);
121
- const existing = resolveFeishuAccount({ cfg: params.cfg, accountId }).config.allowFrom ?? [];
122
- const accountLabel = accountId === DEFAULT_ACCOUNT_ID ? "default" : accountId;
123
- await params.prompter.note(
124
- [
125
- `Account: ${accountLabel}`,
126
- "Allowlist Feishu DMs by open_id or user_id.",
127
- "You can find user open_id in Feishu admin console or via API.",
128
- "Examples:",
129
- "- ou_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
130
- "- on_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
131
- ].join("\n"),
132
- "Feishu allowlist",
133
- );
134
-
135
- while (true) {
136
- const entry = await params.prompter.text({
137
- message: "Feishu allowFrom (user open_ids)",
138
- placeholder: "ou_xxxxx, ou_yyyyy",
139
- initialValue: existing[0] ? String(existing[0]) : undefined,
140
- validate: (value) => (String(value ?? "").trim() ? undefined : "Required"),
141
- });
142
- const parts = parseAllowFromInput(String(entry));
143
- if (parts.length === 0) {
144
- await params.prompter.note("Enter at least one user.", "Feishu allowlist");
145
- continue;
146
- }
147
-
148
- const unique = [
149
- ...new Set([...existing.map((v) => String(v).trim()).filter(Boolean), ...parts]),
150
- ];
151
- return setFeishuAllowFrom(params.cfg, unique, accountId);
152
- }
153
- }
154
-
155
- async function noteFeishuCredentialHelp(prompter: WizardPrompter): Promise<void> {
156
- await prompter.note(
157
- [
158
- "1) Go to Feishu Open Platform (open.feishu.cn)",
159
- "2) Create a self-built app",
160
- "3) Get App ID and App Secret from Credentials page",
161
- "4) Enable required permissions: im:message, im:chat, contact:user.base:readonly",
162
- "5) Publish the app or add it to a test group",
163
- "Tip: you can also set FEISHU_APP_ID / FEISHU_APP_SECRET env vars.",
164
- `Docs: ${formatDocsLink("/channels/feishu", "feishu")}`,
165
- ].join("\n"),
166
- "Feishu credentials",
167
- );
168
- }
169
-
170
- function setFeishuGroupPolicy(
171
- cfg: ClawdbotConfig,
172
- groupPolicy: "open" | "allowlist" | "disabled",
173
- accountId?: string,
174
- ): ClawdbotConfig {
175
- const resolvedAccountId = resolveOnboardingAccountId(cfg, accountId);
176
- return upsertFeishuAccountConfig(cfg, resolvedAccountId, { enabled: true, groupPolicy });
177
- }
178
-
179
- function setFeishuGroupAllowFrom(
180
- cfg: ClawdbotConfig,
181
- groupAllowFrom: string[],
182
- accountId?: string,
183
- ): ClawdbotConfig {
184
- const resolvedAccountId = resolveOnboardingAccountId(cfg, accountId);
185
- return upsertFeishuAccountConfig(cfg, resolvedAccountId, { groupAllowFrom });
186
- }
187
-
188
- function setFeishuDomain(cfg: ClawdbotConfig, domain: "feishu" | "lark", accountId?: string): ClawdbotConfig {
189
- const resolvedAccountId = resolveOnboardingAccountId(cfg, accountId);
190
- return upsertFeishuAccountConfig(cfg, resolvedAccountId, { domain });
191
- }
192
-
193
- const dmPolicy: ChannelOnboardingDmPolicy = {
194
- label: "Feishu",
195
- channel,
196
- policyKey: "channels.feishu.dmPolicy",
197
- allowFromKey: "channels.feishu.allowFrom",
198
- getCurrent: (cfg) => {
199
- const accountId = resolveDmPolicyAccountId(cfg);
200
- return resolveFeishuAccount({ cfg, accountId }).config.dmPolicy ?? "pairing";
201
- },
202
- setPolicy: (cfg, policy) => setFeishuDmPolicy(cfg, policy, onboardingDmPolicyAccountId),
203
- promptAllowFrom: promptFeishuAllowFrom,
204
- };
205
-
206
- export const feishuOnboardingAdapter: ChannelOnboardingAdapter = {
207
- channel,
208
- getStatus: async ({ cfg, accountOverrides }) => {
209
- const override = accountOverrides?.feishu?.trim();
210
- const accountIds = override
211
- ? [resolveOnboardingAccountId(cfg, override)]
212
- : listFeishuAccountIds(cfg);
213
- const accounts = accountIds.map((accountId) => resolveFeishuAccount({ cfg, accountId }));
214
- const configuredAccounts = accounts.filter((account) => account.configured);
215
- const configured = configuredAccounts.length > 0;
216
-
217
- // Try to probe if configured
218
- let probeResult = null;
219
- if (configuredAccounts[0]) {
220
- try {
221
- probeResult = await probeFeishu(configuredAccounts[0]);
222
- } catch {
223
- // Ignore probe errors
224
- }
225
- }
226
-
227
- const statusLines: string[] = [];
228
- if (!configured) {
229
- statusLines.push("Feishu: needs app credentials");
230
- } else if (probeResult?.ok) {
231
- const probeAccountId = configuredAccounts[0]?.accountId ?? DEFAULT_ACCOUNT_ID;
232
- const accountNote = probeAccountId === DEFAULT_ACCOUNT_ID ? "" : ` [${probeAccountId}]`;
233
- statusLines.push(
234
- `Feishu${accountNote}: connected as ${probeResult.botName ?? probeResult.botOpenId ?? "bot"}`,
235
- );
236
- if (!override && configuredAccounts.length > 1) {
237
- statusLines.push(`Feishu: ${configuredAccounts.length} account(s) configured`);
238
- }
239
- } else if (!override && configuredAccounts.length > 1) {
240
- statusLines.push(`Feishu: configured (${configuredAccounts.length} account(s), connection not verified)`);
241
- } else {
242
- statusLines.push("Feishu: configured (connection not verified)");
243
- }
244
-
245
- return {
246
- channel,
247
- configured,
248
- statusLines,
249
- selectionHint: configured ? "configured" : "needs app creds",
250
- quickstartScore: configured ? 2 : 0,
251
- };
252
- },
253
-
254
- configure: async ({ cfg, prompter, accountOverrides, shouldPromptAccountIds }) => {
255
- const feishuOverride = accountOverrides?.feishu?.trim();
256
- const defaultFeishuAccountId = resolveDefaultFeishuAccountId(cfg);
257
- let feishuAccountId = feishuOverride
258
- ? normalizeAccountId(feishuOverride)
259
- : defaultFeishuAccountId;
260
- if (shouldPromptAccountIds && !feishuOverride) {
261
- feishuAccountId = await promptAccountId({
262
- cfg,
263
- prompter,
264
- label: "Feishu",
265
- currentId: feishuAccountId,
266
- listAccountIds: listFeishuAccountIds,
267
- defaultAccountId: defaultFeishuAccountId,
268
- });
269
- }
270
- onboardingDmPolicyAccountId = feishuAccountId;
271
- const accountLabel = feishuAccountId === DEFAULT_ACCOUNT_ID ? "default" : feishuAccountId;
272
- const currentAccount = resolveFeishuAccount({ cfg, accountId: feishuAccountId });
273
- const resolved = resolveFeishuCredentials(currentAccount.config);
274
- const hasConfigCreds = Boolean(
275
- currentAccount.config.appId?.trim() && currentAccount.config.appSecret?.trim(),
276
- );
277
- const canUseEnv = Boolean(
278
- feishuAccountId === DEFAULT_ACCOUNT_ID &&
279
- !hasConfigCreds &&
280
- process.env.FEISHU_APP_ID?.trim() &&
281
- process.env.FEISHU_APP_SECRET?.trim(),
282
- );
283
-
284
- let next = cfg;
285
- let appId: string | null = null;
286
- let appSecret: string | null = null;
287
- const appIdPrompt =
288
- feishuAccountId === DEFAULT_ACCOUNT_ID
289
- ? "Enter Feishu App ID"
290
- : `Enter Feishu App ID for account "${accountLabel}"`;
291
- const appSecretPrompt =
292
- feishuAccountId === DEFAULT_ACCOUNT_ID
293
- ? "Enter Feishu App Secret"
294
- : `Enter Feishu App Secret for account "${accountLabel}"`;
295
-
296
- if (!resolved) {
297
- await noteFeishuCredentialHelp(prompter);
298
- }
299
-
300
- if (canUseEnv) {
301
- const keepEnv = await prompter.confirm({
302
- message: "FEISHU_APP_ID + FEISHU_APP_SECRET detected. Use env vars?",
303
- initialValue: true,
304
- });
305
- if (keepEnv) {
306
- next = upsertFeishuAccountConfig(next, feishuAccountId, { enabled: true });
307
- } else {
308
- appId = String(
309
- await prompter.text({
310
- message: appIdPrompt,
311
- validate: (value) => (value?.trim() ? undefined : "Required"),
312
- }),
313
- ).trim();
314
- appSecret = String(
315
- await prompter.text({
316
- message: appSecretPrompt,
317
- validate: (value) => (value?.trim() ? undefined : "Required"),
318
- }),
319
- ).trim();
320
- }
321
- } else if (hasConfigCreds) {
322
- const keep = await prompter.confirm({
323
- message: `Feishu credentials already configured for account "${accountLabel}". Keep them?`,
324
- initialValue: true,
325
- });
326
- if (!keep) {
327
- appId = String(
328
- await prompter.text({
329
- message: appIdPrompt,
330
- validate: (value) => (value?.trim() ? undefined : "Required"),
331
- }),
332
- ).trim();
333
- appSecret = String(
334
- await prompter.text({
335
- message: appSecretPrompt,
336
- validate: (value) => (value?.trim() ? undefined : "Required"),
337
- }),
338
- ).trim();
339
- }
340
- } else {
341
- appId = String(
342
- await prompter.text({
343
- message: appIdPrompt,
344
- validate: (value) => (value?.trim() ? undefined : "Required"),
345
- }),
346
- ).trim();
347
- appSecret = String(
348
- await prompter.text({
349
- message: appSecretPrompt,
350
- validate: (value) => (value?.trim() ? undefined : "Required"),
351
- }),
352
- ).trim();
353
- }
354
-
355
- if (appId && appSecret) {
356
- next = upsertFeishuAccountConfig(next, feishuAccountId, {
357
- enabled: true,
358
- appId,
359
- appSecret,
360
- });
361
-
362
- // Test connection
363
- const testAccount = resolveFeishuAccount({ cfg: next, accountId: feishuAccountId });
364
- try {
365
- const probe = await probeFeishu(testAccount);
366
- if (probe.ok) {
367
- await prompter.note(
368
- `Connected as ${probe.botName ?? probe.botOpenId ?? "bot"}`,
369
- `Feishu connection test (${accountLabel})`,
370
- );
371
- } else {
372
- await prompter.note(
373
- `Connection failed: ${probe.error ?? "unknown error"}`,
374
- `Feishu connection test (${accountLabel})`,
375
- );
376
- }
377
- } catch (err) {
378
- await prompter.note(
379
- `Connection test failed: ${String(err)}`,
380
- `Feishu connection test (${accountLabel})`,
381
- );
382
- }
383
- }
384
-
385
- // Domain selection
386
- const currentDomain = resolveFeishuAccount({ cfg: next, accountId: feishuAccountId }).config.domain ?? "feishu";
387
- const domain = await prompter.select({
388
- message: "Which Feishu domain?",
389
- options: [
390
- { value: "feishu", label: "Feishu (feishu.cn) - China" },
391
- { value: "lark", label: "Lark (larksuite.com) - International" },
392
- ],
393
- initialValue: currentDomain,
394
- });
395
- if (domain) {
396
- next = setFeishuDomain(next, domain as "feishu" | "lark", feishuAccountId);
397
- }
398
-
399
- // Group policy
400
- const groupPolicyAccount = resolveFeishuAccount({ cfg: next, accountId: feishuAccountId });
401
- const groupPolicy = await prompter.select({
402
- message: "Group chat policy",
403
- options: [
404
- { value: "allowlist", label: "Allowlist - only respond in specific groups" },
405
- { value: "open", label: "Open - respond in all groups (requires mention)" },
406
- { value: "disabled", label: "Disabled - don't respond in groups" },
407
- ],
408
- initialValue: groupPolicyAccount.config.groupPolicy ?? "allowlist",
409
- });
410
- if (groupPolicy) {
411
- next = setFeishuGroupPolicy(
412
- next,
413
- groupPolicy as "open" | "allowlist" | "disabled",
414
- feishuAccountId,
415
- );
416
- }
417
-
418
- // Group allowlist if needed
419
- if (groupPolicy === "allowlist") {
420
- const existing =
421
- resolveFeishuAccount({ cfg: next, accountId: feishuAccountId }).config.groupAllowFrom ?? [];
422
- const entry = await prompter.text({
423
- message: "Group chat allowlist (chat_ids)",
424
- placeholder: "oc_xxxxx, oc_yyyyy",
425
- initialValue: existing.length > 0 ? existing.map(String).join(", ") : undefined,
426
- });
427
- if (entry) {
428
- const parts = parseAllowFromInput(String(entry));
429
- if (parts.length > 0) {
430
- next = setFeishuGroupAllowFrom(next, parts, feishuAccountId);
431
- }
432
- }
433
- }
434
-
435
- return { cfg: next, accountId: feishuAccountId };
436
- },
437
-
438
- dmPolicy,
439
-
440
- onAccountRecorded: (accountId) => {
441
- onboardingDmPolicyAccountId = normalizeAccountId(accountId);
442
- },
443
-
444
- disable: (cfg) => ({
445
- ...cfg,
446
- channels: {
447
- ...cfg.channels,
448
- feishu: { ...cfg.channels?.feishu, enabled: false },
449
- },
450
- }),
451
- };