@vltpkg/graph 1.3.8 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +59 -3
  2. package/dist/actual/load.js +28 -12
  3. package/dist/build.js +30 -5
  4. package/dist/dependencies.d.ts +11 -0
  5. package/dist/dependencies.js +47 -7
  6. package/dist/filter-nodes-by-query.d.ts +39 -0
  7. package/dist/filter-nodes-by-query.js +100 -0
  8. package/dist/ideal/append-nodes.js +19 -6
  9. package/dist/ideal/build-ideal-from-starting-graph.js +8 -0
  10. package/dist/ideal/get-importer-specs.d.ts +1 -0
  11. package/dist/ideal/get-importer-specs.js +27 -7
  12. package/dist/ideal/refresh-ideal-graph.js +6 -1
  13. package/dist/index.d.ts +3 -0
  14. package/dist/index.js +2 -0
  15. package/dist/install.d.ts +23 -8
  16. package/dist/install.js +46 -1
  17. package/dist/lockfile/load-edges.js +10 -4
  18. package/dist/modifiers.d.ts +7 -0
  19. package/dist/modifiers.js +24 -0
  20. package/dist/reify/build.d.ts +7 -0
  21. package/dist/reify/check-needed-build.d.ts +5 -0
  22. package/dist/reify/check-needed-build.js +13 -0
  23. package/dist/reify/index.js +9 -39
  24. package/dist/reify/update-importers-package-json.js +5 -9
  25. package/dist/skills/dirs.d.ts +58 -0
  26. package/dist/skills/dirs.js +211 -0
  27. package/dist/skills/discover.d.ts +73 -0
  28. package/dist/skills/discover.js +234 -0
  29. package/dist/skills/gitignore.d.ts +13 -0
  30. package/dist/skills/gitignore.js +53 -0
  31. package/dist/skills/index.d.ts +123 -0
  32. package/dist/skills/index.js +225 -0
  33. package/dist/skills/lazy.d.ts +8 -0
  34. package/dist/skills/lazy.js +8 -0
  35. package/dist/skills/mask.d.ts +26 -0
  36. package/dist/skills/mask.js +43 -0
  37. package/dist/skills/mounts.d.ts +64 -0
  38. package/dist/skills/mounts.js +136 -0
  39. package/dist/uninstall.d.ts +24 -7
  40. package/dist/uninstall.js +26 -1
  41. package/dist/update.d.ts +24 -4
  42. package/dist/update.js +24 -1
  43. package/dist/visualization/mermaid-output.d.ts +8 -0
  44. package/dist/visualization/mermaid-output.js +25 -8
  45. package/package.json +21 -21
@@ -0,0 +1,73 @@
1
+ import type { DepID } from '@vltpkg/dep-id';
2
+ import type { PathScurry } from 'path-scurry';
3
+ import type { Node } from '../node.ts';
4
+ import type { SkillMount } from './mounts.ts';
5
+ /**
6
+ * An agent skill (a dir with a `SKILL.md`) shipped by an installed
7
+ * package. See https://agentskills.io/specification
8
+ */
9
+ export type Skill = {
10
+ /** package name, e.g. `@s/bar` */
11
+ name: string;
12
+ /**
13
+ * name a direct dep is installed under, when not `name`, as `vlt ls`
14
+ * shows it
15
+ */
16
+ alias?: string;
17
+ /** package version */
18
+ version?: string;
19
+ /** package DepID */
20
+ id: DepID;
21
+ /**
22
+ * skill name: its dir name (`skills/<dir>`, `dist/skills/<dir>`,
23
+ * `.agents/skills/<dir>`), or the root SKILL.md `name`
24
+ */
25
+ skill: string;
26
+ /**
27
+ * `SKILL.md` frontmatter `name`, when it differs from `skill`
28
+ * (matched by `:skill()`)
29
+ */
30
+ frontmatterName?: string;
31
+ /** frontmatter description (folded to one line, control chars stripped) */
32
+ description?: string;
33
+ /** realpath of the dir holding SKILL.md */
34
+ path: string;
35
+ /** dirs holding a vlt link named `skill` to `path` */
36
+ linked: string[];
37
+ };
38
+ /**
39
+ * Result of `discoverSkills`.
40
+ */
41
+ export type DiscoveredSkills = {
42
+ /** skills of the picked version of each package, sorted by name */
43
+ skills: Skill[];
44
+ /** every scanned package version, w/ the skills it ships */
45
+ scanned: Map<DepID, Pick<Skill, 'skill' | 'path'>[]>;
46
+ };
47
+ /**
48
+ * The `SKILL.md` frontmatter fields vlt reads.
49
+ */
50
+ export type SkillFrontmatter = {
51
+ name?: string;
52
+ description?: string;
53
+ };
54
+ /**
55
+ * Read `name` and `description` from a `SKILL.md` YAML frontmatter.
56
+ * Minimal line reader: top-level keys, plain/quoted/folded values.
57
+ */
58
+ export declare const readFrontmatter: (file: string) => SkillFrontmatter;
59
+ /** Ships agent skills, as `discoverSkills` finds them. */
60
+ export declare const shipsSkills: (node: Node) => boolean;
61
+ /**
62
+ * Find the agent skills shipped by the given installed packages.
63
+ *
64
+ * Only packages in the vlt store are scanned, for a root `SKILL.md`,
65
+ * then `<dir>/SKILL.md` in `skills/`, `dist/skills/` and
66
+ * `.agents/skills/`: the first skill of a name wins. One version per
67
+ * package is picked, even if it ships none: a dep of the main
68
+ * importer, else of any importer, else the highest version, then
69
+ * DepID. Same-name skills of different packages are all returned,
70
+ * sorted by rank then DepID (`linkSkills` resolves them). `mounts`
71
+ * set each skill's `linked` dirs.
72
+ */
73
+ export declare const discoverSkills: (nodes: Iterable<Node>, scurry: PathScurry, mounts?: SkillMount[]) => DiscoveredSkills;
@@ -0,0 +1,234 @@
1
+ import { readdirSync, readFileSync } from 'node:fs';
2
+ import { resolve } from 'node:path';
3
+ import { splitDepID } from '@vltpkg/dep-id';
4
+ import { parse } from '@vltpkg/semver';
5
+ import { lstat, norm, packageKey, realOr } from "./mounts.js";
6
+ // safe single path segment, no dotfiles or scopes
7
+ const skillDirName = /^[a-z0-9][a-z0-9._-]*$/i;
8
+ // agentskills.io `name`
9
+ const specName = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
10
+ const topKey = /^([A-Za-z0-9_-]+):/;
11
+ const blockIndicator = /^[>|][-+1-9]*$/;
12
+ const fold = (inline, more) => {
13
+ let value = [blockIndicator.test(inline) ? '' : inline, ...more]
14
+ .filter(Boolean)
15
+ .join(' ');
16
+ const q = value[0];
17
+ if (value.length > 1 &&
18
+ (q === '"' || q === "'") &&
19
+ value.endsWith(q)) {
20
+ value = value.slice(1, -1).trim();
21
+ }
22
+ return value || undefined;
23
+ };
24
+ /**
25
+ * Read `name` and `description` from a `SKILL.md` YAML frontmatter.
26
+ * Minimal line reader: top-level keys, plain/quoted/folded values.
27
+ */
28
+ export const readFrontmatter = (file) => {
29
+ let text;
30
+ try {
31
+ text = readFileSync(file, 'utf8');
32
+ }
33
+ catch {
34
+ return {};
35
+ }
36
+ const lines = text.split(/\r?\n/);
37
+ if (lines[0]?.trim() !== '---')
38
+ return {};
39
+ const end = lines.findIndex((l, i) => i > 0 && l.trim() === '---');
40
+ if (end === -1)
41
+ return {};
42
+ const fm = {};
43
+ let key;
44
+ let inline = '';
45
+ let more = [];
46
+ const flush = () => {
47
+ if (key)
48
+ fm[key] = fold(inline, more);
49
+ key = undefined;
50
+ };
51
+ for (const line of lines.slice(1, end)) {
52
+ if (!line.trim())
53
+ continue;
54
+ const m = topKey.exec(line);
55
+ if (m) {
56
+ flush();
57
+ const k = m[1];
58
+ if (k === 'name' || k === 'description') {
59
+ key = k;
60
+ inline = line.slice(k.length + 1).trim();
61
+ // drop a ` # comment` from unquoted values
62
+ const hash = inline.search(/(?:^|\s)#/);
63
+ if (hash !== -1 && !/^["']/.test(inline)) {
64
+ inline = inline.slice(0, hash).trim();
65
+ }
66
+ more = [];
67
+ }
68
+ }
69
+ else if (/^\s/.test(line)) {
70
+ if (key)
71
+ more.push(line.trim());
72
+ }
73
+ else {
74
+ flush();
75
+ }
76
+ }
77
+ flush();
78
+ return fm;
79
+ };
80
+ // package-controlled text printed to terminals
81
+ const sanitize = (s) => {
82
+ const clean = s
83
+ ?.replace(/\p{Cc}/gu, ' ')
84
+ .replace(/\s+/g, ' ')
85
+ .trim();
86
+ return clean ? clean.slice(0, 1024) : undefined;
87
+ };
88
+ const lc = (s) => s.toLowerCase();
89
+ const isDir = (path) => !!lstat(path)?.isDirectory();
90
+ const isFile = (path) => !!lstat(path)?.isFile();
91
+ // `skills/SKILL.md` would make the dir look like a skill
92
+ const isSkillMd = (name) => name.toLowerCase() === 'skill.md';
93
+ // where packages ship skills, in precedence order (a name found first
94
+ // wins), after a root SKILL.md
95
+ const shipDirs = ['skills', 'dist/skills', '.agents/skills'];
96
+ // a real dir, no symlink on the way: skills stay inside the package
97
+ const subdir = (dir, sub) => {
98
+ let path = dir;
99
+ for (const seg of sub.split('/')) {
100
+ path = resolve(path, seg);
101
+ if (!isDir(path))
102
+ return undefined;
103
+ }
104
+ return path;
105
+ };
106
+ const scan = (dir, pkgName) => {
107
+ const found = [];
108
+ const has = (skill) => found.some(f => lc(f.skill) === lc(skill));
109
+ const root = resolve(dir, 'SKILL.md');
110
+ if (isFile(root)) {
111
+ const { name, description } = readFrontmatter(root);
112
+ const skill = name && name.length <= 64 && specName.test(name) ?
113
+ name
114
+ : pkgName.slice(pkgName.lastIndexOf('/') + 1);
115
+ if (skillDirName.test(skill) && !isSkillMd(skill)) {
116
+ found.push({ skill, path: dir, name, description });
117
+ }
118
+ }
119
+ for (const sub of shipDirs) {
120
+ const skillsDir = subdir(dir, sub);
121
+ if (!skillsDir)
122
+ continue;
123
+ // one per name, any case
124
+ for (const d of readdirSync(skillsDir).sort()) {
125
+ if (!skillDirName.test(d) || isSkillMd(d) || has(d))
126
+ continue;
127
+ const path = resolve(skillsDir, d);
128
+ const file = resolve(path, 'SKILL.md');
129
+ if (!isDir(path) || !isFile(file))
130
+ continue;
131
+ const { name, description } = readFrontmatter(file);
132
+ found.push({ skill: d, path, name, description });
133
+ }
134
+ }
135
+ return found;
136
+ };
137
+ // 0: dep of the main importer, 1: of any importer, 2: transitive
138
+ const rank = (node) => {
139
+ let r = 2;
140
+ for (const { from } of node.edgesIn) {
141
+ if (from.mainImporter)
142
+ return 0;
143
+ if (from.importer)
144
+ r = 1;
145
+ }
146
+ return r;
147
+ };
148
+ const ver = (node) => node.version ? parse(node.version) : undefined;
149
+ // higher version first, if both valid
150
+ const byVersion = (a, b) => {
151
+ const va = ver(a);
152
+ const vb = ver(b);
153
+ return va && vb ? vb.compare(va) : 0;
154
+ };
155
+ const compare = (a, b) => rank(a) - rank(b) ||
156
+ byVersion(a, b) ||
157
+ a.id.localeCompare(b.id, 'en');
158
+ // the name a direct dep is installed under, if not its own
159
+ const aliasOf = (node) => {
160
+ const edges = [...node.edgesIn];
161
+ const n = (edges.find(e => e.from.mainImporter) ??
162
+ edges.find(e => e.from.importer))?.name;
163
+ return n === node.name ? undefined : n;
164
+ };
165
+ // store pkgs only: lockfile keeps a tarball's path as its location
166
+ const scannable = (node) => !node.importer &&
167
+ node.inVltStore() &&
168
+ splitDepID(node.id)[0] !== 'file';
169
+ /** Ships agent skills, as `discoverSkills` finds them. */
170
+ export const shipsSkills = (node) => scannable(node) &&
171
+ scan(realOr(resolve(node.projectRoot, node.location)), node.name)
172
+ .length > 0;
173
+ /**
174
+ * Find the agent skills shipped by the given installed packages.
175
+ *
176
+ * Only packages in the vlt store are scanned, for a root `SKILL.md`,
177
+ * then `<dir>/SKILL.md` in `skills/`, `dist/skills/` and
178
+ * `.agents/skills/`: the first skill of a name wins. One version per
179
+ * package is picked, even if it ships none: a dep of the main
180
+ * importer, else of any importer, else the highest version, then
181
+ * DepID. Same-name skills of different packages are all returned,
182
+ * sorted by rank then DepID (`linkSkills` resolves them). `mounts`
183
+ * set each skill's `linked` dirs.
184
+ */
185
+ export const discoverSkills = (nodes, scurry, mounts = []) => {
186
+ const key = (name, path) => `${lc(name)}\0${norm(path)}`;
187
+ const linkedIn = new Map();
188
+ for (const m of mounts) {
189
+ const k = key(m.name, realOr(m.target));
190
+ const dirs = linkedIn.get(k) ?? [];
191
+ if (!dirs.includes(m.dir.dir))
192
+ dirs.push(m.dir.dir);
193
+ linkedIn.set(k, dirs);
194
+ }
195
+ const scanned = new Map();
196
+ const byPkg = new Map();
197
+ for (const node of nodes) {
198
+ if (!scannable(node))
199
+ continue;
200
+ const found = scan(realOr(node.resolvedLocation(scurry)), node.name);
201
+ scanned.set(node.id, found.map(({ skill, path }) => ({ skill, path })));
202
+ const key = packageKey(node.id);
203
+ const list = byPkg.get(key) ?? [];
204
+ list.push({ node, found });
205
+ byPkg.set(key, list);
206
+ }
207
+ const rows = [];
208
+ for (const list of byPkg.values()) {
209
+ const { node, found } = list.reduce((a, b) => compare(a.node, b.node) <= 0 ? a : b);
210
+ for (const { skill, path, name, description } of found) {
211
+ const fm = sanitize(name);
212
+ rows.push([
213
+ {
214
+ name: node.name,
215
+ alias: aliasOf(node),
216
+ version: node.version,
217
+ id: node.id,
218
+ skill,
219
+ ...(fm && lc(fm) !== lc(skill) && { frontmatterName: fm }),
220
+ description: sanitize(description),
221
+ path,
222
+ linked: linkedIn.get(key(skill, path)) ?? [],
223
+ },
224
+ rank(node),
225
+ ]);
226
+ }
227
+ }
228
+ const skills = rows
229
+ .sort(([a, ra], [b, rb]) => lc(a.skill).localeCompare(lc(b.skill), 'en') ||
230
+ ra - rb ||
231
+ a.id.localeCompare(b.id, 'en'))
232
+ .map(([s]) => s);
233
+ return { skills, scanned };
234
+ };
@@ -0,0 +1,13 @@
1
+ export declare const GITIGNORE_BEGIN = "# >>> vlt agent skills (links into node_modules)";
2
+ export declare const GITIGNORE_END = "# <<< vlt agent skills";
3
+ /**
4
+ * Keep a `/<name>` line per vlt link in a marked block of
5
+ * `<dir>/.gitignore`: add `add`, drop `remove`. Other lines are kept.
6
+ * Written only when changed, created only to add a line, never
7
+ * removed. `'refused'`: `.gitignore` is not a regular file, or an fs
8
+ * error.
9
+ */
10
+ export declare const editGitignore: (dir: string, edit: {
11
+ add: string[];
12
+ remove: string[];
13
+ }) => "ok" | "refused";
@@ -0,0 +1,53 @@
1
+ import { lstatSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ export const GITIGNORE_BEGIN = '# >>> vlt agent skills (links into node_modules)';
4
+ export const GITIGNORE_END = '# <<< vlt agent skills';
5
+ const sorted = (names) => [...names]
6
+ .sort((a, b) => a.localeCompare(b, 'en'))
7
+ .map(n => `/${n}`);
8
+ const update = (file, { add, remove }) => {
9
+ const st = lstatSync(file, { throwIfNoEntry: false });
10
+ if (st && !st.isFile())
11
+ return 'refused';
12
+ const text = st ? readFileSync(file, 'utf8') : '';
13
+ const eol = text.includes('\r\n') ? '\r\n' : '\n';
14
+ const lines = text.split(eol);
15
+ const begin = lines.findIndex(l => l.trim() === GITIGNORE_BEGIN);
16
+ const end = lines.findIndex((l, i) => begin !== -1 && i > begin && l.trim() === GITIGNORE_END);
17
+ let out;
18
+ if (end === -1) {
19
+ if (!add.length)
20
+ return 'ok';
21
+ out =
22
+ (text && !text.endsWith(eol) ? text + eol : text) +
23
+ [GITIGNORE_BEGIN, ...sorted(add), GITIGNORE_END].join(eol) +
24
+ eol;
25
+ }
26
+ else {
27
+ const rm = new Set(sorted(remove));
28
+ const block = lines
29
+ .slice(begin + 1, end)
30
+ .filter(l => !rm.has(l.trim()));
31
+ const have = new Set(block.map(l => l.trim()));
32
+ lines.splice(begin + 1, end - begin - 1, ...block, ...sorted(add).filter(l => !have.has(l)));
33
+ out = lines.join(eol);
34
+ }
35
+ if (out !== text)
36
+ writeFileSync(file, out);
37
+ return 'ok';
38
+ };
39
+ /**
40
+ * Keep a `/<name>` line per vlt link in a marked block of
41
+ * `<dir>/.gitignore`: add `add`, drop `remove`. Other lines are kept.
42
+ * Written only when changed, created only to add a line, never
43
+ * removed. `'refused'`: `.gitignore` is not a regular file, or an fs
44
+ * error.
45
+ */
46
+ export const editGitignore = (dir, edit) => {
47
+ try {
48
+ return update(join(dir, '.gitignore'), edit);
49
+ }
50
+ catch {
51
+ return 'refused';
52
+ }
53
+ };
@@ -0,0 +1,123 @@
1
+ import type { LoadOptions } from '../actual/load.ts';
2
+ import type { Graph } from '../graph.ts';
3
+ import type { Skill } from './discover.ts';
4
+ import type { UnmatchedPattern } from './mask.ts';
5
+ export * from './discover.ts';
6
+ export type { SkillMount, MountResult } from './mounts.ts';
7
+ export type { SkillsDir } from './dirs.ts';
8
+ export type { UnmatchedPattern } from './mask.ts';
9
+ /**
10
+ * Options for {@link listSkills} and {@link linkSkills}.
11
+ */
12
+ export type SkillsOptions = LoadOptions & {
13
+ /**
14
+ * DSS query selecting packages, selectors may end with `:skill()`;
15
+ * default `*`
16
+ */
17
+ target?: string;
18
+ /** graph to use instead of loading the actual one */
19
+ graph?: Graph;
20
+ /**
21
+ * agent skill dirs, relative to projectRoot. Unset: detect; `[]`:
22
+ * none
23
+ */
24
+ skillsDirs?: string[];
25
+ /** env read by detection; default `process.env` */
26
+ env?: Record<string, string | undefined>;
27
+ };
28
+ /**
29
+ * Options for {@link linkSkills}.
30
+ */
31
+ export type LinkSkillsOptions = SkillsOptions & {
32
+ /** DSS query selecting packages */
33
+ target: string;
34
+ /**
35
+ * also remove every vlt link this selection does not make, in every
36
+ * scanned dir (`vlt skills link`). Default: only add links and remove
37
+ * links whose skill is gone (installs)
38
+ */
39
+ apply?: boolean;
40
+ };
41
+ /**
42
+ * A skill linked in one dir.
43
+ */
44
+ export type SkillRow = Skill & {
45
+ /** the dir */
46
+ dir: string;
47
+ /** `<dir>/<skill>` */
48
+ mount: string;
49
+ };
50
+ /**
51
+ * Skills left out as their packages have no security data (fail
52
+ * closed).
53
+ */
54
+ export type UnscannedSkills = {
55
+ /** `<skill> (<package>)` */
56
+ unscanned?: string[];
57
+ /** why: the request timed out, or socket.dev has not scanned them */
58
+ unscannedReason?: 'timeout' | 'unscanned';
59
+ };
60
+ /**
61
+ * Result of {@link listSkills}.
62
+ */
63
+ export type ListSkillsResult = UnscannedSkills & {
64
+ /** main importer name, the tree root */
65
+ project: string;
66
+ /** the graph the skills were found in */
67
+ graph: Graph;
68
+ skills: Skill[];
69
+ /** target dirs */
70
+ dirs: string[];
71
+ /** targets dropped: `<dir> (<reason>)` */
72
+ skipped?: string[];
73
+ };
74
+ /**
75
+ * Result of {@link linkSkills}.
76
+ */
77
+ export type LinkSkillsResult = UnscannedSkills & {
78
+ /** links created or re-pointed */
79
+ linked: SkillRow[];
80
+ /** selected skills already linked */
81
+ unchanged: SkillRow[];
82
+ /**
83
+ * vlt links removed (skill gone, or with `apply`: not selected or dir
84
+ * no longer a target): `/`-separated relative paths
85
+ */
86
+ removed: string[];
87
+ /**
88
+ * skipped per dir: path in use (`<mount>`), or name taken by another
89
+ * package (`<mount> (<package>)`)
90
+ */
91
+ conflicts: string[];
92
+ /** target dirs */
93
+ dirs: string[];
94
+ /** targets dropped: `<dir> (<reason>)` */
95
+ skipped?: string[];
96
+ /** `<dir>/.gitignore` files not updated: not a regular file, fs error */
97
+ gitignore?: string[];
98
+ /** `:skill()` patterns that matched no skill of their packages */
99
+ unmatched?: UnmatchedPattern[];
100
+ };
101
+ /**
102
+ * List the agent skills shipped by installed packages (`:skill()`
103
+ * applied, `unscanned` left out), and the agent skill dirs. Creates
104
+ * nothing.
105
+ */
106
+ export declare const listSkills: (o: SkillsOptions) => Promise<ListSkillsResult>;
107
+ /**
108
+ * Link the agent skills of the selected packages into each of the
109
+ * project's agent skill dirs (see `skillsDirs`), created only when a
110
+ * skill is picked. `target` selectors may end with `:skill()`. One
111
+ * package per name, among picked skills, in every dir: the package of
112
+ * the first existing link (in dir order), else rank (main importer
113
+ * dep, importer dep, transitive), then DepID; the others are
114
+ * reported. Installs keep a dir's link to another installed skill.
115
+ * A link follows its package to the picked version, in every scanned
116
+ * dir. Then remove vlt links whose skill is no longer installed (with
117
+ * `apply`: every vlt link not made here), and keep each dir's
118
+ * `.gitignore` block in sync. Linked packages get private copies of
119
+ * files hardlinked from the global store. Security queries fail
120
+ * closed: skills of packages with no security data are not linked,
121
+ * and reported in `unscanned` unless still linked.
122
+ */
123
+ export declare const linkSkills: (o: LinkSkillsOptions) => Promise<LinkSkillsResult>;