UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

29 lines (28 loc) • 1.87 kB
import { admitImageBytes, ADMITTED_IMAGE_MIME_TYPES, IMAGE_ADMISSION, unprocessedSizeClause } from "../image-admission.js"; function inlineImageRefusal(detail) { const e = new Error(`TaskSpec.images: ${detail}`); e.code = "config.image_invalid"; return e; } export async function admitInlineImage(img, processor, subject = "an inline image") { const data = img.data; const mimeType = img.mimeType; if (typeof data !== "string" || typeof mimeType !== "string") throw inlineImageRefusal(`${subject} needs a base64 string \`data\` and a string \`mimeType\`.`); const admitted = await admitImageBytes(Buffer.from(data, "base64"), mimeType, processor); if (admitted.ok) return admitted.block; switch (admitted.reason) { case "mime_unsupported": throw inlineImageRefusal(`${subject} declares mimeType ${JSON.stringify(mimeType.slice(0, 80))}, which the model API does not accept (accepted: ${ADMITTED_IMAGE_MIME_TYPES.join(", ")}).`); case "magic_mismatch": throw inlineImageRefusal(`${subject}'s bytes do not open with a valid ${mimeType} header (empty, truncated, mis-declared, not base64, or a variant such as an Apple-optimized PNG that model APIs cannot read) — refused rather than sent.`); case "raw_too_large": throw inlineImageRefusal(`${subject} is ${admitted.rawBytes} bytes, over the ${admitted.cap}-byte cap.`); case "dimensions_unreadable": case "dimensions_too_large": throw inlineImageRefusal(`${subject} is refused rather than sent: ${unprocessedSizeClause(admitted)}.`); case "base64_too_large": throw inlineImageRefusal(`${subject}'s base64 is ${admitted.base64.length} chars, over the ${IMAGE_ADMISSION.hard.imageBase64Chars}-char / 5MB API image limit — shrink it before sending.`); } }