@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
48 lines (47 loc) • 2.41 kB
TypeScript
/**
* In-turn context guard for Anthropic-shaped agent loops.
*
* The direct Anthropic loop (`providers/anthropic/client.ts`) grows a
* `conversation` array on every step: an assistant message carrying `tool_use`
* blocks, then a user message carrying the matching `tool_result` blocks. It
* had no in-turn guard, so a long agentic run overflowed the window mid-loop
* and lost every completed step.
*
* Granularity differs from the OpenAI-compatible shape: there, each tool result
* is its own message; here, ONE user message carries every `tool_result` for a
* step. That makes the message the natural batch unit — dropping an assistant
* `tool_use` message together with its following `tool_result` message can
* never orphan a block, which Anthropic rejects outright.
*
* The reclaim POLICY lives in `loopGuardCore` and is shared with the other
* provider loops. This module owns only the shape mapping.
*/
import type { AnthropicGuardMessage, LoopGuardPlan } from "../types/index.js";
/** Marker left where dropped history used to be. */
export declare const ANTHROPIC_ELISION_NOTE = "[Earlier tool exchanges were removed to fit the context window.]";
/**
* Text-level preview used for an oversized `tool_result` payload. Exposed so
* the caller can apply it to its OWN block type — this module never rebuilds
* the caller's messages, which keeps it free of generic-spread assignability
* problems and of the double type assertions Critical Rule 14 forbids.
*/
export declare function previewAnthropicToolResultText(text: string): string;
/** True when this message carries `tool_result` blocks worth previewing. */
export declare function isAnthropicToolResultMessage(message: AnthropicGuardMessage): boolean;
/**
* Decide what to reclaim from an Anthropic-shaped agent loop.
*
* Returns `undefined` when the loop still fits — the caller must then leave its
* history byte-identical, because any rewrite invalidates the rolling
* `cache_control` prefix this path depends on.
*/
export declare function planAnthropicLoopReclaim(args: {
conversation: readonly AnthropicGuardMessage[];
availableInputTokens: number;
fixedOverheadTokens: number;
provider?: string;
observedPromptTokens?: number;
previousSentEstimate?: number;
onSentEstimate?: (tokens: number) => void;
observedDescribesCurrentPayload?: boolean;
}): LoopGuardPlan | undefined;