UNPKG

@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

78 lines (77 loc) • 3.6 kB
/** * Enhanced proxy-aware fetch implementation for AI SDK providers * Supports HTTP/HTTPS, SOCKS4/5, authentication, and NO_PROXY bypass * Lightweight implementation extracted from research of major proxy packages */ import type { ProxyAgent } from "undici"; import type { GoogleGenAIHttpOptions } from "../types/index.js"; /** * Classify a fetch failure as a transient network error worth retrying. * * undici's `fetch()` wraps the real failure in `TypeError: fetch failed` * with the actionable code (`ECONNRESET`, `UND_ERR_SOCKET`, ...) on * `error.cause` — sometimes nested another level (e.g. SocketError inside * a ConnectTimeoutError). Walk the cause chain so those are recognized; * checking only the top-level error silently classified every undici * connection death as non-retryable. * * Deliberately NOT retried: `UND_ERR_HEADERS_TIMEOUT` / `UND_ERR_BODY_TIMEOUT` * — those already waited out undici's own long deadline (default 300s), and * replaying them can triple a stall under the caller's wall-clock budget. * * Exported for direct coverage by the no-API test suite. */ export declare function isTransientNetworkError(error: unknown): boolean; /** * The dispatcher a request to `targetUrl` must use to go through the proxy the * environment configures, or `null` when none applies (no proxy variable for * the URL's scheme, or the host is listed in NO_PROXY). * * Unlike the fetch from {@link createProxyFetch}, a caller that needs a * request to be proxied or refused (a download whose destination was vetted * under proxy rules) gets no fallback to a direct connection from this: a proxy * that cannot be used (an unsupported SOCKS URL, an unparsable URL) throws. */ export declare function getProxyDispatcherForUrl(targetUrl: string): Promise<ProxyAgent | null>; /** * Create a proxy-aware fetch function with enhanced capabilities * Supports HTTP/HTTPS, SOCKS4/5, authentication, and NO_PROXY bypass */ export declare function createProxyFetch(): typeof fetch; /** * Mask credentials in a proxy URL for safe logging/reporting. * * Exported so provider-side fetch loggers (lmStudio, llamaCpp, deepseek, * nvidiaNim) can sanitize upstream URLs before emitting warnings — reverse- * proxied deployments can embed credentials or signed query params in the * base URL, and those should never reach application logs verbatim. */ export declare function maskProxyUrl(url: string | null | undefined): string | null; /** * Get enhanced proxy status information */ export declare function getProxyStatus(): { enabled: boolean; httpProxy: string | null; httpsProxy: string | null; allProxy: string | null; socksProxy: string | null; noProxy: string | null; method: string; capabilities: string[]; }; /** * `httpOptions` fragment that routes the @google/genai SDK through the * configured proxy, or nothing when none is configured. * * The SDK calls global `fetch` unless `HttpOptions.fetch` is set (declared and * used from 2.23.0), and global `fetch` does not read HTTP_PROXY / HTTPS_PROXY * unless Node itself was started with env-proxy support. The proxy-aware fetch * is passed only when a proxy is configured: with none, the SDK keeps its own * request path instead of gaining this module's trace headers and retries. * * Covers the SDK's `generateContent`, `generateContentStream` and * `embedContent` requests. It does not reach Gemini Live websockets or the * Application Default Credentials token requests. */ export declare function googleSdkProxyHttpOptions(): GoogleGenAIHttpOptions;