diff --git a/CHANGELOG.md b/CHANGELOG.md index 4929d185..b8c0c3f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ like `## [0.4.16] - 2026-10-24`. ## [Unreleased] +### Fixed + +- A picture too large for the model no longer fails the chat or a subagent through CLIProxyAPI: on the OpenAI-compatible wire, as on the Anthropic one, an image over 8000 px a side goes as a note, and for Claude, once a request holds more than 20 images and PDFs, so does one over 3000 px. The stored history keeps the picture. A remaining "image dimensions exceed max allowed size" 400 drops the picture and retries instead of falling back to another model. A full-page browser screenshot of a page wider than 7900 px stops at 7900 px. + ### Changed - Desktop: Open folder… uses the native OS folder picker for this app's paired computer, then confirms the folder with the existing checks and Add as project. Other devices keep the in-app browser; cancelling keeps the current folder, and a native dialog failure opens the in-app browser with an explanation. diff --git a/packages/core/src/browser/manager.ts b/packages/core/src/browser/manager.ts index e26e2a70..6c69bb57 100644 --- a/packages/core/src/browser/manager.ts +++ b/packages/core/src/browser/manager.ts @@ -1977,9 +1977,10 @@ export class BrowserManager implements BrowserTakeover { CDP_MS, page.evaluate(() => ({ w: document.documentElement.scrollWidth, h: document.documentElement.scrollHeight })), ).catch(() => undefined); - if (dims && dims.h > MAX_SHOT_HEIGHT) { - clip = { x: 0, y: 0, width: dims.w, height: MAX_SHOT_HEIGHT }; - notes.push(`Page is ${dims.h}px tall; the image stops at ${MAX_SHOT_HEIGHT}px.`); + if (dims && (dims.h > MAX_SHOT_HEIGHT || dims.w > MAX_SHOT_HEIGHT)) { + clip = { x: 0, y: 0, width: Math.min(dims.w, MAX_SHOT_HEIGHT), height: Math.min(dims.h, MAX_SHOT_HEIGHT) }; + if (dims.h > MAX_SHOT_HEIGHT) notes.push(`Page is ${dims.h}px tall; the image stops at ${MAX_SHOT_HEIGHT}px.`); + if (dims.w > MAX_SHOT_HEIGHT) notes.push(`Page is ${dims.w}px wide; the image stops at ${MAX_SHOT_HEIGHT}px.`); } } for (let attempt = 0; attempt < 2; attempt++) { diff --git a/packages/core/src/providers/anthropic.ts b/packages/core/src/providers/anthropic.ts index 919280a4..8bf6ebb3 100644 --- a/packages/core/src/providers/anthropic.ts +++ b/packages/core/src/providers/anthropic.ts @@ -1,11 +1,10 @@ import { createHash } from 'node:crypto'; import { logEvent } from '../util/log'; -import type { ContentBlock, StopReason, ToolResultBlock, Usage } from '../types'; -import { imageLeftOutNote, imageSize } from '../attachments/image'; +import type { ContentBlock, StopReason, Usage } from '../types'; import type { ProviderConfig } from '../config/schema'; import { sanitizeToolId } from '../util/ids'; import { normalizeModelId } from './catalog'; -import { BaseProvider, budgetOf, documentsForWire, effortOf, parseToolInput } from './common'; +import { BaseProvider, budgetOf, capImageSizes, documentsForWire, effortOf, parseToolInput } from './common'; import { clampEffort, claudeEffort, claudeThinkingOff, type Effort } from './effort'; import { classifyStreamError, isUnreadablePdfError, parseRateLimitHeaders } from './errors'; import { getJson, hostOf, openStream, postJson, trimSlash } from './http'; @@ -222,37 +221,10 @@ function lastCacheable(blocks: Wire[]): Wire | undefined { return undefined; } -/** Over this many images in one request, each image's longer side must stay within MANY_IMAGE_MAX_PX. */ -const MANY_IMAGES = 20; -// Anthropic 400: "At least one of the image dimensions exceed max allowed size for many-image requests: 3000 pixels" (vision docs: 3,000 px) -const MANY_IMAGE_MAX_PX = 3000; - -/** - * The request's messages with each image too large for a many-image request - * replaced by a note, once the request holds more than MANY_IMAGES images - * (attachments and tool results' images). On the wire only: the stored - * history keeps the image, so it comes back when the count drops. An image - * whose size cannot be read is kept (the runner's bad-image retry covers it). - */ -function capManyImages(contents: ContentBlock[][]): ContentBlock[][] { - const count = (bs: ContentBlock[]): number => - bs.reduce((n, b) => n + (b.type === 'image' ? 1 : b.type === 'tool_result' && typeof b.content !== 'string' ? count(b.content) : 0), 0); - if (contents.reduce((n, c) => n + count(c), 0) <= MANY_IMAGES) return contents; - const one = (b: ContentBlock): ContentBlock => { - if (b.type === 'tool_result') return typeof b.content === 'string' ? b : { ...b, content: b.content.map(one) as ToolResultBlock['content'] }; - if (b.type !== 'image' || !b.data) return b; - // the header is near the start; 256 KB of base64 covers a JPEG's EXIF and ICC segments before its frame header - const s = imageSize(Buffer.from(b.data.replace(/^data:[^,]*,/, '').slice(0, 256 * 1024), 'base64')); - if (!s || Math.max(s.width, s.height) <= MANY_IMAGE_MAX_PX) return b; - return { type: 'text', text: imageLeftOutNote(`(${b.mediaType})`, `too large (${s.width}x${s.height}) for a request with more than ${MANY_IMAGES} images; ask for a smaller copy`) }; - }; - return contents.map((c) => c.map(one)); -} - function mapMessages(msgs: ProviderMessage[], names: ToolNameMap, breakpoints: Wire[], vision: boolean): Wire[] { const out: Wire[] = []; // text documents as text: Anthropic-compatible endpoints (Z.ai) drop a text-source document - const contents = capManyImages(msgs.map((m) => (m.role === 'user' ? documentsForWire(m.content, true, vision) : m.content))); + const contents = capImageSizes(msgs.map((m) => (m.role === 'user' ? documentsForWire(m.content, true, vision) : m.content))); for (const [i, m] of msgs.entries()) { const content = contents[i]; let blocks = content.map((b) => mapBlock(b, names)).filter((x): x is Wire => !!x); diff --git a/packages/core/src/providers/common.ts b/packages/core/src/providers/common.ts index e43f238b..03288403 100644 --- a/packages/core/src/providers/common.ts +++ b/packages/core/src/providers/common.ts @@ -1,6 +1,6 @@ import type { ContentBlock, DocumentBlock, ImageBlock, TextBlock, ToolResultBlock } from '../types'; import { isZip, mediaTypeFor } from '../attachments/limits'; -import { imageLeftOutNote, isModelImage, SNIFF_BYTES, sniffImageType } from '../attachments/image'; +import { imageLeftOutNote, imageSize, isModelImage, SNIFF_BYTES, sniffImageType } from '../attachments/image'; import { pdfLeftOutNote, pdfProblem } from '../attachments/pdf'; import type { ProviderConfig } from '../config/schema'; import { catalogEntries, familyForProvider, findCatalogEntry, lookupModel, normalizeModelId, type CatalogEntry } from './catalog'; @@ -212,6 +212,45 @@ export function documentsForWire(content: ContentBlock[], pdf: boolean, vision = return content.map(one); } +/** Anthropic vision docs: "The maximum dimensions for each image are 8000x8000 px." */ +export const IMAGE_MAX_PX = 8000; +/** Over this many image and document blocks in one request, each image's longer side must stay within MANY_IMAGE_MAX_PX. */ +export const MANY_IMAGES = 20; +// Anthropic 400: "At least one of the image dimensions exceed max allowed size for many-image requests: 3000 pixels"; vision docs: "neither dimension exceeds 3,000 px" is safe on all platforms +export const MANY_IMAGE_MAX_PX = 3000; + +/** An image's pixel size from the start of its base64; a JPEG whose frame header sits past a large EXIF or ICC segment needs more. */ +function base64ImageSize(data: string): { width: number; height: number } | undefined { + const b64 = data.replace(/^data:[^,]*,/, ''); + const at = (n: number) => imageSize(Buffer.from(b64.slice(0, n), 'base64')); + return at(4096) ?? at(256 * 1024); +} + +/** + * The request's messages (each message's content, after documentsForWire) + * with each image too large for the API replaced by a note: a side over + * IMAGE_MAX_PX always, and when `many` and the request holds more than + * MANY_IMAGES image and document blocks (attachments and tool results'), + * a side over MANY_IMAGE_MAX_PX. On the wire only: the stored history keeps + * the image, so it comes back when the count drops. An image whose size + * cannot be read is kept (the runner's bad-image retry covers it). + */ +export function capImageSizes(contents: ContentBlock[][], many = true): ContentBlock[][] { + const count = (bs: ContentBlock[]): number => + bs.reduce((n, b) => n + (b.type === 'image' || b.type === 'document' ? 1 : b.type === 'tool_result' && typeof b.content !== 'string' ? count(b.content) : 0), 0); + const crowded = many && contents.reduce((n, c) => n + count(c), 0) > MANY_IMAGES; + const one = (b: ContentBlock): ContentBlock => { + if (b.type === 'tool_result') return typeof b.content === 'string' ? b : { ...b, content: b.content.map(one) as ToolResultBlock['content'] }; + if (b.type !== 'image' || !b.data) return b; + const s = base64ImageSize(b.data); + const side = s ? Math.max(s.width, s.height) : 0; + if (side > IMAGE_MAX_PX) return { type: 'text', text: imageLeftOutNote(`(${b.mediaType})`, `too large (${s!.width}x${s!.height}), over ${IMAGE_MAX_PX} px a side; ask for a smaller copy`) }; + if (crowded && side > MANY_IMAGE_MAX_PX) return { type: 'text', text: imageLeftOutNote(`(${b.mediaType})`, `too large (${s!.width}x${s!.height}) for a request with more than ${MANY_IMAGES} images; ask for a smaller copy`) }; + return b; + }; + return contents.map((c) => c.map(one)); +} + /** True when the request asks for thinking: a level or a budget, not Off. */ export function wantsThinking(thinking: ChatRequest['thinking']): boolean { return !!thinking && !thinking.off; diff --git a/packages/core/src/providers/errors.ts b/packages/core/src/providers/errors.ts index 01f86101..662434f9 100644 --- a/packages/core/src/providers/errors.ts +++ b/packages/core/src/providers/errors.ts @@ -587,10 +587,10 @@ export function classifyHttpError( /** * Anthropic's "Could not process image" (and "Image does not match the provided media type"), OpenAI's "Invalid image" and image_parse_error. - * Also Anthropic's "image dimensions exceed max allowed size for many-image requests" for a picture whose size the wire cap could not read - * (anthropic.ts capManyImages): each drop takes one image out, so the request falls to 20 images, where the cap no longer applies. + * Also Anthropic's "image dimensions exceed max allowed size: 8000 pixels" and "... for many-image requests: 3000 pixels" (direct or through + * CLIProxyAPI) for a picture whose size the wire cap could not read (common.ts capImageSizes): each drop takes one image out of the history. */ -const UNREADABLE_IMAGE_RE = /could not process image|image does not match the provided media type|invalid[ _]image|image_parse_error|exceed max allowed size for many-image requests/i; +const UNREADABLE_IMAGE_RE = /could not process image|image does not match the provided media type|invalid[ _]image|image_parse_error|image dimensions exceed max allowed size/i; export function isUnreadableImageText(text: string): boolean { return UNREADABLE_IMAGE_RE.test(text); diff --git a/packages/core/src/providers/openai.ts b/packages/core/src/providers/openai.ts index 4141c6ad..4e5c6d89 100644 --- a/packages/core/src/providers/openai.ts +++ b/packages/core/src/providers/openai.ts @@ -1,6 +1,6 @@ import type { ContentBlock, ImageBlock, StopReason, Usage } from '../types'; import { findCatalogEntry, normalizeModelId } from './catalog'; -import { BaseProvider, budgetOf, dataUrl, documentsForWire, effortOf, parseToolInput, toolResultMedia, toolResultString, toolTurnLabel, wantsThinking } from './common'; +import { BaseProvider, budgetOf, capImageSizes, dataUrl, documentsForWire, effortOf, parseToolInput, toolResultMedia, toolResultString, toolTurnLabel, wantsThinking } from './common'; import { classifyStreamError, parseRateLimitHeaders } from './errors'; import { SKIP_SIGNATURE as GEMINI_SKIP_SIGNATURE } from './gemini'; import { hostOf, openStream, trimSlash } from './http'; @@ -80,7 +80,9 @@ export function chatMessages(req: ChatRequest, names: ToolNameMap, pdf = true, v if (system) out.push({ role: 'system', content: system }); const toolNames = new Map(); for (const m of req.messages) for (const b of m.content) if (b.type === 'tool_use') toolNames.set(b.id, names.wire(b.name)); - for (const m of req.messages) { + // Claude behind an OpenAI-compatible endpoint (CLIProxyAPI) keeps Anthropic's many-image limit; 8000 px a side goes for every model + const contents = capImageSizes(req.messages.map((m) => (m.role === 'user' ? documentsForWire(m.content, pdf, vision) : m.content)), /claude/i.test(req.model)); + for (const [i, m] of req.messages.entries()) { if (m.role === 'assistant') { const a = assistantMessage(m, names, req.model); if (a) out.push(a); @@ -88,7 +90,7 @@ export function chatMessages(req: ChatRequest, names: ToolNameMap, pdf = true, v } const tools: Wire[] = []; const parts: Wire[] = []; - for (const b of documentsForWire(m.content, pdf, vision)) { + for (const b of contents[i]) { if (b.type === 'tool_result') { tools.push({ role: 'tool', tool_call_id: b.toolUseId, content: toolResultString(b) }); const imgs = toolResultMedia(b) diff --git a/packages/core/test/attachments/image.test.ts b/packages/core/test/attachments/image.test.ts index 44fa212b..2839019e 100644 --- a/packages/core/test/attachments/image.test.ts +++ b/packages/core/test/attachments/image.test.ts @@ -137,5 +137,16 @@ describe('the API could not read a picture', () => { const big = classifyHttpError('cliproxy', 400, undefined, JSON.stringify({ type: 'error', error: { type: 'invalid_request_error', message: many } })); expect(big.kind).toBe('bad_request'); expect(isUnreadableImageError(big)).toBe(true); + // any image over 8000 px a side (hub log 2026-11-11), from cliproxy in either error shape and from Anthropic direct + const side = 'messages.1.content.29.image.source.base64.data: At least one of the image dimensions exceed max allowed size: 8000 pixels'; + for (const [provider, body] of [ + ['cliproxy', { type: 'error', error: { type: 'invalid_request_error', message: side } }], + ['cliproxy', { error: { message: side, type: 'invalid_request_error', code: null } }], + ['anthropic', { type: 'error', error: { type: 'invalid_request_error', message: side } }], + ] as const) { + const err = classifyHttpError(provider, 400, undefined, JSON.stringify(body)); + expect(err.kind).toBe('bad_request'); + expect(isUnreadableImageError(err)).toBe(true); + } }); }); diff --git a/packages/core/test/providers/anthropic.test.ts b/packages/core/test/providers/anthropic.test.ts index bac13f40..d6208ed1 100644 --- a/packages/core/test/providers/anthropic.test.ts +++ b/packages/core/test/providers/anthropic.test.ts @@ -454,6 +454,14 @@ describe('AnthropicProvider request mapping', () => { const body = provider().buildBody(base({ messages: req(20) })) as any; expect(body.messages[0].content.every((b: any) => b.type === 'image')).toBe(true); expect(body.messages[2].content.filter((b: any) => b.type === 'image')).toHaveLength(10); + // a PDF counts toward the 20 (vision docs: "20 or fewer image and document blocks") + const pdf = { type: 'document' as const, mediaType: 'application/pdf', data: Buffer.from('%PDF-1.4 fake').toString('base64'), title: 'a.pdf' }; + const withPdf = req(19); + withPdf[0].content.push(pdf); + expect((provider().buildBody(base({ messages: withPdf })) as any).messages[2].content[0].content[0].type).toBe('text'); + // over 8000 px a side is a note even alone + const tall = (provider().buildBody(base({ messages: [{ role: 'user', content: [{ type: 'image', mediaType: 'image/png', data: png(1280, 13000) }] }] })) as any).messages[0].content[0]; + expect(tall).toEqual({ type: 'text', text: '[image (image/png) left out: too large (1280x13000), over 8000 px a side; ask for a smaller copy]' }); }); test('responseSchema uses output_config on the first-party API only', () => { diff --git a/packages/core/test/providers/openai.test.ts b/packages/core/test/providers/openai.test.ts index 04cf0cef..f13ce134 100644 --- a/packages/core/test/providers/openai.test.ts +++ b/packages/core/test/providers/openai.test.ts @@ -250,6 +250,42 @@ describe('OpenAIProvider request mapping', () => { expect(m[4]).toEqual({ role: 'tool', tool_call_id: 'call_x', content: 'Error: boom' }); }); + test('image sizes: over 8000 px a side is a note for any model; over 20 images, over 3000 px only for Claude', () => { + const be32 = (n: number) => [n >>> 24, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff]; + const png = (w: number, h: number) => + Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52, ...be32(w), ...be32(h), 8, 6, 0, 0, 0]).toString('base64'); + const small = { type: 'image' as const, mediaType: 'image/png', data: png(800, 600) }; + const cliproxy = new OpenAIProvider({ + id: 'cliproxy', + config: { type: 'openai', baseUrl: 'http://127.0.0.1:8317/v1', modelOverrides: { 'claude-opus-5-5': { vision: true }, 'gpt-6.1-sol': { vision: true } } }, + }); + // the big one is a tool result's image (a Read of a full-page screenshot), the rest are attachments + const tool = (model: string, w: number, h: number, smalls: number) => { + const { messages } = cliproxy.buildBody({ + model, + system: [], + messages: [ + { role: 'user', content: Array.from({ length: smalls }, () => small) }, + { role: 'assistant', content: [{ type: 'tool_use', id: 'call_1', name: 'Read', input: { file_path: 'shot.png' } }] }, + { role: 'user', content: [{ type: 'tool_result', toolUseId: 'call_1', content: [{ type: 'image', mediaType: 'image/png', data: png(w, h) }] }] }, + ], + }) as any; + return messages.slice(messages.findIndex((m: any) => m.role === 'tool')); + }; + const kept = (w: number, h: number) => [ + { role: 'tool', tool_call_id: 'call_1', content: '(image output follows)' }, + { role: 'user', content: [{ type: 'text', text: 'Images returned by tool call call_1:' }, { type: 'image_url', image_url: { url: `data:image/png;base64,${png(w, h)}` } }] }, + ]; + const note = (why: string) => [{ role: 'tool', tool_call_id: 'call_1', content: `[image (image/png) left out: ${why}; ask for a smaller copy]` }]; + for (const model of ['claude-opus-5-5', 'gpt-6.1-sol']) { + expect(tool(model, 1280, 8100, 0)).toEqual(note('too large (1280x8100), over 8000 px a side')); + expect(tool(model, 1280, 8000, 0)).toEqual(kept(1280, 8000)); + } + expect(tool('claude-opus-5-5', 3100, 100, 20)).toEqual(note('too large (3100x100) for a request with more than 20 images')); + expect(tool('claude-opus-5-5', 3100, 100, 19)).toEqual(kept(3100, 100)); + expect(tool('gpt-6.1-sol', 3100, 100, 20)).toEqual(kept(3100, 100)); + }); + test('official OpenAI: max_completion_tokens, prompt_cache_key, reasoning_effort, no temperature for reasoning models', () => { const body = official().buildBody({ ...ask('x'), model: 'gpt-5.5', maxTokens: 500, cacheKey: 'sess-1', temperature: 0.3, thinking: { effort: 'high' } }) as any; expect(body.max_completion_tokens).toBe(500);