mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 14:48:28 +00:00
fix: sanitize oversized image payloads
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||
import type { ImageContent } from "@mariozechner/pi-ai";
|
||||
|
||||
import { createSubsystemLogger } from "../logging.js";
|
||||
import { getImageMetadata, resizeToJpeg } from "../media/image-ops.js";
|
||||
|
||||
type ToolContentBlock = AgentToolResult<unknown>["content"][number];
|
||||
@@ -14,6 +16,7 @@ type TextContentBlock = Extract<ToolContentBlock, { type: "text" }>;
|
||||
// and recompress base64 image blocks when they exceed these limits.
|
||||
const MAX_IMAGE_DIMENSION_PX = 2000;
|
||||
const MAX_IMAGE_BYTES = 5 * 1024 * 1024;
|
||||
const log = createSubsystemLogger("agents/tool-images");
|
||||
|
||||
function isImageBlock(block: unknown): block is ImageContentBlock {
|
||||
if (!block || typeof block !== "object") return false;
|
||||
@@ -41,26 +44,41 @@ async function resizeImageBase64IfNeeded(params: {
|
||||
mimeType: string;
|
||||
maxDimensionPx: number;
|
||||
maxBytes: number;
|
||||
}): Promise<{ base64: string; mimeType: string; resized: boolean }> {
|
||||
label?: string;
|
||||
}): Promise<{
|
||||
base64: string;
|
||||
mimeType: string;
|
||||
resized: boolean;
|
||||
width?: number;
|
||||
height?: number;
|
||||
}> {
|
||||
const buf = Buffer.from(params.base64, "base64");
|
||||
const meta = await getImageMetadata(buf);
|
||||
const width = meta?.width;
|
||||
const height = meta?.height;
|
||||
const overBytes = buf.byteLength > params.maxBytes;
|
||||
const maxDim = Math.max(width ?? 0, height ?? 0);
|
||||
if (typeof width !== "number" || typeof height !== "number") {
|
||||
if (!overBytes) {
|
||||
return {
|
||||
base64: params.base64,
|
||||
mimeType: params.mimeType,
|
||||
resized: false,
|
||||
};
|
||||
}
|
||||
} else if (!overBytes && width <= params.maxDimensionPx && height <= params.maxDimensionPx) {
|
||||
return { base64: params.base64, mimeType: params.mimeType, resized: false };
|
||||
const hasDimensions = typeof width === "number" && typeof height === "number";
|
||||
if (hasDimensions && !overBytes && width <= params.maxDimensionPx && height <= params.maxDimensionPx) {
|
||||
return {
|
||||
base64: params.base64,
|
||||
mimeType: params.mimeType,
|
||||
resized: false,
|
||||
width,
|
||||
height,
|
||||
};
|
||||
}
|
||||
if (hasDimensions && (width > params.maxDimensionPx || height > params.maxDimensionPx || overBytes)) {
|
||||
log.warn("Image exceeds limits; resizing", {
|
||||
label: params.label,
|
||||
width,
|
||||
height,
|
||||
maxDimensionPx: params.maxDimensionPx,
|
||||
maxBytes: params.maxBytes,
|
||||
});
|
||||
}
|
||||
|
||||
const qualities = [85, 75, 65, 55, 45, 35];
|
||||
const maxDim = hasDimensions ? Math.max(width ?? 0, height ?? 0) : params.maxDimensionPx;
|
||||
const sideStart = maxDim > 0 ? Math.min(params.maxDimensionPx, maxDim) : params.maxDimensionPx;
|
||||
const sideGrid = [sideStart, 1800, 1600, 1400, 1200, 1000, 800]
|
||||
.map((v) => Math.min(params.maxDimensionPx, v))
|
||||
@@ -80,10 +98,23 @@ async function resizeImageBase64IfNeeded(params: {
|
||||
smallest = { buffer: out, size: out.byteLength };
|
||||
}
|
||||
if (out.byteLength <= params.maxBytes) {
|
||||
log.info("Image resized", {
|
||||
label: params.label,
|
||||
width,
|
||||
height,
|
||||
maxDimensionPx: params.maxDimensionPx,
|
||||
maxBytes: params.maxBytes,
|
||||
originalBytes: buf.byteLength,
|
||||
resizedBytes: out.byteLength,
|
||||
quality,
|
||||
side,
|
||||
});
|
||||
return {
|
||||
base64: out.toString("base64"),
|
||||
mimeType: "image/jpeg",
|
||||
resized: true,
|
||||
width,
|
||||
height,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -127,6 +158,7 @@ export async function sanitizeContentBlocksImages(
|
||||
mimeType,
|
||||
maxDimensionPx,
|
||||
maxBytes,
|
||||
label,
|
||||
});
|
||||
out.push({
|
||||
...block,
|
||||
@@ -144,6 +176,17 @@ export async function sanitizeContentBlocksImages(
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function sanitizeImageBlocks(
|
||||
images: ImageContent[],
|
||||
label: string,
|
||||
opts: { maxDimensionPx?: number; maxBytes?: number } = {},
|
||||
): Promise<{ images: ImageContent[]; dropped: number }> {
|
||||
if (images.length === 0) return { images, dropped: 0 };
|
||||
const sanitized = await sanitizeContentBlocksImages(images as ToolContentBlock[], label, opts);
|
||||
const next = sanitized.filter(isImageBlock) as ImageContent[];
|
||||
return { images: next, dropped: Math.max(0, images.length - next.length) };
|
||||
}
|
||||
|
||||
export async function sanitizeToolResultImages(
|
||||
result: AgentToolResult<unknown>,
|
||||
label: string,
|
||||
|
||||
Reference in New Issue
Block a user