From 20c0d1f2c58a4ca71378deabf3a316bdc0b8acb6 Mon Sep 17 00:00:00 2001 From: Shadow Date: Tue, 27 Jan 2026 15:59:11 -0600 Subject: [PATCH] fix: avoid global image size regression --- src/agents/pi-embedded-helpers.ts | 1 + src/agents/pi-embedded-helpers/errors.ts | 18 ++++++++++++++++-- src/agents/pi-embedded-runner/run.ts | 11 ++++++++--- src/media/constants.ts | 2 +- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/agents/pi-embedded-helpers.ts b/src/agents/pi-embedded-helpers.ts index 4aed2d047..88443756f 100644 --- a/src/agents/pi-embedded-helpers.ts +++ b/src/agents/pi-embedded-helpers.ts @@ -30,6 +30,7 @@ export { isRateLimitErrorMessage, isTimeoutErrorMessage, parseImageDimensionError, + parseImageSizeError, } from "./pi-embedded-helpers/errors.js"; export { isGoogleModelApi, sanitizeGoogleTurnOrdering } from "./pi-embedded-helpers/google.js"; diff --git a/src/agents/pi-embedded-helpers/errors.ts b/src/agents/pi-embedded-helpers/errors.ts index bad476176..849c4293e 100644 --- a/src/agents/pi-embedded-helpers/errors.ts +++ b/src/agents/pi-embedded-helpers/errors.ts @@ -401,6 +401,7 @@ const ERROR_PATTERNS = { const IMAGE_DIMENSION_ERROR_RE = /image dimensions exceed max allowed size for many-image requests:\s*(\d+)\s*pixels/i; const IMAGE_DIMENSION_PATH_RE = /messages\.(\d+)\.content\.(\d+)\.image/i; +const IMAGE_SIZE_ERROR_RE = /image exceeds\s*(\d+(?:\.\d+)?)\s*mb/i; function matchesErrorPatterns(raw: string, patterns: readonly ErrorPattern[]): boolean { if (!raw) return false; @@ -467,10 +468,23 @@ export function isImageDimensionErrorMessage(raw: string): boolean { return Boolean(parseImageDimensionError(raw)); } +export function parseImageSizeError(raw: string): { + maxMb?: number; + raw: string; +} | null { + if (!raw) return null; + const lower = raw.toLowerCase(); + if (!lower.includes("image exceeds") || !lower.includes("mb")) return null; + const match = raw.match(IMAGE_SIZE_ERROR_RE); + return { + maxMb: match?.[1] ? Number.parseFloat(match[1]) : undefined, + raw, + }; +} + export function isImageSizeError(errorMessage?: string): boolean { if (!errorMessage) return false; - const lower = errorMessage.toLowerCase(); - return lower.includes("image exceeds") && lower.includes("mb"); + return Boolean(parseImageSizeError(errorMessage)); } export function isCloudCodeAssistFormatError(raw: string): boolean { diff --git a/src/agents/pi-embedded-runner/run.ts b/src/agents/pi-embedded-runner/run.ts index 006172e14..870453f38 100644 --- a/src/agents/pi-embedded-runner/run.ts +++ b/src/agents/pi-embedded-runner/run.ts @@ -34,7 +34,7 @@ import { isContextOverflowError, isFailoverAssistantError, isFailoverErrorMessage, - isImageSizeError, + parseImageSizeError, parseImageDimensionError, isRateLimitAssistantError, isTimeoutErrorMessage, @@ -442,12 +442,17 @@ export async function runEmbeddedPiAgent( }; } // Handle image size errors with a user-friendly message (no retry needed) - if (isImageSizeError(errorText)) { + const imageSizeError = parseImageSizeError(errorText); + if (imageSizeError) { + const maxMb = imageSizeError.maxMb; + const maxMbLabel = + typeof maxMb === "number" && Number.isFinite(maxMb) ? `${maxMb}` : null; + const maxBytesHint = maxMbLabel ? ` (max ${maxMbLabel}MB)` : ""; return { payloads: [ { text: - "Image too large for the model (max 5MB). " + + `Image too large for the model${maxBytesHint}. ` + "Please compress or resize the image and try again.", isError: true, }, diff --git a/src/media/constants.ts b/src/media/constants.ts index 8577b6d20..e74ac6934 100644 --- a/src/media/constants.ts +++ b/src/media/constants.ts @@ -1,4 +1,4 @@ -export const MAX_IMAGE_BYTES = 5 * 1024 * 1024; // 5MB (Anthropic API limit) +export const MAX_IMAGE_BYTES = 6 * 1024 * 1024; // 6MB export const MAX_AUDIO_BYTES = 16 * 1024 * 1024; // 16MB export const MAX_VIDEO_BYTES = 16 * 1024 * 1024; // 16MB export const MAX_DOCUMENT_BYTES = 100 * 1024 * 1024; // 100MB