Skip to content

Commit ecc1bbd

Browse files
marchhaosteipete
authored andcommitted
fix: hydrate current turn image attachments
1 parent d967760 commit ecc1bbd

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎src/auto-reply/reply/current-turn-images.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ImageContent } from "@earendil-works/pi-ai";
22
import type { OpenClawConfig } from "../../config/types.openclaw.js";
33
import { logVerbose } from "../../globals.js";
44
import { formatErrorMessage } from "../../infra/errors.js";
5+
import { mimeTypeFromFilePath } from "../../media/mime.js";
56
import type { PromptImageOrderEntry } from "../../media/prompt-image-order.js";
67
import { normalizeOptionalString } from "../../shared/string-coerce.js";
78
import type { MsgContext } from "../templating.js";
@@ -13,6 +14,30 @@ type CurrentImageAttachment = {
1314
mediaType: string;
1415
};
1516

17+
function isGenericMediaType(mediaType: string | undefined): boolean {
18+
if (!mediaType) {
19+
return true;
20+
}
21+
const normalized = mediaType.split(";")[0]?.trim().toLowerCase();
22+
return normalized === "application/octet-stream" || normalized === "binary/octet-stream";
23+
}
24+
25+
function resolveCurrentImageMediaType(pathValue: unknown, mediaType?: unknown): string | undefined {
26+
const mediaPath = normalizeOptionalString(pathValue);
27+
if (!mediaPath) {
28+
return undefined;
29+
}
30+
const normalizedMediaType = normalizeOptionalString(mediaType);
31+
if (normalizedMediaType?.startsWith("image/")) {
32+
return normalizedMediaType;
33+
}
34+
if (!isGenericMediaType(normalizedMediaType)) {
35+
return undefined;
36+
}
37+
const inferredType = mimeTypeFromFilePath(mediaPath);
38+
return inferredType?.startsWith("image/") ? inferredType : undefined;
39+
}
40+
1641
function collectCurrentImageAttachments(ctx: MsgContext): CurrentImageAttachment[] {
1742
const pathsFromArray = Array.isArray(ctx.MediaPaths) ? ctx.MediaPaths : undefined;
1843
const paths =
@@ -31,8 +56,8 @@ function collectCurrentImageAttachments(ctx: MsgContext): CurrentImageAttachment
3156
const attachments: CurrentImageAttachment[] = [];
3257
for (const [index, pathValue] of paths.entries()) {
3358
const mediaPath = normalizeOptionalString(pathValue);
34-
const mediaType = normalizeOptionalString(types?.[index] ?? ctx.MediaType);
35-
if (mediaPath && mediaType?.startsWith("image/")) {
59+
const mediaType = resolveCurrentImageMediaType(pathValue, types?.[index] ?? ctx.MediaType);
60+
if (mediaPath && mediaType) {
3661
attachments.push({ index, path: mediaPath, mediaType });
3762
}
3863
}

‎src/auto-reply/reply/get-reply-run.media-only.test.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ describe("runPreparedReply media-only handling", () => {
878878
expect(call?.followupRun.prompt).toContain("[User sent media without caption]");
879879
});
880880

881-
it("hydrates current MediaPaths into queued followup images", async () => {
881+
it("hydrates current image MediaPaths by extension when MediaTypes are missing", async () => {
882882
const tmpDir = await mkdtemp(path.join(os.tmpdir(), "openclaw-followup-image-"));
883883
cleanupPaths.push(tmpDir);
884884
const imagePath = path.join(tmpDir, "inbound.png");
@@ -897,7 +897,6 @@ describe("runPreparedReply media-only handling", () => {
897897
RawBody: "describe this",
898898
CommandBody: "describe this",
899899
MediaPaths: [imagePath],
900-
MediaTypes: ["image/png"],
901900
MediaWorkspaceDir: tmpDir,
902901
OriginatingChannel: "discord",
903902
OriginatingTo: "C123",
@@ -911,7 +910,6 @@ describe("runPreparedReply media-only handling", () => {
911910
OriginatingTo: "C123",
912911
ChatType: "group",
913912
MediaPaths: [imagePath],
914-
MediaTypes: ["image/png"],
915913
MediaWorkspaceDir: tmpDir,
916914
},
917915
}),

0 commit comments

Comments
 (0)