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

297 lines (296 loc) 9.64 kB
/** * HTTP/WebSocket Client Library * * Core client for accessing the NeuroLink API with built-in authentication, * retry logic, and middleware support. Supports both browser and Node.js environments. * * @module @neurolink/client */ import type { ClientConfig, ClientRequestOptions, ClientApiResponse, ClientMiddleware, ClientStreamCallbacks, ClientStreamResult, ClientGenerateRequestOptions, ClientGenerateResponse, ClientStreamRequestOptions, ClientAgentExecuteOptions, ClientAgentExecuteResult, ClientAgentInfo, ClientWorkflowExecuteOptions, ClientWorkflowExecuteResult, ClientWorkflowInfo, ClientToolInfo, ClientProviderStatus, ClientWebSocketOptions, ClientWebSocketState, ClientWebSocketMessageHandler, UnknownRecord } from "../types/index.js"; import { HttpError, ClientNetworkError, ClientTimeoutError } from "./errors.js"; /** * Combine multiple AbortSignals into a single signal. * Aborts as soon as any input signal is aborted. */ export declare function combineSignals(...signals: AbortSignal[]): AbortSignal; /** * Promise-based delay utility. */ export declare function sleep(ms: number): Promise<void>; export { HttpError as NeuroLinkApiError, ClientNetworkError, ClientTimeoutError, }; /** * HTTP Client for NeuroLink API * * Provides type-safe access to all NeuroLink API endpoints with * built-in authentication, retry logic, and middleware support. * * @example Basic usage * ```typescript * import { createClient } from '@neurolink/client'; * * const client = createClient({ * baseUrl: 'https://api.neurolink.example.com', * apiKey: 'your-api-key', * }); * * const result = await client.generate({ * input: { text: 'Hello, world!' }, * provider: 'openai', * }); * ``` * * @example With middleware * ```typescript * const client = createClient({ baseUrl: 'https://api.example.com' }); * * client.use(async (request, next) => { * console.log('Request:', request.url); * const response = await next(); * console.log('Response:', response.status); * return response; * }); * ``` */ export declare class NeuroLinkClient { private config; private middlewares; private wsConnection; private wsState; private wsMessageHandlers; constructor(config: ClientConfig); /** * Get default fetch implementation */ private getDefaultFetch; /** * Add middleware to the client * * @param middleware - ClientMiddleware function * @returns Client instance for chaining */ use(middleware: ClientMiddleware): this; /** * Clear all middleware */ clearMiddleware(): this; /** * Generate unique request ID */ private generateRequestId; /** * Calculate delay for exponential backoff */ private calculateDelay; /** * Delay utility — delegates to shared sleep() */ private delay; /** * Combine multiple abort signals — delegates to shared combineSignals() */ private combineSignals; /** * Debug logging via the unified NeuroLink logger. * Guarded by `logger.shouldLog("debug")` so expensive serialization is * skipped when debug output is disabled. */ private log; /** * Execute HTTP request with middleware and retry logic */ private request; private _doRequest; /** * Generate text using AI models * * @example * ```typescript * const response = await client.generate({ * input: { text: 'Write a poem about coding' }, * provider: 'openai', * model: 'gpt-4o', * temperature: 0.7, * }); * console.log(response.data.content); * ``` */ generate(options: ClientGenerateRequestOptions, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientGenerateResponse>>; /** * Stream text generation * * @example * ```typescript * await client.stream( * { input: { text: 'Tell me a story' }, provider: 'openai' }, * { * onText: (text) => process.stdout.write(text), * onDone: (result) => console.log('\nDone!', result.usage), * } * ); * ``` */ stream(options: ClientStreamRequestOptions | ClientGenerateRequestOptions, callbacks?: ClientStreamCallbacks, requestOptions?: ClientRequestOptions): Promise<ClientStreamResult>; /** * Handle individual stream events */ private handleStreamEvent; /** * Execute an agent * * @example * ```typescript * const result = await client.executeAgent({ * agentId: 'customer-support', * input: 'I need help with my order', * sessionId: 'user-123', * }); * console.log(result.data.content); * ``` */ executeAgent(options: ClientAgentExecuteOptions, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientAgentExecuteResult>>; /** * Stream agent execution */ streamAgent(options: ClientAgentExecuteOptions, callbacks?: ClientStreamCallbacks, requestOptions?: ClientRequestOptions): Promise<ClientStreamResult>; /** * List available agents */ listAgents(requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientAgentInfo[]>>; /** * Get agent details */ getAgent(agentId: string, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientAgentInfo>>; /** * Execute a workflow * * @example * ```typescript * const result = await client.executeWorkflow({ * workflowId: 'data-pipeline', * input: { data: [...] }, * }); * * if (result.data.status === 'running') { * // Poll for completion * const status = await client.getWorkflowStatus( * 'data-pipeline', * result.data.runId * ); * } * ``` */ executeWorkflow(options: ClientWorkflowExecuteOptions, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientWorkflowExecuteResult>>; /** * Resume a suspended workflow */ resumeWorkflow(workflowId: string, resumeToken: string, resumeData?: UnknownRecord, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientWorkflowExecuteResult>>; /** * Get workflow execution status */ getWorkflowStatus(workflowId: string, runId: string, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientWorkflowExecuteResult>>; /** * Cancel workflow execution */ cancelWorkflow(workflowId: string, runId: string, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<{ success: boolean; }>>; /** * List available workflows */ listWorkflows(requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientWorkflowInfo[]>>; /** * Get workflow details */ getWorkflow(workflowId: string, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientWorkflowInfo>>; /** * List available tools * * @example * ```typescript * const tools = await client.listTools({ category: 'data' }); * console.log(tools.data); * ``` */ listTools(options?: { category?: string; serverId?: string; }, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientToolInfo[]>>; /** * Execute a tool */ executeTool(toolName: string, params: UnknownRecord, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<unknown>>; /** * Get tool details */ getTool(toolName: string, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientToolInfo>>; /** * List available providers */ listProviders(requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientProviderStatus[]>>; /** * Get provider status */ getProviderStatus(providerName: string, requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<ClientProviderStatus>>; /** * Health check */ health(requestOptions?: ClientRequestOptions): Promise<ClientApiResponse<{ status: string; version: string; }>>; /** * Connect to WebSocket for real-time communication * * @example * ```typescript * client.connectWebSocket({ * url: 'wss://api.example.com/ws', * autoReconnect: true, * }); * * client.onWebSocketMessage('chat', (data) => { * console.log('Chat message:', data); * }); * ``` */ connectWebSocket(options?: Partial<ClientWebSocketOptions>): void; /** * Disconnect WebSocket */ disconnectWebSocket(): void; /** * Send message over WebSocket */ sendWebSocketMessage(data: unknown): void; /** * Register WebSocket message handler */ onWebSocketMessage(messageType: string, handler: ClientWebSocketMessageHandler): () => void; /** * Get WebSocket connection state */ getWebSocketState(): ClientWebSocketState; /** * Update client configuration */ updateConfig(config: Partial<ClientConfig>): void; /** * Get current configuration (readonly) */ getConfig(): Readonly<ClientConfig>; } /** * Create a new NeuroLink client instance * * @example * ```typescript * import { createClient } from '@neurolink/client'; * * const client = createClient({ * baseUrl: 'https://api.neurolink.example.com', * apiKey: process.env.NEUROLINK_API_KEY, * debug: true, * }); * ``` */ export declare function createClient(config: ClientConfig): NeuroLinkClient;