@leonidstern/labit 0.0.0-stage → 0.6.1
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/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +12 -0
- package/CHANGELOG.md +282 -0
- package/LICENSE +21 -0
- package/README.md +46 -2
- package/api/lab-comments.mjs +439 -0
- package/dist/chunk-T4IF6ZJQ.mjs +106 -0
- package/dist/commands-BPVIWBCF.mjs +255 -0
- package/dist/devserver-KCLPMBNL.mjs +56 -0
- package/dist/local.mjs +1487 -0
- package/dist/mcp-BKU67CST.mjs +192 -0
- package/dist/proxy-FLFZV6EI.mjs +74 -0
- package/package.json +49 -4
- package/public/_lab/comments.js +46 -0
- package/public/_lab/favicon.svg +1 -0
- package/public/_lab/lab.css +129 -0
- package/skills/labit/SKILL.md +57 -0
- package/skills/labit/zoom.md +29 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import {
|
|
2
|
+
listenPages,
|
|
3
|
+
mySession,
|
|
4
|
+
runningLabs
|
|
5
|
+
} from "./chunk-T4IF6ZJQ.mjs";
|
|
6
|
+
|
|
7
|
+
// lib/commands.ts
|
|
8
|
+
import http from "node:http";
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
async function runCommand({ pos, opts, HERE, CMD, ME, die }) {
|
|
12
|
+
if (pos[0] === "preview") {
|
|
13
|
+
let entry = function() {
|
|
14
|
+
const args = [".", "--no-open", ...typeof opts.proxy === "string" ? ["--proxy", opts.proxy] : opts.proxy ? ["--proxy"] : []];
|
|
15
|
+
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 };
|
|
16
|
+
};
|
|
17
|
+
const root = path.resolve(pos[1] || ".");
|
|
18
|
+
if (!fs.existsSync(root) || !fs.statSync(root).isDirectory()) die(`Not a folder: ${root}`);
|
|
19
|
+
const file = path.join(root, ".claude/launch.json");
|
|
20
|
+
let config = { version: "0.0.1", configurations: [] };
|
|
21
|
+
if (fs.existsSync(file)) {
|
|
22
|
+
const jsonc = (t) => t.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (m, str) => str || "").replace(/,(\s*[}\]])/g, "$1");
|
|
23
|
+
try {
|
|
24
|
+
config = JSON.parse(jsonc(fs.readFileSync(file, "utf8")));
|
|
25
|
+
} catch {
|
|
26
|
+
die(`${file} isn't valid JSON, so it's left as it is. Add this to its "configurations":
|
|
27
|
+
${JSON.stringify(entry(), null, 2)}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
config.configurations = [...(config.configurations || []).filter((c) => c.name !== "lab"), entry()];
|
|
31
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
32
|
+
fs.writeFileSync(file, JSON.stringify(config, null, 2) + "\n");
|
|
33
|
+
console.log(`Added "lab" to ${file}.`);
|
|
34
|
+
console.log('Open this folder in the Claude desktop app and ask Claude to start the "lab" preview.');
|
|
35
|
+
console.log(`Then: run \`${CMD} wait\` in the background and answer the comments.`);
|
|
36
|
+
process.exit(0);
|
|
37
|
+
}
|
|
38
|
+
const forHere = (lab) => lab.root === HERE || lab.root.startsWith(HERE + path.sep) || HERE.startsWith(lab.root + path.sep);
|
|
39
|
+
const LINKS = path.join(HERE, ".lab", "links.json");
|
|
40
|
+
const savedLinks = () => {
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(fs.readFileSync(LINKS, "utf8"));
|
|
43
|
+
} catch {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const cloudOpen = async (url) => {
|
|
48
|
+
try {
|
|
49
|
+
const u = new URL(url);
|
|
50
|
+
const text = await (await fetch(`${u.origin}/_lab/agent${u.pathname}`, { signal: AbortSignal.timeout(3e3) })).text();
|
|
51
|
+
return Number(text.match(/^open: (\d+)/m)?.[1] || 0);
|
|
52
|
+
} catch {
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
if (pos[0] === "add" || pos[0] === "remove") {
|
|
57
|
+
const raw = pos[1] || die(`Give the page's address: ${CMD} ${pos[0]} https://<lab domain>/<page>`);
|
|
58
|
+
let url;
|
|
59
|
+
try {
|
|
60
|
+
url = new URL(raw);
|
|
61
|
+
} catch {
|
|
62
|
+
}
|
|
63
|
+
if (!url || !/^https?:$/.test(url.protocol)) die(`Not a page address: ${raw}`);
|
|
64
|
+
const key = url.origin + url.pathname.replace(/\/$/, "");
|
|
65
|
+
let list = savedLinks().filter((l) => l.url !== key);
|
|
66
|
+
let title = opts.name || "";
|
|
67
|
+
if (pos[0] === "add") {
|
|
68
|
+
if (!title) {
|
|
69
|
+
try {
|
|
70
|
+
const html = await (await fetch(key, { signal: AbortSignal.timeout(5e3) })).text();
|
|
71
|
+
title = html.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1].trim() || "";
|
|
72
|
+
} catch {
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
list.push({ url: key, title });
|
|
76
|
+
}
|
|
77
|
+
fs.mkdirSync(path.dirname(LINKS), { recursive: true });
|
|
78
|
+
fs.writeFileSync(LINKS, JSON.stringify(list, null, 2) + "\n");
|
|
79
|
+
console.log(pos[0] === "add" ? `Saved ${key}${title ? ` (${title})` : ""}. It shows in \`${CMD} pages\` and \`${CMD} list\`.` : `Removed ${key}.`);
|
|
80
|
+
process.exit(0);
|
|
81
|
+
}
|
|
82
|
+
if (pos[0] === "list") {
|
|
83
|
+
const labs = runningLabs();
|
|
84
|
+
if (!labs.length) console.log("No labs running.");
|
|
85
|
+
for (const lab of labs) {
|
|
86
|
+
let pages = "";
|
|
87
|
+
try {
|
|
88
|
+
const info = await (await fetch(`http://localhost:${lab.port}/_lab/pages`, { signal: AbortSignal.timeout(2e3) })).json();
|
|
89
|
+
const open = info.pages.reduce((n, p) => n + p.open, 0);
|
|
90
|
+
pages = ` \xB7 ${info.pages.length} page${info.pages.length === 1 ? "" : "s"}${open ? `, ${open} open comment${open === 1 ? "" : "s"}` : ""}${info.listening ? ` \xB7 ${info.listening} listening` : ""}`;
|
|
91
|
+
} catch {
|
|
92
|
+
}
|
|
93
|
+
console.log(`${lab.name} http://localhost:${lab.port} ${lab.root}${lab.proxy ? ` (on ${lab.proxy})` : ""}${pages}`);
|
|
94
|
+
}
|
|
95
|
+
const links = savedLinks();
|
|
96
|
+
if (links.length) console.log(`
|
|
97
|
+
Cloud pages saved for ${HERE}:
|
|
98
|
+
${links.map((l) => `${l.title || l.url} ${l.url}`).join("\n")}`);
|
|
99
|
+
process.exit(0);
|
|
100
|
+
}
|
|
101
|
+
if (pos[0] === "pages") {
|
|
102
|
+
const labs = [];
|
|
103
|
+
const here = runningLabs().filter((l) => opts.all || forHere(l));
|
|
104
|
+
const near = here.filter((l) => l.root === HERE || l.root.startsWith(HERE + path.sep));
|
|
105
|
+
for (const lab of opts.all || !near.length ? here : near) {
|
|
106
|
+
try {
|
|
107
|
+
const res = await fetch(`http://localhost:${lab.port}/_lab/pages`, { signal: AbortSignal.timeout(2e3) });
|
|
108
|
+
if (res.ok) labs.push(await res.json());
|
|
109
|
+
} catch {
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
labs.sort((a, b) => a.name.localeCompare(b.name));
|
|
113
|
+
const groups = labs.map((lab) => ({ name: lab.name, pages: lab.pages }));
|
|
114
|
+
const byHost = /* @__PURE__ */ new Map();
|
|
115
|
+
for (const link of savedLinks()) {
|
|
116
|
+
const host = new URL(link.url).host;
|
|
117
|
+
byHost.set(host, [...byHost.get(host) || [], { url: link.url, title: link.title, path: new URL(link.url).pathname, open: await cloudOpen(link.url) }]);
|
|
118
|
+
}
|
|
119
|
+
for (const [host, pages] of byHost) groups.push({ name: host, pages });
|
|
120
|
+
const lines = [];
|
|
121
|
+
for (const group of groups) {
|
|
122
|
+
if (groups.length > 1) lines.push(`**${group.name}**`, "");
|
|
123
|
+
for (const p of group.pages) {
|
|
124
|
+
const name = (p.title || p.file || p.path).replace(/[[\]]/g, "");
|
|
125
|
+
lines.push(`- [${name}](${p.url})${p.open ? ` \xB7 ${p.open} open` : ""}`);
|
|
126
|
+
}
|
|
127
|
+
lines.push("");
|
|
128
|
+
}
|
|
129
|
+
const out = path.join(HERE, ".lab", "lab-pages.md");
|
|
130
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
131
|
+
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");
|
|
132
|
+
console.log(out);
|
|
133
|
+
process.exit(0);
|
|
134
|
+
}
|
|
135
|
+
if (["wait", "reply", "resolve", "invite", "zoom"].includes(pos[0])) {
|
|
136
|
+
const [cmd, ref, ...words] = pos;
|
|
137
|
+
let dir = path.resolve(opts.dir || ".");
|
|
138
|
+
while (!fs.existsSync(path.join(dir, ".lab/server.json"))) {
|
|
139
|
+
if (path.dirname(dir) === dir) die(`No lab server for this folder. Start one: run ${CMD} in the project.`);
|
|
140
|
+
dir = path.dirname(dir);
|
|
141
|
+
}
|
|
142
|
+
const server = JSON.parse(fs.readFileSync(path.join(dir, ".lab/server.json"), "utf8"));
|
|
143
|
+
const session = mySession(opts.name, opts.model);
|
|
144
|
+
const call = (pathname, body) => new Promise((resolve) => {
|
|
145
|
+
const data2 = JSON.stringify(body);
|
|
146
|
+
const req = http.request(
|
|
147
|
+
{
|
|
148
|
+
host: "127.0.0.1",
|
|
149
|
+
port: server.port,
|
|
150
|
+
path: pathname,
|
|
151
|
+
method: "POST",
|
|
152
|
+
headers: {
|
|
153
|
+
host: `localhost:${server.port}`,
|
|
154
|
+
"content-type": "application/json",
|
|
155
|
+
"content-length": Buffer.byteLength(data2),
|
|
156
|
+
"x-lab-admin": server.token,
|
|
157
|
+
"x-lab-agent": "1",
|
|
158
|
+
"x-lab-session": encodeURIComponent(JSON.stringify(session))
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
(res) => {
|
|
162
|
+
let text = "";
|
|
163
|
+
res.on("data", (c) => text += c);
|
|
164
|
+
res.on("end", () => resolve({ status: res.statusCode || 0, text }));
|
|
165
|
+
}
|
|
166
|
+
);
|
|
167
|
+
req.on(
|
|
168
|
+
"error",
|
|
169
|
+
() => die(
|
|
170
|
+
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() + "."
|
|
171
|
+
)
|
|
172
|
+
);
|
|
173
|
+
req.end(data2);
|
|
174
|
+
});
|
|
175
|
+
if (cmd === "invite") {
|
|
176
|
+
const out = await call("/_lab/invite", { name: [ref, ...words].filter(Boolean).join(" ") });
|
|
177
|
+
if (out.status === 404) die("This lab is open to this computer only. Start it with --wifi to let a phone in.");
|
|
178
|
+
if (out.status >= 300) die(`Could not make a link: ${out.text}`);
|
|
179
|
+
const { links, name } = JSON.parse(out.text);
|
|
180
|
+
console.log(links.join("\n"));
|
|
181
|
+
if (name) console.log(`
|
|
182
|
+
The device that opens it is ${name} in this lab.`);
|
|
183
|
+
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.");
|
|
184
|
+
process.exit(0);
|
|
185
|
+
}
|
|
186
|
+
if (cmd === "wait") {
|
|
187
|
+
const out = await call("/_lab/wait", { timeout: Number(opts.timeout) || 1500, pages: listenPages(opts, true) });
|
|
188
|
+
if (out.status === 204) console.log("No new comments yet. Run wait again to keep listening.");
|
|
189
|
+
else if (out.status === 409) console.log("Another session is listening now; this one stopped.");
|
|
190
|
+
else if (out.status === 410) console.log("The page was closed, so this stopped listening. Wait again only when the person asks or opens the page.");
|
|
191
|
+
else console.log(out.text);
|
|
192
|
+
process.exit(out.status < 300 || out.status === 409 || out.status === 410 ? 0 : 1);
|
|
193
|
+
}
|
|
194
|
+
const data = {};
|
|
195
|
+
const store = path.join(dir, ".lab/comments");
|
|
196
|
+
if (!fs.existsSync(store)) Object.assign(data, JSON.parse(fs.readFileSync(path.join(dir, ".lab/comments.json"), "utf8")));
|
|
197
|
+
else
|
|
198
|
+
for (const name of fs.readdirSync(store).filter((n) => n.endsWith(".json"))) {
|
|
199
|
+
try {
|
|
200
|
+
const file = JSON.parse(fs.readFileSync(path.join(store, name), "utf8"));
|
|
201
|
+
if (typeof file.page === "string" && file.comments) data[file.page] = file.comments;
|
|
202
|
+
} catch {
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const want = opts.page ? "/" + opts.page.replace(/^\/+/, "") : null;
|
|
206
|
+
let hit = null;
|
|
207
|
+
for (const [page, list] of Object.entries(data)) if (list[ref]) hit = { page, id: list[ref].parent || ref };
|
|
208
|
+
if (!hit && /^\d+$/.test(ref || "")) {
|
|
209
|
+
const hits = Object.entries(data).filter(([page]) => !want || page === want).map(([page, list]) => {
|
|
210
|
+
const open = Object.values(list).filter((c) => !c.parent && !c.resolved).sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
211
|
+
return open[Number(ref) - 1] && { page, id: open[Number(ref) - 1].id };
|
|
212
|
+
}).filter(Boolean);
|
|
213
|
+
if (hits.length > 1) die(`Comment ${ref} is on several pages (${hits.map((h) => h.page).join(", ")}). Add --page <path>.`);
|
|
214
|
+
hit = hits[0];
|
|
215
|
+
}
|
|
216
|
+
if (!hit) die(`No open comment ${ref || ""}${want ? ` on ${want}` : ""}. See .lab/comments.md.`);
|
|
217
|
+
if (cmd === "zoom") {
|
|
218
|
+
const [file] = words;
|
|
219
|
+
if (!file) die(`Give the file with the block: ${CMD} zoom <id> <file>`);
|
|
220
|
+
if (!fs.existsSync(file)) die(`No file ${file}.`);
|
|
221
|
+
const out = await call("/_lab/api/zoom", { page: hit.page, id: hit.id, html: fs.readFileSync(file, "utf8") });
|
|
222
|
+
if (out.status >= 300) {
|
|
223
|
+
let why = out.text;
|
|
224
|
+
try {
|
|
225
|
+
why = JSON.parse(out.text).error || why;
|
|
226
|
+
} catch {
|
|
227
|
+
}
|
|
228
|
+
die(`Not placed: ${why || out.status}. Fix it and run it again.`);
|
|
229
|
+
}
|
|
230
|
+
const r = JSON.parse(out.text);
|
|
231
|
+
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}"`);
|
|
232
|
+
process.exit(0);
|
|
233
|
+
}
|
|
234
|
+
if (cmd === "reply") {
|
|
235
|
+
if (!words.length) die(`Reply text is missing: ${CMD} reply <id> "what you did"`);
|
|
236
|
+
const who = process.env.LAB_AGENT_NAME || session.agent;
|
|
237
|
+
const name = `${who}${session.name !== session.agent ? ` \xB7 ${session.name}` : ""}`;
|
|
238
|
+
const out = await call("/_lab/api/comments", { page: hit.page, parent: hit.id, name, text: words.join(" ") });
|
|
239
|
+
if (out.status >= 300) die(`Could not reply: ${out.text}`);
|
|
240
|
+
}
|
|
241
|
+
if (cmd === "resolve" || opts.resolve) {
|
|
242
|
+
const out = await call("/_lab/api/comments", { page: hit.page, action: "resolve", id: hit.id });
|
|
243
|
+
if (out.status >= 300) die(`Could not resolve: ${out.text}`);
|
|
244
|
+
}
|
|
245
|
+
console.log(`${cmd === "reply" ? opts.resolve ? "Replied and resolved" : "Replied" : "Resolved"} on ${hit.page}.`);
|
|
246
|
+
if (opts.wait) {
|
|
247
|
+
const out = await call("/_lab/wait", { timeout: Number(opts.timeout) || 1500, pages: listenPages(opts) });
|
|
248
|
+
console.log(out.status === 204 ? "No new comments yet. Run wait again to keep listening." : out.status === 409 ? "Another session is listening now; this one stopped." : out.status === 410 ? "The page was closed, so this stopped listening." : out.text);
|
|
249
|
+
}
|
|
250
|
+
process.exit(0);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
export {
|
|
254
|
+
runCommand
|
|
255
|
+
};
|
|
@@ -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
|
+
};
|