UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

103 lines (102 loc) • 3.08 kB
// ============================================ // TYPE GUARDS // ============================================ /** * Type guard to check if content is TextContent */ export function isTextContent(content) { return content.type === "text"; } /** * Type guard to check if content is ImageContent */ export function isImageContent(content) { return content.type === "image"; } /** * Type guard to check if content is CSVContent */ export function isCSVContent(content) { return content.type === "csv"; } /** * Type guard to check if content is PDFContent */ export function isPDFContent(content) { return content.type === "pdf"; } /** * Type guard to check if content is AudioContent */ export function isAudioContent(content) { return content.type === "audio"; } /** * Type guard to check if content is VideoContent */ export function isVideoContent(content) { return content.type === "video"; } /** * Type guard to check if input contains multimodal content * Now includes audio and video detection */ /** * Type guard to validate if an object matches the DirectorSegment shape. * Checks for required prompt (string) and image (Buffer, string, or ImageWithAltText). */ function isDirectorSegment(segment) { if (!segment || typeof segment !== "object") { return false; } const maybeSegment = segment; // Check for required prompt field if (typeof maybeSegment.prompt !== "string" || !maybeSegment.prompt) { return false; } // Check for required image field const { image } = maybeSegment; if (!image) { return false; } // Validate image type: Buffer, string (URL/path), or ImageWithAltText if (Buffer.isBuffer(image)) { return true; } if (typeof image === "string") { return true; } // Check for ImageWithAltText structure if (typeof image === "object" && "data" in image) { const imgData = image.data; return Buffer.isBuffer(imgData) || typeof imgData === "string"; } return false; } export function isMultimodalInput(input) { if (typeof input !== "object" || input === null) { return false; } const m = input; // Each field must be a NON-EMPTY ARRAY. Checking `.length` alone is unsafe: // a string value (e.g. `{ images: "oops" }`) has a truthy `.length`, so the // old guard would claim a malformed input is multimodal and then crash // downstream when the field is iterated as an array. const hasItems = (v) => Array.isArray(v) && v.length > 0; return !!(hasItems(m.images) || hasItems(m.csvFiles) || hasItems(m.pdfFiles) || hasItems(m.files) || hasItems(m.content) || hasItems(m.audioFiles) || hasItems(m.videoFiles) || (Array.isArray(m.segments) && m.segments.length > 0 && m.segments.every(isDirectorSegment))); } /** * Type guard to check if message content is multimodal (array) */ export function isMultimodalMessageContent(content) { return Array.isArray(content); }