|
| 1 | +# 144 - Dep License Auditor |
| 2 | + |
| 3 | +```rig |
| 4 | +import { agent, p, s, defineTool } from "rig"; |
| 5 | +
|
| 6 | +const extractLicense = defineTool("extractLicense", { |
| 7 | + description: "Read license field from a package in node_modules.", |
| 8 | + parameters: s.object({ packageName: s.string }), |
| 9 | + async handler({ packageName }) { |
| 10 | + const { readFileSync } = await import("node:fs"); |
| 11 | + try { |
| 12 | + const pkgPath = `node_modules/${packageName}/package.json`; |
| 13 | + const pkg = JSON.parse(readFileSync(pkgPath, "utf8")); |
| 14 | + const license: string = pkg.license ?? pkg.licenses?.[0]?.type ?? "UNKNOWN"; |
| 15 | + let category: "permissive" | "copyleft" | "unknown" = "unknown"; |
| 16 | + const upper = license.toUpperCase(); |
| 17 | + if (["MIT", "ISC", "BSD", "APACHE", "0BSD", "WTFPL"].some(l => upper.includes(l))) { |
| 18 | + category = "permissive"; |
| 19 | + } else if (["GPL", "LGPL", "AGPL", "MPL", "EUPL"].some(l => upper.includes(l))) { |
| 20 | + category = "copyleft"; |
| 21 | + } |
| 22 | + return { license, category }; |
| 23 | + } catch { |
| 24 | + return { license: "UNKNOWN", category: "unknown" as const }; |
| 25 | + } |
| 26 | + }, |
| 27 | +}); |
| 28 | +
|
| 29 | +// Agent role: Audit dependency licenses and flag copyleft packages. |
| 30 | +const depLicenseAuditor = agent({ |
| 31 | + model: "small", |
| 32 | + instructions: p`Audit dependency licenses for this project. |
| 33 | +
|
| 34 | +List installed packages: |
| 35 | +${p.bash("npm ls --json --depth=0 2>/dev/null | node -e \"const d=JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));console.log(Object.keys(d.dependencies||{}).join('\\n'))\" 2>/dev/null || ls node_modules | head -50")} |
| 36 | +
|
| 37 | +For each package, use the extractLicense tool to get its license and category. |
| 38 | +Return packages array, hasCopyleft flag, and totalPackages count.`, |
| 39 | + tools: [extractLicense], |
| 40 | + output: s.object({ |
| 41 | + packages: s.array( |
| 42 | + s.object({ |
| 43 | + name: s.string, |
| 44 | + license: s.string, |
| 45 | + category: s.enum("permissive", "copyleft", "unknown"), |
| 46 | + }), |
| 47 | + ), |
| 48 | + hasCopyleft: s.boolean, |
| 49 | + totalPackages: s.int, |
| 50 | + }), |
| 51 | +}); |
| 52 | +
|
| 53 | +export default depLicenseAuditor; |
| 54 | +``` |
0 commit comments