@juspay/neurolink
Version:
Universal AI Development Platform with working MCP integration, multi-provider support, and professional CLI. Built-in tools operational, 58+ external MCP servers discoverable. Connect to filesystem, GitHub, database operations, and more. Build, test, and
141 lines (140 loc) • 5.02 kB
TypeScript
/**
* Timeout utilities for NeuroLink
*
* Provides flexible timeout parsing and error handling for AI operations.
* Supports multiple time formats: milliseconds, seconds, minutes, hours.
*/
/**
* Custom error class for timeout operations
*/
export declare class TimeoutError extends Error {
readonly timeout: number;
readonly provider?: string | undefined;
readonly operation?: "generate" | "stream" | undefined;
constructor(message: string, timeout: number, provider?: string | undefined, operation?: "generate" | "stream" | undefined);
}
/**
* Parse timeout value from various formats
* @param timeout - Can be number (ms), string with unit, or undefined
* @returns Parsed timeout in milliseconds or undefined
* @throws Error if format is invalid
*
* Examples:
* - parseTimeout(5000) => 5000
* - parseTimeout('30s') => 30000
* - parseTimeout('2m') => 120000
* - parseTimeout('1.5h') => 5400000
* - parseTimeout(undefined) => undefined
*/
export declare function parseTimeout(timeout: number | string | undefined): number | undefined;
/**
* Default timeout configurations for different providers and operations
*/
export declare const DEFAULT_TIMEOUTS: {
global: string;
streaming: string;
providers: {
openai: string;
bedrock: string;
vertex: string;
anthropic: string;
azure: string;
"google-ai": string;
huggingface: string;
ollama: string;
mistral: string;
};
tools: {
default: string;
filesystem: string;
network: string;
computation: string;
};
};
/**
* Get default timeout for a specific provider
* @param provider - Provider name
* @param operation - Operation type (generate or stream)
* @returns Default timeout string
*/
export declare function getDefaultTimeout(provider: string, operation?: "generate" | "stream"): string;
/**
* Create a timeout promise that rejects after specified duration
* @param timeout - Timeout duration
* @param provider - Provider name for error message
* @param operation - Operation type for error message
* @returns Promise that rejects with TimeoutError
*/
export declare function createTimeoutPromise(timeout: number | string | undefined, provider: string, operation: "generate" | "stream"): Promise<never> | null;
export interface TimeoutConfig {
operation: string;
timeout?: number | string;
gracefulShutdown?: boolean;
retryOnTimeout?: boolean;
maxRetries?: number;
abortSignal?: AbortSignal;
}
export interface TimeoutResult<T> {
success: boolean;
data?: T;
error?: Error;
timedOut: boolean;
executionTime: number;
retriesUsed: number;
}
/**
* Enhanced timeout manager with proper cleanup and abort controller integration
* Consolidated from timeout-manager.ts
*/
export declare class TimeoutManager {
private activeTimeouts;
/**
* Execute operation with timeout and proper cleanup
*/
executeWithTimeout<T>(operation: () => Promise<T>, config: TimeoutConfig): Promise<TimeoutResult<T>>;
private performSingleOperation;
private getTimeoutMs;
private generateOperationId;
private createTimeoutPromise;
private registerTimeout;
cleanup(operationId: string): void;
gracefulShutdown(): void;
}
/**
* Wrapper functions consolidated from timeout-wrapper.ts
*/
/**
* Wrap a promise with timeout
* @param promise - The promise to wrap
* @param timeout - Timeout duration (number in ms or string with unit)
* @param provider - Provider name for error messages
* @param operation - Operation type (generate or stream)
* @returns The result of the promise or throws TimeoutError
*/
export declare function withTimeout<T>(promise: Promise<T>, timeout: number | string | undefined, provider: string, operation: "generate" | "stream"): Promise<T>;
/**
* Wrap a streaming async generator with timeout
* @param generator - The async generator to wrap
* @param timeout - Timeout duration for the entire stream
* @param provider - Provider name for error messages
* @returns Wrapped async generator that respects timeout
*/
export declare function withStreamingTimeout<T>(generator: AsyncGenerator<T>, timeout: number | string | undefined, provider: string): AsyncGenerator<T>;
/**
* Create an abort controller with timeout
* @param timeout - Timeout duration
* @param provider - Provider name for error messages
* @param operation - Operation type
* @returns AbortController and cleanup function
*/
export declare function createTimeoutController(timeout: number | string | undefined, provider: string, operation: "generate" | "stream"): {
controller: AbortController;
cleanup: () => void;
timeoutMs: number;
} | null;
/**
* Merge abort signals (for combining user abort with timeout)
* @param signals - Array of abort signals to merge
* @returns Combined abort controller
*/
export declare function mergeAbortSignals(signals: (AbortSignal | undefined)[]): AbortController;