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

78 lines (77 loc) • 3.75 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/. /** Shared by the per-harness plugin upgraders: running an external command with a timeout, and the entry shapes they return. */ import * as childProcess from "node:child_process"; import fs from "node:fs"; import path from "node:path"; import { toErrorMessage } from "../../core/common.js"; import { UsageError } from "../../core/errors.js"; import { ChannelWriteError } from "./plugin-channels.js"; export const READ_TIMEOUT_MS = 30_000; export const REFRESH_TIMEOUT_MS = 120_000; export const PREFETCH_TIMEOUT_MS = 180_000; export function runCommand(command, args, timeoutMs, opts) { const result = childProcess.spawnSync(command, args, { encoding: "utf8", env: process.env, stdio: "pipe", timeout: timeoutMs, killSignal: "SIGKILL", cwd: opts?.cwd, }); const label = `${command} ${args.join(" ")}`; if (result.error) { const code = result.error.code; if (code === "ETIMEDOUT") return { ok: false, missing: false, error: `\`${label}\` timed out after ${timeoutMs / 1000}s` }; return { ok: false, missing: code === "ENOENT", error: `\`${label}\` could not run: ${result.error.message}` }; } if (result.status !== 0) { const detail = (result.stderr ?? "").trim() || (result.stdout ?? "").trim() || `exit code ${result.status}`; return { ok: false, missing: false, error: `\`${label}\` failed: ${detail}` }; } return { ok: true, stdout: result.stdout ?? "" }; } /** * Reads one file of the marketplace's channel: in place from a plain local directory (a baked-in image: no clone, no * network), else from a blobless clone at `checkout` with nothing checked out. */ export function marketplaceReader(source, channel, checkout) { // asset-io: install if (fs.statSync(source, { throwIfNoEntry: false })?.isDirectory() && !fs.existsSync(path.join(source, ".git"))) { return (file) => { try { // asset-io: install return { ok: true, stdout: fs.readFileSync(path.join(source, file), "utf8") }; } catch (error) { return { ok: false, missing: false, error: toErrorMessage(error) }; } }; } const branch = channel === "next" ? ["--branch", "next"] : []; const cloneArgs = ["clone", "--depth", "1", "--single-branch", "--filter=blob:none", "--no-checkout", ...branch]; const cloned = runCommand("git", [...cloneArgs, "--", source, checkout], REFRESH_TIMEOUT_MS); if (!cloned.ok) throw new UsageError(`Could not check the target plugin version: ${cloned.error}`); return (file) => runCommand("git", ["-C", checkout, "show", `HEAD:${file}`], REFRESH_TIMEOUT_MS); } export function skipped(harness, message) { return { harness, outcome: "skipped", message }; } export function failed(harness, message) { return { harness, outcome: "failed", message }; } export function channelWriteFailure(harness, error, before) { return { ...failed(harness, String(error)), channel: error instanceof ChannelWriteError && error.writtenFiles.length ? "unknown" : before, ...(error instanceof ChannelWriteError ? { backups: error.backups } : {}), }; } /** An entry for a refresh that ran: `updated` when the installed version moved, else `current`. */ export function refreshed(harness, from, to) { const moved = from !== undefined && to !== undefined && from !== to; return { harness, outcome: moved ? "updated" : "current", from, to }; }