Skip to content

Commit 9fabdcf

Browse files
fix(feishu): send blocks as independent messages when blockStreaming is enabled (#94250)
* fix(feishu): send blocks as independent messages when blockStreaming is enabled * fix(feishu): preserve mention targets on first independently sent block * fix(feishu): route independent block sends through chunked text delivery Route the independent block-send path through sendChunkedTextReply instead of calling sendMessageFeishu directly, so the configured Feishu textChunkLimit is honored for completed blocks. Preserve mention targets only on the first emitted block chunk and keep sentBlockText based on the original block text for duplicate-final suppression. Add regression test with a small textChunkLimit that verifies a long block is split into chunked messages and mentions only appear on the first chunk. * fix(feishu): preserve block delivery target --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent a5a8d99 commit 9fabdcf

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
192192
});
193193
}
194194

195+
function useNonStreamingBlockAccount() {
196+
resolveFeishuAccountMock.mockReturnValue({
197+
accountId: "main",
198+
appId: "app_id",
199+
appSecret: "app_secret",
200+
domain: "feishu",
201+
config: {
202+
renderMode: "auto",
203+
streaming: false,
204+
blockStreaming: true,
205+
},
206+
});
207+
}
208+
195209
function setupNonStreamingAutoDispatcher() {
196210
useNonStreamingAutoAccount();
197211

@@ -667,6 +681,58 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
667681
});
668682
});
669683

684+
it("sends complete chunked blocks to the DM target", async () => {
685+
useNonStreamingBlockAccount();
686+
const runtime = getFeishuRuntimeMock();
687+
runtime.channel.text.resolveTextChunkLimit.mockReturnValue(10);
688+
runtime.channel.text.chunkTextWithMode.mockImplementation((text: string) =>
689+
text === "First paragraph." ? ["First ", "paragraph."] : [text],
690+
);
691+
const mentions = [{ openId: "ou_target", name: "Target User", key: "@_user_1" }];
692+
const { options } = createDispatcherHarness({
693+
chatId: "oc_p2p_chat",
694+
sendTarget: "user:ou_sender",
695+
mentionTargets: mentions,
696+
});
697+
698+
await options.deliver({ text: "First paragraph." }, { kind: "block" });
699+
await options.deliver(
700+
{ text: "Second paragraph.", mediaUrl: "https://example.com/block.png" },
701+
{ kind: "block" },
702+
);
703+
await options.onIdle?.();
704+
705+
expect(sendMessageFeishuMock).toHaveBeenCalledTimes(3);
706+
expectMockArgFields(sendMessageFeishuMock, "first block chunk", {
707+
to: "user:ou_sender",
708+
text: "First ",
709+
mentions,
710+
});
711+
expectMockArgFields(sendMessageFeishuMock, "second block chunk", { text: "paragraph." }, 1);
712+
expectMockArgFields(sendMessageFeishuMock, "second block", { text: "Second paragraph." }, 2);
713+
expect(sendMessageFeishuMock.mock.calls[1]?.[0]).not.toHaveProperty("mentions");
714+
expect(sendMessageFeishuMock.mock.calls[2]?.[0]).not.toHaveProperty("mentions");
715+
expectMockArgFields(sendMediaFeishuMock, "block media", {
716+
to: "user:ou_sender",
717+
mediaUrl: "https://example.com/block.png",
718+
});
719+
expect(streamingInstances).toHaveLength(0);
720+
expect(sendStructuredCardFeishuMock).not.toHaveBeenCalled();
721+
});
722+
723+
it("delivers a final message when it differs from independently sent blocks", async () => {
724+
useNonStreamingBlockAccount();
725+
const { options } = createDispatcherHarness();
726+
727+
await options.deliver({ text: "partial block" }, { kind: "block" });
728+
await options.deliver({ text: "final answer" }, { kind: "final" });
729+
await options.onIdle?.();
730+
731+
expect(sendMessageFeishuMock).toHaveBeenCalledTimes(2);
732+
expectMockArgFields(sendMessageFeishuMock, "block message", { text: "partial block" });
733+
expectMockArgFields(sendMessageFeishuMock, "final message", { text: "final answer" }, 1);
734+
});
735+
670736
it("does not prepend automatic mentions to streaming card closes", async () => {
671737
const overrides = {
672738
runtime: createRuntimeLogger(),

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
272272
// Partial previews are replaceable; only committed final text may precede an error notice.
273273
let hasStreamingFinalText = false;
274274
const deliveredFinalTexts = new Set<string>();
275+
let sentIndependentBlockText = false;
275276
let partialUpdateQueue: Promise<void> = Promise.resolve();
276277
let streamingStartPromise: Promise<void> | null = null;
277278
let streamingClosedForReply = false;
@@ -638,6 +639,7 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
638639
if (!replyLifecycleStateInitialized) {
639640
replyLifecycleStateInitialized = true;
640641
deliveredFinalTexts.clear();
642+
sentIndependentBlockText = false;
641643
streamingClosedForReply = false;
642644
streamingCloseErroredForReply = false;
643645
visibleReplySent = false;
@@ -717,8 +719,36 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
717719
if (shouldDeliverText) {
718720
if (info?.kind === "block") {
719721
// Drop internal block chunks unless we can safely consume them as
720-
// streaming-card fallback content.
722+
// streaming-card fallback content or send them as independent
723+
// messages for true progressive delivery.
721724
if (!useStreamingCard) {
725+
if (coreBlockStreamingEnabled) {
726+
// Reuse normal text chunking, but notify mentions only on the first visible chunk.
727+
const isFirstBlock = !sentIndependentBlockText;
728+
await sendChunkedTextReply({
729+
text,
730+
useCard: false,
731+
infoKind: "block",
732+
sendChunk: async ({ chunk, isFirst }) => {
733+
await sendMessageFeishu({
734+
cfg,
735+
to: sendTarget,
736+
text: chunk,
737+
replyToMessageId: sendReplyToMessageId,
738+
replyInThread: effectiveReplyInThread,
739+
allowTopLevelReplyFallback,
740+
accountId,
741+
...(isFirstBlock && isFirst && mentionTargets?.length
742+
? { mentions: mentionTargets }
743+
: {}),
744+
});
745+
},
746+
});
747+
sentIndependentBlockText = true;
748+
if (hasMedia) {
749+
await sendMediaReplies(payload);
750+
}
751+
}
722752
return;
723753
}
724754
startStreaming();

0 commit comments

Comments
 (0)