UNPKG

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

157 lines (156 loc) • 7.55 kB
// 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/. import fs from "node:fs"; import path from "node:path"; import { isDeepStrictEqual } from "node:util"; import { applyEdits, modify } from "jsonc-parser"; import { parse as parseToml } from "smol-toml"; import { existingFileMode, parseJsonc, timestampForFilename, writeFileAtomic } from "../../core/common.js"; /** Preserve unrelated settings and comments; only the named native key changes. */ export function editChannelJson(file, keys, value, content) { // asset-io: install harness plugin settings const before = content ?? fs.readFileSync(file, "utf8"); parseJsonc(before); const after = applyEdits(before, modify(before, keys, value, { formattingOptions: { insertSpaces: true, tabSize: 2 } })); parseJsonc(after); return { file, content: after }; } export function backupChannelFile(file) { const backup = `${file}.akm-channel-${timestampForFilename()}-${process.pid}.bak`; // asset-io: install harness plugin settings fs.copyFileSync(file, backup, fs.constants.COPYFILE_EXCL); fs.chmodSync(backup, existingFileMode(file) ?? 0o600); return backup; } export class ChannelWriteError extends Error { backups; writtenFiles; constructor(cause, backups, writtenFiles) { super(`Channel settings could not all be written: ${String(cause)}. Backups: ${backups.join(", ") || "none"}`); this.backups = backups; this.writtenFiles = writtenFiles; } } export function writeChannelEdits(edits) { const backups = []; const writtenFiles = []; try { // asset-io: install harness plugin settings const changed = edits.filter(({ file, content }) => fs.readFileSync(file, "utf8") !== content); for (const { file } of changed) backups.push(backupChannelFile(file)); for (const { file, content } of changed) { // asset-io: install harness plugin settings writeFileAtomic(file, content, existingFileMode(file)); writtenFiles.push(file); } return backups; } catch (error) { throw new ChannelWriteError(error, backups, writtenFiles); } } /** Claude has no command to change a registered marketplace's ref in place. Never remove the marketplace. */ export function claudeChannelEdits(configDir, pluginsRoot, channel, projectPaths = []) { const ref = channel === "next" ? "next" : undefined; const knownFile = path.join(pluginsRoot, "known_marketplaces.json"); // asset-io: install harness plugin settings const known = parseJsonc(fs.readFileSync(knownFile, "utf8")); const source = known["akm-plugins"]?.source; if (!source || !["github", "git"].includes(source.source ?? "")) { throw new Error("the akm-plugins marketplace has no Git source whose channel can be switched"); } const edits = [editChannelJson(knownFile, ["akm-plugins", "source", "ref"], ref)]; const settingsFiles = [ path.join(configDir, "settings.json"), ...[process.cwd(), ...projectPaths].flatMap((project) => [ path.join(project, ".claude", "settings.json"), path.join(project, ".claude", "settings.local.json"), ]), ]; for (const file of new Set(settingsFiles)) { // asset-io: install harness plugin settings if (!fs.existsSync(file)) continue; // asset-io: install harness plugin settings const settings = parseJsonc(fs.readFileSync(file, "utf8")); if (!settings.extraKnownMarketplaces?.["akm-plugins"]) continue; edits.push(editChannelJson(file, ["extraKnownMarketplaces", "akm-plugins", "source", "ref"], ref)); } return edits; } /** Read native TOML, including quoted keys and inline tables. */ export function codexMarketplace(file) { // asset-io: install harness plugin settings const config = parseToml(fs.readFileSync(file, "utf8")); const marketplaces = config.marketplaces; const marketplace = marketplaces?.["akm-plugins"]; return marketplace && typeof marketplace === "object" ? marketplace : undefined; } /** Codex's add refuses a changed ref on an existing source; preserve the installed plugin and edit only its ref. */ export function codexChannelEdit(file, channel) { // asset-io: install harness plugin settings const before = fs.readFileSync(file, "utf8"); const expected = parseToml(before); const entry = expected.marketplaces?.["akm-plugins"]; if (!entry || entry.source_type !== "git") throw new Error("the akm-plugins marketplace has no Git source whose channel can be switched"); if (channel === "next") entry.ref = "next"; else delete entry.ref; const headers = [...before.matchAll(/^[ \t]*\[(?!\[)([^\]\r\n]+)\][ \t]*(?:#[^\r\n]*)?\r?$/gm)]; let after = before; let edited = false; for (const [index, header] of headers.entries()) { let parsed; try { parsed = parseToml(`[${header[1]}]`); } catch { continue; } const target = parsed.marketplaces?.["akm-plugins"]; if (!target || Object.keys(target).length !== 0) continue; const start = header.index + header[0].length; const end = headers[index + 1]?.index ?? before.length; const section = before.slice(start, end); const refLine = /^[ \t]*(?:ref|"ref"|'ref')[ \t]*=[ \t]*(?:"(?:\\.|[^"\\])*"|'[^']*')([ \t]*(?:#[^\r\n]*)?)(\r?\n|$)/m; const newline = before.includes("\r\n") ? "\r\n" : "\n"; const nextSection = refLine.test(section) ? section.replace(refLine, (_line, comment, end) => channel === "next" ? `ref = "next"${comment}${end}` : comment.trim() ? `${comment.trimStart()}${end}` : "") : channel === "next" ? `${newline}ref = "next"${section}` : section; after = before.slice(0, start) + nextSection + before.slice(end); edited = true; break; } if (!edited) { // Inline tables have no section to insert into. Change just this table's ref and retain every other byte. const inline = /((?:"akm-plugins"|'akm-plugins'|akm-plugins)\s*=\s*\{)([^\n]*?)(\})/g; for (const match of before.matchAll(inline)) { const [, open, body, close] = match; const ref = /(?:^|,)\s*(?:ref|"ref"|'ref')\s*=\s*(?:"(?:\\.|[^"\\])*"|'[^']*')\s*/; const stripped = body.replace(ref, "").replace(/^\s*,/, ""); const next = channel === "next" ? `${stripped.trimEnd()}${stripped.trim() ? ", " : ""}ref = "next" ` : stripped; const candidate = `${before.slice(0, match.index)}${open}${next}${close}${before.slice(match.index + match[0].length)}`; try { if (isDeepStrictEqual(parseToml(candidate), expected)) { after = candidate; break; } } catch { /* Try the next table with this name. */ } } } // Validate the whole semantic document: an unusual layout is never partially rewritten. if (!isDeepStrictEqual(parseToml(after), expected)) throw new Error("could not locate only the akm-plugins ref in Codex config.toml; no settings were changed"); return { file, content: after }; }