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/commands.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin"
|
|
2
|
+
import type { AcpCommand } from "./acp-client.ts"
|
|
3
|
+
import type { AcpServerManager, LogFn } from "./server-manager.ts"
|
|
4
|
+
import { followSkillUpdates } from "./skills.ts"
|
|
5
|
+
import { agentName, errorMessage } from "./util.ts"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Exposes the slash commands an ACP agent advertises as OpenCode commands.
|
|
9
|
+
*
|
|
10
|
+
* ACP has no "execute command" method: a slash command is prompt text the agent
|
|
11
|
+
* intercepts, so forwarding `/<name> <args>` as a prompt is the whole
|
|
12
|
+
* implementation. Names are namespaced by server (`claude:review`) because
|
|
13
|
+
* commands belong to the agent, not to one of its models.
|
|
14
|
+
*/
|
|
15
|
+
export interface AcpCommandBridge {
|
|
16
|
+
/** Re-register the commands after one server advertised a new list, or all of them. */
|
|
17
|
+
sync: (server?: string) => Promise<void>
|
|
18
|
+
close: () => Promise<void>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface CommandBridgeOptions {
|
|
22
|
+
ctx: Plugin.Context
|
|
23
|
+
manager: AcpServerManager
|
|
24
|
+
/** ACP server behind an OpenCode provider id, or undefined for other providers. */
|
|
25
|
+
serverForProvider: (providerID: string) => string | undefined
|
|
26
|
+
log: LogFn
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface RegisteredCommand {
|
|
30
|
+
name: string
|
|
31
|
+
description?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Plugin storage key for the last command lists the agents advertised. */
|
|
35
|
+
export const COMMAND_CACHE_KEY = "commands.v1"
|
|
36
|
+
|
|
37
|
+
export async function createCommandBridge(options: CommandBridgeOptions): Promise<AcpCommandBridge> {
|
|
38
|
+
const { ctx, manager, serverForProvider, log } = options
|
|
39
|
+
// Seeded from the previous run so the commands are registered while the
|
|
40
|
+
// plugin is still in setup. See `readCache`.
|
|
41
|
+
const commands = await readCache(ctx, manager, log)
|
|
42
|
+
|
|
43
|
+
let closed = false
|
|
44
|
+
// Updates arrive as agents send them; serialize so rebuilds cannot interleave.
|
|
45
|
+
let queue: Promise<void> = Promise.resolve()
|
|
46
|
+
|
|
47
|
+
const rebuild = async (server?: string) => {
|
|
48
|
+
if (closed) return
|
|
49
|
+
// Read on every rebuild: OpenCode discovers skills in the background, so a
|
|
50
|
+
// set captured during setup is missing most of them.
|
|
51
|
+
const skills = await skillDescriptions(ctx, log)
|
|
52
|
+
let changed = false
|
|
53
|
+
for (const [id, state] of manager.servers) {
|
|
54
|
+
if (server && id !== server) continue
|
|
55
|
+
if (state.config.disabled) continue
|
|
56
|
+
// Not reported yet: keep the cached list until the agent sends its own.
|
|
57
|
+
if (state.commands === undefined) continue
|
|
58
|
+
const forwarded = forwardable(state.commands, skills)
|
|
59
|
+
const previous = commands.get(id)
|
|
60
|
+
if (forwarded.length) {
|
|
61
|
+
commands.set(id, forwarded)
|
|
62
|
+
if (!previous || !sameRegistered(previous, forwarded)) changed = true
|
|
63
|
+
} else if (previous) {
|
|
64
|
+
commands.delete(id)
|
|
65
|
+
changed = true
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// Reloading republishes the registry, so skip it when nothing moved.
|
|
69
|
+
if (!changed) return
|
|
70
|
+
await ctx.command.reload()
|
|
71
|
+
const cache: Record<string, Array<Record<string, string>>> = Object.fromEntries(
|
|
72
|
+
[...commands].map(([id, list]) => [
|
|
73
|
+
id,
|
|
74
|
+
list.map(({ name, description }): Record<string, string> =>
|
|
75
|
+
description === undefined ? { name } : { name, description },
|
|
76
|
+
),
|
|
77
|
+
]),
|
|
78
|
+
)
|
|
79
|
+
await ctx.storage.set(COMMAND_CACHE_KEY, cache).catch((error) => {
|
|
80
|
+
log("warn", `could not cache ACP commands: ${errorMessage(error)}`)
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A command can be typed in a session that is not on the server that
|
|
86
|
+
* advertised it, because the command list is per location rather than per
|
|
87
|
+
* model. Refuse rather than send the text to an agent that has no such
|
|
88
|
+
* command.
|
|
89
|
+
*/
|
|
90
|
+
const forward = async (input: {
|
|
91
|
+
server: string
|
|
92
|
+
name: string
|
|
93
|
+
sessionID: string
|
|
94
|
+
text: string
|
|
95
|
+
delivery: "steer" | "queue"
|
|
96
|
+
}) => {
|
|
97
|
+
const state = manager.get(input.server)
|
|
98
|
+
if (!state) throw new Error(`ACP server "${input.server}" is no longer configured.`)
|
|
99
|
+
const agent = agentName(state.config)
|
|
100
|
+
|
|
101
|
+
const session = await ctx.session.get({ sessionID: input.sessionID })
|
|
102
|
+
const providerID = session.model?.providerID
|
|
103
|
+
if (!providerID || serverForProvider(providerID) !== input.server) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`/${input.server}:${input.name} belongs to ${agent}, but this session is using ` +
|
|
106
|
+
`${providerID ?? "no model"}. Switch the session to ${agent} to run it.`,
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const text = input.text.trim()
|
|
111
|
+
await ctx.session.prompt({
|
|
112
|
+
sessionID: input.sessionID,
|
|
113
|
+
text: text ? `/${input.name} ${text}` : `/${input.name}`,
|
|
114
|
+
delivery: input.delivery,
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const registration = await ctx.command.transform((editor) => {
|
|
119
|
+
for (const [server, list] of commands) {
|
|
120
|
+
for (const command of list) {
|
|
121
|
+
editor.add({
|
|
122
|
+
name: `${server}:${command.name}`,
|
|
123
|
+
description: command.description,
|
|
124
|
+
execute: async ({ sessionID, prompt, delivery }) => {
|
|
125
|
+
await forward({
|
|
126
|
+
server,
|
|
127
|
+
name: command.name,
|
|
128
|
+
sessionID,
|
|
129
|
+
text: typeof prompt.text === "string" ? prompt.text : "",
|
|
130
|
+
delivery,
|
|
131
|
+
})
|
|
132
|
+
},
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
const sync = (server?: string) => {
|
|
139
|
+
const next = queue.then(() => rebuild(server)).catch((error) => {
|
|
140
|
+
log("warn", `could not refresh ACP commands: ${errorMessage(error)}`)
|
|
141
|
+
})
|
|
142
|
+
queue = next
|
|
143
|
+
return next
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// The skill filter changes as OpenCode discovers skills, which it finishes
|
|
147
|
+
// while this plugin is still starting up.
|
|
148
|
+
const watcher = followSkillUpdates(ctx, sync)
|
|
149
|
+
await rebuild()
|
|
150
|
+
watcher.settle()
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
sync,
|
|
154
|
+
close: async () => {
|
|
155
|
+
closed = true
|
|
156
|
+
watcher.dispose()
|
|
157
|
+
|
|
158
|
+
await queue.catch(() => {})
|
|
159
|
+
await registration.dispose().catch(() => {})
|
|
160
|
+
},
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The command lists stored by the previous run, for servers still configured.
|
|
166
|
+
*
|
|
167
|
+
* Agents start in the background, so their commands normally arrive after
|
|
168
|
+
* setup. OpenCode 2.3 publishes a command change made after setup without a
|
|
169
|
+
* location, and clients only refetch commands for a location-scoped
|
|
170
|
+
* `command.updated`. So a client already connected never sees late commands.
|
|
171
|
+
* Changes made during setup are batched and published with the location, so
|
|
172
|
+
* registering the last known lists here reaches every client. The agent's
|
|
173
|
+
* own list then replaces the cached one, which costs nothing when it is
|
|
174
|
+
* unchanged, the usual case.
|
|
175
|
+
*/
|
|
176
|
+
async function readCache(
|
|
177
|
+
ctx: Plugin.Context,
|
|
178
|
+
manager: AcpServerManager,
|
|
179
|
+
log: LogFn,
|
|
180
|
+
): Promise<Map<string, RegisteredCommand[]>> {
|
|
181
|
+
const commands = new Map<string, RegisteredCommand[]>()
|
|
182
|
+
let stored: unknown
|
|
183
|
+
try {
|
|
184
|
+
stored = await ctx.storage.get(COMMAND_CACHE_KEY)
|
|
185
|
+
} catch (error) {
|
|
186
|
+
log("warn", `could not read cached ACP commands: ${errorMessage(error)}`)
|
|
187
|
+
return commands
|
|
188
|
+
}
|
|
189
|
+
if (!stored || typeof stored !== "object" || Array.isArray(stored)) return commands
|
|
190
|
+
for (const [id, list] of Object.entries(stored)) {
|
|
191
|
+
const state = manager.get(id)
|
|
192
|
+
if (!state || state.config.disabled || !Array.isArray(list)) continue
|
|
193
|
+
const valid = list.flatMap((entry): RegisteredCommand[] => {
|
|
194
|
+
if (!entry || typeof entry !== "object") return []
|
|
195
|
+
const { name, description } = entry as { name?: unknown; description?: unknown }
|
|
196
|
+
if (typeof name !== "string" || !name) return []
|
|
197
|
+
return [{ name, ...(typeof description === "string" ? { description } : {}) }]
|
|
198
|
+
})
|
|
199
|
+
if (valid.length) commands.set(id, valid)
|
|
200
|
+
}
|
|
201
|
+
return commands
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Whether the registered set for one server already matches a rebuilt one. */
|
|
205
|
+
function sameRegistered(left: RegisteredCommand[], right: RegisteredCommand[]) {
|
|
206
|
+
if (left.length !== right.length) return false
|
|
207
|
+
return left.every((command, index) => {
|
|
208
|
+
const other = right[index]
|
|
209
|
+
return command.name === other.name && command.description === other.description
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Drop commands that only restate a skill OpenCode already has. Agents raise
|
|
215
|
+
* their own skills to slash commands, so without this the palette fills with a
|
|
216
|
+
* second copy of every skill.
|
|
217
|
+
*/
|
|
218
|
+
function forwardable(commands: AcpCommand[], skills: Set<string>) {
|
|
219
|
+
const seen = new Set<string>()
|
|
220
|
+
const forwarded: RegisteredCommand[] = []
|
|
221
|
+
for (const command of commands) {
|
|
222
|
+
if (seen.has(command.name)) continue
|
|
223
|
+
if (isSkill(command, skills)) continue
|
|
224
|
+
seen.add(command.name)
|
|
225
|
+
const hint = command.hint ? `usage: ${command.hint}` : ""
|
|
226
|
+
const description = [command.description?.trim(), hint].filter(Boolean).join(" ")
|
|
227
|
+
forwarded.push({ name: command.name, ...(description ? { description } : {}) })
|
|
228
|
+
}
|
|
229
|
+
return forwarded
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Whether this command duplicates a skill, decided by reading OpenCode's skill
|
|
234
|
+
* registry rather than by keeping a list of names.
|
|
235
|
+
*
|
|
236
|
+
* Agents that mark skills structurally are taken at their word: `cmd` uses a
|
|
237
|
+
* `skill:` prefix and codex a `$` sigil. For the rest, a command is a skill when
|
|
238
|
+
* its description says the same thing as a registered skill's, because some
|
|
239
|
+
* agents advertise a skill under a plain name. Agents append where the skill came
|
|
240
|
+
* from (`(user)`, `(claude.ai sync)`) to that description, so a trailing
|
|
241
|
+
* parenthetical is ignored.
|
|
242
|
+
*/
|
|
243
|
+
function isSkill(command: AcpCommand, skills: Set<string>) {
|
|
244
|
+
if (/^(skill:|\$)/i.test(command.name)) return true
|
|
245
|
+
const description = normalizeDescription(command.description)
|
|
246
|
+
return description ? skills.has(description) : false
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Normalized descriptions of the skills OpenCode has registered. */
|
|
250
|
+
async function skillDescriptions(ctx: Plugin.Context, log: LogFn): Promise<Set<string>> {
|
|
251
|
+
const descriptions = new Set<string>()
|
|
252
|
+
try {
|
|
253
|
+
const { data } = await ctx.skill.list()
|
|
254
|
+
for (const skill of data as ReadonlyArray<{ description?: unknown }>) {
|
|
255
|
+
const description = normalizeDescription(skill.description)
|
|
256
|
+
if (description) descriptions.add(description)
|
|
257
|
+
}
|
|
258
|
+
} catch (error) {
|
|
259
|
+
log(
|
|
260
|
+
"warn",
|
|
261
|
+
`could not read OpenCode skills, so ACP commands may repeat them: ${errorMessage(error)}`,
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
return descriptions
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Compare prose without the agent's provenance suffix, case, or punctuation. */
|
|
268
|
+
function normalizeDescription(description: unknown) {
|
|
269
|
+
if (typeof description !== "string") return ""
|
|
270
|
+
return description
|
|
271
|
+
.trim()
|
|
272
|
+
.replace(/\s*\([^()]*\)\s*$/, "")
|
|
273
|
+
.toLowerCase()
|
|
274
|
+
.replace(/[^a-z0-9]+/g, "")
|
|
275
|
+
}
|
package/src/debug.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { constants } from "node:fs"
|
|
2
|
+
import { open, type FileHandle } from "node:fs/promises"
|
|
3
|
+
import { tmpdir } from "node:os"
|
|
4
|
+
import { join } from "node:path"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The default name carries the uid because `os.tmpdir()` is the shared `/tmp`
|
|
8
|
+
* on Linux, where another user could otherwise claim the name first.
|
|
9
|
+
*/
|
|
10
|
+
const FILE =
|
|
11
|
+
process.env.OPENCODE_ACP_PROVIDER_LOG ??
|
|
12
|
+
join(
|
|
13
|
+
tmpdir(),
|
|
14
|
+
typeof process.getuid === "function" ? `opencode-acp-provider-${process.getuid()}.log` : "opencode-acp-provider.log",
|
|
15
|
+
)
|
|
16
|
+
const MAX_BYTES = 10 * 1024 * 1024
|
|
17
|
+
// Agent stderr can contain credentials, so keep the file private to this user.
|
|
18
|
+
const FILE_MODE = 0o600
|
|
19
|
+
// Never follow a symlink planted at the log path.
|
|
20
|
+
const OPEN_FLAGS = constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | (constants.O_NOFOLLOW ?? 0)
|
|
21
|
+
|
|
22
|
+
let queue: Promise<void> = Promise.resolve()
|
|
23
|
+
let bytes = 0
|
|
24
|
+
/** undefined: not opened yet; null: the file is unusable, so logging is off. */
|
|
25
|
+
let handle: FileHandle | null | undefined
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Open the log, refusing a file this user does not own or that is not a
|
|
29
|
+
* regular file. `mode` only applies on creation, so an existing file that
|
|
30
|
+
* others can read is tightened before anything is written to it.
|
|
31
|
+
*/
|
|
32
|
+
async function openLog(): Promise<FileHandle | null> {
|
|
33
|
+
const file = await open(FILE, OPEN_FLAGS, FILE_MODE).catch(() => undefined)
|
|
34
|
+
if (!file) return null
|
|
35
|
+
try {
|
|
36
|
+
const info = await file.stat()
|
|
37
|
+
const uid = typeof process.getuid === "function" ? process.getuid() : undefined
|
|
38
|
+
if (!info.isFile() || (uid !== undefined && info.uid !== uid)) {
|
|
39
|
+
await file.close().catch(() => {})
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
if (process.platform !== "win32" && (info.mode & 0o077) !== 0) await file.chmod(FILE_MODE)
|
|
43
|
+
bytes = info.size
|
|
44
|
+
return file
|
|
45
|
+
} catch {
|
|
46
|
+
await file.close().catch(() => {})
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Append one line to the debug log without blocking the event loop. Writes are
|
|
53
|
+
* serialized, and the file is truncated once it grows past 10 MB.
|
|
54
|
+
*/
|
|
55
|
+
export function debugLog(scope: string, message: string) {
|
|
56
|
+
const line = `${new Date().toISOString()} ${scope} ${message}\n`
|
|
57
|
+
queue = queue
|
|
58
|
+
.then(async () => {
|
|
59
|
+
if (handle === undefined) handle = await openLog()
|
|
60
|
+
if (!handle) return
|
|
61
|
+
const size = Buffer.byteLength(line)
|
|
62
|
+
if (bytes + size > MAX_BYTES) {
|
|
63
|
+
// O_APPEND puts the next write at the new end.
|
|
64
|
+
await handle.truncate(0).catch(() => {})
|
|
65
|
+
bytes = 0
|
|
66
|
+
}
|
|
67
|
+
await handle.write(line).catch(() => {})
|
|
68
|
+
bytes += size
|
|
69
|
+
})
|
|
70
|
+
.catch(() => {})
|
|
71
|
+
}
|