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.4

Permissions in 0.1.4

  • Provide agents agents.provideMediumAdds agents to the app.Provide the OpenCode 2 agent, and serve plugin tools to it through the host's loopback MCP server
  • Run named programs processMediumStarts the listed programs.Run the OpenCode 2 server (`opencode serve`, or the binary you choose in Settings), read its version and update it (`opencode upgrade`), and ask npm for the newest versionPrograms: opencode${settings.binaryPath}npm
  • Network access netMediumConnects to the listed hosts.Talk to the OpenCode server it starts on this computer, at the port it picks for each launch, to the OpenCode background service on this computer, and to the external server you choose in SettingsHosts: localhost:*${settings.serverUrl}
  • Environment variables envMediumReads the listed environment variables.Read the user name and password of the OpenCode server, and where OpenCode keeps its background service recordVariables: OPENCODE_SERVER_USERNAMEOPENCODE_SERVER_PASSWORDDIVERGENCE_OPENCODE_SERVER_PASSWORDXDG_STATE_HOMEHOME
  • Read files fs.readMediumReads files in the listed places.Read the record of the OpenCode background service (its address and password) so chats use the service you already run instead of starting another serverPlaces: ~/.local/state/opencode/service.json

Files

service.test.ts6.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 * as z from "zod";
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);
});

// A service that stopped while no event stream was open must not leave every call failing until
// the plugin reloads.
test("a discovered service that stops taking connections is forgotten", async () => {
  const { agent, server, run } = discovery(record);
  assert.equal(await run(agent.base()), SERVICE);
  server.routes = () => {
    throw new Error("connect ECONNREFUSED 127.0.0.1:49374");
  };
  await assert.rejects(run(agent.call("GET", "/api/provider", z.unknown())), /ECONNREFUSED/);
  assert.equal(agent.service, null);
  assert.equal(agent.external, null, "the next call looks for the service again or starts a server");
});

test("no update is offered for a discovered service, which the plugin does not run", async () => {
  const { agent, run } = discovery(record);
  const info = await run(agent.initialize());
  assert.equal(info.status?.state, "ready");
  assert.equal(info.maintenance, undefined);
});

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");
});

Versions

VersionPublishedPlugin APISizePermissionsStatus
0.1.5latestNov 21, 2026>=5 <672.4 KB5 permissionsListed
0.1.4Nov 16, 2026>=5 <672 KB5 permissionsListed
0.1.3Nov 11, 2026>=5 <662.4 KB5 permissionsListed
0.1.2Nov 9, 2026>=5 <659.2 KB5 permissionsListed
0.1.1Oct 27, 2026>=2 <458.4 KB5 permissionsListed

Reviews and comments

0 threads · 0 reviews

No comments yet.