/** * WaveSpeed API client implementation. */ /** * Run options interface. */ export interface RunOptions { timeout?: number; pollInterval?: number; enableSyncMode?: boolean; maxRetries?: number; } /** * Base exception class for WaveSpeed errors */ export declare class WavespeedException extends Error { readonly taskId: string; readonly model: string; constructor(message: string, taskId?: string, model?: string); } /** * Timeout exception */ export declare class WavespeedTimeoutException extends WavespeedException { readonly timeout: number; constructor(taskId: string, model: string, timeout: number); } /** * Sync-mode wait timed out, but the task is still processing asynchronously */ export declare class WavespeedSyncTimeoutException extends WavespeedException { readonly resultUrl?: string | undefined; constructor(taskId: string, model: string, errorMessage: string, resultUrl?: string | undefined); } /** * Connection exception */ export declare class WavespeedConnectionException extends WavespeedException { constructor(taskId: string, model: string, details: string); } /** * Prediction failed exception */ export declare class WavespeedPredictionException extends WavespeedException { constructor(taskId: string, model: string, errorMessage: string); } /** * Unknown exception */ export declare class WavespeedUnknownException extends WavespeedException { constructor(taskId: string, model: string, originalError: any); } /** * Detail information for runNoThrow result. */ export interface RunDetail { taskId: string; status: 'completed' | 'failed' | 'processing'; model: string; error?: WavespeedException; createdAt?: string; resultUrl?: string; } /** * Result interface for runNoThrow method. */ export interface RunNoThrowResult { outputs: any[] | null; detail: RunDetail; } /** * WaveSpeed API client. * * Example: * const client = new Client("your-api-key"); * const output = await client.run("wavespeed-ai/z-image/turbo", { prompt: "Cat" }); * * // With sync mode (best-effort single request, waits for result) * const output2 = await client.run("wavespeed-ai/z-image/turbo", { prompt: "Cat" }, { enableSyncMode: true }); * * // With retry * const output3 = await client.run("wavespeed-ai/z-image/turbo", { prompt: "Cat" }, { maxRetries: 3 }); */ export declare class Client { private apiKey; private baseUrl; readonly connectionTimeout: number; readonly timeout: number; readonly maxRetries: number; readonly maxConnectionRetries: number; readonly retryInterval: number; /** * Initialize the client. * * Args: * apiKey: WaveSpeed API key. If not provided, uses wavespeed.config.api.apiKey. * options.baseUrl: Base URL for the API. If not provided, uses wavespeed.config.api.baseUrl. * options.connectionTimeout: Timeout for HTTP requests in seconds. * options.timeout: Total API call timeout in seconds. * options.maxRetries: Maximum number of retries for the entire operation. * options.maxConnectionRetries: Maximum retries for individual HTTP requests. * options.retryInterval: Base interval between retries in seconds. */ constructor(apiKey?: string, options?: { baseUrl?: string; connectionTimeout?: number; timeout?: number; maxRetries?: number; maxConnectionRetries?: number; retryInterval?: number; }); /** * Get request headers with authentication. */ private _getHeaders; /** * Submit a prediction request. * * Args: * model: Model identifier. * input: Input parameters. * enableSyncMode: If true, wait for result in single request. * timeout: Request timeout in seconds. * * Returns: * Tuple of [requestId, result]. In async mode, result is null. * In sync mode, requestId is null and result contains the response. * * Throws: * Error: If submission fails after retries. */ private _submit; /** * Get prediction result. * * Args: * requestId: The prediction request ID. * timeout: Request timeout in seconds. * * Returns: * Full API response. * * Throws: * Error: If fetching result fails after retries. */ private _getResult; /** * Wait for prediction to complete. * * Args: * requestId: The prediction request ID. * timeout: Maximum wait time in seconds (undefined = no timeout). * pollInterval: Time between polls in seconds. * * Returns: * Dict with "outputs" array. * * Throws: * Error: If prediction fails. * Error: If prediction times out. */ private _wait; /** * Determine if an error is worth retrying at the task level. * * Args: * error: The exception to check. * * Returns: * True if the error is retryable. */ private _isRetryableError; private _resultUrlFromData; private _isSyncTimeoutData; private _syncModeError; /** * Run a model and wait for the output. * * Args: * model: Model identifier (e.g., "wavespeed-ai/flux-dev"). * input: Input parameters for the model. * options.timeout: Maximum time to wait for completion (undefined = no timeout). * options.pollInterval: Interval between status checks in seconds. * options.enableSyncMode: If true, use synchronous mode (best-effort single request). * options.maxRetries: Maximum task-level retries (overrides client setting). * * Returns: * Dict containing "outputs" array with model outputs. * * Throws: * Error: If API key is not configured. * Error: If the prediction fails. * Error: If the prediction times out. */ run(model: string, input?: Record, options?: RunOptions): Promise>; /** * Run a model and wait for the output (no-throw version). * * This method is similar to run() but does not throw exceptions. * Instead, it returns a result object with outputs (null on failure) and detail information. * The detail object always contains the taskId, which is useful for debugging and tracking. * * Args: * model: Model identifier (e.g., "wavespeed-ai/flux-dev"). * input: Input parameters for the model. * options.timeout: Maximum time to wait for completion (undefined = no timeout). * options.pollInterval: Interval between status checks in seconds. * options.enableSyncMode: If true, use synchronous mode (best-effort single request). * options.maxRetries: Maximum task-level retries (overrides client setting). * * Returns: * Object containing: * - outputs: Array of model outputs (null if failed) * - detail: Object with taskId, status, error (if any), and other metadata * * Example: * const result = await client.runNoThrow("wavespeed-ai/z-image/turbo", { prompt: "Cat" }); * * if (result.outputs) { * console.log("Success:", result.outputs); * console.log("Task ID:", result.detail.taskId); * } else { * console.log("Failed:", result.detail.error); * console.log("Task ID:", result.detail.taskId); * } */ runNoThrow(model: string, input?: Record, options?: RunOptions): Promise; /** * Upload a file to WaveSpeed. * * Args: * file: File path string to upload. * options.timeout: Total API call timeout in seconds. * * Returns: * URL of the uploaded file. * * Throws: * Error: If API key is not configured. * Error: If file path does not exist. * Error: If upload fails. * * Example: * const url = await client.upload("/path/to/image.png"); * console.log(url); */ upload(file: string, options?: { timeout?: number; }): Promise; }