@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
109 lines (108 loc) • 5.49 kB
TypeScript
/**
* Enhanced proxy-aware fetch implementation for AI SDK providers
* Supports HTTP/HTTPS proxies, authentication, and NO_PROXY bypass.
* SOCKS proxy URLs are recognized but not supported: a request that selects
* one falls back to a direct connection, or fails under strict mode
* (NEUROLINK_PROXY_STRICT, or `createProxyFetch({ strict: true })`).
* Lightweight implementation extracted from research of major proxy packages
*/
import type { Dispatcher, ProxyAgent } from "undici";
import type { GoogleGenAIHttpOptions, ProxyTimeouts, ProxyFetchOptions } 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, timeouts?: ProxyTimeouts): Promise<ProxyAgent | null>;
/**
* Fetch for the proxy command's own outbound requests. A configured proxy is
* mandatory for its target: a failed proxy connection must never retry direct.
* The optional dispatcher preserves callers' direct-connection tuning.
*/
export declare function fetchProxyAware(url: string | URL, init?: RequestInit, directDispatcher?: Dispatcher, proxyTimeouts?: ProxyTimeouts): Promise<Response>;
/**
* Whether NEUROLINK_PROXY_STRICT asks for "proxy or nothing": `true`, `1`,
* `yes` or `on` (any case). Any other value, or none, keeps the default of
* retrying a failed proxy attempt over a direct connection.
*/
export declare function isProxyStrictMode(): boolean;
/**
* The WebSocket clients (`ws`) are built without a proxy agent, so they always
* connect directly. Call this before opening one: when a proxy is configured
* for `wsUrl` (and NO_PROXY does not exempt it), strict mode refuses the
* connection, and otherwise a warning says the socket bypasses the proxy.
* `label` names the client in the message; the URL itself is never logged,
* since some carry an API key in the query string.
*/
export declare function assertWebSocketProxyPolicy(wsUrl: string, label: string): void;
/**
* Create a proxy-aware fetch function with enhanced capabilities
* Supports HTTP/HTTPS proxies, authentication, and NO_PROXY bypass.
*
* By default a request whose proxy attempt fails is retried once over a
* direct connection, with a warning. Strict mode (`options.strict`, or
* NEUROLINK_PROXY_STRICT when the option is omitted) fails it instead. Strict
* mode never blocks a request NO_PROXY exempts, or one sent while no proxy is
* configured: those go direct by design.
*/
export declare function createProxyFetch(options?: ProxyFetchOptions): 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;
strict: boolean;
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;