@kodelyth/feishu 2026.5.42 → 2026.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/klaw.plugin.json +1712 -47
  2. package/package.json +19 -6
  3. package/api.ts +0 -32
  4. package/channel-entry.ts +0 -20
  5. package/channel-plugin-api.ts +0 -1
  6. package/contract-api.ts +0 -16
  7. package/index.ts +0 -82
  8. package/runtime-api.ts +0 -52
  9. package/secret-contract-api.ts +0 -5
  10. package/security-contract-api.ts +0 -1
  11. package/session-key-api.ts +0 -1
  12. package/setup-api.ts +0 -3
  13. package/setup-entry.test.ts +0 -19
  14. package/setup-entry.ts +0 -13
  15. package/src/accounts.test.ts +0 -480
  16. package/src/accounts.ts +0 -333
  17. package/src/agent-config.ts +0 -21
  18. package/src/app-registration.ts +0 -331
  19. package/src/approval-auth.test.ts +0 -24
  20. package/src/approval-auth.ts +0 -25
  21. package/src/async.test.ts +0 -35
  22. package/src/async.ts +0 -104
  23. package/src/audio-preflight.runtime.ts +0 -9
  24. package/src/bitable.test.ts +0 -136
  25. package/src/bitable.ts +0 -762
  26. package/src/bot-content.ts +0 -485
  27. package/src/bot-group-name.test.ts +0 -116
  28. package/src/bot-runtime-api.ts +0 -12
  29. package/src/bot-sender-name.ts +0 -125
  30. package/src/bot.broadcast.test.ts +0 -523
  31. package/src/bot.card-action.test.ts +0 -552
  32. package/src/bot.checkBotMentioned.test.ts +0 -265
  33. package/src/bot.helpers.test.ts +0 -135
  34. package/src/bot.stripBotMention.test.ts +0 -126
  35. package/src/bot.test.ts +0 -3671
  36. package/src/bot.ts +0 -1703
  37. package/src/card-action.ts +0 -447
  38. package/src/card-interaction.test.ts +0 -131
  39. package/src/card-interaction.ts +0 -159
  40. package/src/card-test-helpers.ts +0 -54
  41. package/src/card-ux-approval.ts +0 -65
  42. package/src/card-ux-launcher.test.ts +0 -106
  43. package/src/card-ux-launcher.ts +0 -121
  44. package/src/card-ux-shared.ts +0 -33
  45. package/src/channel-runtime-api.ts +0 -16
  46. package/src/channel.runtime.ts +0 -47
  47. package/src/channel.test.ts +0 -1151
  48. package/src/channel.ts +0 -1423
  49. package/src/chat-schema.ts +0 -25
  50. package/src/chat.test.ts +0 -240
  51. package/src/chat.ts +0 -188
  52. package/src/client-timeout.ts +0 -42
  53. package/src/client.test.ts +0 -447
  54. package/src/client.ts +0 -262
  55. package/src/comment-dispatcher-runtime-api.ts +0 -6
  56. package/src/comment-dispatcher.test.ts +0 -185
  57. package/src/comment-dispatcher.ts +0 -107
  58. package/src/comment-handler-runtime-api.ts +0 -3
  59. package/src/comment-handler.test.ts +0 -592
  60. package/src/comment-handler.ts +0 -303
  61. package/src/comment-reaction.test.ts +0 -138
  62. package/src/comment-reaction.ts +0 -259
  63. package/src/comment-shared.test.ts +0 -183
  64. package/src/comment-shared.ts +0 -406
  65. package/src/comment-target.ts +0 -44
  66. package/src/config-schema.test.ts +0 -326
  67. package/src/config-schema.ts +0 -335
  68. package/src/conversation-id.test.ts +0 -18
  69. package/src/conversation-id.ts +0 -199
  70. package/src/dedup-runtime-api.ts +0 -1
  71. package/src/dedup.ts +0 -141
  72. package/src/dedupe-key.ts +0 -72
  73. package/src/directory.static.ts +0 -61
  74. package/src/directory.test.ts +0 -141
  75. package/src/directory.ts +0 -124
  76. package/src/doc-schema.ts +0 -182
  77. package/src/docx-batch-insert.test.ts +0 -116
  78. package/src/docx-batch-insert.ts +0 -223
  79. package/src/docx-color-text.ts +0 -154
  80. package/src/docx-table-ops.test.ts +0 -53
  81. package/src/docx-table-ops.ts +0 -316
  82. package/src/docx-types.ts +0 -38
  83. package/src/docx.account-selection.test.ts +0 -95
  84. package/src/docx.test.ts +0 -701
  85. package/src/docx.ts +0 -1596
  86. package/src/drive-schema.ts +0 -92
  87. package/src/drive.test.ts +0 -1237
  88. package/src/drive.ts +0 -829
  89. package/src/dynamic-agent.test.ts +0 -155
  90. package/src/dynamic-agent.ts +0 -143
  91. package/src/event-types.ts +0 -45
  92. package/src/external-keys.test.ts +0 -20
  93. package/src/external-keys.ts +0 -19
  94. package/src/lifecycle.test-support.ts +0 -220
  95. package/src/media.test.ts +0 -955
  96. package/src/media.ts +0 -1105
  97. package/src/mention-target.types.ts +0 -5
  98. package/src/mention.ts +0 -114
  99. package/src/message-action-contract.ts +0 -13
  100. package/src/monitor-state-runtime-api.ts +0 -7
  101. package/src/monitor-transport-runtime-api.ts +0 -10
  102. package/src/monitor.account.ts +0 -492
  103. package/src/monitor.acp-init-failure.lifecycle.test-support.ts +0 -219
  104. package/src/monitor.bot-identity.ts +0 -86
  105. package/src/monitor.bot-menu-handler.ts +0 -165
  106. package/src/monitor.bot-menu.lifecycle.test-support.ts +0 -224
  107. package/src/monitor.bot-menu.test.ts +0 -188
  108. package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +0 -264
  109. package/src/monitor.card-action.lifecycle.test-support.ts +0 -421
  110. package/src/monitor.cleanup.test.ts +0 -383
  111. package/src/monitor.comment-notice-handler.ts +0 -105
  112. package/src/monitor.comment.test.ts +0 -967
  113. package/src/monitor.comment.ts +0 -1386
  114. package/src/monitor.lifecycle.test.ts +0 -4
  115. package/src/monitor.message-handler.ts +0 -350
  116. package/src/monitor.reaction.lifecycle.test-support.ts +0 -68
  117. package/src/monitor.reaction.test.ts +0 -739
  118. package/src/monitor.startup.test.ts +0 -213
  119. package/src/monitor.startup.ts +0 -74
  120. package/src/monitor.state.defaults.test.ts +0 -46
  121. package/src/monitor.state.ts +0 -170
  122. package/src/monitor.synthetic-error.ts +0 -18
  123. package/src/monitor.test-mocks.ts +0 -46
  124. package/src/monitor.transport.ts +0 -451
  125. package/src/monitor.ts +0 -100
  126. package/src/monitor.webhook-e2e.test.ts +0 -279
  127. package/src/monitor.webhook-security.test.ts +0 -389
  128. package/src/monitor.webhook.test-helpers.ts +0 -116
  129. package/src/outbound-runtime-api.ts +0 -1
  130. package/src/outbound.test.ts +0 -1118
  131. package/src/outbound.ts +0 -785
  132. package/src/perm-schema.ts +0 -52
  133. package/src/perm.ts +0 -170
  134. package/src/pins.ts +0 -108
  135. package/src/policy.test.ts +0 -223
  136. package/src/policy.ts +0 -318
  137. package/src/post.test.ts +0 -105
  138. package/src/post.ts +0 -275
  139. package/src/probe.test.ts +0 -283
  140. package/src/probe.ts +0 -166
  141. package/src/processing-claims.ts +0 -59
  142. package/src/qr-terminal.ts +0 -1
  143. package/src/reactions.ts +0 -123
  144. package/src/reasoning-preview.test.ts +0 -113
  145. package/src/reasoning-preview.ts +0 -28
  146. package/src/reply-dispatcher-runtime-api.ts +0 -7
  147. package/src/reply-dispatcher.test.ts +0 -1513
  148. package/src/reply-dispatcher.ts +0 -748
  149. package/src/runtime.ts +0 -9
  150. package/src/secret-contract.ts +0 -145
  151. package/src/secret-input.ts +0 -1
  152. package/src/security-audit-shared.ts +0 -69
  153. package/src/security-audit.test.ts +0 -59
  154. package/src/security-audit.ts +0 -1
  155. package/src/send-result.ts +0 -80
  156. package/src/send-target.test.ts +0 -86
  157. package/src/send-target.ts +0 -35
  158. package/src/send.reply-fallback.test.ts +0 -417
  159. package/src/send.test.ts +0 -621
  160. package/src/send.ts +0 -861
  161. package/src/sequential-key.test.ts +0 -72
  162. package/src/sequential-key.ts +0 -25
  163. package/src/sequential-queue.test.ts +0 -165
  164. package/src/sequential-queue.ts +0 -86
  165. package/src/session-conversation.ts +0 -42
  166. package/src/session-route.ts +0 -48
  167. package/src/setup-core.ts +0 -51
  168. package/src/setup-surface.test.ts +0 -484
  169. package/src/setup-surface.ts +0 -618
  170. package/src/streaming-card.test.ts +0 -397
  171. package/src/streaming-card.ts +0 -571
  172. package/src/subagent-hooks.test.ts +0 -627
  173. package/src/subagent-hooks.ts +0 -413
  174. package/src/targets.ts +0 -97
  175. package/src/test-support/lifecycle-test-support.ts +0 -454
  176. package/src/thread-bindings.test.ts +0 -180
  177. package/src/thread-bindings.ts +0 -331
  178. package/src/tool-account-routing.test.ts +0 -250
  179. package/src/tool-account.test.ts +0 -44
  180. package/src/tool-account.ts +0 -93
  181. package/src/tool-factory-test-harness.ts +0 -79
  182. package/src/tool-result.test.ts +0 -32
  183. package/src/tool-result.ts +0 -16
  184. package/src/tools-config.test.ts +0 -21
  185. package/src/tools-config.ts +0 -22
  186. package/src/types.ts +0 -106
  187. package/src/typing.test.ts +0 -144
  188. package/src/typing.ts +0 -214
  189. package/src/wiki-schema.ts +0 -69
  190. package/src/wiki.ts +0 -270
  191. package/subagent-hooks-api.ts +0 -31
  192. package/tsconfig.json +0 -16
@@ -1,485 +0,0 @@
1
- import type { ClawdbotConfig } from "../runtime-api.js";
2
- import { buildFeishuConversationId } from "./conversation-id.js";
3
- import { normalizeFeishuExternalKey } from "./external-keys.js";
4
- import { saveMessageResourceFeishu } from "./media.js";
5
- import { isFeishuBroadcastMention } from "./mention.js";
6
- import { parsePostContent } from "./post.js";
7
- import { getFeishuRuntime } from "./runtime.js";
8
- import type { FeishuChatType, FeishuMediaInfo } from "./types.js";
9
-
10
- type FeishuMention = {
11
- key: string;
12
- id: {
13
- open_id?: string;
14
- user_id?: string;
15
- union_id?: string;
16
- };
17
- name: string;
18
- tenant_key?: string;
19
- };
20
-
21
- type FeishuMessageLike = {
22
- message: {
23
- content: string;
24
- message_type: string;
25
- mentions?: FeishuMention[];
26
- chat_id: string;
27
- root_id?: string;
28
- parent_id?: string;
29
- thread_id?: string;
30
- message_id: string;
31
- };
32
- sender: {
33
- sender_id: {
34
- open_id?: string;
35
- user_id?: string;
36
- };
37
- };
38
- };
39
-
40
- type GroupSessionScope = "group" | "group_sender" | "group_topic" | "group_topic_sender";
41
-
42
- type FeishuLogger = (...args: unknown[]) => void;
43
-
44
- type ResolvedFeishuGroupSession = {
45
- peerId: string;
46
- parentPeer: { kind: "group"; id: string } | null;
47
- groupSessionScope: GroupSessionScope;
48
- replyInThread: boolean;
49
- threadReply: boolean;
50
- };
51
-
52
- export function resolveFeishuGroupSession(params: {
53
- chatId: string;
54
- senderOpenId: string;
55
- messageId: string;
56
- rootId?: string;
57
- threadId?: string;
58
- chatType?: FeishuChatType;
59
- groupConfig?: {
60
- groupSessionScope?: GroupSessionScope;
61
- topicSessionMode?: "enabled" | "disabled";
62
- replyInThread?: "enabled" | "disabled";
63
- };
64
- feishuCfg?: {
65
- groupSessionScope?: GroupSessionScope;
66
- topicSessionMode?: "enabled" | "disabled";
67
- replyInThread?: "enabled" | "disabled";
68
- };
69
- }): ResolvedFeishuGroupSession {
70
- const { chatId, senderOpenId, messageId, rootId, threadId, chatType, groupConfig, feishuCfg } =
71
- params;
72
- const normalizedThreadId = threadId?.trim();
73
- const normalizedRootId = rootId?.trim();
74
- const threadReply = Boolean(normalizedThreadId || normalizedRootId);
75
- const replyInThread =
76
- (groupConfig?.replyInThread ?? feishuCfg?.replyInThread ?? "disabled") === "enabled" ||
77
- threadReply;
78
- const legacyTopicSessionMode =
79
- groupConfig?.topicSessionMode ?? feishuCfg?.topicSessionMode ?? "disabled";
80
- const groupSessionScope: GroupSessionScope =
81
- groupConfig?.groupSessionScope ??
82
- feishuCfg?.groupSessionScope ??
83
- (legacyTopicSessionMode === "enabled" ? "group_topic" : "group");
84
- const normalizedTopicGroupThreadId =
85
- chatType === "topic_group" ? (normalizedThreadId ?? normalizedRootId) : undefined;
86
- const topicScope =
87
- groupSessionScope === "group_topic" || groupSessionScope === "group_topic_sender"
88
- ? (normalizedTopicGroupThreadId ??
89
- normalizedRootId ??
90
- normalizedThreadId ??
91
- (replyInThread ? messageId : null))
92
- : null;
93
-
94
- let peerId = chatId;
95
- switch (groupSessionScope) {
96
- case "group_sender":
97
- peerId = buildFeishuConversationId({ chatId, scope: "group_sender", senderOpenId });
98
- break;
99
- case "group_topic":
100
- peerId = topicScope
101
- ? buildFeishuConversationId({ chatId, scope: "group_topic", topicId: topicScope })
102
- : chatId;
103
- break;
104
- case "group_topic_sender":
105
- peerId = topicScope
106
- ? buildFeishuConversationId({
107
- chatId,
108
- scope: "group_topic_sender",
109
- topicId: topicScope,
110
- senderOpenId,
111
- })
112
- : buildFeishuConversationId({ chatId, scope: "group_sender", senderOpenId });
113
- break;
114
- case "group":
115
- default:
116
- peerId = chatId;
117
- break;
118
- }
119
-
120
- return {
121
- peerId,
122
- parentPeer:
123
- topicScope &&
124
- (groupSessionScope === "group_topic" || groupSessionScope === "group_topic_sender")
125
- ? { kind: "group", id: chatId }
126
- : null,
127
- groupSessionScope,
128
- replyInThread,
129
- threadReply,
130
- };
131
- }
132
-
133
- export function parseMessageContent(content: string, messageType: string): string {
134
- if (messageType === "post") {
135
- return parsePostContent(content).textContent;
136
- }
137
-
138
- try {
139
- const parsed = JSON.parse(content);
140
- if (messageType === "text") {
141
- return parsed.text || "";
142
- }
143
- if (["image", "file", "audio", "video", "media", "sticker"].includes(messageType)) {
144
- if (messageType === "audio") {
145
- const speechToText =
146
- typeof parsed.speech_to_text === "string" ? parsed.speech_to_text.trim() : "";
147
- if (speechToText) {
148
- return speechToText;
149
- }
150
- }
151
- const placeholder = inferPlaceholder(messageType);
152
- const fileName = typeof parsed.file_name === "string" ? parsed.file_name.trim() : "";
153
- return fileName ? `${placeholder} (${fileName})` : placeholder;
154
- }
155
- if (messageType === "share_chat") {
156
- if (parsed && typeof parsed === "object") {
157
- const share = parsed as { body?: unknown; summary?: unknown; share_chat_id?: unknown };
158
- if (typeof share.body === "string" && share.body.trim()) {
159
- return share.body.trim();
160
- }
161
- if (typeof share.summary === "string" && share.summary.trim()) {
162
- return share.summary.trim();
163
- }
164
- if (typeof share.share_chat_id === "string" && share.share_chat_id.trim()) {
165
- return `[Forwarded message: ${share.share_chat_id.trim()}]`;
166
- }
167
- }
168
- return "[Forwarded message]";
169
- }
170
- if (messageType === "merge_forward") {
171
- return "[Merged and Forwarded Message - loading...]";
172
- }
173
- return content;
174
- } catch {
175
- return content;
176
- }
177
- }
178
-
179
- function formatSubMessageContent(content: string, contentType: string): string {
180
- try {
181
- const parsed = JSON.parse(content);
182
- switch (contentType) {
183
- case "text":
184
- return parsed.text || content;
185
- case "post":
186
- return parsePostContent(content).textContent;
187
- case "image":
188
- return "[Image]";
189
- case "file":
190
- return `[File: ${parsed.file_name || "unknown"}]`;
191
- case "audio":
192
- return "[Audio]";
193
- case "video":
194
- return "[Video]";
195
- case "sticker":
196
- return "[Sticker]";
197
- case "merge_forward":
198
- return "[Nested Merged Forward]";
199
- default:
200
- return `[${contentType}]`;
201
- }
202
- } catch {
203
- return content;
204
- }
205
- }
206
-
207
- export function parseMergeForwardContent(params: { content: string; log?: FeishuLogger }): string {
208
- const { content, log } = params;
209
- const maxMessages = 50;
210
- log?.("feishu: parsing merge_forward sub-messages from API response");
211
-
212
- let items: Array<{
213
- message_id?: string;
214
- msg_type?: string;
215
- body?: { content?: string };
216
- sender?: { id?: string };
217
- upper_message_id?: string;
218
- create_time?: string;
219
- }>;
220
- try {
221
- items = JSON.parse(content);
222
- } catch {
223
- log?.("feishu: merge_forward items parse failed");
224
- return "[Merged and Forwarded Message - parse error]";
225
- }
226
- if (!Array.isArray(items) || items.length === 0) {
227
- return "[Merged and Forwarded Message - no sub-messages]";
228
- }
229
- const subMessages = items.filter((item) => item.upper_message_id);
230
- if (subMessages.length === 0) {
231
- return "[Merged and Forwarded Message - no sub-messages found]";
232
- }
233
-
234
- log?.(`feishu: merge_forward contains ${subMessages.length} sub-messages`);
235
- subMessages.sort(
236
- (a, b) => Number.parseInt(a.create_time || "0", 10) - Number.parseInt(b.create_time || "0", 10),
237
- );
238
-
239
- const lines = ["[Merged and Forwarded Messages]"];
240
- for (const item of subMessages.slice(0, maxMessages)) {
241
- lines.push(`- ${formatSubMessageContent(item.body?.content || "", item.msg_type || "text")}`);
242
- }
243
- if (subMessages.length > maxMessages) {
244
- lines.push(`... and ${subMessages.length - maxMessages} more messages`);
245
- }
246
- return lines.join("\n");
247
- }
248
-
249
- export function checkBotMentioned(event: FeishuMessageLike, botOpenId?: string): boolean {
250
- if (!botOpenId) {
251
- return false;
252
- }
253
- const mentions = event.message.mentions ?? [];
254
- if (mentions.length > 0) {
255
- return mentions.some(
256
- (mention) => !isFeishuBroadcastMention(mention) && mention.id.open_id === botOpenId,
257
- );
258
- }
259
- if (event.message.message_type === "post") {
260
- return parsePostContent(event.message.content).mentionedOpenIds.some(
261
- (id) => id.trim().toLowerCase() !== "all" && id === botOpenId,
262
- );
263
- }
264
- return false;
265
- }
266
-
267
- export function normalizeMentions(
268
- text: string,
269
- mentions?: FeishuMention[],
270
- botStripId?: string,
271
- ): string {
272
- if (!mentions || mentions.length === 0) {
273
- return text;
274
- }
275
- const escaped = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
276
- const escapeName = (value: string) => value.replace(/</g, "&lt;").replace(/>/g, "&gt;");
277
- let result = text;
278
- for (const mention of mentions) {
279
- const mentionId = mention.id.open_id;
280
- const replacement =
281
- botStripId && mentionId === botStripId
282
- ? ""
283
- : mentionId
284
- ? `<at user_id="${mentionId}">${escapeName(mention.name)}</at>`
285
- : `@${mention.name}`;
286
- result = result.replace(new RegExp(escaped(mention.key), "g"), () => replacement).trim();
287
- }
288
- return result;
289
- }
290
-
291
- export function normalizeFeishuCommandProbeBody(text: string): string {
292
- if (!text) {
293
- return "";
294
- }
295
- return text
296
- .replace(/<at\b[^>]*>[^<]*<\/at>/giu, " ")
297
- .replace(/(^|\s)@[^/\s]+(?=\s|$|\/)/gu, "$1")
298
- .replace(/\s+/g, " ")
299
- .trim();
300
- }
301
-
302
- function parseMediaKeys(
303
- content: string,
304
- messageType: string,
305
- ): { imageKey?: string; fileKey?: string; fileName?: string } {
306
- try {
307
- const parsed = JSON.parse(content);
308
- const imageKey = normalizeFeishuExternalKey(parsed.image_key);
309
- const fileKey = normalizeFeishuExternalKey(parsed.file_key);
310
- switch (messageType) {
311
- case "image":
312
- return { imageKey, fileName: parsed.file_name };
313
- case "file":
314
- case "audio":
315
- case "sticker":
316
- return { fileKey, fileName: parsed.file_name };
317
- case "video":
318
- case "media":
319
- return { fileKey, imageKey, fileName: parsed.file_name };
320
- default:
321
- return {};
322
- }
323
- } catch {
324
- return {};
325
- }
326
- }
327
-
328
- export function toMessageResourceType(messageType: string): "image" | "file" {
329
- return messageType === "image" ? "image" : "file";
330
- }
331
-
332
- async function resolveSavedFeishuMedia(params: {
333
- result:
334
- | Awaited<ReturnType<typeof saveMessageResourceFeishu>>
335
- | { buffer: Buffer; contentType?: string; fileName?: string };
336
- maxBytes: number;
337
- originalFilename?: string;
338
- }) {
339
- if ("saved" in params.result) {
340
- return params.result.saved;
341
- }
342
- const core = getFeishuRuntime();
343
- const contentType =
344
- params.result.contentType ?? (await core.media.detectMime({ buffer: params.result.buffer }));
345
- return await core.channel.media.saveMediaBuffer(
346
- params.result.buffer,
347
- contentType,
348
- "inbound",
349
- params.maxBytes,
350
- params.result.fileName ?? params.originalFilename,
351
- );
352
- }
353
-
354
- function inferPlaceholder(messageType: string): string {
355
- switch (messageType) {
356
- case "image":
357
- return "<media:image>";
358
- case "file":
359
- return "<media:document>";
360
- case "audio":
361
- return "<media:audio>";
362
- case "video":
363
- case "media":
364
- return "<media:video>";
365
- case "sticker":
366
- return "<media:sticker>";
367
- default:
368
- return "<media:document>";
369
- }
370
- }
371
-
372
- export async function resolveFeishuMediaList(params: {
373
- cfg: ClawdbotConfig;
374
- messageId: string;
375
- messageType: string;
376
- content: string;
377
- maxBytes: number;
378
- log?: (msg: string) => void;
379
- accountId?: string;
380
- }): Promise<FeishuMediaInfo[]> {
381
- const { cfg, messageId, messageType, content, maxBytes, log, accountId } = params;
382
- const mediaTypes = ["image", "file", "audio", "video", "media", "sticker", "post"];
383
- if (!mediaTypes.includes(messageType)) {
384
- return [];
385
- }
386
-
387
- const out: FeishuMediaInfo[] = [];
388
- if (messageType === "post") {
389
- const { imageKeys, mediaKeys } = parsePostContent(content);
390
- if (imageKeys.length === 0 && mediaKeys.length === 0) {
391
- return [];
392
- }
393
- if (imageKeys.length > 0) {
394
- log?.(`feishu: post message contains ${imageKeys.length} embedded image(s)`);
395
- }
396
- if (mediaKeys.length > 0) {
397
- log?.(`feishu: post message contains ${mediaKeys.length} embedded media file(s)`);
398
- }
399
-
400
- for (const imageKey of imageKeys) {
401
- try {
402
- const result = await saveMessageResourceFeishu({
403
- cfg,
404
- messageId,
405
- fileKey: imageKey,
406
- type: "image",
407
- accountId,
408
- maxBytes,
409
- });
410
- const saved = await resolveSavedFeishuMedia({ result, maxBytes });
411
- out.push({
412
- path: saved.path,
413
- contentType: saved.contentType,
414
- placeholder: "<media:image>",
415
- });
416
- log?.(`feishu: downloaded embedded image ${imageKey}, saved to ${saved.path}`);
417
- } catch (err) {
418
- log?.(`feishu: failed to download embedded image ${imageKey}: ${String(err)}`);
419
- }
420
- }
421
-
422
- for (const media of mediaKeys) {
423
- try {
424
- const result = await saveMessageResourceFeishu({
425
- cfg,
426
- messageId,
427
- fileKey: media.fileKey,
428
- type: "file",
429
- accountId,
430
- maxBytes,
431
- originalFilename: media.fileName,
432
- });
433
- const saved = await resolveSavedFeishuMedia({
434
- result,
435
- maxBytes,
436
- originalFilename: media.fileName,
437
- });
438
- out.push({
439
- path: saved.path,
440
- contentType: saved.contentType,
441
- placeholder: "<media:video>",
442
- });
443
- log?.(`feishu: downloaded embedded media ${media.fileKey}, saved to ${saved.path}`);
444
- } catch (err) {
445
- log?.(`feishu: failed to download embedded media ${media.fileKey}: ${String(err)}`);
446
- }
447
- }
448
- return out;
449
- }
450
-
451
- const mediaKeys = parseMediaKeys(content, messageType);
452
- if (!mediaKeys.imageKey && !mediaKeys.fileKey) {
453
- return [];
454
- }
455
-
456
- try {
457
- const fileKey = mediaKeys.fileKey || mediaKeys.imageKey;
458
- if (!fileKey) {
459
- return [];
460
- }
461
- const result = await saveMessageResourceFeishu({
462
- cfg,
463
- messageId,
464
- fileKey,
465
- type: toMessageResourceType(messageType),
466
- accountId,
467
- maxBytes,
468
- originalFilename: mediaKeys.fileName,
469
- });
470
- const saved = await resolveSavedFeishuMedia({
471
- result,
472
- maxBytes,
473
- originalFilename: mediaKeys.fileName,
474
- });
475
- out.push({
476
- path: saved.path,
477
- contentType: saved.contentType,
478
- placeholder: inferPlaceholder(messageType),
479
- });
480
- log?.(`feishu: downloaded ${messageType} media, saved to ${saved.path}`);
481
- } catch (err) {
482
- log?.(`feishu: failed to download ${messageType} media: ${String(err)}`);
483
- }
484
- return out;
485
- }
@@ -1,116 +0,0 @@
1
- import { afterAll, describe, it, expect, vi, beforeEach } from "vitest";
2
- import { resolveGroupName, clearGroupNameCache } from "./bot.js";
3
- import type { ResolvedFeishuAccount } from "./types.js";
4
-
5
- const mockGetChatInfo = vi.hoisted(() => vi.fn());
6
- const mockCreateFeishuClient = vi.hoisted(() => vi.fn());
7
-
8
- vi.mock("./chat.js", () => ({ getChatInfo: mockGetChatInfo }));
9
- vi.mock("./client.js", () => ({ createFeishuClient: mockCreateFeishuClient }));
10
-
11
- function makeAccount(id = "test-account"): ResolvedFeishuAccount {
12
- return {
13
- accountId: id,
14
- selectionSource: "explicit",
15
- enabled: true,
16
- configured: true,
17
- appId: "cli_test",
18
- appSecret: "secret",
19
- domain: "feishu",
20
- config: {
21
- domain: "feishu",
22
- connectionMode: "websocket",
23
- webhookPath: "/feishu/events",
24
- dmPolicy: "pairing",
25
- reactionNotifications: "own",
26
- groupPolicy: "allowlist",
27
- typingIndicator: true,
28
- resolveSenderNames: true,
29
- },
30
- };
31
- }
32
-
33
- /**
34
- * Unit tests for resolveGroupName.
35
- *
36
- * Covers: successful lookup, API failure, empty name, positive cache,
37
- * negative cache, undefined response, and cross-account isolation.
38
- */
39
- describe("resolveGroupName", () => {
40
- const account = makeAccount();
41
- const log = vi.fn();
42
-
43
- afterAll(() => {
44
- vi.doUnmock("./chat.js");
45
- vi.doUnmock("./client.js");
46
- vi.resetModules();
47
- });
48
-
49
- beforeEach(() => {
50
- vi.clearAllMocks();
51
- mockGetChatInfo.mockReset();
52
- mockCreateFeishuClient.mockReset();
53
- mockCreateFeishuClient.mockReturnValue({});
54
- clearGroupNameCache();
55
- });
56
-
57
- it("returns the trimmed group name on successful API call", async () => {
58
- mockGetChatInfo.mockResolvedValue({ name: " Engineering Team " });
59
- const result = await resolveGroupName({ account, chatId: "oc_test1", log });
60
- expect(result).toBe("Engineering Team");
61
- expect(mockGetChatInfo).toHaveBeenCalledOnce();
62
- });
63
-
64
- it("returns undefined and logs on API failure", async () => {
65
- mockGetChatInfo.mockRejectedValue(new Error("network timeout"));
66
- const result = await resolveGroupName({ account, chatId: "oc_test2", log });
67
- expect(result).toBeUndefined();
68
- expect(log).toHaveBeenCalledWith(
69
- "feishu[test-account]: getChatInfo failed for oc_test2: Error: network timeout",
70
- );
71
- });
72
-
73
- it("returns undefined for whitespace-only name", async () => {
74
- mockGetChatInfo.mockResolvedValue({ name: " " });
75
- const result = await resolveGroupName({ account, chatId: "oc_test3", log });
76
- expect(result).toBeUndefined();
77
- });
78
-
79
- it("serves subsequent calls from cache (positive hit)", async () => {
80
- mockGetChatInfo.mockResolvedValue({ name: "Cached Group" });
81
- await resolveGroupName({ account, chatId: "oc_test4", log });
82
- const result = await resolveGroupName({ account, chatId: "oc_test4", log });
83
- expect(result).toBe("Cached Group");
84
- expect(mockGetChatInfo).toHaveBeenCalledOnce(); // only 1 API call
85
- });
86
-
87
- it("caches negative result (API failure) and skips retry", async () => {
88
- mockGetChatInfo.mockRejectedValue(new Error("fail"));
89
- await resolveGroupName({ account, chatId: "oc_test5", log });
90
- mockGetChatInfo.mockResolvedValue({ name: "Recovered" });
91
- const result = await resolveGroupName({ account, chatId: "oc_test5", log });
92
- expect(result).toBeUndefined(); // still cached negative
93
- expect(mockGetChatInfo).toHaveBeenCalledOnce();
94
- });
95
-
96
- it("returns undefined when API returns object with missing name field", async () => {
97
- mockGetChatInfo.mockResolvedValue({ name: undefined });
98
- const result = await resolveGroupName({ account, chatId: "oc_test6", log });
99
- expect(result).toBeUndefined();
100
- });
101
-
102
- it("isolates cache entries across different accounts", async () => {
103
- const accountA = makeAccount("account-A");
104
- const accountB = makeAccount("account-B");
105
- mockGetChatInfo
106
- .mockResolvedValueOnce({ name: "Team Alpha" })
107
- .mockResolvedValueOnce({ name: "Team Beta" });
108
-
109
- const nameA = await resolveGroupName({ account: accountA, chatId: "oc_shared", log });
110
- const nameB = await resolveGroupName({ account: accountB, chatId: "oc_shared", log });
111
-
112
- expect(nameA).toBe("Team Alpha");
113
- expect(nameB).toBe("Team Beta");
114
- expect(mockGetChatInfo).toHaveBeenCalledTimes(2); // separate API calls
115
- });
116
- });
@@ -1,12 +0,0 @@
1
- export {
2
- buildAgentMediaPayload,
3
- resolveChannelContextVisibilityMode,
4
- type ClawdbotConfig,
5
- type RuntimeEnv,
6
- } from "../runtime-api.js";
7
- export {
8
- evaluateSupplementalContextVisibility,
9
- filterSupplementalContextItems,
10
- normalizeAgentId,
11
- } from "../runtime-api.js";
12
- export { loadSessionStore, resolveSessionStoreEntry } from "../runtime-api.js";