Skip to content

Commit 0a806a4

Browse files
committed
Preserve heartbeat text for media mirrors
PER-161
1 parent b154199 commit 0a806a4

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎src/infra/heartbeat-runner.respects-ackmaxchars-heartbeat-acks.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,53 @@ describe("runHeartbeatOnce ack handling", () => {
332332
});
333333
});
334334

335+
it("preserves heartbeat mirror text when notifications include media URLs", async () => {
336+
await withTempHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => {
337+
const cfg = createWhatsAppHeartbeatConfig({
338+
tmpDir,
339+
storePath,
340+
});
341+
const sessionKey = "agent:main:whatsapp:direct:test-user";
342+
await seedSessionStore(storePath, sessionKey, {
343+
sessionId: "direct-sid",
344+
lastChannel: "whatsapp",
345+
lastProvider: "whatsapp",
346+
lastTo: "test-user",
347+
});
348+
349+
replySpy.mockResolvedValue({
350+
text: "Deepali sent the programme attachment.",
351+
mediaUrls: ["file:///tmp/programme.pdf"],
352+
});
353+
const sendWhatsApp = createMessageSendSpy();
354+
355+
await runHeartbeatOnce({
356+
cfg,
357+
sessionKey,
358+
deps: {
359+
...makeWhatsAppDeps({ sendWhatsApp }),
360+
getReplyFromConfig: replySpy,
361+
},
362+
});
363+
364+
expect(sendWhatsApp).toHaveBeenCalledWith(
365+
"test-user",
366+
"Deepali sent the programme attachment.",
367+
expect.any(Object),
368+
);
369+
expect(transcriptMocks.appendAssistantMessageToSessionTranscript).toHaveBeenCalledTimes(1);
370+
const appendCall =
371+
transcriptMocks.appendAssistantMessageToSessionTranscript.mock.calls[0]?.[0];
372+
expect(appendCall).toMatchObject({
373+
agentId: "main",
374+
sessionKey,
375+
text: "Deepali sent the programme attachment.",
376+
});
377+
expect(appendCall?.text).not.toBe("programme.pdf");
378+
expect(appendCall?.idempotencyKey).toMatch(/^heartbeat-delivery-mirror:v1:/);
379+
});
380+
});
381+
335382
it.each([
336383
{
337384
title: "does not deliver HEARTBEAT_OK to telegram when showOk is false",

‎src/infra/heartbeat-runner.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ function buildHeartbeatDeliveryMirror(params: {
216216
if (!text && mediaUrls.length === 0) {
217217
return undefined;
218218
}
219+
const mirrorMediaUrls = text ? [] : mediaUrls;
219220
return {
220221
sessionKey: params.sessionKey,
221222
agentId: params.agentId,
222223
...(text ? { text } : {}),
223-
...(mediaUrls.length > 0 ? { mediaUrls } : {}),
224+
...(mirrorMediaUrls.length > 0 ? { mediaUrls: mirrorMediaUrls } : {}),
224225
idempotencyKey: buildHeartbeatMirrorIdempotencyKey({
225226
runSessionKey: params.runSessionKey,
226227
sessionKey: params.sessionKey,

0 commit comments

Comments
 (0)