@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
75 lines (74 loc) • 5.26 kB
TypeScript
/**
* Shared OpenAI chat-completions wire-format helpers used by providers that
* talk to an OpenAI-shaped /chat/completions endpoint (openai-compatible,
* litellm, groq, perplexity, xai, fireworks, togetherAi, cohere, cloudflare,
* huggingFace, llamaCpp, lmStudio, deepseek, nvidiaNim, and openAI itself).
*
* Everything in this module is provider-agnostic: pure functions that convert
* between NeuroLink-shaped values and the OpenAI wire format, plus the SSE
* parser + queue primitives a streaming provider needs. Provider classes own
* their own orchestration (executeStream + runStreamLoop) for now — that
* extraction is a follow-up PR.
*
* Nothing here imports from "ai" or "@ai-sdk/*". The whole point of this
* module is to be the native replacement for the AI SDK's OpenAI wrapper.
*/
import type { OpenAICompatBuildBodyArgs, OpenAICompatChatMessage, OpenAICompatChatRequest, OpenAICompatChatTool, OpenAICompatMessage, OpenAICompatMessageContent, OpenAICompatResponseFormat, OpenAICompatSSEResult, OpenAICompatToolChoiceWire, OpenAICompatUsage, OpenAICompatV3CallToolChoice, OpenAICompatV3CallTools, DeferredUsage, Tool } from "../types/index.js";
export declare const stripTrailingSlash: (s: string) => string;
/**
* Build a bijective original ↔ wire tool-name map. Returns undefined when
* every name is already wire-valid and unreserved (the common case — the
* wire then uses original names untouched and callers skip all mapping).
* Sanitized names that collide get a deterministic numeric suffix so the
* map stays invertible.
*
* `reservedWireNames` seeds the collision set with wire names already
* declared in the request — a mid-turn discovery refresh maps only the
* newly hydrated subset, and without seeding a hydrated name (sanitized OR
* wire-valid) could reuse a wire name an earlier tool already claimed,
* making the reverse mapping ambiguous.
*/
export declare const buildWireToolNameMaps: (names: readonly string[], reservedWireNames?: ReadonlySet<string>) => {
toWire: Map<string, string>;
fromWire: Map<string, string>;
} | undefined;
/**
* Estimate the input-token cost of a fully-built wire request (messages +
* tool definitions). Used by the per-request max_tokens fit against a
* RUNTIME-DISCOVERED context window — deliberately the same char-based
* estimator the budget pipeline uses, so both layers agree.
*
* Image parts are billed at the flat IMAGE_TOKEN_ESTIMATE, never by the length
* of their base64 data URL: a PDF rendered to 10 page images otherwise
* stringifies to ~1.2M "tokens" and is rejected before it is ever sent.
*/
export declare const estimateWireTokens: (messages: ReadonlyArray<OpenAICompatChatMessage>, tools: OpenAICompatChatTool[] | undefined, provider?: string) => number;
export declare const safeStringify: (value: unknown) => string;
export declare const stringifyToolInput: (input: unknown) => string;
export declare const stringifyToolOutput: (output: unknown) => string;
export declare const imageDataToURL: (data: unknown, mediaType?: string) => string | undefined;
export declare const convertContentForOpenAI: (content: unknown) => string | OpenAICompatMessageContent[];
export declare const messageBuilderToOpenAI: (messages: ReadonlyArray<OpenAICompatMessage>, toolNameToWire?: Map<string, string>, replayReasoning?: boolean) => OpenAICompatChatMessage[];
export declare const buildToolsForOpenAI: (tools: Record<string, Tool>, toolNameToWire?: Map<string, string>) => OpenAICompatChatTool[] | undefined;
export declare const v3ToolsToOpenAI: (tools: OpenAICompatV3CallTools | undefined, toolNameToWire?: Map<string, string>) => OpenAICompatChatTool[] | undefined;
export declare const v3ToolChoiceToOpenAI: (choice: OpenAICompatV3CallToolChoice, toolNameToWire?: Map<string, string>) => OpenAICompatToolChoiceWire | undefined;
export declare const v3ResponseFormatToOpenAI: (rf: {
type: "text" | "json";
schema?: Record<string, unknown>;
name?: string;
description?: string;
}) => OpenAICompatResponseFormat | undefined;
export declare const mapNeuroLinkToolChoice: (choice: unknown, toolNameToWire?: Map<string, string>) => OpenAICompatToolChoiceWire | undefined;
export declare const messagesContainJsonWord: (messages: ReadonlyArray<OpenAICompatChatMessage>) => boolean;
export declare const ensureJsonWordInBody: (body: OpenAICompatChatRequest) => OpenAICompatChatRequest;
export declare const requiresMaxCompletionTokens: (modelId: string) => boolean;
export declare const buildBody: (args: OpenAICompatBuildBodyArgs) => OpenAICompatChatRequest;
export declare const parseSSEStream: (body: ReadableStream<Uint8Array>, onTextDelta: (delta: string) => void, onReasoningDelta?: (delta: string) => void) => Promise<OpenAICompatSSEResult>;
export declare const buildAPIError: (url: string, body: OpenAICompatChatRequest, res: Response) => Promise<Error>;
export declare const createDeferredAnalytics: () => {
usagePromise: Promise<DeferredUsage>;
finishPromise: Promise<string>;
resolveUsage: (u: DeferredUsage) => void;
resolveFinish: (reason: string) => void;
};
export declare const mergeUsage: (a: OpenAICompatUsage | undefined, b: OpenAICompatUsage | undefined) => OpenAICompatUsage | undefined;