vit 0.0.1 → 0.1.3

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.
@@ -0,0 +1,181 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 sol pbc
3
+
4
+ import { requireDid } from '../lib/config.js';
5
+ import { CAP_COLLECTION } from '../lib/constants.js';
6
+ import { restoreAgent } from '../lib/oauth.js';
7
+ import { readBeaconSet, readFollowing, readLog } from '../lib/vit-dir.js';
8
+ import { requireAgent, detectCodingAgent } from '../lib/agent.js';
9
+ import { shouldBypassVet } from '../lib/trust-gate.js';
10
+ import { resolveRef, REF_PATTERN } from '../lib/cap-ref.js';
11
+ import { brand, name } from '../lib/brand.js';
12
+ import { resolvePds, listRecordsFromPds, batchQuery } from '../lib/pds.js';
13
+ import { loadConfig } from '../lib/config.js';
14
+ import { jsonOk, jsonError } from '../lib/json-output.js';
15
+
16
+ export default function register(program) {
17
+ program
18
+ .command('remix')
19
+ .argument('<ref>', 'Three-word cap reference (e.g. fast-cache-invalidation)')
20
+ .description('Derive a vetted cap into the local codebase')
21
+ .option('--did <did>', 'DID to use')
22
+ .option('--json', 'Output as JSON')
23
+ .option('-v, --verbose', 'Show step-by-step details')
24
+ .action(async (ref, opts) => {
25
+ try {
26
+ const gate = requireAgent();
27
+ if (!gate.ok) {
28
+ if (opts.json) {
29
+ jsonError('agent required', 'run vit remix from a coding agent');
30
+ return;
31
+ }
32
+ console.error(`${name} remix should be run by a coding agent (e.g. claude code, gemini cli).`);
33
+ console.error(`open your agent and ask it to run '${name} remix' for you.`);
34
+ process.exitCode = 1;
35
+ return;
36
+ }
37
+
38
+ const { verbose } = opts;
39
+ const vlog = opts.json ? (...a) => console.error(...a) : console.log;
40
+
41
+ if (!REF_PATTERN.test(ref)) {
42
+ if (opts.json) {
43
+ jsonError('invalid ref', 'expected three lowercase words with dashes');
44
+ return;
45
+ }
46
+ console.error('invalid ref. expected three lowercase words with dashes (e.g. fast-cache-invalidation)');
47
+ process.exitCode = 1;
48
+ return;
49
+ }
50
+
51
+ if (opts.json && !(opts.did || loadConfig().did)) {
52
+ jsonError('no DID configured', "run 'vit login <handle>' first");
53
+ return;
54
+ }
55
+ const did = requireDid(opts);
56
+ if (!did) return;
57
+ if (verbose) vlog(`[verbose] DID: ${did}`);
58
+
59
+ const beaconSet = readBeaconSet();
60
+ if (beaconSet.size === 0) {
61
+ if (opts.json) {
62
+ jsonError('no beacon set', "run 'vit init' first");
63
+ return;
64
+ }
65
+ console.error(`no beacon set. run '${name} init' in a project directory first.`);
66
+ process.exitCode = 1;
67
+ return;
68
+ }
69
+ if (verbose) vlog(`[verbose] beacons: ${[...beaconSet].join(', ')}`);
70
+
71
+ const trusted = readLog('trusted.jsonl');
72
+ const trustedEntry = trusted.find(e => e.ref === ref);
73
+ if (!trustedEntry) {
74
+ const trustGate = shouldBypassVet();
75
+ if (!trustGate.bypass) {
76
+ if (opts.json) {
77
+ jsonError(`cap '${ref}' is not trusted`, `ask user to run 'vit vet ${ref} --trust'`);
78
+ return;
79
+ }
80
+ console.error(`cap '${ref}' is not trusted. ask the user to vet it first:`);
81
+ console.error('');
82
+ console.error(` vit vet ${ref}`);
83
+ console.error('');
84
+ console.error('after reviewing, they can trust it with:');
85
+ console.error('');
86
+ console.error(` vit vet ${ref} --trust`);
87
+ if (detectCodingAgent()) {
88
+ console.error('');
89
+ console.error('or, to trust all items without review:');
90
+ console.error('');
91
+ console.error(' vit vet --dangerous-accept --confirm');
92
+ }
93
+ process.exitCode = 1;
94
+ return;
95
+ }
96
+ if (verbose) vlog(`[verbose] vet gate bypassed: ${trustGate.reason}`);
97
+ }
98
+ if (verbose && trustedEntry) vlog(`[verbose] trusted entry found, uri: ${trustedEntry.uri}`);
99
+
100
+ const { agent } = await restoreAgent(did);
101
+ if (verbose) vlog('[verbose] session restored');
102
+
103
+ const following = readFollowing();
104
+ const dids = following.map(e => e.did);
105
+ dids.push(did);
106
+
107
+ const allRecords = await batchQuery(dids, async (repoDid) => {
108
+ const pds = await resolvePds(repoDid);
109
+ if (verbose) vlog(`[verbose] ${repoDid}: resolved PDS ${pds}`);
110
+ return (await listRecordsFromPds(pds, repoDid, CAP_COLLECTION, 50)).records;
111
+ }, { verbose });
112
+
113
+ let match = null;
114
+ for (const records of allRecords) {
115
+ for (const rec of records) {
116
+ if (!beaconSet.has(rec.value.beacon)) continue;
117
+ const recRef = resolveRef(rec.value, rec.cid);
118
+ if (recRef === ref) {
119
+ if (!match || (rec.value.createdAt || '') > (match.value.createdAt || '')) {
120
+ match = rec;
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ if (!match) {
127
+ if (opts.json) {
128
+ jsonError(`no cap found with ref '${ref}' for this beacon`);
129
+ return;
130
+ }
131
+ console.error(`no cap found with ref '${ref}' for this beacon.`);
132
+ process.exitCode = 1;
133
+ return;
134
+ }
135
+
136
+ const record = match.value;
137
+ const author = match.uri.split('/')[2];
138
+ const title = record.title || ref;
139
+ const description = record.description || '';
140
+ const text = record.text || '';
141
+
142
+ if (opts.json) {
143
+ jsonOk({ ref, author, title, description, text });
144
+ return;
145
+ }
146
+
147
+ console.log(`# ${brand} remix: ${title}`);
148
+ console.log('');
149
+ console.log(`ref: ${ref}`);
150
+ console.log(`author: ${author}`);
151
+ if (description) console.log(`description: ${description}`);
152
+ console.log('');
153
+ console.log('---');
154
+ console.log('');
155
+ console.log('you are remixing a vetted cap into the current codebase.');
156
+ console.log('create a thorough implementation plan that:');
157
+ console.log('');
158
+ console.log('1. adapts the cap to this repo\'s architecture, conventions, and existing code');
159
+ console.log('2. follows local guidelines (CLAUDE.md, project conventions, coding standards)');
160
+ console.log('3. identifies all files to create or modify');
161
+ console.log('4. specifies tests to add or update');
162
+ console.log('5. notes any dependencies or migrations needed');
163
+ console.log('');
164
+ console.log('do not apply the cap blindly. produce a well-researched plan first.');
165
+ console.log('');
166
+ console.log('---');
167
+ console.log('');
168
+ console.log('## cap content');
169
+ console.log('');
170
+ console.log(text);
171
+ } catch (err) {
172
+ const msg = err instanceof Error ? err.message : String(err);
173
+ if (opts.json) {
174
+ jsonError(msg);
175
+ return;
176
+ }
177
+ console.error(msg);
178
+ process.exitCode = 1;
179
+ }
180
+ });
181
+ }
@@ -0,0 +1,207 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 sol pbc
3
+
4
+ import { CAP_COLLECTION, SKILL_COLLECTION, DEFAULT_JETSTREAM_URL } from '../lib/constants.js';
5
+ import { resolveRef } from '../lib/cap-ref.js';
6
+ import { resolveHandleFromDid } from '../lib/pds.js';
7
+ import { brand } from '../lib/brand.js';
8
+ import { jsonOk, jsonError } from '../lib/json-output.js';
9
+ import { readBeaconSet } from '../lib/vit-dir.js';
10
+
11
+ export default function register(program) {
12
+ program
13
+ .command('scan')
14
+ .description('Discover cap and skill publishers across the network via Jetstream replay')
15
+ .option('--days <n>', 'Number of days to replay', '7')
16
+ .option('--beacon <beacon>', 'Filter by beacon (caps only)')
17
+ .option('--skills', 'Show only skill publishers')
18
+ .option('--caps', 'Show only cap publishers')
19
+ .option('--tag <tag>', 'Filter skills by tag')
20
+ .option('-v, --verbose', 'Show each event as it arrives')
21
+ .option('--json', 'Output as JSON')
22
+ .option('--jetstream <url>', 'Jetstream WebSocket URL (default: VIT_JETSTREAM_URL env or built-in)')
23
+ .action(async (opts) => {
24
+ try {
25
+ const vlog = opts.json ? (...a) => console.error(...a) : console.log;
26
+ const days = parseInt(opts.days, 10);
27
+ if (isNaN(days) || days < 1) {
28
+ if (opts.json) {
29
+ jsonError('--days must be a positive integer');
30
+ return;
31
+ }
32
+ console.error('error: --days must be a positive integer');
33
+ process.exitCode = 1;
34
+ return;
35
+ }
36
+
37
+ const jetstreamUrl = opts.jetstream || process.env.VIT_JETSTREAM_URL || DEFAULT_JETSTREAM_URL;
38
+
39
+ const wantCaps = !opts.skills;
40
+ const wantSkills = !opts.caps;
41
+ let beaconSet = null;
42
+ if (opts.beacon) {
43
+ if (opts.beacon === '.') {
44
+ beaconSet = readBeaconSet();
45
+ if (beaconSet.size === 0) {
46
+ if (opts.json) {
47
+ jsonError("no beacon set — run 'vit init' first");
48
+ return;
49
+ }
50
+ console.error("no beacon set — run 'vit init' first");
51
+ process.exitCode = 1;
52
+ return;
53
+ }
54
+ } else {
55
+ beaconSet = new Set([opts.beacon]);
56
+ }
57
+ }
58
+
59
+ const cursor = (Date.now() - days * 86400000) * 1000;
60
+ const timeout = Math.max(120000, Math.min(600000, days * 60000));
61
+
62
+ // Build wanted collections
63
+ const collections = [];
64
+ if (wantCaps) collections.push(CAP_COLLECTION);
65
+ if (wantSkills) collections.push(SKILL_COLLECTION);
66
+
67
+ const url = new URL(jetstreamUrl);
68
+ for (const col of collections) {
69
+ url.searchParams.append('wantedCollections', col);
70
+ }
71
+ url.searchParams.set('cursor', String(cursor));
72
+
73
+ const scanType = wantCaps && wantSkills ? 'cap + skill' : wantSkills ? 'skill' : 'cap';
74
+ if (!opts.json) {
75
+ console.log(`${brand} scan`);
76
+ console.log(` Replaying ${days} day${days === 1 ? '' : 's'} of ${scanType} events...`);
77
+ if (beaconSet) console.log(` Beacon filter: ${[...beaconSet].join(', ')}`);
78
+ if (opts.tag) console.log(` Tag filter: ${opts.tag}`);
79
+ console.log(` Timeout: ${Math.round(timeout / 1000)}s`);
80
+ console.log('');
81
+ }
82
+
83
+ const publishers = new Map();
84
+
85
+ await new Promise((resolve, reject) => {
86
+ const ws = new WebSocket(url.toString());
87
+ const timer = setTimeout(() => {
88
+ ws.close();
89
+ resolve();
90
+ }, timeout);
91
+
92
+ ws.onmessage = (event) => {
93
+ let msg;
94
+ try { msg = JSON.parse(event.data); } catch { return; }
95
+
96
+ if (msg.kind !== 'commit' || msg.commit?.operation !== 'create') return;
97
+
98
+ const record = msg.commit?.record;
99
+ if (!record) return;
100
+
101
+ const collection = msg.commit?.collection;
102
+ const isCapEvent = collection === CAP_COLLECTION;
103
+ const isSkillEvent = collection === SKILL_COLLECTION;
104
+
105
+ if (!isCapEvent && !isSkillEvent) return;
106
+
107
+ // Apply filters
108
+ if (isCapEvent && beaconSet && !beaconSet.has(record.beacon)) return;
109
+ if (isSkillEvent && opts.tag) {
110
+ const tags = record.tags || [];
111
+ if (!tags.some(t => t.toLowerCase() === opts.tag.toLowerCase())) return;
112
+ }
113
+
114
+ const did = msg.did;
115
+ const ref = isCapEvent && msg.commit?.cid ? resolveRef(record, msg.commit.cid) : null;
116
+
117
+ if (opts.verbose) {
118
+ const didShort = did.slice(-12);
119
+ if (isCapEvent) {
120
+ const title = record.title || '';
121
+ const refPart = ref ? ` (${ref})` : '';
122
+ vlog(` ${didShort}: [cap] ${title}${refPart} [${record.beacon || 'no beacon'}]`);
123
+ } else {
124
+ const skillName = record.name || '';
125
+ const tags = record.tags ? ` [${record.tags.join(', ')}]` : '';
126
+ vlog(` ${didShort}: [skill] ${skillName}${tags}`);
127
+ }
128
+ }
129
+
130
+ if (!publishers.has(did)) {
131
+ publishers.set(did, { capCount: 0, skillCount: 0, beacons: new Set(), tags: new Set(), lastActive: '' });
132
+ }
133
+ const entry = publishers.get(did);
134
+ if (isCapEvent) {
135
+ entry.capCount++;
136
+ if (record.beacon) entry.beacons.add(record.beacon);
137
+ } else {
138
+ entry.skillCount++;
139
+ if (record.tags) {
140
+ for (const t of record.tags) entry.tags.add(t);
141
+ }
142
+ }
143
+ if (record.createdAt && record.createdAt > entry.lastActive) {
144
+ entry.lastActive = record.createdAt;
145
+ }
146
+ };
147
+
148
+ ws.onerror = (err) => {
149
+ clearTimeout(timer);
150
+ reject(new Error(`WebSocket error: ${err?.message ?? 'unknown'}`));
151
+ };
152
+
153
+ ws.onclose = () => {
154
+ clearTimeout(timer);
155
+ resolve();
156
+ };
157
+ });
158
+
159
+ if (publishers.size === 0) {
160
+ if (opts.json) {
161
+ jsonOk({ publishers: [] });
162
+ return;
163
+ }
164
+ console.log(`no ${scanType} publishers found in this time window.`);
165
+ return;
166
+ }
167
+
168
+ const entries = [];
169
+ for (const [did, stats] of publishers) {
170
+ const handle = await resolveHandleFromDid(did);
171
+ entries.push({ handle, did, ...stats, beacons: [...stats.beacons], tags: [...stats.tags] });
172
+ }
173
+
174
+ const totalCount = (e) => e.capCount + e.skillCount;
175
+ entries.sort((a, b) => totalCount(b) - totalCount(a));
176
+
177
+ if (opts.json) {
178
+ jsonOk({ publishers: entries });
179
+ return;
180
+ }
181
+ console.log(`found ${entries.length} publisher${entries.length === 1 ? '' : 's'}:\n`);
182
+ for (const e of entries) {
183
+ console.log(` @${e.handle}`);
184
+ const parts = [];
185
+ if (wantCaps && e.capCount > 0) {
186
+ const beaconStr = e.beacons.length > 0 ? e.beacons.join(', ') : '(none)';
187
+ parts.push(`caps: ${e.capCount} beacons: ${beaconStr}`);
188
+ }
189
+ if (wantSkills && e.skillCount > 0) {
190
+ const tagStr = e.tags.length > 0 ? e.tags.join(', ') : '(none)';
191
+ parts.push(`skills: ${e.skillCount} tags: ${tagStr}`);
192
+ }
193
+ const lastActive = e.lastActive ? e.lastActive.split('T')[0] : 'unknown';
194
+ parts.push(`last active: ${lastActive}`);
195
+ console.log(` ${parts.join(' ')}`);
196
+ }
197
+ } catch (err) {
198
+ const msg = err instanceof Error ? err.message : String(err);
199
+ if (opts.json) {
200
+ jsonError(msg);
201
+ return;
202
+ }
203
+ console.error(msg);
204
+ process.exitCode = 1;
205
+ }
206
+ });
207
+ }
@@ -0,0 +1,76 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 sol pbc
3
+
4
+ import { spawnSync } from 'node:child_process';
5
+ import { loadConfig, saveConfig } from '../lib/config.js';
6
+ import { requireNotAgent } from '../lib/agent.js';
7
+ import { which } from '../lib/compat.js';
8
+ import { mark, brand, name } from '../lib/brand.js';
9
+
10
+ export default function register(program) {
11
+ program
12
+ .command('setup')
13
+ .description('Initialize user-level vit setup')
14
+ .action(async () => {
15
+ try {
16
+ const gate = requireNotAgent();
17
+ if (!gate.ok) {
18
+ console.error(`${name} setup must be run by a human. run it in your own terminal.`);
19
+ process.exitCode = 1;
20
+ return;
21
+ }
22
+
23
+ const gitPath = which('git');
24
+ console.log(`${mark} git: ${gitPath ? 'found' : 'not found'}`);
25
+ if (!gitPath) {
26
+ console.error('missing required tool: git');
27
+ process.exitCode = 1;
28
+ return;
29
+ }
30
+
31
+ // skill installation
32
+ const npxPath = which('npx');
33
+ if (npxPath) {
34
+ try {
35
+ const result = spawnSync(
36
+ 'npx', ['skills', 'add', 'https://github.com/solpbc/vit/tree/main/skills/vit', '-a', 'claude-code', '-y'],
37
+ {
38
+ encoding: 'utf-8',
39
+ stdio: ['pipe', 'pipe', 'pipe'],
40
+ }
41
+ );
42
+ if (result.status === 0) {
43
+ console.log(`${mark} skill: installed (using-vit)`);
44
+ } else {
45
+ const errText = (result.stderr || '').trim();
46
+ console.log(`${mark} skill: failed (${errText || 'unknown error'})`);
47
+ }
48
+ } catch {
49
+ console.log(`${mark} skill: failed (could not run npx skills)`);
50
+ }
51
+ } else {
52
+ console.log(`${mark} skill: skipped (npx not found)`);
53
+ }
54
+
55
+ const config = loadConfig();
56
+ if (config.did) {
57
+ console.log(`${mark} login: ${config.did}`);
58
+ } else {
59
+ console.log(`${mark} login: not logged in`);
60
+ console.log(`next: run '${name} login <handle>' to authenticate with Bluesky`);
61
+ }
62
+
63
+ if (!config.setup_at) {
64
+ config.setup_at = Math.floor(Date.now() / 1000);
65
+ saveConfig(config);
66
+ }
67
+
68
+ if (config.did) {
69
+ console.log(`${brand} setup complete`);
70
+ }
71
+ } catch (err) {
72
+ console.error(err instanceof Error ? err.message : String(err));
73
+ process.exitCode = 1;
74
+ }
75
+ });
76
+ }