opencode-ahp 0.1.2 → 0.1.4
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.md +49 -9
- package/options.example.jsonc +9 -3
- package/package.json +2 -2
- package/src/bridge.ts +331 -0
- package/src/channels.ts +80 -3
- package/src/host.ts +541 -6
- package/src/index.ts +131 -1
- package/src/options.ts +3 -1
- package/src/state.ts +25 -1
- package/src/transport.ts +9 -2
package/src/index.ts
CHANGED
|
@@ -25,8 +25,12 @@
|
|
|
25
25
|
* Every failure path logs and stays up: a bad client connection or a
|
|
26
26
|
* bridge error must never take down the opencode server (R2).
|
|
27
27
|
*/
|
|
28
|
+
import { execFile } from "node:child_process";
|
|
29
|
+
import { mkdir, readFile as fsReadFile, rm, writeFile as fsWriteFile } from "node:fs/promises";
|
|
30
|
+
import { dirname, isAbsolute, join, relative } from "node:path";
|
|
28
31
|
import { readFileSync } from "node:fs";
|
|
29
32
|
import { fileURLToPath } from "node:url";
|
|
33
|
+
import { promisify } from "node:util";
|
|
30
34
|
import { Plugin } from "@opencode/plugin";
|
|
31
35
|
// Type-only import of the plugin context. The package's root entry
|
|
32
36
|
// intentionally re-exports no `Context` (it is only ever observed as the
|
|
@@ -140,6 +144,44 @@ function isHiddenSessionEvent(event: unknown, hiddenSessionID: string): boolean
|
|
|
140
144
|
return isRecord(data) && data["sessionID"] === hiddenSessionID;
|
|
141
145
|
}
|
|
142
146
|
|
|
147
|
+
// ── Phase 2: changeset git helpers (plan §14.10) ──────────────────────────
|
|
148
|
+
//
|
|
149
|
+
// The plugin `ctx` has no "file content at HEAD" or "discard one file"
|
|
150
|
+
// surface (`ctx.vcs` only diffs), so the facade shells out to `git` in the
|
|
151
|
+
// location directory. Every helper degrades to null/[] on any failure — a
|
|
152
|
+
// VCS hiccup must never throw into the host (R2).
|
|
153
|
+
|
|
154
|
+
const execFileAsync = promisify(execFile);
|
|
155
|
+
|
|
156
|
+
/** Run `git <args…>` in `cwd` and return stdout. Throws on non-zero exit. */
|
|
157
|
+
async function git(cwd: string, args: string[]): Promise<string> {
|
|
158
|
+
// 64 MiB buffer: file contents can be large; a buffer overflow must read
|
|
159
|
+
// as "failed" (→ null/[]), not hang the host.
|
|
160
|
+
const { stdout } = await execFileAsync("git", args, { cwd, maxBuffer: 64 * 1024 * 1024 });
|
|
161
|
+
return stdout;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The git repo root containing `cwd`, or null when it is not inside one. */
|
|
165
|
+
async function gitRepoRoot(cwd: string): Promise<string | null> {
|
|
166
|
+
try {
|
|
167
|
+
const out = (await git(cwd, ["rev-parse", "--show-toplevel"])).trim();
|
|
168
|
+
return out.length > 0 ? out : null;
|
|
169
|
+
} catch {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The repo-root-relative path of `absPath`, or null when outside the repo. */
|
|
175
|
+
function repoRelative(root: string, absPath: string): string | null {
|
|
176
|
+
const rel = relative(root, absPath);
|
|
177
|
+
if (rel.length === 0 || rel.startsWith("..") || isAbsolute(rel)) return null;
|
|
178
|
+
return rel;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function isEnoent(err: unknown): boolean {
|
|
182
|
+
return (err as NodeJS.ErrnoException | null)?.code === "ENOENT";
|
|
183
|
+
}
|
|
184
|
+
|
|
143
185
|
/**
|
|
144
186
|
* The narrow opencode surface the bridge needs (plan §3.2, §14.7, §11).
|
|
145
187
|
*
|
|
@@ -163,6 +205,24 @@ function isHiddenSessionEvent(event: unknown, hiddenSessionID: string): boolean
|
|
|
163
205
|
* version may map).
|
|
164
206
|
*/
|
|
165
207
|
function implementFacade(ctx: Context, hiddenSessionID: string | null): OpencodeFacade {
|
|
208
|
+
const locationDir = ctx.location.directory;
|
|
209
|
+
|
|
210
|
+
const readTextFile = async (absPath: string): Promise<string | null> => {
|
|
211
|
+
try {
|
|
212
|
+
return await fsReadFile(absPath, "utf-8");
|
|
213
|
+
} catch (err) {
|
|
214
|
+
if (isEnoent(err)) return null; // absent → null (caller decides)
|
|
215
|
+
throw err; // other errors propagate (host maps them)
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
const removeFile = async (absPath: string): Promise<void> => {
|
|
219
|
+
try {
|
|
220
|
+
await rm(absPath);
|
|
221
|
+
} catch (err) {
|
|
222
|
+
if (!isEnoent(err)) throw err; // already gone → no-op
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
166
226
|
return {
|
|
167
227
|
async listCatalog() {
|
|
168
228
|
const [providers, models] = await Promise.all([ctx.provider.list(), ctx.model.list()]);
|
|
@@ -285,6 +345,65 @@ function implementFacade(ctx: Context, hiddenSessionID: string | null): Opencode
|
|
|
285
345
|
if (mapped !== null) yield mapped;
|
|
286
346
|
}
|
|
287
347
|
},
|
|
348
|
+
// ── Phase 2: changesets (plan §14.10) ────────────────────────────────
|
|
349
|
+
async vcsDiffWorking() {
|
|
350
|
+
const root = await gitRepoRoot(locationDir);
|
|
351
|
+
if (root === null) return []; // not a git repo → no working diff
|
|
352
|
+
try {
|
|
353
|
+
// `ctx.vcs.diff` returns `{ location, data: FileDiff.Info[] }` with
|
|
354
|
+
// repo-root-relative `file` paths — join to the repo root for the
|
|
355
|
+
// facade's absolute-path contract.
|
|
356
|
+
const res = await ctx.vcs.diff({ mode: "working", untracked: "files" });
|
|
357
|
+
return res.data.map((d) => ({
|
|
358
|
+
path: join(root, d.file),
|
|
359
|
+
additions: d.additions,
|
|
360
|
+
deletions: d.deletions,
|
|
361
|
+
status: d.status,
|
|
362
|
+
}));
|
|
363
|
+
} catch {
|
|
364
|
+
return []; // R2: degrade to empty, never throw
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
async gitHeadFile(absPath: string) {
|
|
368
|
+
const root = await gitRepoRoot(locationDir);
|
|
369
|
+
if (root === null) return null;
|
|
370
|
+
const rel = repoRelative(root, absPath);
|
|
371
|
+
if (rel === null) return null;
|
|
372
|
+
try {
|
|
373
|
+
return await git(root, ["show", `HEAD:${rel}`]);
|
|
374
|
+
} catch {
|
|
375
|
+
return null; // no HEAD / not tracked / failed → absent
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
async readFile(absPath: string) {
|
|
379
|
+
return readTextFile(absPath);
|
|
380
|
+
},
|
|
381
|
+
async writeFile(absPath: string, content: string) {
|
|
382
|
+
await mkdir(dirname(absPath), { recursive: true });
|
|
383
|
+
await fsWriteFile(absPath, content, "utf-8");
|
|
384
|
+
},
|
|
385
|
+
async deleteFile(absPath: string) {
|
|
386
|
+
await removeFile(absPath);
|
|
387
|
+
},
|
|
388
|
+
async discardFile(absPath: string) {
|
|
389
|
+
const root = await gitRepoRoot(locationDir);
|
|
390
|
+
const rel = root !== null ? repoRelative(root, absPath) : null;
|
|
391
|
+
if (root === null || rel === null) {
|
|
392
|
+
await removeFile(absPath); // not in a repo → just delete
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
try {
|
|
396
|
+
await git(root, ["ls-files", "--error-unmatch", "--", rel]);
|
|
397
|
+
} catch {
|
|
398
|
+
await removeFile(absPath); // untracked (or staged-new) → delete
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
try {
|
|
402
|
+
await git(root, ["checkout", "HEAD", "--", rel]);
|
|
403
|
+
} catch {
|
|
404
|
+
await removeFile(absPath); // in the index but not in HEAD → delete
|
|
405
|
+
}
|
|
406
|
+
},
|
|
288
407
|
};
|
|
289
408
|
}
|
|
290
409
|
|
|
@@ -364,6 +483,7 @@ export default Plugin.define({
|
|
|
364
483
|
facade: implementFacade(ctx, hiddenSessionID),
|
|
365
484
|
replayWindow: opts.replayWindow,
|
|
366
485
|
defaultDirectory: ctx.location.directory,
|
|
486
|
+
changesets: opts.capabilities.changesets,
|
|
367
487
|
send: (socketId, message) => {
|
|
368
488
|
listener?.sendTo({ id: socketId }, JSON.stringify(message));
|
|
369
489
|
},
|
|
@@ -372,6 +492,16 @@ export default Plugin.define({
|
|
|
372
492
|
},
|
|
373
493
|
log: (msg) => console.warn(`${TAG} ${msg}`),
|
|
374
494
|
});
|
|
495
|
+
// TLS material (Phase 3): read the key/cert files here so a
|
|
496
|
+
// missing/unreadable file fails before binding and takes the R2
|
|
497
|
+
// degrade path below — the transport itself stays
|
|
498
|
+
// filesystem-free (it receives the PEM contents).
|
|
499
|
+
const tls = opts.tls
|
|
500
|
+
? {
|
|
501
|
+
key: await fsReadFile(opts.tls.keyPath, "utf8"),
|
|
502
|
+
cert: await fsReadFile(opts.tls.certPath, "utf8"),
|
|
503
|
+
}
|
|
504
|
+
: null;
|
|
375
505
|
listener = createListener(
|
|
376
506
|
{
|
|
377
507
|
host: opts.host,
|
|
@@ -380,7 +510,7 @@ export default Plugin.define({
|
|
|
380
510
|
healthPath: opts.healthPath,
|
|
381
511
|
token,
|
|
382
512
|
allowedOrigins: opts.allowedOrigins,
|
|
383
|
-
tls
|
|
513
|
+
tls,
|
|
384
514
|
},
|
|
385
515
|
{
|
|
386
516
|
onOpen: (info) => host.onOpen(info.id),
|
package/src/options.ts
CHANGED
|
@@ -71,7 +71,9 @@ export const BUILTIN_OPTIONS: Options = {
|
|
|
71
71
|
keepAlive: true,
|
|
72
72
|
keepAliveIntervalMinutes: 10,
|
|
73
73
|
replayWindow: 1000,
|
|
74
|
-
|
|
74
|
+
// Phase 2: changesets are on by default (opt out with
|
|
75
|
+
// `capabilities.changesets: false`).
|
|
76
|
+
capabilities: { changesets: true, terminals: false, automations: false },
|
|
75
77
|
};
|
|
76
78
|
|
|
77
79
|
export function isRecord(v: unknown): v is Record<string, unknown> {
|
package/src/state.ts
CHANGED
|
@@ -6,14 +6,18 @@
|
|
|
6
6
|
*
|
|
7
7
|
* - root channel: `rootReducer` (Phase 0);
|
|
8
8
|
* - session channels: `sessionReducer` (Phase 1);
|
|
9
|
-
* - chat channels: `chatReducer` (Phase 1)
|
|
9
|
+
* - chat channels: `chatReducer` (Phase 1);
|
|
10
|
+
* - changeset channels: `changesetReducer` (Phase 2).
|
|
10
11
|
*/
|
|
11
12
|
import {
|
|
12
13
|
chatReducer,
|
|
14
|
+
changesetReducer,
|
|
13
15
|
rootReducer,
|
|
14
16
|
sessionReducer,
|
|
15
17
|
type ChatAction,
|
|
16
18
|
type ChatState,
|
|
19
|
+
type ChangesetAction,
|
|
20
|
+
type ChangesetState,
|
|
17
21
|
type RootAction,
|
|
18
22
|
type RootState,
|
|
19
23
|
type SessionAction,
|
|
@@ -41,6 +45,13 @@ export interface ChatStateStore {
|
|
|
41
45
|
apply(action: ChatAction): ChatState;
|
|
42
46
|
}
|
|
43
47
|
|
|
48
|
+
export interface ChangesetStateStore {
|
|
49
|
+
/** Current changeset state (the `…/changeset/…` snapshot payload). */
|
|
50
|
+
readonly state: ChangesetState;
|
|
51
|
+
/** Apply a changeset-channel action through the official reducer. */
|
|
52
|
+
apply(action: ChangesetAction): ChangesetState;
|
|
53
|
+
}
|
|
54
|
+
|
|
44
55
|
export function createRootStateStore(initial: RootState): RootStateStore {
|
|
45
56
|
let state: RootState = initial;
|
|
46
57
|
return {
|
|
@@ -79,3 +90,16 @@ export function createChatStateStore(initial: ChatState): ChatStateStore {
|
|
|
79
90
|
},
|
|
80
91
|
};
|
|
81
92
|
}
|
|
93
|
+
|
|
94
|
+
export function createChangesetStateStore(initial: ChangesetState): ChangesetStateStore {
|
|
95
|
+
let state: ChangesetState = initial;
|
|
96
|
+
return {
|
|
97
|
+
get state(): ChangesetState {
|
|
98
|
+
return state;
|
|
99
|
+
},
|
|
100
|
+
apply(action: ChangesetAction): ChangesetState {
|
|
101
|
+
state = changesetReducer(state, action);
|
|
102
|
+
return state;
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
package/src/transport.ts
CHANGED
|
@@ -104,7 +104,14 @@ export interface ListenerOptions {
|
|
|
104
104
|
/** When null, a non-loopback bind refuses to start (R4). */
|
|
105
105
|
token: string | null;
|
|
106
106
|
allowedOrigins: string[] | "*";
|
|
107
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Native TLS termination → `wss://` (off-LAN transports, D3). The PEM
|
|
109
|
+
* **contents** of the key and certificate — not paths: `Bun.serve`'s
|
|
110
|
+
* `tls.key`/`tls.cert` take contents (string/Buffer/BunFile), and the
|
|
111
|
+
* plugin entry reads the files (the transport stays filesystem-free so
|
|
112
|
+
* its unit tests never touch the disk).
|
|
113
|
+
*/
|
|
114
|
+
tls?: { key: string; cert: string } | null;
|
|
108
115
|
}
|
|
109
116
|
|
|
110
117
|
/** Transport-local connection identity. */
|
|
@@ -198,7 +205,7 @@ export function createListener(opts: ListenerOptions, callbacks: TransportCallba
|
|
|
198
205
|
const server = bun.serve({
|
|
199
206
|
hostname: opts.host,
|
|
200
207
|
port: opts.port,
|
|
201
|
-
...(opts.tls ? { tls: { key: opts.tls.
|
|
208
|
+
...(opts.tls ? { tls: { key: opts.tls.key, cert: opts.tls.cert } } : {}),
|
|
202
209
|
fetch(req, srv) {
|
|
203
210
|
const url = new URL(req.url);
|
|
204
211
|
switch (routePath(url, { ahp: opts.path, health: opts.healthPath })) {
|