broader-vids 0.0.0-stage → 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,99 @@
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const os = require("node:os");
5
+ const path = require("node:path");
6
+ const { randomUUID } = require("node:crypto");
7
+
8
+ function resolveCodex(env = process.env, platform = process.platform) {
9
+ if (platform !== "win32") return ["codex", []];
10
+ const directories = (env.PATH || env.Path || "").split(path.delimiter);
11
+ for (const directory of directories) {
12
+ const folder = directory.replace(/^"(.*)"$/, "$1");
13
+ if (!folder) continue;
14
+ const executable = path.join(folder, "codex.exe");
15
+ if (fs.existsSync(executable)) return [executable, []];
16
+ const script = path.join(
17
+ folder,
18
+ "node_modules",
19
+ "@openai",
20
+ "codex",
21
+ "bin",
22
+ "codex.js",
23
+ );
24
+ if (fs.existsSync(script)) return [process.execPath, [script]];
25
+ }
26
+ throw new Error(
27
+ "Install Codex CLI and add it to PATH before installing the plugin.",
28
+ );
29
+ }
30
+
31
+ function installPlugin(version, run, env = process.env) {
32
+ const [command, prefix] = resolveCodex(env);
33
+ const home = path.resolve(
34
+ env.CODEX_HOME || path.join(os.homedir(), ".codex"),
35
+ );
36
+ const root = path.join(home, "broader-vids-npm");
37
+ const directory = path.join(root, ".agents", "plugins");
38
+ for (const folder of [root, path.join(root, ".agents"), directory]) {
39
+ fs.mkdirSync(folder, { recursive: true });
40
+ if (fs.lstatSync(folder).isSymbolicLink())
41
+ throw new Error(
42
+ "The managed marketplace must not contain symbolic links.",
43
+ );
44
+ }
45
+ const lock = path.join(root, "install.lock");
46
+ let handle;
47
+ try {
48
+ handle = fs.openSync(lock, "wx", 0o600);
49
+ } catch (error) {
50
+ if (error.code === "EEXIST")
51
+ throw new Error(
52
+ "Another plugin installation holds this marketplace. Wait for it to finish.",
53
+ );
54
+ throw error;
55
+ }
56
+ const catalog = path.join(directory, "marketplace.json");
57
+ const temporary = path.join(directory, `.${randomUUID()}.tmp`);
58
+ try {
59
+ if (fs.existsSync(catalog) && fs.lstatSync(catalog).isSymbolicLink())
60
+ throw new Error("The managed catalog must not be a symbolic link.");
61
+ const marketplace = {
62
+ name: "broader-vids-npm",
63
+ interface: { displayName: "Broader Vids (npm)" },
64
+ plugins: [
65
+ {
66
+ name: "broader-vids",
67
+ source: {
68
+ source: "npm",
69
+ package: "broader-vids",
70
+ version,
71
+ registry: "https://registry.npmjs.org",
72
+ },
73
+ policy: { installation: "AVAILABLE", authentication: "ON_USE" },
74
+ category: "Productivity",
75
+ },
76
+ ],
77
+ };
78
+ fs.writeFileSync(temporary, JSON.stringify(marketplace, null, 2) + "\n", {
79
+ flag: "wx",
80
+ mode: 0o600,
81
+ });
82
+ fs.renameSync(temporary, catalog);
83
+ run(command, [...prefix, "plugin", "marketplace", "add", root]);
84
+ run(command, [...prefix, "plugin", "add", "broader-vids@broader-vids-npm"]);
85
+ return { status: "installed", marketplace: marketplace.name, version };
86
+ } finally {
87
+ try {
88
+ fs.rmSync(temporary, { force: true });
89
+ } finally {
90
+ try {
91
+ fs.closeSync(handle);
92
+ } finally {
93
+ fs.unlinkSync(lock);
94
+ }
95
+ }
96
+ }
97
+ }
98
+
99
+ module.exports = { installPlugin, resolveCodex };
@@ -0,0 +1,327 @@
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const os = require("node:os");
5
+ const path = require("node:path");
6
+ const { randomUUID } = require("node:crypto");
7
+ const { withSetup } = require("./runtime-lock.cjs");
8
+
9
+ const MARKER = ".broader-vids-profile.json";
10
+ const FORMAT = "broader-vids.profile";
11
+ const KEEP_RESET = new Set([
12
+ MARKER,
13
+ ".launcher",
14
+ "runtimes",
15
+ "wheels",
16
+ "models",
17
+ "access",
18
+ "launcher.json",
19
+ ]);
20
+ const MANAGED = new Set([
21
+ ...KEEP_RESET,
22
+ ".dashboard",
23
+ ".reset-pending",
24
+ "onboarding.json",
25
+ "backups",
26
+ "evidence",
27
+ "recommendations",
28
+ "discovery",
29
+ "feedback",
30
+ "runs",
31
+ "archives",
32
+ "broader-vids.sqlite3",
33
+ "broader-vids.sqlite3-wal",
34
+ "broader-vids.sqlite3-shm",
35
+ "broader-vids.sqlite3-journal",
36
+ ]);
37
+
38
+ function inside(parent, child) {
39
+ const relative = path.relative(parent, child);
40
+ return (
41
+ relative === "" ||
42
+ (!relative.startsWith(`..${path.sep}`) &&
43
+ relative !== ".." &&
44
+ !path.isAbsolute(relative))
45
+ );
46
+ }
47
+
48
+ function ensureMarker(root) {
49
+ const file = path.join(root, MARKER);
50
+ if (fs.existsSync(file)) return;
51
+ const temporary = path.join(root, `.${randomUUID()}.tmp`);
52
+ try {
53
+ fs.writeFileSync(
54
+ temporary,
55
+ JSON.stringify({ format: FORMAT, version: 1 }),
56
+ { flag: "wx", mode: 0o600 },
57
+ );
58
+ fs.renameSync(temporary, file);
59
+ } finally {
60
+ if (fs.existsSync(temporary)) fs.unlinkSync(temporary);
61
+ }
62
+ }
63
+
64
+ function readSmall(file) {
65
+ if (fs.lstatSync(file).isSymbolicLink() || fs.statSync(file).size > 4096)
66
+ throw Error("Profile identity metadata is invalid.");
67
+ return JSON.parse(fs.readFileSync(file, "utf8"));
68
+ }
69
+
70
+ function recognized(root) {
71
+ const marker = path.join(root, MARKER);
72
+ if (fs.existsSync(marker)) {
73
+ const value = readSmall(marker);
74
+ return value && value.format === FORMAT && value.version === 1;
75
+ }
76
+ const config = path.join(root, "onboarding.json");
77
+ const database = path.join(root, "broader-vids.sqlite3");
78
+ if (
79
+ !fs.existsSync(config) ||
80
+ !fs.existsSync(database) ||
81
+ fs.lstatSync(database).isSymbolicLink()
82
+ )
83
+ return false;
84
+ const value = readSmall(config);
85
+ const header = Buffer.alloc(16);
86
+ const descriptor = fs.openSync(database, "r");
87
+ try {
88
+ fs.readSync(descriptor, header, 0, header.length, 0);
89
+ } finally {
90
+ fs.closeSync(descriptor);
91
+ }
92
+ return (
93
+ value &&
94
+ value.version === 1 &&
95
+ ["youtube", "bilibili", "both"].includes(value.default_platform) &&
96
+ header.toString("binary") === "SQLite format 3\0"
97
+ );
98
+ }
99
+
100
+ function validateRoot(root, packageRoot) {
101
+ root = path.resolve(root);
102
+ const protectedPaths = [
103
+ path.parse(root).root,
104
+ os.homedir(),
105
+ os.tmpdir(),
106
+ process.env.USERPROFILE,
107
+ process.env.LOCALAPPDATA,
108
+ process.env.APPDATA,
109
+ process.env.WINDIR,
110
+ ]
111
+ .filter(Boolean)
112
+ .map((item) => path.resolve(item));
113
+ if (
114
+ protectedPaths.some((item) => inside(root, item)) ||
115
+ inside(root, path.resolve(packageRoot))
116
+ )
117
+ throw Error(
118
+ "Reset and Clean cannot target a system directory, user home, or a directory containing the plugin.",
119
+ );
120
+ for (let current = root; ; current = path.dirname(current)) {
121
+ if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink())
122
+ throw Error(
123
+ "The data directory and its parents cannot be symbolic links or junctions.",
124
+ );
125
+ if (path.dirname(current) === current) break;
126
+ }
127
+ for (const name of [".launcher", ".dashboard"]) {
128
+ const control = path.join(root, name);
129
+ if (fs.existsSync(control) && fs.lstatSync(control).isSymbolicLink())
130
+ throw Error(
131
+ "Profile control directories cannot be symbolic links or junctions.",
132
+ );
133
+ }
134
+ if (
135
+ fs.existsSync(root) &&
136
+ (!fs.statSync(root).isDirectory() || !recognized(root))
137
+ )
138
+ throw Error(
139
+ "The target is not a recognized Broader Vids profile. No files were removed.",
140
+ );
141
+ return root;
142
+ }
143
+
144
+ function managed(name) {
145
+ return (
146
+ MANAGED.has(name) ||
147
+ /^broader-vids\.before-[A-Za-z0-9-]+\.sqlite3(?:-wal|-shm|-journal)?$/.test(
148
+ name,
149
+ )
150
+ );
151
+ }
152
+
153
+ function sizeOf(target) {
154
+ let files = 0,
155
+ bytes = 0;
156
+ const pending = [target];
157
+ while (pending.length) {
158
+ const current = pending.pop();
159
+ const stat = fs.lstatSync(current);
160
+ if (stat.isSymbolicLink() || stat.isFile()) {
161
+ files += 1;
162
+ bytes += stat.size;
163
+ } else if (stat.isDirectory()) {
164
+ for (const name of fs.readdirSync(current))
165
+ pending.push(path.join(current, name));
166
+ } else
167
+ throw Error(
168
+ "A managed directory contains an unsupported special file. No files were removed.",
169
+ );
170
+ }
171
+ return { files, bytes };
172
+ }
173
+
174
+ function preview(root, operation, packageRoot) {
175
+ if (!["reset", "clean"].includes(operation))
176
+ throw Error("Use reset or clean.");
177
+ root = validateRoot(root, packageRoot);
178
+ const result = {
179
+ operation,
180
+ data_directory: root,
181
+ status: "preview",
182
+ dry_run: true,
183
+ files: 0,
184
+ bytes: 0,
185
+ remove: [],
186
+ preserve: [],
187
+ unknown: [],
188
+ blockers: [],
189
+ };
190
+ if (!fs.existsSync(root)) return { ...result, status: "absent" };
191
+ for (const name of fs.readdirSync(root)) {
192
+ if (/^[a-f0-9]{32}\.lock$/.test(name)) {
193
+ result.blockers.push(`Active or uncertain engine lock: ${name}`);
194
+ continue;
195
+ }
196
+ if (!managed(name)) {
197
+ result.unknown.push(name);
198
+ continue;
199
+ }
200
+ if (operation === "reset" && KEEP_RESET.has(name)) {
201
+ result.preserve.push(name);
202
+ continue;
203
+ }
204
+ result.remove.push(name);
205
+ const size = sizeOf(path.join(root, name));
206
+ result.files += size.files;
207
+ result.bytes += size.bytes;
208
+ }
209
+ const controls = path.join(root, ".launcher");
210
+ if (fs.existsSync(controls)) {
211
+ for (const name of fs.readdirSync(controls)) {
212
+ if (
213
+ name === "setup.json" ||
214
+ name === "gate.lock" ||
215
+ name.endsWith(".use.json")
216
+ )
217
+ result.blockers.push(`Active or uncertain launcher operation: ${name}`);
218
+ }
219
+ }
220
+ if (fs.existsSync(path.join(root, "access", "bilibili", "browser.lock")))
221
+ result.blockers.push("Finish the active Bilibili browser operation.");
222
+ if (operation === "clean" && result.unknown.length)
223
+ result.blockers.push(
224
+ "Move unknown files out of this directory before Clean.",
225
+ );
226
+ return result;
227
+ }
228
+
229
+ function removeOwned(root, name) {
230
+ const target = path.join(root, name);
231
+ if (path.dirname(target) !== root || !managed(name))
232
+ throw Error("A removal target is outside the managed profile scope.");
233
+ fs.rmSync(target, {
234
+ recursive: true,
235
+ force: false,
236
+ maxRetries: 3,
237
+ retryDelay: 100,
238
+ });
239
+ }
240
+
241
+ function resetFiles(root, names) {
242
+ const pending = path.join(root, ".reset-pending");
243
+ if (fs.existsSync(pending)) removeOwned(root, ".reset-pending");
244
+ const selected = names.filter((name) => name !== ".reset-pending");
245
+ if (!selected.length) return;
246
+ fs.mkdirSync(pending);
247
+ const moved = [];
248
+ try {
249
+ selected.sort(
250
+ (a, b) =>
251
+ Number(!a.startsWith("broader-vids.sqlite3")) -
252
+ Number(!b.startsWith("broader-vids.sqlite3")),
253
+ );
254
+ for (const name of selected) {
255
+ fs.renameSync(path.join(root, name), path.join(pending, name));
256
+ moved.push(name);
257
+ }
258
+ } catch (error) {
259
+ for (const name of moved.reverse()) {
260
+ try {
261
+ fs.renameSync(path.join(pending, name), path.join(root, name));
262
+ } catch {
263
+ /* Keep staged files for explicit recovery. */
264
+ }
265
+ }
266
+ if (fs.readdirSync(pending).length === 0) fs.rmdirSync(pending);
267
+ throw error;
268
+ }
269
+ removeOwned(root, ".reset-pending");
270
+ }
271
+
272
+ function apply(root, operation, packageRoot, confirmation) {
273
+ if (
274
+ !confirmation ||
275
+ !path.isAbsolute(confirmation) ||
276
+ path.relative(path.resolve(root), path.resolve(confirmation)) !== ""
277
+ )
278
+ throw Error(
279
+ "Supply --confirm-path with the exact absolute data directory shown by the preview.",
280
+ );
281
+ const initial = preview(root, operation, packageRoot);
282
+ if (initial.status === "absent")
283
+ return { ...initial, status: "absent", dry_run: false };
284
+ if (initial.blockers.length) throw Error(initial.blockers.join(" "));
285
+ root = initial.data_directory;
286
+ withSetup(root, () => {
287
+ validateRoot(root, packageRoot);
288
+ ensureMarker(root);
289
+ const names = fs.readdirSync(root);
290
+ if (operation === "clean" && names.some((name) => !managed(name)))
291
+ throw Error("The directory changed. Preview Clean again.");
292
+ if (operation === "reset") resetFiles(root, initial.remove);
293
+ else {
294
+ for (const name of initial.remove.filter(
295
+ (name) => name !== ".launcher" && name !== MARKER,
296
+ ))
297
+ removeOwned(root, name);
298
+ if (fs.existsSync(path.join(root, MARKER))) removeOwned(root, MARKER);
299
+ }
300
+ });
301
+ if (operation === "clean") {
302
+ try {
303
+ fs.rmdirSync(path.join(root, ".launcher"));
304
+ fs.rmdirSync(root);
305
+ } catch (error) {
306
+ return {
307
+ ...initial,
308
+ status: "partial",
309
+ dry_run: false,
310
+ error:
311
+ "Managed data was removed, but the directory is not empty or is in use. Preview again before another action.",
312
+ code: error.code,
313
+ };
314
+ }
315
+ }
316
+ return {
317
+ ...initial,
318
+ status: "completed",
319
+ dry_run: false,
320
+ next_action:
321
+ operation === "reset"
322
+ ? "Run the setup skill to choose interests or import preferences. Installed tools and Bilibili sign-in remain."
323
+ : "The plugin remains installed. Run the setup skill before using it again.",
324
+ };
325
+ }
326
+
327
+ module.exports = { preview, apply, ensureMarker, MARKER };
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const path = require("node:path");
5
+ const { randomUUID } = require("node:crypto");
6
+
7
+ function busy(message) {
8
+ const error = new Error(message);
9
+ error.code = "RUNTIME_BUSY";
10
+ return error;
11
+ }
12
+
13
+ function alive(pid) {
14
+ if (!Number.isSafeInteger(pid) || pid <= 0) return true;
15
+ try {
16
+ process.kill(pid, 0);
17
+ return true;
18
+ } catch (error) {
19
+ return error.code !== "ESRCH";
20
+ }
21
+ }
22
+
23
+ function read(file) {
24
+ try {
25
+ const record = JSON.parse(fs.readFileSync(file, "utf8"));
26
+ if (
27
+ !record ||
28
+ typeof record !== "object" ||
29
+ !Number.isSafeInteger(record.pid) ||
30
+ record.pid <= 0
31
+ )
32
+ throw busy(
33
+ "A runtime lock has an invalid owner. Check the operation before repairing the lock.",
34
+ );
35
+ return record;
36
+ } catch (error) {
37
+ if (error.code === "ENOENT") return undefined;
38
+ throw busy(
39
+ "A runtime lock is unreadable. Check its owner before repairing the lock.",
40
+ );
41
+ }
42
+ }
43
+
44
+ function gate(root, task) {
45
+ const folder = path.join(root, ".launcher");
46
+ fs.mkdirSync(folder, { recursive: true });
47
+ const file = path.join(folder, "gate.lock");
48
+ let descriptor;
49
+ const deadline = performance.now() + 1000;
50
+ while (descriptor === undefined) {
51
+ try {
52
+ descriptor = fs.openSync(file, "wx", 0o600);
53
+ } catch (error) {
54
+ if (error.code !== "EEXIST") throw error;
55
+ if (performance.now() >= deadline)
56
+ throw busy(
57
+ "Another launcher operation holds the runtime gate. Retry, or check the gate owner before repairing a stale lock.",
58
+ );
59
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10);
60
+ }
61
+ }
62
+ try {
63
+ fs.writeFileSync(descriptor, JSON.stringify({ pid: process.pid }));
64
+ return task(folder);
65
+ } finally {
66
+ fs.closeSync(descriptor);
67
+ fs.unlinkSync(file);
68
+ }
69
+ }
70
+
71
+ function activeSetup(folder) {
72
+ const file = path.join(folder, "setup.json");
73
+ const record = read(file);
74
+ if (!record) return false;
75
+ return true;
76
+ }
77
+
78
+ function release(file, token) {
79
+ if (read(file)?.token === token) fs.unlinkSync(file);
80
+ }
81
+
82
+ function withRuntimeUse(root, task) {
83
+ const token = randomUUID();
84
+ const file = gate(root, (folder) => {
85
+ if (activeSetup(folder))
86
+ throw busy(
87
+ "Runtime installation is active. Wait for setup to finish, then retry.",
88
+ );
89
+ const file = path.join(folder, `${token}.use.json`);
90
+ fs.writeFileSync(file, JSON.stringify({ pid: process.pid, token }), {
91
+ flag: "wx",
92
+ mode: 0o600,
93
+ });
94
+ return file;
95
+ });
96
+ try {
97
+ return task();
98
+ } finally {
99
+ release(file, token);
100
+ }
101
+ }
102
+
103
+ function withSetup(root, task) {
104
+ const token = randomUUID();
105
+ const file = gate(root, (folder) => {
106
+ if (activeSetup(folder))
107
+ throw busy(
108
+ "Another runtime installation is active. Retry after it finishes.",
109
+ );
110
+ for (const name of fs
111
+ .readdirSync(folder)
112
+ .filter((name) => name.endsWith(".use.json"))) {
113
+ const file = path.join(folder, name);
114
+ const record = read(file);
115
+ if (record)
116
+ throw busy(
117
+ "A launcher command holds this profile. Finish it before setup. If its parent exited, check for surviving children before repairing the lock.",
118
+ );
119
+ }
120
+ if (
121
+ fs.readdirSync(root).some((name) => /^[a-f0-9]{32}\.lock$/.test(name)) ||
122
+ fs.existsSync(path.join(root, "access", "bilibili", "browser.lock"))
123
+ ) {
124
+ throw busy(
125
+ "An engine or Bilibili browser operation holds this profile. Finish it before setup.",
126
+ );
127
+ }
128
+ const session = read(path.join(root, ".dashboard", "server.json"));
129
+ if (session && alive(session.pid))
130
+ throw busy(
131
+ "A dashboard server uses this profile. Stop it with dashboard stop before setup.",
132
+ );
133
+ const file = path.join(folder, "setup.json");
134
+ fs.writeFileSync(file, JSON.stringify({ pid: process.pid, token }), {
135
+ flag: "wx",
136
+ mode: 0o600,
137
+ });
138
+ return file;
139
+ });
140
+ try {
141
+ return task();
142
+ } finally {
143
+ release(file, token);
144
+ }
145
+ }
146
+
147
+ function setupInProgress(root) {
148
+ return !!read(path.join(root, ".launcher", "setup.json"));
149
+ }
150
+
151
+ module.exports = { withRuntimeUse, withSetup, setupInProgress };