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

119 lines (118 loc) • 6.44 kB
/** * Client image references on the proxy's translation routes, vetted before the * SDK sees them. * * The OpenAI, Gemini and (once a model is routed or falls back to another * provider) Claude routes copy whatever a client put in an image slot into * `input.images`, and the SDK resolves a string itself: an `http(s)` URL is * fetched with redirects followed and no address check, and anything else that * names an existing file is read from the local disk. Both suit a program that * names its own files. Neither suits a request that arrived over a socket. * * So the proxy resolves every reference itself and hands the SDK nothing but * images it has checked: * - `data:` URIs carry their own content. The payload is decoded strictly (an * alphabet, length or padding no decoder takes is refused) and its leading * bytes must be a recognised image, whatever the URI says it is. The SDK is * handed `data:<detected type>;base64,<standard payload>`: the type comes * from the bytes, never from the client, so a JPEG labelled `image/png` * reaches the provider as `image/jpeg`; * - `https://` URLs are downloaded with the same helper the agent routes use * (`resolveAgentInput` -> `safeDownload`): redirects refused, addresses * checked, size capped. The bytes must then be a recognised image too, or * the URL is refused, so a page that answers `200 text/html` is never sent * on as a picture. The SDK reads the type of those bytes itself, from the * same signatures; * - everything else (local paths, `http://`, `file://`, any other scheme, an * empty string) is refused before the file system or the network is * touched, so the answer for a path that exists and one that does not is the * same. * * An image slot that carries nothing is refused too, never skipped. The doors * hand over what the client sent: an empty `data:` payload (an OpenAI * `data:image/png;base64,`, a Claude `base64` source or a Gemini `inlineData` * of an image type whose `data` is empty or absent) is `invalid_image_data`, and * an empty URL (an OpenAI `image_url.url` or a Claude `url` source) is * `invalid_image_reference`. * * "A recognised image" is a file signature (`sniffInputImageMimeType`): PNG, * JPEG, WebP and GIF, which every provider takes, and HEIC, HEIF, AVIF, BMP, * TIFF, ICO and JPEG 2000, which no provider except Google (HEIC and HEIF only) * takes. The SDK tries to convert those seven to PNG before dispatch, for every * provider, Google included; when neither sharp nor ffmpeg can read the file it * sends the original bytes under the detected type. SVG is markup and is * refused. Nothing in the proxy decodes the picture: a body that is corrupt * after a valid signature is the provider's to refuse. * * Unlike the agent routes there is no allowed-roots setting: the proxy listens * on loopback without credentials by default, and a path allow-list on a server * with no caller identity would still hand its owner's disk to every local * process, and to every holder of a share token. Local paths are refused * outright. * * Count and size are bounded as the agent routes bound them: at most 20 images, * each at most 50 MB, all together at most 100 MB, with a `data:` URI charged * its string length. * * The refusal is a {@link ProxyContextPreflightError}, so every site that * already answers a local admission refusal (400, no retry, no fallback) answers * this one in the client's own error format. * * @module proxy/proxyImageInput */ import { ProxyContextPreflightError } from "./proxyContextPreflight.js"; /** * `data` as padded standard-alphabet base64, which is what a `data:` URI carries, * or undefined when it is not well-formed base64. * * The SDK forwards the base64 text of a `data:` URI to the provider as it is, so * a URL-safe or unpadded payload would reach a provider that expects neither. * The alphabet check alone lets through a payload no decoder takes: one * character over a multiple of four is no base64 length at all, and padding * must be exactly what the length calls for. An empty string is not base64. * * One rule for every door: the Gemini parser applies it before wrapping raw * `inlineData`, and the vetter applies it again to every `data:` URI it is given. */ export declare function toStandardBase64(data: string): string | undefined; /** An image reference the proxy refuses; answers 400 in the client's format. */ export declare class ProxyImageInputError extends ProxyContextPreflightError { constructor(code: string, message: string); } /** * Resolve one list of client image references to what the SDK may be given: * `data:` URIs rewritten to carry the type their bytes have, and downloaded * bytes that are an image. * * Inline images are judged before anything is downloaded, so a request already * doomed by one costs no network. Size and count are judged on the `data:` URIs * as the client sent them (their string length), then on the downloads (their * bytes), exactly as the agent routes judge them. * * `signal` cancels the work: a download in flight is aborted and no further * reference is started. A cancellation rejects with the signal's own error, not * with a {@link ProxyImageInputError}, since nothing was refused. * * @throws {ProxyImageInputError} for the first reference that is refused * (`invalid_image_reference`, `invalid_image_data`, `image_url_unfetchable`), * or for a list that carries too many images or too many bytes * (`image_input_too_large`). */ export declare function vetProxyImages(images: readonly unknown[], signal?: AbortSignal): Promise<Array<string | Buffer>>; /** * Per-request holder of vetted images. * * A translated request can make several attempts, each handing the SDK the same * references, so each reference is resolved once and looked up by its text * afterwards. `apply` fails closed: a list that carries a reference `vet` has * not seen is refused, never forwarded. */ export declare function createProxyImageVetter(): { /** * Vet the images `body` would hand the SDK that no earlier call vetted. * `signal` cancels downloads still running; see {@link vetProxyImages}. */ vet(body: object, signal?: AbortSignal): Promise<void>; /** `body` with its `input.images` replaced by the vetted content. */ apply<T extends object>(body: T): T; };