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

101 lines (100 loc) • 4.9 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 { rootOf } from "../../core/adapter/types.js"; import { compileWorkflowSource } from "../../workflows/compile.js"; import { isUsableWorkflowSource, workflowNameForSourcePath } from "../../workflows/source-files.js"; import { buildMetadataSkipWarning } from "../passes/metadata.js"; import { indexDocumentToStashEntry } from "./doc-to-entry.js"; /** The markdown-workflow renderer name the `akm` adapter carries on `documentJson.renderer`. */ const WORKFLOW_MD_RENDERER = "workflow-md"; /** * The durable entry of a recognized document, as the scan, a write-path refresh and the facade's refresh all record * it. An entry no file holds (a secret manager's) has no path; `fileName` is its name. */ export function indexRowEntry(doc, fileName) { const entry = indexDocumentToStashEntry(doc); if (doc.path === undefined) entry.filename = fileName; return entry; } /** * Drain one directory's recognized documents into durable entries. * * `fileContexts` are the dir's walked files (the drain does not pre-filter — * filtering is adapter-owned). `adapter.recognize` returns * `null` for a file it abstains on (no matcher claims it, an OKF reserved file, * or an AKM sensitive/infra file) — silently skipped. */ export function drainDirDocuments(adapter, bundle, fileContexts) { const entries = []; const warnings = []; const hashByFile = new Map(); const conceptIdByFile = new Map(); for (const file of fileContexts) { const doc = adapter.recognize(bundle, file); if (doc === null) continue; if (!doc.conceptId) { warnings.push(`Skipped ${file.absPath}: adapter "${adapter.id}" returned no conceptId.`); continue; } const entry = indexRowEntry(doc, file.fileName); // Workflow docs: drop-with-warning if broken; otherwise cache a lossless // runtime projection when the current executor can represent the source. const dropWarning = brokenWorkflowWarning(doc, file); if (dropWarning !== null) { warnings.push(dropWarning); continue; } // A symlinked workflow source of another extension is warned about once, as the scan always has. if (doc.type === "workflow" && workflowNameForSourcePath(rootOf(bundle), adapter.id, file.absPath) !== undefined) { isUsableWorkflowSource(rootOf(bundle), file.absPath); } if (doc.hash !== undefined) hashByFile.set(file.absPath, doc.hash); conceptIdByFile.set(file.absPath, doc.conceptId); entries.push(entry); } return { entries, warnings, hashByFile, conceptIdByFile }; } /** * If `doc` is a workflow, compile it through source IR: return a * `Skipped workflow …` drop warning when it is broken, or return `null` when * it compiles. Non-workflow docs return `null` immediately. A broken workflow is no row of the index, whoever * records the row: the scan, a write, or the first ask of an asset that has none. */ export function brokenWorkflowWarning(doc, file) { if (doc.type !== "workflow") return null; // asset-io: deprecated workflows are not read through the adapters const workflowBundle = doc.adapterId === "akm-workflow"; // asset-io: deprecated workflows are not read through the adapters const akmBundle = doc.adapterId === "akm"; if (!workflowBundle && !(akmBundle && docRenderer(doc) === WORKFLOW_MD_RENDERER)) return null; const result = compileWorkflowSource(file.content(), { path: file.relPath }); if (!result.ok) return workflowDropWarning(file, result.errors); return null; } /** The winning renderer name the `akm` adapter carries on `documentJson.renderer`, or `undefined`. */ function docRenderer(doc) { const dj = doc.documentJson; if (dj !== null && typeof dj === "object" && "renderer" in dj) { const renderer = dj.renderer; return typeof renderer === "string" ? renderer : undefined; } return undefined; } /** * Build the `Skipped workflow <path>:\n…` warning byte-for-byte the way the live * pipeline did: the workflow parser's `path:line — message` summary wrapped in * the `Workflow has errors:` prefix (the string `loadDocument`/`loadProgram` * threw), then {@link buildMetadataSkipWarning}'s workflow branch. `startsWith * "Skipped workflow "` so `isWorkflowSkipWarning` counts it for the summary. */ function workflowDropWarning(file, errors) { const summary = errors.map((e) => `${file.relPath}:${e.line} — ${e.message}`).join("\n"); return buildMetadataSkipWarning(file.absPath, "workflow", `Workflow has errors:\n${summary}`); }