Skip to content

Commit 063938e

Browse files
committed
fix(feishu): fall back to post mode when markdown tables exceed card limit
Feishu rejects static interactive cards with more than 5 table components (ErrCode 11310: card table number over limit), which silently drops the reply into the delivery queue. Tables are counted with the shared GFM markdown parser (the same parser used for post-mode table conversion), so every parser-recognized table form is caught — including pipe-less GFM tables and alignment-colon delimiters — while fenced code, thematic breaks, and plain pipes in prose are not counted. Feishu was verified to reject pipe-less tables the same way as piped ones (ErrCode 11310 in both cases). Guards are applied at every static-card decision point: - presentation-card.ts: withinCardTableLimit (parser-backed) plus feishuCardWithinTableLimit, which counts tables across every markdown element of a built card - outbound.ts: guard sendOutboundText and sendText payload routing; buildFeishuPayloadCard returns undefined when the built card exceeds the limit so generated presentation cards fall back to the text path - channel.ts: the message-action presentation path rejects the generated card when it exceeds the limit and routes delivery through the existing presentationFellBack -> sendPayload text fallback - reply-dispatcher.ts: guard useStaticCard only (useStreamingCard is left unchanged), and route streaming recovery through post delivery when the recovered content exceeds the limit, instead of retrying a static card the API would reject again Streaming CardKit cards are intentionally left unchanged: they push content into a markdown element rather than table components, so the 5-table component limit does not apply there. Opaque native Feishu cards supplied by the caller are not inspected. Part of #43690
1 parent b8f6086 commit 063938e

4 files changed

Lines changed: 113 additions & 21 deletions

File tree

‎extensions/feishu/src/channel.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import { resolveFeishuGroupToolPolicy } from "./policy.js";
9292
import {
9393
assertFeishuCardWithinEnvelope,
9494
buildFeishuPresentationCard,
95+
feishuCardWithinTableLimit,
9596
isFeishuCardWithinEnvelope,
9697
} from "./presentation-card.js";
9798
import {
@@ -1085,16 +1086,23 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount, FeishuProbeResul
10851086
if (textCard && !presentation) {
10861087
assertFeishuCardWithinEnvelope(textCard, "Feishu native card");
10871088
}
1089+
const presentationFallbackText = textCard
1090+
? undefined
1091+
: resolveLegacyInteractiveTextFallback({ text, interactive });
10881092
const generatedCard = presentation
10891093
? buildFeishuPresentationCard({
10901094
presentation,
1091-
fallbackText: textCard
1092-
? undefined
1093-
: resolveLegacyInteractiveTextFallback({ text, interactive }),
1095+
fallbackText: presentationFallbackText,
10941096
})
10951097
: undefined;
1098+
// Feishu static cards cap out at 5 table components (ErrCode 11310). Count
1099+
// tables across every markdown element of the generated card (fallback text
1100+
// and presentation blocks alike) and fall back to the text path when
1101+
// exceeded via presentationFellBack.
10961102
const presentationCard =
1097-
generatedCard && isFeishuCardWithinEnvelope(generatedCard)
1103+
generatedCard &&
1104+
feishuCardWithinTableLimit(generatedCard) &&
1105+
isFeishuCardWithinEnvelope(generatedCard)
10981106
? generatedCard
10991107
: undefined;
11001108
const presentationFellBack = Boolean(generatedCard && !presentationCard);

‎extensions/feishu/src/outbound.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ import type { ChannelOutboundAdapter } from "./outbound-runtime-api.js";
5555
import {
5656
assertFeishuCardWithinEnvelope,
5757
buildFeishuPresentationCardElements,
58+
feishuCardWithinTableLimit,
5859
isFeishuCardWithinEnvelope,
60+
withinCardTableLimit,
5961
} from "./presentation-card.js";
6062
import {
6163
sendCardFeishu,
@@ -216,6 +218,7 @@ function buildFeishuPayloadCard(params: {
216218
text: rawText,
217219
interactive,
218220
});
221+
219222
const elements = presentation
220223
? buildFeishuPresentationCardElements({ presentation, fallbackText: text })
221224
: [
@@ -250,7 +253,13 @@ function buildFeishuPayloadCard(params: {
250253
: {}),
251254
body: { elements },
252255
});
253-
return isFeishuCardWithinEnvelope(card) ? card : undefined;
256+
// Static cards cap out at 5 table components (Feishu ErrCode 11310). Count
257+
// tables across every markdown element of the built card (fallback text and
258+
// presentation blocks alike) and fall back to the text path when exceeded.
259+
if (!isFeishuCardWithinEnvelope(card) || !feishuCardWithinTableLimit(card)) {
260+
return undefined;
261+
}
262+
return card;
254263
}
255264

256265
// Keep this aligned with the shared fallback renderer: guidance is valid only
@@ -421,7 +430,10 @@ async function sendOutboundText(params: {
421430
// Decide card routing on the original text so card content is never
422431
// modified by post-md newline normalization. Only the post path below
423432
// materializes CommonMark soft breaks for Feishu rendering.
424-
if (renderMode === "card" || (renderMode === "auto" && shouldUseCard(text))) {
433+
if (
434+
(renderMode === "card" || (renderMode === "auto" && shouldUseCard(text))) &&
435+
withinCardTableLimit(text)
436+
) {
425437
return await reportFeishuOutboundDelivery(
426438
await sendMarkdownCardFeishu({
427439
cfg,
@@ -862,7 +874,9 @@ export const feishuOutbound: ChannelOutboundAdapter = {
862874

863875
const account = resolveFeishuAccount({ cfg, accountId: accountId ?? undefined });
864876
const renderMode = account.config?.renderMode ?? "auto";
865-
const useCard = renderMode === "card" || (renderMode === "auto" && shouldUseCard(text));
877+
const useCard =
878+
(renderMode === "card" || (renderMode === "auto" && shouldUseCard(text))) &&
879+
withinCardTableLimit(text);
866880
if (useCard) {
867881
const header = identity
868882
? {

‎extensions/feishu/src/presentation-card.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type MessagePresentationBlock,
88
type MessagePresentationButton,
99
} from "openclaw/plugin-sdk/interactive-runtime";
10+
import { markdownToIRWithMeta } from "openclaw/plugin-sdk/text-chunking";
1011
import { createFeishuCardInteractionEnvelope } from "./card-interaction.js";
1112

1213
type NormalizedMessagePresentation = NonNullable<ReturnType<typeof normalizeMessagePresentation>>;
@@ -57,6 +58,58 @@ export function assertFeishuCardWithinEnvelope(
5758
}
5859
}
5960

61+
/** Feishu allows at most 5 table components per static interactive card (ErrCode 11310). */
62+
const FEISHU_CARD_TABLE_LIMIT = 5;
63+
64+
/**
65+
* Count GFM tables with the shared markdown parser (the same parser used for
66+
* post-mode table conversion), so every parser-recognized table form is caught
67+
* — including pipe-less GFM tables and alignment-colon delimiters — while
68+
* fenced code, thematic breaks, and plain pipes in prose are not counted.
69+
*/
70+
function countMarkdownTables(text: string): number {
71+
if (!text) {
72+
return 0;
73+
}
74+
return markdownToIRWithMeta(text, { tableMode: "block" }).tables.length;
75+
}
76+
77+
/** Check whether the number of markdown tables is within Feishu static-card limits. */
78+
export function withinCardTableLimit(text: string): boolean {
79+
return countMarkdownTables(text) <= FEISHU_CARD_TABLE_LIMIT;
80+
}
81+
82+
function collectFeishuCardMarkdownTexts(value: unknown, out: string[]): void {
83+
if (Array.isArray(value)) {
84+
for (const item of value) {
85+
collectFeishuCardMarkdownTexts(item, out);
86+
}
87+
return;
88+
}
89+
if (!value || typeof value !== "object") {
90+
return;
91+
}
92+
const record = value as Record<string, unknown>;
93+
if (record.tag === "markdown" && typeof record.content === "string") {
94+
out.push(record.content);
95+
}
96+
for (const child of Object.values(record)) {
97+
collectFeishuCardMarkdownTexts(child, out);
98+
}
99+
}
100+
101+
/**
102+
* Check a built Feishu card JSON against the table limit by counting tables in
103+
* the combined markdown content of all its elements. Covers generated cards
104+
* whose tables come from fallback text as well as presentation blocks.
105+
*/
106+
export function feishuCardWithinTableLimit(card: Record<string, unknown>): boolean {
107+
const texts: string[] = [];
108+
collectFeishuCardMarkdownTexts(card, texts);
109+
const total = texts.reduce((sum, text) => sum + countMarkdownTables(text), 0);
110+
return total <= FEISHU_CARD_TABLE_LIMIT;
111+
}
112+
60113
function escapeFeishuCardMarkdownText(text: string): string {
61114
return text.replace(/[&<>]/g, (char) => {
62115
switch (char) {

‎extensions/feishu/src/reply-dispatcher.ts‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { chunkFeishuPostMarkdown, materializeFeishuPostMarkdownSoftBreaks } from
2828
import { buildFeishuMediaFallbackText } from "./media-fallback.js";
2929
import { sendMediaFeishu, shouldSuppressFeishuTextForVoiceMedia } from "./media.js";
3030
import type { MentionTarget } from "./mention-target.types.js";
31+
import { withinCardTableLimit } from "./presentation-card.js";
3132
import {
3233
createFeishuPartialReplyDeliveryError,
3334
createFeishuReplyDeliveryResult,
@@ -957,24 +958,39 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
957958
}
958959
const cardHeader = resolveCardHeader(agentId, identity);
959960
const cardNote = resolveCardNote(agentId, identity, prefixContext.prefixContext);
961+
// Static cards cap out at 5 table components (Feishu ErrCode 11310). When the
962+
// recovered content exceeds the limit, deliver it through the post path instead
963+
// of retrying a static card the API would reject again.
964+
const useRecoveryCard = withinCardTableLimit(content);
960965
return await sendChunkedTextReply({
961966
text: content,
962-
useCard: true,
967+
useCard: useRecoveryCard,
963968
infoKind,
964969
chunkMentions: requiredMentionTargets,
965970
sendChunk: async ({ chunk, mentions }) =>
966-
await sendStructuredCardFeishu({
967-
cfg,
968-
to: sendTarget,
969-
text: chunk,
970-
replyToMessageId: sendReplyToMessageId,
971-
replyInThread: effectiveReplyInThread,
972-
allowTopLevelReplyFallback,
973-
accountId,
974-
header: cardHeader,
975-
note: cardNote,
976-
...(mentions ? { mentions } : {}),
977-
}),
971+
useRecoveryCard
972+
? await sendStructuredCardFeishu({
973+
cfg,
974+
to: sendTarget,
975+
text: chunk,
976+
replyToMessageId: sendReplyToMessageId,
977+
replyInThread: effectiveReplyInThread,
978+
allowTopLevelReplyFallback,
979+
accountId,
980+
header: cardHeader,
981+
note: cardNote,
982+
...(mentions ? { mentions } : {}),
983+
})
984+
: await sendMessageFeishu({
985+
cfg,
986+
to: sendTarget,
987+
text: chunk,
988+
replyToMessageId: sendReplyToMessageId,
989+
replyInThread: effectiveReplyInThread,
990+
allowTopLevelReplyFallback,
991+
accountId,
992+
...(mentions ? { mentions } : {}),
993+
}),
978994
});
979995
};
980996

@@ -1289,7 +1305,8 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
12891305
hasText &&
12901306
(renderMode === "card" ||
12911307
(info?.kind === "block" && coreBlockStreamingEnabled && renderMode !== "raw") ||
1292-
(renderMode === "auto" && shouldUseCard(text)));
1308+
(renderMode === "auto" && shouldUseCard(text))) &&
1309+
withinCardTableLimit(text);
12931310
const useStreamingCard =
12941311
hasText &&
12951312
streamingEnabled &&

0 commit comments

Comments
 (0)