@openclaw/feishu 2026.2.25 → 2026.3.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.
Files changed (73) hide show
  1. package/index.ts +2 -0
  2. package/package.json +2 -1
  3. package/skills/feishu-doc/SKILL.md +109 -3
  4. package/src/accounts.test.ts +161 -0
  5. package/src/accounts.ts +76 -8
  6. package/src/async.ts +62 -0
  7. package/src/bitable.ts +189 -215
  8. package/src/bot.card-action.test.ts +63 -0
  9. package/src/bot.checkBotMentioned.test.ts +56 -1
  10. package/src/bot.test.ts +1271 -56
  11. package/src/bot.ts +499 -215
  12. package/src/card-action.ts +79 -0
  13. package/src/channel.ts +26 -4
  14. package/src/chat-schema.ts +24 -0
  15. package/src/chat.test.ts +89 -0
  16. package/src/chat.ts +130 -0
  17. package/src/client.test.ts +121 -0
  18. package/src/client.ts +13 -0
  19. package/src/config-schema.test.ts +101 -1
  20. package/src/config-schema.ts +66 -11
  21. package/src/dedup.ts +47 -1
  22. package/src/doc-schema.ts +135 -0
  23. package/src/docx-batch-insert.ts +190 -0
  24. package/src/docx-color-text.ts +149 -0
  25. package/src/docx-table-ops.ts +298 -0
  26. package/src/docx.account-selection.test.ts +70 -0
  27. package/src/docx.test.ts +331 -9
  28. package/src/docx.ts +996 -72
  29. package/src/drive.ts +38 -33
  30. package/src/media.test.ts +227 -7
  31. package/src/media.ts +52 -11
  32. package/src/mention.ts +1 -1
  33. package/src/monitor.account.ts +534 -0
  34. package/src/monitor.reaction.test.ts +578 -0
  35. package/src/monitor.startup.test.ts +203 -0
  36. package/src/monitor.startup.ts +51 -0
  37. package/src/monitor.state.defaults.test.ts +46 -0
  38. package/src/monitor.state.ts +152 -0
  39. package/src/monitor.test-mocks.ts +12 -0
  40. package/src/monitor.transport.ts +163 -0
  41. package/src/monitor.ts +44 -346
  42. package/src/monitor.webhook-security.test.ts +53 -10
  43. package/src/onboarding.status.test.ts +25 -0
  44. package/src/onboarding.ts +144 -52
  45. package/src/outbound.test.ts +181 -0
  46. package/src/outbound.ts +94 -7
  47. package/src/perm.ts +37 -30
  48. package/src/policy.test.ts +56 -1
  49. package/src/policy.ts +5 -1
  50. package/src/post.test.ts +105 -0
  51. package/src/post.ts +274 -0
  52. package/src/probe.test.ts +271 -0
  53. package/src/probe.ts +131 -19
  54. package/src/reply-dispatcher.test.ts +300 -0
  55. package/src/reply-dispatcher.ts +159 -46
  56. package/src/secret-input.ts +19 -0
  57. package/src/send-target.test.ts +74 -0
  58. package/src/send-target.ts +6 -2
  59. package/src/send.reply-fallback.test.ts +105 -0
  60. package/src/send.test.ts +168 -0
  61. package/src/send.ts +143 -18
  62. package/src/streaming-card.ts +131 -43
  63. package/src/targets.test.ts +55 -1
  64. package/src/targets.ts +32 -7
  65. package/src/tool-account-routing.test.ts +129 -0
  66. package/src/tool-account.ts +70 -0
  67. package/src/tool-factory-test-harness.ts +76 -0
  68. package/src/tools-config.test.ts +21 -0
  69. package/src/tools-config.ts +2 -1
  70. package/src/types.ts +10 -1
  71. package/src/typing.test.ts +144 -0
  72. package/src/typing.ts +140 -10
  73. package/src/wiki.ts +55 -50
@@ -0,0 +1,271 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ const createFeishuClientMock = vi.hoisted(() => vi.fn());
4
+
5
+ vi.mock("./client.js", () => ({
6
+ createFeishuClient: createFeishuClientMock,
7
+ }));
8
+
9
+ import { FEISHU_PROBE_REQUEST_TIMEOUT_MS, probeFeishu, clearProbeCache } from "./probe.js";
10
+
11
+ function makeRequestFn(response: Record<string, unknown>) {
12
+ return vi.fn().mockResolvedValue(response);
13
+ }
14
+
15
+ function setupClient(response: Record<string, unknown>) {
16
+ const requestFn = makeRequestFn(response);
17
+ createFeishuClientMock.mockReturnValue({ request: requestFn });
18
+ return requestFn;
19
+ }
20
+
21
+ describe("probeFeishu", () => {
22
+ beforeEach(() => {
23
+ clearProbeCache();
24
+ vi.restoreAllMocks();
25
+ });
26
+
27
+ afterEach(() => {
28
+ clearProbeCache();
29
+ });
30
+
31
+ it("returns error when credentials are missing", async () => {
32
+ const result = await probeFeishu();
33
+ expect(result).toEqual({ ok: false, error: "missing credentials (appId, appSecret)" });
34
+ });
35
+
36
+ it("returns error when appId is missing", async () => {
37
+ const result = await probeFeishu({ appSecret: "secret" } as never);
38
+ expect(result).toEqual({ ok: false, error: "missing credentials (appId, appSecret)" });
39
+ });
40
+
41
+ it("returns error when appSecret is missing", async () => {
42
+ const result = await probeFeishu({ appId: "cli_123" } as never);
43
+ expect(result).toEqual({ ok: false, error: "missing credentials (appId, appSecret)" });
44
+ });
45
+
46
+ it("returns bot info on successful probe", async () => {
47
+ const requestFn = setupClient({
48
+ code: 0,
49
+ bot: { bot_name: "TestBot", open_id: "ou_abc123" },
50
+ });
51
+
52
+ const result = await probeFeishu({ appId: "cli_123", appSecret: "secret" });
53
+ expect(result).toEqual({
54
+ ok: true,
55
+ appId: "cli_123",
56
+ botName: "TestBot",
57
+ botOpenId: "ou_abc123",
58
+ });
59
+ expect(requestFn).toHaveBeenCalledTimes(1);
60
+ });
61
+
62
+ it("passes the probe timeout to the Feishu request", async () => {
63
+ const requestFn = setupClient({
64
+ code: 0,
65
+ bot: { bot_name: "TestBot", open_id: "ou_abc123" },
66
+ });
67
+
68
+ await probeFeishu({ appId: "cli_123", appSecret: "secret" });
69
+
70
+ expect(requestFn).toHaveBeenCalledWith(
71
+ expect.objectContaining({
72
+ method: "GET",
73
+ url: "/open-apis/bot/v3/info",
74
+ timeout: FEISHU_PROBE_REQUEST_TIMEOUT_MS,
75
+ }),
76
+ );
77
+ });
78
+
79
+ it("returns timeout error when request exceeds timeout", async () => {
80
+ vi.useFakeTimers();
81
+ try {
82
+ const requestFn = vi.fn().mockImplementation(() => new Promise(() => {}));
83
+ createFeishuClientMock.mockReturnValue({ request: requestFn });
84
+
85
+ const promise = probeFeishu({ appId: "cli_123", appSecret: "secret" }, { timeoutMs: 1_000 });
86
+ await vi.advanceTimersByTimeAsync(1_000);
87
+ const result = await promise;
88
+
89
+ expect(result).toMatchObject({ ok: false, error: "probe timed out after 1000ms" });
90
+ } finally {
91
+ vi.useRealTimers();
92
+ }
93
+ });
94
+
95
+ it("returns aborted when abort signal is already aborted", async () => {
96
+ createFeishuClientMock.mockClear();
97
+ const abortController = new AbortController();
98
+ abortController.abort();
99
+
100
+ const result = await probeFeishu(
101
+ { appId: "cli_123", appSecret: "secret" },
102
+ { abortSignal: abortController.signal },
103
+ );
104
+
105
+ expect(result).toMatchObject({ ok: false, error: "probe aborted" });
106
+ expect(createFeishuClientMock).not.toHaveBeenCalled();
107
+ });
108
+ it("returns cached result on subsequent calls within TTL", async () => {
109
+ const requestFn = setupClient({
110
+ code: 0,
111
+ bot: { bot_name: "TestBot", open_id: "ou_abc123" },
112
+ });
113
+
114
+ const creds = { appId: "cli_123", appSecret: "secret" };
115
+ const first = await probeFeishu(creds);
116
+ const second = await probeFeishu(creds);
117
+
118
+ expect(first).toEqual(second);
119
+ // Only one API call should have been made
120
+ expect(requestFn).toHaveBeenCalledTimes(1);
121
+ });
122
+
123
+ it("makes a fresh API call after cache expires", async () => {
124
+ vi.useFakeTimers();
125
+ try {
126
+ const requestFn = setupClient({
127
+ code: 0,
128
+ bot: { bot_name: "TestBot", open_id: "ou_abc123" },
129
+ });
130
+
131
+ const creds = { appId: "cli_123", appSecret: "secret" };
132
+ await probeFeishu(creds);
133
+ expect(requestFn).toHaveBeenCalledTimes(1);
134
+
135
+ // Advance time past the success TTL
136
+ vi.advanceTimersByTime(10 * 60 * 1000 + 1);
137
+
138
+ await probeFeishu(creds);
139
+ expect(requestFn).toHaveBeenCalledTimes(2);
140
+ } finally {
141
+ vi.useRealTimers();
142
+ }
143
+ });
144
+
145
+ it("caches failed probe results (API error) for the error TTL", async () => {
146
+ vi.useFakeTimers();
147
+ try {
148
+ const requestFn = makeRequestFn({ code: 99, msg: "token expired" });
149
+ createFeishuClientMock.mockReturnValue({ request: requestFn });
150
+
151
+ const creds = { appId: "cli_123", appSecret: "secret" };
152
+ const first = await probeFeishu(creds);
153
+ const second = await probeFeishu(creds);
154
+ expect(first).toMatchObject({ ok: false, error: "API error: token expired" });
155
+ expect(second).toMatchObject({ ok: false, error: "API error: token expired" });
156
+ expect(requestFn).toHaveBeenCalledTimes(1);
157
+
158
+ vi.advanceTimersByTime(60 * 1000 + 1);
159
+
160
+ await probeFeishu(creds);
161
+ expect(requestFn).toHaveBeenCalledTimes(2);
162
+ } finally {
163
+ vi.useRealTimers();
164
+ }
165
+ });
166
+
167
+ it("caches thrown request errors for the error TTL", async () => {
168
+ vi.useFakeTimers();
169
+ try {
170
+ const requestFn = vi.fn().mockRejectedValue(new Error("network error"));
171
+ createFeishuClientMock.mockReturnValue({ request: requestFn });
172
+
173
+ const creds = { appId: "cli_123", appSecret: "secret" };
174
+ const first = await probeFeishu(creds);
175
+ const second = await probeFeishu(creds);
176
+ expect(first).toMatchObject({ ok: false, error: "network error" });
177
+ expect(second).toMatchObject({ ok: false, error: "network error" });
178
+ expect(requestFn).toHaveBeenCalledTimes(1);
179
+
180
+ vi.advanceTimersByTime(60 * 1000 + 1);
181
+
182
+ await probeFeishu(creds);
183
+ expect(requestFn).toHaveBeenCalledTimes(2);
184
+ } finally {
185
+ vi.useRealTimers();
186
+ }
187
+ });
188
+
189
+ it("caches per account independently", async () => {
190
+ const requestFn = setupClient({
191
+ code: 0,
192
+ bot: { bot_name: "Bot1", open_id: "ou_1" },
193
+ });
194
+
195
+ await probeFeishu({ appId: "cli_aaa", appSecret: "s1" });
196
+ expect(requestFn).toHaveBeenCalledTimes(1);
197
+
198
+ // Different appId should trigger a new API call
199
+ await probeFeishu({ appId: "cli_bbb", appSecret: "s2" });
200
+ expect(requestFn).toHaveBeenCalledTimes(2);
201
+
202
+ // Same appId + appSecret as first call should return cached
203
+ await probeFeishu({ appId: "cli_aaa", appSecret: "s1" });
204
+ expect(requestFn).toHaveBeenCalledTimes(2);
205
+ });
206
+
207
+ it("does not share cache between accounts with same appId but different appSecret", async () => {
208
+ const requestFn = setupClient({
209
+ code: 0,
210
+ bot: { bot_name: "Bot1", open_id: "ou_1" },
211
+ });
212
+
213
+ // First account with appId + secret A
214
+ await probeFeishu({ appId: "cli_shared", appSecret: "secret_aaa" });
215
+ expect(requestFn).toHaveBeenCalledTimes(1);
216
+
217
+ // Second account with same appId but different secret (e.g. after rotation)
218
+ // must NOT reuse the cached result
219
+ await probeFeishu({ appId: "cli_shared", appSecret: "secret_bbb" });
220
+ expect(requestFn).toHaveBeenCalledTimes(2);
221
+ });
222
+
223
+ it("uses accountId for cache key when available", async () => {
224
+ const requestFn = setupClient({
225
+ code: 0,
226
+ bot: { bot_name: "Bot1", open_id: "ou_1" },
227
+ });
228
+
229
+ // Two accounts with same appId+appSecret but different accountIds are cached separately
230
+ await probeFeishu({ accountId: "acct-1", appId: "cli_123", appSecret: "secret" });
231
+ expect(requestFn).toHaveBeenCalledTimes(1);
232
+
233
+ await probeFeishu({ accountId: "acct-2", appId: "cli_123", appSecret: "secret" });
234
+ expect(requestFn).toHaveBeenCalledTimes(2);
235
+
236
+ // Same accountId should return cached
237
+ await probeFeishu({ accountId: "acct-1", appId: "cli_123", appSecret: "secret" });
238
+ expect(requestFn).toHaveBeenCalledTimes(2);
239
+ });
240
+
241
+ it("clearProbeCache forces fresh API call", async () => {
242
+ const requestFn = setupClient({
243
+ code: 0,
244
+ bot: { bot_name: "TestBot", open_id: "ou_abc123" },
245
+ });
246
+
247
+ const creds = { appId: "cli_123", appSecret: "secret" };
248
+ await probeFeishu(creds);
249
+ expect(requestFn).toHaveBeenCalledTimes(1);
250
+
251
+ clearProbeCache();
252
+
253
+ await probeFeishu(creds);
254
+ expect(requestFn).toHaveBeenCalledTimes(2);
255
+ });
256
+
257
+ it("handles response.data.bot fallback path", async () => {
258
+ setupClient({
259
+ code: 0,
260
+ data: { bot: { bot_name: "DataBot", open_id: "ou_data" } },
261
+ });
262
+
263
+ const result = await probeFeishu({ appId: "cli_123", appSecret: "secret" });
264
+ expect(result).toEqual({
265
+ ok: true,
266
+ appId: "cli_123",
267
+ botName: "DataBot",
268
+ botOpenId: "ou_data",
269
+ });
270
+ });
271
+ });
package/src/probe.ts CHANGED
@@ -1,44 +1,156 @@
1
+ import { raceWithTimeoutAndAbort } from "./async.js";
1
2
  import { createFeishuClient, type FeishuClientCredentials } from "./client.js";
2
3
  import type { FeishuProbeResult } from "./types.js";
3
4
 
4
- export async function probeFeishu(creds?: FeishuClientCredentials): Promise<FeishuProbeResult> {
5
+ /** Cache probe results to reduce repeated health-check calls.
6
+ * Gateway health checks call probeFeishu() every minute; without caching this
7
+ * burns ~43,200 calls/month, easily exceeding Feishu's free-tier quota.
8
+ * Successful bot info is effectively static, while failures are cached briefly
9
+ * to avoid hammering the API during transient outages. */
10
+ const probeCache = new Map<string, { result: FeishuProbeResult; expiresAt: number }>();
11
+ const PROBE_SUCCESS_TTL_MS = 10 * 60 * 1000; // 10 minutes
12
+ const PROBE_ERROR_TTL_MS = 60 * 1000; // 1 minute
13
+ const MAX_PROBE_CACHE_SIZE = 64;
14
+ export const FEISHU_PROBE_REQUEST_TIMEOUT_MS = 10_000;
15
+ export type ProbeFeishuOptions = {
16
+ timeoutMs?: number;
17
+ abortSignal?: AbortSignal;
18
+ };
19
+
20
+ type FeishuBotInfoResponse = {
21
+ code: number;
22
+ msg?: string;
23
+ bot?: { bot_name?: string; open_id?: string };
24
+ data?: { bot?: { bot_name?: string; open_id?: string } };
25
+ };
26
+
27
+ function setCachedProbeResult(
28
+ cacheKey: string,
29
+ result: FeishuProbeResult,
30
+ ttlMs: number,
31
+ ): FeishuProbeResult {
32
+ probeCache.set(cacheKey, { result, expiresAt: Date.now() + ttlMs });
33
+ if (probeCache.size > MAX_PROBE_CACHE_SIZE) {
34
+ const oldest = probeCache.keys().next().value;
35
+ if (oldest !== undefined) {
36
+ probeCache.delete(oldest);
37
+ }
38
+ }
39
+ return result;
40
+ }
41
+
42
+ export async function probeFeishu(
43
+ creds?: FeishuClientCredentials,
44
+ options: ProbeFeishuOptions = {},
45
+ ): Promise<FeishuProbeResult> {
5
46
  if (!creds?.appId || !creds?.appSecret) {
6
47
  return {
7
48
  ok: false,
8
49
  error: "missing credentials (appId, appSecret)",
9
50
  };
10
51
  }
52
+ if (options.abortSignal?.aborted) {
53
+ return {
54
+ ok: false,
55
+ appId: creds.appId,
56
+ error: "probe aborted",
57
+ };
58
+ }
59
+
60
+ const timeoutMs = options.timeoutMs ?? FEISHU_PROBE_REQUEST_TIMEOUT_MS;
61
+
62
+ // Return cached result if still valid.
63
+ // Use accountId when available; otherwise include appSecret prefix so two
64
+ // accounts sharing the same appId (e.g. after secret rotation) don't
65
+ // pollute each other's cache entry.
66
+ const cacheKey = creds.accountId ?? `${creds.appId}:${creds.appSecret.slice(0, 8)}`;
67
+ const cached = probeCache.get(cacheKey);
68
+ if (cached && cached.expiresAt > Date.now()) {
69
+ return cached.result;
70
+ }
11
71
 
12
72
  try {
13
73
  const client = createFeishuClient(creds);
14
74
  // Use bot/v3/info API to get bot information
15
75
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- SDK generic request method
16
- const response = await (client as any).request({
17
- method: "GET",
18
- url: "/open-apis/bot/v3/info",
19
- data: {},
20
- });
76
+ const responseResult = await raceWithTimeoutAndAbort<FeishuBotInfoResponse>(
77
+ (client as any).request({
78
+ method: "GET",
79
+ url: "/open-apis/bot/v3/info",
80
+ data: {},
81
+ timeout: timeoutMs,
82
+ }) as Promise<FeishuBotInfoResponse>,
83
+ {
84
+ timeoutMs,
85
+ abortSignal: options.abortSignal,
86
+ },
87
+ );
21
88
 
22
- if (response.code !== 0) {
89
+ if (responseResult.status === "aborted") {
23
90
  return {
24
91
  ok: false,
25
92
  appId: creds.appId,
26
- error: `API error: ${response.msg || `code ${response.code}`}`,
93
+ error: "probe aborted",
27
94
  };
28
95
  }
96
+ if (responseResult.status === "timeout") {
97
+ return setCachedProbeResult(
98
+ cacheKey,
99
+ {
100
+ ok: false,
101
+ appId: creds.appId,
102
+ error: `probe timed out after ${timeoutMs}ms`,
103
+ },
104
+ PROBE_ERROR_TTL_MS,
105
+ );
106
+ }
107
+
108
+ const response = responseResult.value;
109
+ if (options.abortSignal?.aborted) {
110
+ return {
111
+ ok: false,
112
+ appId: creds.appId,
113
+ error: "probe aborted",
114
+ };
115
+ }
116
+
117
+ if (response.code !== 0) {
118
+ return setCachedProbeResult(
119
+ cacheKey,
120
+ {
121
+ ok: false,
122
+ appId: creds.appId,
123
+ error: `API error: ${response.msg || `code ${response.code}`}`,
124
+ },
125
+ PROBE_ERROR_TTL_MS,
126
+ );
127
+ }
29
128
 
30
129
  const bot = response.bot || response.data?.bot;
31
- return {
32
- ok: true,
33
- appId: creds.appId,
34
- botName: bot?.bot_name,
35
- botOpenId: bot?.open_id,
36
- };
130
+ return setCachedProbeResult(
131
+ cacheKey,
132
+ {
133
+ ok: true,
134
+ appId: creds.appId,
135
+ botName: bot?.bot_name,
136
+ botOpenId: bot?.open_id,
137
+ },
138
+ PROBE_SUCCESS_TTL_MS,
139
+ );
37
140
  } catch (err) {
38
- return {
39
- ok: false,
40
- appId: creds.appId,
41
- error: err instanceof Error ? err.message : String(err),
42
- };
141
+ return setCachedProbeResult(
142
+ cacheKey,
143
+ {
144
+ ok: false,
145
+ appId: creds.appId,
146
+ error: err instanceof Error ? err.message : String(err),
147
+ },
148
+ PROBE_ERROR_TTL_MS,
149
+ );
43
150
  }
44
151
  }
152
+
153
+ /** Clear the probe cache (for testing). */
154
+ export function clearProbeCache(): void {
155
+ probeCache.clear();
156
+ }