UNPKG

@juspay/neurolink

Version:

Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applicatio

197 lines (196 loc) 7.03 kB
/** * Image processing utilities for multimodal support * Handles format conversion for different AI providers */ import type { ProcessedImage, FileProcessingResult } from "../types/index.js"; /** * Image processor class for handling provider-specific image formatting */ export declare class ImageProcessor { /** * Process image Buffer (unified interface) * Matches CSVProcessor.process() signature for consistency * * @param content - Image file as Buffer * @param options - Processing options (unused for now) * @returns Processed image as data URI */ static process(content: Buffer, _options?: unknown): Promise<FileProcessingResult>; /** * Validate processed output meets required format * Checks: * - Base64 content is non-empty * - Data URI format is valid (data:{mimeType};base64,{content}) * - MIME type is in the allowed list * @param dataUri - The complete data URI string * @param base64 - The base64-encoded content * @param mediaType - The MIME type of the image * @throws Error if any validation fails */ private static validateProcessOutput; /** * Process image for OpenAI (requires data URI format) */ static processImageForOpenAI(image: Buffer | string): string; /** * Process image for Google AI (requires base64 without data URI prefix) */ static processImageForGoogle(image: Buffer | string): { mimeType: string; data: string; }; /** * Process image for Anthropic (requires base64 without data URI prefix) */ static processImageForAnthropic(image: Buffer | string): { mediaType: string; data: string; }; /** * Process image for Vertex AI (model-specific routing) */ static processImageForVertex(image: Buffer | string, model: string): { mimeType?: string; mediaType?: string; data: string; }; /** * Detect image type from filename or data */ static detectImageType(input: string | Buffer): string; /** * Validate image size (default 10MB limit) */ static validateImageSize(data: Buffer | string, maxSize?: number): boolean; /** * Validate image format */ static validateImageFormat(mediaType: string): boolean; /** * Get image dimensions from Buffer (basic implementation) */ static getImageDimensions(buffer: Buffer): { width: number; height: number; } | null; /** * Convert image to ProcessedImage format */ static processImage(image: Buffer | string, provider: string, model?: string): ProcessedImage; } /** * Whitelist of valid image file extensions (lowercase, no dots). * Used to validate file extensions against a known set of image formats. */ export declare const VALID_IMAGE_EXTENSIONS: string[]; /** * Utility functions for image handling */ export declare const imageUtils: { /** * Check if a string is a valid data URI */ isDataUri: (str: string) => boolean; /** * Check if a string is a valid URL */ isUrl: (str: string) => boolean; /** * Check if a string is base64 encoded */ isBase64: (str: string) => boolean; /** * Extract file extension from filename or URL. * Strips query strings and fragments before matching so that * "image.jpg?v=1" correctly returns "jpg". * Returns null if no extension is found or if the extension * contains non-alphanumeric characters. */ getFileExtension: (filename: string) => string | null; /** * Validate that an extension is a recognised image format. * Case-insensitive; rejects extensions with special characters. */ isValidImageExtension: (extension: string) => boolean; /** * Extract and validate image file extension from a filename or URL. * Returns null if the extension is missing or not a recognised image format. * * Security note: the last extension is used, so "malware.exe.jpg" returns * "jpg". Callers should apply additional checks (e.g. content inspection) * where double-extension attacks are a concern. */ getValidatedImageExtension: (filename: string) => string | null; /** * Convert file size to human readable format */ formatFileSize: (bytes: number) => string; /** * Convert Buffer to base64 string */ bufferToBase64: (buffer: Buffer) => string; /** * Convert base64 string to Buffer */ base64ToBuffer: (base64: string) => Buffer; /** * Convert file path to base64 data URI */ fileToBase64DataUri: (filePath: string, maxBytes?: number) => Promise<string>; /** * Convert URL to base64 data URI by downloading the image. * Implements retry logic with exponential backoff for network errors. * * Retries are performed for: * - Network errors (ECONNRESET, ENOTFOUND, ECONNREFUSED, ETIMEDOUT, ERR_NETWORK, AbortError) * - Server errors (5xx status codes) * - Rate limiting (429 Too Many Requests) * - Request timeouts (408 Request Timeout) * * Retries are NOT performed for: * - Client errors (4xx status codes except 408, 429) * - Invalid content type * - Content size limit exceeded * - Unsupported protocol * * @param url - The URL of the image to download * @param options - Configuration options * @param options.timeoutMs - Timeout for each download attempt (default: 15000ms) * @param options.maxBytes - Maximum allowed file size (default: 10MB) * @param options.maxAttempts - Maximum number of total attempts including initial attempt (default: 3) * @returns Promise<string> - Base64 data URI of the downloaded image * Rate-limited to 10 downloads per second to prevent DoS * Uses LRU cache to avoid redundant downloads of the same URL */ urlToBase64DataUri: (url: string, { timeoutMs, maxBytes, maxAttempts, }?: { timeoutMs?: number; maxBytes?: number; maxAttempts?: number; }) => Promise<string>; /** * Extract base64 data from data URI */ extractBase64FromDataUri: (dataUri: string) => string; /** * Extract MIME type from data URI */ extractMimeTypeFromDataUri: (dataUri: string) => string; /** * Create data URI from base64 and MIME type */ createDataUri: (base64: string, mimeType?: string) => string; /** * Validate base64 string format * Validates format BEFORE buffer allocation to prevent memory exhaustion */ isValidBase64: (str: string) => boolean; /** * Get base64 string size in bytes */ getBase64Size: (base64: string) => number; /** * Compress base64 image by reducing quality (basic implementation) * Note: This is a placeholder - for production use, consider using sharp or similar */ compressBase64: (base64: string, _quality?: number) => string; };