@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
81 lines (80 loc) • 3.83 kB
TypeScript
/**
* Message threads for the native Anthropic loop (beta, `message-threads-2026-08-12`).
*
* Without a thread every step of a turn resends the whole conversation. With
* one, the first step sends it with `thread: {type: "create"}` and each later
* step sends only the messages since the last response, plus the previous
* response id. The API keeps the conversation in memory for a few minutes and
* is best-effort about it, so the caller stays the source of truth: this
* module only decides, per step, whether the thread can be continued, and
* otherwise plans a `create` that sends everything again.
*
* A thread can be continued only when nothing the API fixed at creation has
* changed (model, tools, system, tool choice, thinking, output config and the
* beta set) and the messages already sent are untouched. Anything that
* rewrites history, such as a context reclaim, therefore starts a new thread
* by itself.
*/
import type { AnthropicThreadCarry, AnthropicThreadSession } from "../../types/index.js";
export declare const MESSAGE_THREADS_BETA = "message-threads-2026-08-12";
/**
* Record how a step's response ended. The API stores a thread turn only when
* the response ended normally; a refusal, a cut-off or an early end is resent
* in full by the next step.
*/
export declare function settleThread(session: AnthropicThreadSession, response: {
sawTerminalEvent: boolean;
messageId: string | undefined;
stopReason: string | undefined;
}): void;
/**
* Start the thread state for one turn.
*
* @param betaHeader the `anthropic-beta` value the client already sends; the
* thread beta is added to it, identically on every request of the thread.
*/
export declare function createAnthropicThreadSession(betaHeader: string | undefined, carried?: AnthropicThreadCarry): AnthropicThreadSession;
/** The API's `error.details.error_code` for a thread request that was refused, if there is one. */
export declare function threadErrorCode(error: unknown): string | undefined;
/**
* What to do after a thread request fails: send everything again as a new
* thread, send it without a thread at all, or neither (an ordinary error).
*/
export declare function threadRecovery(error: unknown): "recreate" | "unthreaded" | undefined;
/**
* The threads a NeuroLink instance has left behind for its sessions' next
* calls. In memory and per process only: another process simply creates a new
* thread, which is always correct. An entry is taken out by the call that
* continues it, because a response id can be continued once, so two concurrent
* calls of one session cannot both continue the same thread.
*/
export declare class AnthropicThreadStore {
private readonly entries;
private globalEpoch;
private readonly sessionEpochs;
private sweepTimer;
/**
* The identity of a thread's owner: who asked, in which session, with which
* credential and endpoint. A thread belongs to the key that created it, so
* the credential is part of the key (hashed; the key itself is never kept).
*/
static keyOf(parts: {
userId?: string;
sessionId: string;
credential: string;
baseURL?: string;
}): string;
take(key: string, now?: number): AnthropicThreadCarry | undefined;
/** The epoch a call must hand back to `put` for its thread to be kept. */
epochOf(sessionId: string): string;
put(key: string, sessionId: string, carry: AnthropicThreadCarry, epoch: string, now?: number): void;
/** Drop every entry that has expired. */
sweep(now?: number): void;
private armSweep;
/**
* Forget a session's thread, or every thread when no session is named, and
* fence any call still running from leaving one afterwards.
*/
forget(sessionId?: string): void;
get size(): number;
}