{"version":3,"file":"contextEditing.d.cts","names":["BaseMessage","TokenCounter","Promise","ContextEdit","ClearToolUsesEditConfig","ClearToolUsesEdit","Set","ContextEditingMiddlewareConfig","contextEditingMiddleware","__types_js10","AgentMiddleware"],"sources":["../../../src/agents/middleware/contextEditing.d.ts"],"sourcesContent":["/**\n * Context editing middleware.\n *\n * This middleware mirrors Anthropic's context editing capabilities by clearing\n * older tool results once the conversation grows beyond a configurable token\n * threshold. The implementation is intentionally model-agnostic so it can be used\n * with any LangChain chat model.\n */\nimport type { BaseMessage } from \"@langchain/core/messages\";\n/**\n * Function type for counting tokens in a sequence of messages.\n */\nexport type TokenCounter = (messages: BaseMessage[]) => number | Promise<number>;\n/**\n * Protocol describing a context editing strategy.\n *\n * Implement this interface to create custom strategies for managing\n * conversation context size. The `apply` method should modify the\n * messages array in-place and return the updated token count.\n *\n * @example\n * ```ts\n * import { SystemMessage } from \"langchain\";\n *\n * class RemoveOldSystemMessages implements ContextEdit {\n *   async apply({ tokens, messages, countTokens }) {\n *     // Remove old system messages if over limit\n *     if (tokens > 50000) {\n *       messages = messages.filter(SystemMessage.isInstance);\n *       return await countTokens(messages);\n *     }\n *     return tokens;\n *   }\n * }\n * ```\n */\nexport interface ContextEdit {\n    /**\n     * Apply an edit to the message list, returning the new token count.\n     *\n     * This method should:\n     * 1. Check if editing is needed based on `tokens` parameter\n     * 2. Modify the `messages` array in-place (if needed)\n     * 3. Return the new token count after modifications\n     *\n     * @param params - Parameters for the editing operation\n     * @returns The updated token count after applying edits\n     */\n    apply(params: {\n        /**\n         * Current token count of all messages\n         */\n        tokens: number;\n        /**\n         * Array of messages to potentially edit (modify in-place)\n         */\n        messages: BaseMessage[];\n        /**\n         * Function to count tokens in a message array\n         */\n        countTokens: TokenCounter;\n    }): number | Promise<number>;\n}\n/**\n * Configuration for clearing tool outputs when token limits are exceeded.\n */\nexport interface ClearToolUsesEditConfig {\n    /**\n     * Token count that triggers the edit.\n     * @default 100000\n     */\n    triggerTokens?: number;\n    /**\n     * Minimum number of tokens to reclaim when the edit runs.\n     * @default 0\n     */\n    clearAtLeast?: number;\n    /**\n     * Number of most recent tool results that must be preserved.\n     * @default 3\n     */\n    keep?: number;\n    /**\n     * Whether to clear the originating tool call parameters on the AI message.\n     * @default false\n     */\n    clearToolInputs?: boolean;\n    /**\n     * List of tool names to exclude from clearing.\n     * @default []\n     */\n    excludeTools?: string[];\n    /**\n     * Placeholder text inserted for cleared tool outputs.\n     * @default \"[cleared]\"\n     */\n    placeholder?: string;\n}\n/**\n * Strategy for clearing tool outputs when token limits are exceeded.\n *\n * This strategy mirrors Anthropic's `clear_tool_uses_20250919` behavior by\n * replacing older tool results with a placeholder text when the conversation\n * grows too large. It preserves the most recent tool results and can exclude\n * specific tools from being cleared.\n *\n * @example\n * ```ts\n * import { ClearToolUsesEdit } from \"langchain\";\n *\n * const edit = new ClearToolUsesEdit({\n *   triggerTokens: 100000,       // Start clearing at 100K tokens\n *   clearAtLeast: 0,             // Clear as much as needed\n *   keep: 3,                     // Always keep 3 most recent results\n *   excludeTools: [\"important\"], // Never clear \"important\" tool\n *   clearToolInputs: false,      // Keep tool call arguments\n *   placeholder: \"[cleared]\",    // Replacement text\n * });\n * ```\n */\nexport declare class ClearToolUsesEdit implements ContextEdit {\n    #private;\n    triggerTokens: number;\n    clearAtLeast: number;\n    keep: number;\n    clearToolInputs: boolean;\n    excludeTools: Set<string>;\n    placeholder: string;\n    constructor(config?: ClearToolUsesEditConfig);\n    apply(params: {\n        tokens: number;\n        messages: BaseMessage[];\n        countTokens: TokenCounter;\n    }): Promise<number>;\n}\n/**\n * Configuration for the Context Editing Middleware.\n */\nexport interface ContextEditingMiddlewareConfig {\n    /**\n     * Sequence of edit strategies to apply. Defaults to a single\n     * ClearToolUsesEdit mirroring Anthropic defaults.\n     */\n    edits?: ContextEdit[];\n    /**\n     * Whether to use approximate token counting (faster, less accurate)\n     * or exact counting implemented by the chat model (potentially slower, more accurate).\n     * Currently only OpenAI models support exact counting.\n     * @default \"approx\"\n     */\n    tokenCountMethod?: \"approx\" | \"model\";\n}\n/**\n * Middleware that automatically prunes tool results to manage context size.\n *\n * This middleware applies a sequence of edits when the total input token count\n * exceeds configured thresholds. By default, it uses the `ClearToolUsesEdit` strategy\n * which mirrors Anthropic's `clear_tool_uses_20250919` behaviour by clearing older\n * tool results once the conversation exceeds 100,000 tokens.\n *\n * ## Basic Usage\n *\n * Use the middleware with default settings to automatically manage context:\n *\n * @example Basic usage with defaults\n * ```ts\n * import { contextEditingMiddleware } from \"langchain\";\n * import { createAgent } from \"langchain\";\n *\n * const agent = createAgent({\n *   model: \"anthropic:claude-3-5-sonnet\",\n *   tools: [searchTool, calculatorTool],\n *   middleware: [\n *     contextEditingMiddleware(),\n *   ],\n * });\n * ```\n *\n * The default configuration:\n * - Triggers when context exceeds **100,000 tokens**\n * - Keeps the **3 most recent** tool results\n * - Uses **approximate token counting** (fast)\n * - Does not clear tool call arguments\n *\n * ## Custom Configuration\n *\n * Customize the clearing behavior with `ClearToolUsesEdit`:\n *\n * @example Custom ClearToolUsesEdit configuration\n * ```ts\n * import { contextEditingMiddleware, ClearToolUsesEdit } from \"langchain\";\n *\n * const agent = createAgent({\n *   model: \"anthropic:claude-3-5-sonnet\",\n *   tools: [searchTool, calculatorTool],\n *   middleware: [\n *     contextEditingMiddleware({\n *       edits: [\n *         new ClearToolUsesEdit({\n *           triggerTokens: 50000,      // Clear when exceeding 50K tokens\n *           clearAtLeast: 1000,         // Reclaim at least 1K tokens\n *           keep: 5,                    // Keep 5 most recent tool results\n *           excludeTools: [\"search\"],   // Never clear search results\n *           clearToolInputs: true,      // Also clear tool call arguments\n *         }),\n *       ],\n *       tokenCountMethod: \"approx\",     // Use approximate counting (or \"model\")\n *     }),\n *   ],\n * });\n * ```\n *\n * ## Custom Editing Strategies\n *\n * Implement your own context editing strategy by creating a class that\n * implements the `ContextEdit` interface:\n *\n * @example Custom editing strategy\n * ```ts\n * import { contextEditingMiddleware, type ContextEdit, type TokenCounter } from \"langchain\";\n * import type { BaseMessage } from \"@langchain/core/messages\";\n *\n * class CustomEdit implements ContextEdit {\n *   async apply(params: {\n *     tokens: number;\n *     messages: BaseMessage[];\n *     countTokens: TokenCounter;\n *   }): Promise<number> {\n *     // Implement your custom editing logic here\n *     // and apply it to the messages array, then\n *     // return the new token count after edits\n *     return countTokens(messages);\n *   }\n * }\n * ```\n *\n * @param config - Configuration options for the middleware\n * @returns A middleware instance that can be used with `createAgent`\n */\nexport declare function contextEditingMiddleware(config?: ContextEditingMiddlewareConfig): import(\"./types.js\").AgentMiddleware<undefined, undefined, any>;\n"],"mappings":";;;;;AAoCA;;;AAwBqBC,KAhDTA,YAAAA,GAgDSA,CAAAA,QAAAA,EAhDiBD,WAgDjBC,EAAAA,EAAAA,GAAAA,MAAAA,GAhD4CC,OAgD5CD,CAAAA,MAAAA,CAAAA;;AACG;AAKxB;AAsDA;;;;;;;;AAA6D;AAkB7D;AAqGA;;;;AAA+H;;;;;;UA3M9GE,WAAAA;;;;;;;;;;;;;;;;;;;;cAoBCH;;;;iBAIGC;eACJC;;;;;UAKAE,uBAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsDIC,iBAAAA,YAA6BF;;;;;;gBAMhCG;;uBAEOF;;;cAGPJ;iBACGC;MACbC;;;;;UAKSK,8BAAAA;;;;;UAKLJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgGYK,wBAAAA,UAAkCD,iCAA8B"}