@leonidstern/labit 0.6.0 → 0.6.2
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/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +66 -0
- package/README.md +24 -125
- package/api/lab-comments.mjs +16 -2
- package/dist/chunk-DIR2O7SK.mjs +13 -0
- package/dist/chunk-LNYMIFDQ.mjs +49 -0
- package/dist/chunk-X4RYDWNI.mjs +52 -0
- package/dist/chunk-XZUZQFBJ.mjs +65 -0
- package/dist/commands-LAQNMF2X.mjs +240 -0
- package/dist/devserver-KCLPMBNL.mjs +56 -0
- package/dist/flags-PGKWZHJC.mjs +10 -0
- package/dist/local.mjs +1631 -0
- package/dist/mcp-4UPGXLZR.mjs +236 -0
- package/dist/proxy-FLFZV6EI.mjs +74 -0
- package/dist/settings-XLFKONNN.mjs +175 -0
- package/package.json +8 -6
- package/public/_lab/comments.js +30 -3592
- package/public/_lab/favicon.svg +1 -0
- package/public/_lab/lab.css +12 -0
- package/skills/labit/SKILL.md +7 -3
- package/skills/labit/zoom.md +29 -0
- package/lib/dev.mjs +0 -128
- package/lib/devserver.mjs +0 -58
- package/lib/mcp.mjs +0 -207
- package/lib/proxy.mjs +0 -73
- package/local.mjs +0 -1552
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FOREGROUND_CAP,
|
|
3
|
+
listenPages,
|
|
4
|
+
runningLabs,
|
|
5
|
+
waitSeconds
|
|
6
|
+
} from "./chunk-XZUZQFBJ.mjs";
|
|
7
|
+
import {
|
|
8
|
+
WAIT_SAID,
|
|
9
|
+
findLab,
|
|
10
|
+
labCall
|
|
11
|
+
} from "./chunk-LNYMIFDQ.mjs";
|
|
12
|
+
import {
|
|
13
|
+
mySession
|
|
14
|
+
} from "./chunk-X4RYDWNI.mjs";
|
|
15
|
+
|
|
16
|
+
// lib/commands.ts
|
|
17
|
+
import fs from "node:fs";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
async function runCommand({ pos, opts, HERE, CMD, ME, die }) {
|
|
20
|
+
if (pos[0] === "preview") {
|
|
21
|
+
let entry = function() {
|
|
22
|
+
const args = [".", "--no-open", ...typeof opts.proxy === "string" ? ["--proxy", opts.proxy] : opts.proxy ? ["--proxy"] : []];
|
|
23
|
+
return CMD.startsWith("npx") ? { name: "lab", runtimeExecutable: "npx", runtimeArgs: ["--yes", "@leonidstern/labit", ...args], port: 4400, autoPort: true } : { name: "lab", program: ME, args, port: 4400, autoPort: true };
|
|
24
|
+
};
|
|
25
|
+
const root = path.resolve(pos[1] || ".");
|
|
26
|
+
if (!fs.existsSync(root) || !fs.statSync(root).isDirectory()) die(`Not a folder: ${root}`);
|
|
27
|
+
const file = path.join(root, ".claude/launch.json");
|
|
28
|
+
let config = { version: "0.0.1", configurations: [] };
|
|
29
|
+
if (fs.existsSync(file)) {
|
|
30
|
+
const jsonc = (t) => t.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (m, str) => str || "").replace(/,(\s*[}\]])/g, "$1");
|
|
31
|
+
try {
|
|
32
|
+
config = JSON.parse(jsonc(fs.readFileSync(file, "utf8")));
|
|
33
|
+
} catch {
|
|
34
|
+
die(`${file} isn't valid JSON, so it's left as it is. Add this to its "configurations":
|
|
35
|
+
${JSON.stringify(entry(), null, 2)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
config.configurations = [...(config.configurations || []).filter((c) => c.name !== "lab"), entry()];
|
|
39
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
40
|
+
fs.writeFileSync(file, JSON.stringify(config, null, 2) + "\n");
|
|
41
|
+
console.log(`Added "lab" to ${file}.`);
|
|
42
|
+
console.log('Open this folder in the Claude desktop app and ask Claude to start the "lab" preview.');
|
|
43
|
+
console.log(`Then: run \`${CMD} wait\` in the background and answer the comments.`);
|
|
44
|
+
process.exit(0);
|
|
45
|
+
}
|
|
46
|
+
const forHere = (lab) => lab.root === HERE || lab.root.startsWith(HERE + path.sep) || HERE.startsWith(lab.root + path.sep);
|
|
47
|
+
const LINKS = path.join(HERE, ".lab", "links.json");
|
|
48
|
+
const savedLinks = () => {
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(fs.readFileSync(LINKS, "utf8"));
|
|
51
|
+
} catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const cloudOpen = async (url) => {
|
|
56
|
+
try {
|
|
57
|
+
const u = new URL(url);
|
|
58
|
+
const text = await (await fetch(`${u.origin}/_lab/agent${u.pathname}`, { signal: AbortSignal.timeout(3e3) })).text();
|
|
59
|
+
return Number(text.match(/^open: (\d+)/m)?.[1] || 0);
|
|
60
|
+
} catch {
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
if (pos[0] === "add" || pos[0] === "remove") {
|
|
65
|
+
const raw = pos[1] || die(`Give the page's address: ${CMD} ${pos[0]} https://<lab domain>/<page>`);
|
|
66
|
+
let url;
|
|
67
|
+
try {
|
|
68
|
+
url = new URL(raw);
|
|
69
|
+
} catch {
|
|
70
|
+
}
|
|
71
|
+
if (!url || !/^https?:$/.test(url.protocol)) die(`Not a page address: ${raw}`);
|
|
72
|
+
const key = url.origin + url.pathname.replace(/\/$/, "");
|
|
73
|
+
let list = savedLinks().filter((l) => l.url !== key);
|
|
74
|
+
let title = opts.name || "";
|
|
75
|
+
if (pos[0] === "add") {
|
|
76
|
+
if (!title) {
|
|
77
|
+
try {
|
|
78
|
+
const html = await (await fetch(key, { signal: AbortSignal.timeout(5e3) })).text();
|
|
79
|
+
title = html.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1].trim() || "";
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
list.push({ url: key, title });
|
|
84
|
+
}
|
|
85
|
+
fs.mkdirSync(path.dirname(LINKS), { recursive: true });
|
|
86
|
+
fs.writeFileSync(LINKS, JSON.stringify(list, null, 2) + "\n");
|
|
87
|
+
console.log(pos[0] === "add" ? `Saved ${key}${title ? ` (${title})` : ""}. It shows in \`${CMD} pages\` and \`${CMD} list\`.` : `Removed ${key}.`);
|
|
88
|
+
process.exit(0);
|
|
89
|
+
}
|
|
90
|
+
if (pos[0] === "list") {
|
|
91
|
+
const labs = runningLabs();
|
|
92
|
+
if (!labs.length) console.log("No labs running.");
|
|
93
|
+
for (const lab of labs) {
|
|
94
|
+
let pages = "";
|
|
95
|
+
try {
|
|
96
|
+
const info = await (await fetch(`http://localhost:${lab.port}/_lab/pages`, { signal: AbortSignal.timeout(2e3) })).json();
|
|
97
|
+
const open = info.pages.reduce((n, p) => n + p.open, 0);
|
|
98
|
+
pages = ` \xB7 ${info.pages.length} page${info.pages.length === 1 ? "" : "s"}${open ? `, ${open} open comment${open === 1 ? "" : "s"}` : ""}${info.listening ? ` \xB7 ${info.listening} listening` : ""}`;
|
|
99
|
+
} catch {
|
|
100
|
+
}
|
|
101
|
+
console.log(`${lab.name} http://localhost:${lab.port} ${lab.root}${lab.proxy ? ` (on ${lab.proxy})` : ""}${pages}`);
|
|
102
|
+
}
|
|
103
|
+
const links = savedLinks();
|
|
104
|
+
if (links.length) console.log(`
|
|
105
|
+
Cloud pages saved for ${HERE}:
|
|
106
|
+
${links.map((l) => `${l.title || l.url} ${l.url}`).join("\n")}`);
|
|
107
|
+
process.exit(0);
|
|
108
|
+
}
|
|
109
|
+
if (pos[0] === "pages") {
|
|
110
|
+
const labs = [];
|
|
111
|
+
const here = runningLabs().filter((l) => opts.all || forHere(l));
|
|
112
|
+
const near = here.filter((l) => l.root === HERE || l.root.startsWith(HERE + path.sep));
|
|
113
|
+
for (const lab of opts.all || !near.length ? here : near) {
|
|
114
|
+
try {
|
|
115
|
+
const res = await fetch(`http://localhost:${lab.port}/_lab/pages`, { signal: AbortSignal.timeout(2e3) });
|
|
116
|
+
if (res.ok) labs.push(await res.json());
|
|
117
|
+
} catch {
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
labs.sort((a, b) => a.name.localeCompare(b.name));
|
|
121
|
+
const groups = labs.map((lab) => ({ name: lab.name, pages: lab.pages }));
|
|
122
|
+
const byHost = /* @__PURE__ */ new Map();
|
|
123
|
+
for (const link of savedLinks()) {
|
|
124
|
+
const host = new URL(link.url).host;
|
|
125
|
+
byHost.set(host, [...byHost.get(host) || [], { url: link.url, title: link.title, path: new URL(link.url).pathname, open: await cloudOpen(link.url) }]);
|
|
126
|
+
}
|
|
127
|
+
for (const [host, pages] of byHost) groups.push({ name: host, pages });
|
|
128
|
+
const lines = [];
|
|
129
|
+
for (const group of groups) {
|
|
130
|
+
if (groups.length > 1) lines.push(`**${group.name}**`, "");
|
|
131
|
+
for (const p of group.pages) {
|
|
132
|
+
const name = (p.title || p.file || p.path).replace(/[[\]]/g, "");
|
|
133
|
+
lines.push(`- [${name}](${p.url})${p.open ? ` \xB7 ${p.open} open` : ""}`);
|
|
134
|
+
}
|
|
135
|
+
lines.push("");
|
|
136
|
+
}
|
|
137
|
+
const out = path.join(HERE, ".lab", "lab-pages.md");
|
|
138
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
139
|
+
fs.writeFileSync(out, (lines.join("\n").trim() || (opts.all ? "No labs running, and no saved cloud pages." : "No labs for this folder, and no saved cloud pages. `pages --all` lists every lab on this computer.")) + "\n");
|
|
140
|
+
console.log(out);
|
|
141
|
+
process.exit(0);
|
|
142
|
+
}
|
|
143
|
+
if (["wait", "reply", "resolve", "invite", "zoom"].includes(pos[0])) {
|
|
144
|
+
const [cmd, ref, ...words] = pos;
|
|
145
|
+
const lab = findLab(opts.dir || ".") || die(`No lab server for this folder. Start one: run ${CMD} in the project.`);
|
|
146
|
+
const { dir, server } = lab;
|
|
147
|
+
const session = mySession(opts.name, opts.model);
|
|
148
|
+
const call = (pathname, body) => labCall(server, pathname, body, session).catch(
|
|
149
|
+
() => die(
|
|
150
|
+
process.env.CODEX_SANDBOX_NETWORK_DISABLED ? `Codex's sandbox blocks this command from reaching the lab on this computer (port ${server.port}). Use labit's MCP server instead: codex mcp add labit -- npx @leonidstern/labit@latest mcp. Or let the sandbox reach it: -c sandbox_workspace_write.network_access=true` : `The lab server for ${dir} isn't running. Start it: ${CMD} ${dir === process.cwd() ? "" : dir}`.trim() + "."
|
|
151
|
+
)
|
|
152
|
+
);
|
|
153
|
+
if (cmd === "invite") {
|
|
154
|
+
const out = await call("/_lab/invite", { name: [ref, ...words].filter(Boolean).join(" "), page: opts.page || "" });
|
|
155
|
+
if (out.status === 404) die("This lab is open to this computer only. Start it with --wifi to let a phone in.");
|
|
156
|
+
if (out.status >= 300) die(`Could not make a link: ${out.text}`);
|
|
157
|
+
const { links, name } = JSON.parse(out.text);
|
|
158
|
+
console.log(links.join("\n"));
|
|
159
|
+
if (name) console.log(`
|
|
160
|
+
The device that opens it is ${name} in this lab.`);
|
|
161
|
+
console.log("\nOne use, ten minutes. Over wifi this is plain http: it keeps strangers out, it does not hide the page from someone listening to the network.");
|
|
162
|
+
process.exit(0);
|
|
163
|
+
}
|
|
164
|
+
if (cmd === "wait") {
|
|
165
|
+
const { seconds, capped } = waitSeconds(opts, process.stdout.isTTY === true);
|
|
166
|
+
const out = await call("/_lab/wait", { timeout: seconds, pages: listenPages(opts, true) });
|
|
167
|
+
console.log(WAIT_SAID[out.status] ?? out.text);
|
|
168
|
+
if (capped && out.status === 204)
|
|
169
|
+
console.log(
|
|
170
|
+
`
|
|
171
|
+
That wait was held to ${FOREGROUND_CAP} seconds. Run it in the background (your app's own way of doing that) and add --bg for the full wait: in the foreground this command holds your turn open, and the person cannot say anything to you until it ends.`
|
|
172
|
+
);
|
|
173
|
+
process.exit(out.status < 300 || out.status === 409 || out.status === 410 ? 0 : 1);
|
|
174
|
+
}
|
|
175
|
+
const data = {};
|
|
176
|
+
const store = path.join(dir, ".lab/comments");
|
|
177
|
+
if (!fs.existsSync(store)) Object.assign(data, JSON.parse(fs.readFileSync(path.join(dir, ".lab/comments.json"), "utf8")));
|
|
178
|
+
else
|
|
179
|
+
for (const name of fs.readdirSync(store).filter((n) => n.endsWith(".json"))) {
|
|
180
|
+
try {
|
|
181
|
+
const file = JSON.parse(fs.readFileSync(path.join(store, name), "utf8"));
|
|
182
|
+
if (typeof file.page === "string" && file.comments) data[file.page] = file.comments;
|
|
183
|
+
} catch {
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const want = opts.page ? "/" + opts.page.replace(/^\/+/, "") : null;
|
|
187
|
+
let hit = null;
|
|
188
|
+
for (const [page, list] of Object.entries(data)) if (list[ref]) hit = { page, id: list[ref].parent || ref, to: list[ref].parent ? ref : "" };
|
|
189
|
+
if (!hit && /^\d+$/.test(ref || "")) {
|
|
190
|
+
const hits = Object.entries(data).filter(([page]) => !want || page === want).map(([page, list]) => {
|
|
191
|
+
const open = Object.values(list).filter((c) => !c.parent && !c.resolved).sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
192
|
+
return open[Number(ref) - 1] && { page, id: open[Number(ref) - 1].id };
|
|
193
|
+
}).filter(Boolean);
|
|
194
|
+
if (hits.length > 1) die(`Comment ${ref} is on several pages (${hits.map((h) => h.page).join(", ")}). Add --page <path>.`);
|
|
195
|
+
hit = hits[0];
|
|
196
|
+
}
|
|
197
|
+
if (!hit) die(`No open comment ${ref || ""}${want ? ` on ${want}` : ""}. See .lab/comments.md.`);
|
|
198
|
+
if (cmd === "zoom") {
|
|
199
|
+
const [file] = words;
|
|
200
|
+
if (!file) die(`Give the file with the block: ${CMD} zoom <id> <file>`);
|
|
201
|
+
if (!fs.existsSync(file)) die(`No file ${file}.`);
|
|
202
|
+
const out = await call("/_lab/api/zoom", { page: hit.page, id: hit.id, html: fs.readFileSync(file, "utf8") });
|
|
203
|
+
if (out.status >= 300) {
|
|
204
|
+
let why = out.text;
|
|
205
|
+
try {
|
|
206
|
+
why = JSON.parse(out.text).error || why;
|
|
207
|
+
} catch {
|
|
208
|
+
}
|
|
209
|
+
die(`Not placed: ${why || out.status}. Fix it and run it again.`);
|
|
210
|
+
}
|
|
211
|
+
const r = JSON.parse(out.text);
|
|
212
|
+
console.log(`Placed #${r.id} in ${r.file}. Now answer on the pin in one line, naming #${r.id} (it links to it): ${CMD} reply ${hit.id} "\u2026 #${r.id}"`);
|
|
213
|
+
process.exit(0);
|
|
214
|
+
}
|
|
215
|
+
if (cmd === "reply") {
|
|
216
|
+
if (!words.length) die(`Reply text is missing: ${CMD} reply <id> "what you did"`);
|
|
217
|
+
const who = process.env.LAB_AGENT_NAME || session.agent;
|
|
218
|
+
const name = `${who}${session.name !== session.agent ? ` \xB7 ${session.name}` : ""}`;
|
|
219
|
+
const out = await call("/_lab/api/comments", { page: hit.page, parent: hit.id, ...hit.to ? { to: hit.to } : {}, name, text: words.join(" "), ...typeof opts["suggest-rule"] === "string" ? { suggest: opts["suggest-rule"] } : {} });
|
|
220
|
+
if (out.status >= 300) die(`Could not reply: ${out.text}`);
|
|
221
|
+
}
|
|
222
|
+
if (cmd === "resolve" || opts.resolve) {
|
|
223
|
+
const out = await call("/_lab/api/comments", { page: hit.page, action: "resolve", id: hit.id });
|
|
224
|
+
if (out.status >= 300) die(`Could not resolve: ${out.text}`);
|
|
225
|
+
}
|
|
226
|
+
console.log(`${cmd === "reply" ? opts.resolve ? "Replied and resolved" : "Replied" : "Resolved"} on ${hit.page}.`);
|
|
227
|
+
if (opts.wait) {
|
|
228
|
+
const { seconds, capped } = waitSeconds(opts, process.stdout.isTTY === true);
|
|
229
|
+
const out = await call("/_lab/wait", { timeout: seconds, pages: listenPages(opts) });
|
|
230
|
+
console.log(WAIT_SAID[out.status] ?? out.text);
|
|
231
|
+
if (capped && out.status === 204)
|
|
232
|
+
console.log(`
|
|
233
|
+
That wait was held to ${FOREGROUND_CAP} seconds: run the command in the background and add --bg for the full wait.`);
|
|
234
|
+
}
|
|
235
|
+
process.exit(0);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
export {
|
|
239
|
+
runCommand
|
|
240
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// lib/devserver.ts
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
function devServers() {
|
|
6
|
+
const lsof = (args) => {
|
|
7
|
+
try {
|
|
8
|
+
return execFileSync("lsof", args, { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 5e3 });
|
|
9
|
+
} catch (e) {
|
|
10
|
+
const err = e;
|
|
11
|
+
if (err.code === "ENOENT" || !err.stdout) throw e;
|
|
12
|
+
return err.stdout;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
let out;
|
|
16
|
+
try {
|
|
17
|
+
out = lsof(["-nP", "-iTCP", "-sTCP:LISTEN", "-Fpn"]);
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
const ports = /* @__PURE__ */ new Map();
|
|
22
|
+
let pid = 0;
|
|
23
|
+
for (const line of out.split("\n")) {
|
|
24
|
+
if (line[0] === "p") pid = Number(line.slice(1));
|
|
25
|
+
else if (line[0] === "n" && pid !== process.pid) {
|
|
26
|
+
const port = Number(line.match(/:(\d+)$/)?.[1]);
|
|
27
|
+
if (port) ports.set(pid, [.../* @__PURE__ */ new Set([...ports.get(pid) || [], port])]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (!ports.size) return [];
|
|
31
|
+
const cwds = /* @__PURE__ */ new Map();
|
|
32
|
+
try {
|
|
33
|
+
for (const line of lsof(["-a", "-p", [...ports.keys()].join(","), "-d", "cwd", "-Fpn"]).split("\n")) {
|
|
34
|
+
if (line[0] === "p") pid = Number(line.slice(1));
|
|
35
|
+
else if (line[0] === "n") cwds.set(pid, line.slice(1));
|
|
36
|
+
}
|
|
37
|
+
} catch {
|
|
38
|
+
}
|
|
39
|
+
return [...ports].flatMap(([p, list]) => list.map((port) => ({ port, dir: cwds.get(p) || "" })));
|
|
40
|
+
}
|
|
41
|
+
function findDevServer(root, labPort) {
|
|
42
|
+
const inRoot = (dir) => dir === root || dir.startsWith(root + path.sep);
|
|
43
|
+
const all = devServers();
|
|
44
|
+
if (!all) return { port: null, others: [] };
|
|
45
|
+
const mine = all.filter((s) => inRoot(s.dir) && s.port !== labPort).sort((a, b) => a.port - b.port);
|
|
46
|
+
const others = all.filter((s) => !inRoot(s.dir) && s.port >= 1024 && s.port < 1e4 && s.dir.startsWith(os.homedir() + path.sep)).sort((a, b) => a.port - b.port);
|
|
47
|
+
return { port: mine[0]?.port ?? null, others };
|
|
48
|
+
}
|
|
49
|
+
var tilde = (dir) => dir.replace(os.homedir(), "~");
|
|
50
|
+
var othersNote = (others) => others.length ? `
|
|
51
|
+
Other projects' servers (not this one): ${others.slice(0, 4).map((s) => `:${s.port} ${tilde(s.dir)}`).join(", ")}` : "";
|
|
52
|
+
export {
|
|
53
|
+
findDevServer,
|
|
54
|
+
othersNote,
|
|
55
|
+
tilde
|
|
56
|
+
};
|