@bobotu/feishu-fork 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +922 -0
  3. package/index.ts +65 -0
  4. package/openclaw.plugin.json +10 -0
  5. package/package.json +72 -0
  6. package/skills/feishu-doc/SKILL.md +161 -0
  7. package/skills/feishu-doc/references/block-types.md +102 -0
  8. package/skills/feishu-drive/SKILL.md +96 -0
  9. package/skills/feishu-perm/SKILL.md +90 -0
  10. package/skills/feishu-task/SKILL.md +210 -0
  11. package/skills/feishu-wiki/SKILL.md +96 -0
  12. package/src/accounts.ts +140 -0
  13. package/src/bitable-tools/actions.ts +199 -0
  14. package/src/bitable-tools/common.ts +90 -0
  15. package/src/bitable-tools/index.ts +1 -0
  16. package/src/bitable-tools/meta.ts +80 -0
  17. package/src/bitable-tools/register.ts +195 -0
  18. package/src/bitable-tools/schemas.ts +221 -0
  19. package/src/bot.ts +1125 -0
  20. package/src/channel.ts +334 -0
  21. package/src/client.ts +114 -0
  22. package/src/config-schema.ts +237 -0
  23. package/src/dedup.ts +54 -0
  24. package/src/directory.ts +165 -0
  25. package/src/doc-tools/actions.ts +341 -0
  26. package/src/doc-tools/common.ts +33 -0
  27. package/src/doc-tools/index.ts +2 -0
  28. package/src/doc-tools/register.ts +90 -0
  29. package/src/doc-tools/schemas.ts +85 -0
  30. package/src/doc-write-service.ts +711 -0
  31. package/src/drive-tools/actions.ts +182 -0
  32. package/src/drive-tools/common.ts +18 -0
  33. package/src/drive-tools/index.ts +2 -0
  34. package/src/drive-tools/register.ts +71 -0
  35. package/src/drive-tools/schemas.ts +67 -0
  36. package/src/dynamic-agent.ts +135 -0
  37. package/src/external-keys.ts +19 -0
  38. package/src/media.ts +510 -0
  39. package/src/mention.ts +121 -0
  40. package/src/monitor.ts +323 -0
  41. package/src/onboarding.ts +449 -0
  42. package/src/outbound.ts +40 -0
  43. package/src/perm-tools/actions.ts +111 -0
  44. package/src/perm-tools/common.ts +18 -0
  45. package/src/perm-tools/index.ts +2 -0
  46. package/src/perm-tools/register.ts +65 -0
  47. package/src/perm-tools/schemas.ts +52 -0
  48. package/src/policy.ts +117 -0
  49. package/src/probe.ts +147 -0
  50. package/src/reactions.ts +160 -0
  51. package/src/reply-dispatcher.ts +240 -0
  52. package/src/runtime.ts +14 -0
  53. package/src/send.ts +391 -0
  54. package/src/streaming-card.ts +211 -0
  55. package/src/targets.ts +58 -0
  56. package/src/task-tools/actions.ts +590 -0
  57. package/src/task-tools/common.ts +18 -0
  58. package/src/task-tools/constants.ts +13 -0
  59. package/src/task-tools/index.ts +1 -0
  60. package/src/task-tools/register.ts +263 -0
  61. package/src/task-tools/schemas.ts +567 -0
  62. package/src/text/markdown-links.ts +104 -0
  63. package/src/tools-common/feishu-api.ts +184 -0
  64. package/src/tools-common/tool-context.ts +23 -0
  65. package/src/tools-common/tool-exec.ts +73 -0
  66. package/src/tools-config.ts +22 -0
  67. package/src/types.ts +79 -0
  68. package/src/typing.ts +75 -0
  69. package/src/wiki-tools/actions.ts +166 -0
  70. package/src/wiki-tools/common.ts +18 -0
  71. package/src/wiki-tools/index.ts +2 -0
  72. package/src/wiki-tools/register.ts +66 -0
  73. package/src/wiki-tools/schemas.ts +55 -0
@@ -0,0 +1,240 @@
1
+ import {
2
+ createReplyPrefixContext,
3
+ createTypingCallbacks,
4
+ logTypingFailure,
5
+ type ClawdbotConfig,
6
+ type ReplyPayload,
7
+ type RuntimeEnv,
8
+ } from "openclaw/plugin-sdk";
9
+ import { resolveFeishuAccount } from "./accounts.js";
10
+ import { createFeishuClient } from "./client.js";
11
+ import { buildMentionedCardContent, type MentionTarget } from "./mention.js";
12
+ import { normalizeFeishuMarkdownLinks } from "./text/markdown-links.js";
13
+ import { getFeishuRuntime } from "./runtime.js";
14
+ import { sendMarkdownCardFeishu, sendMessageFeishu } from "./send.js";
15
+ import { FeishuStreamingSession } from "./streaming-card.js";
16
+ import { resolveReceiveIdType } from "./targets.js";
17
+ import { addTypingIndicator, removeTypingIndicator, type TypingIndicatorState } from "./typing.js";
18
+
19
+ /** Detect if text contains markdown elements that benefit from card rendering */
20
+ function shouldUseCard(text: string): boolean {
21
+ return /```[\s\S]*?```/.test(text) || /\|.+\|[\r\n]+\|[-:| ]+\|/.test(text);
22
+ }
23
+
24
+ export type CreateFeishuReplyDispatcherParams = {
25
+ cfg: ClawdbotConfig;
26
+ agentId: string;
27
+ runtime: RuntimeEnv;
28
+ chatId: string;
29
+ replyToMessageId?: string;
30
+ mentionTargets?: MentionTarget[];
31
+ accountId?: string;
32
+ };
33
+
34
+ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherParams) {
35
+ const core = getFeishuRuntime();
36
+ const { cfg, agentId, chatId, replyToMessageId, mentionTargets, accountId } = params;
37
+ const account = resolveFeishuAccount({ cfg, accountId });
38
+ const prefixContext = createReplyPrefixContext({ cfg, agentId });
39
+
40
+ let typingState: TypingIndicatorState | null = null;
41
+ const typingCallbacks = createTypingCallbacks({
42
+ start: async () => {
43
+ if (!replyToMessageId) {
44
+ return;
45
+ }
46
+ typingState = await addTypingIndicator({ cfg, messageId: replyToMessageId, accountId });
47
+ },
48
+ stop: async () => {
49
+ if (!typingState) {
50
+ return;
51
+ }
52
+ await removeTypingIndicator({ cfg, state: typingState, accountId });
53
+ typingState = null;
54
+ },
55
+ onStartError: (err) =>
56
+ logTypingFailure({
57
+ log: (message) => params.runtime.log?.(message),
58
+ channel: "feishu",
59
+ action: "start",
60
+ error: err,
61
+ }),
62
+ onStopError: (err) =>
63
+ logTypingFailure({
64
+ log: (message) => params.runtime.log?.(message),
65
+ channel: "feishu",
66
+ action: "stop",
67
+ error: err,
68
+ }),
69
+ });
70
+
71
+ const textChunkLimit = core.channel.text.resolveTextChunkLimit(cfg, "feishu", account.accountId, {
72
+ fallbackLimit: 4000,
73
+ });
74
+ const chunkMode = core.channel.text.resolveChunkMode(cfg, "feishu", account.accountId);
75
+ const tableMode = core.channel.text.resolveMarkdownTableMode({
76
+ cfg,
77
+ channel: "feishu",
78
+ accountId: account.accountId,
79
+ });
80
+ const renderMode = account.config?.renderMode ?? "auto";
81
+ const streamingEnabled = account.config?.streaming === true && renderMode !== "raw";
82
+
83
+ let streaming: FeishuStreamingSession | null = null;
84
+ let streamText = "";
85
+ let lastPartial = "";
86
+ let partialUpdateQueue: Promise<void> = Promise.resolve();
87
+ let streamingStartPromise: Promise<void> | null = null;
88
+
89
+ const startStreaming = () => {
90
+ if (!streamingEnabled || streamingStartPromise || streaming) {
91
+ return;
92
+ }
93
+ streamingStartPromise = (async () => {
94
+ const creds =
95
+ account.appId && account.appSecret
96
+ ? { appId: account.appId, appSecret: account.appSecret, domain: account.domain }
97
+ : null;
98
+ if (!creds) {
99
+ return;
100
+ }
101
+
102
+ streaming = new FeishuStreamingSession(createFeishuClient(account), creds, (message) =>
103
+ params.runtime.log?.(`feishu[${account.accountId}] ${message}`),
104
+ );
105
+ try {
106
+ await streaming.start(chatId, resolveReceiveIdType(chatId));
107
+ } catch (error) {
108
+ params.runtime.error?.(`feishu: streaming start failed: ${String(error)}`);
109
+ streaming = null;
110
+ }
111
+ })();
112
+ };
113
+
114
+ const closeStreaming = async () => {
115
+ if (streamingStartPromise) {
116
+ await streamingStartPromise;
117
+ }
118
+ await partialUpdateQueue;
119
+ if (streaming?.isActive()) {
120
+ let text = streamText;
121
+ if (mentionTargets?.length) {
122
+ text = buildMentionedCardContent(mentionTargets, text);
123
+ }
124
+ await streaming.close(normalizeFeishuMarkdownLinks(text));
125
+ }
126
+ streaming = null;
127
+ streamingStartPromise = null;
128
+ streamText = "";
129
+ lastPartial = "";
130
+ };
131
+
132
+ const { dispatcher, replyOptions, markDispatchIdle } =
133
+ core.channel.reply.createReplyDispatcherWithTyping({
134
+ responsePrefix: prefixContext.responsePrefix,
135
+ responsePrefixContextProvider: prefixContext.responsePrefixContextProvider,
136
+ humanDelay: core.channel.reply.resolveHumanDelayConfig(cfg, agentId),
137
+ onReplyStart: () => {
138
+ if (streamingEnabled && renderMode === "card") {
139
+ startStreaming();
140
+ }
141
+ void typingCallbacks.onReplyStart?.();
142
+ },
143
+ deliver: async (payload: ReplyPayload, info) => {
144
+ const text = payload.text ?? "";
145
+ if (!text.trim()) {
146
+ return;
147
+ }
148
+
149
+ const useCard = renderMode === "card" || (renderMode === "auto" && shouldUseCard(text));
150
+
151
+ if ((info?.kind === "block" || info?.kind === "final") && streamingEnabled && useCard) {
152
+ startStreaming();
153
+ if (streamingStartPromise) {
154
+ await streamingStartPromise;
155
+ }
156
+ }
157
+
158
+ if (streaming?.isActive()) {
159
+ if (info?.kind === "final") {
160
+ streamText = text;
161
+ await closeStreaming();
162
+ }
163
+ return;
164
+ }
165
+
166
+ let first = true;
167
+ if (useCard) {
168
+ for (const chunk of core.channel.text.chunkTextWithMode(text, textChunkLimit, chunkMode)) {
169
+ await sendMarkdownCardFeishu({
170
+ cfg,
171
+ to: chatId,
172
+ text: chunk,
173
+ replyToMessageId,
174
+ mentions: first ? mentionTargets : undefined,
175
+ accountId,
176
+ });
177
+ first = false;
178
+ }
179
+ } else {
180
+ const converted = core.channel.text.convertMarkdownTables(text, tableMode);
181
+ for (const chunk of core.channel.text.chunkTextWithMode(
182
+ converted,
183
+ textChunkLimit,
184
+ chunkMode,
185
+ )) {
186
+ await sendMessageFeishu({
187
+ cfg,
188
+ to: chatId,
189
+ text: chunk,
190
+ replyToMessageId,
191
+ mentions: first ? mentionTargets : undefined,
192
+ accountId,
193
+ });
194
+ first = false;
195
+ }
196
+ }
197
+ },
198
+ onError: async (error, info) => {
199
+ params.runtime.error?.(
200
+ `feishu[${account.accountId}] ${info.kind} reply failed: ${String(error)}`,
201
+ );
202
+ await closeStreaming();
203
+ typingCallbacks.onIdle?.();
204
+ },
205
+ onIdle: async () => {
206
+ await closeStreaming();
207
+ typingCallbacks.onIdle?.();
208
+ },
209
+ onCleanup: () => {
210
+ typingCallbacks.onCleanup?.();
211
+ },
212
+ });
213
+
214
+ return {
215
+ dispatcher,
216
+ replyOptions: {
217
+ ...replyOptions,
218
+ onModelSelected: prefixContext.onModelSelected,
219
+ onPartialReply: streamingEnabled
220
+ ? (payload: ReplyPayload) => {
221
+ const partialText = normalizeFeishuMarkdownLinks(payload.text ?? "");
222
+ if (!partialText || partialText === lastPartial) {
223
+ return;
224
+ }
225
+ lastPartial = partialText;
226
+ streamText = partialText;
227
+ partialUpdateQueue = partialUpdateQueue.then(async () => {
228
+ if (streamingStartPromise) {
229
+ await streamingStartPromise;
230
+ }
231
+ if (streaming?.isActive()) {
232
+ await streaming.update(streamText);
233
+ }
234
+ });
235
+ }
236
+ : undefined,
237
+ },
238
+ markDispatchIdle,
239
+ };
240
+ }
package/src/runtime.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { PluginRuntime } from "openclaw/plugin-sdk";
2
+
3
+ let runtime: PluginRuntime | null = null;
4
+
5
+ export function setFeishuRuntime(next: PluginRuntime) {
6
+ runtime = next;
7
+ }
8
+
9
+ export function getFeishuRuntime(): PluginRuntime {
10
+ if (!runtime) {
11
+ throw new Error("Feishu runtime not initialized");
12
+ }
13
+ return runtime;
14
+ }
package/src/send.ts ADDED
@@ -0,0 +1,391 @@
1
+ import type { ClawdbotConfig } from "openclaw/plugin-sdk";
2
+ import type { FeishuSendResult, ResolvedFeishuAccount } from "./types.js";
3
+ import type { MentionTarget } from "./mention.js";
4
+ import { buildMentionedMessage, buildMentionedCardContent } from "./mention.js";
5
+ import { createFeishuClient } from "./client.js";
6
+ import { normalizeFeishuMarkdownLinks } from "./text/markdown-links.js";
7
+ import { resolveReceiveIdType, normalizeFeishuTarget } from "./targets.js";
8
+ import { getFeishuRuntime } from "./runtime.js";
9
+ import { resolveFeishuAccount } from "./accounts.js";
10
+
11
+ export type FeishuMessageInfo = {
12
+ messageId: string;
13
+ chatId: string;
14
+ senderId?: string;
15
+ senderOpenId?: string;
16
+ content: string;
17
+ contentType: string;
18
+ createTime?: number;
19
+ };
20
+
21
+ /**
22
+ * Get a message by its ID.
23
+ * Useful for fetching quoted/replied message content.
24
+ */
25
+ export async function getMessageFeishu(params: {
26
+ cfg: ClawdbotConfig;
27
+ messageId: string;
28
+ accountId?: string;
29
+ }): Promise<FeishuMessageInfo | null> {
30
+ const { cfg, messageId, accountId } = params;
31
+ const account = resolveFeishuAccount({ cfg, accountId });
32
+ if (!account.configured) {
33
+ throw new Error(`Feishu account "${account.accountId}" not configured`);
34
+ }
35
+
36
+ const client = createFeishuClient(account);
37
+
38
+ try {
39
+ const response = (await client.im.message.get({
40
+ path: { message_id: messageId },
41
+ })) as {
42
+ code?: number;
43
+ msg?: string;
44
+ data?: {
45
+ items?: Array<{
46
+ message_id?: string;
47
+ chat_id?: string;
48
+ msg_type?: string;
49
+ body?: { content?: string };
50
+ sender?: {
51
+ id?: string;
52
+ id_type?: string;
53
+ sender_type?: string;
54
+ };
55
+ create_time?: string;
56
+ }>;
57
+ };
58
+ };
59
+
60
+ if (response.code !== 0) {
61
+ return null;
62
+ }
63
+
64
+ const item = response.data?.items?.[0];
65
+ if (!item) {
66
+ return null;
67
+ }
68
+
69
+ // Parse content based on message type
70
+ let content = item.body?.content ?? "";
71
+ try {
72
+ const parsed = JSON.parse(content);
73
+ if (item.msg_type === "text" && parsed.text) {
74
+ content = parsed.text;
75
+ } else if (parsed.content || parsed.elements) {
76
+ // Extract plain text from rich text (post) or interactive (card) format.
77
+ // Both use nested arrays: Array<Array<{tag, text?, href?, ...}>>
78
+ const blocks = parsed.content ?? parsed.elements ?? [];
79
+ const lines: string[] = [];
80
+ for (const paragraph of blocks) {
81
+ if (!Array.isArray(paragraph)) continue;
82
+ const line = paragraph
83
+ .map((node: { tag?: string; text?: string; href?: string }) => {
84
+ if (node.tag === "text") return node.text ?? "";
85
+ if (node.tag === "a") return node.text ?? node.href ?? "";
86
+ if (node.tag === "at") return "";
87
+ if (node.tag === "img") return "[图片]";
88
+ return node.text ?? "";
89
+ })
90
+ .join("");
91
+ if (line.trim()) lines.push(line);
92
+ }
93
+ const extracted = (parsed.title ? parsed.title + "\n" : "") + lines.join("\n");
94
+ // Filter out Feishu's degraded card placeholder text
95
+ if (extracted.trim() && !extracted.includes("请升级至最新版本客户端")) {
96
+ content = extracted;
97
+ } else if (extracted.includes("请升级至最新版本客户端")) {
98
+ content = "[卡片消息]";
99
+ }
100
+ }
101
+ } catch {
102
+ // Keep raw content if parsing fails
103
+ }
104
+
105
+ return {
106
+ messageId: item.message_id ?? messageId,
107
+ chatId: item.chat_id ?? "",
108
+ senderId: item.sender?.id,
109
+ senderOpenId: item.sender?.id_type === "open_id" ? item.sender?.id : undefined,
110
+ content,
111
+ contentType: item.msg_type ?? "text",
112
+ createTime: item.create_time ? parseInt(item.create_time, 10) : undefined,
113
+ };
114
+ } catch {
115
+ return null;
116
+ }
117
+ }
118
+
119
+ export type SendFeishuMessageParams = {
120
+ cfg: ClawdbotConfig;
121
+ to: string;
122
+ text: string;
123
+ replyToMessageId?: string;
124
+ /** Mention target users */
125
+ mentions?: MentionTarget[];
126
+ /** Account ID (optional, uses default if not specified) */
127
+ accountId?: string;
128
+ };
129
+
130
+ function buildFeishuPostMessagePayload(params: { messageText: string }): {
131
+ content: string;
132
+ msgType: string;
133
+ } {
134
+ const { messageText } = params;
135
+ return {
136
+ content: JSON.stringify({
137
+ zh_cn: {
138
+ content: [
139
+ [
140
+ {
141
+ tag: "md",
142
+ text: messageText,
143
+ },
144
+ ],
145
+ ],
146
+ },
147
+ }),
148
+ msgType: "post",
149
+ };
150
+ }
151
+
152
+ export async function sendMessageFeishu(params: SendFeishuMessageParams): Promise<FeishuSendResult> {
153
+ const { cfg, to, text, replyToMessageId, mentions, accountId } = params;
154
+ const account = resolveFeishuAccount({ cfg, accountId });
155
+ if (!account.configured) {
156
+ throw new Error(`Feishu account "${account.accountId}" not configured`);
157
+ }
158
+
159
+ const client = createFeishuClient(account);
160
+ const receiveId = normalizeFeishuTarget(to);
161
+ if (!receiveId) {
162
+ throw new Error(`Invalid Feishu target: ${to}`);
163
+ }
164
+
165
+ const receiveIdType = resolveReceiveIdType(receiveId);
166
+ const tableMode = getFeishuRuntime().channel.text.resolveMarkdownTableMode({
167
+ cfg,
168
+ channel: "feishu",
169
+ });
170
+
171
+ // Build message content (with @mention support)
172
+ let rawText = text ?? "";
173
+ if (mentions && mentions.length > 0) {
174
+ rawText = buildMentionedMessage(mentions, rawText);
175
+ }
176
+ const messageText = normalizeFeishuMarkdownLinks(
177
+ getFeishuRuntime().channel.text.convertMarkdownTables(rawText, tableMode),
178
+ );
179
+
180
+ const { content, msgType } = buildFeishuPostMessagePayload({ messageText });
181
+
182
+ if (replyToMessageId) {
183
+ const response = await client.im.message.reply({
184
+ path: { message_id: replyToMessageId },
185
+ data: {
186
+ content,
187
+ msg_type: msgType,
188
+ },
189
+ });
190
+
191
+ if (response.code !== 0) {
192
+ throw new Error(`Feishu reply failed: ${response.msg || `code ${response.code}`}`);
193
+ }
194
+
195
+ return {
196
+ messageId: response.data?.message_id ?? "unknown",
197
+ chatId: receiveId,
198
+ };
199
+ }
200
+
201
+ const response = await client.im.message.create({
202
+ params: { receive_id_type: receiveIdType },
203
+ data: {
204
+ receive_id: receiveId,
205
+ content,
206
+ msg_type: msgType,
207
+ },
208
+ });
209
+
210
+ if (response.code !== 0) {
211
+ throw new Error(`Feishu send failed: ${response.msg || `code ${response.code}`}`);
212
+ }
213
+
214
+ return {
215
+ messageId: response.data?.message_id ?? "unknown",
216
+ chatId: receiveId,
217
+ };
218
+ }
219
+
220
+ export type SendFeishuCardParams = {
221
+ cfg: ClawdbotConfig;
222
+ to: string;
223
+ card: Record<string, unknown>;
224
+ replyToMessageId?: string;
225
+ accountId?: string;
226
+ };
227
+
228
+ export async function sendCardFeishu(params: SendFeishuCardParams): Promise<FeishuSendResult> {
229
+ const { cfg, to, card, replyToMessageId, accountId } = params;
230
+ const account = resolveFeishuAccount({ cfg, accountId });
231
+ if (!account.configured) {
232
+ throw new Error(`Feishu account "${account.accountId}" not configured`);
233
+ }
234
+
235
+ const client = createFeishuClient(account);
236
+ const receiveId = normalizeFeishuTarget(to);
237
+ if (!receiveId) {
238
+ throw new Error(`Invalid Feishu target: ${to}`);
239
+ }
240
+
241
+ const receiveIdType = resolveReceiveIdType(receiveId);
242
+ const content = JSON.stringify(card);
243
+
244
+ if (replyToMessageId) {
245
+ const response = await client.im.message.reply({
246
+ path: { message_id: replyToMessageId },
247
+ data: {
248
+ content,
249
+ msg_type: "interactive",
250
+ },
251
+ });
252
+
253
+ if (response.code !== 0) {
254
+ throw new Error(`Feishu card reply failed: ${response.msg || `code ${response.code}`}`);
255
+ }
256
+
257
+ return {
258
+ messageId: response.data?.message_id ?? "unknown",
259
+ chatId: receiveId,
260
+ };
261
+ }
262
+
263
+ const response = await client.im.message.create({
264
+ params: { receive_id_type: receiveIdType },
265
+ data: {
266
+ receive_id: receiveId,
267
+ content,
268
+ msg_type: "interactive",
269
+ },
270
+ });
271
+
272
+ if (response.code !== 0) {
273
+ throw new Error(`Feishu card send failed: ${response.msg || `code ${response.code}`}`);
274
+ }
275
+
276
+ return {
277
+ messageId: response.data?.message_id ?? "unknown",
278
+ chatId: receiveId,
279
+ };
280
+ }
281
+
282
+ export async function updateCardFeishu(params: {
283
+ cfg: ClawdbotConfig;
284
+ messageId: string;
285
+ card: Record<string, unknown>;
286
+ accountId?: string;
287
+ }): Promise<void> {
288
+ const { cfg, messageId, card, accountId } = params;
289
+ const account = resolveFeishuAccount({ cfg, accountId });
290
+ if (!account.configured) {
291
+ throw new Error(`Feishu account "${account.accountId}" not configured`);
292
+ }
293
+
294
+ const client = createFeishuClient(account);
295
+ const content = JSON.stringify(card);
296
+
297
+ const response = await client.im.message.patch({
298
+ path: { message_id: messageId },
299
+ data: { content },
300
+ });
301
+
302
+ if (response.code !== 0) {
303
+ throw new Error(`Feishu card update failed: ${response.msg || `code ${response.code}`}`);
304
+ }
305
+ }
306
+
307
+ /**
308
+ * Build a Feishu interactive card with markdown content.
309
+ * Cards render markdown properly (code blocks, tables, links, etc.)
310
+ * Uses schema 2.0 format for proper markdown rendering.
311
+ */
312
+ export function buildMarkdownCard(text: string): Record<string, unknown> {
313
+ return {
314
+ schema: "2.0",
315
+ config: {
316
+ wide_screen_mode: true,
317
+ },
318
+ body: {
319
+ elements: [
320
+ {
321
+ tag: "markdown",
322
+ content: text,
323
+ },
324
+ ],
325
+ },
326
+ };
327
+ }
328
+
329
+ /**
330
+ * Send a message as a markdown card (interactive message).
331
+ * This renders markdown properly in Feishu (code blocks, tables, bold/italic, etc.)
332
+ */
333
+ export async function sendMarkdownCardFeishu(params: {
334
+ cfg: ClawdbotConfig;
335
+ to: string;
336
+ text: string;
337
+ replyToMessageId?: string;
338
+ /** Mention target users */
339
+ mentions?: MentionTarget[];
340
+ accountId?: string;
341
+ }): Promise<FeishuSendResult> {
342
+ const { cfg, to, text, replyToMessageId, mentions, accountId } = params;
343
+ // Build message content (with @mention support)
344
+ let cardText = text;
345
+ if (mentions && mentions.length > 0) {
346
+ cardText = buildMentionedCardContent(mentions, text);
347
+ }
348
+ cardText = normalizeFeishuMarkdownLinks(cardText);
349
+ const card = buildMarkdownCard(cardText);
350
+ return sendCardFeishu({ cfg, to, card, replyToMessageId, accountId });
351
+ }
352
+
353
+ /**
354
+ * Edit an existing text message.
355
+ * Note: Feishu only allows editing messages within 24 hours.
356
+ */
357
+ export async function editMessageFeishu(params: {
358
+ cfg: ClawdbotConfig;
359
+ messageId: string;
360
+ text: string;
361
+ accountId?: string;
362
+ }): Promise<void> {
363
+ const { cfg, messageId, text, accountId } = params;
364
+ const account = resolveFeishuAccount({ cfg, accountId });
365
+ if (!account.configured) {
366
+ throw new Error(`Feishu account "${account.accountId}" not configured`);
367
+ }
368
+
369
+ const client = createFeishuClient(account);
370
+ const tableMode = getFeishuRuntime().channel.text.resolveMarkdownTableMode({
371
+ cfg,
372
+ channel: "feishu",
373
+ });
374
+ const messageText = normalizeFeishuMarkdownLinks(
375
+ getFeishuRuntime().channel.text.convertMarkdownTables(text ?? "", tableMode),
376
+ );
377
+
378
+ const { content, msgType } = buildFeishuPostMessagePayload({ messageText });
379
+
380
+ const response = await client.im.message.update({
381
+ path: { message_id: messageId },
382
+ data: {
383
+ msg_type: msgType,
384
+ content,
385
+ },
386
+ });
387
+
388
+ if (response.code !== 0) {
389
+ throw new Error(`Feishu message edit failed: ${response.msg || `code ${response.code}`}`);
390
+ }
391
+ }