@lexwdex-org/opencode-dcp 3.3.3 → 3.4.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.
- package/README.en.md +290 -0
- package/README.md +112 -56
- package/dist/index.js +292 -27
- package/dist/index.js.map +1 -1
- package/dist/lib/compress/external-inference.d.ts +15 -0
- package/dist/lib/compress/external-inference.d.ts.map +1 -0
- package/dist/lib/compress/message-utils.d.ts.map +1 -1
- package/dist/lib/compress/message.d.ts.map +1 -1
- package/dist/lib/compress/range-utils.d.ts.map +1 -1
- package/dist/lib/compress/range.d.ts.map +1 -1
- package/dist/lib/compress/search.d.ts.map +1 -1
- package/dist/lib/compress/types.d.ts +2 -2
- package/dist/lib/compress/types.d.ts.map +1 -1
- package/dist/lib/config-env-override.d.ts +3 -0
- package/dist/lib/config-env-override.d.ts.map +1 -0
- package/dist/lib/config.d.ts +8 -0
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/prompts/extensions/tool.d.ts +4 -2
- package/dist/lib/prompts/extensions/tool.d.ts.map +1 -1
- package/dist/lib/state/state.d.ts.map +1 -1
- package/package.json +1 -1
package/README.en.md
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
# Dynamic Context Pruning Plugin
|
|
2
|
+
|
|
3
|
+
[](https://ko-fi.com/dansmolsky)
|
|
4
|
+
[](https://www.npmjs.com/package/@lexwdex-org/opencode-dcp)
|
|
5
|
+
|
|
6
|
+
[**中文文档**](./README.md) | **English**
|
|
7
|
+
|
|
8
|
+
Automatically reduces token usage in OpenCode by managing conversation context.
|
|
9
|
+
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Install from the CLI:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
opencode plugin @lexwdex-org/opencode-dcp@latest --global
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
This installs the package and adds it to your global OpenCode config.
|
|
21
|
+
|
|
22
|
+
## How It Works
|
|
23
|
+
|
|
24
|
+
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.
|
|
25
|
+
|
|
26
|
+
### Compress
|
|
27
|
+
|
|
28
|
+
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.
|
|
29
|
+
|
|
30
|
+
DCP supports two compression modes:
|
|
31
|
+
|
|
32
|
+
- `range` mode compresses contiguous spans of conversation into one or more summaries.
|
|
33
|
+
- `message` mode (experimental) compresses individual raw messages independently, letting the model manage context much more surgically.
|
|
34
|
+
|
|
35
|
+
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.
|
|
36
|
+
|
|
37
|
+
### Deduplication
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
### Purge Errors
|
|
42
|
+
|
|
43
|
+
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.
|
|
44
|
+
|
|
45
|
+
## Configuration
|
|
46
|
+
|
|
47
|
+
DCP uses its own config file, searched in order:
|
|
48
|
+
|
|
49
|
+
1. Global: `~/.config/opencode/dcp.jsonc` (or `dcp.json`), created automatically on first run
|
|
50
|
+
2. Custom config directory: `$OPENCODE_CONFIG_DIR/dcp.jsonc` (or `dcp.json`), if `OPENCODE_CONFIG_DIR` is set
|
|
51
|
+
3. Project: `.opencode/dcp.jsonc` (or `dcp.json`) in your project's `.opencode` directory
|
|
52
|
+
|
|
53
|
+
Each level overrides the previous, so project settings take priority over global. Restart OpenCode after making config changes.
|
|
54
|
+
|
|
55
|
+
> [!NOTE]
|
|
56
|
+
> 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.
|
|
57
|
+
|
|
58
|
+
> [!IMPORTANT]
|
|
59
|
+
> Defaults are applied automatically. Expand this if you want to review or override settings.
|
|
60
|
+
|
|
61
|
+
<details>
|
|
62
|
+
<summary><strong>Default Configuration</strong> (click to expand)</summary>
|
|
63
|
+
|
|
64
|
+
```jsonc
|
|
65
|
+
{
|
|
66
|
+
"$schema": "https://raw.githubusercontent.com/LeXwDeX/opencode-dynamic-context-pruning/master/dcp.schema.json",
|
|
67
|
+
// Enable or disable the plugin
|
|
68
|
+
"enabled": true,
|
|
69
|
+
// Automatically update npm-installed DCP when a newer npm latest is available.
|
|
70
|
+
// Version-locked plugin specs are not updated.
|
|
71
|
+
"autoUpdate": true,
|
|
72
|
+
// Enable debug logging to ~/.config/opencode/logs/dcp/
|
|
73
|
+
"debug": false,
|
|
74
|
+
// Notification display: "off", "minimal", or "detailed"
|
|
75
|
+
"pruneNotification": "detailed",
|
|
76
|
+
// Notification type: "chat" (in-conversation) or "toast" (system toast)
|
|
77
|
+
"pruneNotificationType": "chat",
|
|
78
|
+
// Slash commands configuration
|
|
79
|
+
"commands": {
|
|
80
|
+
"enabled": true,
|
|
81
|
+
// Additional tools to protect from pruning via commands (e.g., /dcp sweep)
|
|
82
|
+
"protectedTools": [],
|
|
83
|
+
},
|
|
84
|
+
// Manual mode: disables autonomous context management,
|
|
85
|
+
// tools only run when explicitly triggered via /dcp commands
|
|
86
|
+
"manualMode": {
|
|
87
|
+
"enabled": false,
|
|
88
|
+
// When true, automatic cleanup (deduplication, purgeErrors)
|
|
89
|
+
// still runs even in manual mode
|
|
90
|
+
"automaticStrategies": true,
|
|
91
|
+
},
|
|
92
|
+
// Protect from pruning for <turns> message turns past tool invocation
|
|
93
|
+
"turnProtection": {
|
|
94
|
+
"enabled": false,
|
|
95
|
+
"turns": 4,
|
|
96
|
+
},
|
|
97
|
+
// Experimental settings
|
|
98
|
+
"experimental": {
|
|
99
|
+
// Allow DCP processing in subagent sessions
|
|
100
|
+
"allowSubAgents": false,
|
|
101
|
+
// Enable user-editable prompt overrides under dcp-prompts directories
|
|
102
|
+
// When false (default), prompt override files/directories are ignored
|
|
103
|
+
"customPrompts": false,
|
|
104
|
+
},
|
|
105
|
+
// Protect file operations from pruning via glob patterns
|
|
106
|
+
// Patterns match tool parameters.filePath (e.g. read/write/edit)
|
|
107
|
+
"protectedFilePatterns": [],
|
|
108
|
+
// Unified context compression tool and behavior settings
|
|
109
|
+
"compress": {
|
|
110
|
+
// Compression mode: "range" (compress spans into block summaries)
|
|
111
|
+
// or experimental "message" (compress individual raw messages)
|
|
112
|
+
"mode": "range",
|
|
113
|
+
// Permission mode: "allow" (no prompt), "ask" (prompt), "deny" (tool not registered)
|
|
114
|
+
"permission": "allow",
|
|
115
|
+
// Show compression content in a chat notification
|
|
116
|
+
"showCompression": false,
|
|
117
|
+
// Let active summary tokens extend the effective maxContextLimit
|
|
118
|
+
"summaryBuffer": true,
|
|
119
|
+
// Soft upper threshold: above this, DCP keeps injecting strong
|
|
120
|
+
// compression nudges (based on nudgeFrequency), so compression is
|
|
121
|
+
// much more likely. Accepts: number or "X%" of model context window.
|
|
122
|
+
"maxContextLimit": "85%",
|
|
123
|
+
// Soft lower threshold for reminder nudges: below this, turn/iteration
|
|
124
|
+
// reminders are off (compression less likely). At/above this, reminders
|
|
125
|
+
// are on. Accepts: number or "X%" of model context window.
|
|
126
|
+
"minContextLimit": "50%",
|
|
127
|
+
// Optional per-model override for maxContextLimit by providerID/modelID.
|
|
128
|
+
// If present, this wins over the global maxContextLimit.
|
|
129
|
+
// Accepts: number or "X%".
|
|
130
|
+
// Example:
|
|
131
|
+
// "modelMaxLimits": {
|
|
132
|
+
// "openai/gpt-5.3-codex": 120000,
|
|
133
|
+
// "anthropic/claude-sonnet-4.6": "80%"
|
|
134
|
+
// },
|
|
135
|
+
// Optional per-model override for minContextLimit.
|
|
136
|
+
// If present, this wins over the global minContextLimit.
|
|
137
|
+
// "modelMinLimits": {
|
|
138
|
+
// "openai/gpt-5.3-codex": 50000,
|
|
139
|
+
// "anthropic/claude-sonnet-4.6": "25%"
|
|
140
|
+
// },
|
|
141
|
+
// How often the context-limit nudge fires (1 = every fetch, 2 = every 2nd)
|
|
142
|
+
"nudgeFrequency": 2,
|
|
143
|
+
// Start adding compression reminders after this many
|
|
144
|
+
// messages have happened since the last user message
|
|
145
|
+
"iterationNudgeThreshold": 15,
|
|
146
|
+
// Controls how likely compression is after user messages
|
|
147
|
+
// ("strong" = more likely, "soft" = less likely)
|
|
148
|
+
"nudgeForce": "strong",
|
|
149
|
+
// Tool names whose completed outputs are appended to the compression
|
|
150
|
+
"protectedTools": [],
|
|
151
|
+
// Preserve text wrapped in <protect>...</protect> when compressed
|
|
152
|
+
"protectTags": false,
|
|
153
|
+
// Preserve your messages during compression.
|
|
154
|
+
// Warning: large copy-pasted prompts will never be compressed away
|
|
155
|
+
"protectUserMessages": false,
|
|
156
|
+
},
|
|
157
|
+
// Automatic pruning strategies
|
|
158
|
+
"strategies": {
|
|
159
|
+
// Remove duplicate tool calls (same tool with same arguments)
|
|
160
|
+
"deduplication": {
|
|
161
|
+
"enabled": true,
|
|
162
|
+
// Additional tools to protect from pruning
|
|
163
|
+
"protectedTools": [],
|
|
164
|
+
},
|
|
165
|
+
// Prune tool inputs for errored tools after X turns
|
|
166
|
+
"purgeErrors": {
|
|
167
|
+
"enabled": true,
|
|
168
|
+
// Number of turns before errored tool inputs are pruned
|
|
169
|
+
"turns": 4,
|
|
170
|
+
// Additional tools to protect from pruning
|
|
171
|
+
"protectedTools": [],
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
</details>
|
|
178
|
+
|
|
179
|
+
### Commands
|
|
180
|
+
|
|
181
|
+
DCP provides a `/dcp` slash command:
|
|
182
|
+
|
|
183
|
+
- `/dcp` — Shows available DCP commands
|
|
184
|
+
- `/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.
|
|
185
|
+
- `/dcp stats` — Shows cumulative pruning statistics across all sessions.
|
|
186
|
+
- `/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`.
|
|
187
|
+
- `/dcp manual [on|off]` — Toggle manual mode or set explicit state. When on, the AI will not autonomously use context management tools.
|
|
188
|
+
- `/dcp compress [focus]` — Trigger a single compress tool execution. Optional focus text directs what content to compress, following the active `compress.mode`.
|
|
189
|
+
- `/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.
|
|
190
|
+
- `/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.
|
|
191
|
+
|
|
192
|
+
### Prompt Overrides
|
|
193
|
+
|
|
194
|
+
DCP exposes six editable prompts:
|
|
195
|
+
|
|
196
|
+
- `system`
|
|
197
|
+
- `compress-range`
|
|
198
|
+
- `compress-message`
|
|
199
|
+
- `context-limit-nudge`
|
|
200
|
+
- `turn-nudge`
|
|
201
|
+
- `iteration-nudge`
|
|
202
|
+
|
|
203
|
+
This feature is disabled by default. Set `experimental.customPrompts` to `true` in your DCP config to activate it.
|
|
204
|
+
|
|
205
|
+
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.
|
|
206
|
+
|
|
207
|
+
To customize behavior, add a file with the same name under an overrides directory and edit it as plain text.
|
|
208
|
+
|
|
209
|
+
To reset an override, delete the matching file from your overrides directory.
|
|
210
|
+
|
|
211
|
+
### Protected Tools
|
|
212
|
+
|
|
213
|
+
By default, these tools are always protected from pruning:
|
|
214
|
+
`task`, `skill`, `todowrite`, `todoread`, `compress`, `batch`, `plan_enter`, `plan_exit`, `write`, `edit`
|
|
215
|
+
|
|
216
|
+
The `protectedTools` arrays in `commands` and `strategies` add to this default list.
|
|
217
|
+
|
|
218
|
+
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`.
|
|
219
|
+
|
|
220
|
+
### External Model Compression
|
|
221
|
+
|
|
222
|
+
Offload compression summaries to a cheaper model instead of using the expensive main model.
|
|
223
|
+
|
|
224
|
+
#### Priority Chain
|
|
225
|
+
|
|
226
|
+
| Priority | Source | Description |
|
|
227
|
+
| ------------ | --------------------- | ------------------------------------------------------ |
|
|
228
|
+
| 1 (highest) | Environment variables | `OPENCODE_DCP_EXTERNAL_COMPRESS_*` |
|
|
229
|
+
| 2 | Project config | `<project>/.opencode/dcp.jsonc:compress.externalModel` |
|
|
230
|
+
| 3 | Global config | `~/.config/opencode/dcp.jsonc:compress.externalModel` |
|
|
231
|
+
| 4 (fallback) | Main model | Original behavior unchanged |
|
|
232
|
+
|
|
233
|
+
#### Usage
|
|
234
|
+
|
|
235
|
+
**Method 1 — Environment variables (recommended for quick setup):**
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_URL="http://your-proxy-url/v1"
|
|
239
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_MODEL="your-model-name"
|
|
240
|
+
# Optional:
|
|
241
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_KEY="your-api-key"
|
|
242
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_TIMEOUT="120000"
|
|
243
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_RETRIES="1"
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Persist in shell RC (`~/.zshrc`, `~/.bashrc`) for automatic availability across sessions.
|
|
247
|
+
|
|
248
|
+
**Method 2 — Config file (`dcp.jsonc`):**
|
|
249
|
+
|
|
250
|
+
```jsonc
|
|
251
|
+
{
|
|
252
|
+
"compress": {
|
|
253
|
+
"externalModel": {
|
|
254
|
+
"url": "http://your-proxy-url/v1",
|
|
255
|
+
"model": "your-model-name",
|
|
256
|
+
"apiKey": "your-api-key",
|
|
257
|
+
"timeout": 120000,
|
|
258
|
+
"retries": 1,
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
#### Behavior
|
|
265
|
+
|
|
266
|
+
| Scenario | Behavior |
|
|
267
|
+
| ---------------------------------------------- | ------------------------------------------------------------------------------ |
|
|
268
|
+
| External model configured + `summary` omitted | Plugin fetches range content → calls external model → stores generated summary |
|
|
269
|
+
| External model configured + `summary` provided | Plugin uses provided `summary` directly (backward compatible) |
|
|
270
|
+
| External model call fails | Tool throws error; main model retries with fallback summary |
|
|
271
|
+
| Not configured | Everything unchanged — main model writes summaries as before |
|
|
272
|
+
| `url` is not OpenAI-compatible | External model call fails → falls to error path above |
|
|
273
|
+
|
|
274
|
+
**Compatibility:** Only OpenAI-compatible `/chat/completions` endpoints are supported (local proxies, OpenAI, DeepSeek, etc.).
|
|
275
|
+
|
|
276
|
+
## Impact on Prompt Caching
|
|
277
|
+
|
|
278
|
+
LLM providers cache prompts based on exact prefix matching. When DCP prunes content, it changes messages, which invalidates cached prefixes from that point forward.
|
|
279
|
+
|
|
280
|
+
**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.
|
|
281
|
+
|
|
282
|
+
> [!NOTE]
|
|
283
|
+
> In testing, cache hit rates were approximately 85% with DCP vs 90% without.
|
|
284
|
+
|
|
285
|
+
**No impact for:**
|
|
286
|
+
|
|
287
|
+
- **Request-based billing** — Providers like GitHub Copilot that charge per request, not tokens.
|
|
288
|
+
- **Uniform token pricing** — Providers like Cerebras that bill cached and uncached tokens at the same rate.
|
|
289
|
+
|
|
290
|
+
## License
|
package/README.md
CHANGED
|
@@ -1,63 +1,65 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 动态上下文剪枝插件
|
|
2
2
|
|
|
3
3
|
[](https://ko-fi.com/dansmolsky)
|
|
4
4
|
[](https://www.npmjs.com/package/@lexwdex-org/opencode-dcp)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
[**English**](./README.en.md) | **中文**
|
|
7
|
+
|
|
8
|
+
通过管理对话上下文,自动减少 OpenCode 中的 token 消耗。
|
|
7
9
|
|
|
8
10
|

|
|
9
11
|
|
|
10
|
-
##
|
|
12
|
+
## 安装
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
从 CLI 安装:
|
|
13
15
|
|
|
14
16
|
```bash
|
|
15
17
|
opencode plugin @lexwdex-org/opencode-dcp@latest --global
|
|
16
18
|
```
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
这会安装该包并将其添加到全局 OpenCode 配置中。
|
|
19
21
|
|
|
20
|
-
##
|
|
22
|
+
## 工作原理
|
|
21
23
|
|
|
22
|
-
DCP
|
|
24
|
+
DCP 通过压缩工具和自动清理来减小上下文大小。会话历史记录不会被修改——DCP 会在向 LLM 发送请求之前用占位符替换已剪枝的内容。
|
|
23
25
|
|
|
24
|
-
###
|
|
26
|
+
### 压缩
|
|
25
27
|
|
|
26
|
-
Compress
|
|
28
|
+
Compress 是一个暴露给模型的工具,它会将已关闭的、过时的对话内容替换为高保真度的技术摘要。你可以将其视为 OpenCode 原生合并过程的更智能版本。它不是在你的会话达到最大上下文时才静态触发并对整个编码会话生效,而是允许模型根据任务完成情况自主选择何时激活,并且只压缩那些不再需要逐字保留的特定消息。
|
|
27
29
|
|
|
28
|
-
DCP
|
|
30
|
+
DCP 支持两种压缩模式:
|
|
29
31
|
|
|
30
|
-
- `range`
|
|
31
|
-
- `message`
|
|
32
|
+
- `range` 模式将连续的多段对话压缩为一个或多个摘要。
|
|
33
|
+
- `message` 模式(实验性)独立压缩单条原始消息,让模型能够更精细地管理上下文。
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
在 `range` 模式下,当新的压缩与先前的压缩重叠时,先前的摘要会被嵌套到新的摘要中,这样信息在多层压缩中得到保留而非被稀释。在两种模式下,受保护的工具输出(如子 agent 和技能)以及受保护的文件模式都会保留在压缩摘要中,确保最重要的信息永不会丢失。你还可以启用 `protectUserMessages` 来在压缩期间保留你的消息原样,但请注意,大型提示(例如在提示中粘贴日志文件)将永远不会被压缩掉。
|
|
34
36
|
|
|
35
|
-
###
|
|
37
|
+
### 去重
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
识别重复的工具调用(相同工具、相同参数),只保留最近一次的输出。在压缩工具运行时重新计算,因此提示缓存仅在与压缩同时发生时受到影响。
|
|
38
40
|
|
|
39
|
-
###
|
|
41
|
+
### 清除错误
|
|
40
42
|
|
|
41
|
-
|
|
43
|
+
在可配置的消息轮次后(默认:4 轮),修剪出错工具调用的输入。错误消息会被保留,仅移除可能较大的输入内容。在使用压缩工具时重新计算。
|
|
42
44
|
|
|
43
|
-
##
|
|
45
|
+
## 配置
|
|
44
46
|
|
|
45
|
-
DCP
|
|
47
|
+
DCP 使用自己的配置文件,按以下顺序搜索:
|
|
46
48
|
|
|
47
|
-
1.
|
|
48
|
-
2.
|
|
49
|
-
3.
|
|
49
|
+
1. 全局:`~/.config/opencode/dcp.jsonc`(或 `dcp.json`),首次运行时自动创建
|
|
50
|
+
2. 自定义配置目录:`$OPENCODE_CONFIG_DIR/dcp.jsonc`(或 `dcp.json`),如果设置了 `OPENCODE_CONFIG_DIR`
|
|
51
|
+
3. 项目:`.opencode/dcp.jsonc`(或 `dcp.json`),位于项目的 `.opencode` 目录中
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
每个层级覆盖前一个层级,因此项目设置优先于全局设置。修改配置后请重启 OpenCode。
|
|
52
54
|
|
|
53
55
|
> [!NOTE]
|
|
54
|
-
>
|
|
56
|
+
> 如果你使用的是较小上下文窗口的模型,例如 GitHub Copilot 模型或本地模型,请在配置中降低 `compress.minContextLimit` 和 `compress.maxContextLimit` 以匹配可用上下文。
|
|
55
57
|
|
|
56
58
|
> [!IMPORTANT]
|
|
57
|
-
>
|
|
59
|
+
> 默认值会自动应用。展开此项可查看或覆盖设置。
|
|
58
60
|
|
|
59
61
|
<details>
|
|
60
|
-
<summary><strong
|
|
62
|
+
<summary><strong>默认配置</strong>(点击展开)</summary>
|
|
61
63
|
|
|
62
64
|
```jsonc
|
|
63
65
|
{
|
|
@@ -174,22 +176,22 @@ Each level overrides the previous, so project settings take priority over global
|
|
|
174
176
|
|
|
175
177
|
</details>
|
|
176
178
|
|
|
177
|
-
###
|
|
179
|
+
### 命令
|
|
178
180
|
|
|
179
|
-
DCP
|
|
181
|
+
DCP 提供 `/dcp` 斜杠命令:
|
|
180
182
|
|
|
181
|
-
- `/dcp` —
|
|
182
|
-
- `/dcp context` —
|
|
183
|
-
- `/dcp stats` —
|
|
184
|
-
- `/dcp sweep` —
|
|
185
|
-
- `/dcp manual [on|off]` —
|
|
186
|
-
- `/dcp compress [focus]` —
|
|
187
|
-
- `/dcp decompress <n>` —
|
|
188
|
-
- `/dcp recompress <n>` —
|
|
183
|
+
- `/dcp` — 显示可用的 DCP 命令
|
|
184
|
+
- `/dcp context` — 显示当前会话按类别(system、user、assistant、tools 等)划分的 token 使用明细以及通过剪枝节省的量。
|
|
185
|
+
- `/dcp stats` — 显示跨所有会话的累积剪枝统计信息。
|
|
186
|
+
- `/dcp sweep` — 修剪自上次用户消息以来的所有工具。接受可选数量:`/dcp sweep 10` 修剪最后 10 个工具。遵循 `commands.protectedTools`。
|
|
187
|
+
- `/dcp manual [on|off]` — 切换手动模式或设置显式状态。开启时,AI 不会自主使用上下文管理工具。
|
|
188
|
+
- `/dcp compress [focus]` — 触发一次压缩工具执行。可选的 focus 文本指示要压缩的内容,遵循当前的 `compress.mode`。
|
|
189
|
+
- `/dcp decompress <n>` — 按 ID 恢复特定的活跃压缩(例如 `/dcp decompress 2`)。不加参数运行时显示可用的压缩 ID、token 大小和主题。
|
|
190
|
+
- `/dcp recompress <n>` — 按 ID 重新压缩用户已解压的压缩(例如 `/dcp recompress 2`)。不加参数运行时显示可重新压缩的 ID、token 大小和主题。
|
|
189
191
|
|
|
190
|
-
###
|
|
192
|
+
### 提示词覆盖
|
|
191
193
|
|
|
192
|
-
DCP
|
|
194
|
+
DCP 暴露了六个可编辑的提示词:
|
|
193
195
|
|
|
194
196
|
- `system`
|
|
195
197
|
- `compress-range`
|
|
@@ -198,37 +200,91 @@ DCP exposes six editable prompts:
|
|
|
198
200
|
- `turn-nudge`
|
|
199
201
|
- `iteration-nudge`
|
|
200
202
|
|
|
201
|
-
|
|
203
|
+
此功能默认禁用。在你的 DCP 配置中将 `experimental.customPrompts` 设置为 `true` 来激活它。
|
|
204
|
+
|
|
205
|
+
启用后,管理的默认值会以纯文本提示词文件的形式写入 `~/.config/opencode/dcp-prompts/defaults/`。该目录中的 `README.md` 文件会解释每个提示词以及如何创建覆盖。
|
|
206
|
+
|
|
207
|
+
如需自定义行为,请在覆盖目录下添加同名文件并编辑为纯文本。
|
|
208
|
+
|
|
209
|
+
要重置覆盖,请从覆盖目录中删除对应的文件。
|
|
210
|
+
|
|
211
|
+
### 受保护工具
|
|
212
|
+
|
|
213
|
+
默认情况下,以下工具始终受保护不被修剪:
|
|
214
|
+
`task`、`skill`、`todowrite`、`todoread`、`compress`、`batch`、`plan_enter`、`plan_exit`、`write`、`edit`
|
|
215
|
+
|
|
216
|
+
`commands` 和 `strategies` 中的 `protectedTools` 数组会追加到这个默认列表。
|
|
217
|
+
|
|
218
|
+
对于 `compress` 工具,`compress.protectedTools` 确保特定工具的输出会被附加到压缩摘要中。默认包含 `task`、`skill`、`todowrite` 和 `todoread`。
|
|
219
|
+
|
|
220
|
+
### 外部模型压缩
|
|
221
|
+
|
|
222
|
+
将压缩摘要的生成工作卸载到更便宜的模型,而不是使用昂贵的主模型。
|
|
223
|
+
|
|
224
|
+
#### 优先级链
|
|
225
|
+
|
|
226
|
+
| 优先级 | 来源 | 说明 |
|
|
227
|
+
| --------- | -------- | ------------------------------------------------------ |
|
|
228
|
+
| 1(最高) | 环境变量 | `OPENCODE_DCP_EXTERNAL_COMPRESS_*` |
|
|
229
|
+
| 2 | 项目配置 | `<project>/.opencode/dcp.jsonc:compress.externalModel` |
|
|
230
|
+
| 3 | 全局配置 | `~/.config/opencode/dcp.jsonc:compress.externalModel` |
|
|
231
|
+
| 4(兜底) | 主模型 | 原始行为不变 |
|
|
202
232
|
|
|
203
|
-
|
|
233
|
+
#### 使用方法
|
|
204
234
|
|
|
205
|
-
|
|
235
|
+
**方法 1 — 环境变量(推荐用于快速设置):**
|
|
206
236
|
|
|
207
|
-
|
|
237
|
+
```bash
|
|
238
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_URL="http://your-proxy-url/v1"
|
|
239
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_MODEL="your-model-name"
|
|
240
|
+
# Optional:
|
|
241
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_KEY="your-api-key"
|
|
242
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_TIMEOUT="120000"
|
|
243
|
+
export OPENCODE_DCP_EXTERNAL_COMPRESS_RETRIES="1"
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
在 shell RC 文件(`~/.zshrc`、`~/.bashrc`)中持久化,以便跨会话自动可用。
|
|
208
247
|
|
|
209
|
-
|
|
248
|
+
**方法 2 — 配置文件(`dcp.jsonc`):**
|
|
249
|
+
|
|
250
|
+
```jsonc
|
|
251
|
+
{
|
|
252
|
+
"compress": {
|
|
253
|
+
"externalModel": {
|
|
254
|
+
"url": "http://your-proxy-url/v1",
|
|
255
|
+
"model": "your-model-name",
|
|
256
|
+
"apiKey": "your-api-key",
|
|
257
|
+
"timeout": 120000,
|
|
258
|
+
"retries": 1,
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
}
|
|
262
|
+
```
|
|
210
263
|
|
|
211
|
-
|
|
212
|
-
`task`, `skill`, `todowrite`, `todoread`, `compress`, `batch`, `plan_enter`, `plan_exit`, `write`, `edit`
|
|
264
|
+
#### 行为说明
|
|
213
265
|
|
|
214
|
-
|
|
266
|
+
| 场景 | 行为 |
|
|
267
|
+
| --------------------------------- | ------------------------------------------------ |
|
|
268
|
+
| 配置了外部模型 + 未提供 `summary` | 插件获取范围内容 → 调用外部模型 → 存储生成的摘要 |
|
|
269
|
+
| 配置了外部模型 + 提供了 `summary` | 插件直接使用提供的 `summary`(向后兼容) |
|
|
270
|
+
| 外部模型调用失败 | 工具抛出错误;主模型使用回退摘要重试 |
|
|
271
|
+
| 未配置 | 一切不变——主模型像以前一样编写摘要 |
|
|
272
|
+
| `url` 不兼容 OpenAI | 外部模型调用失败 → 进入上述错误路径 |
|
|
215
273
|
|
|
216
|
-
|
|
274
|
+
**兼容性:** 仅支持兼容 OpenAI 的 `/chat/completions` 端点(本地代理、OpenAI、DeepSeek 等)。
|
|
217
275
|
|
|
218
|
-
##
|
|
276
|
+
## 对提示词缓存的影响
|
|
219
277
|
|
|
220
|
-
LLM
|
|
278
|
+
LLM 提供商基于精确前缀匹配来缓存提示词。当 DCP 剪枝内容时,它会修改消息,从而从该位置开始使缓存的提示词前缀失效。
|
|
221
279
|
|
|
222
|
-
|
|
280
|
+
**权衡:** 你会损失一些缓存命中,但通过减小上下文尺寸和减少因过时上下文导致的幻觉来获得 token 节省。在大多数情况下,尤其是在长会话中,节省量超过了缓存未命中的成本。
|
|
223
281
|
|
|
224
282
|
> [!NOTE]
|
|
225
|
-
>
|
|
283
|
+
> 测试中,使用 DCP 的缓存命中率约为 85%,而未使用时约为 90%。
|
|
226
284
|
|
|
227
|
-
|
|
285
|
+
**无影响的情况:**
|
|
228
286
|
|
|
229
|
-
-
|
|
230
|
-
-
|
|
287
|
+
- **按请求计费** — 按请求收费而非按 token 收费的提供商,如 GitHub Copilot。
|
|
288
|
+
- **统一 token 定价** — 对缓存和未缓存 token 按相同费率计费的提供商,如 Cerebras。
|
|
231
289
|
|
|
232
290
|
## License
|
|
233
|
-
|
|
234
|
-
AGPL-3.0-or-later
|