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

208 lines (207 loc) • 9.83 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/. /** * Sources / registries / installed entries + the 0.9.0 `bundles` shape. * Extracted verbatim from the former `config-schema.ts` monolith — no behavior * change. */ import { z } from "zod"; import { isBundleSlug } from "../../asset/asset-ref.js"; import { httpUrl, isApiKeyReference, nonEmptyString, positiveInt } from "./primitives.js"; // ── Sources / registries / installed ──────────────────────────────────────── const SourceConfigEntryOptionsSchema = z.record(z.string(), z.unknown()); export const SourceConfigEntrySchema = z .object({ type: nonEmptyString, path: z.string().min(1).optional(), url: z.string().min(1).optional(), name: z.string().min(1).optional(), enabled: z.boolean().optional(), writable: z.boolean().optional(), credential: z.string().min(1).optional(), location: z.string().min(1).optional(), primary: z.boolean().optional(), options: SourceConfigEntryOptionsSchema.optional(), }) .passthrough() .superRefine((entry, ctx) => { if (entry.options && "pushOnCommit" in entry.options) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["options", "pushOnCommit"], message: "options.pushOnCommit is not supported", }); } if (!["filesystem", "git", "website", "npm", "location"].includes(entry.type)) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["type"], message: `unsupported source type "${entry.type}"; expected filesystem, git, website, npm, or location`, }); } if (entry.writable === true && (entry.type === "website" || entry.type === "npm")) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `writable: true is only supported on filesystem, git and location sources (got "${entry.type}"` + (entry.name ? ` on source "${entry.name}"` : "") + ").", }); } if (entry.credential !== undefined && entry.type !== "git") { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["credential"], message: "credential is only supported on git sources", }); } if (entry.credential !== undefined && !isApiKeyReference(entry.credential)) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["credential"], message: "git credential must be a $VAR, ${VAR} or ${secrets/<name>} reference", }); } }); export const RegistryConfigEntrySchema = z .object({ url: httpUrl, name: z.string().min(1).optional(), enabled: z.boolean().optional(), provider: z.string().min(1).optional(), options: z.record(z.string(), z.unknown()).optional(), }) .passthrough(); // ── Bundles ───────────────────────────────────────────────────────────────── // // `bundles` + `defaultBundle` are the desired-configuration shape. Each bundle // entry carries ONE source descriptor (`path` | `git` | `website` | `npm` | // `location` — mirroring today's source types), an optional `writable`, an optional // `registryId` locator (the original registry install id, preserved verbatim so // a non-slug-legal id like `github:owner/repo` is not lost when its slug-legal // bundle KEY is derived), an optional `adapter` and, for a `path` or `git` // bundle, an optional `root`. The map KEY is the workspace bundle slug (charset: no // `/`, `:`, `.`, `#`, whitespace), validated with {@link isBundleSlug}. // // `bindings` is not accepted here; the top-level schema rejects it. /** Website source descriptor for a bundle entry. */ const BundleWebsiteDescriptorSchema = z .object({ url: httpUrl, refresh: z.string().min(1).optional(), maxPages: positiveInt.optional(), maxDepth: positiveInt.optional(), // Default true: crawl-scoped robots.txt compliance (Disallow/Crawl-delay // for the akm/akm-cli product tokens or "*"). See // src/sources/snapshot-fetchers/robots.ts. Opt out with `false` to // restore pre-P1 behavior exactly (no /robots.txt request at all). respectRobots: z.boolean().optional(), // Hard wall-clock cap on the whole crawl, in milliseconds. Defaults to // 10 minutes. Unlike a between-page check, this aborts work already in // flight — including a `Retry-After` sleep, which a server can otherwise // make arbitrarily long. Set to 0 to disable the cap entirely for a // deliberately long-running crawl. crawlTimeoutMs: z.number().int().min(0).optional(), }) .passthrough(); export const BundleConfigEntrySchema = z .object({ // Exactly one source descriptor (enforced in superRefine below): path: z.string().min(1).optional(), git: z.string().min(1).optional(), website: BundleWebsiteDescriptorSchema.optional(), npm: z.string().min(1).optional(), /** Where the bundle's adapter finds a bundle that is not a directory (a keyring's name, a vault's name). Needs `adapter`. */ location: z.string().min(1).optional(), /** The adapter that reads the bundle. Absent: detected from its content, then recorded by `akm index`. */ adapter: nonEmptyString.optional(), /** The content root inside a `path` or `git` bundle, relative to it. */ root: z.string().min(1).optional(), /** * A reference to a secret: a `git` bundle's HTTPS bearer credential, resolved only at the Git subprocess boundary, * or the password a location bundle's adapter unlocks its store with, when it takes one (`secret-tool`). */ credential: z.string().min(1).optional(), writable: z.boolean().optional(), // Opt a bundle out of indexing, search, refresh, and write targeting // without deleting it. The runtime honors the derived value in write and // search source selection. enabled: z.boolean().optional(), // The registry install id when the bundle key was slug-derived from it // (e.g. registryId `github:owner/repo` → key `repo`). registryId: z.string().min(1).optional(), // Lint rules to skip, per rule, for the assets whose bundle-relative path matches one of the // globs (`**` is the whole bundle). A rule name lint does not know is warned about and ignored. lint: z.object({ skip: z.record(z.string(), z.array(z.string())).optional() }).optional(), }) .passthrough() .superRefine((entry, ctx) => { const options = entry.options; if (options && typeof options === "object" && "pushOnCommit" in options) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["options", "pushOnCommit"], message: "options.pushOnCommit is not supported", }); } const descriptors = ["path", "git", "website", "npm", "location"].filter((k) => entry[k] !== undefined); if (descriptors.length === 0) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "a bundle entry must carry exactly one source descriptor (path, git, website, npm, or location)", }); } else if (descriptors.length > 1) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `a bundle entry must carry exactly one source descriptor; got ${descriptors.join(", ")}`, }); } if (entry.writable === true && (entry.website !== undefined || entry.npm !== undefined)) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["writable"], message: "writable: true is only supported on path, git and location bundle sources", }); } // A location bundle's adapter refuses a credential it does not take: only it knows. // asset-io: config the config entry, validated if (entry.credential !== undefined && entry.git === undefined && entry.location === undefined) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["credential"], message: "credential is only supported on git and location bundle sources", }); } if (entry.credential !== undefined && !isApiKeyReference(entry.credential)) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["credential"], message: `${entry.git !== undefined ? "git" : "a bundle's"} credential must be a $VAR, \${VAR} or \${secrets/<name>} reference`, }); } // asset-io: config the config entry, validated if (entry.location !== undefined && entry.adapter === undefined) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["adapter"], message: "a location bundle names the adapter that reads it, in `adapter`", }); } if (entry.root !== undefined && entry.path === undefined && entry.git === undefined) { ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["root"], message: "root is only supported on path and git bundle sources", }); } }); /** * `bundles` map. Keys are workspace bundle slugs. * The key charset is validated with {@link isBundleSlug} so a key can never carry * `/`, `:`, `.`, `#`, or whitespace (which would break the `bundle//conceptId` * ref grammar). */ export const BundlesConfigSchema = z.record(z.string().min(1).refine(isBundleSlug, { message: "bundle key must be a legal slug (no '/', ':', '.', '#', or whitespace)", }), BundleConfigEntrySchema);