@lexwdex-org/opencode-dcp 3.2.0

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 (135) hide show
  1. package/LICENSE +619 -0
  2. package/README.md +234 -0
  3. package/dist/index.d.ts +4 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +7946 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/auth.d.ts +4 -0
  8. package/dist/lib/auth.d.ts.map +1 -0
  9. package/dist/lib/commands/compression-targets.d.ts +14 -0
  10. package/dist/lib/commands/compression-targets.d.ts.map +1 -0
  11. package/dist/lib/commands/context.d.ts +52 -0
  12. package/dist/lib/commands/context.d.ts.map +1 -0
  13. package/dist/lib/commands/decompress.d.ts +12 -0
  14. package/dist/lib/commands/decompress.d.ts.map +1 -0
  15. package/dist/lib/commands/help.d.ts +17 -0
  16. package/dist/lib/commands/help.d.ts.map +1 -0
  17. package/dist/lib/commands/index.d.ts +8 -0
  18. package/dist/lib/commands/index.d.ts.map +1 -0
  19. package/dist/lib/commands/manual.d.ts +23 -0
  20. package/dist/lib/commands/manual.d.ts.map +1 -0
  21. package/dist/lib/commands/recompress.d.ts +12 -0
  22. package/dist/lib/commands/recompress.d.ts.map +1 -0
  23. package/dist/lib/commands/stats.d.ts +15 -0
  24. package/dist/lib/commands/stats.d.ts.map +1 -0
  25. package/dist/lib/commands/sweep.d.ts +23 -0
  26. package/dist/lib/commands/sweep.d.ts.map +1 -0
  27. package/dist/lib/compress/index.d.ts +4 -0
  28. package/dist/lib/compress/index.d.ts.map +1 -0
  29. package/dist/lib/compress/message-utils.d.ts +8 -0
  30. package/dist/lib/compress/message-utils.d.ts.map +1 -0
  31. package/dist/lib/compress/message.d.ts +4 -0
  32. package/dist/lib/compress/message.d.ts.map +1 -0
  33. package/dist/lib/compress/pipeline.d.ts +29 -0
  34. package/dist/lib/compress/pipeline.d.ts.map +1 -0
  35. package/dist/lib/compress/protected-content.d.ts +7 -0
  36. package/dist/lib/compress/protected-content.d.ts.map +1 -0
  37. package/dist/lib/compress/range-utils.d.ts +10 -0
  38. package/dist/lib/compress/range-utils.d.ts.map +1 -0
  39. package/dist/lib/compress/range.d.ts +4 -0
  40. package/dist/lib/compress/range.d.ts.map +1 -0
  41. package/dist/lib/compress/search.d.ts +11 -0
  42. package/dist/lib/compress/search.d.ts.map +1 -0
  43. package/dist/lib/compress/state.d.ts +9 -0
  44. package/dist/lib/compress/state.d.ts.map +1 -0
  45. package/dist/lib/compress/timing.d.ts +18 -0
  46. package/dist/lib/compress/timing.d.ts.map +1 -0
  47. package/dist/lib/compress/types.d.ts +94 -0
  48. package/dist/lib/compress/types.d.ts.map +1 -0
  49. package/dist/lib/compress-permission.d.ts +6 -0
  50. package/dist/lib/compress-permission.d.ts.map +1 -0
  51. package/dist/lib/config.d.ts +72 -0
  52. package/dist/lib/config.d.ts.map +1 -0
  53. package/dist/lib/hooks.d.ts +36 -0
  54. package/dist/lib/hooks.d.ts.map +1 -0
  55. package/dist/lib/host-permissions.d.ts +11 -0
  56. package/dist/lib/host-permissions.d.ts.map +1 -0
  57. package/dist/lib/logger.d.ts +31 -0
  58. package/dist/lib/logger.d.ts.map +1 -0
  59. package/dist/lib/message-ids.d.ts +19 -0
  60. package/dist/lib/message-ids.d.ts.map +1 -0
  61. package/dist/lib/messages/index.d.ts +9 -0
  62. package/dist/lib/messages/index.d.ts.map +1 -0
  63. package/dist/lib/messages/inject/inject.d.ts +8 -0
  64. package/dist/lib/messages/inject/inject.d.ts.map +1 -0
  65. package/dist/lib/messages/inject/subagent-results.d.ts +4 -0
  66. package/dist/lib/messages/inject/subagent-results.d.ts.map +1 -0
  67. package/dist/lib/messages/inject/utils.d.ts +24 -0
  68. package/dist/lib/messages/inject/utils.d.ts.map +1 -0
  69. package/dist/lib/messages/priority.d.ts +13 -0
  70. package/dist/lib/messages/priority.d.ts.map +1 -0
  71. package/dist/lib/messages/prune.d.ts +5 -0
  72. package/dist/lib/messages/prune.d.ts.map +1 -0
  73. package/dist/lib/messages/query.d.ts +7 -0
  74. package/dist/lib/messages/query.d.ts.map +1 -0
  75. package/dist/lib/messages/reasoning-strip.d.ts +8 -0
  76. package/dist/lib/messages/reasoning-strip.d.ts.map +1 -0
  77. package/dist/lib/messages/shape.d.ts +5 -0
  78. package/dist/lib/messages/shape.d.ts.map +1 -0
  79. package/dist/lib/messages/sync.d.ts +4 -0
  80. package/dist/lib/messages/sync.d.ts.map +1 -0
  81. package/dist/lib/messages/utils.d.ts +27 -0
  82. package/dist/lib/messages/utils.d.ts.map +1 -0
  83. package/dist/lib/prompts/compress-message.d.ts +2 -0
  84. package/dist/lib/prompts/compress-message.d.ts.map +1 -0
  85. package/dist/lib/prompts/compress-range.d.ts +2 -0
  86. package/dist/lib/prompts/compress-range.d.ts.map +1 -0
  87. package/dist/lib/prompts/context-limit-nudge.d.ts +2 -0
  88. package/dist/lib/prompts/context-limit-nudge.d.ts.map +1 -0
  89. package/dist/lib/prompts/extensions/nudge.d.ts +5 -0
  90. package/dist/lib/prompts/extensions/nudge.d.ts.map +1 -0
  91. package/dist/lib/prompts/extensions/system.d.ts +4 -0
  92. package/dist/lib/prompts/extensions/system.d.ts.map +1 -0
  93. package/dist/lib/prompts/extensions/tool.d.ts +3 -0
  94. package/dist/lib/prompts/extensions/tool.d.ts.map +1 -0
  95. package/dist/lib/prompts/index.d.ts +4 -0
  96. package/dist/lib/prompts/index.d.ts.map +1 -0
  97. package/dist/lib/prompts/iteration-nudge.d.ts +2 -0
  98. package/dist/lib/prompts/iteration-nudge.d.ts.map +1 -0
  99. package/dist/lib/prompts/store.d.ts +25 -0
  100. package/dist/lib/prompts/store.d.ts.map +1 -0
  101. package/dist/lib/prompts/system.d.ts +2 -0
  102. package/dist/lib/prompts/system.d.ts.map +1 -0
  103. package/dist/lib/prompts/turn-nudge.d.ts +2 -0
  104. package/dist/lib/prompts/turn-nudge.d.ts.map +1 -0
  105. package/dist/lib/protected-patterns.d.ts +5 -0
  106. package/dist/lib/protected-patterns.d.ts.map +1 -0
  107. package/dist/lib/state/index.d.ts +5 -0
  108. package/dist/lib/state/index.d.ts.map +1 -0
  109. package/dist/lib/state/persistence.d.ts +49 -0
  110. package/dist/lib/state/persistence.d.ts.map +1 -0
  111. package/dist/lib/state/state.d.ts +7 -0
  112. package/dist/lib/state/state.d.ts.map +1 -0
  113. package/dist/lib/state/tool-cache.d.ts +13 -0
  114. package/dist/lib/state/tool-cache.d.ts.map +1 -0
  115. package/dist/lib/state/types.d.ts +99 -0
  116. package/dist/lib/state/types.d.ts.map +1 -0
  117. package/dist/lib/state/utils.d.ts +22 -0
  118. package/dist/lib/state/utils.d.ts.map +1 -0
  119. package/dist/lib/strategies/deduplication.d.ts +10 -0
  120. package/dist/lib/strategies/deduplication.d.ts.map +1 -0
  121. package/dist/lib/strategies/index.d.ts +3 -0
  122. package/dist/lib/strategies/index.d.ts.map +1 -0
  123. package/dist/lib/strategies/purge-errors.d.ts +13 -0
  124. package/dist/lib/strategies/purge-errors.d.ts.map +1 -0
  125. package/dist/lib/subagents/subagent-results.d.ts +5 -0
  126. package/dist/lib/subagents/subagent-results.d.ts.map +1 -0
  127. package/dist/lib/token-utils.d.ts +19 -0
  128. package/dist/lib/token-utils.d.ts.map +1 -0
  129. package/dist/lib/ui/notification.d.ts +17 -0
  130. package/dist/lib/ui/notification.d.ts.map +1 -0
  131. package/dist/lib/ui/utils.d.ts +10 -0
  132. package/dist/lib/ui/utils.d.ts.map +1 -0
  133. package/dist/lib/update.d.ts +22 -0
  134. package/dist/lib/update.d.ts.map +1 -0
  135. package/package.json +72 -0
package/README.md ADDED
@@ -0,0 +1,234 @@
1
+ # Dynamic Context Pruning Plugin
2
+
3
+ [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/dansmolsky)
4
+ [![npm version](https://img.shields.io/npm/v/@lexwdex-org/opencode-dcp.svg)](https://www.npmjs.com/package/@lexwdex-org/opencode-dcp)
5
+
6
+ Automatically reduces token usage in OpenCode by managing conversation context.
7
+
8
+ ![DCP in action](assets/images/dcp-demo9.png)
9
+
10
+ ## Installation
11
+
12
+ Install from the CLI:
13
+
14
+ ```bash
15
+ opencode plugin @lexwdex-org/opencode-dcp@latest --global
16
+ ```
17
+
18
+ This installs the package and adds it to your global OpenCode config.
19
+
20
+ ## How It Works
21
+
22
+ DCP reduces context size through a compress tool and automatic cleanup. Your session history is never modified — DCP replaces pruned content with placeholders before sending requests to your LLM.
23
+
24
+ ### Compress
25
+
26
+ Compress is a tool exposed to your model that replaces closed, stale conversation content with high-fidelity technical summaries. You can think of this as a much smarter version of Opencode's compaction process. Instead of triggering statically when your session reaches its maximum context and on the entire coding session, Compress allows the model to pick when to activate based on task completion, and to only compress the specific messages that are no longer needed verbatim.
27
+
28
+ DCP supports two compression modes:
29
+
30
+ - `range` mode compresses contiguous spans of conversation into one or more summaries.
31
+ - `message` mode (experimental) compresses individual raw messages independently, letting the model manage context much more surgically.
32
+
33
+ In `range` mode, when a new compression overlaps an earlier one, the earlier summary is nested inside the new one so information is preserved through layers of compression rather than diluted away. In both modes, protected tool outputs (such as subagents and skills) and protected file patterns are kept in compression summaries, ensuring that the most important information is never lost. You can also enable `protectUserMessages` to preserve your messages verbatim during compression, though note that large prompts (e.g. copy-pasting log files in the prompt) will then never be compressed away.
34
+
35
+ ### Deduplication
36
+
37
+ Identifies repeated tool calls (same tool, same arguments) and keeps only the most recent output. Recalculated when the compress tool runs, so prompt cache is only impacted alongside compression.
38
+
39
+ ### Purge Errors
40
+
41
+ Prunes inputs from errored tool calls after a configurable number of turns (default: 4). Error messages are preserved; only the potentially large input content is removed. Recalculated on compress tool use.
42
+
43
+ ## Configuration
44
+
45
+ DCP uses its own config file, searched in order:
46
+
47
+ 1. Global: `~/.config/opencode/dcp.jsonc` (or `dcp.json`), created automatically on first run
48
+ 2. Custom config directory: `$OPENCODE_CONFIG_DIR/dcp.jsonc` (or `dcp.json`), if `OPENCODE_CONFIG_DIR` is set
49
+ 3. Project: `.opencode/dcp.jsonc` (or `dcp.json`) in your project's `.opencode` directory
50
+
51
+ Each level overrides the previous, so project settings take priority over global. Restart OpenCode after making config changes.
52
+
53
+ > [!NOTE]
54
+ > If you use models with smaller context windows, such as GitHub Copilot models or local models, lower `compress.minContextLimit` and `compress.maxContextLimit` in your configuration to match the available context.
55
+
56
+ > [!IMPORTANT]
57
+ > Defaults are applied automatically. Expand this if you want to review or override settings.
58
+
59
+ <details>
60
+ <summary><strong>Default Configuration</strong> (click to expand)</summary>
61
+
62
+ ```jsonc
63
+ {
64
+ "$schema": "https://raw.githubusercontent.com/Opencode-DCP/opencode-dynamic-context-pruning/master/dcp.schema.json",
65
+ // Enable or disable the plugin
66
+ "enabled": true,
67
+ // Automatically update npm-installed DCP when a newer npm latest is available.
68
+ // Version-locked plugin specs are not updated.
69
+ "autoUpdate": true,
70
+ // Enable debug logging to ~/.config/opencode/logs/dcp/
71
+ "debug": false,
72
+ // Notification display: "off", "minimal", or "detailed"
73
+ "pruneNotification": "detailed",
74
+ // Notification type: "chat" (in-conversation) or "toast" (system toast)
75
+ "pruneNotificationType": "chat",
76
+ // Slash commands configuration
77
+ "commands": {
78
+ "enabled": true,
79
+ // Additional tools to protect from pruning via commands (e.g., /dcp sweep)
80
+ "protectedTools": [],
81
+ },
82
+ // Manual mode: disables autonomous context management,
83
+ // tools only run when explicitly triggered via /dcp commands
84
+ "manualMode": {
85
+ "enabled": false,
86
+ // When true, automatic cleanup (deduplication, purgeErrors)
87
+ // still runs even in manual mode
88
+ "automaticStrategies": true,
89
+ },
90
+ // Protect from pruning for <turns> message turns past tool invocation
91
+ "turnProtection": {
92
+ "enabled": false,
93
+ "turns": 4,
94
+ },
95
+ // Experimental settings
96
+ "experimental": {
97
+ // Allow DCP processing in subagent sessions
98
+ "allowSubAgents": false,
99
+ // Enable user-editable prompt overrides under dcp-prompts directories
100
+ // When false (default), prompt override files/directories are ignored
101
+ "customPrompts": false,
102
+ },
103
+ // Protect file operations from pruning via glob patterns
104
+ // Patterns match tool parameters.filePath (e.g. read/write/edit)
105
+ "protectedFilePatterns": [],
106
+ // Unified context compression tool and behavior settings
107
+ "compress": {
108
+ // Compression mode: "range" (compress spans into block summaries)
109
+ // or experimental "message" (compress individual raw messages)
110
+ "mode": "range",
111
+ // Permission mode: "allow" (no prompt), "ask" (prompt), "deny" (tool not registered)
112
+ "permission": "allow",
113
+ // Show compression content in a chat notification
114
+ "showCompression": false,
115
+ // Let active summary tokens extend the effective maxContextLimit
116
+ "summaryBuffer": true,
117
+ // Soft upper threshold: above this, DCP keeps injecting strong
118
+ // compression nudges (based on nudgeFrequency), so compression is
119
+ // much more likely. Accepts: number or "X%" of model context window.
120
+ "maxContextLimit": 100000,
121
+ // Soft lower threshold for reminder nudges: below this, turn/iteration
122
+ // reminders are off (compression less likely). At/above this, reminders
123
+ // are on. Accepts: number or "X%" of model context window.
124
+ "minContextLimit": 50000,
125
+ // Optional per-model override for maxContextLimit by providerID/modelID.
126
+ // If present, this wins over the global maxContextLimit.
127
+ // Accepts: number or "X%".
128
+ // Example:
129
+ // "modelMaxLimits": {
130
+ // "openai/gpt-5.3-codex": 120000,
131
+ // "anthropic/claude-sonnet-4.6": "80%"
132
+ // },
133
+ // Optional per-model override for minContextLimit.
134
+ // If present, this wins over the global minContextLimit.
135
+ // "modelMinLimits": {
136
+ // "openai/gpt-5.3-codex": 50000,
137
+ // "anthropic/claude-sonnet-4.6": "25%"
138
+ // },
139
+ // How often the context-limit nudge fires (1 = every fetch, 5 = every 5th)
140
+ "nudgeFrequency": 5,
141
+ // Start adding compression reminders after this many
142
+ // messages have happened since the last user message
143
+ "iterationNudgeThreshold": 15,
144
+ // Controls how likely compression is after user messages
145
+ // ("strong" = more likely, "soft" = less likely)
146
+ "nudgeForce": "soft",
147
+ // Tool names whose completed outputs are appended to the compression
148
+ "protectedTools": [],
149
+ // Preserve text wrapped in <protect>...</protect> when compressed
150
+ "protectTags": false,
151
+ // Preserve your messages during compression.
152
+ // Warning: large copy-pasted prompts will never be compressed away
153
+ "protectUserMessages": false,
154
+ },
155
+ // Automatic pruning strategies
156
+ "strategies": {
157
+ // Remove duplicate tool calls (same tool with same arguments)
158
+ "deduplication": {
159
+ "enabled": true,
160
+ // Additional tools to protect from pruning
161
+ "protectedTools": [],
162
+ },
163
+ // Prune tool inputs for errored tools after X turns
164
+ "purgeErrors": {
165
+ "enabled": true,
166
+ // Number of turns before errored tool inputs are pruned
167
+ "turns": 4,
168
+ // Additional tools to protect from pruning
169
+ "protectedTools": [],
170
+ },
171
+ },
172
+ }
173
+ ```
174
+
175
+ </details>
176
+
177
+ ### Commands
178
+
179
+ DCP provides a `/dcp` slash command:
180
+
181
+ - `/dcp` — Shows available DCP commands
182
+ - `/dcp context` — Shows a breakdown of your current session's token usage by category (system, user, assistant, tools, etc.) and how much has been saved through pruning.
183
+ - `/dcp stats` — Shows cumulative pruning statistics across all sessions.
184
+ - `/dcp sweep` — Prunes all tools since the last user message. Accepts an optional count: `/dcp sweep 10` prunes the last 10 tools. Respects `commands.protectedTools`.
185
+ - `/dcp manual [on|off]` — Toggle manual mode or set explicit state. When on, the AI will not autonomously use context management tools.
186
+ - `/dcp compress [focus]` — Trigger a single compress tool execution. Optional focus text directs what content to compress, following the active `compress.mode`.
187
+ - `/dcp decompress <n>` — Restore a specific active compression by ID (for example `/dcp decompress 2`). Running without an argument shows available compression IDs, token sizes, and topics.
188
+ - `/dcp recompress <n>` — Re-apply a user-decompressed compression by ID (for example `/dcp recompress 2`). Running without an argument shows recompressible IDs, token sizes, and topics.
189
+
190
+ ### Prompt Overrides
191
+
192
+ DCP exposes six editable prompts:
193
+
194
+ - `system`
195
+ - `compress-range`
196
+ - `compress-message`
197
+ - `context-limit-nudge`
198
+ - `turn-nudge`
199
+ - `iteration-nudge`
200
+
201
+ This feature is disabled by default. Set `experimental.customPrompts` to `true` in your DCP config to activate it.
202
+
203
+ When enabled, managed defaults are written to `~/.config/opencode/dcp-prompts/defaults/` as plain-text prompt files. A single `README.md` in that directory explains each prompt and how to create overrides.
204
+
205
+ To customize behavior, add a file with the same name under an overrides directory and edit it as plain text.
206
+
207
+ To reset an override, delete the matching file from your overrides directory.
208
+
209
+ ### Protected Tools
210
+
211
+ By default, these tools are always protected from pruning:
212
+ `task`, `skill`, `todowrite`, `todoread`, `compress`, `batch`, `plan_enter`, `plan_exit`, `write`, `edit`
213
+
214
+ The `protectedTools` arrays in `commands` and `strategies` add to this default list.
215
+
216
+ For the `compress` tool, `compress.protectedTools` ensures specific tool outputs are appended to the compressed summary. By default it includes `task`, `skill`, `todowrite`, and `todoread`.
217
+
218
+ ## Impact on Prompt Caching
219
+
220
+ LLM providers cache prompts based on exact prefix matching. When DCP prunes content, it changes messages, which invalidates cached prefixes from that point forward.
221
+
222
+ **Trade-off:** You lose some cache reads but gain token savings from reduced context size and fewer hallucinations from stale context. In most cases, especially in long sessions, the savings outweigh the cache miss cost.
223
+
224
+ > [!NOTE]
225
+ > In testing, cache hit rates were approximately 85% with DCP vs 90% without.
226
+
227
+ **No impact for:**
228
+
229
+ - **Request-based billing** — Providers like GitHub Copilot that charge per request, not tokens.
230
+ - **Uniform token pricing** — Providers like Cerebras that bill cached and uncached tokens at the same rate.
231
+
232
+ ## License
233
+
234
+ AGPL-3.0-or-later
@@ -0,0 +1,4 @@
1
+ import type { Plugin } from "@opencode-ai/plugin";
2
+ declare const server: Plugin;
3
+ export default server;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAqBjD,QAAA,MAAM,MAAM,EAAE,MAiHK,CAAA;AAEnB,eAAe,MAAM,CAAA"}