@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
60 lines (59 loc) • 2.76 kB
TypeScript
/**
* ContextCompactor
*
* Orchestrates multi-stage context reduction:
*
* Stage 1: Tool Output Pruning (cheapest -- no LLM call)
* Stage 2: File Read Deduplication (cheap -- no LLM call)
* Stage 3: LLM Summarization (expensive -- requires LLM call)
* Stage 4: Sliding Window Truncation (fallback -- no LLM call)
*/
import type { ChatMessage, ConversationMemoryConfig, CompactionResult, CompactionConfig, ContextCompactorDeps, ContextCompactRequest } from "../types/index.js";
export declare class ContextCompactor {
private config;
/**
* Injected decision caller and its tuning. Kept OUT of `CompactionConfig`
* deliberately: that type is `Required<>`-ed into `DEFAULT_CONFIG`, so a
* function member would need a default implementation, and the compactor
* must stay usable with no decision provider at all.
*/
private readonly deps;
constructor(config?: CompactionConfig, deps?: ContextCompactorDeps);
/**
* Run the multi-stage compaction pipeline until messages fit within budget.
*/
compact(messages: ChatMessage[], targetTokens: number, memoryConfig?: Partial<ConversationMemoryConfig>, requestId?: string, request?: ContextCompactRequest): Promise<CompactionResult>;
/**
* Stage 0 — drop the earlier messages this request provably does not need.
*
* Returns the surviving messages, or `null` when nothing changed: no
* decision provider, no known request, nothing eligible, or no confident
* drop. Never throws — a failure here must leave the positional stages to
* run exactly as they did before this stage existed.
*/
private runRelevanceStage;
/**
* Whether a generated summary may replace the messages it covers.
*
* Stage 3 accepts any non-empty string today, so a refusal, an error
* message or a truncated fragment silently destroys the conversation it was
* meant to preserve — and the originals are gone before anyone reads it.
*
* Returns true unless a decision model CONFIDENTLY rejects the summary,
* because rejecting one falls through to truncation, which loses strictly
* more than an imperfect summary does.
*/
private acceptSummary;
/**
* Stage 2 — collapse repeated reads of the same file to the last one.
* Returns the deduplicated messages, or `null` when the stage is disabled,
* already within budget, or found nothing to collapse.
*/
private runDeduplicateStage;
/**
* Stage 1 — shrink large tool outputs outside the protected recent window.
* Returns the pruned messages, or `null` when the stage is disabled,
* already within budget, or saved nothing worth keeping.
*/
private runPruneStage;
}