opencode-acp-provider 0.0.0-stage → 1.1.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 +380 -2
- package/package.json +41 -4
- package/src/acp-client.ts +1179 -0
- package/src/acp-tools.ts +311 -0
- package/src/agent-command.ts +20 -0
- package/src/agent-pool.ts +444 -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 +642 -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 +1001 -0
- package/src/prompt.ts +100 -0
- package/src/server-manager.ts +1137 -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
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync, symlinkSync } from "node:fs"
|
|
2
|
+
import { createRequire } from "node:module"
|
|
3
|
+
import { tmpdir } from "node:os"
|
|
4
|
+
import { join } from "node:path"
|
|
5
|
+
import { AcpClient } from "./acp-client.ts"
|
|
6
|
+
import { errorMessage } from "./util.ts"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* One ACP agent process per distinct launch, shared by every OpenCode location.
|
|
10
|
+
*
|
|
11
|
+
* OpenCode loads this plugin once per directory, and each copy used to spawn its
|
|
12
|
+
* own process per agent, paying memory and startup again for every directory.
|
|
13
|
+
* Claude, Codex and Antigravity accept sessions in many directories from one
|
|
14
|
+
* process because each `session/new` carries its own `cwd`, so those agents are
|
|
15
|
+
* shared: `acquire` keys a client by its launch and hands out leases to it.
|
|
16
|
+
*
|
|
17
|
+
* Command Code (`cmd acp`) instead binds a process to the directory of its first
|
|
18
|
+
* session and refuses the others. Such agents use `dedicated`, which keeps one
|
|
19
|
+
* pre-started spare per template. The expensive part (`initialize`, about three
|
|
20
|
+
* seconds for cmd) happens before that binding, and the skills directory passed
|
|
21
|
+
* at spawn is only read when a session starts, so the spare is adopted by
|
|
22
|
+
* symlinking the caller's skills directory into its temporary directory after
|
|
23
|
+
* `initialize`. The link is reported through the lease's `readRoots` so the
|
|
24
|
+
* caller knows it is available.
|
|
25
|
+
*
|
|
26
|
+
* The pool lives on `globalThis` because every location evaluates its own copy
|
|
27
|
+
* of this module; a module-level singleton would give each copy its own pool.
|
|
28
|
+
* The symbol is keyed by ABI and package version so copies from different
|
|
29
|
+
* plugin versions never share a pool whose `AcpClient` they cannot use.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export interface AgentLaunch {
|
|
33
|
+
command: string
|
|
34
|
+
args: string[]
|
|
35
|
+
cwd: string
|
|
36
|
+
env: Record<string, string>
|
|
37
|
+
/** Launches with different scopes never share a process. */
|
|
38
|
+
scope?: string
|
|
39
|
+
autoApprove?: boolean
|
|
40
|
+
requestTimeoutMs?: number
|
|
41
|
+
promptTimeoutMs?: number
|
|
42
|
+
allowPathsOutsideCwd?: boolean
|
|
43
|
+
maxFileBytes?: number
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface AgentSubscriber {
|
|
47
|
+
onLog?: (level: "debug" | "warn" | "error", message: string) => void
|
|
48
|
+
onExit?: (error: Error) => void
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface AgentLease {
|
|
52
|
+
readonly client: AcpClient
|
|
53
|
+
/** Extra directories this client's sessions must be allowed to read (an adopted spare's skill link). */
|
|
54
|
+
readonly readRoots: string[]
|
|
55
|
+
/** Give the client back. Idempotent. */
|
|
56
|
+
release(): void
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface DedicatedTemplate {
|
|
60
|
+
/** Build the launch. `skillDir` is where the agent should read skills, or undefined when skills are off. */
|
|
61
|
+
launch: (skillDir: string | undefined) => AgentLaunch
|
|
62
|
+
/** The caller's own skills directory; undefined when skills are off. */
|
|
63
|
+
skillDir?: string
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface AgentPoolOptions {
|
|
67
|
+
/** Keep a shared client running this long after its last lease is released. Default 0 (stop at once). */
|
|
68
|
+
lingerMs?: number
|
|
69
|
+
/** Keep one pre-started spare client per dedicated template. Default false. */
|
|
70
|
+
spares?: boolean
|
|
71
|
+
/** Parent directory for spare skill-link directories. Default os.tmpdir(). */
|
|
72
|
+
tmpDir?: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Stands in for any skills directory when keying a template, so only skills-on vs skills-off differ. */
|
|
76
|
+
const SKILL_PLACEHOLDER = "\u0000skill-dir\u0000"
|
|
77
|
+
/** How long a stopped client stays reachable from the process exit hook. */
|
|
78
|
+
const RETIRED_TTL_MS = 5_000
|
|
79
|
+
const SPARE_PREFIX = "opencode-acp-provider-spare-"
|
|
80
|
+
|
|
81
|
+
type LogLevel = "debug" | "warn" | "error"
|
|
82
|
+
|
|
83
|
+
interface SharedEntry {
|
|
84
|
+
client: AcpClient
|
|
85
|
+
subscribers: Array<{ subscriber: AgentSubscriber }>
|
|
86
|
+
linger?: ReturnType<typeof setTimeout>
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface Spare {
|
|
90
|
+
client: AcpClient
|
|
91
|
+
dir: string
|
|
92
|
+
link: string
|
|
93
|
+
owner: { subscriber?: AgentSubscriber }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
interface DedicatedState {
|
|
97
|
+
/** Live dedicated leases for this template. */
|
|
98
|
+
interest: number
|
|
99
|
+
spare?: Spare
|
|
100
|
+
spareStop?: ReturnType<typeof setTimeout>
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface Dispatch {
|
|
104
|
+
onLog?: (level: LogLevel, message: string) => void
|
|
105
|
+
onExit?: (error: Error) => void
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Two launches share a client exactly when this key is equal. */
|
|
109
|
+
function launchKey(launch: AgentLaunch) {
|
|
110
|
+
const sortedEnv = Object.keys(launch.env)
|
|
111
|
+
.sort()
|
|
112
|
+
.map((name) => [name, launch.env[name]])
|
|
113
|
+
return JSON.stringify([
|
|
114
|
+
launch.scope ?? null,
|
|
115
|
+
launch.command,
|
|
116
|
+
launch.args,
|
|
117
|
+
launch.cwd,
|
|
118
|
+
sortedEnv,
|
|
119
|
+
launch.autoApprove ?? null,
|
|
120
|
+
launch.requestTimeoutMs ?? null,
|
|
121
|
+
launch.promptTimeoutMs ?? null,
|
|
122
|
+
launch.allowPathsOutsideCwd ?? null,
|
|
123
|
+
launch.maxFileBytes ?? null,
|
|
124
|
+
])
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export class AgentPool {
|
|
128
|
+
private readonly lingerMs: number
|
|
129
|
+
private readonly spares: boolean
|
|
130
|
+
private readonly tmpDir: string
|
|
131
|
+
private readonly shared = new Map<string, SharedEntry>()
|
|
132
|
+
private readonly dedicatedStates = new Map<string, DedicatedState>()
|
|
133
|
+
private readonly dedicatedClients = new Set<AcpClient>()
|
|
134
|
+
private readonly retired = new Set<AcpClient>()
|
|
135
|
+
private readonly retireTimers = new Set<ReturnType<typeof setTimeout>>()
|
|
136
|
+
private readonly dirs = new Set<string>()
|
|
137
|
+
private listening = false
|
|
138
|
+
private halted = false
|
|
139
|
+
|
|
140
|
+
constructor(options: AgentPoolOptions = {}) {
|
|
141
|
+
this.lingerMs = options.lingerMs ?? 0
|
|
142
|
+
this.spares = options.spares ?? false
|
|
143
|
+
this.tmpDir = options.tmpDir ?? tmpdir()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
get stopped() {
|
|
147
|
+
return this.halted
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Agents are spawned detached, so a lingering one must be killed by hand. */
|
|
151
|
+
private readonly onProcessExit = () => {
|
|
152
|
+
for (const client of this.knownClients()) client.killSync()
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
acquire(launch: AgentLaunch, subscriber: AgentSubscriber): AgentLease {
|
|
156
|
+
if (this.halted) throw new Error("ACP agent pool is stopped")
|
|
157
|
+
const key = launchKey(launch)
|
|
158
|
+
const shared = this.shared.get(key) ?? this.createSharedEntry(key, launch)
|
|
159
|
+
this.clearLinger(shared)
|
|
160
|
+
|
|
161
|
+
const registration = { subscriber }
|
|
162
|
+
shared.subscribers.push(registration)
|
|
163
|
+
let released = false
|
|
164
|
+
return {
|
|
165
|
+
client: shared.client,
|
|
166
|
+
readRoots: [],
|
|
167
|
+
release: () => {
|
|
168
|
+
if (released) return
|
|
169
|
+
released = true
|
|
170
|
+
const index = shared.subscribers.indexOf(registration)
|
|
171
|
+
if (index >= 0) shared.subscribers.splice(index, 1)
|
|
172
|
+
// stop() already retired this client and dropped the entry.
|
|
173
|
+
if (this.shared.get(key) !== shared || shared.subscribers.length) return
|
|
174
|
+
if (this.lingerMs > 0) {
|
|
175
|
+
// Stay in the map so a re-acquire before the timer reuses the client.
|
|
176
|
+
const timer = setTimeout(() => {
|
|
177
|
+
shared.linger = undefined
|
|
178
|
+
if (shared.subscribers.length || this.shared.get(key) !== shared) return
|
|
179
|
+
this.shared.delete(key)
|
|
180
|
+
this.retire(shared.client)
|
|
181
|
+
}, this.lingerMs)
|
|
182
|
+
timer.unref?.()
|
|
183
|
+
shared.linger = timer
|
|
184
|
+
} else {
|
|
185
|
+
this.shared.delete(key)
|
|
186
|
+
this.retire(shared.client)
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private createSharedEntry(key: string, launch: AgentLaunch): SharedEntry {
|
|
193
|
+
let entry: SharedEntry
|
|
194
|
+
const client = this.createClient(launch, {
|
|
195
|
+
onLog: (level, message) => entry.subscribers[0]?.subscriber.onLog?.(level, message),
|
|
196
|
+
onExit: (error) => {
|
|
197
|
+
for (const registration of [...entry.subscribers]) registration.subscriber.onExit?.(error)
|
|
198
|
+
},
|
|
199
|
+
})
|
|
200
|
+
entry = { client, subscribers: [] }
|
|
201
|
+
this.shared.set(key, entry)
|
|
202
|
+
return entry
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
dedicated(template: DedicatedTemplate, subscriber: AgentSubscriber): AgentLease {
|
|
206
|
+
if (this.halted) throw new Error("ACP agent pool is stopped")
|
|
207
|
+
const key = this.templateKey(template)
|
|
208
|
+
let state = this.dedicatedStates.get(key)
|
|
209
|
+
if (!state) {
|
|
210
|
+
state = { interest: 0 }
|
|
211
|
+
this.dedicatedStates.set(key, state)
|
|
212
|
+
}
|
|
213
|
+
if (state.spareStop) {
|
|
214
|
+
clearTimeout(state.spareStop)
|
|
215
|
+
state.spareStop = undefined
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
let adopted: Spare | undefined
|
|
219
|
+
if (this.spares && state.spare) {
|
|
220
|
+
const spare = state.spare
|
|
221
|
+
state.spare = undefined
|
|
222
|
+
if (template.skillDir === undefined) {
|
|
223
|
+
adopted = spare
|
|
224
|
+
} else {
|
|
225
|
+
try {
|
|
226
|
+
symlinkSync(template.skillDir, spare.link, "dir")
|
|
227
|
+
adopted = spare
|
|
228
|
+
} catch (error) {
|
|
229
|
+
subscriber.onLog?.("warn", `could not adopt a warm agent: ${errorMessage(error)}`)
|
|
230
|
+
this.retire(spare.client)
|
|
231
|
+
this.removeDir(spare.dir)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (adopted) adopted.owner.subscriber = subscriber
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const client =
|
|
238
|
+
adopted?.client ??
|
|
239
|
+
this.createClient(template.launch(template.skillDir), {
|
|
240
|
+
onLog: (level, message) => subscriber.onLog?.(level, message),
|
|
241
|
+
onExit: (error) => subscriber.onExit?.(error),
|
|
242
|
+
})
|
|
243
|
+
this.dedicatedClients.add(client)
|
|
244
|
+
state.interest++
|
|
245
|
+
if (this.spares) {
|
|
246
|
+
void client
|
|
247
|
+
.start()
|
|
248
|
+
.catch(() => {})
|
|
249
|
+
.finally(() => this.ensureSpare(key, template))
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const current = state
|
|
253
|
+
let released = false
|
|
254
|
+
return {
|
|
255
|
+
client,
|
|
256
|
+
readRoots: adopted ? [adopted.dir] : [],
|
|
257
|
+
release: () => {
|
|
258
|
+
if (released) return
|
|
259
|
+
released = true
|
|
260
|
+
this.dedicatedClients.delete(client)
|
|
261
|
+
this.retire(client)
|
|
262
|
+
if (adopted) this.removeDir(adopted.dir)
|
|
263
|
+
current.interest = Math.max(0, current.interest - 1)
|
|
264
|
+
if (current.interest === 0) this.stopSpareLater(current)
|
|
265
|
+
},
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async stop(): Promise<void> {
|
|
270
|
+
if (this.halted) return
|
|
271
|
+
this.halted = true
|
|
272
|
+
for (const entry of this.shared.values()) this.clearLinger(entry)
|
|
273
|
+
for (const state of this.dedicatedStates.values()) {
|
|
274
|
+
if (state.spareStop) clearTimeout(state.spareStop)
|
|
275
|
+
state.spareStop = undefined
|
|
276
|
+
}
|
|
277
|
+
for (const timer of this.retireTimers) clearTimeout(timer)
|
|
278
|
+
this.retireTimers.clear()
|
|
279
|
+
|
|
280
|
+
const clients = [...this.knownClients()]
|
|
281
|
+
const dirs = [...this.dirs]
|
|
282
|
+
this.shared.clear()
|
|
283
|
+
for (const state of this.dedicatedStates.values()) {
|
|
284
|
+
state.spare = undefined
|
|
285
|
+
state.interest = 0
|
|
286
|
+
}
|
|
287
|
+
this.dedicatedStates.clear()
|
|
288
|
+
this.dedicatedClients.clear()
|
|
289
|
+
this.dirs.clear()
|
|
290
|
+
|
|
291
|
+
const stopping = clients.map((client) => this.retire(client))
|
|
292
|
+
await Promise.allSettled(stopping)
|
|
293
|
+
|
|
294
|
+
for (const dir of dirs) rmSync(dir, { recursive: true, force: true })
|
|
295
|
+
if (this.listening) {
|
|
296
|
+
process.off("exit", this.onProcessExit)
|
|
297
|
+
this.listening = false
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private templateKey(template: DedicatedTemplate) {
|
|
302
|
+
return launchKey(template.launch(template.skillDir === undefined ? undefined : SKILL_PLACEHOLDER))
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private clearLinger(entry: SharedEntry) {
|
|
306
|
+
if (!entry.linger) return
|
|
307
|
+
clearTimeout(entry.linger)
|
|
308
|
+
entry.linger = undefined
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Stop the key's spare once nothing needs it, after the shared linger. */
|
|
312
|
+
private stopSpareLater(state: DedicatedState) {
|
|
313
|
+
if (!state.spare) return
|
|
314
|
+
const run = () => {
|
|
315
|
+
state.spareStop = undefined
|
|
316
|
+
if (state.interest !== 0) return
|
|
317
|
+
const spare = state.spare
|
|
318
|
+
if (!spare) return
|
|
319
|
+
state.spare = undefined
|
|
320
|
+
this.retire(spare.client)
|
|
321
|
+
this.removeDir(spare.dir)
|
|
322
|
+
}
|
|
323
|
+
if (this.lingerMs > 0) {
|
|
324
|
+
const timer = setTimeout(run, this.lingerMs)
|
|
325
|
+
timer.unref?.()
|
|
326
|
+
state.spareStop = timer
|
|
327
|
+
} else {
|
|
328
|
+
run()
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private ensureSpare(key: string, template: DedicatedTemplate) {
|
|
333
|
+
if (!this.spares || this.halted) return
|
|
334
|
+
const state = this.dedicatedStates.get(key)
|
|
335
|
+
if (!state || state.interest === 0 || state.spare) return
|
|
336
|
+
let dir: string | undefined
|
|
337
|
+
try {
|
|
338
|
+
dir = mkdtempSync(join(this.tmpDir, SPARE_PREFIX))
|
|
339
|
+
const spareDir = dir
|
|
340
|
+
this.dirs.add(spareDir)
|
|
341
|
+
const link = join(spareDir, "skills")
|
|
342
|
+
const holder: { spare?: Spare } = {}
|
|
343
|
+
const client = this.createClient(
|
|
344
|
+
template.launch(template.skillDir === undefined ? undefined : link),
|
|
345
|
+
{
|
|
346
|
+
onLog: (level, message) => holder.spare?.owner.subscriber?.onLog?.(level, message),
|
|
347
|
+
onExit: (error) => {
|
|
348
|
+
const spare = holder.spare
|
|
349
|
+
if (!spare) return
|
|
350
|
+
const owner = spare.owner.subscriber
|
|
351
|
+
if (owner) {
|
|
352
|
+
owner.onExit?.(error)
|
|
353
|
+
return
|
|
354
|
+
}
|
|
355
|
+
if (state.spare !== spare) return
|
|
356
|
+
state.spare = undefined
|
|
357
|
+
this.retire(spare.client)
|
|
358
|
+
this.removeDir(spare.dir)
|
|
359
|
+
},
|
|
360
|
+
},
|
|
361
|
+
)
|
|
362
|
+
const spare: Spare = { client, dir: spareDir, link, owner: {} }
|
|
363
|
+
holder.spare = spare
|
|
364
|
+
state.spare = spare
|
|
365
|
+
void client.start().catch(() => {
|
|
366
|
+
if (state.spare !== spare) return
|
|
367
|
+
state.spare = undefined
|
|
368
|
+
this.retire(client)
|
|
369
|
+
this.removeDir(spare.dir)
|
|
370
|
+
})
|
|
371
|
+
} catch {
|
|
372
|
+
if (dir) this.removeDir(dir)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private knownClients() {
|
|
377
|
+
const clients = new Set<AcpClient>()
|
|
378
|
+
for (const entry of this.shared.values()) clients.add(entry.client)
|
|
379
|
+
for (const client of this.dedicatedClients) clients.add(client)
|
|
380
|
+
for (const state of this.dedicatedStates.values()) {
|
|
381
|
+
if (state.spare) clients.add(state.spare.client)
|
|
382
|
+
}
|
|
383
|
+
for (const client of this.retired) clients.add(client)
|
|
384
|
+
return clients
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
private createClient(launch: AgentLaunch, dispatch: Dispatch) {
|
|
388
|
+
if (!this.listening) {
|
|
389
|
+
process.on("exit", this.onProcessExit)
|
|
390
|
+
this.listening = true
|
|
391
|
+
}
|
|
392
|
+
return new AcpClient({
|
|
393
|
+
command: launch.command,
|
|
394
|
+
args: launch.args,
|
|
395
|
+
cwd: launch.cwd,
|
|
396
|
+
env: launch.env,
|
|
397
|
+
autoApprove: launch.autoApprove,
|
|
398
|
+
requestTimeoutMs: launch.requestTimeoutMs,
|
|
399
|
+
promptTimeoutMs: launch.promptTimeoutMs,
|
|
400
|
+
allowPathsOutsideCwd: launch.allowPathsOutsideCwd,
|
|
401
|
+
maxFileBytes: launch.maxFileBytes,
|
|
402
|
+
onLog: dispatch.onLog,
|
|
403
|
+
onExit: dispatch.onExit,
|
|
404
|
+
})
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** Stop a client and keep it killable from the exit hook for a while. */
|
|
408
|
+
private retire(client: AcpClient) {
|
|
409
|
+
const stopping = client.stop().catch(() => {})
|
|
410
|
+
this.retired.add(client)
|
|
411
|
+
const timer = setTimeout(() => {
|
|
412
|
+
this.retired.delete(client)
|
|
413
|
+
this.retireTimers.delete(timer)
|
|
414
|
+
}, RETIRED_TTL_MS)
|
|
415
|
+
timer.unref?.()
|
|
416
|
+
this.retireTimers.add(timer)
|
|
417
|
+
return stopping
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private removeDir(dir: string) {
|
|
421
|
+
this.dirs.delete(dir)
|
|
422
|
+
rmSync(dir, { recursive: true, force: true })
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const POOLS = Symbol.for("opencode-acp-provider.agent-pools")
|
|
427
|
+
/** Bump when AgentPool or AcpClient change incompatibly; copies only share a pool with the same ABI and version. */
|
|
428
|
+
const AGENT_POOL_ABI = 1
|
|
429
|
+
|
|
430
|
+
const { version } = createRequire(import.meta.url)("../package.json") as { version: string }
|
|
431
|
+
|
|
432
|
+
function pools() {
|
|
433
|
+
const scope = globalThis as unknown as Record<symbol, Map<string, AgentPool> | undefined>
|
|
434
|
+
return (scope[POOLS] ??= new Map())
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export function sharedAgentPool(): AgentPool {
|
|
438
|
+
const key = `${AGENT_POOL_ABI}:${version}`
|
|
439
|
+
const existing = pools().get(key)
|
|
440
|
+
if (existing && !existing.stopped) return existing
|
|
441
|
+
const pool = new AgentPool({ lingerMs: 60_000, spares: true })
|
|
442
|
+
pools().set(key, pool)
|
|
443
|
+
return pool
|
|
444
|
+
}
|
|
@@ -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
|
+
}
|