{"_id":"@agentxin-ai/plugin-context-editing","_rev":"2-77c582131d33995de733a523d7728d52","name":"@agentxin-ai/plugin-context-editing","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.1":{"name":"@agentxin-ai/plugin-context-editing","version":"0.0.1","author":{"url":"https://agentxinai.cn","name":"AgentXinAI"},"license":"AGPL-3.0","_id":"@agentxin-ai/plugin-context-editing@0.0.1","maintainers":[{"name":"agentxin-ai","email":"1304040880@qq.com"}],"homepage":"https://github.com/agentxin-ai/agentxin-plugins#readme","bugs":{"url":"https://github.com/agentxin-ai/agentxin-plugins/issues"},"dist":{"shasum":"24c987b5efa328aaa3221bcd579b5c8b7a12d248","tarball":"https://registry.npmjs.org/@agentxin-ai/plugin-context-editing/-/plugin-context-editing-0.0.1.tgz","fileCount":2,"integrity":"sha512-izTKeC6/esrfXIUuN6x5wEZ1azDK5JfQ28/HJLZdbKWx964F5m5wktj5iWAnTyhhZc+6EUhHs5gFSVYB7MmJJA==","signatures":[{"sig":"MEYCIQDPxsrvV5lSwUhUJ0VuFvpiUIsELAnR0+ivP14Skg0UHAIhAPM13rBC2Igm9NvxMw3qvx8ALZpOleNPSAeJeShlKSLY","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":5688},"main":"./dist/index.js","type":"module","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js","default":"./dist/index.js","@agentxin-plugins-starter/source":"./src/index.ts"},"./package.json":"./package.json"},"_npmUser":{"name":"agentxin-ai","email":"1304040880@qq.com"},"repository":{"url":"git+https://github.com/agentxin-ai/agentxin-plugins.git","type":"git"},"_npmVersion":"10.9.4","description":"`@agentxin-ai/plugin-context-editing` adds automatic context pruning to [AgentXin AI](https://github.com/agentxin-ai/agentxin) agents. The middleware wraps LangChain chat requests and mirrors Anthropic-style context editing: once the dialog grows past a t","directories":{},"_nodeVersion":"22.22.0","dependencies":{"tslib":"^2.3.0"},"_hasShrinkwrap":false,"peerDependencies":{"zod":"3.25.67","chalk":"4.1.2","@nestjs/common":"^11.1.6","@langchain/core":"0.3.72","@metad/contracts":"^3.7.0","@agentxin-ai/plugin-sdk":"^3.7.0"},"_npmOperationalInternal":{"tmp":"tmp/plugin-context-editing_0.0.1_1773917429673_0.8593456560063069","host":"s3://npm-registry-packages-npm-production"}},"0.0.2":{"name":"@agentxin-ai/plugin-context-editing","version":"0.0.2","license":"AGPL-3.0","repository":{"type":"git","url":"git+https://github.com/agentxin-ai/agentxin-plugins.git"},"bugs":{"url":"https://github.com/agentxin-ai/agentxin-plugins/issues"},"type":"module","main":"./dist/index.js","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{"./package.json":"./package.json",".":{"@agentxin-plugins-starter/source":"./src/index.ts","types":"./dist/index.d.ts","import":"./dist/index.js","default":"./dist/index.js"}},"dependencies":{"tslib":"^2.3.0"},"peerDependencies":{"zod":"3.25.67","@agentxin-ai/plugin-sdk":"^3.7.0","chalk":"4.1.2","@langchain/core":"0.3.72","@nestjs/common":"^11.1.6","@metad/contracts":"^3.7.0"},"_id":"@agentxin-ai/plugin-context-editing@0.0.2","description":"`@agentxin-ai/plugin-context-editing` adds automatic context pruning to [AgentXin AI](https://github.com/agentxin-ai/agentxin) agents. The middleware wraps LangChain chat requests and mirrors Anthropic-style context editing: once the dialog grows past a t","homepage":"https://github.com/agentxin-ai/agentxin-plugins#readme","_nodeVersion":"22.22.0","_npmVersion":"10.9.4","dist":{"integrity":"sha512-i/Wq2K4+EYw3T/a5/Ed9DNsUFebB58B+LsdOvXPG/xV3fcwSSODAdwWxWKKWlWixnAyJ1AC456T/+12pVyHJyQ==","shasum":"0c4ed8bb4f206e3f6c51e4951a62dbe72ef02b95","tarball":"https://registry.npmjs.org/@agentxin-ai/plugin-context-editing/-/plugin-context-editing-0.0.2.tgz","fileCount":17,"unpackedSize":52735,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIG1FVw8ZlbTOW37LQ4LK5mLP1WSuq19uo6tc56q/0kUPAiB6TJ7to48C3mYZSH/+dBuwvKT1VsFYipRLcoibPW98gg=="}]},"_npmUser":{"name":"agentxin-ai","email":"1304040880@qq.com"},"directories":{},"maintainers":[{"name":"agentxin-ai","email":"1304040880@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/plugin-context-editing_0.0.2_1775182308950_0.4309987970266931"},"_hasShrinkwrap":false}},"time":{"created":"2026-03-19T10:50:29.600Z","modified":"2026-04-03T02:11:49.207Z","0.0.1":"2026-03-19T10:50:29.810Z","0.0.2":"2026-04-03T02:11:49.095Z"},"bugs":{"url":"https://github.com/agentxin-ai/agentxin-plugins/issues"},"license":"AGPL-3.0","homepage":"https://github.com/agentxin-ai/agentxin-plugins#readme","repository":{"type":"git","url":"git+https://github.com/agentxin-ai/agentxin-plugins.git"},"description":"`@agentxin-ai/plugin-context-editing` adds automatic context pruning to [AgentXin AI](https://github.com/agentxin-ai/agentxin) agents. The middleware wraps LangChain chat requests and mirrors Anthropic-style context editing: once the dialog grows past a t","maintainers":[{"name":"agentxin-ai","email":"1304040880@qq.com"}],"readme":"# AgentXin Plugin: Context Editing Middleware\n\n`@agentxin-ai/plugin-context-editing` adds automatic context pruning to [AgentXin AI](https://github.com/agentxin-ai/agentxin) agents. The middleware wraps LangChain chat requests and mirrors Anthropic-style context editing: once the dialog grows past a threshold, older tool outputs are cleared or collapsed so the model stays within its input window.\n\n## Key Features\n\n- Clears older `ToolMessage` results once token/message/fraction triggers are met (default: 100,000 tokens).\n- Keeps the most recent tool outputs by count, token budget, or fraction of model context (default: last 3 results).\n- Optional input scrubbing for tool calls plus exclusion lists per tool name; inserts a placeholder for cleared results.\n- Removes orphaned tool messages automatically and annotates cleared entries with `response_metadata.context_editing`.\n- Supports fast approximate token counting or exact model-based counting when available (e.g., OpenAI `getNumTokensFromMessages`).\n- Ships as a global NestJS module that plugs directly into the AgentXin agent middleware pipeline.\n\n## Installation\n\n```bash\npnpm add @agentxin-ai/plugin-context-editing\n# or\nnpm install @agentxin-ai/plugin-context-editing\n```\n\n> **Note**: Ensure the host service already provides `@agentxin-ai/plugin-sdk`, `@nestjs/common@^11`, `@langchain/core@^0.3`, `zod`, and `chalk`. These are treated as peer/runtime dependencies.\n\n## Quick Start\n\n1. **Register the Plugin**  \n   Start AgentXin with the package in your plugin list:\n   ```sh\n   PLUGINS=@agentxin-ai/plugin-context-editing\n   ```\n   The plugin registers the global `ContextEditingPlugin` module.\n2. **Enable the Middleware on an Agent**  \n   In the AgentXin console (or agent definition), add a middleware entry with strategy `ContextEditingMiddleware` and provide options as needed.\n3. **Configure Triggers and Retention**  \n   Example middleware block:\n   ```json\n   {\n     \"type\": \"ContextEditingMiddleware\",\n     \"options\": {\n       \"trigger\": { \"tokens\": 100000 },\n       \"keep\": { \"messages\": 3 },\n       \"excludeTools\": [\"healthcheck\"],\n       \"placeholder\": \"[cleared]\",\n       \"clearToolInputs\": false,\n       \"tokenCountMethod\": \"approx\"\n     }\n   }\n   ```\n   Swap `trigger` or `keep` to use `fraction` (of model context) or `tokens` depending on your needs.\n\n## Configuration\n\n| Field | Type | Description | Default |\n| ----- | ---- | ----------- | ------- |\n| `trigger` | object | When to start clearing. Provide exactly one of:<br/>- `tokens`: number of tokens in the conversation.<br/>- `messages`: total message count.<br/>- `fraction`: fraction of the model max input tokens. | `{ \"tokens\": 100000 }` |\n| `keep` | object | How much to preserve once triggered. Provide exactly one of:<br/>- `messages`: keep the most recent N tool results.<br/>- `tokens`: keep tool results until this token budget is reached (counting from the newest).<br/>- `fraction`: keep tool results up to this fraction of model context. | `{ \"messages\": 3 }` |\n| `excludeTools` | string[] | Tool names that should never be cleared. | `[]` |\n| `placeholder` | string | Text inserted into cleared `ToolMessage` content. | `\"[cleared]\"` |\n| `clearToolInputs` | boolean | Also clears the originating tool call parameters on the AI message. | `false` |\n| `tokenCountMethod` | `\"approx\"` \\| `\"model\"` | Token counting mode: `approx` is fast character-based; `model` calls the model's `getNumTokensFromMessages` when available. | `\"approx\"` |\n\n> Tips  \n> - Use `trigger.fraction` with models that expose `profile.maxInputTokens` to keep pruning aligned with model limits.  \n> - Combine `excludeTools` with `keep.tokens` to protect critical tool outputs (e.g., auth checks) while trimming large artifacts.\n\n## Editing Behavior\n\n- Before evaluating thresholds, orphaned tool messages (no matching AI tool call) are removed.\n- When triggered, earlier tool results are replaced by the placeholder and tagged with `context_editing.strategy = \"clear_tool_uses\"`.\n- If `clearToolInputs` is true, tool call args on the corresponding AI message are cleared and noted in `context_editing.cleared_tool_inputs`.\n- System messages are included in token counting; approximate mode assumes ~4 characters per token.\n\n## Development & Testing\n\n```bash\nnpm install\nnpx nx build @agentxin-ai/plugin-context-editing\nnpx nx test @agentxin-ai/plugin-context-editing\n```\n\nTypeScript artifacts emit to `packages/context-editing/dist`. Validate middleware behavior against a staging agent run before publishing.\n\n## License\n\nThis project follows the [AGPL-3.0 License](../../../LICENSE) located at the repository root.\n","readmeFilename":"README.md"}