fix: avoid global image size regression
parent
b59ea0e3f3
commit
20c0d1f2c5
|
|
@ -30,6 +30,7 @@ export {
|
||||||
isRateLimitErrorMessage,
|
isRateLimitErrorMessage,
|
||||||
isTimeoutErrorMessage,
|
isTimeoutErrorMessage,
|
||||||
parseImageDimensionError,
|
parseImageDimensionError,
|
||||||
|
parseImageSizeError,
|
||||||
} from "./pi-embedded-helpers/errors.js";
|
} from "./pi-embedded-helpers/errors.js";
|
||||||
export { isGoogleModelApi, sanitizeGoogleTurnOrdering } from "./pi-embedded-helpers/google.js";
|
export { isGoogleModelApi, sanitizeGoogleTurnOrdering } from "./pi-embedded-helpers/google.js";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -401,6 +401,7 @@ const ERROR_PATTERNS = {
|
||||||
const IMAGE_DIMENSION_ERROR_RE =
|
const IMAGE_DIMENSION_ERROR_RE =
|
||||||
/image dimensions exceed max allowed size for many-image requests:\s*(\d+)\s*pixels/i;
|
/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_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 {
|
function matchesErrorPatterns(raw: string, patterns: readonly ErrorPattern[]): boolean {
|
||||||
if (!raw) return false;
|
if (!raw) return false;
|
||||||
|
|
@ -467,10 +468,23 @@ export function isImageDimensionErrorMessage(raw: string): boolean {
|
||||||
return Boolean(parseImageDimensionError(raw));
|
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 {
|
export function isImageSizeError(errorMessage?: string): boolean {
|
||||||
if (!errorMessage) return false;
|
if (!errorMessage) return false;
|
||||||
const lower = errorMessage.toLowerCase();
|
return Boolean(parseImageSizeError(errorMessage));
|
||||||
return lower.includes("image exceeds") && lower.includes("mb");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCloudCodeAssistFormatError(raw: string): boolean {
|
export function isCloudCodeAssistFormatError(raw: string): boolean {
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ import {
|
||||||
isContextOverflowError,
|
isContextOverflowError,
|
||||||
isFailoverAssistantError,
|
isFailoverAssistantError,
|
||||||
isFailoverErrorMessage,
|
isFailoverErrorMessage,
|
||||||
isImageSizeError,
|
parseImageSizeError,
|
||||||
parseImageDimensionError,
|
parseImageDimensionError,
|
||||||
isRateLimitAssistantError,
|
isRateLimitAssistantError,
|
||||||
isTimeoutErrorMessage,
|
isTimeoutErrorMessage,
|
||||||
|
|
@ -442,12 +442,17 @@ export async function runEmbeddedPiAgent(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// Handle image size errors with a user-friendly message (no retry needed)
|
// 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 {
|
return {
|
||||||
payloads: [
|
payloads: [
|
||||||
{
|
{
|
||||||
text:
|
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.",
|
"Please compress or resize the image and try again.",
|
||||||
isError: true,
|
isError: true,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -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_AUDIO_BYTES = 16 * 1024 * 1024; // 16MB
|
||||||
export const MAX_VIDEO_BYTES = 16 * 1024 * 1024; // 16MB
|
export const MAX_VIDEO_BYTES = 16 * 1024 * 1024; // 16MB
|
||||||
export const MAX_DOCUMENT_BYTES = 100 * 1024 * 1024; // 100MB
|
export const MAX_DOCUMENT_BYTES = 100 * 1024 * 1024; // 100MB
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue