Skip to content

Commit 388921b

Browse files
harjothkharaclaude
andcommitted
fix(gateway): resolve canonical media params on message.action dispatch
Gateway `message.action` sends never reach the outbound runner's send payload build, which is where the canonical media alias chain (`media` -> `mediaUrl`/`path`/`filePath`/`fileUrl`/`image` -> `mediaUrls` -> `attachments[]`) is resolved. A caller-supplied attachment therefore arrived at the channel adapter with no `media` set and was silently dropped, reporting a successful text-only send. Resolve the same chain on the gateway path, and give both paths a single collector in `send-media-sources.ts` so their precedence cannot drift: `buildSendPayloadParts` now consumes it and its duplicate `collectMessageAttachmentMediaHints` is deleted. Feishu additionally rejects the two media sources core never resolves (`file`, `attachment`), multi-media sends it cannot deliver, and media on `thread-reply`, which no dispatch path resolves — each of which previously delivered text only. Co-authored-by: Claude <[email protected]>
1 parent ba09e05 commit 388921b

8 files changed

Lines changed: 440 additions & 63 deletions

File tree

‎docs/channels/feishu.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,10 @@ resource payload.
712712
- ✅ Interactive cards (including streaming updates)
713713
- ⚠️ Rich text (post-style formatting; doesn't support full Feishu/Lark authoring capabilities)
714714

715+
Feishu delivers one media item per message. A send declaring more than one
716+
attachment is rejected rather than silently delivering only the first, and
717+
`thread-reply` rejects media outright because it delivers text only.
718+
715719
Native Feishu/Lark audio bubbles use the Feishu `audio` message type and require
716720
Ogg/Opus upload media (`file_type: "opus"`). Existing `.opus` and `.ogg` media
717721
is sent directly as native audio. MP3/WAV/M4A and other likely audio formats are

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

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ describe("feishuPlugin actions", () => {
960960
]);
961961
});
962962

963-
it("sends media through the outbound adapter", async () => {
963+
it("sends core-normalized media through the outbound adapter", async () => {
964964
feishuOutboundSendMediaMock.mockResolvedValueOnce({
965965
channel: "feishu",
966966
messageId: "om_media",
@@ -973,6 +973,9 @@ describe("feishuPlugin actions", () => {
973973
to: "chat:oc_group_1",
974974
message: "test",
975975
media: "/tmp/image.png",
976+
mediaUrl: "/tmp/image.png",
977+
mediaUrls: ["/tmp/image.png"],
978+
attachments: [{ media: "/tmp/image.png" }],
976979
},
977980
cfg,
978981
accountId: undefined,
@@ -992,6 +995,159 @@ describe("feishuPlugin actions", () => {
992995
expect(resultDetails(result).messageId).toBe("om_media");
993996
});
994997

998+
it.each([
999+
["attachment", { path: "/tmp/script.py" }],
1000+
["file", "/tmp/script.py"],
1001+
] as const)("rejects unsupported Feishu attachment param %s", async (key, value) => {
1002+
await expect(
1003+
feishuPlugin.actions?.handleAction?.({
1004+
action: "send",
1005+
params: {
1006+
to: "chat:oc_group_1",
1007+
message: "see attached",
1008+
[key]: value,
1009+
},
1010+
cfg,
1011+
accountId: undefined,
1012+
toolContext: {},
1013+
mediaLocalRoots: ["/tmp"],
1014+
} as never),
1015+
).rejects.toThrow(
1016+
`Feishu send supports media attachments through the media parameter; ${key} is not supported.`,
1017+
);
1018+
1019+
expect(sendMessageFeishuMock).not.toHaveBeenCalled();
1020+
expect(sendCardFeishuMock).not.toHaveBeenCalled();
1021+
expect(feishuOutboundSendMediaMock).not.toHaveBeenCalled();
1022+
});
1023+
1024+
// Core resolves its own alias chain into `media` before dispatch, so Feishu must not
1025+
// reject the aliases themselves; doing so regressed previously working sends.
1026+
it.each([
1027+
["path", "/tmp/script.py"],
1028+
["filePath", "/tmp/script.py"],
1029+
["fileUrl", "file:///tmp/script.py"],
1030+
["image", "/tmp/script.py"],
1031+
["mediaUrl", "/tmp/script.py"],
1032+
] as const)(
1033+
"accepts core-canonical media alias %s alongside normalized media",
1034+
async (key, value) => {
1035+
feishuOutboundSendMediaMock.mockResolvedValueOnce({
1036+
channel: "feishu",
1037+
messageId: "om_media",
1038+
chatId: "oc_group_1",
1039+
});
1040+
1041+
await feishuPlugin.actions?.handleAction?.({
1042+
action: "send",
1043+
params: {
1044+
to: "chat:oc_group_1",
1045+
message: "see attached",
1046+
media: "/tmp/script.py",
1047+
[key]: value,
1048+
},
1049+
cfg,
1050+
accountId: undefined,
1051+
toolContext: {},
1052+
mediaLocalRoots: ["/tmp"],
1053+
} as never);
1054+
1055+
expect(feishuOutboundSendMediaMock).toHaveBeenCalledOnce();
1056+
expect(sendMessageFeishuMock).not.toHaveBeenCalled();
1057+
},
1058+
);
1059+
1060+
it("rejects multiple media attachments rather than dropping all but the first", async () => {
1061+
await expect(
1062+
feishuPlugin.actions?.handleAction?.({
1063+
action: "send",
1064+
params: {
1065+
to: "chat:oc_group_1",
1066+
message: "see attached",
1067+
media: "/tmp/first.py",
1068+
mediaUrls: ["/tmp/first.py", "/tmp/second.py"],
1069+
},
1070+
cfg,
1071+
accountId: undefined,
1072+
toolContext: {},
1073+
mediaLocalRoots: ["/tmp"],
1074+
} as never),
1075+
).rejects.toThrow("Feishu send supports a single media attachment.");
1076+
1077+
expect(feishuOutboundSendMediaMock).not.toHaveBeenCalled();
1078+
});
1079+
1080+
it("rejects media on thread replies instead of delivering text only", async () => {
1081+
await expect(
1082+
feishuPlugin.actions?.handleAction?.({
1083+
action: "thread-reply",
1084+
params: {
1085+
to: "chat:oc_group_1",
1086+
messageId: "om_parent",
1087+
message: "see attached",
1088+
attachments: [{ media: "/tmp/script.py" }],
1089+
},
1090+
cfg,
1091+
accountId: undefined,
1092+
toolContext: {},
1093+
mediaLocalRoots: ["/tmp"],
1094+
} as never),
1095+
).rejects.toThrow(
1096+
"Feishu thread-reply does not support media attachments; attachments is not supported.",
1097+
);
1098+
1099+
expect(sendMessageFeishuMock).not.toHaveBeenCalled();
1100+
expect(feishuOutboundSendMediaMock).not.toHaveBeenCalled();
1101+
});
1102+
1103+
it("ignores empty optional attachment defaults", async () => {
1104+
sendMessageFeishuMock.mockResolvedValueOnce({ messageId: "om_sent", chatId: "oc_group_1" });
1105+
1106+
await feishuPlugin.actions?.handleAction?.({
1107+
action: "send",
1108+
params: {
1109+
to: "chat:oc_group_1",
1110+
message: "hello",
1111+
attachment: {},
1112+
attachments: [],
1113+
buffer: "",
1114+
file: null,
1115+
filename: "",
1116+
mediaUrls: [],
1117+
},
1118+
cfg,
1119+
accountId: undefined,
1120+
toolContext: {},
1121+
} as never);
1122+
1123+
expect(sendMessageFeishuMock).toHaveBeenCalledOnce();
1124+
expect(feishuOutboundSendMediaMock).not.toHaveBeenCalled();
1125+
});
1126+
1127+
it.each([
1128+
["blank string", " "],
1129+
["object", { path: "/tmp/script.py" }],
1130+
] as const)("rejects invalid Feishu media param %s", async (_name, media) => {
1131+
await expect(
1132+
feishuPlugin.actions?.handleAction?.({
1133+
action: "send",
1134+
params: {
1135+
to: "chat:oc_group_1",
1136+
message: "see attached",
1137+
media,
1138+
},
1139+
cfg,
1140+
accountId: undefined,
1141+
toolContext: {},
1142+
mediaLocalRoots: ["/tmp"],
1143+
} as never),
1144+
).rejects.toThrow("Feishu send media must be a non-empty string.");
1145+
1146+
expect(sendMessageFeishuMock).not.toHaveBeenCalled();
1147+
expect(sendCardFeishuMock).not.toHaveBeenCalled();
1148+
expect(feishuOutboundSendMediaMock).not.toHaveBeenCalled();
1149+
});
1150+
9951151
it("passes asVoice through media sends", async () => {
9961152
feishuOutboundSendMediaMock.mockResolvedValueOnce({
9971153
channel: "feishu",

‎extensions/feishu/src/channel.ts‎

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,92 @@ import { feishuSetupWizard, runFeishuLogin } from "./setup-surface.js";
112112
import { looksLikeFeishuId, normalizeFeishuTarget } from "./targets.js";
113113
import type { FeishuConfig, FeishuProbeResult, ResolvedFeishuAccount } from "./types.js";
114114

115-
function readFeishuMediaParam(params: Record<string, unknown>): string | undefined {
116-
const media = params.media;
117-
if (typeof media !== "string") {
115+
/**
116+
* Media sources core never resolves into `media`. Core folds its own aliases (`media`,
117+
* `path`, `filePath`, `mediaUrl`, `fileUrl`, `image`, `mediaUrls`, `attachments[]`, and the
118+
* snake_case spelling of each) into `media` before dispatch, so those arrive here already
119+
* resolved. These two have no such handling and would be dropped without telling the caller.
120+
*/
121+
const FEISHU_UNRECOGNIZED_MEDIA_SOURCE_PARAM_NAMES = ["attachment", "file"] as const;
122+
123+
/**
124+
* Every spelling a caller can use to declare media. Core resolves the recognized ones into
125+
* `media` for `send`, but no dispatch path does so for `thread-reply`, so a thread reply
126+
* carrying any of these would deliver text only.
127+
*/
128+
const FEISHU_MEDIA_SOURCE_PARAM_NAMES = [
129+
...FEISHU_UNRECOGNIZED_MEDIA_SOURCE_PARAM_NAMES,
130+
"attachments",
131+
"file_path",
132+
"file_url",
133+
"filePath",
134+
"fileUrl",
135+
"image",
136+
"media",
137+
"media_url",
138+
"media_urls",
139+
"mediaUrl",
140+
"mediaUrls",
141+
"path",
142+
] as const;
143+
144+
function hasFeishuAttachmentIntent(
145+
params: Readonly<Record<string, unknown>>,
146+
key: string,
147+
): boolean {
148+
if (!Object.hasOwn(params, key)) {
149+
return false;
150+
}
151+
const value = params[key];
152+
if (typeof value === "string") {
153+
return Boolean(value.trim());
154+
}
155+
if (Array.isArray(value)) {
156+
return value.length > 0;
157+
}
158+
if (value && typeof value === "object") {
159+
return Object.keys(value).length > 0;
160+
}
161+
return Boolean(value);
162+
}
163+
164+
function readFeishuMediaParam(
165+
params: Readonly<Record<string, unknown>>,
166+
action: "send" | "thread-reply",
167+
): string | undefined {
168+
if (action === "thread-reply") {
169+
const declaredMedia = FEISHU_MEDIA_SOURCE_PARAM_NAMES.find((key) =>
170+
hasFeishuAttachmentIntent(params, key),
171+
);
172+
if (declaredMedia) {
173+
throw new Error(
174+
`Feishu thread-reply does not support media attachments; ${declaredMedia} is not supported.`,
175+
);
176+
}
118177
return undefined;
119178
}
120-
return media.trim() ? media : undefined;
179+
const unsupportedParam = FEISHU_UNRECOGNIZED_MEDIA_SOURCE_PARAM_NAMES.find((key) =>
180+
hasFeishuAttachmentIntent(params, key),
181+
);
182+
if (unsupportedParam) {
183+
throw new Error(
184+
`Feishu send supports media attachments through the media parameter; ${unsupportedParam} is not supported.`,
185+
);
186+
}
187+
const media = params.media;
188+
const normalizedMedia = typeof media === "string" && media.trim() ? media : undefined;
189+
// Feishu delivers a single media item per message; a second one would be dropped silently.
190+
const mediaUrls = params.mediaUrls;
191+
if (Array.isArray(mediaUrls) && mediaUrls.length > 1) {
192+
throw new Error("Feishu send supports a single media attachment.");
193+
}
194+
if (media !== undefined) {
195+
if (normalizedMedia) {
196+
return normalizedMedia;
197+
}
198+
throw new Error("Feishu send media must be a non-empty string.");
199+
}
200+
return undefined;
121201
}
122202

123203
function readBooleanParam(params: Record<string, unknown>, keys: string[]): boolean | undefined {
@@ -1084,7 +1164,7 @@ export const feishuPlugin: ChannelPlugin<ResolvedFeishuAccount, FeishuProbeResul
10841164
const presentation =
10851165
normalizeMessagePresentation(ctx.params.presentation) ??
10861166
(interactive ? legacyInteractiveReplyToPresentation(interactive) : undefined);
1087-
const mediaUrl = readFeishuMediaParam(ctx.params);
1167+
const mediaUrl = readFeishuMediaParam(ctx.params, ctx.action);
10881168
const audioAsVoice = readBooleanParam(ctx.params, ["asVoice", "audioAsVoice"]);
10891169
if (textCard && !presentation) {
10901170
assertFeishuCardWithinEnvelope(textCard, "Feishu native card");

‎src/gateway/server-methods/send.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,60 @@ describe("gateway send mirroring", () => {
20052005
);
20062006
});
20072007

2008+
// Gateway dispatch does not run the outbound runner's send payload build, so without an
2009+
// explicit resolve step a canonical attachment reaches the adapter with no `media` set and
2010+
// is silently dropped as a text-only send. Regression guard for that resolve call.
2011+
it("resolves canonical attachments into media before dispatching message action sends", async () => {
2012+
const sendPlugin: ChannelPlugin = {
2013+
id: "whatsapp",
2014+
meta: {
2015+
id: "whatsapp",
2016+
label: "WhatsApp",
2017+
selectionLabel: "WhatsApp",
2018+
docsPath: "/channels/whatsapp",
2019+
blurb: "WhatsApp send dispatch test plugin.",
2020+
},
2021+
capabilities: { chatTypes: ["direct"], media: true },
2022+
config: {
2023+
listAccountIds: () => ["default"],
2024+
resolveAccount: () => ({ enabled: true }),
2025+
isConfigured: () => true,
2026+
},
2027+
actions: {
2028+
describeMessageTool: () => ({ actions: ["send"] }),
2029+
supportsAction: ({ action }) => action === "send",
2030+
handleAction: async () => jsonResult({ ok: true }),
2031+
},
2032+
};
2033+
setActivePluginRegistry(
2034+
createTestRegistry([{ pluginId: "whatsapp", source: "test", plugin: sendPlugin }]),
2035+
"send-test-attachment-resolve",
2036+
);
2037+
2038+
await runMessageActionRequest({
2039+
channel: "whatsapp",
2040+
action: "send",
2041+
idempotencyKey: "message-action-attachment-resolve",
2042+
params: {
2043+
target: "user:15551234567",
2044+
message: "see attached",
2045+
attachments: [{ media: "/tmp/script.py" }],
2046+
},
2047+
requesterAccountId: "default",
2048+
sessionKey: "agent:main:whatsapp:direct:15551234567",
2049+
agentId: "main",
2050+
});
2051+
2052+
expect(mocks.dispatchChannelMessageAction).toHaveBeenCalledWith(
2053+
expect.objectContaining({
2054+
params: expect.objectContaining({
2055+
media: "/tmp/script.py",
2056+
mediaUrls: ["/tmp/script.py"],
2057+
}),
2058+
}),
2059+
);
2060+
});
2061+
20082062
it("strips current-turn context from unauthenticated message action callers", async () => {
20092063
mocks.getChannelPlugin.mockReturnValue({
20102064
actions: {

‎src/gateway/server-methods/send.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
createOutboundPayloadPlan,
4141
projectOutboundPayloadPlanForMirror,
4242
} from "../../infra/outbound/payloads.js";
43+
import { normalizeSendMediaParams } from "../../infra/outbound/send-media-sources.js";
4344
import { buildOutboundSessionContext } from "../../infra/outbound/session-context.js";
4445
import {
4546
beginTerminalSourceReplyDelivery,
@@ -552,6 +553,9 @@ export const sendHandlers: GatewayRequestHandlers = {
552553
mediaLocalRoots: getAgentScopedMediaLocalRoots(cfg, agentId),
553554
}),
554555
});
556+
// Gateway dispatch does not run the outbound runner's send payload build, so
557+
// resolve the canonical media alias chain here or channels never see the media.
558+
normalizeSendMediaParams(request.params);
555559
}
556560
const sourceReplyMirror = {
557561
action: request.action,

0 commit comments

Comments
 (0)