@kodelyth/feishu 2026.5.42 → 2026.6.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 (192) hide show
  1. package/klaw.plugin.json +1712 -47
  2. package/package.json +17 -4
  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
package/src/outbound.ts DELETED
@@ -1,785 +0,0 @@
1
- import path from "node:path";
2
- import {
3
- attachChannelToResult,
4
- createAttachedChannelResultAdapter,
5
- } from "klaw/plugin-sdk/channel-send-result";
6
- import {
7
- interactiveReplyToPresentation,
8
- normalizeInteractiveReply,
9
- normalizeMessagePresentation,
10
- renderMessagePresentationFallbackText,
11
- resolveInteractiveTextFallback,
12
- type MessagePresentationBlock,
13
- type MessagePresentationButton,
14
- } from "klaw/plugin-sdk/interactive-runtime";
15
- import {
16
- resolvePayloadMediaUrls,
17
- sendPayloadMediaSequenceAndFinalize,
18
- sendTextMediaPayload,
19
- } from "klaw/plugin-sdk/reply-payload";
20
- import { statRegularFileSync } from "klaw/plugin-sdk/security-runtime";
21
- import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
22
- import { resolveFeishuAccount } from "./accounts.js";
23
- import { createFeishuCardInteractionEnvelope } from "./card-interaction.js";
24
- import { createFeishuClient } from "./client.js";
25
- import { cleanupAmbientCommentTypingReaction } from "./comment-reaction.js";
26
- import { parseFeishuCommentTarget } from "./comment-target.js";
27
- import { deliverCommentThreadText } from "./drive.js";
28
- import { sendMediaFeishu, shouldSuppressFeishuTextForVoiceMedia } from "./media.js";
29
- import { chunkTextForOutbound, type ChannelOutboundAdapter } from "./outbound-runtime-api.js";
30
- import {
31
- resolveFeishuCardTemplate,
32
- sendCardFeishu,
33
- sendMarkdownCardFeishu,
34
- sendMessageFeishu,
35
- sendStructuredCardFeishu,
36
- } from "./send.js";
37
-
38
- const RENDERED_FEISHU_CARD = Symbol("klaw.renderedFeishuCard");
39
-
40
- function normalizePossibleLocalImagePath(text: string | undefined): string | null {
41
- const raw = text?.trim();
42
- if (!raw) {
43
- return null;
44
- }
45
-
46
- // Only auto-convert when the message is a pure path-like payload.
47
- // Avoid converting regular sentences that merely contain a path.
48
- const hasWhitespace = /\s/.test(raw);
49
- if (hasWhitespace) {
50
- return null;
51
- }
52
-
53
- // Ignore links/data URLs; those should stay in normal mediaUrl/text paths.
54
- if (/^(https?:\/\/|data:|file:\/\/)/i.test(raw)) {
55
- return null;
56
- }
57
-
58
- const ext = normalizeLowercaseStringOrEmpty(path.extname(raw));
59
- const isImageExt = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".ico", ".tiff"].includes(
60
- ext,
61
- );
62
- if (!isImageExt) {
63
- return null;
64
- }
65
-
66
- if (!path.isAbsolute(raw)) {
67
- return null;
68
- }
69
- try {
70
- const stat = statRegularFileSync(raw);
71
- if (stat.missing) {
72
- return null;
73
- }
74
- } catch {
75
- return null;
76
- }
77
-
78
- return raw;
79
- }
80
-
81
- function shouldUseCard(text: string): boolean {
82
- return /```[\s\S]*?```/.test(text) || /\|.+\|[\r\n]+\|[-:| ]+\|/.test(text);
83
- }
84
-
85
- function isRecord(value: unknown): value is Record<string, unknown> {
86
- return Boolean(value && typeof value === "object" && !Array.isArray(value));
87
- }
88
-
89
- function escapeFeishuCardMarkdownText(text: string): string {
90
- return text.replace(/[&<>]/g, (char) => {
91
- switch (char) {
92
- case "&":
93
- return "&amp;";
94
- case "<":
95
- return "&lt;";
96
- case ">":
97
- return "&gt;";
98
- default:
99
- return char;
100
- }
101
- });
102
- }
103
-
104
- function resolveSafeFeishuButtonUrl(url: string | undefined): string | undefined {
105
- const trimmed = url?.trim();
106
- if (!trimmed) {
107
- return undefined;
108
- }
109
- try {
110
- const parsed = new URL(trimmed);
111
- return parsed.protocol === "https:" || parsed.protocol === "http:" ? trimmed : undefined;
112
- } catch {
113
- return undefined;
114
- }
115
- }
116
-
117
- function markRenderedFeishuCard(card: Record<string, unknown>): Record<string, unknown> {
118
- Object.defineProperty(card, RENDERED_FEISHU_CARD, {
119
- value: true,
120
- enumerable: false,
121
- });
122
- return card;
123
- }
124
-
125
- function sanitizeNativeFeishuCardButton(button: unknown): Record<string, unknown> | undefined {
126
- if (!isRecord(button)) {
127
- return undefined;
128
- }
129
- const text =
130
- isRecord(button.text) && typeof button.text.content === "string"
131
- ? button.text.content
132
- : undefined;
133
- if (!text?.trim()) {
134
- return undefined;
135
- }
136
- const style =
137
- button.type === "danger" ? "danger" : button.type === "primary" ? "primary" : undefined;
138
- const rendered: Record<string, unknown> = {
139
- tag: "button",
140
- text: { tag: "plain_text", content: text },
141
- type: mapFeishuButtonType(style),
142
- };
143
- const safeUrl = resolveSafeFeishuButtonUrl(
144
- typeof button.url === "string" ? button.url : undefined,
145
- );
146
- if (safeUrl) {
147
- rendered.url = safeUrl;
148
- }
149
- if (isRecord(button.value) && button.value.oc === "ocf1") {
150
- rendered.value = button.value;
151
- }
152
- return rendered.url || rendered.value ? rendered : undefined;
153
- }
154
-
155
- function sanitizeNativeFeishuCardElement(element: unknown): Record<string, unknown> | undefined {
156
- if (!isRecord(element) || typeof element.tag !== "string") {
157
- return undefined;
158
- }
159
- if (element.tag === "hr") {
160
- return { tag: "hr" };
161
- }
162
- if (element.tag === "markdown" && typeof element.content === "string") {
163
- return { tag: "markdown", content: escapeFeishuCardMarkdownText(element.content) };
164
- }
165
- if (element.tag === "action" && Array.isArray(element.actions)) {
166
- const actions = element.actions
167
- .map((action) => sanitizeNativeFeishuCardButton(action))
168
- .filter((action): action is Record<string, unknown> => Boolean(action));
169
- return actions.length > 0 ? { tag: "action", actions } : undefined;
170
- }
171
- return undefined;
172
- }
173
-
174
- function sanitizeNativeFeishuCard(
175
- card: Record<string, unknown>,
176
- ): Record<string, unknown> | undefined {
177
- const body = isRecord(card.body) ? card.body : undefined;
178
- const rawElements = Array.isArray(body?.elements) ? body.elements : [];
179
- const elements = rawElements
180
- .map((element) => sanitizeNativeFeishuCardElement(element))
181
- .filter((element): element is Record<string, unknown> => Boolean(element));
182
- if (elements.length === 0) {
183
- return undefined;
184
- }
185
-
186
- const header = isRecord(card.header) ? card.header : undefined;
187
- const title =
188
- isRecord(header?.title) && typeof header.title.content === "string"
189
- ? header.title.content
190
- : undefined;
191
- return markRenderedFeishuCard({
192
- schema: "2.0",
193
- config: { width_mode: "fill" },
194
- ...(title?.trim()
195
- ? {
196
- header: {
197
- title: { tag: "plain_text", content: title },
198
- template:
199
- resolveFeishuCardTemplate(
200
- typeof header?.template === "string" ? header.template : undefined,
201
- ) ?? "blue",
202
- },
203
- }
204
- : {}),
205
- body: { elements },
206
- });
207
- }
208
-
209
- function readNativeFeishuCard(payload: { channelData?: Record<string, unknown> }) {
210
- const feishuData = payload.channelData?.feishu;
211
- if (!isRecord(feishuData)) {
212
- return undefined;
213
- }
214
- const card = feishuData.card ?? feishuData.interactiveCard;
215
- if (!isRecord(card)) {
216
- return undefined;
217
- }
218
- if ((card as { [RENDERED_FEISHU_CARD]?: true })[RENDERED_FEISHU_CARD] === true) {
219
- return card;
220
- }
221
- return sanitizeNativeFeishuCard(card);
222
- }
223
-
224
- function mapFeishuButtonType(style: MessagePresentationButton["style"]) {
225
- if (style === "primary" || style === "success") {
226
- return "primary";
227
- }
228
- if (style === "danger") {
229
- return "danger";
230
- }
231
- return "default";
232
- }
233
-
234
- function buildFeishuPayloadButton(
235
- button: MessagePresentationButton,
236
- ): Record<string, unknown> | undefined {
237
- const rendered: Record<string, unknown> = {
238
- tag: "button",
239
- text: {
240
- tag: "plain_text",
241
- content: button.label,
242
- },
243
- type: mapFeishuButtonType(button.style),
244
- };
245
- if (button.url) {
246
- const safeUrl = resolveSafeFeishuButtonUrl(button.url);
247
- if (safeUrl) {
248
- rendered.url = safeUrl;
249
- }
250
- }
251
- if (button.value) {
252
- rendered.value = createFeishuCardInteractionEnvelope({
253
- k: "quick",
254
- a: "feishu.payload.button",
255
- q: button.value,
256
- });
257
- }
258
- return rendered.url || rendered.value ? rendered : undefined;
259
- }
260
-
261
- function buildFeishuCardElementForBlock(
262
- block: MessagePresentationBlock,
263
- ): Record<string, unknown> | undefined {
264
- if (block.type === "text") {
265
- return { tag: "markdown", content: escapeFeishuCardMarkdownText(block.text) };
266
- }
267
- if (block.type === "context") {
268
- return {
269
- tag: "markdown",
270
- content: `<font color='grey'>${escapeFeishuCardMarkdownText(block.text)}</font>`,
271
- };
272
- }
273
- if (block.type === "divider") {
274
- return { tag: "hr" };
275
- }
276
- if (block.type === "buttons") {
277
- const actions = block.buttons
278
- .map((button) => buildFeishuPayloadButton(button))
279
- .filter((button): button is Record<string, unknown> => Boolean(button));
280
- if (actions.length === 0) {
281
- return undefined;
282
- }
283
- return {
284
- tag: "action",
285
- actions,
286
- };
287
- }
288
- const labels = block.options.map((option) => `- ${option.label}`).join("\n");
289
- return {
290
- tag: "markdown",
291
- content: `${escapeFeishuCardMarkdownText(
292
- block.placeholder?.trim() || "Options",
293
- )}:\n${escapeFeishuCardMarkdownText(labels)}`,
294
- };
295
- }
296
-
297
- function buildFeishuPayloadCard(params: {
298
- payload: Parameters<NonNullable<ChannelOutboundAdapter["sendPayload"]>>[0]["payload"];
299
- text?: string;
300
- identity?: Parameters<NonNullable<ChannelOutboundAdapter["sendPayload"]>>[0]["identity"];
301
- }): Record<string, unknown> | undefined {
302
- const nativeCard = readNativeFeishuCard(params.payload);
303
- if (nativeCard) {
304
- return nativeCard;
305
- }
306
-
307
- const interactive = normalizeInteractiveReply(params.payload.interactive);
308
- const presentation =
309
- normalizeMessagePresentation(params.payload.presentation) ??
310
- (interactive ? interactiveReplyToPresentation(interactive) : undefined);
311
- if (!presentation && !interactive) {
312
- return undefined;
313
- }
314
-
315
- const text = resolveInteractiveTextFallback({
316
- text: params.text ?? params.payload.text,
317
- interactive,
318
- });
319
- const elements: Record<string, unknown>[] = [];
320
- if (text?.trim()) {
321
- elements.push({ tag: "markdown", content: escapeFeishuCardMarkdownText(text) });
322
- }
323
- for (const block of presentation?.blocks ?? []) {
324
- const element = buildFeishuCardElementForBlock(block);
325
- if (element) {
326
- elements.push(element);
327
- }
328
- }
329
- if (elements.length === 0) {
330
- elements.push({
331
- tag: "markdown",
332
- content: renderMessagePresentationFallbackText({ text, presentation }),
333
- });
334
- }
335
-
336
- const identityTitle = params.identity
337
- ? params.identity.emoji
338
- ? `${params.identity.emoji} ${params.identity.name ?? ""}`.trim()
339
- : (params.identity.name ?? "")
340
- : "";
341
- const title = presentation?.title ?? identityTitle;
342
- const template = resolveFeishuCardTemplate(
343
- presentation?.tone === "danger"
344
- ? "red"
345
- : presentation?.tone === "warning"
346
- ? "orange"
347
- : presentation?.tone === "success"
348
- ? "green"
349
- : "blue",
350
- );
351
-
352
- return markRenderedFeishuCard({
353
- schema: "2.0",
354
- config: { width_mode: "fill" },
355
- ...(title
356
- ? {
357
- header: {
358
- title: { tag: "plain_text", content: title },
359
- template: template ?? "blue",
360
- },
361
- }
362
- : {}),
363
- body: { elements },
364
- });
365
- }
366
-
367
- function renderFeishuPresentationPayload({
368
- payload,
369
- presentation,
370
- ctx,
371
- }: Parameters<NonNullable<ChannelOutboundAdapter["renderPresentation"]>>[0]) {
372
- const card = buildFeishuPayloadCard({
373
- payload,
374
- text: payload.text,
375
- identity: ctx.identity,
376
- });
377
- if (!card) {
378
- return null;
379
- }
380
- const existingFeishuData = isRecord(payload.channelData?.feishu)
381
- ? payload.channelData.feishu
382
- : undefined;
383
- return {
384
- ...payload,
385
- text: renderMessagePresentationFallbackText({ text: payload.text, presentation }),
386
- channelData: {
387
- ...payload.channelData,
388
- feishu: {
389
- ...existingFeishuData,
390
- card,
391
- },
392
- },
393
- };
394
- }
395
-
396
- function resolveReplyToMessageId(params: {
397
- replyToId?: string | null;
398
- threadId?: string | number | null;
399
- }): string | undefined {
400
- const replyToId = params.replyToId?.trim();
401
- if (replyToId) {
402
- return replyToId;
403
- }
404
- if (params.threadId == null) {
405
- return undefined;
406
- }
407
- const trimmed = String(params.threadId).trim();
408
- return trimmed || undefined;
409
- }
410
-
411
- type FeishuMediaReplyMode = {
412
- replyToMessageId: string | undefined;
413
- replyInThread: boolean;
414
- };
415
-
416
- function resolveFeishuMediaReplyMode(params: {
417
- replyToId?: string | null;
418
- threadId?: string | number | null;
419
- }): FeishuMediaReplyMode {
420
- const trimmedReplyToId = params.replyToId?.trim() || undefined;
421
- const replyToMessageId = resolveReplyToMessageId(params);
422
- const replyInThread = params.threadId != null && !trimmedReplyToId;
423
- return { replyToMessageId, replyInThread };
424
- }
425
-
426
- async function sendCommentThreadReply(params: {
427
- cfg: Parameters<typeof sendMessageFeishu>[0]["cfg"];
428
- to: string;
429
- text: string;
430
- replyId?: string;
431
- accountId?: string;
432
- }) {
433
- const target = parseFeishuCommentTarget(params.to);
434
- if (!target) {
435
- return null;
436
- }
437
- const account = resolveFeishuAccount({ cfg: params.cfg, accountId: params.accountId });
438
- const client = createFeishuClient(account);
439
- const replyId = params.replyId?.trim();
440
- try {
441
- const result = await deliverCommentThreadText(client, {
442
- file_token: target.fileToken,
443
- file_type: target.fileType,
444
- comment_id: target.commentId,
445
- content: params.text,
446
- });
447
- return {
448
- messageId:
449
- (typeof result.reply_id === "string" && result.reply_id) ||
450
- (typeof result.comment_id === "string" && result.comment_id) ||
451
- "",
452
- chatId: target.commentId,
453
- result,
454
- };
455
- } finally {
456
- if (replyId) {
457
- void cleanupAmbientCommentTypingReaction({
458
- client,
459
- deliveryContext: {
460
- channel: "feishu",
461
- to: params.to,
462
- threadId: replyId,
463
- },
464
- });
465
- }
466
- }
467
- }
468
-
469
- async function sendOutboundText(params: {
470
- cfg: Parameters<typeof sendMessageFeishu>[0]["cfg"];
471
- to: string;
472
- text: string;
473
- replyToMessageId?: string;
474
- replyInThread?: boolean;
475
- accountId?: string;
476
- }) {
477
- const { cfg, to, text, accountId, replyToMessageId, replyInThread } = params;
478
- const commentResult = await sendCommentThreadReply({
479
- cfg,
480
- to,
481
- text,
482
- replyId: replyToMessageId,
483
- accountId,
484
- });
485
- if (commentResult) {
486
- return commentResult;
487
- }
488
-
489
- const account = resolveFeishuAccount({ cfg, accountId });
490
- const renderMode = account.config?.renderMode ?? "auto";
491
-
492
- if (renderMode === "card" || (renderMode === "auto" && shouldUseCard(text))) {
493
- return sendMarkdownCardFeishu({
494
- cfg,
495
- to,
496
- text,
497
- accountId,
498
- replyToMessageId,
499
- replyInThread,
500
- });
501
- }
502
-
503
- return sendMessageFeishu({ cfg, to, text, accountId, replyToMessageId, replyInThread });
504
- }
505
-
506
- export const feishuOutbound: ChannelOutboundAdapter = {
507
- deliveryMode: "direct",
508
- chunker: chunkTextForOutbound,
509
- chunkerMode: "markdown",
510
- textChunkLimit: 4000,
511
- presentationCapabilities: {
512
- supported: true,
513
- buttons: true,
514
- selects: false,
515
- context: true,
516
- divider: true,
517
- limits: {
518
- actions: {
519
- maxActions: 20,
520
- maxActionsPerRow: 5,
521
- maxLabelLength: 40,
522
- maxValueBytes: 1024,
523
- },
524
- text: {
525
- maxLength: 4000,
526
- encoding: "characters",
527
- markdownDialect: "markdown",
528
- },
529
- },
530
- },
531
- renderPresentation: renderFeishuPresentationPayload,
532
- sendPayload: async (ctx) => {
533
- const card = buildFeishuPayloadCard({
534
- payload: ctx.payload,
535
- text: ctx.text,
536
- identity: ctx.identity,
537
- });
538
- if (!card) {
539
- return await sendTextMediaPayload({
540
- channel: "feishu",
541
- ctx,
542
- adapter: feishuOutbound,
543
- });
544
- }
545
-
546
- const replyToMessageId = resolveReplyToMessageId({
547
- replyToId: ctx.replyToId,
548
- threadId: ctx.threadId,
549
- });
550
- const commentTarget = parseFeishuCommentTarget(ctx.to);
551
- if (commentTarget) {
552
- return await sendTextMediaPayload({
553
- channel: "feishu",
554
- ctx: {
555
- ...ctx,
556
- payload: {
557
- ...ctx.payload,
558
- text: renderMessagePresentationFallbackText({
559
- text: ctx.payload.text,
560
- presentation:
561
- normalizeMessagePresentation(ctx.payload.presentation) ??
562
- (() => {
563
- const interactive = normalizeInteractiveReply(ctx.payload.interactive);
564
- return interactive ? interactiveReplyToPresentation(interactive) : undefined;
565
- })(),
566
- }),
567
- interactive: undefined,
568
- presentation: undefined,
569
- channelData: undefined,
570
- },
571
- },
572
- adapter: feishuOutbound,
573
- });
574
- }
575
-
576
- const mediaUrls = resolvePayloadMediaUrls(ctx.payload)
577
- .map((entry) => entry.trim())
578
- .filter(Boolean);
579
- return attachChannelToResult(
580
- "feishu",
581
- await sendPayloadMediaSequenceAndFinalize({
582
- text: ctx.payload.text ?? "",
583
- mediaUrls,
584
- send: async ({ mediaUrl }) =>
585
- await sendMediaFeishu({
586
- cfg: ctx.cfg,
587
- to: ctx.to,
588
- mediaUrl,
589
- accountId: ctx.accountId ?? undefined,
590
- mediaLocalRoots: ctx.mediaLocalRoots,
591
- replyToMessageId,
592
- ...(ctx.payload.audioAsVoice === true || ctx.audioAsVoice === true
593
- ? { audioAsVoice: true }
594
- : {}),
595
- }),
596
- finalize: async () =>
597
- await sendCardFeishu({
598
- cfg: ctx.cfg,
599
- to: ctx.to,
600
- card,
601
- replyToMessageId,
602
- replyInThread: ctx.threadId != null && !ctx.replyToId,
603
- accountId: ctx.accountId ?? undefined,
604
- }),
605
- }),
606
- );
607
- },
608
- ...createAttachedChannelResultAdapter({
609
- channel: "feishu",
610
- sendText: async ({
611
- cfg,
612
- to,
613
- text,
614
- accountId,
615
- replyToId,
616
- threadId,
617
- mediaLocalRoots,
618
- identity,
619
- }) => {
620
- const { replyToMessageId, replyInThread } = resolveFeishuMediaReplyMode({
621
- replyToId,
622
- threadId,
623
- });
624
- // Scheme A compatibility shim:
625
- // when upstream accidentally returns a local image path as plain text,
626
- // auto-upload and send as Feishu image message instead of leaking path text.
627
- const localImagePath = normalizePossibleLocalImagePath(text);
628
- if (localImagePath) {
629
- try {
630
- return await sendMediaFeishu({
631
- cfg,
632
- to,
633
- mediaUrl: localImagePath,
634
- accountId: accountId ?? undefined,
635
- replyToMessageId,
636
- replyInThread,
637
- mediaLocalRoots,
638
- });
639
- } catch (err) {
640
- console.error(`[feishu] local image path auto-send failed:`, err);
641
- // fall through to plain text as last resort
642
- }
643
- }
644
-
645
- if (parseFeishuCommentTarget(to)) {
646
- return await sendOutboundText({
647
- cfg,
648
- to,
649
- text,
650
- accountId: accountId ?? undefined,
651
- replyToMessageId,
652
- replyInThread,
653
- });
654
- }
655
-
656
- const account = resolveFeishuAccount({ cfg, accountId: accountId ?? undefined });
657
- const renderMode = account.config?.renderMode ?? "auto";
658
- const useCard = renderMode === "card" || (renderMode === "auto" && shouldUseCard(text));
659
- if (useCard) {
660
- const header = identity
661
- ? {
662
- title: identity.emoji
663
- ? `${identity.emoji} ${identity.name ?? ""}`.trim()
664
- : (identity.name ?? ""),
665
- template: "blue" as const,
666
- }
667
- : undefined;
668
- return await sendStructuredCardFeishu({
669
- cfg,
670
- to,
671
- text,
672
- replyToMessageId,
673
- replyInThread,
674
- accountId: accountId ?? undefined,
675
- header: header?.title ? header : undefined,
676
- });
677
- }
678
- return await sendOutboundText({
679
- cfg,
680
- to,
681
- text,
682
- accountId: accountId ?? undefined,
683
- replyToMessageId,
684
- replyInThread,
685
- });
686
- },
687
- sendMedia: async ({
688
- cfg,
689
- to,
690
- text,
691
- mediaUrl,
692
- audioAsVoice,
693
- accountId,
694
- mediaLocalRoots,
695
- replyToId,
696
- threadId,
697
- }) => {
698
- const { replyToMessageId, replyInThread } = resolveFeishuMediaReplyMode({
699
- replyToId,
700
- threadId,
701
- });
702
- const commentTarget = parseFeishuCommentTarget(to);
703
- if (commentTarget) {
704
- const commentText = [text?.trim(), mediaUrl?.trim()].filter(Boolean).join("\n\n");
705
- return await sendOutboundText({
706
- cfg,
707
- to,
708
- text: commentText || mediaUrl || text || "",
709
- accountId: accountId ?? undefined,
710
- replyToMessageId,
711
- replyInThread,
712
- });
713
- }
714
-
715
- const suppressTextForVoiceMedia =
716
- mediaUrl !== undefined &&
717
- shouldSuppressFeishuTextForVoiceMedia({
718
- mediaUrl,
719
- audioAsVoice,
720
- });
721
-
722
- // Send text first if provided, except for Feishu native voice bubbles.
723
- if (text?.trim() && !suppressTextForVoiceMedia) {
724
- await sendOutboundText({
725
- cfg,
726
- to,
727
- text,
728
- accountId: accountId ?? undefined,
729
- replyToMessageId,
730
- replyInThread,
731
- });
732
- }
733
-
734
- // Upload and send media if URL or local path provided
735
- if (mediaUrl) {
736
- try {
737
- const result = await sendMediaFeishu({
738
- cfg,
739
- to,
740
- mediaUrl,
741
- accountId: accountId ?? undefined,
742
- mediaLocalRoots,
743
- replyToMessageId,
744
- replyInThread,
745
- ...(audioAsVoice === true ? { audioAsVoice: true } : {}),
746
- });
747
- if (result.voiceIntentDegradedToFile && text?.trim()) {
748
- await sendOutboundText({
749
- cfg,
750
- to,
751
- text,
752
- accountId: accountId ?? undefined,
753
- replyToMessageId,
754
- replyInThread,
755
- });
756
- }
757
- return result;
758
- } catch (err) {
759
- // Log the error for debugging
760
- console.error(`[feishu] sendMediaFeishu failed:`, err);
761
- // Fallback to URL link if upload fails
762
- const fallbackText = [text?.trim(), `📎 ${mediaUrl}`].filter(Boolean).join("\n\n");
763
- return await sendOutboundText({
764
- cfg,
765
- to,
766
- text: fallbackText,
767
- accountId: accountId ?? undefined,
768
- replyToMessageId,
769
- replyInThread,
770
- });
771
- }
772
- }
773
-
774
- // No media URL, just return text result
775
- return await sendOutboundText({
776
- cfg,
777
- to,
778
- text: text ?? "",
779
- accountId: accountId ?? undefined,
780
- replyToMessageId,
781
- replyInThread,
782
- });
783
- },
784
- }),
785
- };