@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
100 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAX_VIEW_IMAGE_BYTES = void 0;
exports.assertViewImageByteLength = assertViewImageByteLength;
exports.imageOutputFromBytes = imageOutputFromBytes;
exports.sniffImageMediaType = sniffImageMediaType;
exports.toUint8Array = toUint8Array;
const errors_1 = require("../../errors.js");
exports.MAX_VIEW_IMAGE_BYTES = 10 * 1024 * 1024;
function assertViewImageByteLength(path, byteLength) {
if (byteLength > exports.MAX_VIEW_IMAGE_BYTES) {
throw new errors_1.UserError(`Image file exceeds the 10 MB limit: ${path}`);
}
}
function imageOutputFromBytes(path, bytes) {
assertViewImageByteLength(path, bytes.byteLength);
const mediaType = sniffImageMediaType(bytes, path);
if (!mediaType) {
throw new errors_1.UserError(`Unsupported image format for view_image: ${path}`);
}
return {
type: 'image',
image: {
data: Uint8Array.from(bytes),
mediaType,
},
};
}
function sniffImageMediaType(bytes, path) {
if (matchesBytes(bytes, 0, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
return 'image/png';
}
if (matchesBytes(bytes, 0, [0xff, 0xd8, 0xff])) {
return 'image/jpeg';
}
if (matchesAscii(bytes, 0, 'GIF87a') || matchesAscii(bytes, 0, 'GIF89a')) {
return 'image/gif';
}
if (matchesAscii(bytes, 0, 'RIFF') && matchesAscii(bytes, 8, 'WEBP')) {
return 'image/webp';
}
if (matchesAscii(bytes, 0, 'BM')) {
return 'image/bmp';
}
if (matchesBytes(bytes, 0, [0x49, 0x49, 0x2a, 0x00]) ||
matchesBytes(bytes, 0, [0x4d, 0x4d, 0x00, 0x2a]) ||
matchesBytes(bytes, 0, [0x49, 0x49, 0x2b, 0x00]) ||
matchesBytes(bytes, 0, [0x4d, 0x4d, 0x00, 0x2b])) {
return 'image/tiff';
}
if (looksLikeSvg(bytes)) {
return 'image/svg+xml';
}
return svgMediaTypeFromPath(path);
}
async function toUint8Array(value) {
if (!value) {
return new Uint8Array();
}
if (value instanceof Uint8Array) {
return Uint8Array.from(value);
}
if (value instanceof ArrayBuffer) {
return new Uint8Array(value);
}
if (ArrayBuffer.isView(value)) {
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
}
if (typeof value === 'string') {
return new TextEncoder().encode(value);
}
if (typeof Blob !== 'undefined' && value instanceof Blob) {
return new Uint8Array(await value.arrayBuffer());
}
return Uint8Array.from(value);
}
function matchesAscii(bytes, offset, expected) {
return matchesBytes(bytes, offset, Array.from(expected, (character) => character.charCodeAt(0)));
}
function matchesBytes(bytes, offset, expected) {
if (offset < 0 || offset + expected.length > bytes.byteLength) {
return false;
}
return expected.every((byte, index) => bytes[offset + index] === byte);
}
function looksLikeSvg(bytes) {
const prefix = new TextDecoder()
.decode(bytes.subarray(0, Math.min(bytes.byteLength, 512)))
.trimStart()
.toLowerCase();
return prefix.startsWith('<svg') || /^<\?xml[\s\S]*<svg/u.test(prefix);
}
function svgMediaTypeFromPath(path) {
const lowerPath = path?.trim().toLowerCase() ?? '';
if (lowerPath.endsWith('.svg') || lowerPath.endsWith('.svgz')) {
return 'image/svg+xml';
}
return null;
}
//# sourceMappingURL=media.js.map