@nikcli-ai/plugin-dynamic-context-pruning 0.0.6

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.
@@ -0,0 +1,18 @@
1
+ import type { Plugin } from "@nikcli-ai/plugin";
2
+ /**
3
+ * Dynamic Context Pruning
4
+ *
5
+ * Reduces token consumption by truncating long tool outputs in older messages.
6
+ * The most recent `keepRecent` messages are never touched.
7
+ * Tool result parts older than that are truncated to `maxOutputLength` characters.
8
+ *
9
+ * Options:
10
+ * keepRecent — number of recent messages to leave untouched (default: 10)
11
+ * maxOutputLength — max chars for a single tool output before pruning (default: 2000)
12
+ * pruneThreshold — minimum total message count before pruning activates (default: 20)
13
+ */
14
+ export declare const DynamicContextPruningPlugin: Plugin;
15
+ declare const _default: {
16
+ server: Plugin;
17
+ };
18
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Dynamic Context Pruning
3
+ *
4
+ * Reduces token consumption by truncating long tool outputs in older messages.
5
+ * The most recent `keepRecent` messages are never touched.
6
+ * Tool result parts older than that are truncated to `maxOutputLength` characters.
7
+ *
8
+ * Options:
9
+ * keepRecent — number of recent messages to leave untouched (default: 10)
10
+ * maxOutputLength — max chars for a single tool output before pruning (default: 2000)
11
+ * pruneThreshold — minimum total message count before pruning activates (default: 20)
12
+ */
13
+ export const DynamicContextPruningPlugin = async (_input, options) => {
14
+ const keepRecent = options?.keepRecent ?? 10;
15
+ const maxOutputLength = options?.maxOutputLength ?? 2000;
16
+ const pruneThreshold = options?.pruneThreshold ?? 20;
17
+ return {
18
+ "experimental.chat.messages.transform": async (_input, output) => {
19
+ const { messages } = output;
20
+ if (messages.length < pruneThreshold)
21
+ return;
22
+ const cutoffIndex = Math.max(0, messages.length - keepRecent);
23
+ for (let i = 0; i < cutoffIndex; i++) {
24
+ const msg = messages[i];
25
+ for (const part of msg.parts) {
26
+ const p = part;
27
+ if (p.type === "tool") {
28
+ const state = p.state;
29
+ if (!state)
30
+ continue;
31
+ // Prune completed tool state output
32
+ if (state.status === "completed" && typeof state.output === "string") {
33
+ if (state.output.length > maxOutputLength) {
34
+ const pruned = state.output.length - maxOutputLength;
35
+ state.output =
36
+ state.output.slice(0, maxOutputLength) + `\n[...${pruned} chars pruned by dynamic-context-pruning]`;
37
+ }
38
+ }
39
+ // Prune error messages
40
+ if (state.status === "error" && typeof state.error === "string") {
41
+ if (state.error.length > maxOutputLength) {
42
+ const pruned = state.error.length - maxOutputLength;
43
+ state.error = state.error.slice(0, maxOutputLength) + `\n[...${pruned} chars pruned]`;
44
+ }
45
+ }
46
+ }
47
+ if (p.type === "text" && typeof p.text === "string") {
48
+ if (p.text.length > maxOutputLength * 2) {
49
+ const pruned = p.text.length - maxOutputLength * 2;
50
+ p.text =
51
+ p.text.slice(0, maxOutputLength * 2) + `\n[...${pruned} chars pruned by dynamic-context-pruning]`;
52
+ }
53
+ }
54
+ }
55
+ }
56
+ },
57
+ };
58
+ };
59
+ export default { server: DynamicContextPruningPlugin };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/package.json",
3
+ "name": "@nikcli-ai/plugin-dynamic-context-pruning",
4
+ "version": "0.0.6",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "description": "Optimize token usage by pruning obsolete tool outputs",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "publishConfig": {
15
+ "exports": {
16
+ ".": "./dist/index.js"
17
+ }
18
+ },
19
+ "scripts": {
20
+ "build": "tsc --project tsconfig.json"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "devDependencies": {
26
+ "@tsconfig/node22": "22.0.2",
27
+ "@types/node": "22.13.9",
28
+ "typescript": "5.8.2",
29
+ "@nikcli-ai/plugin": "0.0.6"
30
+ }
31
+ }