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

188 lines (187 loc) • 7.59 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/. /** * The `akm-task` adapter for task `.yml` sources. * * A native akm task-YAML bundle. A `.yml` file derives * `type: task`; conceptId strips the `.yml` extension. Tasks are AKM-native * YAML, NOT OKF markdown concepts. Recognition does NOT gate on validity — an * invalid task (e.g. `uses` plus `run`) is still RECOGNIZED; the `invalid-task-yaml` * violation surfaces only in `validate`. * * `.yaml` is the one extension `validate` inspects but `recognize` refuses: it * is not a task spelling (nothing indexes or schedules it), so it is reported * as `invalid-task-yaml` rather than silently skipped (issue #760). * * ── validate ── * * Validation enters the canonical task source parser (`parseTaskSource`, * which reads only task source v4 — a `version: 2` or `version: 3` * document fails closed with `TASK_SCHEMA_VERSION_UNSUPPORTED`, naming * `akm migrate apply`, which converts it). That parser owns the closed key sets, * the executable-selector XOR, hostile YAML policy, the `akm/command` * builtin, and bounds. The adapter only translates a parser failure into * the format-family diagnostic shape. * * Conformance oracle (authored, DO NOT modify): fixture * `tests/fixtures/bundles/akm-task/` + goldens * `tests/fixtures/format-family-goldens/akm-task/{recognition,lint,renderer}.json`. */ import fs from "node:fs"; import path from "node:path"; import { parseTaskSource, peekTaskSourceVersion } from "../../../tasks/source/parse-task-source.js"; import { TASK_EXTENSION, TASK_NEAR_MISS_EXTENSION, taskExtensionDetail } from "../../asset/asset-placement.js"; import { readBoundedTaskSourceYaml, taskSourceErrorDetail } from "../../asset/bounded-task-document.js"; import { toPosix } from "../../common.js"; import { DirectoryAdapterBase } from "../directory-adapter.js"; import { hashContent } from "./shared.js"; function describe(file, conceptId) { const raw = file.content(); return { hash: hashContent(raw), type: "task", name: conceptId.split("/").pop() ?? conceptId, content: raw }; } /** * Every `.yaml` file of the bundle, by path. A `.yaml` file is no task, so the bundle does not list it and no change * names it: it is found by scanning the bundle through {@link FileValidateContext.list}, to say what it is (issue #760). */ async function nearMissFiles(ctx, dir = ".") { const found = []; for (const name of await ctx.list(dir)) { if (name.startsWith(".") || name === "node_modules") continue; const relPath = dir === "." ? name : `${dir}/${name}`; if (path.posix.extname(name).toLowerCase() === TASK_NEAR_MISS_EXTENSION) found.push(relPath); else found.push(...(await nearMissFiles(ctx, relPath))); } return found; } async function validate(_c, changes, ctx) { const diagnostics = []; const changed = new Set(changes.map((change) => toPosix(change.path))); for (const relPath of await nearMissFiles(ctx)) { if (changed.has(relPath)) continue; // reported with the changes below diagnostics.push({ file: relPath, issue: "invalid-task-yaml", detail: taskExtensionDetail(relPath), fixed: false, }); } for (const change of changes) { if (change.op === "delete") continue; const raw = change.after ?? (await ctx.readFile(change.path)); if (typeof raw !== "string") continue; const ext = path.extname(change.path).toLowerCase(); // `.yaml` is NOT a task extension — the file never indexes and never runs. // It is validated here purely so the near miss is REPORTED rather than // skipped the way every other extension is (issue #760). if (ext !== TASK_EXTENSION && ext !== TASK_NEAR_MISS_EXTENSION) continue; const relPath = toPosix(change.path); if (ext === TASK_NEAR_MISS_EXTENSION) { diagnostics.push({ file: relPath, issue: "invalid-task-yaml", detail: taskExtensionDetail(relPath), fixed: false, }); continue; } try { parseTaskSource({ yaml: raw, filePath: relPath }); } catch (cause) { diagnostics.push({ file: relPath, issue: "invalid-task-yaml", detail: taskSourceErrorDetail(cause), fixed: false, }); } } return diagnostics; } export class AkmTaskAdapter extends DirectoryAdapterBase { id = "akm-task"; version = "0.9.2"; /** A `.yml` file is a task; its concept id is its path without the extension. */ identify(relPath) { return path.posix.extname(relPath).toLowerCase() === TASK_EXTENSION ? toPosix(relPath).replace(/\.yml$/i, "") : null; } describe(_b, file, conceptId) { return describe(file, conceptId); } validateFiles(b, changes, ctx) { return validate(b, changes, ctx); } spellings(conceptId) { return [`${toPosix(conceptId).replace(/\.ya?ml$/i, "")}.yml`]; } /** Tasks live anywhere under the bundle root. */ directoryList() { return ["."]; } /** * Install-time probe (§1.2): a root holding a top-level, valid task source * v4 `.yml` file, or a task v2/v3 file that `akm migrate apply` has yet to * convert (the runtime refuses those, so without this the migrator and * `akm task sync` would not find them in a bundle whose adapter config does * not record). The full parser keeps this disjoint from unrelated YAML and * prevents probe semantics from drifting from validation semantics. */ looksLikeRoot(root) { let entries; try { entries = fs.readdirSync(root, { withFileTypes: true }); } catch { return false; } for (const entry of entries) { if (!entry.isFile() || path.extname(entry.name).toLowerCase() !== TASK_EXTENSION) continue; let raw; try { raw = fs.readFileSync(path.join(root, entry.name), "utf8"); } catch { continue; } try { parseTaskSource({ yaml: raw, filePath: entry.name }); return true; } catch { if (isLegacyTaskDocument(raw, entry.name)) return true; // Continue probing the remaining top-level .yml files. } } return false; } } export const akmTaskAdapter = new AkmTaskAdapter(); /** The executable keys each retired task schema version required one of. */ const LEGACY_TASK_TARGET_KEYS = { 2: ["workflow", "prompt", "command"], 3: ["uses", "run"], }; /** A task v2/v3 document: its schema version, plus a target key that version used. */ function isLegacyTaskDocument(yaml, filePath) { let root; try { root = readBoundedTaskSourceYaml({ yaml, filePath }, { sourceLabel: "task source" }).root; } catch { return false; } const keys = LEGACY_TASK_TARGET_KEYS[peekTaskSourceVersion(root) ?? 0]; return keys?.some((key) => Object.hasOwn(root, key)) === true; }