Skip to content

Commit 2aa4b69

Browse files
authored
fix(feishu): preserve accepted reply chunks without receipts (#128213)
1 parent 16a800e commit 2aa4b69

2 files changed

Lines changed: 62 additions & 7 deletions

File tree

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,14 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
16381638
.deliver({ text }, { kind: "final" })
16391639
.catch((caught: unknown) => caught);
16401640

1641-
expect(isChannelPartialDeliveryError(error)).toBe(true);
1641+
expect(error).toMatchObject({
1642+
code: "CHANNEL_PARTIAL_DELIVERY",
1643+
deliveryResult: {
1644+
content: text,
1645+
messageIds: [],
1646+
visibleReplySent: true,
1647+
},
1648+
});
16421649
expect(provider).toHaveBeenCalledOnce();
16431650
await Promise.resolve(options.onError?.(error, { kind: "final" }));
16441651
expect(result.getVisibleReplyState().visibleReplySent).toBe(true);
@@ -1650,6 +1657,50 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
16501657
},
16511658
);
16521659

1660+
it.each([
1661+
{ kind: "text", provider: sendMessageFeishuMock, acceptedBeforeReceiptLoss: 0 },
1662+
{ kind: "text", provider: sendMessageFeishuMock, acceptedBeforeReceiptLoss: 1 },
1663+
{ kind: "card", provider: sendStructuredCardFeishuMock, acceptedBeforeReceiptLoss: 0 },
1664+
{ kind: "card", provider: sendStructuredCardFeishuMock, acceptedBeforeReceiptLoss: 1 },
1665+
])(
1666+
"retains accepted $kind chunk content after receipt loss with $acceptedBeforeReceiptLoss prior receipts",
1667+
async ({ kind, provider, acceptedBeforeReceiptLoss }) => {
1668+
useNonStreamingAutoAccount();
1669+
const runtime = getFeishuRuntimeMock();
1670+
runtime.channel.text.resolveTextChunkLimit.mockReturnValue(6);
1671+
runtime.channel.text.chunkMarkdownTextWithMode.mockReturnValue(["first", "second", "third"]);
1672+
1673+
if (acceptedBeforeReceiptLoss > 0) {
1674+
provider.mockResolvedValueOnce({ messageId: "om-first" });
1675+
}
1676+
provider.mockRejectedValueOnce(
1677+
createChannelPartialDeliveryError(
1678+
new Error("Feishu reply failed: no message_id returned"),
1679+
{
1680+
messageIds: [],
1681+
visibleReplySent: true,
1682+
},
1683+
),
1684+
);
1685+
const { options } = createDispatcherHarness();
1686+
const text = kind === "card" ? "| first | second |\n| - | - |" : "firstsecondthird";
1687+
1688+
const error = await options
1689+
.deliver({ text }, { kind: "final" })
1690+
.catch((caught: unknown) => caught);
1691+
1692+
expect(error).toMatchObject({
1693+
code: "CHANNEL_PARTIAL_DELIVERY",
1694+
deliveryResult: {
1695+
content: acceptedBeforeReceiptLoss > 0 ? "firstsecond" : "first",
1696+
messageIds: acceptedBeforeReceiptLoss > 0 ? ["om-first"] : [],
1697+
visibleReplySent: true,
1698+
},
1699+
});
1700+
expect(provider).toHaveBeenCalledTimes(acceptedBeforeReceiptLoss + 1);
1701+
},
1702+
);
1703+
16531704
it("retains the finalized streaming card when companion media never dispatches", async () => {
16541705
const marker = Object.assign(
16551706
new Error("media load failed", { cause: new Error("blocked local load") }),

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -763,18 +763,22 @@ export function createFeishuReplyDispatcher(params: CreateFeishuReplyDispatcherP
763763
acceptedChunks.push(chunk);
764764
markVisibleReplySent();
765765
} catch (error: unknown) {
766-
if (isChannelPartialDeliveryError(error)) {
766+
const acceptedChunk = isChannelPartialDeliveryError(error)
767+
? error.deliveryResult
768+
: undefined;
769+
if (acceptedChunk) {
770+
acceptedChunks.push(acceptedChunk.content ?? chunk);
767771
markVisibleReplySent();
768772
}
769-
throw createFeishuPartialReplyDeliveryError(
770-
error,
771-
createFeishuReplyDeliveryResult({
773+
throw createFeishuPartialReplyDeliveryError(error, {
774+
...acceptedChunk,
775+
...createFeishuReplyDeliveryResult({
772776
results,
773-
visibleReplySent: results.length > 0,
777+
visibleReplySent: results.length > 0 || acceptedChunk !== undefined,
774778
content: acceptedChunks.join(""),
775779
kind: paramsLocal.useCard ? "card" : "text",
776780
}),
777-
);
781+
});
778782
}
779783
}
780784
if (paramsLocal.infoKind === "final") {

0 commit comments

Comments
 (0)