Skip to content

Commit ab989f0

Browse files
ruel225claudesteipete
authored
fix(outbound): deliver media through sendFormattedMedia-only adapters (#133168)
* fix(outbound): count sendFormattedMedia adapters as media-capable supportsMedia ignored sendFormattedMedia, so a compliant plugin that implements only the formatted sender family had every media URL silently dropped: deliver-core fell back to plain text, recorded the payload as sent, and mirrored mediaUrls as empty, leaving no trace of the loss. Media-only payloads even failed with a factually wrong "does not implement sendMedia" error. deliver-core already prefers sendFormattedMedia over sendMedia when the handler claims media support, so the two senders are equivalent alternatives; the outbound surface contract accepts either alone. Count both when building the handler and make the fallback diagnostics name both senders. Co-Authored-By: Claude <[email protected]> * refactor(outbound): unify media dispatch and cover formatted-only adapters Select the bound media sender once while preserving formatted precedence, queue handoff guards, and delivery identities. Extend the existing sender fixture to cover captionless and multiple attachments without an ordinary media sender, including the delivered-media observer. Co-authored-by: ruel225 <[email protected]> --------- Co-authored-by: ruel225 <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 48b0857 commit ab989f0

3 files changed

Lines changed: 119 additions & 80 deletions

File tree

‎src/infra/outbound/deliver-channel.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ function createPluginHandler(
315315
cfg: params.cfg,
316316
accountId: params.accountId,
317317
}) === true,
318-
supportsMedia: Boolean(messageMedia ?? sendMedia),
318+
// sendFormattedMedia is a first-class media sender (deliver-core prefers it
319+
// over sendMedia), so leaving it out here silently drops media for
320+
// formatted-only adapters and records the fallback as a plain sent text.
321+
supportsMedia: Boolean(messageMedia ?? sendMedia ?? outbound?.sendFormattedMedia),
319322
sanitizeText: outbound?.sanitizeText
320323
? (payload) =>
321324
outbound.sanitizeText!({

‎src/infra/outbound/deliver-core.ts‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ export async function deliverOutboundPayloadsCore(
398398
}
399399
} else if (!deliveryHandler.supportsMedia) {
400400
log.warn(
401-
"Plugin outbound adapter does not implement sendMedia; media URLs will be dropped and text fallback will be used",
401+
"Plugin outbound adapter does not implement sendMedia or sendFormattedMedia; media URLs will be dropped and text fallback will be used",
402402
{
403403
channel,
404404
to,
@@ -408,7 +408,7 @@ export async function deliverOutboundPayloadsCore(
408408
const fallbackText = payloadSummary.text.trim();
409409
if (!fallbackText) {
410410
throw new Error(
411-
"Plugin outbound adapter does not implement sendMedia and no text fallback is available for media payload",
411+
"Plugin outbound adapter does not implement sendMedia or sendFormattedMedia and no text fallback is available for media payload",
412412
);
413413
}
414414
await sendTextChunks(deliveryHandler, fallbackText, sendOverrides);
@@ -423,23 +423,18 @@ export async function deliverOutboundPayloadsCore(
423423
overrides: sendOverrides,
424424
consumeReplyTo: applySendReplyToConsumption,
425425
});
426+
const sendMedia = deliveryHandler.sendFormattedMedia ?? deliveryHandler.sendMedia;
426427
for (const unit of mediaUnits) {
427428
if (unit.kind !== "media") {
428429
continue;
429430
}
430431
throwIfAborted(abortSignal);
431432
const resultIndex = results.length;
432-
const delivery = deliveryHandler.sendFormattedMedia
433-
? await deliveryHandler.sendFormattedMedia(
434-
unit.caption ?? "",
435-
unit.mediaUrl,
436-
withPreparedTarget(unit.overrides),
437-
)
438-
: await deliveryHandler.sendMedia(
439-
unit.caption ?? "",
440-
unit.mediaUrl,
441-
withPreparedTarget(unit.overrides),
442-
);
433+
const delivery = await sendMedia(
434+
unit.caption ?? "",
435+
unit.mediaUrl,
436+
withPreparedTarget(unit.overrides),
437+
);
443438
const recorded = await recordIdentifiedDeliveryResult(delivery);
444439
adoptSuccessfulResultsSince(resultIndex);
445440
if (recorded) {

‎src/infra/outbound/deliver.test.ts‎

Lines changed: 107 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3728,72 +3728,113 @@ describe("deliverOutboundPayloads", () => {
37283728
]);
37293729
});
37303730

3731-
it("uses adapter-provided formatted senders and scoped media roots when available", async () => {
3732-
const sendText = vi.fn(async ({ text }: { text: string }) => ({
3733-
channel: "line" as const,
3734-
messageId: `fallback:${text}`,
3735-
}));
3736-
const sendMedia = vi.fn(async ({ text }: { text: string }) => ({
3737-
channel: "line" as const,
3738-
messageId: `media:${text}`,
3739-
}));
3740-
const sendFormattedText = vi.fn(async ({ text }: { text: string }) => [
3741-
{ channel: "line" as const, messageId: `fmt:${text}:1` },
3742-
{ channel: "line" as const, messageId: `fmt:${text}:2` },
3743-
]);
3744-
const sendFormattedMedia = vi.fn(
3745-
async ({ text }: { text: string; mediaLocalRoots?: readonly string[] }) => ({
3731+
it.each([
3732+
{ ordinaryMedia: true, caption: "photo", mediaUrls: ["file:///tmp/f.png"] },
3733+
{ ordinaryMedia: false, caption: "photo", mediaUrls: ["file:///tmp/f.png"] },
3734+
{ ordinaryMedia: false, caption: "", mediaUrls: ["file:///tmp/f.png"] },
3735+
{
3736+
ordinaryMedia: false,
3737+
caption: "album",
3738+
mediaUrls: ["file:///tmp/f.png", "file:///tmp/g.png"],
3739+
},
3740+
])(
3741+
"uses formatted senders and scoped roots ($ordinaryMedia, $caption)",
3742+
async ({ ordinaryMedia, caption, mediaUrls }) => {
3743+
const sendText = vi.fn(async ({ text }: { text: string }) => ({
37463744
channel: "line" as const,
3747-
messageId: `fmt-media:${text}`,
3748-
}),
3749-
);
3750-
setTestOutbound({ sendText, sendMedia, sendFormattedText, sendFormattedMedia }, "line");
3751-
3752-
const textResults = await deliverOutboundPayloads({
3753-
cfg: { channels: { line: {} } } as OpenClawConfig,
3754-
channel: "line",
3755-
to: "U123",
3756-
accountId: "default",
3757-
payloads: [{ text: "hello **boss**" }],
3758-
});
3745+
messageId: `fallback:${text}`,
3746+
}));
3747+
const sendMedia = vi.fn(async ({ text }: { text: string }) => ({
3748+
channel: "line" as const,
3749+
messageId: `media:${text}`,
3750+
}));
3751+
const sendFormattedText = vi.fn(async ({ text }: { text: string }) => [
3752+
{ channel: "line" as const, messageId: `fmt:${text}:1` },
3753+
{ channel: "line" as const, messageId: `fmt:${text}:2` },
3754+
]);
3755+
const sendFormattedMedia = vi.fn(
3756+
async ({
3757+
text,
3758+
mediaUrl,
3759+
}: {
3760+
text: string;
3761+
mediaUrl: string;
3762+
mediaLocalRoots?: readonly string[];
3763+
}) => ({
3764+
channel: "line" as const,
3765+
messageId: `fmt-media:${mediaUrl}:${text}`,
3766+
}),
3767+
);
3768+
setTestOutbound(
3769+
{
3770+
sendText,
3771+
...(ordinaryMedia ? { sendMedia } : {}),
3772+
sendFormattedText,
3773+
sendFormattedMedia,
3774+
},
3775+
"line",
3776+
);
37593777

3760-
expect(sendFormattedText).toHaveBeenCalledTimes(1);
3761-
const formattedTextOptions = requireMockCallArg(sendFormattedText, "sendFormattedText") as
3762-
| { accountId?: unknown; text?: unknown; to?: unknown }
3763-
| undefined;
3764-
expect(formattedTextOptions?.to).toBe("U123");
3765-
expect(formattedTextOptions?.text).toBe("hello **boss**");
3766-
expect(formattedTextOptions?.accountId).toBe("default");
3767-
expect(sendText).not.toHaveBeenCalled();
3768-
expect(textResults.map((entry) => entry.messageId)).toEqual([
3769-
"fmt:hello **boss**:1",
3770-
"fmt:hello **boss**:2",
3771-
]);
3778+
const textResults = await deliverOutboundPayloads({
3779+
cfg: { channels: { line: {} } } as OpenClawConfig,
3780+
channel: "line",
3781+
to: "U123",
3782+
accountId: "default",
3783+
payloads: [{ text: "hello **boss**" }],
3784+
});
37723785

3773-
const cfg = { channels: { line: {} } } as OpenClawConfig;
3774-
await deliverOutboundPayloads({
3775-
cfg,
3776-
channel: "line",
3777-
to: "U123",
3778-
payloads: [{ text: "photo", mediaUrl: "file:///tmp/f.png" }],
3779-
session: { agentId: "work" },
3780-
});
3786+
expect(sendFormattedText).toHaveBeenCalledTimes(1);
3787+
const formattedTextOptions = requireMockCallArg(sendFormattedText, "sendFormattedText") as
3788+
| { accountId?: unknown; text?: unknown; to?: unknown }
3789+
| undefined;
3790+
expect(formattedTextOptions?.to).toBe("U123");
3791+
expect(formattedTextOptions?.text).toBe("hello **boss**");
3792+
expect(formattedTextOptions?.accountId).toBe("default");
3793+
expect(sendText).not.toHaveBeenCalled();
3794+
expect(textResults.map((entry) => entry.messageId)).toEqual([
3795+
"fmt:hello **boss**:1",
3796+
"fmt:hello **boss**:2",
3797+
]);
3798+
3799+
const cfg = { channels: { line: {} } } as OpenClawConfig;
3800+
const onDeliveredPayload = vi.fn();
3801+
const mediaResults = await deliverOutboundPayloads({
3802+
cfg,
3803+
channel: "line",
3804+
to: "U123",
3805+
payloads: [{ text: caption, mediaUrls }],
3806+
session: { agentId: "work" },
3807+
onDeliveredPayload,
3808+
});
37813809

3782-
expect(sendFormattedMedia).toHaveBeenCalledTimes(1);
3783-
const sendFormattedMediaCall = requireMockCallArg(sendFormattedMedia, "sendFormattedMedia") as
3784-
| { mediaLocalRoots?: string[]; mediaUrl?: unknown; text?: unknown; to?: unknown }
3785-
| undefined;
3786-
expect(sendFormattedMediaCall?.to).toBe("U123");
3787-
expect(sendFormattedMediaCall?.text).toBe("photo");
3788-
expect(sendFormattedMediaCall?.mediaUrl).toBe("file:///tmp/f.png");
3789-
expect(sendFormattedMediaCall?.mediaLocalRoots).toContain(expectedPreferredTmpRoot);
3790-
expect(
3791-
sendFormattedMediaCall?.mediaLocalRoots?.some((root) =>
3792-
root.endsWith(path.join(".openclaw", "workspace-work")),
3793-
),
3794-
).toBe(true);
3795-
expect(sendMedia).not.toHaveBeenCalled();
3796-
});
3810+
expect(sendFormattedMedia).toHaveBeenCalledTimes(mediaUrls.length);
3811+
const sendFormattedMediaCall = requireMockCallArg(
3812+
sendFormattedMedia,
3813+
"sendFormattedMedia",
3814+
) as
3815+
| { mediaLocalRoots?: string[]; mediaUrl?: unknown; text?: unknown; to?: unknown }
3816+
| undefined;
3817+
expect(sendFormattedMediaCall?.to).toBe("U123");
3818+
expect(sendFormattedMediaCall?.text).toBe(caption);
3819+
expect(sendFormattedMediaCall?.mediaUrl).toBe(mediaUrls[0]);
3820+
expect(sendFormattedMedia.mock.calls.map(([call]) => [call.text, call.mediaUrl])).toEqual(
3821+
mediaUrls.map((mediaUrl, index) => [index === 0 ? caption : "", mediaUrl]),
3822+
);
3823+
expect(mediaResults.map((entry) => entry.messageId)).toEqual(
3824+
mediaUrls.map((mediaUrl, index) => `fmt-media:${mediaUrl}:${index === 0 ? caption : ""}`),
3825+
);
3826+
expect(onDeliveredPayload).toHaveBeenCalledWith(
3827+
expect.objectContaining({ text: caption, mediaUrls }),
3828+
);
3829+
expect(sendFormattedMediaCall?.mediaLocalRoots).toContain(expectedPreferredTmpRoot);
3830+
expect(
3831+
sendFormattedMediaCall?.mediaLocalRoots?.some((root) =>
3832+
root.endsWith(path.join(".openclaw", "workspace-work")),
3833+
),
3834+
).toBe(true);
3835+
expect(sendMedia).not.toHaveBeenCalled();
3836+
},
3837+
);
37973838

37983839
it("persists formatted sub-send results before a later adapter chunk fails", async () => {
37993840
const firstResult = { channel: "line" as const, messageId: "fmt-1" };
@@ -6046,7 +6087,7 @@ describe("deliverOutboundPayloads", () => {
60466087
});
60476088
const warnCall = requireMockCall(logMocks.warn, "warn");
60486089
expect(warnCall[0]).toBe(
6049-
"Plugin outbound adapter does not implement sendMedia; media URLs will be dropped and text fallback will be used",
6090+
"Plugin outbound adapter does not implement sendMedia or sendFormattedMedia; media URLs will be dropped and text fallback will be used",
60506091
);
60516092
const warnContext = warnCall[1] as { channel?: unknown; mediaCount?: unknown } | undefined;
60526093
expect(warnContext?.channel).toBe("matrix");
@@ -6071,7 +6112,7 @@ describe("deliverOutboundPayloads", () => {
60716112
expect(requireMockCallArg(sendText, "sendText").text).toBe("caption");
60726113
const warnCall = requireMockCall(logMocks.warn, "warn");
60736114
expect(warnCall[0]).toBe(
6074-
"Plugin outbound adapter does not implement sendMedia; media URLs will be dropped and text fallback will be used",
6115+
"Plugin outbound adapter does not implement sendMedia or sendFormattedMedia; media URLs will be dropped and text fallback will be used",
60756116
);
60766117
const warnContext = warnCall[1] as { channel?: unknown; mediaCount?: unknown } | undefined;
60776118
expect(warnContext?.channel).toBe("matrix");
@@ -6091,13 +6132,13 @@ describe("deliverOutboundPayloads", () => {
60916132
payloads: [{ text: " ", mediaUrl: "https://example.com/file.png" }],
60926133
}),
60936134
).rejects.toThrow(
6094-
"Plugin outbound adapter does not implement sendMedia and no text fallback is available for media payload",
6135+
"Plugin outbound adapter does not implement sendMedia or sendFormattedMedia and no text fallback is available for media payload",
60956136
);
60966137

60976138
expect(sendText).not.toHaveBeenCalled();
60986139
const warnCall = requireMockCall(logMocks.warn, "warn");
60996140
expect(warnCall[0]).toBe(
6100-
"Plugin outbound adapter does not implement sendMedia; media URLs will be dropped and text fallback will be used",
6141+
"Plugin outbound adapter does not implement sendMedia or sendFormattedMedia; media URLs will be dropped and text fallback will be used",
61016142
);
61026143
const warnContext = warnCall[1] as { channel?: unknown; mediaCount?: unknown } | undefined;
61036144
expect(warnContext?.channel).toBe("matrix");

0 commit comments

Comments
 (0)