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