@bigmistqke/whiteboard 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +609 -2
- package/bin/whiteboard.ts +431 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +471 -0
- package/dist/client/runtime.js +391 -0
- package/dist/define.js +7 -0
- package/dist/plugins/callout/README.md +13 -0
- package/dist/plugins/callout/index.js +27 -0
- package/dist/plugins/callout/style.css +12 -0
- package/dist/plugins/chat/README.md +45 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +46 -0
- package/dist/plugins/chat/style.css +17 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +64 -0
- package/dist/plugins/code/index.js +77 -0
- package/dist/plugins/code/style.css +29 -0
- package/dist/plugins/comments/client.js +10 -0
- package/dist/plugins/comments/index.js +51 -0
- package/dist/plugins/comments/style.css +32 -0
- package/dist/plugins/details/README.md +17 -0
- package/dist/plugins/details/index.js +17 -0
- package/dist/plugins/details/style.css +3 -0
- package/dist/plugins/form/README.md +111 -0
- package/dist/plugins/form/index.js +102 -0
- package/dist/plugins/form/style.css +41 -0
- package/dist/plugins/gallery/README.md +30 -0
- package/dist/plugins/gallery/client.js +1 -0
- package/dist/plugins/gallery/index.js +74 -0
- package/dist/plugins/gallery/style.css +27 -0
- package/dist/plugins/grid/README.md +33 -0
- package/dist/plugins/grid/index.js +33 -0
- package/dist/plugins/grid/style.css +25 -0
- package/dist/plugins/index.js +26 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +34 -0
- package/dist/plugins/mermaid/style.css +16 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +17 -0
- package/dist/plugins/terminal/client.js +41 -0
- package/dist/plugins/terminal/host.js +332 -0
- package/dist/plugins/terminal/index.js +171 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +57 -0
- package/dist/plugins/terminal/style.css +357 -0
- package/dist/solid/index.js +35 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +274 -0
- package/dist/src/client/page/main.js +1004 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +162 -0
- package/dist/src/html.js +34 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +120 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +49 -0
- package/dist/src/pages/index.js +14 -0
- package/dist/src/pages/markdown.js +307 -0
- package/dist/src/pages/render.js +257 -0
- package/dist/src/plugins.js +238 -0
- package/dist/src/running.js +24 -0
- package/dist/src/serve.js +71 -0
- package/dist/src/server.js +700 -0
- package/dist/src/status.js +43 -0
- package/package.json +131 -4
- package/plugins/callout/README.md +13 -0
- package/plugins/callout/index.ts +25 -0
- package/plugins/callout/style.css +12 -0
- package/plugins/chat/README.md +45 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +81 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +17 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +58 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +448 -0
- package/plugins/comments/index.ts +52 -0
- package/plugins/comments/style.css +32 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/form/README.md +111 -0
- package/plugins/form/index.ts +104 -0
- package/plugins/form/style.css +41 -0
- package/plugins/gallery/README.md +30 -0
- package/plugins/gallery/client.tsx +64 -0
- package/plugins/gallery/index.ts +67 -0
- package/plugins/gallery/style.css +27 -0
- package/plugins/grid/README.md +33 -0
- package/plugins/grid/index.ts +36 -0
- package/plugins/grid/style.css +25 -0
- package/plugins/index.ts +19 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +39 -0
- package/plugins/mermaid/style.css +16 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +27 -0
- package/plugins/terminal/client.ts +317 -0
- package/plugins/terminal/host.ts +326 -0
- package/plugins/terminal/index.ts +138 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/server.ts +215 -0
- package/plugins/terminal/sizes.ts +60 -0
- package/plugins/terminal/style.css +71 -0
- package/skills/whiteboard/SKILL.md +292 -0
- package/solid/index.tsx +56 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +186 -0
- package/src/client/page/base.css +274 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/main.ts +249 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +109 -0
- package/src/client/runtime.ts +170 -0
- package/src/config.ts +96 -0
- package/src/define.ts +546 -0
- package/src/events.ts +194 -0
- package/src/html.ts +40 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +109 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +67 -0
- package/src/pages/index.ts +36 -0
- package/src/pages/markdown.ts +276 -0
- package/src/pages/render.ts +196 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +644 -0
- package/src/status.ts +69 -0
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { configFileIn, findProjectDir, pageId, project as project$1 } from "../src/config.js";
|
|
3
|
+
import { CLAIM, appendEvent, follow, forAgent, handOut, matches, pick, pickedUp, read, readEvents } from "../src/events.js";
|
|
4
|
+
import { BUILTIN_COMMANDS, createConfigLoader, pluginDir } from "../src/loader.js";
|
|
5
|
+
import { parseCommandArgs, pluginCommands, usage } from "../src/plugins.js";
|
|
6
|
+
import { projectToken } from "../src/auth.js";
|
|
7
|
+
import { packagePath } from "../src/package.js";
|
|
8
|
+
import { runningServer } from "../src/running.js";
|
|
9
|
+
import { statusLines, whose } from "../src/status.js";
|
|
10
|
+
import fs from "node:fs";
|
|
11
|
+
import os from "node:os";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { spawn } from "node:child_process";
|
|
14
|
+
import http from "node:http";
|
|
15
|
+
import { parseArgs } from "node:util";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
// bin/whiteboard.ts
|
|
18
|
+
const fail = (err) => {
|
|
19
|
+
console.error(`whiteboard: ${err?.message ?? err}`);
|
|
20
|
+
process.exit(1);
|
|
21
|
+
};
|
|
22
|
+
process.on("uncaughtException", fail);
|
|
23
|
+
process.on("unhandledRejection", fail);
|
|
24
|
+
const HELP = `whiteboard — markdown pages in the browser, what the reader does on them as events in an ndjson log
|
|
25
|
+
|
|
26
|
+
whiteboard init [name] make this folder a project: .whiteboard/pages/
|
|
27
|
+
whiteboard root print where pages go (this project's .whiteboard/pages)
|
|
28
|
+
whiteboard new <name> create a page under a name no other page has (name, name-2, …), print its path
|
|
29
|
+
whiteboard show <page|file> start this project's server if needed, print the page URL
|
|
30
|
+
whiteboard url <page|file> the same: print the page URL
|
|
31
|
+
whiteboard docs [plugin] how to write pages here: markdown, and each plugin's blocks — read before writing one;
|
|
32
|
+
with a plugin's name, only its docs
|
|
33
|
+
whiteboard events [filters] print recorded events as ndjson; --follow: then each new one as it comes
|
|
34
|
+
whiteboard wait [filters] block until the next matching event, print it, exit
|
|
35
|
+
whiteboard watch [filters] like wait, but never exits: print each matching event once
|
|
36
|
+
whiteboard append --page <p> --type <t> [--data <json>] add an event to a page's log (e.g. a reply)
|
|
37
|
+
whiteboard pick <event id> record an event as picked up, as wait does when it hands one out
|
|
38
|
+
whiteboard status [--page <p>] per page: questions without an answer, answers no agent picked up, who listens
|
|
39
|
+
whiteboard serve run this project's server in the foreground
|
|
40
|
+
whiteboard skill [dir] print where the agent skill is, or link it into a skills folder
|
|
41
|
+
|
|
42
|
+
filters: --page <page|file> (includes pages below it) --type <type>
|
|
43
|
+
wait: --timeout <seconds> (exit 2 on timeout) --count <n> (default 1)
|
|
44
|
+
events: --since <iso time or event id> --last <n>
|
|
45
|
+
|
|
46
|
+
Every command works on the project of the current folder: the nearest folder upward with a
|
|
47
|
+
.whiteboard/ folder. A project needs no config: .whiteboard/config.ts only says what differs from
|
|
48
|
+
the defaults (markdown with the built-in plugins). Each project runs its own server; its port is in .whiteboard/server.json.
|
|
49
|
+
A page appends events {id, ts, page, type, action, data} to .whiteboard/events.ndjson;
|
|
50
|
+
which types it sends is up to its plugins (\`whiteboard docs\` says). The project's plugins
|
|
51
|
+
can add commands of their own; \`whiteboard help\` in a project lists them.`;
|
|
52
|
+
const [first, ...rest] = process.argv.slice(2);
|
|
53
|
+
const cmd = first === "--help" || first === "-h" ? "help" : first;
|
|
54
|
+
const { values: opts, positionals } = cmd && !BUILTIN_COMMANDS.has(cmd) ? {
|
|
55
|
+
values: {},
|
|
56
|
+
positionals: []
|
|
57
|
+
} : parseArgs({
|
|
58
|
+
args: rest,
|
|
59
|
+
allowPositionals: true,
|
|
60
|
+
options: {
|
|
61
|
+
page: { type: "string" },
|
|
62
|
+
type: { type: "string" },
|
|
63
|
+
data: { type: "string" },
|
|
64
|
+
timeout: { type: "string" },
|
|
65
|
+
count: { type: "string" },
|
|
66
|
+
since: { type: "string" },
|
|
67
|
+
last: { type: "string" },
|
|
68
|
+
follow: { type: "boolean" },
|
|
69
|
+
help: {
|
|
70
|
+
type: "boolean",
|
|
71
|
+
short: "h"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
const project = project$1(findProjectDir());
|
|
76
|
+
/** The current project, or exit with how to make one. */
|
|
77
|
+
function needProject() {
|
|
78
|
+
if (project) return project;
|
|
79
|
+
console.error(`whiteboard: not inside a project (no .whiteboard/ folder in ${process.cwd()} or above it).
|
|
80
|
+
Run \`whiteboard init\` in the folder that should become one.`);
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
const filter = () => ({
|
|
84
|
+
page: opts.page ? pageId(needProject(), opts.page) : void 0,
|
|
85
|
+
type: opts.type
|
|
86
|
+
});
|
|
87
|
+
/** The running server of this project: its base URL, or null. */
|
|
88
|
+
const running = (proj) => runningServer(proj);
|
|
89
|
+
/** This project's server, started in the background when it is not running. @returns its base URL */
|
|
90
|
+
async function ensureServer() {
|
|
91
|
+
const proj = needProject();
|
|
92
|
+
const base = await running(proj);
|
|
93
|
+
if (base) return base;
|
|
94
|
+
const lock = path.join(proj.wb, "server.lock");
|
|
95
|
+
const claim = () => {
|
|
96
|
+
try {
|
|
97
|
+
fs.writeFileSync(lock, String(process.pid), { flag: "wx" });
|
|
98
|
+
return true;
|
|
99
|
+
} catch (err) {
|
|
100
|
+
if (err.code !== "EEXIST") throw err;
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
let mine = claim();
|
|
105
|
+
if (!mine) {
|
|
106
|
+
let stale = false;
|
|
107
|
+
try {
|
|
108
|
+
const owner = Number(fs.readFileSync(lock, "utf8"));
|
|
109
|
+
try {
|
|
110
|
+
process.kill(owner, 0);
|
|
111
|
+
} catch (err) {
|
|
112
|
+
stale = err.code === "ESRCH";
|
|
113
|
+
}
|
|
114
|
+
} catch {}
|
|
115
|
+
if (stale) {
|
|
116
|
+
try {
|
|
117
|
+
fs.unlinkSync(lock);
|
|
118
|
+
} catch {}
|
|
119
|
+
mine = claim();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (mine) {
|
|
123
|
+
const log = fs.openSync(proj.logFile, "a");
|
|
124
|
+
spawn(process.execPath, [fileURLToPath(import.meta.url), "serve"], {
|
|
125
|
+
cwd: proj.dir,
|
|
126
|
+
detached: true,
|
|
127
|
+
stdio: [
|
|
128
|
+
"ignore",
|
|
129
|
+
log,
|
|
130
|
+
log
|
|
131
|
+
],
|
|
132
|
+
env: process.env
|
|
133
|
+
}).unref();
|
|
134
|
+
}
|
|
135
|
+
for (let i = 0; i < 75; i++) {
|
|
136
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
137
|
+
const base = await running(proj);
|
|
138
|
+
if (base) return base;
|
|
139
|
+
}
|
|
140
|
+
throw new Error(`server did not start; see ${proj.logFile}`);
|
|
141
|
+
}
|
|
142
|
+
async function api(route) {
|
|
143
|
+
const res = await fetch(`${await ensureServer()}${route}`);
|
|
144
|
+
const body = await res.json();
|
|
145
|
+
if (!res.ok) throw new Error(body.error ?? res.statusText);
|
|
146
|
+
return body;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Tell the project's server that an agent is waiting, for as long as this command runs: pages show
|
|
150
|
+
* what is waited for. Holds one connection open, the listener; reconnects when the server restarts.
|
|
151
|
+
* Reading the log does not depend on it, so answers are never missed when the server is away.
|
|
152
|
+
*/
|
|
153
|
+
function listen(f, command) {
|
|
154
|
+
const connect = async () => {
|
|
155
|
+
let base;
|
|
156
|
+
try {
|
|
157
|
+
base = await ensureServer();
|
|
158
|
+
} catch {
|
|
159
|
+
return setTimeout(connect, 2e3).unref();
|
|
160
|
+
}
|
|
161
|
+
const q = new URLSearchParams({
|
|
162
|
+
command,
|
|
163
|
+
...f.page ? { page: f.page } : {},
|
|
164
|
+
...f.type ? { type: f.type } : {}
|
|
165
|
+
});
|
|
166
|
+
let again = false;
|
|
167
|
+
const retry = (ms) => {
|
|
168
|
+
if (!again) {
|
|
169
|
+
again = true;
|
|
170
|
+
setTimeout(connect, ms).unref();
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const req = http.get(`${base}/api/listen?${q}`, (res) => {
|
|
174
|
+
res.resume();
|
|
175
|
+
res.on("close", () => retry(1e3));
|
|
176
|
+
});
|
|
177
|
+
req.on("error", () => retry(2e3));
|
|
178
|
+
req.on("socket", (socket) => socket.unref());
|
|
179
|
+
};
|
|
180
|
+
connect();
|
|
181
|
+
}
|
|
182
|
+
function init(name) {
|
|
183
|
+
const dir = process.cwd();
|
|
184
|
+
const wb = path.join(dir, ".whiteboard");
|
|
185
|
+
if (fs.existsSync(wb)) {
|
|
186
|
+
console.log(`already a project: ${wb}`);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const home = dir === os.homedir();
|
|
190
|
+
const named = name && name !== path.basename(dir);
|
|
191
|
+
fs.mkdirSync(path.join(wb, "pages"), { recursive: true });
|
|
192
|
+
if (named || home) fs.writeFileSync(path.join(wb, "config.ts"), `import { defineConfig } from '@bigmistqke/whiteboard'
|
|
193
|
+
|
|
194
|
+
export default defineConfig({
|
|
195
|
+
${named ? ` name: '${name}',\n` : ""}${home ? " // only .whiteboard/ is served: this folder is not a repository\n files: false,\n" : ""}})
|
|
196
|
+
`);
|
|
197
|
+
fs.writeFileSync(path.join(wb, ".gitignore"), `# pages are working documents; remove this line to keep them in git
|
|
198
|
+
pages/
|
|
199
|
+
# events from the pages (and which ones an agent picked up), and the running server's port and output
|
|
200
|
+
events.ndjson
|
|
201
|
+
server.json
|
|
202
|
+
server.log
|
|
203
|
+
server.lock
|
|
204
|
+
# the secret for anything that runs code (a plugin like the terminal)
|
|
205
|
+
token
|
|
206
|
+
`);
|
|
207
|
+
console.log(`project: ${wb}
|
|
208
|
+
pages: ${path.join(wb, "pages")} (gitignored); \`whiteboard show <page>\` starts the server
|
|
209
|
+
config: ${configFileIn(wb) ?? `none: the defaults (markdown with the built-in plugins); add ${path.join(wb, "config.ts")} to change them`}`);
|
|
210
|
+
}
|
|
211
|
+
const relFile = (file) => path.relative(process.cwd(), file) || file;
|
|
212
|
+
/**
|
|
213
|
+
* The project's config, as the command line needs it: for its plugins (their commands, the
|
|
214
|
+
* session, which events reach an agent), so without the pages. Loaded once per command.
|
|
215
|
+
*/
|
|
216
|
+
let loaded = null;
|
|
217
|
+
const projectConfig = (proj) => loaded ??= createConfigLoader(proj, { pages: false }).config();
|
|
218
|
+
/**
|
|
219
|
+
* The agent session this command runs in, as the project's plugins know it (Claude Code's
|
|
220
|
+
* plugin reads it from the environment): the first one that names one. Null without any.
|
|
221
|
+
*/
|
|
222
|
+
async function currentSession(proj) {
|
|
223
|
+
try {
|
|
224
|
+
for (const e of (await projectConfig(proj)).plugins) {
|
|
225
|
+
const session = typeof e.session === "function" ? await e.session() : null;
|
|
226
|
+
if (session) return String(session);
|
|
227
|
+
}
|
|
228
|
+
} catch (err) {
|
|
229
|
+
console.error(`whiteboard: which session runs this: ${err.message}`);
|
|
230
|
+
}
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
async function status() {
|
|
234
|
+
const proj = needProject();
|
|
235
|
+
const [pages, session] = await Promise.all([api(`/api/status${opts.page ? `?page=${encodeURIComponent(pageId(proj, opts.page))}` : ""}`), currentSession(proj)]);
|
|
236
|
+
const busy = pages.filter((p) => p.unpicked.length || p.open.length);
|
|
237
|
+
if (!busy.length) {
|
|
238
|
+
console.log("nothing open: every question is answered, and every answer picked up");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
for (const p of busy) {
|
|
242
|
+
console.log(`${p.page} (${whose(p, session)})`);
|
|
243
|
+
for (const l of statusLines(p, relFile(p.file))) console.log(` ${l}`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
switch (cmd) {
|
|
247
|
+
case "serve": {
|
|
248
|
+
const proj = needProject();
|
|
249
|
+
const base = await running(proj);
|
|
250
|
+
if (base) {
|
|
251
|
+
console.error(`whiteboard: ${proj.name} is already served at ${base}`);
|
|
252
|
+
process.exit(1);
|
|
253
|
+
}
|
|
254
|
+
const { start } = await import("../src/serve.js");
|
|
255
|
+
await start(proj);
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
case "init":
|
|
259
|
+
init(positionals[0]);
|
|
260
|
+
break;
|
|
261
|
+
case "root": {
|
|
262
|
+
const dir = needProject().pagesDir;
|
|
263
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
264
|
+
console.log(dir);
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case "new": {
|
|
268
|
+
const name = (positionals[0] ?? "").replace(/\\/g, "/").replace(/\.md$/, "").replace(/^\/+|\/+$/g, "");
|
|
269
|
+
if (!name || name.split("/").some((part) => !part || part === "." || part === ".." || part === "-" || part === "_")) throw new Error("usage: whiteboard new <name> (a page name like plan or task/plan)");
|
|
270
|
+
const proj = needProject();
|
|
271
|
+
const answered = new Set(readEvents(proj.eventsFile).map((e) => e.page));
|
|
272
|
+
for (let n = 1;; n++) {
|
|
273
|
+
const id = n === 1 ? name : `${name}-${n}`;
|
|
274
|
+
if (answered.has(id)) continue;
|
|
275
|
+
const file = path.join(proj.pagesDir, `${id}.md`);
|
|
276
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
277
|
+
try {
|
|
278
|
+
fs.writeFileSync(file, "", { flag: "wx" });
|
|
279
|
+
const session = await currentSession(proj);
|
|
280
|
+
if (session) appendEvent(proj.eventsFile, {
|
|
281
|
+
page: id,
|
|
282
|
+
type: CLAIM,
|
|
283
|
+
data: { session }
|
|
284
|
+
}, "cli");
|
|
285
|
+
console.log(file);
|
|
286
|
+
break;
|
|
287
|
+
} catch (err) {
|
|
288
|
+
if (err.code !== "EEXIST") throw err;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
case "show":
|
|
294
|
+
case "url": {
|
|
295
|
+
if (cmd === "show" && !positionals[0]) throw new Error("usage: whiteboard show <page|file>");
|
|
296
|
+
const base = await ensureServer();
|
|
297
|
+
console.log(`${base}/${pageId(project, positionals[0] ?? "")}`);
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
case "docs": {
|
|
301
|
+
const info = await api("/api/docs");
|
|
302
|
+
for (const e of info.errors) console.error(`config error: ${e}`);
|
|
303
|
+
const only = positionals[0];
|
|
304
|
+
const names = (info.plugins ?? []).map((x) => x.name);
|
|
305
|
+
if (only && !names.includes(only)) throw new Error(`no plugin named "${only}" here: ${names.join(", ")}`);
|
|
306
|
+
const shown = (x) => !only || x.name === only;
|
|
307
|
+
if (!only) console.log(info.pages ? `${info.pages.trim()}\n` : "The config failed to load, so pages show as their raw text.\n");
|
|
308
|
+
for (const e of (info.plugins ?? []).filter(shown)) {
|
|
309
|
+
console.log(`<!-- plugin: ${e.name} -->\n# Plugin: ${e.name}\n`);
|
|
310
|
+
if (e.error) console.log(`It failed to load: ${e.error}\n`);
|
|
311
|
+
if (e.docs) console.log(`${e.docs.trim()}\n`);
|
|
312
|
+
if (e.events?.length) {
|
|
313
|
+
console.log("Events it writes (handed out: a `wait` or `watch` on the page gets them, and `status` counts them; the others only when asked for by type):\n");
|
|
314
|
+
for (const v of e.events) console.log(`- \`${v.type}\`${v.handOut ? " (handed out)" : ""}${v.description ? `: ${v.description}` : ""}`);
|
|
315
|
+
console.log();
|
|
316
|
+
}
|
|
317
|
+
for (const c of e.commands) console.log(`- \`whiteboard ${[c.name, c.usage].filter(Boolean).join(" ")}\`: ${c.description}`);
|
|
318
|
+
if (e.routes.length) console.log(`\nRoutes (\`auth\`: needs the project's token, header \`x-whiteboard-token\`):\n`);
|
|
319
|
+
for (const r of e.routes) {
|
|
320
|
+
const takes = [
|
|
321
|
+
r.body && `body ${JSON.stringify(r.body)}`,
|
|
322
|
+
r.query && `query ${JSON.stringify(r.query)}`,
|
|
323
|
+
r.stream && "server-sent events"
|
|
324
|
+
].filter(Boolean).join(", ");
|
|
325
|
+
console.log(`- \`${r.method} ${r.path}\`${r.auth ? " (auth)" : ""}: ${r.description}${takes ? ` (${takes})` : ""}`);
|
|
326
|
+
}
|
|
327
|
+
console.log();
|
|
328
|
+
}
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
case "events": {
|
|
332
|
+
const f = filter();
|
|
333
|
+
const log = needProject().eventsFile;
|
|
334
|
+
const { events, end } = read(log);
|
|
335
|
+
let list = events.filter((e) => matches(e, f));
|
|
336
|
+
if (opts.since) {
|
|
337
|
+
const i = list.findIndex((e) => e.id === opts.since);
|
|
338
|
+
const since = opts.since;
|
|
339
|
+
list = i >= 0 ? list.slice(i + 1) : list.filter((e) => e.ts > since);
|
|
340
|
+
}
|
|
341
|
+
if (opts.last) list = list.slice(-Number(opts.last));
|
|
342
|
+
for (const e of list) console.log(JSON.stringify(e));
|
|
343
|
+
if (opts.follow) follow(log, end, (e) => {
|
|
344
|
+
if (matches(e, f)) console.log(JSON.stringify(e));
|
|
345
|
+
});
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case "pick": {
|
|
349
|
+
const proj = needProject();
|
|
350
|
+
const id = positionals[0];
|
|
351
|
+
if (!id) throw new Error("usage: whiteboard pick <event id>");
|
|
352
|
+
const events = readEvents(proj.eventsFile);
|
|
353
|
+
const event = events.find((e) => e.id === id);
|
|
354
|
+
if (!event) throw new Error(`no event ${id} in ${proj.eventsFile}`);
|
|
355
|
+
if (pickedUp(events).has(id)) {
|
|
356
|
+
console.error(`whiteboard: ${id} was picked up already`);
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
console.log(JSON.stringify(pick(proj.eventsFile, event)));
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
case "wait":
|
|
363
|
+
case "watch": {
|
|
364
|
+
const proj = needProject();
|
|
365
|
+
const f = filter();
|
|
366
|
+
const { handedOut } = await projectConfig(proj);
|
|
367
|
+
handOut(proj.eventsFile, f, (e) => console.log(JSON.stringify(e)), {
|
|
368
|
+
count: cmd === "wait" ? Number(opts.count ?? 1) : Infinity,
|
|
369
|
+
forAgent: forAgent(handedOut)
|
|
370
|
+
}).then(() => process.exit(0));
|
|
371
|
+
listen(f, cmd);
|
|
372
|
+
if (opts.timeout && cmd === "wait") setTimeout(() => {
|
|
373
|
+
console.error("timeout");
|
|
374
|
+
process.exit(2);
|
|
375
|
+
}, Number(opts.timeout) * 1e3);
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case "append": {
|
|
379
|
+
const proj = needProject();
|
|
380
|
+
if (!opts.page || !opts.type) throw new Error("usage: whiteboard append --page <page|file> --type <type> [--data '<json>']");
|
|
381
|
+
let data = {};
|
|
382
|
+
if (opts.data) try {
|
|
383
|
+
data = JSON.parse(opts.data);
|
|
384
|
+
} catch {
|
|
385
|
+
throw new Error("--data must be JSON, e.g. '{\"text\":\"…\"}'");
|
|
386
|
+
}
|
|
387
|
+
console.log(JSON.stringify(appendEvent(proj.eventsFile, {
|
|
388
|
+
page: pageId(proj, opts.page),
|
|
389
|
+
type: opts.type,
|
|
390
|
+
data
|
|
391
|
+
}, "cli")));
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
case "status":
|
|
395
|
+
await status();
|
|
396
|
+
break;
|
|
397
|
+
case "skill": {
|
|
398
|
+
const source = packagePath("skills", "whiteboard");
|
|
399
|
+
if (!positionals[0]) {
|
|
400
|
+
console.log(source);
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
const target = path.join(path.resolve(positionals[0].replace(/^~(?=$|\/)/, os.homedir())), "whiteboard");
|
|
404
|
+
let existing = null;
|
|
405
|
+
try {
|
|
406
|
+
existing = fs.lstatSync(target);
|
|
407
|
+
} catch {}
|
|
408
|
+
if (existing) {
|
|
409
|
+
if (existing.isSymbolicLink() && path.resolve(path.dirname(target), fs.readlinkSync(target)) === source) {
|
|
410
|
+
console.log(`already linked: ${target} -> ${source}`);
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
console.error(`whiteboard: ${target} already exists; remove it first to link the skill there`);
|
|
414
|
+
process.exit(1);
|
|
415
|
+
}
|
|
416
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
417
|
+
fs.symlinkSync(source, target);
|
|
418
|
+
console.log(`linked: ${target} -> ${source}`);
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
default: {
|
|
422
|
+
const commands = project ? await projectCommands(project) : [];
|
|
423
|
+
const mine = commands.filter((c) => c.plugin === cmd);
|
|
424
|
+
const found = mine.find((c) => c.sub && c.sub === rest[0]) ?? mine.find((c) => !c.sub);
|
|
425
|
+
if (found && project) {
|
|
426
|
+
const args = found.sub ? rest.slice(1) : rest;
|
|
427
|
+
const { options, positionals, rest: after } = parseCommandArgs(found.command, args, found.name);
|
|
428
|
+
await found.command.run({
|
|
429
|
+
options,
|
|
430
|
+
positionals,
|
|
431
|
+
rest: after,
|
|
432
|
+
project: {
|
|
433
|
+
name: project.name,
|
|
434
|
+
dir: project.dir,
|
|
435
|
+
wb: project.wb
|
|
436
|
+
},
|
|
437
|
+
eventsFile: project.eventsFile,
|
|
438
|
+
dataDir: pluginDir(project.wb, found.plugin),
|
|
439
|
+
token: projectToken(project.wb),
|
|
440
|
+
server: ensureServer,
|
|
441
|
+
pagePath: (page) => `/${pageId(project, page ?? "")}`,
|
|
442
|
+
status: async (page) => api(`/api/status${page ? `?page=${encodeURIComponent(pageId(project, page))}` : ""}`),
|
|
443
|
+
handOut: async (filter, onEvent, options) => handOut(project.eventsFile, {
|
|
444
|
+
...filter,
|
|
445
|
+
page: filter.page ? pageId(project, filter.page) : null
|
|
446
|
+
}, onEvent, {
|
|
447
|
+
...options,
|
|
448
|
+
forAgent: forAgent((await projectConfig(project)).handedOut)
|
|
449
|
+
}),
|
|
450
|
+
authLink: async (to) => `${await ensureServer()}/_/auth?token=${encodeURIComponent(projectToken(project.wb))}&to=${encodeURIComponent(to)}`
|
|
451
|
+
});
|
|
452
|
+
break;
|
|
453
|
+
}
|
|
454
|
+
const asked = !cmd || cmd === "help" || opts.help;
|
|
455
|
+
const print = asked ? console.log : console.error;
|
|
456
|
+
print(HELP);
|
|
457
|
+
if (commands.length) {
|
|
458
|
+
print(`\nfrom this project's plugins:\n`);
|
|
459
|
+
for (const c of commands) print(` whiteboard ${[c.name, usage(c.command)].filter(Boolean).join(" ")}\n ${c.command.description}`);
|
|
460
|
+
}
|
|
461
|
+
if (mine.length) console.error(`\nwhiteboard: ${cmd} has no command ${JSON.stringify(rest[0] ?? "")}`);
|
|
462
|
+
if (!asked) process.exit(1);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/** The commands the project's plugins add: `whiteboard <plugin> [sub]`. */
|
|
466
|
+
async function projectCommands(proj) {
|
|
467
|
+
const { plugins, errors } = await projectConfig(proj);
|
|
468
|
+
for (const e of errors) console.error(`whiteboard: config error: ${e.message}`);
|
|
469
|
+
return plugins.flatMap(pluginCommands);
|
|
470
|
+
}
|
|
471
|
+
export {};
|