import { BaseMessage, UsageMetadata } from '@langchain/core/messages'; import type { TokenCounter } from '@/types/run'; import { ContentTypes, Providers } from '@/common'; import type { ContextPruningConfig } from '@/types/graph'; export type PruneMessagesFactoryParams = { provider?: Providers; maxTokens: number; startIndex: number; tokenCounter: TokenCounter; indexTokenCountMap: Record; thinkingEnabled?: boolean; /** * Optional position-based content degradation config. When `enabled: true`, * tool result messages outside the protected zone (system + last N assistant * turns + image content) are soft-trimmed (head + tail) once their * position-age exceeds `softTrimRatio`, then hard-cleared with a placeholder * once age exceeds `hardClearRatio`. Defaults disabled to preserve existing * behavior — opt in per deployment. */ contextPruningConfig?: ContextPruningConfig; }; export type PruneMessagesParams = { messages: BaseMessage[]; usageMetadata?: Partial; startType?: ReturnType; }; /** * Calculates the total tokens from a single usage object * * @param usage The usage metadata object containing token information * @returns An object containing the total input and output tokens */ export declare function calculateTotalTokens(usage: Partial): UsageMetadata; export type PruningResult = { context: BaseMessage[]; remainingContextTokens: number; messagesToRefine: BaseMessage[]; thinkingStartIndex?: number; }; export declare function getMessagesWithinTokenLimit({ messages: _messages, maxContextTokens, indexTokenCountMap, startType: _startType, thinkingEnabled, tokenCounter, thinkingStartIndex: _thinkingStartIndex, reasoningType, instructionTokens: _instructionTokens, }: { messages: BaseMessage[]; maxContextTokens: number; indexTokenCountMap: Record; startType?: string | string[]; thinkingEnabled?: boolean; tokenCounter: TokenCounter; thinkingStartIndex?: number; reasoningType?: ContentTypes.THINKING | ContentTypes.REASONING_CONTENT; /** * Token overhead for instructions (system message + tool schemas + summary) * that are NOT included in `messages`. When messages[0] is already a * SystemMessage the budget is deducted from its indexTokenCountMap entry * as before; otherwise this value is subtracted from the available budget. */ instructionTokens?: number; }): PruningResult; export declare function checkValidNumber(value: unknown): value is number; /** * Repairs orphan tool messages in a pruned context. Drops `ToolMessage`s whose * matching `AIMessage` was pruned, and strips orphan `tool_use` content blocks * (and `tool_calls` entries) from `AIMessage`s whose corresponding tool results * are missing. Without this, Anthropic / Bedrock / OpenAI reject the request * with a structural validation 400. * * Intended to run on the post-prune `context` array (not the pre-prune * `allMessages`). The dropped messages are returned so the caller can append * them to `messagesToRefine` for summarization fidelity. */ export declare function repairOrphanedToolMessages({ context, allMessages, tokenCounter, indexTokenCountMap, }: { context: BaseMessage[]; allMessages: BaseMessage[]; tokenCounter: TokenCounter; indexTokenCountMap: Record; }): { context: BaseMessage[]; reclaimedTokens: number; droppedOrphanCount: number; /** Messages removed from context during orphan repair. These should be * appended to `messagesToRefine` so that summarization can still see them * (e.g. a ToolMessage whose parent AI was pruned). */ droppedMessages: BaseMessage[]; }; /** * Lightweight structural cleanup: strips orphan tool_use blocks from AI messages * and drops orphan ToolMessages whose AI counterpart is missing. * * Unlike `repairOrphanedToolMessages`, this does NOT track tokens — it is * intended as a final safety net right before model invocation to prevent * Anthropic / Bedrock structural validation errors. * * Uses duck-typing instead of `getType()` because messages at this stage * may be plain objects (from LangGraph state serialization) rather than * proper BaseMessage class instances. * * Includes a fast-path: if every tool_call has a matching tool_result and * vice-versa, the original array is returned immediately with zero allocation. */ export declare function sanitizeOrphanToolBlocks(messages: BaseMessage[]): BaseMessage[]; /** * Pre-flight truncation: truncates oversized tool result string content before * the budget walker runs. Older results get smaller budgets (recency factor), * with a 200-char floor so even ancient entries keep diagnostic signal. Returns * the number of ToolMessages truncated. */ export declare function preFlightTruncateToolResults(params: { messages: BaseMessage[]; maxContextTokens: number; indexTokenCountMap: Record; tokenCounter: TokenCounter; }): number; /** * Pre-flight truncation: truncates oversized `tool_use` input fields in AI messages. * * Tool call inputs (arguments) can be very large — e.g., code evaluation payloads from * MCP tools like chrome-devtools. Since these tool calls have already been executed, * the model only needs a summary of what was called, not the full arguments. Truncating * them before pruning can prevent entire messages from being dropped. * * Uses 15% of the context window (in estimated characters, ~4 chars/token) as the * per-input cap, capped at 200K chars. * * @returns The number of AI messages that had tool_use inputs truncated. */ export declare function preFlightTruncateToolCallInputs(params: { messages: BaseMessage[]; maxContextTokens: number; indexTokenCountMap: Record; tokenCounter: TokenCounter; }): number; export declare function createPruneMessages(factoryParams: PruneMessagesFactoryParams): (params: PruneMessagesParams) => { context: BaseMessage[]; indexTokenCountMap: Record; messagesToRefine: BaseMessage[]; originalToolContent?: Map; }; /** Calculates context utilization as a percentage (0-100) */ export declare function getContextUtilization(indexTokenCountMap: Record, instructionTokens: number, maxContextTokens: number): number;