Skip to content

Commit b521ce6

Browse files
steipetePeter Steinberger
andauthored
fix(feishu): consume implicit reply targets across media delivery (#117196)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 602b575 commit b521ce6

2 files changed

Lines changed: 135 additions & 13 deletions

File tree

‎extensions/feishu/src/outbound.test.ts‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,6 +2394,118 @@ describe("feishuOutbound.sendMedia replyToId forwarding", () => {
23942394
expect(sendMediaCall()?.replyInThread).toBe(false);
23952395
});
23962396

2397+
it("consumes an implicit first-reply target on the caption before sending its attachment", async () => {
2398+
await feishuOutbound.sendMedia?.({
2399+
cfg: emptyConfig,
2400+
to: "chat_1",
2401+
text: "caption text",
2402+
mediaUrl: "https://example.com/image.png",
2403+
replyToId: "om_reply_target",
2404+
replyToIdSource: "implicit",
2405+
replyToMode: "first",
2406+
accountId: "main",
2407+
});
2408+
2409+
expect(sendMessageCall()?.replyToMessageId).toBe("om_reply_target");
2410+
expect(sendMediaCall()?.replyToMessageId).toBeUndefined();
2411+
});
2412+
2413+
it("does not reuse an implicit first-reply target for the upload-failure fallback", async () => {
2414+
sendMediaFeishuMock.mockRejectedValueOnce(new Error("upload failed"));
2415+
2416+
await feishuOutbound.sendMedia?.({
2417+
cfg: emptyConfig,
2418+
to: "chat_1",
2419+
text: "caption text",
2420+
mediaUrl: "https://example.com/image.png",
2421+
replyToId: "om_reply_target",
2422+
replyToIdSource: "implicit",
2423+
replyToMode: "first",
2424+
accountId: "main",
2425+
});
2426+
2427+
expect(sendMessageFeishuMock.mock.calls[0]?.[0]?.replyToMessageId).toBe("om_reply_target");
2428+
expect(sendMessageFeishuMock.mock.calls[1]?.[0]?.replyToMessageId).toBeUndefined();
2429+
});
2430+
2431+
it("preserves an unconsumed implicit reply target when the first media upload fails", async () => {
2432+
sendMediaFeishuMock.mockRejectedValueOnce(new Error("upload failed"));
2433+
2434+
await feishuOutbound.sendMedia?.({
2435+
cfg: emptyConfig,
2436+
to: "chat_1",
2437+
text: "spoken reply",
2438+
mediaUrl: "https://example.com/reply.mp3",
2439+
audioAsVoice: true,
2440+
replyToId: "om_reply_target",
2441+
replyToIdSource: "implicit",
2442+
replyToMode: "first",
2443+
accountId: "main",
2444+
});
2445+
2446+
expect(sendMediaCall()?.replyToMessageId).toBe("om_reply_target");
2447+
expect(sendMessageCall()?.replyToMessageId).toBe("om_reply_target");
2448+
});
2449+
2450+
it("consumes an implicit first-reply target on degraded voice media before its text", async () => {
2451+
sendMediaFeishuMock.mockResolvedValueOnce({
2452+
messageId: "file_msg",
2453+
voiceIntentDegradedToFile: true,
2454+
});
2455+
2456+
await feishuOutbound.sendMedia?.({
2457+
cfg: emptyConfig,
2458+
to: "chat_1",
2459+
text: "spoken reply",
2460+
mediaUrl: "https://example.com/reply.mp3",
2461+
audioAsVoice: true,
2462+
replyToId: "om_reply_target",
2463+
replyToIdSource: "implicit",
2464+
replyToMode: "first",
2465+
accountId: "main",
2466+
});
2467+
2468+
expect(sendMediaCall()?.replyToMessageId).toBe("om_reply_target");
2469+
expect(sendMessageCall()?.replyToMessageId).toBeUndefined();
2470+
});
2471+
2472+
it("keeps explicit first-mode reply targets sticky across captions and media", async () => {
2473+
await feishuOutbound.sendMedia?.({
2474+
cfg: emptyConfig,
2475+
to: "chat_1",
2476+
text: "caption text",
2477+
mediaUrl: "https://example.com/image.png",
2478+
replyToId: "om_reply_target",
2479+
replyToIdSource: "explicit",
2480+
replyToMode: "first",
2481+
accountId: "main",
2482+
});
2483+
2484+
expect(sendMessageCall()?.replyToMessageId).toBe("om_reply_target");
2485+
expect(sendMediaCall()?.replyToMessageId).toBe("om_reply_target");
2486+
});
2487+
2488+
it("keeps native topic roots sticky across captions, attachments, and fallback", async () => {
2489+
sendMediaFeishuMock.mockRejectedValueOnce(new Error("upload failed"));
2490+
2491+
await feishuOutbound.sendMedia?.({
2492+
cfg: emptyConfig,
2493+
to: "chat_1",
2494+
text: "caption text",
2495+
mediaUrl: "https://example.com/image.png",
2496+
threadId: "om_topic_root",
2497+
replyToMode: "first",
2498+
accountId: "main",
2499+
});
2500+
2501+
expect(sendMediaCall()?.replyToMessageId).toBe("om_topic_root");
2502+
expect(sendMediaCall()?.replyInThread).toBe(true);
2503+
for (const [params] of sendMessageFeishuMock.mock.calls) {
2504+
expect(params.replyToMessageId).toBe("om_topic_root");
2505+
expect(params.replyInThread).toBe(true);
2506+
}
2507+
});
2508+
23972509
it("forwards threadId as replyInThread=true to sendMediaFeishu", async () => {
23982510
await feishuOutbound.sendMedia?.({
23992511
cfg: emptyConfig,

‎extensions/feishu/src/outbound.ts‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,27 @@ export const feishuOutbound: ChannelOutboundAdapter = {
876876
accountId,
877877
mediaLocalRoots,
878878
replyToId,
879+
replyToIdSource,
880+
replyToMode,
879881
threadId,
880882
onDeliveryResult,
881883
}) => {
882-
const { replyToMessageId, replyInThread } = resolveFeishuReplyMode({
884+
const { normalizedReplyToId } = resolveFeishuReplyMode({
883885
replyToId,
884886
threadId,
885887
});
888+
const nextReplyToId = createReplyToFanout({
889+
replyToId: normalizedReplyToId,
890+
replyToIdSource,
891+
replyToMode,
892+
});
893+
const nextReplyMode = () => {
894+
const { replyToMessageId, replyInThread } = resolveFeishuReplyMode({
895+
replyToId: nextReplyToId(),
896+
threadId,
897+
});
898+
return { replyToMessageId, replyInThread };
899+
};
886900
const commentTarget = parseFeishuCommentTarget(to);
887901
if (commentTarget) {
888902
const commentText = mediaUrl?.trim()
@@ -897,8 +911,7 @@ export const feishuOutbound: ChannelOutboundAdapter = {
897911
to,
898912
text: commentText,
899913
accountId: accountId ?? undefined,
900-
replyToMessageId,
901-
replyInThread,
914+
...nextReplyMode(),
902915
});
903916
}
904917

@@ -920,8 +933,7 @@ export const feishuOutbound: ChannelOutboundAdapter = {
920933
to,
921934
text,
922935
accountId: accountId ?? undefined,
923-
replyToMessageId,
924-
replyInThread,
936+
...nextReplyMode(),
925937
});
926938
textSent = true;
927939
await reportDelivery(textResult);
@@ -930,15 +942,15 @@ export const feishuOutbound: ChannelOutboundAdapter = {
930942
// Upload and send media if URL or local path provided
931943
if (mediaUrl) {
932944
let mediaResult: Awaited<ReturnType<typeof sendMediaFeishu>>;
945+
const mediaReplyMode = nextReplyMode();
933946
try {
934947
mediaResult = await sendMediaFeishu({
935948
cfg,
936949
to,
937950
mediaUrl,
938951
accountId: accountId ?? undefined,
939952
mediaLocalRoots,
940-
replyToMessageId,
941-
replyInThread,
953+
...mediaReplyMode,
942954
...(audioAsVoice === true ? { audioAsVoice: true } : {}),
943955
});
944956
} catch (err) {
@@ -957,8 +969,8 @@ export const feishuOutbound: ChannelOutboundAdapter = {
957969
to,
958970
text: fallbackText,
959971
accountId: accountId ?? undefined,
960-
replyToMessageId,
961-
replyInThread,
972+
// A rejected upload never delivered its attempted reply target.
973+
...(textSent ? nextReplyMode() : mediaReplyMode),
962974
});
963975
await reportDelivery(fallbackResult);
964976
return fallbackResult;
@@ -973,8 +985,7 @@ export const feishuOutbound: ChannelOutboundAdapter = {
973985
to,
974986
text,
975987
accountId: accountId ?? undefined,
976-
replyToMessageId,
977-
replyInThread,
988+
...nextReplyMode(),
978989
});
979990
await reportDelivery(textResult);
980991
}
@@ -987,8 +998,7 @@ export const feishuOutbound: ChannelOutboundAdapter = {
987998
to,
988999
text: text ?? "",
9891000
accountId: accountId ?? undefined,
990-
replyToMessageId,
991-
replyInThread,
1001+
...nextReplyMode(),
9921002
});
9931003
},
9941004
}),

0 commit comments

Comments
 (0)