diff --git a/README.md b/README.md index 40fad41..12c77fa 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,8 @@ Default scope: `vanta-api.all:read vanta-api.all:write` ## Quick start +For guided agent workflows, run `vanta skills list`, then `vanta skills read --name `. + ```bash # List controls vanta controls list --page-size 50 diff --git a/package.json b/package.json index 9354ac1..299cac8 100644 --- a/package.json +++ b/package.json @@ -12,10 +12,10 @@ "type": "module", "scripts": { "generate": "openapi-ts", - "dev": "tsx src/cli.ts", + "dev": "node --import ./scripts/text-loader.mjs --import tsx src/cli.ts", "build": "pnpm run generate && tsup", "typecheck": "tsc --noEmit", - "test": "node --import tsx --test 'src/**/*.test.ts'", + "test": "node --import ./scripts/text-loader.mjs --import tsx --test 'src/**/*.test.ts'", "build:binaries": "tsx scripts/build-binaries.ts", "format": "oxfmt", "format:check": "oxfmt --check" @@ -23,7 +23,8 @@ "dependencies": { "@napi-rs/keyring": "^1.3.0", "@toon-format/toon": "^4.4.0", - "commander": "^14.0.0" + "commander": "^14.0.0", + "yaml": "^2.9.1" }, "devDependencies": { "@hey-api/openapi-ts": "^0.99.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bfb16a1..72bede0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -175,6 +175,9 @@ importers: commander: specifier: ^14.0.0 version: 14.0.3 + yaml: + specifier: ^2.9.1 + version: 2.9.1 devDependencies: '@hey-api/openapi-ts': specifier: ^0.99.0 @@ -190,7 +193,7 @@ importers: version: 0.72.0 tsup: specifier: ^8.5.0 - version: 8.5.1(jiti@2.7.0)(tsx@4.23.12)(typescript@5.9.3) + version: 8.5.1(jiti@2.7.0)(tsx@4.23.12)(typescript@5.9.3)(yaml@2.9.1) tsx: specifier: ^4.20.0 version: 4.23.12 @@ -1377,6 +1380,11 @@ packages: resolution: {integrity: sha512-g/eziiSUNBSsdDJtCLB8bdYEUMj4jR7AGeUo96p/3dTafgjHhpF4RiCFPiRILwjQoDXx5MqkBr4fwWtR3Ky4Wg==} engines: {node: '>=20'} + yaml@2.9.1: + resolution: {integrity: sha512-3NxN8+78OdzbT7C/WjGsyfPAtJaN3FNDsWxv7Y7mcDsT/oOmgW8BpyQQFFBnvZE3j9Y2Sdz1ULFLezL7Eb2yFw==} + engines: {node: '>= 14.6'} + hasBin: true + snapshots: '@esbuild/aix-ppc64@0.27.7': @@ -2118,12 +2126,13 @@ snapshots: exsolve: 1.1.1 pathe: 2.0.3 - postcss-load-config@6.0.1(jiti@2.7.0)(tsx@4.23.12): + postcss-load-config@6.0.1(jiti@2.7.0)(tsx@4.23.12)(yaml@2.9.1): dependencies: lilconfig: 3.1.3 optionalDependencies: jiti: 2.7.0 tsx: 4.23.12 + yaml: 2.9.1 powershell-utils@0.1.0: {} @@ -2215,7 +2224,7 @@ snapshots: ts-interface-checker@0.1.13: {} - tsup@8.5.1(jiti@2.7.0)(tsx@4.23.12)(typescript@5.9.3): + tsup@8.5.1(jiti@2.7.0)(tsx@4.23.12)(typescript@5.9.3)(yaml@2.9.1): dependencies: bundle-require: 5.1.0(esbuild@0.27.7) cac: 6.7.14 @@ -2226,7 +2235,7 @@ snapshots: fix-dts-default-cjs-exports: 1.0.1 joycon: 3.1.1 picocolors: 1.1.1 - postcss-load-config: 6.0.1(jiti@2.7.0)(tsx@4.23.12) + postcss-load-config: 6.0.1(jiti@2.7.0)(tsx@4.23.12)(yaml@2.9.1) resolve-from: 5.0.0 rollup: 4.62.4 source-map: 0.7.6 @@ -2262,3 +2271,5 @@ snapshots: dependencies: is-wsl: 3.1.1 powershell-utils: 0.1.0 + + yaml@2.9.1: {} diff --git a/scripts/build-binaries.ts b/scripts/build-binaries.ts index 9adf26f..de8c53c 100644 --- a/scripts/build-binaries.ts +++ b/scripts/build-binaries.ts @@ -58,6 +58,7 @@ async function main(): Promise { "build", "--compile", `--target=${target.bun}`, + "--loader=.md:text", "--define", `__VANTA_VERSION__=${JSON.stringify(version)}`, "--outfile", diff --git a/scripts/text-loader.mjs b/scripts/text-loader.mjs new file mode 100644 index 0000000..5c68caf --- /dev/null +++ b/scripts/text-loader.mjs @@ -0,0 +1,17 @@ +import { readFileSync } from "node:fs"; +import { register } from "node:module"; +import { isMainThread } from "node:worker_threads"; + +// Match the bundlers' text imports when running source with Node. +if (isMainThread) register(import.meta.url); + +export function load(url, context, nextLoad) { + if (url.endsWith(".md")) { + return { + format: "module", + shortCircuit: true, + source: `export default ${JSON.stringify(readFileSync(new URL(url), "utf8"))}`, + }; + } + return nextLoad(url, context); +} diff --git a/src/cli.ts b/src/cli.ts index 0039f45..4c37c49 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,6 +4,7 @@ import { registerApiCommands } from "./commands/index.js"; import { registerLoginCommand } from "./commands/login.js"; import { registerLogoutCommand } from "./commands/logout.js"; import { registerVersionCommand } from "./commands/version.js"; +import { registerSkillsCommand } from "./commands/skills.js"; import { finishBackgroundUpdateCheck, startBackgroundUpdateCheck, @@ -46,6 +47,10 @@ function buildProgram(): Command { .option( "--scope ", "OAuth scope (default: vanta-api.all:read vanta-api.all:write)", + ) + .addHelpText( + "after", + '\nRun "vanta skills list" to discover skills that can help you use the CLI for certain tasks.', ); const getFlags = (): GlobalFlags => { @@ -74,7 +79,8 @@ function buildProgram(): Command { }; }; - program.hook("preAction", () => { + program.hook("preAction", (_command, actionCommand) => { + if (actionCommand.parent?.name() === "skills") return; startBackgroundUpdateCheck({ agentMode: getFlags().agentMode }); }); program.hook("postAction", async () => { @@ -84,6 +90,7 @@ function buildProgram(): Command { registerLoginCommand(program, getFlags); registerLogoutCommand(program, getFlags); registerVersionCommand(program); + registerSkillsCommand(program); registerApiCommands(program, getFlags); return program; diff --git a/src/commands/skills.test.ts b/src/commands/skills.test.ts new file mode 100644 index 0000000..adf8406 --- /dev/null +++ b/src/commands/skills.test.ts @@ -0,0 +1,92 @@ +import assert from "node:assert/strict"; +import { readFile, readdir } from "node:fs/promises"; +import { describe, it } from "node:test"; +import { Command } from "commander"; +import { registerSkillsCommand } from "./skills.js"; + +async function runSkills(args: string[]): Promise { + const program = new Command() + .exitOverride() + .configureOutput({ writeErr: () => {} }); + registerSkillsCommand(program); + const output: string[] = []; + const log = console.log; + console.log = (text: string) => output.push(text); + try { + await program.parseAsync(["node", "vanta", "skills", ...args]); + } finally { + console.log = log; + } + return output.join("\n"); +} + +describe("bundled skills", () => { + const root = new URL("../../skills/", import.meta.url); + + it("lists every skill without loading its instructions", async () => { + const output = await runSkills(["list"]); + for (const entry of await readdir(root, { withFileTypes: true })) { + if (entry.isDirectory()) assert.ok(output.includes(`${entry.name}\n`)); + } + assert.ok(!output.includes("# Test Remediation Digest")); + }); + + it("returns complete source instructions and references without credentials or network access", async () => { + const fetch = globalThis.fetch; + globalThis.fetch = () => { + throw new Error("Skills must not access the network"); + }; + try { + for (const entry of await readdir(root, { withFileTypes: true })) { + if (!entry.isDirectory()) continue; + const output = await runSkills(["read", "--name", entry.name]); + const content = await readFile( + new URL(`${entry.name}/SKILL.md`, root), + "utf8", + ); + assert.ok( + output.includes(content.trimEnd()), + `${entry.name} is complete`, + ); + const references = new URL(`${entry.name}/references/`, root); + const files = await readdir(references).catch( + (error: NodeJS.ErrnoException) => { + if (error.code === "ENOENT") return []; + throw error; + }, + ); + for (const file of files) { + assert.ok(output.includes(`references/${file}`)); + assert.ok( + output.includes( + (await readFile(new URL(file, references), "utf8")).trimEnd(), + ), + `${entry.name}/${file} is complete`, + ); + } + } + } finally { + globalThis.fetch = fetch; + } + }); + + it("requires a skill name flag", async () => { + await assert.rejects( + runSkills(["read"]), + /required option '--name '/, + ); + await assert.rejects( + runSkills(["read", "vanta"]), + /required option '--name '/, + ); + }); + + it("rejects unknown names and file paths with discovery guidance", async () => { + for (const name of ["missing", "../../README.md", "__proto__"]) { + await assert.rejects( + runSkills(["read", "--name", name]), + /Unknown skill .*vanta skills list/, + ); + } + }); +}); diff --git a/src/commands/skills.ts b/src/commands/skills.ts new file mode 100644 index 0000000..95f5c24 --- /dev/null +++ b/src/commands/skills.ts @@ -0,0 +1,48 @@ +import type { Command } from "commander"; +import { bundledSkills } from "../skills.js"; + +const guidance = `Load a skill's instructions to learn how to use it: + vanta skills read --name +You can load multiple skills; each call returns one skill's instructions and +references. Follow the loaded instructions when carrying out the task. +To describe a skill, use its catalog description instead of loading it.`; + +export function registerSkillsCommand(program: Command): void { + const skills = program + .command("skills") + .description("Load skill instructions for a task") + .addHelpText("after", `\n${guidance}`); + + skills + .command("list") + .description("List available skills and when to use them") + .action(() => { + console.log(`${guidance}\n\nAvailable skills:\n`); + for (const skill of bundledSkills) { + console.log(`${skill.name}\n ${skill.description}\n`); + } + }); + + skills + .command("read") + .description("Read a skill's full instructions and bundled references") + .requiredOption("--name ", "Skill name from vanta skills list") + .addHelpText( + "after", + "\nExample:\n vanta skills read --name test-remediation-digest\n\nPrints Markdown instructions; does not execute a workflow or call the API.", + ) + .action((options: { name: string }) => { + const skill = bundledSkills.find((entry) => entry.name === options.name); + if (!skill) { + throw new Error( + `Unknown skill ${JSON.stringify(options.name)}. Run "vanta skills list" to see available names.`, + ); + } + console.log(skill.content.trimEnd()); + for (const reference of skill.references) { + console.log( + `\n# Bundled reference: ${reference.path}\n\n${reference.content.trimEnd()}`, + ); + } + }); +} diff --git a/src/skills.ts b/src/skills.ts new file mode 100644 index 0000000..d0dd267 --- /dev/null +++ b/src/skills.ts @@ -0,0 +1,57 @@ +import { parse } from "yaml"; +import computerSecurity from "../skills/computer-security-blocker-explainer/SKILL.md"; +import controlTesting from "../skills/control-testing-procedures/SKILL.md"; +import personnel from "../skills/personnel-report/SKILL.md"; +import mitigatingControls from "../skills/suggest-mitigating-controls/SKILL.md"; +import riskScenarios from "../skills/suggest-risk-scenarios/SKILL.md"; +import taxonomy from "../skills/suggest-risk-scenarios/references/scenario-taxonomy.md"; +import remediation from "../skills/test-remediation-digest/SKILL.md"; +import fixStyle from "../skills/test-remediation-digest/references/how-to-fix-style.md"; +import unassignedControls from "../skills/unassigned-controls-report/SKILL.md"; +import vanta from "../skills/vanta/SKILL.md"; +import vulnerabilityTriage from "../skills/weekly-vulnerability-triage/SKILL.md"; + +type SkillSource = { + content: string; + references?: { path: string; content: string }[]; +}; + +export const bundledSkills = ( + [ + { content: computerSecurity }, + { content: controlTesting }, + { content: personnel }, + { content: mitigatingControls }, + { + content: riskScenarios, + references: [ + { path: "references/scenario-taxonomy.md", content: taxonomy }, + ], + }, + { + content: remediation, + references: [ + { path: "references/how-to-fix-style.md", content: fixStyle }, + ], + }, + { content: unassignedControls }, + { content: vanta }, + { content: vulnerabilityTriage }, + ] satisfies SkillSource[] +).map(({ content, references = [] }) => { + const frontmatter = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/.exec(content); + if (!frontmatter) throw new Error("Missing skill frontmatter"); + const metadata = parse(frontmatter[1]); + if ( + typeof metadata?.name !== "string" || + typeof metadata?.description !== "string" + ) { + throw new Error("Invalid skill name or description"); + } + return { + name: metadata.name, + description: metadata.description.trim(), + content, + references, + }; +}); diff --git a/src/text.d.ts b/src/text.d.ts new file mode 100644 index 0000000..c94d67b --- /dev/null +++ b/src/text.d.ts @@ -0,0 +1,4 @@ +declare module "*.md" { + const content: string; + export default content; +} diff --git a/tsup.config.ts b/tsup.config.ts index 605508d..9618a44 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -7,6 +7,7 @@ export default defineConfig({ outDir: "dist", clean: true, sourcemap: true, + loader: { ".md": "text" }, banner: { js: "#!/usr/bin/env node", },