ai-agentmanager 1.51.1 → 1.54.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.
- package/CHANGELOG.md +15 -0
- package/README.md +56 -2
- package/package.json +1 -1
- package/public/app.js +41 -8
- package/public/board.js +177 -0
- package/public/boardcols.js +54 -0
- package/public/eval.js +2 -2
- package/public/index.html +9 -0
- package/public/keys.js +3 -2
- package/public/side.js +20 -3
- package/public/skills.js +215 -0
- package/public/style.css +60 -1
- package/public/system.js +31 -4
- package/public/usage.js +6 -0
- package/src/brief.js +74 -1
- package/src/cli.js +96 -2
- package/src/config.js +2 -0
- package/src/engines/claude.js +15 -1
- package/src/eval.js +45 -8
- package/src/lanes.js +156 -0
- package/src/lean.js +151 -0
- package/src/manager.js +71 -5
- package/src/races.js +21 -12
- package/src/server.js +58 -5
- package/src/skills.js +339 -0
- package/src/system.js +18 -9
package/src/skills.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
// Skills: the Agent Skills Claude Code has on this computer (yours in ~/.claude/skills, and each
|
|
2
|
+
// project's in <project>/.claude/skills), and adding more from a git repository. A skill is a
|
|
3
|
+
// folder with a SKILL.md (a name, a description of when to use it, the instructions) and whatever
|
|
4
|
+
// scripts and files it needs. Adding one is a look first (what the repository offers, each skill's
|
|
5
|
+
// files, which of them are scripts) and then a copy; where it came from and the commit are kept, so
|
|
6
|
+
// it can be checked for a newer version and updated, and a skill edited here since says so.
|
|
7
|
+
// Which agents used which skill is read from their sessions. No model is called.
|
|
8
|
+
|
|
9
|
+
import { execFile } from 'node:child_process';
|
|
10
|
+
import crypto from 'node:crypto';
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import os from 'node:os';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { paths } from './config.js';
|
|
15
|
+
|
|
16
|
+
const err = (status, message) => Object.assign(new Error(message), { status });
|
|
17
|
+
const SKIP = new Set(['.git', 'node_modules', '.venv', 'venv', '__pycache__', 'dist', 'build', 'target']);
|
|
18
|
+
const SCRIPT = /\.(sh|bash|zsh|py|js|mjs|cjs|ts|rb|pl|php|ps1|bat|cmd)$/i;
|
|
19
|
+
const MAX_FILES = 400;
|
|
20
|
+
const MAX_BYTES = 20 * 1024 * 1024;
|
|
21
|
+
const NAME = /^[a-z0-9][a-z0-9._-]{0,63}$/i;
|
|
22
|
+
|
|
23
|
+
export const personalSkillsDir = () => path.join(process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude'), 'skills');
|
|
24
|
+
export const projectSkillsDir = (dir) => path.join(dir, '.claude', 'skills');
|
|
25
|
+
|
|
26
|
+
/** SKILL.md's front matter: { name, description } (a folded or literal block as one line), and the body. */
|
|
27
|
+
export function parseSkill(text) {
|
|
28
|
+
const m = String(text ?? '').match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);
|
|
29
|
+
const out = { name: null, description: null, body: m ? m[2] : String(text ?? '') };
|
|
30
|
+
if (!m) return out;
|
|
31
|
+
const lines = m[1].split(/\r?\n/);
|
|
32
|
+
for (let i = 0; i < lines.length; i++) {
|
|
33
|
+
const kv = lines[i].match(/^([A-Za-z][\w-]*):\s*(.*)$/);
|
|
34
|
+
if (!kv) continue;
|
|
35
|
+
let value = kv[2].trim();
|
|
36
|
+
if (/^[>|][+-]?$/.test(value) || value === '') {
|
|
37
|
+
const block = [];
|
|
38
|
+
while (i + 1 < lines.length && (/^\s+\S/.test(lines[i + 1]) || lines[i + 1].trim() === '')) block.push(lines[++i].trim());
|
|
39
|
+
value = block.join(' ').replace(/\s+/g, ' ').trim();
|
|
40
|
+
} else {
|
|
41
|
+
value = value.replace(/^(["'])(.*)\1$/, '$2');
|
|
42
|
+
}
|
|
43
|
+
if (kv[1] === 'name') out.name = value || null;
|
|
44
|
+
if (kv[1] === 'description') out.description = value || null;
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A skill folder's files (relative paths, sorted), their size, which are scripts, and a hash of it all. */
|
|
50
|
+
export function skillFiles(dir) {
|
|
51
|
+
const files = [];
|
|
52
|
+
let size = 0;
|
|
53
|
+
const walk = (d, rel) => {
|
|
54
|
+
for (const e of fs.readdirSync(d, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
55
|
+
if (SKIP.has(e.name) || e.isSymbolicLink()) continue;
|
|
56
|
+
const p = path.join(d, e.name);
|
|
57
|
+
const r = rel ? `${rel}/${e.name}` : e.name;
|
|
58
|
+
if (e.isDirectory()) walk(p, r);
|
|
59
|
+
else if (e.isFile() && files.length < MAX_FILES) {
|
|
60
|
+
const st = fs.statSync(p);
|
|
61
|
+
size += st.size;
|
|
62
|
+
files.push({ path: r, size: st.size, script: SCRIPT.test(e.name) || Boolean(st.mode & 0o111) });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
walk(dir, '');
|
|
67
|
+
const h = crypto.createHash('sha256');
|
|
68
|
+
for (const f of files) {
|
|
69
|
+
h.update(`${f.path}\0`);
|
|
70
|
+
if (size <= MAX_BYTES) h.update(fs.readFileSync(path.join(dir, f.path)));
|
|
71
|
+
}
|
|
72
|
+
return { files, size, scripts: files.filter((f) => f.script).map((f) => f.path), hash: h.digest('hex').slice(0, 16) };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** One skill folder as a record, or null when it has no SKILL.md. */
|
|
76
|
+
export function readSkill(dir) {
|
|
77
|
+
let text;
|
|
78
|
+
try {
|
|
79
|
+
text = fs.readFileSync(path.join(dir, 'SKILL.md'), 'utf8');
|
|
80
|
+
} catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
const meta = parseSkill(text);
|
|
84
|
+
const f = skillFiles(dir);
|
|
85
|
+
return { name: meta.name || path.basename(dir), folder: path.basename(dir), description: meta.description, dir, files: f.files.length, size: f.size, scripts: f.scripts, hash: f.hash, updatedAt: fs.statSync(path.join(dir, 'SKILL.md')).mtime.toISOString() };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The skills directly in a skills folder (one folder a skill). */
|
|
89
|
+
export function skillsIn(root) {
|
|
90
|
+
let names = [];
|
|
91
|
+
try {
|
|
92
|
+
names = fs.readdirSync(root, { withFileTypes: true }).filter((e) => e.isDirectory() && !SKIP.has(e.name)).map((e) => e.name);
|
|
93
|
+
} catch {}
|
|
94
|
+
return names.sort().map((n) => readSkill(path.join(root, n))).filter(Boolean);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Every skill in a repository's checkout, wherever it keeps them (up to four folders down). */
|
|
98
|
+
export function findSkills(root) {
|
|
99
|
+
const out = [];
|
|
100
|
+
const walk = (d, depth) => {
|
|
101
|
+
if (fs.existsSync(path.join(d, 'SKILL.md'))) {
|
|
102
|
+
const s = readSkill(d);
|
|
103
|
+
if (s) out.push({ ...s, path: path.relative(root, d) || '.' });
|
|
104
|
+
return; // a skill's own folders are its files
|
|
105
|
+
}
|
|
106
|
+
if (depth >= 4) return;
|
|
107
|
+
let list = [];
|
|
108
|
+
try {
|
|
109
|
+
list = fs.readdirSync(d, { withFileTypes: true });
|
|
110
|
+
} catch {}
|
|
111
|
+
for (const e of list) if (e.isDirectory() && !e.isSymbolicLink() && !SKIP.has(e.name)) walk(path.join(d, e.name), depth + 1);
|
|
112
|
+
};
|
|
113
|
+
walk(root, 0);
|
|
114
|
+
return out.sort((a, b) => a.name.localeCompare(b.name));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** "owner/repo", a GitHub page address or a git URL as a URL git can clone (https only), or null. */
|
|
118
|
+
export function repoUrl(input) {
|
|
119
|
+
const s = String(input ?? '').trim().replace(/\.git$/, '').replace(/\/+$/, '');
|
|
120
|
+
if (/^[\w.-]+\/[\w.-]+$/.test(s)) return `https://github.com/${s}.git`;
|
|
121
|
+
let u;
|
|
122
|
+
try {
|
|
123
|
+
u = new URL(s);
|
|
124
|
+
} catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
if (u.protocol !== 'https:' || u.username || u.password) return null;
|
|
128
|
+
const parts = u.pathname.split('/').filter(Boolean);
|
|
129
|
+
if (parts.length < 2) return null;
|
|
130
|
+
// A GitHub page inside the repository (…/tree/main/skills) is still that repository.
|
|
131
|
+
const repo = u.hostname === 'github.com' ? parts.slice(0, 2) : parts;
|
|
132
|
+
return `https://${u.host}/${repo.join('/')}.git`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The skills each session used, from its transcript: { name: times }. */
|
|
136
|
+
export function skillUses(text) {
|
|
137
|
+
const out = {};
|
|
138
|
+
for (const m of String(text ?? '').matchAll(/"name":"Skill","input":\{"skill":"([^"\\]{1,120})"/g)) out[m[1]] = (out[m[1]] ?? 0) + 1;
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const run = (cmd, args, opts = {}) =>
|
|
143
|
+
new Promise((resolve, reject) =>
|
|
144
|
+
execFile(cmd, args, { timeout: 120_000, maxBuffer: 4 * 1024 * 1024, ...opts, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' } }, (e, out, errOut) => (e ? reject(new Error(String(errOut || e.message).trim().split('\n').pop())) : resolve(String(out)))),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
export class Skills {
|
|
148
|
+
/**
|
|
149
|
+
* `projects()`: [{ key, name, dir }] the projects with a folder here; `sessions()`: [{ id, name,
|
|
150
|
+
* file }] each agent's session transcript; `clone(url, dir)`: fetches a repository (tests pass their own).
|
|
151
|
+
*/
|
|
152
|
+
constructor({ store, projects = () => [], sessions = () => [], clone = null, cache = path.join(paths.home, 'skills-cache'), personal = personalSkillsDir, now = () => new Date().toISOString() }) {
|
|
153
|
+
Object.assign(this, { store, projects, sessions, cache, personal, now });
|
|
154
|
+
this.clone = clone ?? ((url, dir) => run('git', ['clone', '--depth', '1', '--quiet', url, dir]));
|
|
155
|
+
this.usesCache = new Map(); // transcript -> { key, uses }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get state() {
|
|
159
|
+
const s = (this.store.data.skills ??= {});
|
|
160
|
+
s.sources ??= {};
|
|
161
|
+
return s;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** The places skills live: yours, and each project's. */
|
|
165
|
+
places() {
|
|
166
|
+
return [{ id: 'personal', label: 'Yours (every project)', dir: this.personal() }, ...this.projects().filter((p) => p.dir && fs.existsSync(p.dir)).map((p) => ({ id: `project:${p.key}`, label: p.name ?? p.key, dir: projectSkillsDir(p.dir), project: p.key }))];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
place(id) {
|
|
170
|
+
const p = this.places().find((x) => x.id === id);
|
|
171
|
+
if (!p) throw err(404, 'No such place for skills');
|
|
172
|
+
return p;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Which agents used which skill: { name: [{ id, name, times }] }. */
|
|
176
|
+
usedBy() {
|
|
177
|
+
const out = {};
|
|
178
|
+
for (const s of this.sessions()) {
|
|
179
|
+
let st;
|
|
180
|
+
try {
|
|
181
|
+
st = fs.statSync(s.file);
|
|
182
|
+
} catch {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const key = `${st.mtimeMs}:${st.size}`;
|
|
186
|
+
let c = this.usesCache.get(s.file);
|
|
187
|
+
if (c?.key !== key) {
|
|
188
|
+
// A long session is read from its end: enough to know it used a skill lately.
|
|
189
|
+
let text = '';
|
|
190
|
+
try {
|
|
191
|
+
const max = 24 * 1024 * 1024;
|
|
192
|
+
if (st.size <= max) text = fs.readFileSync(s.file, 'utf8');
|
|
193
|
+
else {
|
|
194
|
+
const fd = fs.openSync(s.file, 'r');
|
|
195
|
+
const buf = Buffer.alloc(max);
|
|
196
|
+
fs.readSync(fd, buf, 0, max, st.size - max);
|
|
197
|
+
fs.closeSync(fd);
|
|
198
|
+
text = buf.toString('utf8');
|
|
199
|
+
}
|
|
200
|
+
} catch {}
|
|
201
|
+
c = { key, uses: skillUses(text) };
|
|
202
|
+
this.usesCache.set(s.file, c);
|
|
203
|
+
}
|
|
204
|
+
for (const [name, times] of Object.entries(c.uses)) (out[name] ??= []).push({ id: s.id, name: s.name, times });
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
list() {
|
|
210
|
+
const used = this.usedBy();
|
|
211
|
+
const places = this.places().map((p) => ({
|
|
212
|
+
id: p.id,
|
|
213
|
+
label: p.label,
|
|
214
|
+
dir: p.dir,
|
|
215
|
+
skills: skillsIn(p.dir).map((s) => {
|
|
216
|
+
const id = `${p.id}/${s.folder}`;
|
|
217
|
+
const src = this.state.sources[id] ?? null;
|
|
218
|
+
return { ...s, id, place: p.id, source: src ? { repo: src.repo, path: src.path, commit: src.commit, installedAt: src.installedAt } : null, edited: Boolean(src && src.hash !== s.hash), usedBy: used[s.name] ?? used[s.folder] ?? [] };
|
|
219
|
+
}),
|
|
220
|
+
}));
|
|
221
|
+
return { places, count: places.reduce((n, p) => n + p.skills.length, 0) };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
find(id) {
|
|
225
|
+
const [placeId, folder] = [String(id).slice(0, String(id).lastIndexOf('/')), String(id).slice(String(id).lastIndexOf('/') + 1)];
|
|
226
|
+
if (!NAME.test(folder)) throw err(400, 'No such skill');
|
|
227
|
+
const p = this.place(placeId);
|
|
228
|
+
const s = readSkill(path.join(p.dir, folder));
|
|
229
|
+
if (!s) throw err(404, 'No such skill');
|
|
230
|
+
return { place: p, skill: s, id: `${p.id}/${folder}` };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** A skill's instructions and its files. */
|
|
234
|
+
show(id) {
|
|
235
|
+
const { skill, id: sid } = this.find(id);
|
|
236
|
+
const f = skillFiles(skill.dir);
|
|
237
|
+
return { ...skill, id: sid, markdown: fs.readFileSync(path.join(skill.dir, 'SKILL.md'), 'utf8').slice(0, 200_000), list: f.files, source: this.state.sources[sid] ?? null };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** A repository fetched to look at: its commit and the skills in it. */
|
|
241
|
+
async fetch(input) {
|
|
242
|
+
const url = repoUrl(input);
|
|
243
|
+
if (!url) throw err(400, 'Give a repository: owner/name on GitHub, or an https git address');
|
|
244
|
+
const dir = path.join(this.cache, crypto.createHash('sha1').update(url).digest('hex').slice(0, 12));
|
|
245
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
246
|
+
fs.mkdirSync(this.cache, { recursive: true });
|
|
247
|
+
try {
|
|
248
|
+
await this.clone(url, dir);
|
|
249
|
+
} catch (e) {
|
|
250
|
+
throw err(502, `Could not fetch ${url}: ${e.message}`);
|
|
251
|
+
}
|
|
252
|
+
let commit = null;
|
|
253
|
+
try {
|
|
254
|
+
commit = (await run('git', ['-C', dir, 'rev-parse', '--short', 'HEAD'])).trim();
|
|
255
|
+
} catch {}
|
|
256
|
+
return { url, dir, commit, skills: findSkills(dir) };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** What a repository offers, before anything is copied: each skill, its files, and which are scripts. */
|
|
260
|
+
async preview(input) {
|
|
261
|
+
const r = await this.fetch(input);
|
|
262
|
+
const have = new Set(this.list().places.flatMap((p) => p.skills.map((s) => s.id)));
|
|
263
|
+
return { repo: r.url, commit: r.commit, skills: r.skills.map(({ dir, ...s }) => ({ ...s, installedIn: this.places().filter((p) => have.has(`${p.id}/${s.folder}`)).map((p) => p.id) })) };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
copy(from, place, folder, source) {
|
|
267
|
+
if (!NAME.test(folder)) throw err(400, `"${folder}" is not a folder name a skill can have`);
|
|
268
|
+
const f = skillFiles(from);
|
|
269
|
+
if (f.files.length >= MAX_FILES || f.size > MAX_BYTES) throw err(400, `${folder} is too large to add (${f.files.length} files)`);
|
|
270
|
+
const to = path.join(place.dir, folder);
|
|
271
|
+
fs.rmSync(to, { recursive: true, force: true });
|
|
272
|
+
fs.mkdirSync(to, { recursive: true });
|
|
273
|
+
// Its own files only: no links out of the folder.
|
|
274
|
+
for (const file of f.files) {
|
|
275
|
+
fs.mkdirSync(path.dirname(path.join(to, file.path)), { recursive: true });
|
|
276
|
+
fs.copyFileSync(path.join(from, file.path), path.join(to, file.path));
|
|
277
|
+
}
|
|
278
|
+
this.state.sources[`${place.id}/${folder}`] = { ...source, hash: skillFiles(to).hash, installedAt: this.now() };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Copy the named skills of a repository to a place. One already there is left unless `replace`. */
|
|
282
|
+
async install({ repo, names = [], place = 'personal', replace = false } = {}) {
|
|
283
|
+
const p = this.place(place);
|
|
284
|
+
if (!Array.isArray(names) || !names.length) throw err(400, 'Pick at least one skill');
|
|
285
|
+
const r = await this.fetch(repo);
|
|
286
|
+
const added = [];
|
|
287
|
+
const skipped = [];
|
|
288
|
+
for (const n of names) {
|
|
289
|
+
const s = r.skills.find((x) => x.folder === n || x.name === n);
|
|
290
|
+
if (!s) throw err(404, `${n} is not in ${r.url}`);
|
|
291
|
+
if (!replace && fs.existsSync(path.join(p.dir, s.folder))) {
|
|
292
|
+
skipped.push(s.folder);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
this.copy(s.dir, p, s.folder, { repo: r.url, path: s.path, commit: r.commit });
|
|
296
|
+
added.push(s.folder);
|
|
297
|
+
}
|
|
298
|
+
this.store.save();
|
|
299
|
+
fs.rmSync(r.dir, { recursive: true, force: true });
|
|
300
|
+
return { added, skipped, place: p.id, ...this.list() };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Whether the place a skill came from has a newer version: { id, current, latest, changed, edited }. */
|
|
304
|
+
async check(id) {
|
|
305
|
+
const { skill, id: sid } = this.find(id);
|
|
306
|
+
const src = this.state.sources[sid];
|
|
307
|
+
if (!src) throw err(409, 'This skill was not added from a repository here, so there is nothing to compare it with');
|
|
308
|
+
const r = await this.fetch(src.repo);
|
|
309
|
+
const theirs = r.skills.find((x) => x.path === src.path) ?? r.skills.find((x) => x.folder === skill.folder);
|
|
310
|
+
fs.rmSync(r.dir, { recursive: true, force: true });
|
|
311
|
+
if (!theirs) return { id: sid, gone: true, current: src.commit, latest: r.commit, changed: false, edited: src.hash !== skill.hash };
|
|
312
|
+
return { id: sid, current: src.commit, latest: r.commit, changed: theirs.hash !== src.hash, edited: src.hash !== skill.hash };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Replace a skill with its repository's current version (your edits to it go). */
|
|
316
|
+
async update(id) {
|
|
317
|
+
const { place, skill, id: sid } = this.find(id);
|
|
318
|
+
const src = this.state.sources[sid];
|
|
319
|
+
if (!src) throw err(409, 'This skill was not added from a repository here');
|
|
320
|
+
const r = await this.fetch(src.repo);
|
|
321
|
+
const theirs = r.skills.find((x) => x.path === src.path) ?? r.skills.find((x) => x.folder === skill.folder);
|
|
322
|
+
if (!theirs) throw err(404, `${skill.folder} is no longer in ${src.repo}`);
|
|
323
|
+
this.copy(theirs.dir, place, skill.folder, { repo: src.repo, path: theirs.path, commit: r.commit });
|
|
324
|
+
this.store.save();
|
|
325
|
+
fs.rmSync(r.dir, { recursive: true, force: true });
|
|
326
|
+
return this.list();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Delete a skill's folder. */
|
|
330
|
+
remove(id) {
|
|
331
|
+
const { skill, place, id: sid } = this.find(id);
|
|
332
|
+
// Only ever a folder directly inside a skills folder.
|
|
333
|
+
if (path.dirname(fs.realpathSync(skill.dir)) !== fs.realpathSync(place.dir)) throw err(400, 'Not a skill folder');
|
|
334
|
+
fs.rmSync(skill.dir, { recursive: true, force: true });
|
|
335
|
+
delete this.state.sources[sid];
|
|
336
|
+
this.store.save();
|
|
337
|
+
return this.list();
|
|
338
|
+
}
|
|
339
|
+
}
|
package/src/system.js
CHANGED
|
@@ -7,6 +7,7 @@ import { execFile } from 'node:child_process';
|
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import os from 'node:os';
|
|
9
9
|
import path from 'node:path';
|
|
10
|
+
import { listeners, portsBySession } from './lanes.js';
|
|
10
11
|
|
|
11
12
|
const SAMPLE_MS = 5000;
|
|
12
13
|
const KEEP = 120; // ten minutes of samples
|
|
@@ -157,9 +158,12 @@ async function disks(folders) {
|
|
|
157
158
|
}
|
|
158
159
|
|
|
159
160
|
export class System {
|
|
160
|
-
/**
|
|
161
|
-
|
|
162
|
-
|
|
161
|
+
/**
|
|
162
|
+
* `agents()` lists the agent records (id, name, pid of its open session, its lane or null);
|
|
163
|
+
* `folders()` the [label, dir] pairs whose disks to show; `lanes()` the lane settings (lanes.js).
|
|
164
|
+
*/
|
|
165
|
+
constructor({ agents = () => [], folders = () => [['Home', os.homedir()]], lanes = () => null, version = null, timer = true } = {}) {
|
|
166
|
+
Object.assign(this, { agents, folders, lanes, version });
|
|
163
167
|
this.history = [];
|
|
164
168
|
this.last = cpuTimes();
|
|
165
169
|
this.cpuPercent = 0;
|
|
@@ -187,16 +191,20 @@ export class System {
|
|
|
187
191
|
async view() {
|
|
188
192
|
const cpus = os.cpus();
|
|
189
193
|
const cores = cpus.length || 1;
|
|
190
|
-
const [name, mem, disk, ps] = await Promise.all([(this.osName ??= osName()), memory(), disks(this.folders()), run('ps', ['-Ao', 'pid=,ppid=,pcpu=,rss=,comm='])]);
|
|
194
|
+
const [name, mem, disk, ps, listening] = await Promise.all([(this.osName ??= osName()), memory(), disks(this.folders()), run('ps', ['-Ao', 'pid=,ppid=,pcpu=,rss=,comm=']), listeners()]);
|
|
191
195
|
const procs = parsePs(ps);
|
|
192
196
|
// The memory in the history is the same measure as the one shown (what can't be freed at once).
|
|
193
197
|
if (this.history.length) this.history[this.history.length - 1].mem = mem.percent;
|
|
194
198
|
const load = os.loadavg().map((n) => Math.round(n * 100) / 100);
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
.
|
|
198
|
-
|
|
199
|
-
|
|
199
|
+
// With what each listens on, and whether that is inside its own lane of ports.
|
|
200
|
+
const sessions = portsBySession(
|
|
201
|
+
this.agents()
|
|
202
|
+
.filter((a) => a.pid)
|
|
203
|
+
.map((a) => ({ id: a.id, name: a.name, pid: a.pid, lane: a.lane ?? null, ...(treeUsage(procs, a.pid) ?? { cpu: 0, mem: 0, count: 0 }) }))
|
|
204
|
+
.filter((a) => a.count),
|
|
205
|
+
procs,
|
|
206
|
+
listening,
|
|
207
|
+
).sort((x, y) => y.cpu - x.cpu || y.mem - x.mem);
|
|
200
208
|
const self = treeUsage(procs, process.pid);
|
|
201
209
|
const nets = Object.entries(os.networkInterfaces())
|
|
202
210
|
.flatMap(([nic, list]) => (list ?? []).filter((n) => !n.internal && n.family === 'IPv4').map((n) => ({ nic, address: n.address })));
|
|
@@ -232,6 +240,7 @@ export class System {
|
|
|
232
240
|
count: procs.length,
|
|
233
241
|
top: [...procs].sort((x, y) => y.cpu - x.cpu || y.mem - x.mem).slice(0, 12).map(({ pid, cpu, mem: m, name: n, command }) => ({ pid, cpu, mem: m, name: n, command })),
|
|
234
242
|
},
|
|
243
|
+
lanes: this.lanes(),
|
|
235
244
|
history: this.history,
|
|
236
245
|
sampleSeconds: SAMPLE_MS / 1000,
|
|
237
246
|
};
|