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

58 lines (57 loc) • 3.26 kB
/** * Shared provider-error classification. Migrated providers' * `formatProviderError(error)` delegate here instead of hand-rolling their * own TimeoutError-check → .includes()-chain → `new XError(...)` ladder. * Not yet migrated (they do not call it): Google AI Studio, SageMaker, the * media and embedding providers (Ideogram, Recraft, Stability, Replicate, * Jina, Voyage) and the System One decision provider. * * `classifyProviderError` picks the Error subclass + message; it does NOT * stamp statusCode/isRetryable/retryAfterMs onto the result — that * passthrough already happens generically in * `BaseProvider.handleProviderError()` (src/lib/core/baseProvider.ts) for * every provider's returned error, migrated or not, so duplicating it here * would risk the two copies disagreeing. */ import { type ProviderErrorRule } from "../types/index.js"; /** * Classify a raw provider error into a NeuroLink `ProviderError` subclass. * `rules` are tried in order; the first match wins. `TimeoutError` is * always handled first, ahead of any rule table — every provider treated * it identically before this change, so it is not made overridable. */ export declare function classifyProviderError(error: unknown, rules: ProviderErrorRule[], provider: string, modelName?: string): Error; /** * True when `status` is written in `message` as an HTTP status, as opposed to * an unrelated number that happens to have the same digits ("max_tokens (429) * exceeds the model limit", a request id, a token count). Provider rules that * once tested the bare digits (`/429/`) classified such a 400 as a rate limit. * * Status-shaped means one of: * - the text starts with it, the way some SDKs format an API error * ("429 {...}", "401 Unauthorized"); * - it follows "status", "status code" or "HTTP" ("Request failed with status * code 429", "HTTP/1.1 429"); * - it sits next to the word "error" in either order ("error 429", "429 error", * and the nested JSON body `{"error":{"code":429`). * * The gap between the word and the number is bounded and holds no digit, so * unrelated digits further along cannot bridge the match. This is the shape the * shared 5xx rule in `DEFAULT_ERROR_RULES` already uses, so the two stay in * step. It is a heuristic over text: a rule that can read the structured * status should still check `ctx.statusCode` first. */ export declare function messageNamesStatus(message: string, status: number): boolean; /** * Generic fallback rule table covering the five categories every * OpenAI-compatible provider already hand-rolled near-identically: * auth (401), rate limit (429), model-not-found, network/connection * errors, and 5xx server errors. Model-not-found is a 404 whose text names * the model or deployment as missing, or the old "model not found" message * text at any status; any other 404 stays a plain `ProviderError` carrying * the status and the vendor's message. Providers with a provider-specific * auth message (naming the exact env var) prepend one override rule and * spread this table after it — see errorClassifier usage in any migrated * provider's formatProviderError for the pattern. */ export declare const DEFAULT_ERROR_RULES: ProviderErrorRule[];