|
| 1 | +import type { TranslatorBudget } from "../../lib/translator-budget"; |
| 2 | +import type { AdapterRequest } from "../../adapters/base"; |
| 3 | + |
| 4 | +// https://docs.mistral.ai/capabilities/vision/ documents 8 images per API request, and most |
| 5 | +// Mistral vision models refuse a ninth with HTTP 400 ("Total number of images exceeds the maximum |
| 6 | +// allowed of 8"). Some models accept more; DeepSeek V4.1 Flash answers an opaque HTTP 503 (code |
| 7 | +// 3800) past its cap. Operators declare a model's cap with `modelCapabilities.<id>.mistralMaxImages`. |
| 8 | +export const MISTRAL_DEFAULT_MAX_IMAGES = 8; |
| 9 | +/** |
| 10 | + * Caps measured on api.mistral.ai (2026-11-04) for models above the documented 8: the largest |
| 11 | + * accepted count before refusal, or for GLM the largest count tried without one. |
| 12 | + */ |
| 13 | +const MEASURED_MAX_IMAGES: Readonly<Record<string, number>> = { |
| 14 | + "deepseek-v4-1-flash-1017": 20, |
| 15 | + "mistral-large-4": 100, |
| 16 | + "mistral-large-4-0": 100, |
| 17 | + "zai-glm-5-3-flash": 256, |
| 18 | +}; |
| 19 | + |
| 20 | +/** The operator's declaration, then a measured cap, then the documented default. */ |
| 21 | +export function mistralMaxImages(model: string, declared?: number): number { |
| 22 | + return declared ?? (Object.hasOwn(MEASURED_MAX_IMAGES, model) ? MEASURED_MAX_IMAGES[model]! : MISTRAL_DEFAULT_MAX_IMAGES); |
| 23 | +} |
| 24 | + |
| 25 | +const NOTE = "[image omitted: Mistral accepts a limited number of images per request; older images were dropped]"; |
| 26 | + |
| 27 | +/** Narrow parsed JSON objects without treating arrays as image or message records. */ |
| 28 | +function record(value: unknown): value is Record<string, unknown> { |
| 29 | + return value !== null && typeof value === "object" && !Array.isArray(value); |
| 30 | +} |
| 31 | + |
| 32 | +/** Only the first-party Chat destination, never a similarly named gateway. */ |
| 33 | +export function isMistralChatRequest(request: AdapterRequest): boolean { |
| 34 | + try { |
| 35 | + const url = new URL(request.url); |
| 36 | + return url.origin === "https://api.mistral.ai" && url.pathname === "/v1/chat/completions" && request.method === "POST"; |
| 37 | + } catch { return false; } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Keep the newest images a Mistral request may carry and replace older ones with a text note, |
| 42 | + * across every message (including promoted tool-result images). The client history stays intact; |
| 43 | + * only this outbound body is pruned. Returns undefined when nothing needs to change. |
| 44 | + */ |
| 45 | +export function mistralImageLimitBody( |
| 46 | + request: AdapterRequest, |
| 47 | + options: { maxImages: number; budget?: TranslatorBudget }, |
| 48 | +): string | undefined { |
| 49 | + if (!isMistralChatRequest(request)) return undefined; |
| 50 | + // Cheap bound before parsing: a request with no more image parts than the cap is never rewritten. |
| 51 | + let occurrences = 0; |
| 52 | + for (let at = request.body.indexOf('"image_url"'); at !== -1; at = request.body.indexOf('"image_url"', at + 1)) { |
| 53 | + if (++occurrences > options.maxImages) break; |
| 54 | + } |
| 55 | + if (occurrences <= options.maxImages) return undefined; |
| 56 | + const requestBytes = Buffer.byteLength(request.body, "utf8"); |
| 57 | + const releaseCopy = options.budget?.observeAcceptedRequestCopy(requestBytes * 2); |
| 58 | + try { |
| 59 | + const body: unknown = JSON.parse(request.body); |
| 60 | + if (!record(body) || !Array.isArray(body.messages)) return undefined; |
| 61 | + const images: Array<{ parts: unknown[]; index: number }> = []; |
| 62 | + for (const message of body.messages) { |
| 63 | + if (!record(message) || !Array.isArray(message.content)) continue; |
| 64 | + message.content.forEach((part, index) => { |
| 65 | + if (record(part) && part.type === "image_url") images.push({ parts: message.content as unknown[], index }); |
| 66 | + }); |
| 67 | + } |
| 68 | + const excess = images.length - options.maxImages; |
| 69 | + if (excess <= 0) return undefined; |
| 70 | + for (const { parts, index } of images.slice(0, excess)) parts[index] = { type: "text", text: NOTE }; |
| 71 | + const releaseSerialization = options.budget?.observeExternallyCapped("passthrough_serialization", requestBytes); |
| 72 | + try { return JSON.stringify(body); } finally { releaseSerialization?.(); } |
| 73 | + } finally { releaseCopy?.(); } |
| 74 | +} |
0 commit comments