@openclaw/feishu 2026.2.24 → 2026.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) 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 +90 -0
  5. package/src/accounts.ts +11 -2
  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 +55 -0
  10. package/src/bot.test.ts +863 -9
  11. package/src/bot.ts +414 -200
  12. package/src/card-action.ts +79 -0
  13. package/src/channel.ts +6 -0
  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 +107 -0
  18. package/src/client.ts +13 -0
  19. package/src/config-schema.test.ts +82 -1
  20. package/src/config-schema.ts +54 -3
  21. package/src/doc-schema.ts +141 -0
  22. package/src/docx-batch-insert.ts +190 -0
  23. package/src/docx-color-text.ts +149 -0
  24. package/src/docx-table-ops.ts +298 -0
  25. package/src/docx.account-selection.test.ts +76 -0
  26. package/src/docx.test.ts +470 -0
  27. package/src/docx.ts +996 -72
  28. package/src/drive.ts +38 -33
  29. package/src/media.test.ts +123 -6
  30. package/src/media.ts +31 -10
  31. package/src/monitor.account.ts +286 -0
  32. package/src/monitor.reaction.test.ts +235 -0
  33. package/src/monitor.startup.test.ts +187 -0
  34. package/src/monitor.startup.ts +51 -0
  35. package/src/monitor.state.ts +76 -0
  36. package/src/monitor.transport.ts +163 -0
  37. package/src/monitor.ts +44 -346
  38. package/src/monitor.webhook-security.test.ts +27 -1
  39. package/src/outbound.test.ts +181 -0
  40. package/src/outbound.ts +94 -7
  41. package/src/perm.ts +37 -30
  42. package/src/policy.test.ts +56 -1
  43. package/src/policy.ts +5 -1
  44. package/src/post.test.ts +105 -0
  45. package/src/post.ts +274 -0
  46. package/src/probe.test.ts +253 -0
  47. package/src/probe.ts +99 -7
  48. package/src/reply-dispatcher.test.ts +259 -0
  49. package/src/reply-dispatcher.ts +139 -45
  50. package/src/send.reply-fallback.test.ts +105 -0
  51. package/src/send.test.ts +168 -0
  52. package/src/send.ts +143 -18
  53. package/src/streaming-card.ts +131 -43
  54. package/src/targets.test.ts +26 -1
  55. package/src/targets.ts +11 -6
  56. package/src/tool-account-routing.test.ts +129 -0
  57. package/src/tool-account.ts +70 -0
  58. package/src/tool-factory-test-harness.ts +76 -0
  59. package/src/tools-config.test.ts +21 -0
  60. package/src/tools-config.ts +2 -1
  61. package/src/types.ts +1 -0
  62. package/src/typing.test.ts +144 -0
  63. package/src/typing.ts +140 -10
  64. package/src/wiki.ts +55 -50
@@ -0,0 +1,253 @@
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("uses explicit timeout for bot info 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
+
109
+ it("returns cached result on subsequent calls within TTL", async () => {
110
+ const requestFn = setupClient({
111
+ code: 0,
112
+ bot: { bot_name: "TestBot", open_id: "ou_abc123" },
113
+ });
114
+
115
+ const creds = { appId: "cli_123", appSecret: "secret" };
116
+ const first = await probeFeishu(creds);
117
+ const second = await probeFeishu(creds);
118
+
119
+ expect(first).toEqual(second);
120
+ // Only one API call should have been made
121
+ expect(requestFn).toHaveBeenCalledTimes(1);
122
+ });
123
+
124
+ it("makes a fresh API call after cache expires", async () => {
125
+ vi.useFakeTimers();
126
+ try {
127
+ const requestFn = setupClient({
128
+ code: 0,
129
+ bot: { bot_name: "TestBot", open_id: "ou_abc123" },
130
+ });
131
+
132
+ const creds = { appId: "cli_123", appSecret: "secret" };
133
+ await probeFeishu(creds);
134
+ expect(requestFn).toHaveBeenCalledTimes(1);
135
+
136
+ // Advance time past the 10-minute TTL
137
+ vi.advanceTimersByTime(10 * 60 * 1000 + 1);
138
+
139
+ await probeFeishu(creds);
140
+ expect(requestFn).toHaveBeenCalledTimes(2);
141
+ } finally {
142
+ vi.useRealTimers();
143
+ }
144
+ });
145
+
146
+ it("does not cache failed probe results (API error)", async () => {
147
+ const requestFn = makeRequestFn({ code: 99, msg: "token expired" });
148
+ createFeishuClientMock.mockReturnValue({ request: requestFn });
149
+
150
+ const creds = { appId: "cli_123", appSecret: "secret" };
151
+ const first = await probeFeishu(creds);
152
+ expect(first).toMatchObject({ ok: false, error: "API error: token expired" });
153
+
154
+ // Second call should make a fresh request since failures are not cached
155
+ await probeFeishu(creds);
156
+ expect(requestFn).toHaveBeenCalledTimes(2);
157
+ });
158
+
159
+ it("does not cache results when request throws", async () => {
160
+ const requestFn = vi.fn().mockRejectedValue(new Error("network error"));
161
+ createFeishuClientMock.mockReturnValue({ request: requestFn });
162
+
163
+ const creds = { appId: "cli_123", appSecret: "secret" };
164
+ const first = await probeFeishu(creds);
165
+ expect(first).toMatchObject({ ok: false, error: "network error" });
166
+
167
+ await probeFeishu(creds);
168
+ expect(requestFn).toHaveBeenCalledTimes(2);
169
+ });
170
+
171
+ it("caches per account independently", async () => {
172
+ const requestFn = setupClient({
173
+ code: 0,
174
+ bot: { bot_name: "Bot1", open_id: "ou_1" },
175
+ });
176
+
177
+ await probeFeishu({ appId: "cli_aaa", appSecret: "s1" });
178
+ expect(requestFn).toHaveBeenCalledTimes(1);
179
+
180
+ // Different appId should trigger a new API call
181
+ await probeFeishu({ appId: "cli_bbb", appSecret: "s2" });
182
+ expect(requestFn).toHaveBeenCalledTimes(2);
183
+
184
+ // Same appId + appSecret as first call should return cached
185
+ await probeFeishu({ appId: "cli_aaa", appSecret: "s1" });
186
+ expect(requestFn).toHaveBeenCalledTimes(2);
187
+ });
188
+
189
+ it("does not share cache between accounts with same appId but different appSecret", async () => {
190
+ const requestFn = setupClient({
191
+ code: 0,
192
+ bot: { bot_name: "Bot1", open_id: "ou_1" },
193
+ });
194
+
195
+ // First account with appId + secret A
196
+ await probeFeishu({ appId: "cli_shared", appSecret: "secret_aaa" });
197
+ expect(requestFn).toHaveBeenCalledTimes(1);
198
+
199
+ // Second account with same appId but different secret (e.g. after rotation)
200
+ // must NOT reuse the cached result
201
+ await probeFeishu({ appId: "cli_shared", appSecret: "secret_bbb" });
202
+ expect(requestFn).toHaveBeenCalledTimes(2);
203
+ });
204
+
205
+ it("uses accountId for cache key when available", async () => {
206
+ const requestFn = setupClient({
207
+ code: 0,
208
+ bot: { bot_name: "Bot1", open_id: "ou_1" },
209
+ });
210
+
211
+ // Two accounts with same appId+appSecret but different accountIds are cached separately
212
+ await probeFeishu({ accountId: "acct-1", appId: "cli_123", appSecret: "secret" });
213
+ expect(requestFn).toHaveBeenCalledTimes(1);
214
+
215
+ await probeFeishu({ accountId: "acct-2", appId: "cli_123", appSecret: "secret" });
216
+ expect(requestFn).toHaveBeenCalledTimes(2);
217
+
218
+ // Same accountId should return cached
219
+ await probeFeishu({ accountId: "acct-1", appId: "cli_123", appSecret: "secret" });
220
+ expect(requestFn).toHaveBeenCalledTimes(2);
221
+ });
222
+
223
+ it("clearProbeCache forces fresh API call", async () => {
224
+ const requestFn = setupClient({
225
+ code: 0,
226
+ bot: { bot_name: "TestBot", open_id: "ou_abc123" },
227
+ });
228
+
229
+ const creds = { appId: "cli_123", appSecret: "secret" };
230
+ await probeFeishu(creds);
231
+ expect(requestFn).toHaveBeenCalledTimes(1);
232
+
233
+ clearProbeCache();
234
+
235
+ await probeFeishu(creds);
236
+ expect(requestFn).toHaveBeenCalledTimes(2);
237
+ });
238
+
239
+ it("handles response.data.bot fallback path", async () => {
240
+ setupClient({
241
+ code: 0,
242
+ data: { bot: { bot_name: "DataBot", open_id: "ou_data" } },
243
+ });
244
+
245
+ const result = await probeFeishu({ appId: "cli_123", appSecret: "secret" });
246
+ expect(result).toEqual({
247
+ ok: true,
248
+ appId: "cli_123",
249
+ botName: "DataBot",
250
+ botOpenId: "ou_data",
251
+ });
252
+ });
253
+ });
package/src/probe.ts CHANGED
@@ -1,23 +1,98 @@
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 successful probe results to reduce API calls (bot info is static).
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
+ * A 10-min TTL cuts that to ~4,320 calls/month. (#26684) */
9
+ const probeCache = new Map<string, { result: FeishuProbeResult; expiresAt: number }>();
10
+ const PROBE_CACHE_TTL_MS = 10 * 60 * 1000; // 10 minutes
11
+ const MAX_PROBE_CACHE_SIZE = 64;
12
+ export const FEISHU_PROBE_REQUEST_TIMEOUT_MS = 10_000;
13
+
14
+ export type ProbeFeishuOptions = {
15
+ timeoutMs?: number;
16
+ abortSignal?: AbortSignal;
17
+ };
18
+
19
+ type FeishuBotInfoResponse = {
20
+ code: number;
21
+ msg?: string;
22
+ bot?: { bot_name?: string; open_id?: string };
23
+ data?: { bot?: { bot_name?: string; open_id?: string } };
24
+ };
25
+
26
+ export async function probeFeishu(
27
+ creds?: FeishuClientCredentials,
28
+ options: ProbeFeishuOptions = {},
29
+ ): Promise<FeishuProbeResult> {
5
30
  if (!creds?.appId || !creds?.appSecret) {
6
31
  return {
7
32
  ok: false,
8
33
  error: "missing credentials (appId, appSecret)",
9
34
  };
10
35
  }
36
+ if (options.abortSignal?.aborted) {
37
+ return {
38
+ ok: false,
39
+ appId: creds.appId,
40
+ error: "probe aborted",
41
+ };
42
+ }
43
+
44
+ const timeoutMs = options.timeoutMs ?? FEISHU_PROBE_REQUEST_TIMEOUT_MS;
45
+
46
+ // Return cached result if still valid.
47
+ // Use accountId when available; otherwise include appSecret prefix so two
48
+ // accounts sharing the same appId (e.g. after secret rotation) don't
49
+ // pollute each other's cache entry.
50
+ const cacheKey = creds.accountId ?? `${creds.appId}:${creds.appSecret.slice(0, 8)}`;
51
+ const cached = probeCache.get(cacheKey);
52
+ if (cached && cached.expiresAt > Date.now()) {
53
+ return cached.result;
54
+ }
11
55
 
12
56
  try {
13
57
  const client = createFeishuClient(creds);
14
58
  // Use bot/v3/info API to get bot information
15
59
  // 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
- });
60
+ const responseResult = await raceWithTimeoutAndAbort<FeishuBotInfoResponse>(
61
+ (client as any).request({
62
+ method: "GET",
63
+ url: "/open-apis/bot/v3/info",
64
+ data: {},
65
+ timeout: timeoutMs,
66
+ }) as Promise<FeishuBotInfoResponse>,
67
+ {
68
+ timeoutMs,
69
+ abortSignal: options.abortSignal,
70
+ },
71
+ );
72
+
73
+ if (responseResult.status === "aborted") {
74
+ return {
75
+ ok: false,
76
+ appId: creds.appId,
77
+ error: "probe aborted",
78
+ };
79
+ }
80
+ if (responseResult.status === "timeout") {
81
+ return {
82
+ ok: false,
83
+ appId: creds.appId,
84
+ error: `probe timed out after ${timeoutMs}ms`,
85
+ };
86
+ }
87
+
88
+ const response = responseResult.value;
89
+ if (options.abortSignal?.aborted) {
90
+ return {
91
+ ok: false,
92
+ appId: creds.appId,
93
+ error: "probe aborted",
94
+ };
95
+ }
21
96
 
22
97
  if (response.code !== 0) {
23
98
  return {
@@ -28,12 +103,24 @@ export async function probeFeishu(creds?: FeishuClientCredentials): Promise<Feis
28
103
  }
29
104
 
30
105
  const bot = response.bot || response.data?.bot;
31
- return {
106
+ const result: FeishuProbeResult = {
32
107
  ok: true,
33
108
  appId: creds.appId,
34
109
  botName: bot?.bot_name,
35
110
  botOpenId: bot?.open_id,
36
111
  };
112
+
113
+ // Cache successful results only
114
+ probeCache.set(cacheKey, { result, expiresAt: Date.now() + PROBE_CACHE_TTL_MS });
115
+ // Evict oldest entry if cache exceeds max size
116
+ if (probeCache.size > MAX_PROBE_CACHE_SIZE) {
117
+ const oldest = probeCache.keys().next().value;
118
+ if (oldest !== undefined) {
119
+ probeCache.delete(oldest);
120
+ }
121
+ }
122
+
123
+ return result;
37
124
  } catch (err) {
38
125
  return {
39
126
  ok: false,
@@ -42,3 +129,8 @@ export async function probeFeishu(creds?: FeishuClientCredentials): Promise<Feis
42
129
  };
43
130
  }
44
131
  }
132
+
133
+ /** Clear the probe cache (for testing). */
134
+ export function clearProbeCache(): void {
135
+ probeCache.clear();
136
+ }
@@ -4,9 +4,12 @@ const resolveFeishuAccountMock = vi.hoisted(() => vi.fn());
4
4
  const getFeishuRuntimeMock = vi.hoisted(() => vi.fn());
5
5
  const sendMessageFeishuMock = vi.hoisted(() => vi.fn());
6
6
  const sendMarkdownCardFeishuMock = vi.hoisted(() => vi.fn());
7
+ const sendMediaFeishuMock = vi.hoisted(() => vi.fn());
7
8
  const createFeishuClientMock = vi.hoisted(() => vi.fn());
8
9
  const resolveReceiveIdTypeMock = vi.hoisted(() => vi.fn());
9
10
  const createReplyDispatcherWithTypingMock = vi.hoisted(() => vi.fn());
11
+ const addTypingIndicatorMock = vi.hoisted(() => vi.fn(async () => ({ messageId: "om_msg" })));
12
+ const removeTypingIndicatorMock = vi.hoisted(() => vi.fn(async () => {}));
10
13
  const streamingInstances = vi.hoisted(() => [] as any[]);
11
14
 
12
15
  vi.mock("./accounts.js", () => ({ resolveFeishuAccount: resolveFeishuAccountMock }));
@@ -15,8 +18,13 @@ vi.mock("./send.js", () => ({
15
18
  sendMessageFeishu: sendMessageFeishuMock,
16
19
  sendMarkdownCardFeishu: sendMarkdownCardFeishuMock,
17
20
  }));
21
+ vi.mock("./media.js", () => ({ sendMediaFeishu: sendMediaFeishuMock }));
18
22
  vi.mock("./client.js", () => ({ createFeishuClient: createFeishuClientMock }));
19
23
  vi.mock("./targets.js", () => ({ resolveReceiveIdType: resolveReceiveIdTypeMock }));
24
+ vi.mock("./typing.js", () => ({
25
+ addTypingIndicator: addTypingIndicatorMock,
26
+ removeTypingIndicator: removeTypingIndicatorMock,
27
+ }));
20
28
  vi.mock("./streaming-card.js", () => ({
21
29
  FeishuStreamingSession: class {
22
30
  active = false;
@@ -41,6 +49,7 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
41
49
  beforeEach(() => {
42
50
  vi.clearAllMocks();
43
51
  streamingInstances.length = 0;
52
+ sendMediaFeishuMock.mockResolvedValue(undefined);
44
53
 
45
54
  resolveFeishuAccountMock.mockReturnValue({
46
55
  accountId: "main",
@@ -80,6 +89,86 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
80
89
  });
81
90
  });
82
91
 
92
+ it("skips typing indicator when account typingIndicator is disabled", async () => {
93
+ resolveFeishuAccountMock.mockReturnValue({
94
+ accountId: "main",
95
+ appId: "app_id",
96
+ appSecret: "app_secret",
97
+ domain: "feishu",
98
+ config: {
99
+ renderMode: "auto",
100
+ streaming: true,
101
+ typingIndicator: false,
102
+ },
103
+ });
104
+
105
+ createFeishuReplyDispatcher({
106
+ cfg: {} as never,
107
+ agentId: "agent",
108
+ runtime: {} as never,
109
+ chatId: "oc_chat",
110
+ replyToMessageId: "om_parent",
111
+ });
112
+
113
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
114
+ await options.onReplyStart?.();
115
+
116
+ expect(addTypingIndicatorMock).not.toHaveBeenCalled();
117
+ });
118
+
119
+ it("skips typing indicator for stale replayed messages", async () => {
120
+ createFeishuReplyDispatcher({
121
+ cfg: {} as never,
122
+ agentId: "agent",
123
+ runtime: {} as never,
124
+ chatId: "oc_chat",
125
+ replyToMessageId: "om_parent",
126
+ messageCreateTimeMs: Date.now() - 3 * 60_000,
127
+ });
128
+
129
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
130
+ await options.onReplyStart?.();
131
+
132
+ expect(addTypingIndicatorMock).not.toHaveBeenCalled();
133
+ });
134
+
135
+ it("treats second-based timestamps as stale for typing suppression", async () => {
136
+ createFeishuReplyDispatcher({
137
+ cfg: {} as never,
138
+ agentId: "agent",
139
+ runtime: {} as never,
140
+ chatId: "oc_chat",
141
+ replyToMessageId: "om_parent",
142
+ messageCreateTimeMs: Math.floor((Date.now() - 3 * 60_000) / 1000),
143
+ });
144
+
145
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
146
+ await options.onReplyStart?.();
147
+
148
+ expect(addTypingIndicatorMock).not.toHaveBeenCalled();
149
+ });
150
+
151
+ it("keeps typing indicator for fresh messages", async () => {
152
+ createFeishuReplyDispatcher({
153
+ cfg: {} as never,
154
+ agentId: "agent",
155
+ runtime: {} as never,
156
+ chatId: "oc_chat",
157
+ replyToMessageId: "om_parent",
158
+ messageCreateTimeMs: Date.now() - 30_000,
159
+ });
160
+
161
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
162
+ await options.onReplyStart?.();
163
+
164
+ expect(addTypingIndicatorMock).toHaveBeenCalledTimes(1);
165
+ expect(addTypingIndicatorMock).toHaveBeenCalledWith(
166
+ expect.objectContaining({
167
+ messageId: "om_parent",
168
+ }),
169
+ );
170
+ });
171
+
83
172
  it("keeps auto mode plain text on non-streaming send path", async () => {
84
173
  createFeishuReplyDispatcher({
85
174
  cfg: {} as never,
@@ -102,6 +191,7 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
102
191
  agentId: "agent",
103
192
  runtime: { log: vi.fn(), error: vi.fn() } as never,
104
193
  chatId: "oc_chat",
194
+ rootId: "om_root_topic",
105
195
  });
106
196
 
107
197
  const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
@@ -109,8 +199,177 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
109
199
 
110
200
  expect(streamingInstances).toHaveLength(1);
111
201
  expect(streamingInstances[0].start).toHaveBeenCalledTimes(1);
202
+ expect(streamingInstances[0].start).toHaveBeenCalledWith("oc_chat", "chat_id", {
203
+ replyToMessageId: undefined,
204
+ replyInThread: undefined,
205
+ rootId: "om_root_topic",
206
+ });
112
207
  expect(streamingInstances[0].close).toHaveBeenCalledTimes(1);
113
208
  expect(sendMessageFeishuMock).not.toHaveBeenCalled();
114
209
  expect(sendMarkdownCardFeishuMock).not.toHaveBeenCalled();
115
210
  });
211
+
212
+ it("sends media-only payloads as attachments", async () => {
213
+ createFeishuReplyDispatcher({
214
+ cfg: {} as never,
215
+ agentId: "agent",
216
+ runtime: {} as never,
217
+ chatId: "oc_chat",
218
+ });
219
+
220
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
221
+ await options.deliver({ mediaUrl: "https://example.com/a.png" }, { kind: "final" });
222
+
223
+ expect(sendMediaFeishuMock).toHaveBeenCalledTimes(1);
224
+ expect(sendMediaFeishuMock).toHaveBeenCalledWith(
225
+ expect.objectContaining({
226
+ to: "oc_chat",
227
+ mediaUrl: "https://example.com/a.png",
228
+ }),
229
+ );
230
+ expect(sendMessageFeishuMock).not.toHaveBeenCalled();
231
+ expect(sendMarkdownCardFeishuMock).not.toHaveBeenCalled();
232
+ });
233
+
234
+ it("falls back to legacy mediaUrl when mediaUrls is an empty array", async () => {
235
+ createFeishuReplyDispatcher({
236
+ cfg: {} as never,
237
+ agentId: "agent",
238
+ runtime: {} as never,
239
+ chatId: "oc_chat",
240
+ });
241
+
242
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
243
+ await options.deliver(
244
+ { text: "caption", mediaUrl: "https://example.com/a.png", mediaUrls: [] },
245
+ { kind: "final" },
246
+ );
247
+
248
+ expect(sendMessageFeishuMock).toHaveBeenCalledTimes(1);
249
+ expect(sendMediaFeishuMock).toHaveBeenCalledTimes(1);
250
+ expect(sendMediaFeishuMock).toHaveBeenCalledWith(
251
+ expect.objectContaining({
252
+ mediaUrl: "https://example.com/a.png",
253
+ }),
254
+ );
255
+ });
256
+
257
+ it("sends attachments after streaming final markdown replies", async () => {
258
+ createFeishuReplyDispatcher({
259
+ cfg: {} as never,
260
+ agentId: "agent",
261
+ runtime: { log: vi.fn(), error: vi.fn() } as never,
262
+ chatId: "oc_chat",
263
+ });
264
+
265
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
266
+ await options.deliver(
267
+ { text: "```ts\nconst x = 1\n```", mediaUrls: ["https://example.com/a.png"] },
268
+ { kind: "final" },
269
+ );
270
+
271
+ expect(streamingInstances).toHaveLength(1);
272
+ expect(streamingInstances[0].start).toHaveBeenCalledTimes(1);
273
+ expect(streamingInstances[0].close).toHaveBeenCalledTimes(1);
274
+ expect(sendMediaFeishuMock).toHaveBeenCalledTimes(1);
275
+ expect(sendMediaFeishuMock).toHaveBeenCalledWith(
276
+ expect.objectContaining({
277
+ mediaUrl: "https://example.com/a.png",
278
+ }),
279
+ );
280
+ });
281
+
282
+ it("passes replyInThread to sendMessageFeishu for plain text", async () => {
283
+ createFeishuReplyDispatcher({
284
+ cfg: {} as never,
285
+ agentId: "agent",
286
+ runtime: {} as never,
287
+ chatId: "oc_chat",
288
+ replyToMessageId: "om_msg",
289
+ replyInThread: true,
290
+ });
291
+
292
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
293
+ await options.deliver({ text: "plain text" }, { kind: "final" });
294
+
295
+ expect(sendMessageFeishuMock).toHaveBeenCalledWith(
296
+ expect.objectContaining({
297
+ replyToMessageId: "om_msg",
298
+ replyInThread: true,
299
+ }),
300
+ );
301
+ });
302
+
303
+ it("passes replyInThread to sendMarkdownCardFeishu for card text", async () => {
304
+ resolveFeishuAccountMock.mockReturnValue({
305
+ accountId: "main",
306
+ appId: "app_id",
307
+ appSecret: "app_secret",
308
+ domain: "feishu",
309
+ config: {
310
+ renderMode: "card",
311
+ streaming: false,
312
+ },
313
+ });
314
+
315
+ createFeishuReplyDispatcher({
316
+ cfg: {} as never,
317
+ agentId: "agent",
318
+ runtime: {} as never,
319
+ chatId: "oc_chat",
320
+ replyToMessageId: "om_msg",
321
+ replyInThread: true,
322
+ });
323
+
324
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
325
+ await options.deliver({ text: "card text" }, { kind: "final" });
326
+
327
+ expect(sendMarkdownCardFeishuMock).toHaveBeenCalledWith(
328
+ expect.objectContaining({
329
+ replyToMessageId: "om_msg",
330
+ replyInThread: true,
331
+ }),
332
+ );
333
+ });
334
+
335
+ it("passes replyToMessageId and replyInThread to streaming.start()", async () => {
336
+ createFeishuReplyDispatcher({
337
+ cfg: {} as never,
338
+ agentId: "agent",
339
+ runtime: { log: vi.fn(), error: vi.fn() } as never,
340
+ chatId: "oc_chat",
341
+ replyToMessageId: "om_msg",
342
+ replyInThread: true,
343
+ });
344
+
345
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
346
+ await options.deliver({ text: "```ts\nconst x = 1\n```" }, { kind: "final" });
347
+
348
+ expect(streamingInstances).toHaveLength(1);
349
+ expect(streamingInstances[0].start).toHaveBeenCalledWith("oc_chat", "chat_id", {
350
+ replyToMessageId: "om_msg",
351
+ replyInThread: true,
352
+ });
353
+ });
354
+
355
+ it("passes replyInThread to media attachments", async () => {
356
+ createFeishuReplyDispatcher({
357
+ cfg: {} as never,
358
+ agentId: "agent",
359
+ runtime: {} as never,
360
+ chatId: "oc_chat",
361
+ replyToMessageId: "om_msg",
362
+ replyInThread: true,
363
+ });
364
+
365
+ const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
366
+ await options.deliver({ mediaUrl: "https://example.com/a.png" }, { kind: "final" });
367
+
368
+ expect(sendMediaFeishuMock).toHaveBeenCalledWith(
369
+ expect.objectContaining({
370
+ replyToMessageId: "om_msg",
371
+ replyInThread: true,
372
+ }),
373
+ );
374
+ });
116
375
  });