@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
144 lines (143 loc) • 4.29 kB
JavaScript
/**
* Multimodal Content Types for NeuroLink
*
* Central registry for all multimodal input/output types.
* This file consolidates types from content.ts and conversation.ts
* to provide a single source of truth for multimodal functionality.
*
* @module types/multimodal
*
* @example Basic Multimodal Input
* ```typescript
* import type { MultimodalInput } from './types/multimodal.js';
*
* const input: MultimodalInput = {
* text: "What's in this image?",
* images: [imageBuffer, "https://example.com/image.jpg"],
* pdfFiles: [pdfBuffer]
* };
* ```
*
* @example Audio/Video Input (Future)
* ```typescript
* const avInput: MultimodalInput = {
* text: "Transcribe this audio and analyze this video",
* audioFiles: [audioBuffer],
* videoFiles: ["path/to/video.mp4"]
* };
* ```
*
* @example Advanced Content Array
* ```typescript
* const advanced: MultimodalInput = {
* text: "irrelevant", // ignored when content[] is provided
* content: [
* { type: "text", text: "Analyze these items:" },
* { type: "image", data: imageBuffer, mediaType: "image/jpeg" },
* { type: "pdf", data: pdfBuffer, metadata: { filename: "report.pdf" } }
* ]
* };
* ```
*/
// ============================================
// 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);
}