@openclaw/feishu 2026.3.1 → 2026.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +2 -2
- package/package.json +1 -1
- package/src/accounts.test.ts +268 -11
- package/src/accounts.ts +101 -14
- package/src/bitable.ts +40 -28
- package/src/bot.checkBotMentioned.test.ts +9 -1
- package/src/bot.stripBotMention.test.ts +118 -22
- package/src/bot.test.ts +945 -77
- package/src/bot.ts +492 -165
- package/src/card-action.ts +1 -1
- package/src/channel.test.ts +1 -1
- package/src/channel.ts +72 -68
- package/src/chat.test.ts +2 -2
- package/src/chat.ts +1 -1
- package/src/client.test.ts +221 -4
- package/src/client.ts +70 -5
- package/src/config-schema.test.ts +33 -6
- package/src/config-schema.ts +18 -10
- package/src/dedup.ts +47 -1
- package/src/directory.test.ts +40 -0
- package/src/directory.ts +29 -50
- package/src/doc-schema.ts +16 -22
- package/src/docx-batch-insert.test.ts +90 -0
- package/src/docx-batch-insert.ts +8 -11
- package/src/docx.account-selection.test.ts +10 -16
- package/src/docx.test.ts +41 -189
- package/src/docx.ts +1 -1
- package/src/drive.ts +13 -17
- package/src/dynamic-agent.ts +1 -1
- package/src/feishu-command-handler.ts +59 -0
- package/src/media.test.ts +164 -14
- package/src/media.ts +44 -10
- package/src/mention.ts +1 -1
- package/src/monitor.account.ts +284 -25
- package/src/monitor.reaction.test.ts +395 -46
- package/src/monitor.startup.test.ts +25 -8
- package/src/monitor.startup.ts +20 -7
- package/src/monitor.state.defaults.test.ts +46 -0
- package/src/monitor.state.ts +88 -9
- package/src/monitor.test-mocks.ts +45 -0
- package/src/monitor.transport.ts +4 -1
- package/src/monitor.ts +4 -4
- package/src/monitor.webhook-security.test.ts +13 -11
- package/src/onboarding.status.test.ts +25 -0
- package/src/onboarding.test.ts +143 -0
- package/src/onboarding.ts +213 -106
- package/src/outbound.test.ts +178 -0
- package/src/outbound.ts +39 -6
- package/src/perm.ts +11 -15
- package/src/policy.test.ts +40 -0
- package/src/policy.ts +9 -10
- package/src/probe.test.ts +54 -36
- package/src/probe.ts +57 -37
- package/src/reactions.ts +1 -1
- package/src/reply-dispatcher.test.ts +216 -0
- package/src/reply-dispatcher.ts +89 -22
- package/src/runtime.ts +1 -1
- package/src/secret-input.ts +13 -0
- package/src/send-message.ts +71 -0
- package/src/send-target.test.ts +74 -0
- package/src/send-target.ts +7 -3
- package/src/send.reply-fallback.test.ts +74 -0
- package/src/send.test.ts +1 -1
- package/src/send.ts +88 -49
- package/src/streaming-card.test.ts +54 -0
- package/src/streaming-card.ts +96 -28
- package/src/targets.test.ts +29 -0
- package/src/targets.ts +25 -1
- package/src/tool-account-routing.test.ts +3 -3
- package/src/tool-account.ts +1 -1
- package/src/tool-factory-test-harness.ts +1 -1
- package/src/tool-result.test.ts +32 -0
- package/src/tool-result.ts +14 -0
- package/src/types.ts +11 -4
- package/src/typing.ts +1 -1
- package/src/wiki.ts +15 -19
package/src/card-action.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ClawdbotConfig, RuntimeEnv } from "openclaw/plugin-sdk";
|
|
1
|
+
import type { ClawdbotConfig, RuntimeEnv } from "openclaw/plugin-sdk/feishu";
|
|
2
2
|
import { resolveFeishuAccount } from "./accounts.js";
|
|
3
3
|
import { handleFeishuMessage, type FeishuMessageEvent } from "./bot.js";
|
|
4
4
|
|
package/src/channel.test.ts
CHANGED
package/src/channel.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import type { ChannelMeta, ChannelPlugin, ClawdbotConfig } from "openclaw/plugin-sdk";
|
|
2
1
|
import {
|
|
3
|
-
|
|
2
|
+
collectAllowlistProviderRestrictSendersWarnings,
|
|
3
|
+
formatAllowFromLowercase,
|
|
4
|
+
mapAllowFromEntries,
|
|
5
|
+
} from "openclaw/plugin-sdk/compat";
|
|
6
|
+
import type { ChannelMeta, ChannelPlugin, ClawdbotConfig } from "openclaw/plugin-sdk/feishu";
|
|
7
|
+
import {
|
|
8
|
+
buildProbeChannelStatusSummary,
|
|
9
|
+
buildRuntimeAccountStatusSnapshot,
|
|
4
10
|
createDefaultChannelRuntimeState,
|
|
5
11
|
DEFAULT_ACCOUNT_ID,
|
|
6
12
|
PAIRING_APPROVED_MESSAGE,
|
|
7
|
-
|
|
8
|
-
resolveDefaultGroupPolicy,
|
|
9
|
-
} from "openclaw/plugin-sdk";
|
|
13
|
+
} from "openclaw/plugin-sdk/feishu";
|
|
10
14
|
import {
|
|
11
15
|
resolveFeishuAccount,
|
|
12
16
|
resolveFeishuCredentials,
|
|
@@ -38,6 +42,46 @@ const meta: ChannelMeta = {
|
|
|
38
42
|
order: 70,
|
|
39
43
|
};
|
|
40
44
|
|
|
45
|
+
const secretInputJsonSchema = {
|
|
46
|
+
oneOf: [
|
|
47
|
+
{ type: "string" },
|
|
48
|
+
{
|
|
49
|
+
type: "object",
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
required: ["source", "provider", "id"],
|
|
52
|
+
properties: {
|
|
53
|
+
source: { type: "string", enum: ["env", "file", "exec"] },
|
|
54
|
+
provider: { type: "string", minLength: 1 },
|
|
55
|
+
id: { type: "string", minLength: 1 },
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
} as const;
|
|
60
|
+
|
|
61
|
+
function setFeishuNamedAccountEnabled(
|
|
62
|
+
cfg: ClawdbotConfig,
|
|
63
|
+
accountId: string,
|
|
64
|
+
enabled: boolean,
|
|
65
|
+
): ClawdbotConfig {
|
|
66
|
+
const feishuCfg = cfg.channels?.feishu as FeishuConfig | undefined;
|
|
67
|
+
return {
|
|
68
|
+
...cfg,
|
|
69
|
+
channels: {
|
|
70
|
+
...cfg.channels,
|
|
71
|
+
feishu: {
|
|
72
|
+
...feishuCfg,
|
|
73
|
+
accounts: {
|
|
74
|
+
...feishuCfg?.accounts,
|
|
75
|
+
[accountId]: {
|
|
76
|
+
...feishuCfg?.accounts?.[accountId],
|
|
77
|
+
enabled,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
41
85
|
export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
42
86
|
id: "feishu",
|
|
43
87
|
meta: {
|
|
@@ -72,6 +116,9 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
|
72
116
|
groups: {
|
|
73
117
|
resolveToolPolicy: resolveFeishuGroupToolPolicy,
|
|
74
118
|
},
|
|
119
|
+
mentions: {
|
|
120
|
+
stripPatterns: () => ['<at user_id="[^"]*">[^<]*</at>'],
|
|
121
|
+
},
|
|
75
122
|
reload: { configPrefixes: ["channels.feishu"] },
|
|
76
123
|
configSchema: {
|
|
77
124
|
schema: {
|
|
@@ -81,9 +128,9 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
|
81
128
|
enabled: { type: "boolean" },
|
|
82
129
|
defaultAccount: { type: "string" },
|
|
83
130
|
appId: { type: "string" },
|
|
84
|
-
appSecret:
|
|
131
|
+
appSecret: secretInputJsonSchema,
|
|
85
132
|
encryptKey: { type: "string" },
|
|
86
|
-
verificationToken:
|
|
133
|
+
verificationToken: secretInputJsonSchema,
|
|
87
134
|
domain: {
|
|
88
135
|
oneOf: [
|
|
89
136
|
{ type: "string", enum: ["feishu", "lark"] },
|
|
@@ -122,9 +169,9 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
|
122
169
|
enabled: { type: "boolean" },
|
|
123
170
|
name: { type: "string" },
|
|
124
171
|
appId: { type: "string" },
|
|
125
|
-
appSecret:
|
|
172
|
+
appSecret: secretInputJsonSchema,
|
|
126
173
|
encryptKey: { type: "string" },
|
|
127
|
-
verificationToken:
|
|
174
|
+
verificationToken: secretInputJsonSchema,
|
|
128
175
|
domain: { type: "string", enum: ["feishu", "lark"] },
|
|
129
176
|
connectionMode: { type: "string", enum: ["websocket", "webhook"] },
|
|
130
177
|
webhookHost: { type: "string" },
|
|
@@ -159,23 +206,7 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
|
159
206
|
}
|
|
160
207
|
|
|
161
208
|
// For named accounts, set enabled in accounts[accountId]
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
...cfg,
|
|
165
|
-
channels: {
|
|
166
|
-
...cfg.channels,
|
|
167
|
-
feishu: {
|
|
168
|
-
...feishuCfg,
|
|
169
|
-
accounts: {
|
|
170
|
-
...feishuCfg?.accounts,
|
|
171
|
-
[accountId]: {
|
|
172
|
-
...feishuCfg?.accounts?.[accountId],
|
|
173
|
-
enabled,
|
|
174
|
-
},
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
|
-
};
|
|
209
|
+
return setFeishuNamedAccountEnabled(cfg, accountId, enabled);
|
|
179
210
|
},
|
|
180
211
|
deleteAccount: ({ cfg, accountId }) => {
|
|
181
212
|
const isDefault = accountId === DEFAULT_ACCOUNT_ID;
|
|
@@ -220,28 +251,23 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
|
220
251
|
}),
|
|
221
252
|
resolveAllowFrom: ({ cfg, accountId }) => {
|
|
222
253
|
const account = resolveFeishuAccount({ cfg, accountId });
|
|
223
|
-
return (account.config?.allowFrom
|
|
254
|
+
return mapAllowFromEntries(account.config?.allowFrom);
|
|
224
255
|
},
|
|
225
|
-
formatAllowFrom: ({ allowFrom }) =>
|
|
226
|
-
allowFrom
|
|
227
|
-
.map((entry) => String(entry).trim())
|
|
228
|
-
.filter(Boolean)
|
|
229
|
-
.map((entry) => entry.toLowerCase()),
|
|
256
|
+
formatAllowFrom: ({ allowFrom }) => formatAllowFromLowercase({ allowFrom }),
|
|
230
257
|
},
|
|
231
258
|
security: {
|
|
232
259
|
collectWarnings: ({ cfg, accountId }) => {
|
|
233
260
|
const account = resolveFeishuAccount({ cfg, accountId });
|
|
234
261
|
const feishuCfg = account.config;
|
|
235
|
-
|
|
236
|
-
|
|
262
|
+
return collectAllowlistProviderRestrictSendersWarnings({
|
|
263
|
+
cfg,
|
|
237
264
|
providerConfigPresent: cfg.channels?.feishu !== undefined,
|
|
238
|
-
|
|
239
|
-
|
|
265
|
+
configuredGroupPolicy: feishuCfg?.groupPolicy,
|
|
266
|
+
surface: `Feishu[${account.accountId}] groups`,
|
|
267
|
+
openScope: "any member",
|
|
268
|
+
groupPolicyPath: "channels.feishu.groupPolicy",
|
|
269
|
+
groupAllowFromPath: "channels.feishu.groupAllowFrom",
|
|
240
270
|
});
|
|
241
|
-
if (groupPolicy !== "open") return [];
|
|
242
|
-
return [
|
|
243
|
-
`- Feishu[${account.accountId}] groups: groupPolicy="open" allows any member to trigger (mention-gated). Set channels.feishu.groupPolicy="allowlist" + channels.feishu.groupAllowFrom to restrict senders.`,
|
|
244
|
-
];
|
|
245
271
|
},
|
|
246
272
|
},
|
|
247
273
|
setup: {
|
|
@@ -262,23 +288,7 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
|
262
288
|
};
|
|
263
289
|
}
|
|
264
290
|
|
|
265
|
-
|
|
266
|
-
return {
|
|
267
|
-
...cfg,
|
|
268
|
-
channels: {
|
|
269
|
-
...cfg.channels,
|
|
270
|
-
feishu: {
|
|
271
|
-
...feishuCfg,
|
|
272
|
-
accounts: {
|
|
273
|
-
...feishuCfg?.accounts,
|
|
274
|
-
[accountId]: {
|
|
275
|
-
...feishuCfg?.accounts?.[accountId],
|
|
276
|
-
enabled: true,
|
|
277
|
-
},
|
|
278
|
-
},
|
|
279
|
-
},
|
|
280
|
-
},
|
|
281
|
-
};
|
|
291
|
+
return setFeishuNamedAccountEnabled(cfg, accountId, true);
|
|
282
292
|
},
|
|
283
293
|
},
|
|
284
294
|
onboarding: feishuOnboardingAdapter,
|
|
@@ -323,12 +333,10 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
|
323
333
|
outbound: feishuOutbound,
|
|
324
334
|
status: {
|
|
325
335
|
defaultRuntime: createDefaultChannelRuntimeState(DEFAULT_ACCOUNT_ID, { port: null }),
|
|
326
|
-
buildChannelSummary: ({ snapshot }) =>
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
lastProbeAt: snapshot.lastProbeAt ?? null,
|
|
331
|
-
}),
|
|
336
|
+
buildChannelSummary: ({ snapshot }) =>
|
|
337
|
+
buildProbeChannelStatusSummary(snapshot, {
|
|
338
|
+
port: snapshot.port ?? null,
|
|
339
|
+
}),
|
|
332
340
|
probeAccount: async ({ account }) => await probeFeishu(account),
|
|
333
341
|
buildAccountSnapshot: ({ account, runtime, probe }) => ({
|
|
334
342
|
accountId: account.accountId,
|
|
@@ -337,12 +345,8 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount> = {
|
|
|
337
345
|
name: account.name,
|
|
338
346
|
appId: account.appId,
|
|
339
347
|
domain: account.domain,
|
|
340
|
-
|
|
341
|
-
lastStartAt: runtime?.lastStartAt ?? null,
|
|
342
|
-
lastStopAt: runtime?.lastStopAt ?? null,
|
|
343
|
-
lastError: runtime?.lastError ?? null,
|
|
348
|
+
...buildRuntimeAccountStatusSnapshot({ runtime, probe }),
|
|
344
349
|
port: runtime?.port ?? null,
|
|
345
|
-
probe,
|
|
346
350
|
}),
|
|
347
351
|
},
|
|
348
352
|
gateway: {
|
package/src/chat.test.ts
CHANGED
|
@@ -29,7 +29,7 @@ describe("registerFeishuChatTools", () => {
|
|
|
29
29
|
feishu: {
|
|
30
30
|
enabled: true,
|
|
31
31
|
appId: "app_id",
|
|
32
|
-
appSecret: "app_secret",
|
|
32
|
+
appSecret: "app_secret", // pragma: allowlist secret
|
|
33
33
|
tools: { chat: true },
|
|
34
34
|
},
|
|
35
35
|
},
|
|
@@ -76,7 +76,7 @@ describe("registerFeishuChatTools", () => {
|
|
|
76
76
|
feishu: {
|
|
77
77
|
enabled: true,
|
|
78
78
|
appId: "app_id",
|
|
79
|
-
appSecret: "app_secret",
|
|
79
|
+
appSecret: "app_secret", // pragma: allowlist secret
|
|
80
80
|
tools: { chat: false },
|
|
81
81
|
},
|
|
82
82
|
},
|
package/src/chat.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
2
|
-
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
|
2
|
+
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/feishu";
|
|
3
3
|
import { listEnabledFeishuAccounts } from "./accounts.js";
|
|
4
4
|
import { FeishuChatSchema, type FeishuChatParams } from "./chat-schema.js";
|
|
5
5
|
import { createFeishuClient } from "./client.js";
|
package/src/client.test.ts
CHANGED
|
@@ -12,6 +12,17 @@ const httpsProxyAgentCtorMock = vi.hoisted(() =>
|
|
|
12
12
|
}),
|
|
13
13
|
);
|
|
14
14
|
|
|
15
|
+
const mockBaseHttpInstance = vi.hoisted(() => ({
|
|
16
|
+
request: vi.fn().mockResolvedValue({}),
|
|
17
|
+
get: vi.fn().mockResolvedValue({}),
|
|
18
|
+
post: vi.fn().mockResolvedValue({}),
|
|
19
|
+
put: vi.fn().mockResolvedValue({}),
|
|
20
|
+
patch: vi.fn().mockResolvedValue({}),
|
|
21
|
+
delete: vi.fn().mockResolvedValue({}),
|
|
22
|
+
head: vi.fn().mockResolvedValue({}),
|
|
23
|
+
options: vi.fn().mockResolvedValue({}),
|
|
24
|
+
}));
|
|
25
|
+
|
|
15
26
|
vi.mock("@larksuiteoapi/node-sdk", () => ({
|
|
16
27
|
AppType: { SelfBuild: "self" },
|
|
17
28
|
Domain: { Feishu: "https://open.feishu.cn", Lark: "https://open.larksuite.com" },
|
|
@@ -19,25 +30,36 @@ vi.mock("@larksuiteoapi/node-sdk", () => ({
|
|
|
19
30
|
Client: vi.fn(),
|
|
20
31
|
WSClient: wsClientCtorMock,
|
|
21
32
|
EventDispatcher: vi.fn(),
|
|
33
|
+
defaultHttpInstance: mockBaseHttpInstance,
|
|
22
34
|
}));
|
|
23
35
|
|
|
24
36
|
vi.mock("https-proxy-agent", () => ({
|
|
25
37
|
HttpsProxyAgent: httpsProxyAgentCtorMock,
|
|
26
38
|
}));
|
|
27
39
|
|
|
28
|
-
import {
|
|
40
|
+
import { Client as LarkClient } from "@larksuiteoapi/node-sdk";
|
|
41
|
+
import {
|
|
42
|
+
createFeishuClient,
|
|
43
|
+
createFeishuWSClient,
|
|
44
|
+
clearClientCache,
|
|
45
|
+
FEISHU_HTTP_TIMEOUT_MS,
|
|
46
|
+
FEISHU_HTTP_TIMEOUT_MAX_MS,
|
|
47
|
+
FEISHU_HTTP_TIMEOUT_ENV_VAR,
|
|
48
|
+
} from "./client.js";
|
|
29
49
|
|
|
30
50
|
const proxyEnvKeys = ["https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"] as const;
|
|
31
51
|
type ProxyEnvKey = (typeof proxyEnvKeys)[number];
|
|
32
52
|
|
|
33
53
|
let priorProxyEnv: Partial<Record<ProxyEnvKey, string | undefined>> = {};
|
|
54
|
+
let priorFeishuTimeoutEnv: string | undefined;
|
|
34
55
|
|
|
35
56
|
const baseAccount: ResolvedFeishuAccount = {
|
|
36
57
|
accountId: "main",
|
|
58
|
+
selectionSource: "explicit",
|
|
37
59
|
enabled: true,
|
|
38
60
|
configured: true,
|
|
39
61
|
appId: "app_123",
|
|
40
|
-
appSecret: "secret_123",
|
|
62
|
+
appSecret: "secret_123", // pragma: allowlist secret
|
|
41
63
|
domain: "feishu",
|
|
42
64
|
config: {} as FeishuConfig,
|
|
43
65
|
};
|
|
@@ -49,6 +71,8 @@ function firstWsClientOptions(): { agent?: unknown } {
|
|
|
49
71
|
|
|
50
72
|
beforeEach(() => {
|
|
51
73
|
priorProxyEnv = {};
|
|
74
|
+
priorFeishuTimeoutEnv = process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR];
|
|
75
|
+
delete process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR];
|
|
52
76
|
for (const key of proxyEnvKeys) {
|
|
53
77
|
priorProxyEnv[key] = process.env[key];
|
|
54
78
|
delete process.env[key];
|
|
@@ -65,6 +89,171 @@ afterEach(() => {
|
|
|
65
89
|
process.env[key] = value;
|
|
66
90
|
}
|
|
67
91
|
}
|
|
92
|
+
if (priorFeishuTimeoutEnv === undefined) {
|
|
93
|
+
delete process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR];
|
|
94
|
+
} else {
|
|
95
|
+
process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR] = priorFeishuTimeoutEnv;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("createFeishuClient HTTP timeout", () => {
|
|
100
|
+
beforeEach(() => {
|
|
101
|
+
clearClientCache();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const getLastClientHttpInstance = () => {
|
|
105
|
+
const calls = (LarkClient as unknown as ReturnType<typeof vi.fn>).mock.calls;
|
|
106
|
+
const lastCall = calls[calls.length - 1]?.[0] as
|
|
107
|
+
| { httpInstance?: { get: (...args: unknown[]) => Promise<unknown> } }
|
|
108
|
+
| undefined;
|
|
109
|
+
return lastCall?.httpInstance;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const expectGetCallTimeout = async (timeout: number) => {
|
|
113
|
+
const httpInstance = getLastClientHttpInstance();
|
|
114
|
+
expect(httpInstance).toBeDefined();
|
|
115
|
+
await httpInstance?.get("https://example.com/api");
|
|
116
|
+
expect(mockBaseHttpInstance.get).toHaveBeenCalledWith(
|
|
117
|
+
"https://example.com/api",
|
|
118
|
+
expect.objectContaining({ timeout }),
|
|
119
|
+
);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
it("passes a custom httpInstance with default timeout to Lark.Client", () => {
|
|
123
|
+
createFeishuClient({ appId: "app_1", appSecret: "secret_1", accountId: "timeout-test" }); // pragma: allowlist secret
|
|
124
|
+
|
|
125
|
+
const calls = (LarkClient as unknown as ReturnType<typeof vi.fn>).mock.calls;
|
|
126
|
+
const lastCall = calls[calls.length - 1][0] as { httpInstance?: unknown };
|
|
127
|
+
expect(lastCall.httpInstance).toBeDefined();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("injects default timeout into HTTP request options", async () => {
|
|
131
|
+
createFeishuClient({ appId: "app_2", appSecret: "secret_2", accountId: "timeout-inject" }); // pragma: allowlist secret
|
|
132
|
+
|
|
133
|
+
const calls = (LarkClient as unknown as ReturnType<typeof vi.fn>).mock.calls;
|
|
134
|
+
const lastCall = calls[calls.length - 1][0] as {
|
|
135
|
+
httpInstance: { post: (...args: unknown[]) => Promise<unknown> };
|
|
136
|
+
};
|
|
137
|
+
const httpInstance = lastCall.httpInstance;
|
|
138
|
+
|
|
139
|
+
await httpInstance.post(
|
|
140
|
+
"https://example.com/api",
|
|
141
|
+
{ data: 1 },
|
|
142
|
+
{ headers: { "X-Custom": "yes" } },
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
expect(mockBaseHttpInstance.post).toHaveBeenCalledWith(
|
|
146
|
+
"https://example.com/api",
|
|
147
|
+
{ data: 1 },
|
|
148
|
+
expect.objectContaining({ timeout: FEISHU_HTTP_TIMEOUT_MS, headers: { "X-Custom": "yes" } }),
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("allows explicit timeout override per-request", async () => {
|
|
153
|
+
createFeishuClient({ appId: "app_3", appSecret: "secret_3", accountId: "timeout-override" }); // pragma: allowlist secret
|
|
154
|
+
|
|
155
|
+
const calls = (LarkClient as unknown as ReturnType<typeof vi.fn>).mock.calls;
|
|
156
|
+
const lastCall = calls[calls.length - 1][0] as {
|
|
157
|
+
httpInstance: { get: (...args: unknown[]) => Promise<unknown> };
|
|
158
|
+
};
|
|
159
|
+
const httpInstance = lastCall.httpInstance;
|
|
160
|
+
|
|
161
|
+
await httpInstance.get("https://example.com/api", { timeout: 5_000 });
|
|
162
|
+
|
|
163
|
+
expect(mockBaseHttpInstance.get).toHaveBeenCalledWith(
|
|
164
|
+
"https://example.com/api",
|
|
165
|
+
expect.objectContaining({ timeout: 5_000 }),
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("uses config-configured default timeout when provided", async () => {
|
|
170
|
+
createFeishuClient({
|
|
171
|
+
appId: "app_4",
|
|
172
|
+
appSecret: "secret_4", // pragma: allowlist secret
|
|
173
|
+
accountId: "timeout-config",
|
|
174
|
+
config: { httpTimeoutMs: 45_000 },
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
await expectGetCallTimeout(45_000);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("falls back to default timeout when configured timeout is invalid", async () => {
|
|
181
|
+
createFeishuClient({
|
|
182
|
+
appId: "app_5",
|
|
183
|
+
appSecret: "secret_5", // pragma: allowlist secret
|
|
184
|
+
accountId: "timeout-config-invalid",
|
|
185
|
+
config: { httpTimeoutMs: -1 },
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
await expectGetCallTimeout(FEISHU_HTTP_TIMEOUT_MS);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("uses env timeout override when provided and no direct timeout is set", async () => {
|
|
192
|
+
process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR] = "60000";
|
|
193
|
+
|
|
194
|
+
createFeishuClient({
|
|
195
|
+
appId: "app_8",
|
|
196
|
+
appSecret: "secret_8", // pragma: allowlist secret
|
|
197
|
+
accountId: "timeout-env-override",
|
|
198
|
+
config: { httpTimeoutMs: 45_000 },
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
await expectGetCallTimeout(60_000);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("prefers direct timeout over env override", async () => {
|
|
205
|
+
process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR] = "60000";
|
|
206
|
+
|
|
207
|
+
createFeishuClient({
|
|
208
|
+
appId: "app_10",
|
|
209
|
+
appSecret: "secret_10", // pragma: allowlist secret
|
|
210
|
+
accountId: "timeout-direct-override",
|
|
211
|
+
httpTimeoutMs: 120_000,
|
|
212
|
+
config: { httpTimeoutMs: 45_000 },
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
await expectGetCallTimeout(120_000);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("clamps env timeout override to max bound", async () => {
|
|
219
|
+
process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR] = String(FEISHU_HTTP_TIMEOUT_MAX_MS + 123_456);
|
|
220
|
+
|
|
221
|
+
createFeishuClient({
|
|
222
|
+
appId: "app_9",
|
|
223
|
+
appSecret: "secret_9", // pragma: allowlist secret
|
|
224
|
+
accountId: "timeout-env-clamp",
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
await expectGetCallTimeout(FEISHU_HTTP_TIMEOUT_MAX_MS);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("recreates cached client when configured timeout changes", async () => {
|
|
231
|
+
createFeishuClient({
|
|
232
|
+
appId: "app_6",
|
|
233
|
+
appSecret: "secret_6", // pragma: allowlist secret
|
|
234
|
+
accountId: "timeout-cache-change",
|
|
235
|
+
config: { httpTimeoutMs: 30_000 },
|
|
236
|
+
});
|
|
237
|
+
createFeishuClient({
|
|
238
|
+
appId: "app_6",
|
|
239
|
+
appSecret: "secret_6", // pragma: allowlist secret
|
|
240
|
+
accountId: "timeout-cache-change",
|
|
241
|
+
config: { httpTimeoutMs: 45_000 },
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
const calls = (LarkClient as unknown as ReturnType<typeof vi.fn>).mock.calls;
|
|
245
|
+
expect(calls.length).toBe(2);
|
|
246
|
+
|
|
247
|
+
const lastCall = calls[calls.length - 1][0] as {
|
|
248
|
+
httpInstance: { get: (...args: unknown[]) => Promise<unknown> };
|
|
249
|
+
};
|
|
250
|
+
await lastCall.httpInstance.get("https://example.com/api");
|
|
251
|
+
|
|
252
|
+
expect(mockBaseHttpInstance.get).toHaveBeenCalledWith(
|
|
253
|
+
"https://example.com/api",
|
|
254
|
+
expect.objectContaining({ timeout: 45_000 }),
|
|
255
|
+
);
|
|
256
|
+
});
|
|
68
257
|
});
|
|
69
258
|
|
|
70
259
|
describe("createFeishuWSClient proxy handling", () => {
|
|
@@ -76,9 +265,12 @@ describe("createFeishuWSClient proxy handling", () => {
|
|
|
76
265
|
expect(options?.agent).toBeUndefined();
|
|
77
266
|
});
|
|
78
267
|
|
|
79
|
-
it("
|
|
268
|
+
it("uses proxy env precedence: https_proxy first, then HTTPS_PROXY, then http_proxy/HTTP_PROXY", () => {
|
|
269
|
+
// NOTE: On Windows, environment variables are case-insensitive, so it's not
|
|
270
|
+
// possible to set both https_proxy and HTTPS_PROXY to different values.
|
|
271
|
+
// Keep this test cross-platform by asserting precedence via mutually-exclusive
|
|
272
|
+
// setups.
|
|
80
273
|
process.env.https_proxy = "http://lower-https:8001";
|
|
81
|
-
process.env.HTTPS_PROXY = "http://upper-https:8002";
|
|
82
274
|
process.env.http_proxy = "http://lower-http:8003";
|
|
83
275
|
process.env.HTTP_PROXY = "http://upper-http:8004";
|
|
84
276
|
|
|
@@ -94,6 +286,31 @@ describe("createFeishuWSClient proxy handling", () => {
|
|
|
94
286
|
expect(options.agent).toEqual({ proxyUrl: expectedProxy });
|
|
95
287
|
});
|
|
96
288
|
|
|
289
|
+
it("accepts lowercase https_proxy when it is the configured HTTPS proxy var", () => {
|
|
290
|
+
process.env.https_proxy = "http://lower-https:8001";
|
|
291
|
+
|
|
292
|
+
createFeishuWSClient(baseAccount);
|
|
293
|
+
|
|
294
|
+
const expectedHttpsProxy = process.env.https_proxy || process.env.HTTPS_PROXY;
|
|
295
|
+
expect(httpsProxyAgentCtorMock).toHaveBeenCalledTimes(1);
|
|
296
|
+
expect(expectedHttpsProxy).toBeTruthy();
|
|
297
|
+
expect(httpsProxyAgentCtorMock).toHaveBeenCalledWith(expectedHttpsProxy);
|
|
298
|
+
const options = firstWsClientOptions();
|
|
299
|
+
expect(options.agent).toEqual({ proxyUrl: expectedHttpsProxy });
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("uses HTTPS_PROXY when https_proxy is unset", () => {
|
|
303
|
+
process.env.HTTPS_PROXY = "http://upper-https:8002";
|
|
304
|
+
process.env.http_proxy = "http://lower-http:8003";
|
|
305
|
+
|
|
306
|
+
createFeishuWSClient(baseAccount);
|
|
307
|
+
|
|
308
|
+
expect(httpsProxyAgentCtorMock).toHaveBeenCalledTimes(1);
|
|
309
|
+
expect(httpsProxyAgentCtorMock).toHaveBeenCalledWith("http://upper-https:8002");
|
|
310
|
+
const options = firstWsClientOptions();
|
|
311
|
+
expect(options.agent).toEqual({ proxyUrl: "http://upper-https:8002" });
|
|
312
|
+
});
|
|
313
|
+
|
|
97
314
|
it("passes HTTP_PROXY to ws client when https vars are unset", () => {
|
|
98
315
|
process.env.HTTP_PROXY = "http://upper-http:8999";
|
|
99
316
|
|
package/src/client.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import * as Lark from "@larksuiteoapi/node-sdk";
|
|
2
2
|
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
3
|
-
import type { FeishuDomain, ResolvedFeishuAccount } from "./types.js";
|
|
3
|
+
import type { FeishuConfig, FeishuDomain, ResolvedFeishuAccount } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/** Default HTTP timeout for Feishu API requests (30 seconds). */
|
|
6
|
+
export const FEISHU_HTTP_TIMEOUT_MS = 30_000;
|
|
7
|
+
export const FEISHU_HTTP_TIMEOUT_MAX_MS = 300_000;
|
|
8
|
+
export const FEISHU_HTTP_TIMEOUT_ENV_VAR = "OPENCLAW_FEISHU_HTTP_TIMEOUT_MS";
|
|
4
9
|
|
|
5
10
|
function getWsProxyAgent(): HttpsProxyAgent<string> | undefined {
|
|
6
11
|
const proxyUrl =
|
|
@@ -17,7 +22,7 @@ const clientCache = new Map<
|
|
|
17
22
|
string,
|
|
18
23
|
{
|
|
19
24
|
client: Lark.Client;
|
|
20
|
-
config: { appId: string; appSecret: string; domain?: FeishuDomain };
|
|
25
|
+
config: { appId: string; appSecret: string; domain?: FeishuDomain; httpTimeoutMs: number };
|
|
21
26
|
}
|
|
22
27
|
>();
|
|
23
28
|
|
|
@@ -31,6 +36,30 @@ function resolveDomain(domain: FeishuDomain | undefined): Lark.Domain | string {
|
|
|
31
36
|
return domain.replace(/\/+$/, ""); // Custom URL for private deployment
|
|
32
37
|
}
|
|
33
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Create an HTTP instance that delegates to the Lark SDK's default instance
|
|
41
|
+
* but injects a default request timeout to prevent indefinite hangs
|
|
42
|
+
* (e.g. when the Feishu API is slow, causing per-chat queue deadlocks).
|
|
43
|
+
*/
|
|
44
|
+
function createTimeoutHttpInstance(defaultTimeoutMs: number): Lark.HttpInstance {
|
|
45
|
+
const base: Lark.HttpInstance = Lark.defaultHttpInstance as unknown as Lark.HttpInstance;
|
|
46
|
+
|
|
47
|
+
function injectTimeout<D>(opts?: Lark.HttpRequestOptions<D>): Lark.HttpRequestOptions<D> {
|
|
48
|
+
return { timeout: defaultTimeoutMs, ...opts } as Lark.HttpRequestOptions<D>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
request: (opts) => base.request(injectTimeout(opts)),
|
|
53
|
+
get: (url, opts) => base.get(url, injectTimeout(opts)),
|
|
54
|
+
post: (url, data, opts) => base.post(url, data, injectTimeout(opts)),
|
|
55
|
+
put: (url, data, opts) => base.put(url, data, injectTimeout(opts)),
|
|
56
|
+
patch: (url, data, opts) => base.patch(url, data, injectTimeout(opts)),
|
|
57
|
+
delete: (url, opts) => base.delete(url, injectTimeout(opts)),
|
|
58
|
+
head: (url, opts) => base.head(url, injectTimeout(opts)),
|
|
59
|
+
options: (url, opts) => base.options(url, injectTimeout(opts)),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
34
63
|
/**
|
|
35
64
|
* Credentials needed to create a Feishu client.
|
|
36
65
|
* Both FeishuConfig and ResolvedFeishuAccount satisfy this interface.
|
|
@@ -40,14 +69,48 @@ export type FeishuClientCredentials = {
|
|
|
40
69
|
appId?: string;
|
|
41
70
|
appSecret?: string;
|
|
42
71
|
domain?: FeishuDomain;
|
|
72
|
+
httpTimeoutMs?: number;
|
|
73
|
+
config?: Pick<FeishuConfig, "httpTimeoutMs">;
|
|
43
74
|
};
|
|
44
75
|
|
|
76
|
+
function resolveConfiguredHttpTimeoutMs(creds: FeishuClientCredentials): number {
|
|
77
|
+
const clampTimeout = (value: number): number => {
|
|
78
|
+
const rounded = Math.floor(value);
|
|
79
|
+
return Math.min(Math.max(rounded, 1), FEISHU_HTTP_TIMEOUT_MAX_MS);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const fromDirectField = creds.httpTimeoutMs;
|
|
83
|
+
if (
|
|
84
|
+
typeof fromDirectField === "number" &&
|
|
85
|
+
Number.isFinite(fromDirectField) &&
|
|
86
|
+
fromDirectField > 0
|
|
87
|
+
) {
|
|
88
|
+
return clampTimeout(fromDirectField);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const envRaw = process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR];
|
|
92
|
+
if (envRaw) {
|
|
93
|
+
const envValue = Number(envRaw);
|
|
94
|
+
if (Number.isFinite(envValue) && envValue > 0) {
|
|
95
|
+
return clampTimeout(envValue);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const fromConfig = creds.config?.httpTimeoutMs;
|
|
100
|
+
const timeout = fromConfig;
|
|
101
|
+
if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0) {
|
|
102
|
+
return FEISHU_HTTP_TIMEOUT_MS;
|
|
103
|
+
}
|
|
104
|
+
return clampTimeout(timeout);
|
|
105
|
+
}
|
|
106
|
+
|
|
45
107
|
/**
|
|
46
108
|
* Create or get a cached Feishu client for an account.
|
|
47
109
|
* Accepts any object with appId, appSecret, and optional domain/accountId.
|
|
48
110
|
*/
|
|
49
111
|
export function createFeishuClient(creds: FeishuClientCredentials): Lark.Client {
|
|
50
112
|
const { accountId = "default", appId, appSecret, domain } = creds;
|
|
113
|
+
const defaultHttpTimeoutMs = resolveConfiguredHttpTimeoutMs(creds);
|
|
51
114
|
|
|
52
115
|
if (!appId || !appSecret) {
|
|
53
116
|
throw new Error(`Feishu credentials not configured for account "${accountId}"`);
|
|
@@ -59,23 +122,25 @@ export function createFeishuClient(creds: FeishuClientCredentials): Lark.Client
|
|
|
59
122
|
cached &&
|
|
60
123
|
cached.config.appId === appId &&
|
|
61
124
|
cached.config.appSecret === appSecret &&
|
|
62
|
-
cached.config.domain === domain
|
|
125
|
+
cached.config.domain === domain &&
|
|
126
|
+
cached.config.httpTimeoutMs === defaultHttpTimeoutMs
|
|
63
127
|
) {
|
|
64
128
|
return cached.client;
|
|
65
129
|
}
|
|
66
130
|
|
|
67
|
-
// Create new client
|
|
131
|
+
// Create new client with timeout-aware HTTP instance
|
|
68
132
|
const client = new Lark.Client({
|
|
69
133
|
appId,
|
|
70
134
|
appSecret,
|
|
71
135
|
appType: Lark.AppType.SelfBuild,
|
|
72
136
|
domain: resolveDomain(domain),
|
|
137
|
+
httpInstance: createTimeoutHttpInstance(defaultHttpTimeoutMs),
|
|
73
138
|
});
|
|
74
139
|
|
|
75
140
|
// Cache it
|
|
76
141
|
clientCache.set(accountId, {
|
|
77
142
|
client,
|
|
78
|
-
config: { appId, appSecret, domain },
|
|
143
|
+
config: { appId, appSecret, domain, httpTimeoutMs: defaultHttpTimeoutMs },
|
|
79
144
|
});
|
|
80
145
|
|
|
81
146
|
return client;
|