Skip to content

Commit b17ded9

Browse files
committed
fix: register /dcp command with OpenCode
Expose the /dcp command alongside /dcp-compress so users can manage DCP from chat, including re-enabling auto compression via /dcp manual off.
1 parent 85b6f5c commit b17ded9

3 files changed

Lines changed: 84 additions & 7 deletions

File tree

index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
createSystemPromptHandler,
1717
createTextCompleteHandler,
1818
} from "./lib/hooks"
19+
import { registerOpencodeCommands } from "./lib/commands/register"
1920
import { configureClientAuth, isSecureMode } from "./lib/auth"
2021
import { startAutoUpdate } from "./lib/update"
2122

@@ -94,13 +95,7 @@ const server: Plugin = (async (ctx) => {
9495
config.compress.permission = "deny"
9596
}
9697

97-
if (config.commands.enabled && config.compress.permission !== "deny") {
98-
opencodeConfig.command ??= {}
99-
opencodeConfig.command["dcp-compress"] = {
100-
template: "",
101-
description: "Trigger DCP manual compression with: /dcp-compress [focus]",
102-
}
103-
}
98+
registerOpencodeCommands(opencodeConfig, config)
10499

105100
const toolsToAdd: string[] = []
106101
if (config.compress.permission !== "deny" && !config.experimental.allowSubAgents) {

lib/commands/register.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { PluginConfig } from "./config"
2+
3+
type OpencodeCommandConfig = {
4+
template: string
5+
description: string
6+
}
7+
8+
type OpencodeConfig = {
9+
command?: Record<string, OpencodeCommandConfig>
10+
}
11+
12+
export function registerOpencodeCommands(
13+
opencodeConfig: OpencodeConfig,
14+
config: PluginConfig,
15+
): void {
16+
if (!config.commands.enabled || config.compress.permission === "deny") {
17+
return
18+
}
19+
20+
opencodeConfig.command ??= {}
21+
opencodeConfig.command["dcp"] = {
22+
template: "",
23+
description:
24+
"DCP context management: /dcp stats, /dcp context, /dcp sweep, /dcp manual, /dcp decompress, /dcp recompress",
25+
}
26+
opencodeConfig.command["dcp-compress"] = {
27+
template: "",
28+
description: "Trigger DCP manual compression with: /dcp-compress [focus]",
29+
}
30+
}

tests/plugin-commands.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import assert from "node:assert/strict"
2+
import test from "node:test"
3+
import type { PluginConfig } from "../lib/config"
4+
import { registerOpencodeCommands } from "../lib/commands/register"
5+
6+
function buildConfig(permission: "allow" | "deny" = "allow"): PluginConfig {
7+
return {
8+
enabled: true,
9+
debug: false,
10+
pruneNotification: "off",
11+
pruneNotificationType: "chat",
12+
commands: { enabled: true, protectedTools: [] },
13+
manualMode: { enabled: false, automaticStrategies: true },
14+
turnProtection: { enabled: false, turns: 4 },
15+
experimental: { allowSubAgents: false, customPrompts: false },
16+
protectedFilePatterns: [],
17+
compress: {
18+
mode: "message",
19+
permission,
20+
showCompression: false,
21+
maxContextLimit: 150000,
22+
minContextLimit: 50000,
23+
nudgeFrequency: 5,
24+
iterationNudgeThreshold: 15,
25+
nudgeForce: "soft",
26+
protectedTools: ["task"],
27+
protectTags: false,
28+
protectUserMessages: false,
29+
},
30+
strategies: {
31+
deduplication: { enabled: true, protectedTools: [] },
32+
purgeErrors: { enabled: true, turns: 4, protectedTools: [] },
33+
},
34+
} as PluginConfig
35+
}
36+
37+
test("registerOpencodeCommands exposes /dcp and /dcp-compress", () => {
38+
const opencodeConfig: Record<string, unknown> = {}
39+
registerOpencodeCommands(opencodeConfig, buildConfig("allow"))
40+
41+
const commands = opencodeConfig.command as Record<string, { description: string }>
42+
assert.ok(commands.dcp)
43+
assert.match(commands.dcp.description, /\/dcp manual/)
44+
assert.ok(commands["dcp-compress"])
45+
})
46+
47+
test("registerOpencodeCommands skips commands when compress is denied", () => {
48+
const opencodeConfig: Record<string, unknown> = {}
49+
registerOpencodeCommands(opencodeConfig, buildConfig("deny"))
50+
51+
assert.equal(opencodeConfig.command, undefined)
52+
})

0 commit comments

Comments
 (0)