opencode-acp-provider 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +357 -2
- package/package.json +41 -4
- package/src/acp-client.ts +1033 -0
- package/src/acp-tools.ts +311 -0
- package/src/agent-command.ts +20 -0
- package/src/agents/README.md +63 -0
- package/src/agents/cmd/index.ts +55 -0
- package/src/agents/cmd/mods/no-native-orchestration.ts +54 -0
- package/src/agents/cmd/mods/opencode-skills.ts +21 -0
- package/src/commands.ts +275 -0
- package/src/debug.ts +71 -0
- package/src/index.ts +640 -0
- package/src/inventory.ts +90 -0
- package/src/language-model.ts +427 -0
- package/src/mcp-bridge.mjs +150 -0
- package/src/mcp-config.ts +198 -0
- package/src/mcp-server.ts +943 -0
- package/src/prompt.ts +100 -0
- package/src/server-manager.ts +1042 -0
- package/src/setup.ts +100 -0
- package/src/shim.ts +436 -0
- package/src/skills.ts +232 -0
- package/src/steering.ts +85 -0
- package/src/util.ts +94 -0
package/src/acp-tools.ts
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import type { SessionUpdate } from "./acp-client.ts"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ACP agents run their own tools and report the call through the protocol.
|
|
5
|
+
* OpenCode can only draw a native tool card if the tool is named like one of
|
|
6
|
+
* its own tools and carries the matching input shape, so translate the ACP
|
|
7
|
+
* call into that shape here. Anything unknown stays a generic tool.
|
|
8
|
+
*/
|
|
9
|
+
export interface AcpToolState {
|
|
10
|
+
readonly id: string
|
|
11
|
+
name: string
|
|
12
|
+
kind?: string
|
|
13
|
+
title?: string
|
|
14
|
+
input: Record<string, unknown>
|
|
15
|
+
status: string
|
|
16
|
+
output?: string
|
|
17
|
+
isError: boolean
|
|
18
|
+
done: boolean
|
|
19
|
+
/** Internal calls (for example a "think" tool) that should not become cards. */
|
|
20
|
+
hidden?: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const MAX_OUTPUT = 32_000
|
|
24
|
+
|
|
25
|
+
function text(value: unknown): string | undefined {
|
|
26
|
+
return typeof value === "string" && value.trim() ? value.trim() : undefined
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** File contents must keep their exact whitespace: the edit card renders a diff. */
|
|
30
|
+
function literal(value: unknown): string | undefined {
|
|
31
|
+
return typeof value === "string" ? value : undefined
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function record(value: unknown): Record<string, unknown> | undefined {
|
|
35
|
+
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** codex-acp reports the file path of a read call here instead of in `rawInput`. */
|
|
39
|
+
function locationPath(update: SessionUpdate) {
|
|
40
|
+
const locations = Array.isArray(update.locations) ? update.locations : []
|
|
41
|
+
for (const item of locations) {
|
|
42
|
+
const path = firstText(record(item)?.path)
|
|
43
|
+
if (path) return path
|
|
44
|
+
}
|
|
45
|
+
return undefined
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** codex-acp streams shell and read output through `_meta` rather than `content`. */
|
|
49
|
+
function terminalOutputDelta(update: SessionUpdate) {
|
|
50
|
+
const data = record(record(update._meta)?.terminal_output_delta)?.data
|
|
51
|
+
return typeof data === "string" ? data : undefined
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function firstText(...values: unknown[]): string | undefined {
|
|
55
|
+
for (const value of values) {
|
|
56
|
+
const found = text(value)
|
|
57
|
+
if (found) return found
|
|
58
|
+
}
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Codex sends `["bash", "-lc", "ls"]`; Claude sends the command as a string. */
|
|
63
|
+
function commandOf(value: unknown): string | undefined {
|
|
64
|
+
if (typeof value === "string") return text(value)
|
|
65
|
+
if (!Array.isArray(value)) return undefined
|
|
66
|
+
const parts = value.filter((item): item is string => typeof item === "string")
|
|
67
|
+
if (!parts.length) return undefined
|
|
68
|
+
if (parts.length === 3 && parts[0].endsWith("sh") && parts[1].startsWith("-")) return text(parts[2])
|
|
69
|
+
return text(parts.join(" "))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function toolName(kind: string | undefined, title: string | undefined): string {
|
|
73
|
+
switch (kind) {
|
|
74
|
+
case "execute":
|
|
75
|
+
return "shell"
|
|
76
|
+
case "read":
|
|
77
|
+
return "read"
|
|
78
|
+
case "edit":
|
|
79
|
+
if (title && /write|create/i.test(title)) return "write"
|
|
80
|
+
return "edit"
|
|
81
|
+
case "search":
|
|
82
|
+
return title && /glob/i.test(title) ? "glob" : "grep"
|
|
83
|
+
case "fetch":
|
|
84
|
+
return "webfetch"
|
|
85
|
+
default:
|
|
86
|
+
return "tool"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface McpCall {
|
|
91
|
+
server: string
|
|
92
|
+
tool: string
|
|
93
|
+
arguments?: Record<string, unknown>
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Agents name MCP tools as `mcp__server__tool` or `mcp.server.tool`. */
|
|
97
|
+
function parseMcpName(name: string): { server: string; tool: string } | undefined {
|
|
98
|
+
if (name.startsWith("mcp__")) {
|
|
99
|
+
const [server, ...rest] = name.slice(5).split("__")
|
|
100
|
+
const tool = rest.join("__")
|
|
101
|
+
if (server && tool) return { server, tool }
|
|
102
|
+
}
|
|
103
|
+
if (name.startsWith("mcp.")) {
|
|
104
|
+
const [server, ...rest] = name.slice(4).split(".")
|
|
105
|
+
const tool = rest.join(".")
|
|
106
|
+
if (server && tool) return { server, tool }
|
|
107
|
+
}
|
|
108
|
+
return undefined
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function mcpCall(
|
|
112
|
+
update: SessionUpdate,
|
|
113
|
+
raw: Record<string, unknown> | undefined,
|
|
114
|
+
fallbackTitle?: string,
|
|
115
|
+
): McpCall | undefined {
|
|
116
|
+
const meta = record(update._meta)
|
|
117
|
+
if (meta?.is_mcp_tool_call === true && raw) {
|
|
118
|
+
const server = firstText(raw.server)
|
|
119
|
+
const tool = firstText(raw.tool)
|
|
120
|
+
if (server && tool) return { server, tool, arguments: record(raw.arguments) }
|
|
121
|
+
}
|
|
122
|
+
const names = [
|
|
123
|
+
firstText(update.name),
|
|
124
|
+
firstText(update.title),
|
|
125
|
+
fallbackTitle,
|
|
126
|
+
firstText(record(record(meta?.claudeCode)?.toolName)),
|
|
127
|
+
]
|
|
128
|
+
for (const name of names) {
|
|
129
|
+
if (!name) continue
|
|
130
|
+
const parsed = parseMcpName(name)
|
|
131
|
+
if (parsed) return { ...parsed, arguments: raw }
|
|
132
|
+
}
|
|
133
|
+
return undefined
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function normalize(name: string, raw: Record<string, unknown>, fallbackPath?: string) {
|
|
137
|
+
const input: Record<string, unknown> = { ...raw }
|
|
138
|
+
const path = firstText(raw.path, raw.file_path, raw.filePath, raw.file) ?? fallbackPath
|
|
139
|
+
const command = commandOf(raw.command) ?? firstText(raw.cmd, raw.shell_command)
|
|
140
|
+
const workdir = firstText(raw.workdir, raw.cwd, raw.working_directory)
|
|
141
|
+
switch (name) {
|
|
142
|
+
case "shell":
|
|
143
|
+
if (command) input.command = command
|
|
144
|
+
if (workdir) input.workdir = workdir
|
|
145
|
+
break
|
|
146
|
+
case "read":
|
|
147
|
+
case "edit":
|
|
148
|
+
case "write":
|
|
149
|
+
if (path) input.path = path
|
|
150
|
+
if (name === "edit") {
|
|
151
|
+
const oldString =
|
|
152
|
+
literal(raw.oldString) ?? literal(raw.old_string) ?? literal(raw.oldText) ?? literal(raw.old_text)
|
|
153
|
+
const newString =
|
|
154
|
+
literal(raw.newString) ?? literal(raw.new_string) ?? literal(raw.newText) ?? literal(raw.new_text)
|
|
155
|
+
if (oldString !== undefined) input.oldString = oldString
|
|
156
|
+
if (newString !== undefined) input.newString = newString
|
|
157
|
+
}
|
|
158
|
+
if (name === "write" && input.content === undefined) {
|
|
159
|
+
const content = literal(raw.content)
|
|
160
|
+
if (content !== undefined) input.content = content
|
|
161
|
+
}
|
|
162
|
+
break
|
|
163
|
+
case "grep":
|
|
164
|
+
case "glob": {
|
|
165
|
+
const pattern = firstText(raw.pattern, raw.query, raw.glob)
|
|
166
|
+
if (pattern) input.pattern = pattern
|
|
167
|
+
if (path) input.path = path
|
|
168
|
+
break
|
|
169
|
+
}
|
|
170
|
+
case "webfetch": {
|
|
171
|
+
const url = firstText(raw.url, raw.uri)
|
|
172
|
+
if (url) input.url = url
|
|
173
|
+
break
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return input
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Human-readable output from an ACP tool update, bounded for the card. */
|
|
180
|
+
function textFromContent(content: unknown): string | undefined {
|
|
181
|
+
if (!Array.isArray(content)) return undefined
|
|
182
|
+
const parts: string[] = []
|
|
183
|
+
for (const item of content) {
|
|
184
|
+
const entry = record(item)
|
|
185
|
+
if (!entry) continue
|
|
186
|
+
const nested = record(entry.content)
|
|
187
|
+
const value = firstText(entry.text, nested?.text, entry.output, entry.stdout)
|
|
188
|
+
if (value) {
|
|
189
|
+
parts.push(value)
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
if (entry.type === "diff") {
|
|
193
|
+
const path = firstText(entry.path)
|
|
194
|
+
const oldText = typeof entry.oldText === "string" ? entry.oldText : undefined
|
|
195
|
+
const newText = typeof entry.newText === "string" ? entry.newText : undefined
|
|
196
|
+
parts.push([path, oldText && `-${oldText}`, newText && `+${newText}`].filter(Boolean).join("\n"))
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return parts.length ? parts.join("\n") : undefined
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function outputOf(update: SessionUpdate): string | undefined {
|
|
203
|
+
const raw = update.rawOutput
|
|
204
|
+
if (typeof raw === "string") return clip(raw)
|
|
205
|
+
const rawRecord = record(raw)
|
|
206
|
+
if (rawRecord) {
|
|
207
|
+
const value = firstText(rawRecord.output, rawRecord.stdout, rawRecord.text, rawRecord.result, rawRecord.content)
|
|
208
|
+
if (value) return clip(value)
|
|
209
|
+
// MCP tool results arrive as `{ result: { content: [{ type: "text", text }] } }`.
|
|
210
|
+
const result = record(rawRecord.result)
|
|
211
|
+
if (result) {
|
|
212
|
+
const nested = firstText(result.output, result.stdout, result.text) ?? textFromContent(result.content)
|
|
213
|
+
if (nested) return clip(nested)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const content = textFromContent(update.content)
|
|
217
|
+
return content ? clip(content) : undefined
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function clip(value: string) {
|
|
221
|
+
return value.length > MAX_OUTPUT ? `${value.slice(0, MAX_OUTPUT - 1)}…` : value
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The file-level diff an ACP agent reports for an edit. */
|
|
225
|
+
function diffOf(update: SessionUpdate) {
|
|
226
|
+
if (!Array.isArray(update.content)) return undefined
|
|
227
|
+
for (const item of update.content) {
|
|
228
|
+
const entry = record(item)
|
|
229
|
+
if (!entry || entry.type !== "diff") continue
|
|
230
|
+
return {
|
|
231
|
+
path: text(entry.path),
|
|
232
|
+
oldText: literal(entry.oldText),
|
|
233
|
+
newText: literal(entry.newText),
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return undefined
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Fold one ACP `tool_call` / `tool_call_update` into the state of that call.
|
|
241
|
+
* Returns undefined for a call that already finished, so late updates cannot
|
|
242
|
+
* reopen a settled tool.
|
|
243
|
+
*/
|
|
244
|
+
export function applyToolUpdate(update: SessionUpdate, states: Map<string, AcpToolState>): AcpToolState | undefined {
|
|
245
|
+
const id = text(update.toolCallId)
|
|
246
|
+
if (!id) return undefined
|
|
247
|
+
const existing = states.get(id)
|
|
248
|
+
if (existing?.done) return undefined
|
|
249
|
+
const kind = text(update.kind) ?? existing?.kind
|
|
250
|
+
// `think` calls (for example codex's Guardian Review) are internal reasoning,
|
|
251
|
+
// not actions on the workspace, and their raw input is not card-friendly.
|
|
252
|
+
// Remember the id so later updates cannot surface the call as a generic card.
|
|
253
|
+
if (existing?.hidden) return undefined
|
|
254
|
+
if (!existing && kind === "think") {
|
|
255
|
+
states.set(id, {
|
|
256
|
+
id,
|
|
257
|
+
name: "tool",
|
|
258
|
+
kind,
|
|
259
|
+
input: {},
|
|
260
|
+
status: "completed",
|
|
261
|
+
isError: false,
|
|
262
|
+
done: true,
|
|
263
|
+
hidden: true,
|
|
264
|
+
})
|
|
265
|
+
return undefined
|
|
266
|
+
}
|
|
267
|
+
const title = text(update.title) ?? existing?.title
|
|
268
|
+
const status = text(update.status) ?? existing?.status ?? "pending"
|
|
269
|
+
const raw = record(update.rawInput)
|
|
270
|
+
const mcp = mcpCall(update, raw, existing?.title)
|
|
271
|
+
const name = existing?.name ?? (mcp ? "shell" : toolName(kind, title))
|
|
272
|
+
const path = locationPath(update)
|
|
273
|
+
const input = raw ? normalize(name, raw, path) : { ...(existing?.input ?? {}) }
|
|
274
|
+
if (path && !input.path) input.path = path
|
|
275
|
+
if (mcp) {
|
|
276
|
+
// OpenCode's generic tool card has no detail view, so render the bridge call
|
|
277
|
+
// as a console card where the command names the MCP tool and its arguments.
|
|
278
|
+
const args = mcp.arguments
|
|
279
|
+
const rendered = args && Object.keys(args).length ? ` ${JSON.stringify(args)}` : ""
|
|
280
|
+
if (rendered) {
|
|
281
|
+
input.command = `mcp.${mcp.server}.${mcp.tool}${rendered}`
|
|
282
|
+
} else if (!input.command && (status === "completed" || status === "failed")) {
|
|
283
|
+
input.command = `mcp.${mcp.server}.${mcp.tool}`
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (name === "edit" && input.oldString === undefined) {
|
|
287
|
+
// Agents that send no edit fields in `rawInput` still report the change as
|
|
288
|
+
// a diff, which is what the edit card renders.
|
|
289
|
+
const diff = diffOf(update)
|
|
290
|
+
if (diff) {
|
|
291
|
+
if (diff.path && !input.path) input.path = diff.path
|
|
292
|
+
if (diff.oldText !== undefined) input.oldString = diff.oldText
|
|
293
|
+
if (diff.newText !== undefined) input.newString = diff.newText
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const delta = terminalOutputDelta(update)
|
|
297
|
+
const streamed = delta === undefined ? existing?.output : clip(`${existing?.output ?? ""}${delta}`)
|
|
298
|
+
const state: AcpToolState = {
|
|
299
|
+
id,
|
|
300
|
+
name,
|
|
301
|
+
kind,
|
|
302
|
+
title,
|
|
303
|
+
input,
|
|
304
|
+
status,
|
|
305
|
+
isError: status === "failed",
|
|
306
|
+
done: status === "completed" || status === "failed",
|
|
307
|
+
output: outputOf(update) ?? streamed,
|
|
308
|
+
}
|
|
309
|
+
states.set(id, state)
|
|
310
|
+
return state
|
|
311
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { basename } from "node:path"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The executable a launch command runs, without its directory or a script
|
|
5
|
+
* suffix, so a Windows shim and the bare name compare the same way.
|
|
6
|
+
*/
|
|
7
|
+
export function executableName(command: string | string[]) {
|
|
8
|
+
const argv = Array.isArray(command) ? command : [command]
|
|
9
|
+
return basename(argv[0] ?? "")
|
|
10
|
+
.toLowerCase()
|
|
11
|
+
.replace(/\.(?:exe|cmd|bat|js|mjs|cjs)$/, "")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** True when the command starts Command Code. */
|
|
15
|
+
export function isCmdCommand(command: string | string[]) {
|
|
16
|
+
const executable = executableName(command)
|
|
17
|
+
if (["cmd", "cmdc", "commandcode", "command-code"].includes(executable)) return true
|
|
18
|
+
if (!["node", "bun"].includes(executable) || !Array.isArray(command)) return false
|
|
19
|
+
return /\/command-code\/dist\/(?:index|cli)\.mjs$/.test((command[1] ?? "").replace(/\\/g, "/"))
|
|
20
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Agent-specific customization
|
|
2
|
+
|
|
3
|
+
Some ACP agents need changes that an ACP client cannot make over the protocol. A common case is
|
|
4
|
+
hiding a native orchestration tool that would otherwise compete with OpenCode's own `subagent` and
|
|
5
|
+
`question` tools. Everything specific to one agent lives here, one directory per agent:
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
src/agents/
|
|
9
|
+
├── cmd/
|
|
10
|
+
│ ├── index.ts # plugin side: adds the mods to `cmd acp` launches
|
|
11
|
+
│ └── mods/ # loaded by cmd itself, not by the plugin
|
|
12
|
+
│ ├── no-native-orchestration.ts
|
|
13
|
+
│ └── opencode-skills.ts
|
|
14
|
+
└── <agent>/
|
|
15
|
+
├── index.ts # plugin code that customizes how this agent is launched or driven
|
|
16
|
+
└── mods/ # companion files the agent loads with its own runtime
|
|
17
|
+
└── <capability>.ts # name the file after the behavior it changes
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Conventions
|
|
21
|
+
|
|
22
|
+
- Name the directory after the agent (`cmd`, `claude`, `codex`, …), matching the usual server `id`.
|
|
23
|
+
- `index.ts` is plugin code and may import from the rest of `src/`. The rest of the plugin imports
|
|
24
|
+
the agent's customization from there.
|
|
25
|
+
- Files in `mods/` must be standalone: no imports from `src/`, because the agent loads them with its
|
|
26
|
+
own runtime and API. `tsconfig.json` excludes `src/agents/*/mods/**`, since their types come from
|
|
27
|
+
the agent's own packages.
|
|
28
|
+
- The file name describes the behavior, not the agent: `no-native-orchestration.ts`.
|
|
29
|
+
- Document how the agent loads the file (CLI flag, env var, config key) in a comment at the top of
|
|
30
|
+
the file and in this README.
|
|
31
|
+
|
|
32
|
+
## Command Code (`cmd`)
|
|
33
|
+
|
|
34
|
+
### `no-native-orchestration.ts`
|
|
35
|
+
|
|
36
|
+
Removes the native `agent`, `agent_output`, and `ask_user_question`
|
|
37
|
+
tools from the main loop. Command Code's permission rules cannot block the `agent` tool (its docs
|
|
38
|
+
mark it as always allowed), so the mod uses `cmd.setActiveTools` to remove it from the model's schema
|
|
39
|
+
and `beforeToolCall` to refuse any call that still arrives.
|
|
40
|
+
|
|
41
|
+
It filters from `cmd.getActiveTools()` rather than `cmd.getAllTools()`, so it only removes the listed
|
|
42
|
+
tools. Tools another mod disabled stay disabled, and tools another mod added stay active.
|
|
43
|
+
`setActiveTools` filters the tool surface only: other mods' commands, hooks, providers, renderers,
|
|
44
|
+
and UI are untouched. If some later mod re-enables `agent`, `beforeToolCall` still blocks the call.
|
|
45
|
+
|
|
46
|
+
The plugin loads it automatically whenever the MCP bridge is enabled: `cmd/index.ts` appends
|
|
47
|
+
`--mod <path>` to any `cmd ... acp` command, with the path resolved from the package's install
|
|
48
|
+
location. Do not list it in `opencode.jsonc`. `--mod` is a global `cmd` flag that `cmd acp` accepts
|
|
49
|
+
even though `cmd acp --help` does not list it; `--safe-mode` disables all mods.
|
|
50
|
+
|
|
51
|
+
The mod also removes `ask_user_question`, because ACP has no question method: leaving it enabled
|
|
52
|
+
makes `cmd acp` relay the question as a permission request instead of using OpenCode's question UI.
|
|
53
|
+
With it disabled the agent calls the MCP `question` tool. Verify with a prompt such as "call the
|
|
54
|
+
agent tool with subagent_type explore": the agent no
|
|
55
|
+
longer sees that tool and should report it as unavailable rather than delegating.
|
|
56
|
+
|
|
57
|
+
### `opencode-skills.ts`
|
|
58
|
+
|
|
59
|
+
`cmd acp` accepts the global `--skill` flag but its ACP action never applies it; only the interactive
|
|
60
|
+
and print paths call `setSkillLoadOverrides`. Mods are loaded on the ACP path, and
|
|
61
|
+
`cmd.addSkillsPath` feeds the same skill discovery plan. This mod reads
|
|
62
|
+
`OPENCODE_ACP_PROVIDER_SKILL_DIR` and registers it. The plugin sets the env var and loads the mod through
|
|
63
|
+
`cmd/index.ts`, like `no-native-orchestration.ts`; `--safe-mode` disables it like any other mod.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url"
|
|
2
|
+
import { isCmdCommand } from "../../agent-command.ts"
|
|
3
|
+
|
|
4
|
+
const mod = (name: string) => fileURLToPath(new URL(`./mods/${name}`, import.meta.url))
|
|
5
|
+
|
|
6
|
+
/** Hides cmd's native `agent`/`ask_user_question` tools so the MCP bridge's tools are used. */
|
|
7
|
+
export const CMD_ORCHESTRATION_MOD = mod("no-native-orchestration.ts")
|
|
8
|
+
/** Registers OPENCODE_ACP_PROVIDER_SKILL_DIR with cmd, because `cmd acp` ignores `--skill`. */
|
|
9
|
+
export const CMD_SKILL_MOD = mod("opencode-skills.ts")
|
|
10
|
+
|
|
11
|
+
export interface CmdLaunchOptions {
|
|
12
|
+
/** Load the mod that hides cmd's native orchestration and question tools. */
|
|
13
|
+
orchestration?: boolean
|
|
14
|
+
/** Skills mirror directory to expose to cmd. */
|
|
15
|
+
skillDir?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Add the plugin's companion mods to a `cmd ... acp` launch command.
|
|
20
|
+
*
|
|
21
|
+
* Every mod in `./mods/` is loaded the same way: the plugin appends
|
|
22
|
+
* `--mod <path>` resolved from its own install location, so configs never hold
|
|
23
|
+
* an absolute path into the package. A mod already listed with the same path is
|
|
24
|
+
* not added twice, and `--safe-mode` (which disables all mods) is respected.
|
|
25
|
+
*
|
|
26
|
+
* The skills directory is also passed as `--skill` for cmd builds that apply
|
|
27
|
+
* it on the ACP path; cmd's skill planner de-duplicates the two routes.
|
|
28
|
+
*/
|
|
29
|
+
export function applyCmdMods(command: string | string[], options: CmdLaunchOptions): string | string[] {
|
|
30
|
+
if (!isCmdCommand(command)) return command
|
|
31
|
+
const argv = Array.isArray(command) ? [...command] : [command]
|
|
32
|
+
if (!argv.some((arg) => arg.toLowerCase() === "acp")) return command
|
|
33
|
+
if (argv.includes("--safe-mode")) return command
|
|
34
|
+
|
|
35
|
+
const addMod = (path: string) => {
|
|
36
|
+
if (!hasOption(argv, "--mod", path)) argv.push("--mod", path)
|
|
37
|
+
}
|
|
38
|
+
if (options.orchestration) addMod(CMD_ORCHESTRATION_MOD)
|
|
39
|
+
if (options.skillDir && !argv.includes("--no-skills")) {
|
|
40
|
+
addMod(CMD_SKILL_MOD)
|
|
41
|
+
if (!hasOption(argv, "--skill")) argv.push("--skill", options.skillDir)
|
|
42
|
+
}
|
|
43
|
+
return argv
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function hasOption(argv: string[], name: string, value?: string) {
|
|
47
|
+
if (value !== undefined) {
|
|
48
|
+
for (let index = 0; index < argv.length; index++) {
|
|
49
|
+
if (argv[index] === name && argv[index + 1] === value) return true
|
|
50
|
+
if (argv[index] === `${name}=${value}`) return true
|
|
51
|
+
}
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
return argv.some((arg) => arg === name || arg.startsWith(`${name}=`))
|
|
55
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disable Command Code's native orchestration and question tools inside `cmd acp`.
|
|
3
|
+
*
|
|
4
|
+
* Command Code exposes `agent` and `agent_output` to the main loop. Inside an
|
|
5
|
+
* ACP host such as OpenCode those duplicate the orchestrator, which already
|
|
6
|
+
* exposes its own `subagent` tool through the MCP bridge. Permission rules
|
|
7
|
+
* cannot block `agent` (the docs mark it as always allowed), so this mod
|
|
8
|
+
* removes it from the tool surface and fails closed if a call still arrives.
|
|
9
|
+
*
|
|
10
|
+
* `ask_user_question` is removed for the same reason: ACP has no question
|
|
11
|
+
* method, so `cmd acp` would relay it as a permission request instead of
|
|
12
|
+
* showing OpenCode's question UI. The agent should call the host's MCP
|
|
13
|
+
* `question` tool instead.
|
|
14
|
+
*
|
|
15
|
+
* Loaded automatically by the plugin (src/agents/cmd/index.ts) on any `cmd ... acp`
|
|
16
|
+
* command while the MCP bridge is enabled. `--safe-mode` disables it.
|
|
17
|
+
*/
|
|
18
|
+
import type { ModApi } from "@commandcode/harness"
|
|
19
|
+
|
|
20
|
+
const DISABLED = new Set(["agent", "agent_output", "ask_user_question"])
|
|
21
|
+
|
|
22
|
+
export default function (cmd: ModApi) {
|
|
23
|
+
const hide = () => {
|
|
24
|
+
// Base the filter on the *active* tools, not every registered tool: another
|
|
25
|
+
// mod may already have disabled something, and getAllTools would re-enable it.
|
|
26
|
+
const active = cmd.getActiveTools()
|
|
27
|
+
const next = active.filter((name) => !DISABLED.has(name))
|
|
28
|
+
if (next.length !== active.length) cmd.setActiveTools(next)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
cmd.hooks({
|
|
32
|
+
// setActiveTools needs a bound session.
|
|
33
|
+
onSessionStart() {
|
|
34
|
+
hide()
|
|
35
|
+
},
|
|
36
|
+
// Re-assert before every model request: MCP tools can connect after
|
|
37
|
+
// session start, and the tool schemas are built for this request.
|
|
38
|
+
beforeModelRequest() {
|
|
39
|
+
hide()
|
|
40
|
+
return undefined
|
|
41
|
+
},
|
|
42
|
+
// Fail closed if a call slips through the schema filter.
|
|
43
|
+
beforeToolCall({ toolName }) {
|
|
44
|
+
if (!DISABLED.has(toolName)) return undefined
|
|
45
|
+
return {
|
|
46
|
+
block: true,
|
|
47
|
+
additionalContext:
|
|
48
|
+
toolName === "ask_user_question"
|
|
49
|
+
? "Native questions are disabled. Use the host's MCP `question` tool instead."
|
|
50
|
+
: "Native subagents are disabled. Use the host's own subagent tool instead.",
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expose OpenCode's mirrored skills to `cmd acp`.
|
|
3
|
+
*
|
|
4
|
+
* `cmd acp` accepts the global `--skill` flag, but its ACP action never applies
|
|
5
|
+
* it: only the interactive and print paths call `setSkillLoadOverrides`. Mods,
|
|
6
|
+
* however, are loaded by the ACP path, and `addSkillsPath` feeds the same skill
|
|
7
|
+
* discovery plan. The plugin sets OPENCODE_ACP_PROVIDER_SKILL_DIR for the child and adds
|
|
8
|
+
* this file with `--mod`.
|
|
9
|
+
*
|
|
10
|
+
* Loaded automatically by the plugin (src/agents/cmd/index.ts). A user-supplied mod
|
|
11
|
+
* list keeps working; `--safe-mode` disables all mods, including this one.
|
|
12
|
+
*/
|
|
13
|
+
interface SkillModApi {
|
|
14
|
+
addSkillsPath?: (path: string) => unknown
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default function (cmd: SkillModApi) {
|
|
18
|
+
const directory = process.env.OPENCODE_ACP_PROVIDER_SKILL_DIR
|
|
19
|
+
if (!directory || typeof cmd.addSkillsPath !== "function") return
|
|
20
|
+
cmd.addSkillsPath(directory)
|
|
21
|
+
}
|