Skip to content

Commit e6f34b2

Browse files
SidTakhoffman
andauthored
fix(feishu): preserve block streaming text when final payload is missing (#30663)
* fix(feishu): preserve block streaming text when final payload is missing When Feishu card streaming receives block payloads without matching final/partial callbacks, keep block text in stream state so onIdle close still publishes the reply instead of an empty message. Add a regression test for block-only streaming. Closes #30628 * Feishu: preserve streaming block fallback when final text is missing --------- Co-authored-by: Tak Hoffman <[email protected]>
1 parent 17bb87f commit e6f34b2

3 files changed

Lines changed: 80 additions & 17 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Docs: https://docs.openclaw.ai
154154
- Feishu/Inbound ordering: serialize message handling per chat while preserving cross-chat concurrency to avoid same-chat race drops under bursty inbound traffic. (#31807)
155155
- Feishu/Typing notification suppression: skip typing keepalive reaction re-adds when the indicator is already active, preventing duplicate notification pings from repeated identical emoji adds. (#31580)
156156
- Feishu/Probe failure backoff: cache API and timeout probe failures for one minute per account key while preserving abort-aware probe timeouts, reducing repeated health-check retries during transient credential/network outages. (#29970)
157+
- Feishu/Streaming block fallback: preserve markdown block stream text as final streaming-card content when final payload text is missing, while still suppressing non-card internal block chunk delivery. (#30663)
157158
- BlueBubbles/Message metadata: harden send response ID extraction, include sender identity in DM context, and normalize inbound `message_id` selection to avoid duplicate ID metadata. (#23970) Thanks @tyler6204.
158159
- WebChat/markdown tables: ensure GitHub-flavored markdown table parsing is explicitly enabled at render time and add horizontal overflow handling for wide tables, with regression coverage for table-only and mixed text+table content. (#32365) Thanks @BlueBirdBack.
159160
- Feishu/default account resolution: always honor explicit `channels.feishu.defaultAccount` during outbound account selection (including top-level-credential setups where the preferred id is not present in `accounts`), instead of silently falling back to another account id. (#32253) Thanks @bmendonca3.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
226226
expect(sendMarkdownCardFeishuMock).not.toHaveBeenCalled();
227227
});
228228

229+
it("closes streaming with block text when final reply is missing", async () => {
230+
createFeishuReplyDispatcher({
231+
cfg: {} as never,
232+
agentId: "agent",
233+
runtime: { log: vi.fn(), error: vi.fn() } as never,
234+
chatId: "oc_chat",
235+
});
236+
237+
const options = createReplyDispatcherWithTypingMock.mock.calls[0]?.[0];
238+
await options.deliver({ text: "```md\npartial answer\n```" }, { kind: "block" });
239+
await options.onIdle?.();
240+
241+
expect(streamingInstances).toHaveLength(1);
242+
expect(streamingInstances[0].start).toHaveBeenCalledTimes(1);
243+
expect(streamingInstances[0].close).toHaveBeenCalledTimes(1);
244+
expect(streamingInstances[0].close).toHaveBeenCalledWith("```md\npartial answer\n```");
245+
});
246+
229247
it("sends media-only payloads as attachments", async () => {
230248
createFeishuReplyDispatcher({
231249
cfg: {} as never,

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

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,48 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
146146
let partialUpdateQueue: Promise<void> = Promise.resolve();
147147
let streamingStartPromise: Promise<void> | null = null;
148148

149+
const mergeStreamingText = (nextText: string) => {
150+
if (!streamText) {
151+
streamText = nextText;
152+
return;
153+
}
154+
if (nextText.startsWith(streamText)) {
155+
// Handle cumulative partial payloads where nextText already includes prior text.
156+
streamText = nextText;
157+
return;
158+
}
159+
if (streamText.endsWith(nextText)) {
160+
return;
161+
}
162+
streamText += nextText;
163+
};
164+
165+
const queueStreamingUpdate = (
166+
nextText: string,
167+
options?: {
168+
dedupeWithLastPartial?: boolean;
169+
},
170+
) => {
171+
if (!nextText) {
172+
return;
173+
}
174+
if (options?.dedupeWithLastPartial && nextText === lastPartial) {
175+
return;
176+
}
177+
if (options?.dedupeWithLastPartial) {
178+
lastPartial = nextText;
179+
}
180+
mergeStreamingText(nextText);
181+
partialUpdateQueue = partialUpdateQueue.then(async () => {
182+
if (streamingStartPromise) {
183+
await streamingStartPromise;
184+
}
185+
if (streaming?.isActive()) {
186+
await streaming.update(streamText);
187+
}
188+
});
189+
};
190+
149191
const startStreaming = () => {
150192
if (!streamingEnabled || streamingStartPromise || streaming) {
151193
return;
@@ -205,12 +247,6 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
205247
void typingCallbacks.onReplyStart?.();
206248
},
207249
deliver: async (payload: ReplyPayload, info) => {
208-
// FIX: Filter out internal 'block' reasoning chunks immediately to prevent
209-
// data leak and race conditions with streaming state initialization.
210-
if (info?.kind === "block") {
211-
return;
212-
}
213-
214250
const text = payload.text ?? "";
215251
const mediaList =
216252
payload.mediaUrls && payload.mediaUrls.length > 0
@@ -228,6 +264,18 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
228264
if (hasText) {
229265
const useCard = renderMode === "card" || (renderMode === "auto" && shouldUseCard(text));
230266

267+
if (info?.kind === "block") {
268+
// Drop internal block chunks unless we can safely consume them as
269+
// streaming-card fallback content.
270+
if (!(streamingEnabled && useCard)) {
271+
return;
272+
}
273+
startStreaming();
274+
if (streamingStartPromise) {
275+
await streamingStartPromise;
276+
}
277+
}
278+
231279
if (info?.kind === "final" && streamingEnabled && useCard) {
232280
startStreaming();
233281
if (streamingStartPromise) {
@@ -236,6 +284,11 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
236284
}
237285

238286
if (streaming?.isActive()) {
287+
if (info?.kind === "block") {
288+
// Some runtimes emit block payloads without onPartial/final callbacks.
289+
// Mirror block text into streamText so onIdle close still sends content.
290+
queueStreamingUpdate(text);
291+
}
239292
if (info?.kind === "final") {
240293
streamText = text;
241294
await closeStreaming();
@@ -331,19 +384,10 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
331384
onModelSelected: prefixContext.onModelSelected,
332385
onPartialReply: streamingEnabled
333386
? (payload: ReplyPayload) => {
334-
if (!payload.text || payload.text === lastPartial) {
387+
if (!payload.text) {
335388
return;
336389
}
337-
lastPartial = payload.text;
338-
streamText = payload.text;
339-
partialUpdateQueue = partialUpdateQueue.then(async () => {
340-
if (streamingStartPromise) {
341-
await streamingStartPromise;
342-
}
343-
if (streaming?.isActive()) {
344-
await streaming.update(streamText);
345-
}
346-
});
390+
queueStreamingUpdate(payload.text, { dedupeWithLastPartial: true });
347391
}
348392
: undefined,
349393
},

0 commit comments

Comments
 (0)