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
72 lines (71 loc) • 2.96 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/.
/**
* Where each OpenCode major installs the akm plugin (#1049). `akm upgrade`
* (`commands/sources/plugin-upgrade.ts`) refreshes these caches and `akm health`
* (`commands/health/plugin-staleness.ts`) reads the akm-cli bundled in them, so
* the layout is written down once, here.
*
* OpenCode 1 and OpenCode 2 share `~/.config/opencode` and `~/.cache/opencode`
* but lay both out differently, and each major only loads its own package, so a
* host is handled per flavor and a flavor never touches the other's package,
* cache or config entry.
*/
import fs from "node:fs";
import path from "node:path";
import { tryParseJson } from "../../../core/common.js";
/** The newest numbered build folder (`<epoch-ms>`) under an OpenCode 2 cache folder. */
export function newestBuildDir(folder) {
try {
const builds = fs.readdirSync(folder).filter((name) => /^\d+$/.test(name));
builds.sort((a, b) => Number(b) - Number(a));
return builds[0] ? path.join(folder, builds[0]) : undefined;
}
catch {
return undefined;
}
}
/** A package.json's `version`, or undefined when the file is missing, unreadable or has none. */
export function readPackageVersion(file) {
let text;
try {
text = fs.readFileSync(file, "utf8");
}
catch {
return undefined;
}
const pkg = tryParseJson(text);
return typeof pkg?.version === "string" ? pkg.version : undefined;
}
export const OPENCODE_V1 = {
harness: "opencode",
pkg: "akm-opencode",
major: 1,
configKeys: ["plugin"],
globalConfigOnly: false,
// OpenCode 1: <cache>/opencode/packages/<spec>/node_modules/<pkg>
cacheFolder: (spec) => path.join("packages", spec),
nodeModulesDir: (folder) => path.join(folder, "node_modules"),
// Any OpenCode command that resolves the config installs the plugin again.
refresh: () => ["debug", "config"],
replacesCache: true,
};
export const OPENCODE_V2 = {
harness: "opencode-v2",
pkg: "akm-opencode-v2",
major: 2,
// OpenCode 2.0.26 reads both keys; its own `plugin add` writes `plugins`.
configKeys: ["plugins", "plugin"],
globalConfigOnly: true,
// OpenCode 2: <cache>/opencode/npm/<spec>/<epoch-ms>/node_modules/<pkg>
cacheFolder: (spec) => path.join("npm", spec),
nodeModulesDir: (folder) => {
const build = newestBuildDir(folder);
return build ? path.join(build, "node_modules") : undefined;
},
// `plugin update <spec>` (the spec as the config names it) installs the new build into a fresh
// `<epoch-ms>` folder and leaves the old ones; the running service stays on the old build until it restarts.
refresh: (spec) => ["plugin", "update", spec],
replacesCache: false,
};