From efd9e45cdc375cb7bbf72eb038fcf4971c958c2b Mon Sep 17 00:00:00 2001 From: Nimrod Gutman Date: Sat, 14 Mar 2026 13:14:12 +0200 Subject: [PATCH] fix(reply): make external btw replies explicit --- src/auto-reply/reply/reply-payloads.ts | 6 ++++-- src/auto-reply/reply/route-reply.test.ts | 8 ++++---- src/infra/outbound/deliver.test.ts | 12 ++++++++---- src/infra/outbound/outbound.test.ts | 4 ++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/auto-reply/reply/reply-payloads.ts b/src/auto-reply/reply/reply-payloads.ts index e46ae422a9f..4bf8194e188 100644 --- a/src/auto-reply/reply/reply-payloads.ts +++ b/src/auto-reply/reply/reply-payloads.ts @@ -15,10 +15,12 @@ export function formatBtwTextForExternalDelivery(payload: ReplyPayload): string if (!text) { return payload.text; } - if (!payload.btw?.question?.trim()) { + const question = payload.btw?.question?.trim(); + if (!question) { return payload.text; } - return text.startsWith("BTW:") ? text : `BTW: ${text}`; + const formatted = `BTW\nQuestion: ${question}\n\n${text}`; + return text === formatted || text.startsWith("BTW\nQuestion:") ? text : formatted; } function resolveReplyThreadingForPayload(params: { diff --git a/src/auto-reply/reply/route-reply.test.ts b/src/auto-reply/reply/route-reply.test.ts index ad9efed024c..d8ad0dd7e7e 100644 --- a/src/auto-reply/reply/route-reply.test.ts +++ b/src/auto-reply/reply/route-reply.test.ts @@ -302,7 +302,7 @@ describe("routeReply", () => { ); }); - it("prefixes BTW replies on routed sends", async () => { + it("formats BTW replies prominently on routed sends", async () => { mocks.sendMessageSlack.mockClear(); await routeReply({ payload: { text: "323", btw: { question: "what is 17 * 19?" } }, @@ -312,12 +312,12 @@ describe("routeReply", () => { }); expect(mocks.sendMessageSlack).toHaveBeenCalledWith( "channel:C123", - "BTW: 323", + "BTW\nQuestion: what is 17 * 19?\n\n323", expect.any(Object), ); }); - it("prefixes BTW replies on routed discord sends", async () => { + it("formats BTW replies prominently on routed discord sends", async () => { mocks.sendMessageDiscord.mockClear(); await routeReply({ payload: { text: "323", btw: { question: "what is 17 * 19?" } }, @@ -327,7 +327,7 @@ describe("routeReply", () => { }); expect(mocks.sendMessageDiscord).toHaveBeenCalledWith( "channel:123456", - "BTW: 323", + "BTW\nQuestion: what is 17 * 19?\n\n323", expect.any(Object), ); }); diff --git a/src/infra/outbound/deliver.test.ts b/src/infra/outbound/deliver.test.ts index d30415d0bcb..274b2e09897 100644 --- a/src/infra/outbound/deliver.test.ts +++ b/src/infra/outbound/deliver.test.ts @@ -288,7 +288,7 @@ describe("deliverOutboundPayloads", () => { ); }); - it("prefixes BTW replies for telegram delivery", async () => { + it("formats BTW replies prominently for telegram delivery", async () => { const sendTelegram = vi.fn().mockResolvedValue({ messageId: "m1", chatId: "c1" }); await deliverTelegramPayload({ @@ -301,7 +301,7 @@ describe("deliverOutboundPayloads", () => { expect(sendTelegram).toHaveBeenCalledWith( "123", - "BTW: 323", + "BTW\nQuestion: what is 17 * 19?\n\n323", expect.objectContaining({ verbose: false, textMode: "html" }), ); }); @@ -743,7 +743,7 @@ describe("deliverOutboundPayloads", () => { ]); }); - it("prefixes BTW replies for whatsapp delivery", async () => { + it("formats BTW replies prominently for whatsapp delivery", async () => { const sendWhatsApp = vi.fn().mockResolvedValue({ messageId: "w1", toJid: "jid" }); await deliverWhatsAppPayload({ @@ -751,7 +751,11 @@ describe("deliverOutboundPayloads", () => { payload: { text: "323", btw: { question: "what is 17 * 19?" } }, }); - expect(sendWhatsApp).toHaveBeenCalledWith("+1555", "BTW: 323", expect.any(Object)); + expect(sendWhatsApp).toHaveBeenCalledWith( + "+1555", + "BTW\nQuestion: what is 17 * 19?\n\n323", + expect.any(Object), + ); }); it("continues on errors when bestEffort is enabled", async () => { diff --git a/src/infra/outbound/outbound.test.ts b/src/infra/outbound/outbound.test.ts index 5e5ea6f0ea8..9e58d5c6c05 100644 --- a/src/infra/outbound/outbound.test.ts +++ b/src/infra/outbound/outbound.test.ts @@ -1258,14 +1258,14 @@ describe("normalizeOutboundPayloads", () => { expect(normalized).toEqual([{ text: "final answer", mediaUrls: [] }]); }); - it("prefixes BTW replies for external delivery", () => { + it("formats BTW replies prominently for external delivery", () => { const normalized = normalizeOutboundPayloads([ { text: "323", btw: { question: "what is 17 * 19?" }, }, ]); - expect(normalized).toEqual([{ text: "BTW: 323", mediaUrls: [] }]); + expect(normalized).toEqual([{ text: "BTW\nQuestion: what is 17 * 19?\n\n323", mediaUrls: [] }]); }); });