akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
77 lines (76 loc) • 3.41 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/**
* Which OpenCode major an installed `opencode` binary is.
*
* The opencode harness runs the installed binary through its CLI for both majors;
* the binary's major decides the flags (`./agent-builder.ts`). Plugin upgrades also
* ask it, to refresh a plugin cache with the major that owns it.
* {@link detectOpencodeMajor} runs `<bin> --version` once per binary per process. OpenCode 1 is
* deprecated; the run notice is `warnOpencode1Use` (`cli/deprecations.ts`), which the builder calls.
*/
import { defaultWhich, probeVersion } from "../../agent/detect.js";
/**
* The OpenCode CLI binary: the opencode harness runs it and asks it for its major,
* and plugin upgrades refresh OpenCode's plugin caches with it. akm's own runtime
* does not use it: it runs a binary of its own (`./runtime/binary.ts`).
*/
export const OPENCODE_BIN = "opencode";
/** OpenCode 2's own alias for its binary; it can sit next to an OpenCode 1 `opencode` on PATH. */
const OPENCODE2_BIN = "opencode2";
const VERSION_TIMEOUT_MS = 30_000;
const defaultProbe = (bin) => probeVersion(bin, VERSION_TIMEOUT_MS).version;
let probeOverride;
const detected = new Map();
const onPath = new Map();
/** TEST-ONLY. Swap the `--version` probe (and forget every cached detection and PATH lookup); pass undefined to restore. */
export function _setOpencodeVersionProbeForTests(fake) {
probeOverride = fake;
detected.clear();
onPath.clear();
}
/** `which`, memoized for the process. */
function lookup(bin) {
if (!onPath.has(bin))
onPath.set(bin, defaultWhich(bin));
return onPath.get(bin);
}
/**
* The major in `opencode --version` output (`1.18.34`, `opencode v2.0.26`), or
* undefined when the output is not an OpenCode version (another program's
* `--version`, such as `true (GNU coreutils) 9.7`).
*/
function parseOpencodeMajor(versionOutput) {
const match = versionOutput.trim().match(/^(?:opencode\s+)?v?(\d+)\.\d+(?:\.\d+)?(?:[-+.\w]*)$/im);
return match ? Number(match[1]) : undefined;
}
/**
* The binary plugin upgrade refreshes OpenCode 2's plugin cache with: the most
* recent OpenCode on PATH (`opencode2` when present, else `opencode`). The harness
* itself runs `opencode`, or the `bin` its config names.
*/
export function resolveOpencodeBin() {
return lookup(OPENCODE2_BIN) ? OPENCODE2_BIN : OPENCODE_BIN;
}
/** Detect the OpenCode major of `bin`: one `--version` run per binary per process. */
export function detectOpencodeMajor(bin) {
const path = lookup(bin) ?? bin;
const cached = detected.get(path);
if (cached)
return cached;
const raw = (probeOverride ?? defaultProbe)(path);
const version = raw ?? "";
const reportedMajor = raw === undefined ? undefined : parseOpencodeMajor(raw);
const info = {
...(reportedMajor !== undefined ? { reportedMajor } : {}),
version,
runnable: raw !== undefined,
};
detected.set(path, info);
return info;
}
/** Whether `bin` is OpenCode 2 or later: its CLI takes the flags the builder passes. A binary whose version cannot be read counts as OpenCode 1. */
export function isOpencode2(bin) {
return (detectOpencodeMajor(bin).reportedMajor ?? 0) >= 2;
}