Skip to content

Commit ffdad4a

Browse files
committed
fix(media): normalize HEIC image sequence inputs
1 parent ccc10bf commit ffdad4a

5 files changed

Lines changed: 144 additions & 30 deletions

File tree

‎src/media-understanding/apply.test.ts‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,15 +1262,34 @@ describe("applyMediaUnderstanding", () => {
12621262
);
12631263
});
12641264

1265-
it("normalizes HEIC images before tools.media.image provider execution", async () => {
1266-
const imagePath = await createTempMediaFile({
1265+
it.each([
1266+
{
1267+
name: "HEIC",
1268+
fileName: "photo.heic",
1269+
mime: "image/heic",
1270+
bytes: Buffer.from("heic-source"),
1271+
},
1272+
{
1273+
name: "HEIC sequence",
12671274
fileName: "photo.heic",
1268-
content: "heic-source",
1275+
mime: "image/heic-sequence",
1276+
bytes: Buffer.from("000000186674797068657663000000000000000000000000", "hex"),
1277+
},
1278+
{
1279+
name: "HEIF sequence",
1280+
fileName: "photo.heif",
1281+
mime: "image/heif-sequence",
1282+
bytes: Buffer.from("00000018667479706d736631000000000000000000000000", "hex"),
1283+
},
1284+
])("normalizes $name images before tools.media.image provider execution", async (testCase) => {
1285+
const imagePath = await createTempMediaFile({
1286+
fileName: testCase.fileName,
1287+
content: testCase.bytes,
12691288
});
12701289
const describeImage = vi.fn(async () => ({ text: "normalized image" }));
12711290
const ctx: MsgContext = {
12721291
Body: "",
1273-
media: [{ path: imagePath, contentType: "image/heic" }],
1292+
media: [{ path: imagePath, contentType: testCase.mime }],
12741293
};
12751294
const cfg: OpenClawConfig = {
12761295
tools: {
@@ -1303,11 +1322,11 @@ describe("applyMediaUnderstanding", () => {
13031322
});
13041323

13051324
expect(result.appliedImage).toBe(true);
1306-
expect(mockedConvertHeicToJpeg).toHaveBeenCalledWith(Buffer.from("heic-source"));
1325+
expect(mockedConvertHeicToJpeg).toHaveBeenCalledWith(testCase.bytes);
13071326
expect(describeImage).toHaveBeenCalledWith(
13081327
expect.objectContaining({
13091328
buffer: Buffer.from("jpeg-normalized"),
1310-
fileName: "photo.heic",
1329+
fileName: testCase.fileName,
13111330
mime: "image/jpeg",
13121331
}),
13131332
);

‎src/media-understanding/image-input-normalize.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { normalizeMimeType } from "@openclaw/media-core/mime";
44
import { extractImageContentFromSource } from "../media/input-files.js";
55
import { DEFAULT_MAX_BYTES } from "./defaults.constants.js";
66

7-
const HEIC_MIME_RE = /^image\/hei[cf]$/i;
7+
const HEIC_MIME_RE = /^image\/hei[cf](?:-sequence)?$/i;
88
const HEIC_EXT_RE = /\.(heic|heif)$/i;
99

1010
function isHeicInput(params: { mime?: string; fileName?: string }): boolean {

‎src/media-understanding/runtime.test.ts‎

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -575,28 +575,47 @@ describe("media-understanding runtime", () => {
575575
);
576576
});
577577

578-
it("normalizes local HEIC explicit image descriptions before provider execution", async () => {
579-
mocks.readLocalFileSafely.mockResolvedValue({ buffer: Buffer.from("heic-source") });
580-
581-
await describeImageFileWithModel({
582-
filePath: "/tmp/sample.bin",
578+
it.each([
579+
{
580+
name: "HEIC",
583581
mime: "image/heic; charset=binary",
584-
provider: "zai",
585-
model: "glm-4.6v",
586-
prompt: "Describe it",
587-
cfg: {} as OpenClawConfig,
588-
agentDir: "/tmp/agent",
589-
});
590-
591-
expect(mocks.convertHeicToJpeg).toHaveBeenCalledWith(Buffer.from("heic-source"));
592-
expect(mocks.describeImageWithModel).toHaveBeenCalledWith(
593-
expect.objectContaining({
594-
buffer: Buffer.from("jpeg-normalized"),
595-
fileName: "sample.bin",
596-
mime: "image/jpeg",
597-
}),
598-
);
599-
});
582+
bytes: Buffer.from("heic-source"),
583+
},
584+
{
585+
name: "HEIC sequence",
586+
mime: "image/heic-sequence",
587+
bytes: Buffer.from("000000186674797068657663000000000000000000000000", "hex"),
588+
},
589+
{
590+
name: "HEIF sequence",
591+
mime: "image/heif-sequence",
592+
bytes: Buffer.from("00000018667479706d736631000000000000000000000000", "hex"),
593+
},
594+
])(
595+
"normalizes local $name explicit image descriptions before provider execution",
596+
async (testCase) => {
597+
mocks.readLocalFileSafely.mockResolvedValue({ buffer: testCase.bytes });
598+
599+
await describeImageFileWithModel({
600+
filePath: "/tmp/sample.bin",
601+
mime: testCase.mime,
602+
provider: "zai",
603+
model: "glm-4.6v",
604+
prompt: "Describe it",
605+
cfg: {} as OpenClawConfig,
606+
agentDir: "/tmp/agent",
607+
});
608+
609+
expect(mocks.convertHeicToJpeg).toHaveBeenCalledWith(testCase.bytes);
610+
expect(mocks.describeImageWithModel).toHaveBeenCalledWith(
611+
expect.objectContaining({
612+
buffer: Buffer.from("jpeg-normalized"),
613+
fileName: "sample.bin",
614+
mime: "image/jpeg",
615+
}),
616+
);
617+
},
618+
);
600619

601620
it("preserves fetched metadata for explicit model URL inputs", async () => {
602621
await describeImageFileWithModel({

‎src/media/input-files.fetch-guard.test.ts‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,55 @@ describe("HEIC input image normalization", () => {
199199
mimeType: "image/jpeg",
200200
},
201201
},
202+
{
203+
name: "converts sniffed HEIC sequence images using the existing HEIC allowlist",
204+
source: {
205+
type: "base64",
206+
data: Buffer.from("heic-sequence-source").toString("base64"),
207+
mediaType: "image/heic",
208+
} as const,
209+
limits: createImageSourceLimits(["image/heic", "image/jpeg"]),
210+
detectedMime: "image/heic-sequence",
211+
convertedBytes: Buffer.from("jpeg-heic-sequence"),
212+
expectedImage: {
213+
type: "image",
214+
data: Buffer.from("jpeg-heic-sequence").toString("base64"),
215+
mimeType: "image/jpeg",
216+
},
217+
},
218+
{
219+
name: "converts sniffed HEIF sequence images using the existing HEIF allowlist",
220+
source: {
221+
type: "base64",
222+
data: Buffer.from("heif-sequence-source").toString("base64"),
223+
mediaType: "image/heif",
224+
} as const,
225+
limits: createImageSourceLimits(["image/heif", "image/jpeg"]),
226+
detectedMime: "image/heif-sequence",
227+
convertedBytes: Buffer.from("jpeg-heif-sequence"),
228+
expectedImage: {
229+
type: "image",
230+
data: Buffer.from("jpeg-heif-sequence").toString("base64"),
231+
mimeType: "image/jpeg",
232+
},
233+
},
234+
{
235+
name: "converts fetched HEIC sequence images using the existing HEIC allowlist",
236+
source: {
237+
type: "url",
238+
url: "https://example.com/photo.heic",
239+
} as const,
240+
limits: createImageSourceLimits(["image/heic", "image/jpeg"], true),
241+
detectedMime: "image/heic-sequence",
242+
convertedBytes: Buffer.from("jpeg-url-sequence"),
243+
fetchedContentType: "image/heic-sequence",
244+
fetchedBody: Buffer.from("heic-url-sequence"),
245+
expectedImage: {
246+
type: "image",
247+
data: Buffer.from("jpeg-url-sequence").toString("base64"),
248+
mimeType: "image/jpeg",
249+
},
250+
},
202251
{
203252
name: "keeps declared MIME for non-HEIC images after validation",
204253
source: {
@@ -273,6 +322,7 @@ describe("HEIC input image normalization", () => {
273322
mediaType: "image/png",
274323
},
275324
limits: createImageSourceLimits(["image/png", "image/jpeg"]),
325+
detectedMime: "application/pdf",
276326
expectedError: "Unsupported image MIME type: application/pdf",
277327
},
278328
{
@@ -282,13 +332,36 @@ describe("HEIC input image normalization", () => {
282332
url: "https://example.com/photo.png",
283333
},
284334
limits: createImageSourceLimits(["image/png", "image/jpeg"], true),
335+
detectedMime: "application/pdf",
285336
expectedError: "Unsupported image MIME type: application/pdf",
286337
fetchedUrl: "https://example.com/photo.png",
287338
fetchedContentType: "image/png",
288339
fetchedBody: Buffer.from("%PDF-1.4\n"),
289340
},
341+
{
342+
name: "rejects sequence images when their canonical HEIC MIME is not allowed",
343+
source: {
344+
type: "base64" as const,
345+
data: Buffer.from("heic-sequence-source").toString("base64"),
346+
mediaType: "image/heic-sequence",
347+
},
348+
limits: createImageSourceLimits(["image/png", "image/jpeg"]),
349+
detectedMime: "image/heic-sequence",
350+
expectedError: "Unsupported image MIME type",
351+
},
352+
{
353+
name: "rejects spoofed HEIC sequence metadata when detected bytes are not an image",
354+
source: {
355+
type: "base64" as const,
356+
data: Buffer.from("%PDF-1.4\n").toString("base64"),
357+
mediaType: "image/heic-sequence",
358+
},
359+
limits: createImageSourceLimits(["image/heic", "image/jpeg"]),
360+
detectedMime: "application/pdf",
361+
expectedError: "Unsupported image MIME type: application/pdf",
362+
},
290363
] as const)("$name", async (testCase) => {
291-
detectMimeMock.mockResolvedValueOnce("application/pdf");
364+
detectMimeMock.mockResolvedValueOnce(testCase.detectedMime);
292365
await expectRejectedImageMimeCase(testCase);
293366
expect(convertHeicToJpegMock).not.toHaveBeenCalled();
294367
});

‎src/media/input-files.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ async function normalizeInputImage(params: {
303303
if (declaredMime.startsWith("image/") && detectedMime && !detectedMime.startsWith("image/")) {
304304
throw new Error(`Unsupported image MIME type: ${detectedMime}`);
305305
}
306-
const sourceMime = detectedMime?.startsWith("image/") ? detectedMime : declaredMime;
306+
const sourceMime = (detectedMime?.startsWith("image/") ? detectedMime : declaredMime).replace(
307+
/^(image\/hei[cf])-sequence$/,
308+
"$1",
309+
);
307310
if (!params.limits.allowedMimes.has(sourceMime)) {
308311
throw new Error(`Unsupported image MIME type: ${sourceMime}`);
309312
}

0 commit comments

Comments
 (0)