@agentxin-ai/plugin-context-editing 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +85 -0
  2. package/package.json +44 -0
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # AgentXin Plugin: Context Editing Middleware
2
+
3
+ `@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.
4
+
5
+ ## Key Features
6
+
7
+ - Clears older `ToolMessage` results once token/message/fraction triggers are met (default: 100,000 tokens).
8
+ - Keeps the most recent tool outputs by count, token budget, or fraction of model context (default: last 3 results).
9
+ - Optional input scrubbing for tool calls plus exclusion lists per tool name; inserts a placeholder for cleared results.
10
+ - Removes orphaned tool messages automatically and annotates cleared entries with `response_metadata.context_editing`.
11
+ - Supports fast approximate token counting or exact model-based counting when available (e.g., OpenAI `getNumTokensFromMessages`).
12
+ - Ships as a global NestJS module that plugs directly into the AgentXin agent middleware pipeline.
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ pnpm add @agentxin-ai/plugin-context-editing
18
+ # or
19
+ npm install @agentxin-ai/plugin-context-editing
20
+ ```
21
+
22
+ > **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.
23
+
24
+ ## Quick Start
25
+
26
+ 1. **Register the Plugin**
27
+ Start AgentXin with the package in your plugin list:
28
+ ```sh
29
+ PLUGINS=@agentxin-ai/plugin-context-editing
30
+ ```
31
+ The plugin registers the global `ContextEditingPlugin` module.
32
+ 2. **Enable the Middleware on an Agent**
33
+ In the AgentXin console (or agent definition), add a middleware entry with strategy `ContextEditingMiddleware` and provide options as needed.
34
+ 3. **Configure Triggers and Retention**
35
+ Example middleware block:
36
+ ```json
37
+ {
38
+ "type": "ContextEditingMiddleware",
39
+ "options": {
40
+ "trigger": { "tokens": 100000 },
41
+ "keep": { "messages": 3 },
42
+ "excludeTools": ["healthcheck"],
43
+ "placeholder": "[cleared]",
44
+ "clearToolInputs": false,
45
+ "tokenCountMethod": "approx"
46
+ }
47
+ }
48
+ ```
49
+ Swap `trigger` or `keep` to use `fraction` (of model context) or `tokens` depending on your needs.
50
+
51
+ ## Configuration
52
+
53
+ | Field | Type | Description | Default |
54
+ | ----- | ---- | ----------- | ------- |
55
+ | `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 }` |
56
+ | `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 }` |
57
+ | `excludeTools` | string[] | Tool names that should never be cleared. | `[]` |
58
+ | `placeholder` | string | Text inserted into cleared `ToolMessage` content. | `"[cleared]"` |
59
+ | `clearToolInputs` | boolean | Also clears the originating tool call parameters on the AI message. | `false` |
60
+ | `tokenCountMethod` | `"approx"` \| `"model"` | Token counting mode: `approx` is fast character-based; `model` calls the model's `getNumTokensFromMessages` when available. | `"approx"` |
61
+
62
+ > Tips
63
+ > - Use `trigger.fraction` with models that expose `profile.maxInputTokens` to keep pruning aligned with model limits.
64
+ > - Combine `excludeTools` with `keep.tokens` to protect critical tool outputs (e.g., auth checks) while trimming large artifacts.
65
+
66
+ ## Editing Behavior
67
+
68
+ - Before evaluating thresholds, orphaned tool messages (no matching AI tool call) are removed.
69
+ - When triggered, earlier tool results are replaced by the placeholder and tagged with `context_editing.strategy = "clear_tool_uses"`.
70
+ - If `clearToolInputs` is true, tool call args on the corresponding AI message are cleared and noted in `context_editing.cleared_tool_inputs`.
71
+ - System messages are included in token counting; approximate mode assumes ~4 characters per token.
72
+
73
+ ## Development & Testing
74
+
75
+ ```bash
76
+ npm install
77
+ npx nx build @agentxin-ai/plugin-context-editing
78
+ npx nx test @agentxin-ai/plugin-context-editing
79
+ ```
80
+
81
+ TypeScript artifacts emit to `packages/context-editing/dist`. Validate middleware behavior against a staging agent run before publishing.
82
+
83
+ ## License
84
+
85
+ This project follows the [AGPL-3.0 License](../../../LICENSE) located at the repository root.
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@agentxin-ai/plugin-context-editing",
3
+ "version": "0.0.1",
4
+ "author": {
5
+ "name": "AgentXinAI",
6
+ "url": "https://agentxinai.cn"
7
+ },
8
+ "license": "AGPL-3.0",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/agentxin-ai/agentxin-plugins.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/agentxin-ai/agentxin-plugins/issues"
15
+ },
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "module": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ "./package.json": "./package.json",
22
+ ".": {
23
+ "@agentxin-plugins-starter/source": "./src/index.ts",
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js",
26
+ "default": "./dist/index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "!**/*.tsbuildinfo"
32
+ ],
33
+ "dependencies": {
34
+ "tslib": "^2.3.0"
35
+ },
36
+ "peerDependencies": {
37
+ "zod": "3.25.67",
38
+ "@agentxin-ai/plugin-sdk": "^3.7.0",
39
+ "chalk": "4.1.2",
40
+ "@langchain/core": "0.3.72",
41
+ "@nestjs/common": "^11.1.6",
42
+ "@metad/contracts": "^3.7.0"
43
+ }
44
+ }