UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

27 lines (26 loc) • 1.59 kB
import { admitImageBytes, ADMITTED_IMAGE_MIME_TYPES } from "../../tools/fs/image-admission.js"; import { MCP_IMAGE_MAX_BASE64 } from "../image-downsample.js"; function inlineImageRefusal(detail) { const e = new Error(`TaskSpec.images: ${detail}`); e.code = "config.image_invalid"; return e; } export async function admitInlineImage(img) { const data = img.data; const mimeType = img.mimeType; if (typeof data !== "string" || typeof mimeType !== "string") throw inlineImageRefusal("an inline image needs a base64 string `data` and a string `mimeType`."); const admitted = await admitImageBytes(Buffer.from(data, "base64"), mimeType, undefined); if (admitted.ok) return admitted.block; switch (admitted.reason) { case "mime_unsupported": throw inlineImageRefusal(`an inline image 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(`an inline image's bytes do not begin with a ${mimeType} signature (empty, mis-declared, or not base64) — refused rather than sent.`); case "raw_too_large": throw inlineImageRefusal(`an inline image is ${admitted.rawBytes} bytes, over the ${admitted.cap}-byte cap.`); case "base64_too_large": throw inlineImageRefusal(`an inline image's base64 is ${admitted.base64.length} chars, over the ${MCP_IMAGE_MAX_BASE64}-char / 5MB API image limit — shrink it before sending.`); } }