@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,447 +0,0 @@
1
- import type { ClawdbotConfig, RuntimeEnv } from "../runtime-api.js";
2
- import { resolveFeishuRuntimeAccount } from "./accounts.js";
3
- import { handleFeishuMessage, type FeishuMessageEvent } from "./bot.js";
4
- import { decodeFeishuCardAction, buildFeishuCardActionTextFallback } from "./card-interaction.js";
5
- import {
6
- createApprovalCard,
7
- FEISHU_APPROVAL_CANCEL_ACTION,
8
- FEISHU_APPROVAL_CONFIRM_ACTION,
9
- FEISHU_APPROVAL_REQUEST_ACTION,
10
- } from "./card-ux-approval.js";
11
- import { createFeishuClient } from "./client.js";
12
- import { sendCardFeishu, sendMessageFeishu } from "./send.js";
13
-
14
- export type FeishuCardActionEvent = {
15
- operator: {
16
- open_id: string;
17
- user_id?: string;
18
- union_id?: string;
19
- };
20
- token: string;
21
- action: {
22
- value: Record<string, unknown>;
23
- tag: string;
24
- };
25
- open_message_id?: string;
26
- context: {
27
- open_message_id?: string;
28
- open_id?: string;
29
- user_id?: string;
30
- chat_id?: string;
31
- };
32
- };
33
-
34
- const FEISHU_APPROVAL_CARD_TTL_MS = 5 * 60_000;
35
- const FEISHU_CARD_ACTION_TOKEN_TTL_MS = 15 * 60_000;
36
- const processedCardActionTokens = new Map<
37
- string,
38
- { status: "inflight" | "completed"; expiresAt: number }
39
- >();
40
-
41
- export class FeishuRetryableCardActionError extends Error {
42
- constructor(message: string, options?: ErrorOptions) {
43
- super(message, options);
44
- this.name = "FeishuRetryableCardActionError";
45
- }
46
- }
47
-
48
- export function resetProcessedFeishuCardActionTokensForTests(): void {
49
- processedCardActionTokens.clear();
50
- }
51
-
52
- function pruneProcessedCardActionTokens(now: number): void {
53
- for (const [key, entry] of processedCardActionTokens.entries()) {
54
- if (entry.expiresAt <= now) {
55
- processedCardActionTokens.delete(key);
56
- }
57
- }
58
- }
59
-
60
- function beginFeishuCardActionToken(params: {
61
- token: string;
62
- accountId: string;
63
- now?: number;
64
- }): boolean {
65
- const now = params.now ?? Date.now();
66
- pruneProcessedCardActionTokens(now);
67
- const normalizedToken = params.token.trim();
68
- if (!normalizedToken) {
69
- return false;
70
- }
71
- const key = `${params.accountId}:${normalizedToken}`;
72
- const existing = processedCardActionTokens.get(key);
73
- if (existing && existing.expiresAt > now) {
74
- return false;
75
- }
76
- processedCardActionTokens.set(key, {
77
- status: "inflight",
78
- expiresAt: now + FEISHU_CARD_ACTION_TOKEN_TTL_MS,
79
- });
80
- return true;
81
- }
82
-
83
- function completeFeishuCardActionToken(params: {
84
- token: string;
85
- accountId: string;
86
- now?: number;
87
- }): void {
88
- const now = params.now ?? Date.now();
89
- const normalizedToken = params.token.trim();
90
- if (!normalizedToken) {
91
- return;
92
- }
93
- processedCardActionTokens.set(`${params.accountId}:${normalizedToken}`, {
94
- status: "completed",
95
- expiresAt: now + FEISHU_CARD_ACTION_TOKEN_TTL_MS,
96
- });
97
- }
98
-
99
- function releaseFeishuCardActionToken(params: { token: string; accountId: string }): void {
100
- const normalizedToken = params.token.trim();
101
- if (!normalizedToken) {
102
- return;
103
- }
104
- processedCardActionTokens.delete(`${params.accountId}:${normalizedToken}`);
105
- }
106
-
107
- function buildSyntheticMessageEvent(
108
- event: FeishuCardActionEvent,
109
- content: string,
110
- chatType: "p2p" | "group",
111
- ): FeishuMessageEvent {
112
- const replyTargetMessageId = event.context.open_message_id ?? event.open_message_id;
113
- return {
114
- sender: {
115
- sender_id: {
116
- open_id: event.operator.open_id,
117
- user_id: event.operator.user_id,
118
- union_id: event.operator.union_id,
119
- },
120
- },
121
- message: {
122
- message_id: `card-action-${event.token}`,
123
- ...(replyTargetMessageId ? { reply_target_message_id: replyTargetMessageId } : {}),
124
- ...(!replyTargetMessageId ? { suppress_reply_target: true } : {}),
125
- chat_id: event.context.chat_id || event.operator.open_id,
126
- chat_type: chatType,
127
- message_type: "text",
128
- content: JSON.stringify({ text: content }),
129
- },
130
- };
131
- }
132
-
133
- function resolveCallbackTarget(event: FeishuCardActionEvent): string {
134
- const chatId = event.context.chat_id?.trim();
135
- if (chatId) {
136
- return `chat:${chatId}`;
137
- }
138
- return `user:${event.operator.open_id}`;
139
- }
140
-
141
- async function dispatchSyntheticCommand(params: {
142
- cfg: ClawdbotConfig;
143
- event: FeishuCardActionEvent;
144
- command: string;
145
- account: ReturnType<typeof resolveFeishuRuntimeAccount>;
146
- botOpenId?: string;
147
- runtime?: RuntimeEnv;
148
- accountId?: string;
149
- chatType?: "p2p" | "group";
150
- }): Promise<void> {
151
- const resolvedChatType = await resolveCardActionChatType({
152
- event: params.event,
153
- account: params.account,
154
- chatType: params.chatType,
155
- log: params.runtime?.log ?? console.log,
156
- });
157
- await handleFeishuMessage({
158
- cfg: params.cfg,
159
- event: buildSyntheticMessageEvent(params.event, params.command, resolvedChatType),
160
- botOpenId: params.botOpenId,
161
- runtime: params.runtime,
162
- accountId: params.accountId,
163
- });
164
- }
165
-
166
- // Feishu's im.chat.get returns two fields:
167
- // chat_mode: conversation type — "p2p" | "group" | "topic"
168
- // chat_type: privacy classification — "private" | "public"
169
- // We check chat_mode first because it directly indicates conversation type.
170
- // "private" maps to "p2p" as the safe-failure direction (restrictive DM
171
- // policy) — a private group chat misclassified as p2p is safer than the
172
- // reverse. "topic" and "public" are treated as group semantics.
173
- function normalizeResolvedCardActionChatType(value: unknown): "p2p" | "group" | undefined {
174
- if (value === "group" || value === "topic" || value === "public") {
175
- return "group";
176
- }
177
- if (value === "p2p" || value === "private") {
178
- return "p2p";
179
- }
180
- return undefined;
181
- }
182
-
183
- const resolvedChatTypeCache = new Map<string, { value: "p2p" | "group"; expiresAt: number }>();
184
- const CHAT_TYPE_CACHE_TTL_MS = 30 * 60_000;
185
- const CHAT_TYPE_CACHE_MAX_SIZE = 5_000;
186
-
187
- function pruneChatTypeCache(now: number): void {
188
- for (const [key, entry] of resolvedChatTypeCache.entries()) {
189
- if (entry.expiresAt <= now) {
190
- resolvedChatTypeCache.delete(key);
191
- }
192
- }
193
- if (resolvedChatTypeCache.size > CHAT_TYPE_CACHE_MAX_SIZE) {
194
- const excess = resolvedChatTypeCache.size - CHAT_TYPE_CACHE_MAX_SIZE;
195
- const iter = resolvedChatTypeCache.keys();
196
- for (let i = 0; i < excess; i++) {
197
- const key = iter.next().value;
198
- if (key !== undefined) {
199
- resolvedChatTypeCache.delete(key);
200
- }
201
- }
202
- }
203
- }
204
-
205
- function sanitizeLogValue(v: string): string {
206
- return v.replace(/[\r\n]/g, " ").slice(0, 500);
207
- }
208
-
209
- async function resolveCardActionChatType(params: {
210
- event: FeishuCardActionEvent;
211
- account: ReturnType<typeof resolveFeishuRuntimeAccount>;
212
- chatType?: "p2p" | "group";
213
- log: (message: string) => void;
214
- }): Promise<"p2p" | "group"> {
215
- const explicitChatType = normalizeResolvedCardActionChatType(params.chatType);
216
- if (explicitChatType) {
217
- return explicitChatType;
218
- }
219
-
220
- const chatId = params.event.context.chat_id?.trim();
221
- if (!chatId) {
222
- return "p2p";
223
- }
224
-
225
- const cacheKey = `${params.account.accountId}:${chatId}`;
226
- const now = Date.now();
227
- pruneChatTypeCache(now);
228
- const cached = resolvedChatTypeCache.get(cacheKey);
229
- if (cached) {
230
- return cached.value;
231
- }
232
-
233
- try {
234
- const response = (await createFeishuClient(params.account).im.chat.get({
235
- path: { chat_id: chatId },
236
- })) as { code?: number; msg?: string; data?: { chat_type?: unknown; chat_mode?: unknown } };
237
- if (response.code === 0) {
238
- const resolvedChatType =
239
- normalizeResolvedCardActionChatType(response.data?.chat_mode) ??
240
- normalizeResolvedCardActionChatType(response.data?.chat_type);
241
- if (resolvedChatType) {
242
- resolvedChatTypeCache.set(cacheKey, {
243
- value: resolvedChatType,
244
- expiresAt: now + CHAT_TYPE_CACHE_TTL_MS,
245
- });
246
- return resolvedChatType;
247
- }
248
- params.log(
249
- `feishu[${params.account.accountId}]: card action missing chat type for chat; defaulting to p2p`,
250
- );
251
- } else {
252
- params.log(
253
- `feishu[${params.account.accountId}]: failed to resolve chat type: ${sanitizeLogValue(response.msg ?? "unknown error")}; defaulting to p2p`,
254
- );
255
- }
256
- } catch (err) {
257
- const message = err instanceof Error ? err.message : "unknown";
258
- params.log(
259
- `feishu[${params.account.accountId}]: failed to resolve chat type: ${sanitizeLogValue(message)}; defaulting to p2p`,
260
- );
261
- }
262
-
263
- return "p2p";
264
- }
265
-
266
- async function sendInvalidInteractionNotice(params: {
267
- cfg: ClawdbotConfig;
268
- event: FeishuCardActionEvent;
269
- reason: "malformed" | "stale" | "wrong_user" | "wrong_conversation";
270
- accountId?: string;
271
- }): Promise<void> {
272
- const reasonText =
273
- params.reason === "stale"
274
- ? "This card action has expired. Open a fresh launcher card and try again."
275
- : params.reason === "wrong_user"
276
- ? "This card action belongs to a different user."
277
- : params.reason === "wrong_conversation"
278
- ? "This card action belongs to a different conversation."
279
- : "This card action payload is invalid.";
280
-
281
- await sendMessageFeishu({
282
- cfg: params.cfg,
283
- to: resolveCallbackTarget(params.event),
284
- text: `⚠️ ${reasonText}`,
285
- accountId: params.accountId,
286
- });
287
- }
288
-
289
- export async function handleFeishuCardAction(params: {
290
- cfg: ClawdbotConfig;
291
- event: FeishuCardActionEvent;
292
- botOpenId?: string;
293
- runtime?: RuntimeEnv;
294
- accountId?: string;
295
- }): Promise<void> {
296
- const { cfg, event, runtime, accountId } = params;
297
- const account = resolveFeishuRuntimeAccount({ cfg, accountId });
298
- const log = runtime?.log ?? console.log;
299
- if (!event.token.trim()) {
300
- log(
301
- `feishu[${account.accountId}]: rejected card action from ${event.operator.open_id}: missing token`,
302
- );
303
- return;
304
- }
305
- const decoded = decodeFeishuCardAction({ event });
306
- const claimedToken = beginFeishuCardActionToken({
307
- token: event.token,
308
- accountId: account.accountId,
309
- });
310
- if (!claimedToken) {
311
- log(`feishu[${account.accountId}]: skipping duplicate card action token ${event.token}`);
312
- return;
313
- }
314
-
315
- try {
316
- if (decoded.kind === "invalid") {
317
- log(
318
- `feishu[${account.accountId}]: rejected card action from ${event.operator.open_id}: ${decoded.reason}`,
319
- );
320
- await sendInvalidInteractionNotice({
321
- cfg,
322
- event,
323
- reason: decoded.reason,
324
- accountId,
325
- });
326
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
327
- return;
328
- }
329
-
330
- if (decoded.kind === "structured") {
331
- const { envelope } = decoded;
332
- log(
333
- `feishu[${account.accountId}]: handling structured card action ${envelope.a} from ${event.operator.open_id}`,
334
- );
335
-
336
- if (envelope.a === FEISHU_APPROVAL_REQUEST_ACTION) {
337
- const command = typeof envelope.m?.command === "string" ? envelope.m.command.trim() : "";
338
- if (!command) {
339
- await sendInvalidInteractionNotice({
340
- cfg,
341
- event,
342
- reason: "malformed",
343
- accountId,
344
- });
345
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
346
- return;
347
- }
348
- const prompt =
349
- typeof envelope.m?.prompt === "string" && envelope.m.prompt.trim()
350
- ? envelope.m.prompt
351
- : `Run \`${command}\` in this Feishu conversation?`;
352
- await sendCardFeishu({
353
- cfg,
354
- to: resolveCallbackTarget(event),
355
- card: createApprovalCard({
356
- operatorOpenId: event.operator.open_id,
357
- chatId: event.context.chat_id || undefined,
358
- command,
359
- prompt,
360
- sessionKey: envelope.c?.s,
361
- expiresAt: Date.now() + FEISHU_APPROVAL_CARD_TTL_MS,
362
- chatType: await resolveCardActionChatType({
363
- event,
364
- account,
365
- chatType: envelope.c?.t,
366
- log,
367
- }),
368
- confirmLabel: command === "/reset" ? "Reset" : "Confirm",
369
- }),
370
- accountId,
371
- });
372
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
373
- return;
374
- }
375
-
376
- if (envelope.a === FEISHU_APPROVAL_CANCEL_ACTION) {
377
- await sendMessageFeishu({
378
- cfg,
379
- to: resolveCallbackTarget(event),
380
- text: "Cancelled.",
381
- accountId,
382
- });
383
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
384
- return;
385
- }
386
-
387
- if (envelope.a === FEISHU_APPROVAL_CONFIRM_ACTION || envelope.k === "quick") {
388
- const command = envelope.q?.trim();
389
- if (!command) {
390
- await sendInvalidInteractionNotice({
391
- cfg,
392
- event,
393
- reason: "malformed",
394
- accountId,
395
- });
396
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
397
- return;
398
- }
399
- await dispatchSyntheticCommand({
400
- cfg,
401
- event,
402
- command,
403
- account,
404
- botOpenId: params.botOpenId,
405
- runtime,
406
- accountId,
407
- chatType: envelope.c?.t,
408
- });
409
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
410
- return;
411
- }
412
-
413
- await sendInvalidInteractionNotice({
414
- cfg,
415
- event,
416
- reason: "malformed",
417
- accountId,
418
- });
419
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
420
- return;
421
- }
422
-
423
- const content = buildFeishuCardActionTextFallback(event);
424
-
425
- log(
426
- `feishu[${account.accountId}]: handling card action from ${event.operator.open_id}: ${content}`,
427
- );
428
-
429
- await dispatchSyntheticCommand({
430
- cfg,
431
- event,
432
- command: content,
433
- account,
434
- botOpenId: params.botOpenId,
435
- runtime,
436
- accountId,
437
- });
438
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
439
- } catch (err) {
440
- if (err instanceof FeishuRetryableCardActionError) {
441
- releaseFeishuCardActionToken({ token: event.token, accountId: account.accountId });
442
- } else {
443
- completeFeishuCardActionToken({ token: event.token, accountId: account.accountId });
444
- }
445
- throw err;
446
- }
447
- }
@@ -1,131 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- buildFeishuCardActionTextFallback,
4
- createFeishuCardInteractionEnvelope,
5
- decodeFeishuCardAction,
6
- } from "./card-interaction.js";
7
-
8
- describe("feishu card interaction decoder", () => {
9
- it("decodes valid structured payloads", () => {
10
- const result = decodeFeishuCardAction({
11
- now: 1_700_000_000_000,
12
- event: {
13
- operator: { open_id: "u123" },
14
- context: { chat_id: "chat1" },
15
- action: {
16
- value: createFeishuCardInteractionEnvelope({
17
- k: "quick",
18
- a: "feishu.quick_actions.help",
19
- q: "/help",
20
- c: { u: "u123", h: "chat1", t: "group", e: 1_700_000_060_000 },
21
- }),
22
- },
23
- },
24
- });
25
-
26
- expect(result).toEqual({
27
- kind: "structured",
28
- envelope: {
29
- oc: "ocf1",
30
- k: "quick",
31
- a: "feishu.quick_actions.help",
32
- q: "/help",
33
- c: { u: "u123", h: "chat1", t: "group", e: 1_700_000_060_000 },
34
- },
35
- });
36
- });
37
-
38
- it("falls back for legacy text-like payloads", () => {
39
- const result = decodeFeishuCardAction({
40
- event: {
41
- operator: { open_id: "u123" },
42
- context: { chat_id: "chat1" },
43
- action: { value: { text: "/ping" } },
44
- },
45
- });
46
-
47
- expect(result).toEqual({ kind: "legacy", text: "/ping" });
48
- expect(
49
- buildFeishuCardActionTextFallback({
50
- operator: { open_id: "u123" },
51
- context: { chat_id: "chat1" },
52
- action: { value: { command: "/new" } },
53
- }),
54
- ).toBe("/new");
55
- });
56
-
57
- it("rejects malformed structured payloads", () => {
58
- const result = decodeFeishuCardAction({
59
- event: {
60
- operator: { open_id: "u123" },
61
- context: { chat_id: "chat1" },
62
- action: {
63
- value: {
64
- oc: "ocf1",
65
- k: "quick",
66
- a: "broken",
67
- m: { bad: { nested: true } },
68
- },
69
- },
70
- },
71
- });
72
-
73
- expect(result).toEqual({ kind: "invalid", reason: "malformed" });
74
- });
75
-
76
- it("rejects stale payloads", () => {
77
- const result = decodeFeishuCardAction({
78
- now: 100,
79
- event: {
80
- operator: { open_id: "u123" },
81
- context: { chat_id: "chat1" },
82
- action: {
83
- value: createFeishuCardInteractionEnvelope({
84
- k: "button",
85
- a: "stale",
86
- c: { e: 99, t: "group" },
87
- }),
88
- },
89
- },
90
- });
91
-
92
- expect(result).toEqual({ kind: "invalid", reason: "stale" });
93
- });
94
-
95
- it("rejects wrong-conversation payloads when chat context is enforced", () => {
96
- const result = decodeFeishuCardAction({
97
- event: {
98
- operator: { open_id: "u123" },
99
- context: { chat_id: "chat2" },
100
- action: {
101
- value: createFeishuCardInteractionEnvelope({
102
- k: "button",
103
- a: "scoped",
104
- c: { u: "u123", h: "chat1", t: "group", e: Date.now() + 60_000 },
105
- }),
106
- },
107
- },
108
- });
109
-
110
- expect(result).toEqual({ kind: "invalid", reason: "wrong_conversation" });
111
- });
112
-
113
- it("rejects malformed chat-type context", () => {
114
- const result = decodeFeishuCardAction({
115
- event: {
116
- operator: { open_id: "u123" },
117
- context: { chat_id: "chat1" },
118
- action: {
119
- value: {
120
- oc: "ocf1",
121
- k: "button",
122
- a: "bad",
123
- c: { t: "private" },
124
- },
125
- },
126
- },
127
- });
128
-
129
- expect(result).toEqual({ kind: "invalid", reason: "malformed" });
130
- });
131
- });