Official
opencode-v2
OpenCode 2 agent provider: runs opencode serve (2.x) and talks to its /api over HTTP and server-sent events.
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/opencode-v2@0.1.2
Permissions in 0.1.2
Files
service.test.ts5.7 KB
// Discovery of the OpenCode background service (`service.json`), and the process reports of the
// server this plugin uses.
import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import * as Scope from "effect/Scope";
import type { ChildProcess } from "divergence";
import { diskFs } from "../sdk/testing.ts";
import type { ProcessReport } from "../sdk/agent.ts";
import { OWN_PROCESS } from "./agent.ts";
import { LocalServer, discoverService, servicePath } from "./server.ts";
import { harness } from "./testing.test.ts";
const SERVICE = "http://127.0.0.1:49374";
/// A home folder holding `service.json` (or none), the harness reading it, and the requests the
/// service was asked.
function discovery(record: Record<string, unknown> | null, info: { status?: number; body?: unknown } = {}) {
const home = mkdtempSync(join(tmpdir(), "oc-service-"));
const path = servicePath({ HOME: home });
assert.equal(path, `${home}/.local/state/opencode/service.json`);
if (record) {
const dir = join(home, ".local", "state", "opencode");
mkdirSync(dir, { recursive: true });
writeFileSync(join(dir, "service.json"), JSON.stringify(record));
}
const context = harness({
fs: diskFs(),
routes: (call) =>
call.path === "/api/info" ? { status: 200, body: { version: "2.0.16", pid: 55217 }, ...info } : undefined,
});
context.agent.config = { binaryPath: null, serverUrl: null };
context.agent.env = { HOME: home };
return { ...context, home, path: path as string };
}
const record = { id: "svc", version: "2.0.16", url: SERVICE, pid: 55217, password: "s3cret" };
const basic = (password: string) => `Basic ${btoa(`opencode:${password}`)}`;
test("a registered background service is used and reported as external", async () => {
const { agent, api, run } = discovery(record);
const reports: ProcessReport[] = [];
agent.process = (report) => reports.push(report);
assert.equal(await run(agent.base()), SERVICE);
assert.equal(agent.external, SERVICE);
assert.equal(agent.configured, null);
assert.deepEqual(agent.auth, { username: "opencode", password: "s3cret" });
assert.equal(
(api.fetches[0]?.headers as Record<string, string>)["authorization"],
basic("s3cret"),
"the liveness check carries the file's password",
);
assert.deepEqual(reports, [
{ processId: `opencode-v2:external:${SERVICE}`, state: "started", sessions: [], shared: true, external: true },
]);
// The service is found once, not on every request.
await run(agent.base());
assert.equal(api.fetches.length, 1);
});
test("a stale file, a version mismatch, a refusal and no file all fall through", async () => {
const stale = discovery(record, { body: { version: "2.0.16", pid: 1 } });
assert.equal(await stale.run(discoverService(stale.path)), null, "another process answers");
const old = discovery({ ...record, version: "2.0.15" });
assert.equal(await old.run(discoverService(old.path)), null, "a newer CLI replaced the service");
const refused = discovery(record, { status: 401, body: { message: "no" } });
assert.equal(await refused.run(discoverService(refused.path)), null, "the password does not open it");
const none = discovery(null);
assert.equal(await none.run(discoverService(none.path)), null, "no file");
assert.equal(none.api.fetches.length, 0, "nothing is asked without a file");
const remote = discovery({ ...record, url: "http://203.0.113.9:4096" });
assert.equal(await remote.run(discoverService(remote.path)), null, "only a loopback service");
assert.equal(remote.api.fetches.length, 0);
});
test("a configured server wins over the registered service", async () => {
const { agent, api, run } = discovery(record);
agent.config = { binaryPath: null, serverUrl: "http://127.0.0.1:4096" };
assert.equal(await run(agent.base()), "http://127.0.0.1:4096");
assert.equal(api.fetches.length, 0, "the file is not even read");
});
test("an external server is never stopped, the plugin's own server is", async () => {
const { agent, run } = discovery(record);
const reports: ProcessReport[] = [];
agent.process = (report) => reports.push(report);
await run(agent.base());
await run(agent.stopProcess(`opencode-v2:external:${SERVICE}`));
await run(agent.stopProcess(OWN_PROCESS));
assert.equal(agent.service?.base, SERVICE, "the service is still in use");
assert.deepEqual(
reports.map((report) => report.state),
["started"],
);
const own = harness();
own.agent.config = { binaryPath: null, serverUrl: null };
const ownReports: ProcessReport[] = [];
own.agent.process = (report) => ownReports.push(report);
const server = new LocalServer({ exited: new Promise(() => {}) } as unknown as ChildProcess, Scope.makeUnsafe());
server.base = "http://127.0.0.1:5000";
own.agent.server = server;
own.agent.announce(OWN_PROCESS);
own.agent.sessions.set("ses_1", {
workspace: "/w",
options: {},
tools: [],
instructions: null,
applied: { model: null, agent: null, mode: null, instructions: null },
});
own.agent.report("sessions");
assert.deepEqual(ownReports.at(-1), {
processId: OWN_PROCESS,
state: "sessions",
sessions: ["ses_1"],
shared: true,
external: false,
});
await own.run(own.agent.stopProcess("someone-else"));
assert.equal(own.agent.server, server, "another process id is left alone");
await own.run(own.agent.stopProcess(OWN_PROCESS));
assert.equal(own.agent.server, null);
assert.deepEqual(ownReports.at(-1), {
processId: OWN_PROCESS,
state: "stopped",
sessions: [],
shared: true,
external: false,
});
assert.ok(own.agent.sessions.has("ses_1"), "the session stays known and resumes on the next request");
});
No comments yet.