@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
180 lines (179 loc) • 9.58 kB
TypeScript
import type { AIProviderName } from "../constants/enums.js";
import { BaseProvider } from "../core/baseProvider.js";
import { ProviderError } from "../types/index.js";
import type { DecisionDialect, DecisionError, DecisionQuestion, DecisionRequest, DecisionResult, DecisionPreparedMedia, DecisionState, LanguageModel, StreamOptions, StreamResult, ValidationSchema } from "../types/index.js";
export { asNumber, isRecord } from "./decide/guards.js";
/**
* FastAPI's request-validation envelope, `{"detail":[{loc, msg, input}]}`,
* reduced to its field paths and messages. `input` echoes the caller's own
* request back — possibly the whole state — so it is never surfaced.
*/
export declare function describeValidationErrors(detail: readonly unknown[]): string;
/**
* A LiteLLM proxy echoes credentials back in its error texts, in more than one
* wording: a masked key plus its hash on a rejected key, the whole key when it
* does not look like a LiteLLM key, a key hash on a rate limit. None of it
* belongs in an error message or a log, whatever the wording, so the
* configured key, anything shaped like a LiteLLM key, any long hex run and any
* embedded `data:` URL are all removed.
*
* Its request-validation errors also quote the request back (pydantic's
* `input_value=…`, the start of the state and questions), and its routing
* errors append the gateway's fallback table, which names every model group it
* serves. The first is the caller's data and the second is the gateway's, so
* both are cut.
*/
export declare function redactCredentials(message: string, apiKey: string): string;
/**
* The shared half of every "System One" decision provider — a model that takes
* one `state` plus typed questions and returns one typed answer each, with no
* text anywhere.
*
* Vendors differ in where the request goes, how it is authenticated, what the
* body carries and what their error envelopes look like. They do not differ in
* anything else, so everything else lives here: the missing-key check, the
* auth circuit breaker, the timeout and retry loop, answer parsing and usage.
* A new decision provider implements the abstract hooks and nothing more.
*/
export declare abstract class SystemOneDecisionProvider extends BaseProvider {
protected readonly proxyFetch: typeof fetch;
private readonly maxRetries;
/**
* Set once the key has been rejected. A bad key cannot fix itself, and
* without this every later request would pay a full round trip (~265ms
* measured on Jev) to be told so again — a permanent tax on the hot path for
* a misconfiguration.
*/
private authFailed;
protected constructor(modelName: string | undefined, providerName: AIProviderName, sdk: unknown);
/** Short vendor name for messages and logs, e.g. "TypeSafe". */
protected abstract vendorLabel(): string;
/** How the model is named when describing it, e.g. "TypeSafe (Jev)". */
protected abstract vendorDisplayName(): string;
protected abstract decisionApiKey(): string;
protected abstract missingKeyMessage(): string;
/**
* Why this instance cannot send a request even though it has a key, such as
* a provider with no built-in endpoint and none configured. Undefined when
* nothing is missing.
*/
protected missingConfigMessage(): string | undefined;
/**
* Where the request goes. The model asked for is passed in because one
* vendor names it in the URL path; every other provider ignores it.
*/
protected abstract decisionEndpoint(model: string): string;
protected abstract decisionHeaders(): Record<string, string>;
protected abstract buildDecisionBody(state: DecisionState, questions: Record<string, Record<string, unknown>>, model: string, media?: DecisionPreparedMedia): Record<string, unknown>;
/**
* A refusal the vendor's own gate would make on the request about to be
* sent, which the descriptor's limits cannot express. Throw the refusal
* (see `decisionError`); the default has no such gate.
*/
protected assertWithinVendorLimits(_payload: Record<string, unknown>, _model: string): void;
protected abstract parseDecisionError(status: number, payload: unknown, requestId: string | undefined): DecisionError;
protected abstract readRequestId(headers: Headers): string | undefined;
/**
* The System One wire calls the yes/no primitive `noul`; this codebase and
* every SDK exposing it call it `boolean`.
*/
protected encodeQuestion(question: DecisionQuestion): Record<string, unknown>;
/**
* The wire family this provider speaks. A provider on another layout
* overrides it; every one so far speaks System One.
*/
protected dialect(): DecisionDialect;
/**
* How long a rate-limited or unavailable response asks the caller to wait.
* Undefined keeps the default backoff. A wait longer than what is left of the
* attempt's timeout is not taken: the error is thrown at once, so a fail-open
* consumer is not held for the length of a vendor's cooldown.
*/
protected readRetryAfterMs(_headers: Headers): number | undefined;
/**
* The per-attempt timeout when the caller sets none: the descriptor's
* `decideMs`, plus `decideMsPerKStateTokens` for every full 1,000 estimated
* state tokens when it declares that, since a long state takes longer to read
* and a flat budget would cut off a request well inside the model's window. A
* provider whose latency grows another way overrides this. `stateTokens` is
* the local estimate, 0 when the descriptor sets no local limits.
*/
protected defaultTimeoutMs(_questionCount: number, stateTokens: number): number | undefined;
/**
* The part of a successful response that holds `answers`, `usage` and
* `model`. The default is the response itself; a vendor that wraps every
* answer in an envelope returns what is inside it. Errors are not passed
* through here: they are read from the whole response.
*/
protected readDecisionPayload(payload: unknown): unknown;
/** Per-question confidence a transport reports outside the answer objects. */
protected reportedConfidence(_payload: unknown): Record<string, number>;
protected resolveResponseModel(payload: Record<string, unknown>, requestedModel: string): string;
/**
* The model this request goes to and is checked against: the call's own, else
* the instance's. A provider whose default cannot read some requests may pick
* another for them here.
*/
protected resolveRequestModel(request: DecisionRequest): string;
/** Extra fields for the credential-rejected warning. */
protected diagnosticContext(): Record<string, unknown>;
protected getProviderName(): AIProviderName;
protected getDefaultDecisionModel(): string | undefined;
supportsTools(): boolean;
/**
* A decision model produces no text, so there is no chat model behind it.
* Unreachable while the descriptor declares `inferenceKinds: ["decide"]`;
* kept actionable for the case where something routes here anyway.
*/
protected getAISDKModel(): LanguageModel;
protected executeStream(_options: StreamOptions, _analysisSchema?: ValidationSchema): Promise<StreamResult>;
/**
* Must RETURN the error, never throw it (CLAUDE.md rule 6).
*/
protected formatProviderError(error: unknown): Error;
/**
* Evaluate a state against a batch of typed questions.
*
* Throws a {@link ProviderError} carrying a normalised {@link DecisionError}
* in `cause`. Internal hot paths should prefer `NeuroLink.tryDecide()`,
* which returns null instead.
*/
decide(request: DecisionRequest): Promise<DecisionResult>;
/**
* Refuse a question this provider does not read: a type it does not list in
* `decisionLimits.questionTypes` (absent means boolean, choice and score), or
* a choice or score past its per-question cap. Checked even when the limits
* are advisory, since a server that does not know a question type cannot
* answer it.
*/
private assertSupportedQuestions;
/**
* Turn the request's images, video and audio into wire form, or refuse it.
* Refused as a non-retryable `invalid_request`: resending the same media
* cannot help.
*/
private prepareMedia;
/**
* The encoded body, not just the media, is what the server's size limit
* applies to.
*/
private assertWithinRequestBytes;
/**
* Refuse a request the model cannot read in full. An encoder cuts the state
* off past its window without saying so, so without this a decision would
* be made on input the model never saw — and reported as if it had.
*
* Advisory limits (`decisionLimits.advisory`) are skipped: they publish a
* server's own ceiling for `NeuroLink.decisionLimits()` to report, and the
* server enforces them itself. The estimator and the flattening are the
* same functions that method uses, so a host that sizes a state against the
* reading measures exactly what is checked here.
*/
private assertWithinDecisionLimits;
/**
* Wrap a normalised decision failure in the provider error type, keeping
* the structured detail reachable via `cause` for callers that branch on
* `kind` (authentication vs rate_limit vs max_tokens_exceeded).
*/
protected decisionError(detail: DecisionError): ProviderError;
}