Skip to content

Commit c5d1cb3

Browse files
authored
fix(channels): attachment filenames disappear from model context (#129140)
* fix(channels): preserve inbound attachment filenames Fixes #128956 * test(discord): verify names on successfully downloaded media * test(discord): verify referenced attachment filenames * fix(telegram): preserve accepted resolved-media shapes
1 parent f5cd35f commit c5d1cb3

24 files changed

Lines changed: 170 additions & 39 deletions

‎extensions/discord/src/monitor/message-handler.preflight.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ describe("preflightDiscordMessage", () => {
645645
{
646646
path: "/tmp/openclaw-discord-test/photo.png",
647647
contentType: "image/png",
648+
fileName: "photo.png",
648649
},
649650
]);
650651
});

‎extensions/discord/src/monitor/message-media.references.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ describe("resolveReferencedReplyMediaList", () => {
7878
512,
7979
);
8080

81-
expect(result).toEqual([{ path: "/tmp/reply-image.png", contentType: "image/png" }]);
81+
expect(result).toEqual([
82+
{ path: "/tmp/reply-image.png", contentType: "image/png", fileName: "reply-image.png" },
83+
]);
8284
expect(readRemoteMediaBuffer).toHaveBeenCalledWith(
8385
expect.objectContaining({
8486
url: attachment.url,

‎extensions/discord/src/monitor/message-media.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ function expectSinglePngDownload(params: {
154154
{
155155
path: params.expectedPath,
156156
contentType: "image/png",
157+
fileName: params.filePathHint,
157158
...(params.kind ? { kind: params.kind } : {}),
158159
},
159160
]);
@@ -415,6 +416,7 @@ describe("resolveMediaList", () => {
415416
{
416417
path: "/tmp/voice.ogg",
417418
contentType: undefined,
419+
fileName: "voice.ogg",
418420
kind: "audio",
419421
},
420422
]);
@@ -464,6 +466,7 @@ describe("resolveMediaList", () => {
464466
{
465467
path: "/tmp/image.png",
466468
contentType: "image/png",
469+
fileName: "image.ogg",
467470
},
468471
]);
469472
});
@@ -480,6 +483,7 @@ describe("resolveMediaList", () => {
480483
{
481484
path: "/tmp/voice",
482485
contentType: "audio/ogg",
486+
fileName: "voice",
483487
kind: "audio",
484488
},
485489
]);
@@ -515,6 +519,7 @@ describe("resolveMediaList", () => {
515519
{
516520
path: "/tmp/image.png",
517521
contentType: "image/png",
522+
fileName: "voice.ogg",
518523
},
519524
]);
520525
});
@@ -573,6 +578,7 @@ describe("resolveMediaList", () => {
573578
{
574579
path: "/tmp/good.png",
575580
contentType: "image/png",
581+
fileName: "good.png",
576582
},
577583
{
578584
contentType: "application/pdf",

‎extensions/discord/src/monitor/message-media.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { StickerFormatType, type APIAttachment, type APIStickerItem } from "discord-api-types/v10";
33
import {
44
formatMediaPlaceholderText,
5+
type ChannelInboundMediaInput,
56
type MediaPlaceholderTextFact,
67
} from "openclaw/plugin-sdk/channel-inbound";
78
import { getFileExtension, normalizeMimeType } from "openclaw/plugin-sdk/media-mime";
@@ -36,7 +37,10 @@ const AUDIO_ATTACHMENT_EXTENSIONS = new Set([
3637

3738
const DISCORD_STICKER_ASSET_BASE_URL = "https://media.discordapp.net/stickers";
3839

39-
export type DiscordMediaInfo = Pick<MediaPlaceholderTextFact, "contentType" | "kind" | "path">;
40+
export type DiscordMediaInfo = Pick<
41+
ChannelInboundMediaInput,
42+
"contentType" | "fileName" | "kind" | "path"
43+
>;
4044

4145
type DiscordMediaResolveOptions = {
4246
fetchImpl?: FetchLike;
@@ -350,6 +354,7 @@ async function appendResolvedMediaFromAttachments(params: {
350354
});
351355
params.out.push({
352356
path: saved.path,
357+
fileName: attachment.filename,
353358
...classification,
354359
});
355360
} catch (err) {
@@ -455,6 +460,7 @@ async function appendResolvedMediaFromStickers(params: {
455460
params.out.push({
456461
path: saved.path,
457462
contentType: saved.contentType,
463+
fileName: candidate.fileName,
458464
kind: "sticker",
459465
});
460466
lastError = null;

‎extensions/mattermost/src/mattermost/monitor-resources.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ describe("mattermost monitor resources", () => {
9393
const saveRemoteMedia = vi.fn(async () => ({
9494
path: "/tmp/file.png",
9595
contentType: "image/png",
96+
fileName: "original screenshot.png",
9697
}));
9798

9899
const resources = createMattermostMonitorResources({
@@ -113,6 +114,7 @@ describe("mattermost monitor resources", () => {
113114
{
114115
path: "/tmp/file.png",
115116
contentType: "image/png",
117+
fileName: "original screenshot.png",
116118
kind: "image",
117119
},
118120
]);
@@ -166,7 +168,7 @@ describe("mattermost monitor resources", () => {
166168
.mockRejectedValueOnce(new Error("download failed"));
167169
const request = vi.fn(async (requestPath: string) => {
168170
expect(requestPath).toBe("/files/file-audio/info");
169-
return { mime_type: "audio/mpeg" };
171+
return { mime_type: "audio/mpeg", name: "private-unavailable-recording.mp3" };
170172
});
171173
const resources = createMattermostMonitorResources({
172174
accountId: "default",

‎extensions/mattermost/src/mattermost/monitor-resources.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import {
44
formatInboundMediaUnavailableText,
55
formatMediaPlaceholderText,
66
toInboundMediaFactsWithMetadata,
7+
type ChannelInboundMediaInput,
78
type ChannelInboundMediaPayload,
89
type InboundMediaFacts,
910
type MediaPlaceholderTextFact,
1011
} from "openclaw/plugin-sdk/channel-inbound";
1112
import { pruneMapToMaxSize } from "openclaw/plugin-sdk/collection-runtime";
12-
import type { MediaKind } from "openclaw/plugin-sdk/media-runtime";
13+
import type { MediaKind, SavedRemoteMedia } from "openclaw/plugin-sdk/media-runtime";
1314
import {
1415
asDateTimestampMs,
1516
resolveExpiresAtMsFromDurationMs,
@@ -27,7 +28,9 @@ import {
2728
} from "./client.js";
2829
import { buildButtonProps, type MattermostInteractionResponse } from "./interactions.js";
2930

30-
type MattermostMediaInfo = Omit<MediaPlaceholderTextFact, "kind" | "url"> & { kind: MediaKind };
31+
type MattermostMediaInfo = Pick<ChannelInboundMediaInput, "contentType" | "fileName" | "path"> & {
32+
kind: MediaKind;
33+
};
3134

3235
export async function buildMattermostInboundMediaPayload(
3336
media: readonly MattermostMediaInfo[],
@@ -76,7 +79,7 @@ type SaveRemoteMedia = (params: {
7679
ssrfPolicy?: { allowedHostnames?: string[] };
7780
responseHeaderTimeoutMs?: number;
7881
readIdleTimeoutMs?: number;
79-
}) => Promise<{ path: string; contentType?: string | null }>;
82+
}) => Promise<Pick<SavedRemoteMedia, "contentType" | "fileName" | "path">>;
8083

8184
export function createMattermostMonitorResources(params: {
8285
accountId: string;
@@ -171,6 +174,7 @@ export function createMattermostMonitorResources(params: {
171174
out.push({
172175
path: saved.path,
173176
contentType,
177+
...(saved.fileName ? { fileName: saved.fileName } : {}),
174178
kind: mediaKindFromMime(contentType) ?? "unknown",
175179
});
176180
} catch (err) {

‎extensions/slack/src/monitor/media-types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export type SlackMediaResult = {
33
path: string;
44
contentType?: string;
5+
fileName?: string;
56
placeholder: string;
67
};
78

‎extensions/slack/src/monitor/media.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ describe("resolveSlackMedia", () => {
293293
});
294294
mockFetch.mockResolvedValueOnce(mockResponse);
295295

296-
await resolveSlackMedia({
296+
const result = await resolveSlackMedia({
297297
files: [
298298
{
299299
url_private: "https://files.slack.com/private.jpg",
@@ -306,6 +306,7 @@ describe("resolveSlackMedia", () => {
306306
});
307307

308308
expectFetchCalledWithUrl(mockFetch, "https://files.slack.com/download.jpg");
309+
expect(expectSlackMediaResult(result)[0]?.fileName).toBe("test.jpg");
309310
});
310311

311312
it("preserves Authorization on same-origin redirects for private downloads", async () => {
@@ -937,8 +938,10 @@ describe("resolveSlackMedia", () => {
937938
const first = expectDefined(media[0], "first Slack media result");
938939
const second = expectDefined(media[1], "second Slack media result");
939940
expect(first.path).toBe("/tmp/a.jpg");
941+
expect(first.fileName).toBe("a.jpg");
940942
expect(first.placeholder).toBe("[Slack file: a.jpg (image/jpeg, 12 bytes, fileId: FA)]");
941943
expect(second.path).toBe("/tmp/b.png");
944+
expect(second.fileName).toBe("b.png");
942945
expect(second.placeholder).toBe("[Slack file: b.png (image/png, 34 bytes, fileId: FB)]");
943946
});
944947

@@ -1475,6 +1478,7 @@ describe("resolveSlackAttachmentContent", () => {
14751478
{
14761479
path: "/tmp/forwarded.jpg",
14771480
contentType: "image/jpeg",
1481+
fileName: "forwarded.jpg",
14781482
placeholder: "[Forwarded image: forwarded.jpg]",
14791483
},
14801484
],

‎extensions/slack/src/monitor/media.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ async function downloadSlackMediaFile(params: {
291291
return {
292292
path: saved.path,
293293
...(contentType ? { contentType } : {}),
294+
...(label ? { fileName: label } : {}),
294295
placeholder: `[Slack file: ${formatSlackFileReference({ ...params.file, name: label })}]`,
295296
};
296297
}
@@ -508,6 +509,7 @@ export async function resolveSlackAttachmentContent(params: {
508509
attachmentMedia.push({
509510
path: saved.path,
510511
contentType: saved.contentType,
512+
...(saved.fileName ? { fileName: saved.fileName } : {}),
511513
placeholder: `[Forwarded image: ${label}]`,
512514
});
513515
} catch {

‎extensions/telegram/src/bot-handlers.inbound-media.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ export function createTelegramInboundMedia({
364364
allMedia.push({
365365
path: media.path,
366366
contentType: media.contentType,
367+
...(media.fileName ? { fileName: media.fileName } : {}),
367368
kind: media.kind,
368369
stickerMetadata: media.stickerMetadata,
369370
sourceMessageId,

0 commit comments

Comments
 (0)