@bigmistqke/whiteboard 0.3.1 → 0.4.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/README.md +67 -17
- package/bin/whiteboard.ts +268 -71
- package/dist/client/runtime.js +88 -41
- package/dist/define.js +21 -2
- package/dist/plugins/callout/index.js +3 -1
- package/dist/plugins/callout/public/style.css +30 -0
- package/dist/plugins/chat/index.js +7 -4
- package/dist/plugins/chat/{client.js → public/client.js} +1 -1
- package/dist/plugins/chat/public/style.css +98 -0
- package/dist/plugins/code/index.js +3 -1
- package/dist/plugins/code/public/style.css +68 -0
- package/dist/plugins/comments/index.js +4 -5
- package/dist/plugins/comments/public/client.js +19 -0
- package/dist/plugins/comments/public/style.css +185 -0
- package/dist/plugins/details/index.js +7 -2
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/form/index.js +37 -8
- package/dist/plugins/form/public/style.css +126 -0
- package/dist/plugins/gallery/index.js +4 -3
- package/dist/plugins/gallery/public/client.js +1 -0
- package/dist/plugins/gallery/public/style.css +90 -0
- package/dist/plugins/grid/index.js +9 -3
- package/dist/plugins/grid/public/style.css +71 -0
- package/dist/plugins/iframe/index.js +13 -14
- package/dist/plugins/iframe/public/style.css +13 -0
- package/dist/plugins/index.js +3 -1
- package/dist/plugins/mermaid/index.js +3 -1
- package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/dist/plugins/terminal/index.js +10 -9
- package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
- package/dist/plugins/toc/index.js +4 -3
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +93 -0
- package/dist/plugins/window/README.md +29 -0
- package/dist/plugins/window/index.js +64 -0
- package/dist/solid/index.js +14 -11
- package/dist/src/client/page/base.css +1078 -199
- package/dist/src/client/page/main.js +174 -33
- package/dist/src/loader.js +7 -2
- package/dist/src/package.js +3 -6
- package/dist/src/pages/blocks.js +8 -13
- package/dist/src/pages/markdown.js +1 -1
- package/dist/src/pages/render.js +74 -15
- package/dist/src/pages/window.js +30 -0
- package/dist/src/public.js +24 -0
- package/dist/src/server.js +36 -14
- package/package.json +3 -1
- package/plugins/callout/index.ts +3 -1
- package/plugins/callout/public/style.css +30 -0
- package/plugins/chat/autosize.ts +43 -9
- package/plugins/chat/client.tsx +22 -5
- package/plugins/chat/index.ts +22 -8
- package/plugins/chat/public/style.css +98 -0
- package/plugins/claude-code/index.ts +49 -14
- package/plugins/code/diff.ts +28 -8
- package/plugins/code/index.ts +30 -20
- package/plugins/code/public/style.css +68 -0
- package/plugins/comments/client.ts +139 -71
- package/plugins/comments/index.ts +9 -7
- package/plugins/comments/public/style.css +185 -0
- package/plugins/details/index.ts +7 -2
- package/plugins/details/public/style.css +7 -0
- package/plugins/forge/index.ts +16 -3
- package/plugins/form/index.ts +75 -27
- package/plugins/form/public/style.css +126 -0
- package/plugins/gallery/client.tsx +9 -3
- package/plugins/gallery/index.ts +31 -9
- package/plugins/gallery/public/style.css +90 -0
- package/plugins/grid/index.ts +19 -5
- package/plugins/grid/public/style.css +71 -0
- package/plugins/iframe/index.ts +20 -23
- package/plugins/iframe/public/style.css +13 -0
- package/plugins/index.ts +16 -1
- package/plugins/mermaid/index.ts +3 -1
- package/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/plugins/terminal/client.ts +105 -48
- package/plugins/terminal/host.ts +131 -29
- package/plugins/terminal/index.ts +84 -25
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +64 -13
- package/plugins/terminal/sizes.ts +13 -3
- package/plugins/toc/client.ts +45 -26
- package/plugins/toc/index.ts +9 -6
- package/plugins/toc/public/style.css +93 -0
- package/plugins/window/README.md +29 -0
- package/plugins/window/index.ts +85 -0
- package/skills/whiteboard/SKILL.md +3 -1
- package/solid/index.tsx +24 -9
- package/src/auth.ts +25 -7
- package/src/client/forms.ts +74 -28
- package/src/client/markdown.ts +15 -4
- package/src/client/page/base.css +1078 -199
- package/src/client/page/elsewhere.ts +32 -15
- package/src/client/page/fresh.ts +91 -23
- package/src/client/page/hold.ts +11 -2
- package/src/client/page/live.ts +10 -6
- package/src/client/page/main.ts +106 -36
- package/src/client/page/orphans.ts +25 -10
- package/src/client/page/sort.ts +14 -4
- package/src/client/page/sticky.ts +11 -3
- package/src/client/page/theme.ts +11 -7
- package/src/client/page/waiting.ts +141 -34
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +17 -10
- package/src/client/runtime.ts +130 -35
- package/src/client/scroll.ts +14 -4
- package/src/client/toast.ts +19 -5
- package/src/config.ts +20 -5
- package/src/define.ts +65 -25
- package/src/events.ts +111 -24
- package/src/html.ts +6 -2
- package/src/icons.ts +4 -2
- package/src/load.ts +10 -2
- package/src/loader.ts +75 -11
- package/src/package.ts +9 -8
- package/src/pages/blocks.ts +20 -14
- package/src/pages/markdown.ts +113 -34
- package/src/pages/render.ts +199 -39
- package/src/pages/window.ts +71 -0
- package/src/plugins.ts +90 -24
- package/src/public.ts +39 -0
- package/src/running.ts +8 -2
- package/src/serve.ts +29 -7
- package/src/server.ts +348 -107
- package/src/status.ts +15 -9
- package/dist/plugins/callout/style.css +0 -12
- package/dist/plugins/chat/style.css +0 -24
- package/dist/plugins/code/style.css +0 -29
- package/dist/plugins/comments/client.js +0 -10
- package/dist/plugins/comments/style.css +0 -40
- package/dist/plugins/details/style.css +0 -3
- package/dist/plugins/form/style.css +0 -44
- package/dist/plugins/gallery/client.js +0 -1
- package/dist/plugins/gallery/style.css +0 -27
- package/dist/plugins/grid/style.css +0 -25
- package/dist/plugins/iframe/client.js +0 -1
- package/dist/plugins/iframe/style.css +0 -29
- package/dist/plugins/toc/client.js +0 -1
- package/dist/plugins/toc/style.css +0 -19
- package/plugins/callout/style.css +0 -12
- package/plugins/chat/style.css +0 -24
- package/plugins/code/style.css +0 -29
- package/plugins/comments/style.css +0 -40
- package/plugins/details/style.css +0 -3
- package/plugins/form/style.css +0 -44
- package/plugins/gallery/style.css +0 -27
- package/plugins/grid/style.css +0 -25
- package/plugins/iframe/client.ts +0 -46
- package/plugins/iframe/style.css +0 -29
- package/plugins/terminal/style.css +0 -71
- package/plugins/toc/style.css +0 -19
- /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
package/bin/whiteboard.ts
CHANGED
|
@@ -7,7 +7,21 @@ import http from 'node:http'
|
|
|
7
7
|
import { parseArgs } from 'node:util'
|
|
8
8
|
import { fileURLToPath } from 'node:url'
|
|
9
9
|
import { pageId, findProjectDir, project as projectAt, configFileIn, type Project } from '../src/config.ts'
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
appendEvent,
|
|
12
|
+
read,
|
|
13
|
+
readEvents,
|
|
14
|
+
follow,
|
|
15
|
+
matches,
|
|
16
|
+
pickedUp,
|
|
17
|
+
pick,
|
|
18
|
+
prune,
|
|
19
|
+
editEvent,
|
|
20
|
+
handOut,
|
|
21
|
+
CLAIM,
|
|
22
|
+
type EventFilter,
|
|
23
|
+
forAgent,
|
|
24
|
+
} from '../src/events.ts'
|
|
11
25
|
import { BUILTIN_COMMANDS, createConfigLoader, pluginDir } from '../src/loader.ts'
|
|
12
26
|
import { pluginCommands, parseCommandArgs, usage } from '../src/plugins.ts'
|
|
13
27
|
import { projectToken } from '../src/auth.ts'
|
|
@@ -64,14 +78,25 @@ const [first, ...rest] = process.argv.slice(2)
|
|
|
64
78
|
// `whiteboard --help` and `-h`, where a command goes, ask for the help as `whiteboard help` does
|
|
65
79
|
const cmd = first === '--help' || first === '-h' ? 'help' : first
|
|
66
80
|
// a plugin's command parses its own arguments
|
|
67
|
-
const { values: opts, positionals } =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
81
|
+
const { values: opts, positionals } =
|
|
82
|
+
cmd && !BUILTIN_COMMANDS.has(cmd)
|
|
83
|
+
? { values: {}, positionals: [] }
|
|
84
|
+
: parseArgs({
|
|
85
|
+
args: rest,
|
|
86
|
+
allowPositionals: true,
|
|
87
|
+
options: {
|
|
88
|
+
page: { type: 'string' },
|
|
89
|
+
type: { type: 'string' },
|
|
90
|
+
data: { type: 'string' },
|
|
91
|
+
timeout: { type: 'string' },
|
|
92
|
+
count: { type: 'string' },
|
|
93
|
+
since: { type: 'string' },
|
|
94
|
+
last: { type: 'string' },
|
|
95
|
+
follow: { type: 'boolean' },
|
|
96
|
+
id: { type: 'string' },
|
|
97
|
+
help: { type: 'boolean', short: 'h' },
|
|
98
|
+
},
|
|
99
|
+
})
|
|
75
100
|
|
|
76
101
|
const project = projectAt(findProjectDir())
|
|
77
102
|
|
|
@@ -98,7 +123,10 @@ async function ensureServer(): Promise<string> {
|
|
|
98
123
|
// is left over, and taken over. (Should two servers still start, the server lets one survive.)
|
|
99
124
|
const lock = path.join(proj.wb, 'server.lock')
|
|
100
125
|
const claim = () => {
|
|
101
|
-
try {
|
|
126
|
+
try {
|
|
127
|
+
fs.writeFileSync(lock, String(process.pid), { flag: 'wx' })
|
|
128
|
+
return true
|
|
129
|
+
} catch (err) {
|
|
102
130
|
if ((err as NodeJS.ErrnoException).code !== 'EEXIST') throw err
|
|
103
131
|
return false
|
|
104
132
|
}
|
|
@@ -110,16 +138,31 @@ async function ensureServer(): Promise<string> {
|
|
|
110
138
|
let stale = false
|
|
111
139
|
try {
|
|
112
140
|
const owner = Number(fs.readFileSync(lock, 'utf8'))
|
|
113
|
-
try {
|
|
114
|
-
|
|
141
|
+
try {
|
|
142
|
+
process.kill(owner, 0)
|
|
143
|
+
} catch (err) {
|
|
144
|
+
stale = (err as NodeJS.ErrnoException).code === 'ESRCH'
|
|
145
|
+
}
|
|
146
|
+
} catch {
|
|
147
|
+
/* removed meanwhile */
|
|
148
|
+
}
|
|
115
149
|
if (stale) {
|
|
116
|
-
try {
|
|
150
|
+
try {
|
|
151
|
+
fs.unlinkSync(lock)
|
|
152
|
+
} catch {
|
|
153
|
+
/* another command removed it first */
|
|
154
|
+
}
|
|
117
155
|
mine = claim()
|
|
118
156
|
}
|
|
119
157
|
}
|
|
120
158
|
if (mine) {
|
|
121
159
|
const log = fs.openSync(proj.logFile, 'a')
|
|
122
|
-
const child = spawn(process.execPath, [fileURLToPath(import.meta.url), 'serve'], {
|
|
160
|
+
const child = spawn(process.execPath, [fileURLToPath(import.meta.url), 'serve'], {
|
|
161
|
+
cwd: proj.dir,
|
|
162
|
+
detached: true,
|
|
163
|
+
stdio: ['ignore', log, log],
|
|
164
|
+
env: process.env,
|
|
165
|
+
})
|
|
123
166
|
child.unref()
|
|
124
167
|
}
|
|
125
168
|
for (let i = 0; i < 75; i++) {
|
|
@@ -150,13 +193,29 @@ function listen(f: EventFilter, command: 'wait' | 'watch', fallback = false): Pr
|
|
|
150
193
|
return new Promise(registered => {
|
|
151
194
|
const connect = async () => {
|
|
152
195
|
let base
|
|
153
|
-
try {
|
|
154
|
-
|
|
196
|
+
try {
|
|
197
|
+
base = await ensureServer()
|
|
198
|
+
} catch {
|
|
199
|
+
return setTimeout(connect, 2000).unref()
|
|
200
|
+
}
|
|
201
|
+
const q = new URLSearchParams({
|
|
202
|
+
command,
|
|
203
|
+
...(f.page ? { page: f.page } : {}),
|
|
204
|
+
...(f.type ? { type: f.type } : {}),
|
|
205
|
+
...(fallback ? { fallback: '' } : {}),
|
|
206
|
+
})
|
|
155
207
|
let again = false
|
|
156
|
-
const retry = (ms: number) => {
|
|
208
|
+
const retry = (ms: number) => {
|
|
209
|
+
if (!again) {
|
|
210
|
+
again = true
|
|
211
|
+
setTimeout(connect, ms).unref()
|
|
212
|
+
}
|
|
213
|
+
}
|
|
157
214
|
const req = http.get(`${base}/api/listen?${q}`, res => {
|
|
158
215
|
if (res.statusCode === 409) {
|
|
159
|
-
console.error(
|
|
216
|
+
console.error(
|
|
217
|
+
'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.',
|
|
218
|
+
)
|
|
160
219
|
process.exit(1)
|
|
161
220
|
}
|
|
162
221
|
res.resume()
|
|
@@ -178,20 +237,36 @@ function listen(f: EventFilter, command: 'wait' | 'watch', fallback = false): Pr
|
|
|
178
237
|
async function otherListeners(changed: () => void): Promise<() => Listener[]> {
|
|
179
238
|
let list: Listener[] = []
|
|
180
239
|
const load = async () => {
|
|
181
|
-
try {
|
|
240
|
+
try {
|
|
241
|
+
list = (await (await fetch(`${await ensureServer()}/api/listeners`)).json()) as Listener[]
|
|
242
|
+
} catch {
|
|
243
|
+
/* the server restarts */
|
|
244
|
+
}
|
|
182
245
|
changed()
|
|
183
246
|
}
|
|
184
247
|
const follow = async () => {
|
|
185
248
|
let again = false
|
|
186
|
-
const retry = () => {
|
|
249
|
+
const retry = () => {
|
|
250
|
+
if (!again) {
|
|
251
|
+
again = true
|
|
252
|
+
setTimeout(follow, 1000).unref()
|
|
253
|
+
}
|
|
254
|
+
}
|
|
187
255
|
let base
|
|
188
|
-
try {
|
|
256
|
+
try {
|
|
257
|
+
base = await ensureServer()
|
|
258
|
+
} catch {
|
|
259
|
+
return retry()
|
|
260
|
+
}
|
|
189
261
|
const req = http.get(`${base}/api/watch?dir`, res => {
|
|
190
262
|
let buf = ''
|
|
191
263
|
res.setEncoding('utf8')
|
|
192
264
|
res.on('data', (chunk: string) => {
|
|
193
265
|
buf += chunk
|
|
194
|
-
if (buf.includes('event: listeners')) {
|
|
266
|
+
if (buf.includes('event: listeners')) {
|
|
267
|
+
buf = ''
|
|
268
|
+
load()
|
|
269
|
+
}
|
|
195
270
|
if (buf.length > 4096) buf = buf.slice(-64)
|
|
196
271
|
})
|
|
197
272
|
res.on('close', retry)
|
|
@@ -219,12 +294,18 @@ function init(name: string | undefined) {
|
|
|
219
294
|
const named = name && name !== path.basename(dir)
|
|
220
295
|
fs.mkdirSync(path.join(wb, 'pages'), { recursive: true })
|
|
221
296
|
// a config only for what differs from the defaults
|
|
222
|
-
if (named || home)
|
|
297
|
+
if (named || home)
|
|
298
|
+
fs.writeFileSync(
|
|
299
|
+
path.join(wb, 'config.ts'),
|
|
300
|
+
`import { defineConfig } from '@bigmistqke/whiteboard'
|
|
223
301
|
|
|
224
302
|
export default defineConfig({
|
|
225
303
|
${named ? ` name: '${name}',\n` : ''}${home ? ' // only .whiteboard/ is served: this folder is not a repository\n files: false,\n' : ''}})
|
|
226
|
-
|
|
227
|
-
|
|
304
|
+
`,
|
|
305
|
+
)
|
|
306
|
+
fs.writeFileSync(
|
|
307
|
+
path.join(wb, '.gitignore'),
|
|
308
|
+
`# pages are working documents; remove this line to keep them in git
|
|
228
309
|
pages/
|
|
229
310
|
# events from the pages (and which ones an agent picked up), and the running server's port and output
|
|
230
311
|
events.ndjson
|
|
@@ -233,7 +314,8 @@ server.log
|
|
|
233
314
|
server.lock
|
|
234
315
|
# the secret for anything that runs code (a plugin like the terminal)
|
|
235
316
|
token
|
|
236
|
-
|
|
317
|
+
`,
|
|
318
|
+
)
|
|
237
319
|
console.log(`project: ${wb}
|
|
238
320
|
pages: ${path.join(wb, 'pages')} (gitignored); \`whiteboard show <page>\` starts the server
|
|
239
321
|
config: ${configFileIn(wb) ?? `none: the defaults (markdown with the built-in plugins); add ${path.join(wb, 'config.ts')} to change them`}`)
|
|
@@ -260,11 +342,12 @@ async function currentSession(proj: Project): Promise<string | null> {
|
|
|
260
342
|
const session = typeof e.session === 'function' ? await e.session() : null
|
|
261
343
|
if (session) return String(session)
|
|
262
344
|
}
|
|
263
|
-
} catch (err) {
|
|
345
|
+
} catch (err) {
|
|
346
|
+
console.error(`whiteboard: which session runs this: ${(err as Error).message}`)
|
|
347
|
+
}
|
|
264
348
|
return null
|
|
265
349
|
}
|
|
266
350
|
|
|
267
|
-
|
|
268
351
|
async function status() {
|
|
269
352
|
const proj = needProject()
|
|
270
353
|
const [pages, session]: [PageStatus[], string | null] = await Promise.all([
|
|
@@ -272,8 +355,12 @@ async function status() {
|
|
|
272
355
|
currentSession(proj),
|
|
273
356
|
])
|
|
274
357
|
const busy = pages.filter(p => p.unpicked.length || p.open.length)
|
|
275
|
-
if (pages.some(p => p.listeners.some(l => l.fallback)))
|
|
276
|
-
|
|
358
|
+
if (pages.some(p => p.listeners.some(l => l.fallback)))
|
|
359
|
+
console.log('a fallback listens: it takes what no other wait or watch would\n')
|
|
360
|
+
if (!busy.length) {
|
|
361
|
+
console.log('nothing open: every question is answered, and every answer picked up')
|
|
362
|
+
return
|
|
363
|
+
}
|
|
277
364
|
for (const p of busy) {
|
|
278
365
|
console.log(`${p.page} (${whose(p, session)})`)
|
|
279
366
|
for (const l of statusLines(p, relFile(p.file))) console.log(` ${l}`)
|
|
@@ -284,7 +371,10 @@ switch (cmd) {
|
|
|
284
371
|
case 'serve': {
|
|
285
372
|
const proj = needProject()
|
|
286
373
|
const base = await running(proj)
|
|
287
|
-
if (base) {
|
|
374
|
+
if (base) {
|
|
375
|
+
console.error(`whiteboard: ${proj.name} is already served at ${base}`)
|
|
376
|
+
process.exit(1)
|
|
377
|
+
}
|
|
288
378
|
const { start } = await import('../src/serve.ts')
|
|
289
379
|
await start(proj)
|
|
290
380
|
break
|
|
@@ -302,7 +392,10 @@ switch (cmd) {
|
|
|
302
392
|
case 'new': {
|
|
303
393
|
// claim a page: the exclusive create fails when the file exists, so two agents asking at once
|
|
304
394
|
// never get the same page; the second gets name-2
|
|
305
|
-
const name = (positionals[0] ?? '')
|
|
395
|
+
const name = (positionals[0] ?? '')
|
|
396
|
+
.replace(/\\/g, '/')
|
|
397
|
+
.replace(/\.md$/, '')
|
|
398
|
+
.replace(/^\/+|\/+$/g, '')
|
|
306
399
|
if (!name || name.split('/').some(part => !part || part === '.' || part === '..' || part === '-' || part === '_'))
|
|
307
400
|
throw new Error('usage: whiteboard new <name> (a page name like plan or task/plan)')
|
|
308
401
|
const proj = needProject()
|
|
@@ -341,21 +434,36 @@ switch (cmd) {
|
|
|
341
434
|
if (!target) throw new Error('usage: whiteboard close <page|file>')
|
|
342
435
|
const proj = needProject()
|
|
343
436
|
const id = pageId(proj, target)
|
|
344
|
-
if (id.startsWith('-/'))
|
|
437
|
+
if (id.startsWith('-/'))
|
|
438
|
+
throw new Error(
|
|
439
|
+
`${target} is a file of the repository, not a page: close is for the pages in ${relFile(proj.pagesDir)}`,
|
|
440
|
+
)
|
|
345
441
|
const file = path.join(proj.pagesDir, `${id}.md`)
|
|
346
442
|
const exists = fs.existsSync(file)
|
|
347
443
|
// an agent still waiting on it would wait on nothing
|
|
348
444
|
const base = await running(proj)
|
|
349
445
|
if (base) {
|
|
350
446
|
try {
|
|
351
|
-
const listening = (
|
|
352
|
-
|
|
353
|
-
|
|
447
|
+
const listening = (
|
|
448
|
+
(await (await fetch(`${base}/api/listeners?page=${encodeURIComponent(id)}`)).json()) as Listener[]
|
|
449
|
+
).filter(l => !l.fallback && l.page === id)
|
|
450
|
+
if (listening.length)
|
|
451
|
+
console.error(
|
|
452
|
+
`whiteboard: ${listening.length} wait or watch still listens on ${id}; stop it, it will hear nothing more`,
|
|
453
|
+
)
|
|
454
|
+
} catch {
|
|
455
|
+
/* the server is going: nothing to say */
|
|
456
|
+
}
|
|
354
457
|
}
|
|
355
458
|
if (exists) fs.rmSync(file)
|
|
356
459
|
const removed = prune(proj.eventsFile, id)
|
|
357
|
-
if (!exists && !removed) {
|
|
358
|
-
|
|
460
|
+
if (!exists && !removed) {
|
|
461
|
+
console.error(`whiteboard: there is no page ${id}, nor events of it`)
|
|
462
|
+
process.exit(1)
|
|
463
|
+
}
|
|
464
|
+
console.log(
|
|
465
|
+
`closed ${id}: ${exists ? 'removed its file' : 'it had no file'}, and ${removed} event${removed === 1 ? '' : 's'} from the log`,
|
|
466
|
+
)
|
|
359
467
|
break
|
|
360
468
|
}
|
|
361
469
|
case 'prune': {
|
|
@@ -365,18 +473,29 @@ switch (cmd) {
|
|
|
365
473
|
if (!target) throw new Error('usage: whiteboard prune <page|file> [--type <type>] [--id <event id>]')
|
|
366
474
|
const proj = needProject()
|
|
367
475
|
const id = pageId(proj, target)
|
|
368
|
-
const removed = prune(
|
|
476
|
+
const removed = prune(
|
|
477
|
+
proj.eventsFile,
|
|
478
|
+
id,
|
|
479
|
+
e => (!opts.type || e.type === opts.type) && (!opts.id || e.id === opts.id),
|
|
480
|
+
)
|
|
369
481
|
const which = [opts.type && `of type ${opts.type}`, opts.id && `with id ${opts.id}`].filter(Boolean).join(', ')
|
|
370
|
-
console.log(
|
|
482
|
+
console.log(
|
|
483
|
+
`pruned ${removed} event${removed === 1 ? '' : 's'} of ${id}${which ? ` (${which})` : ''} from the log; the page stays`,
|
|
484
|
+
)
|
|
371
485
|
break
|
|
372
486
|
}
|
|
373
487
|
case 'edit': {
|
|
374
488
|
// an agent's own event, changed in place: --data is merged into its data
|
|
375
489
|
const eventId = positionals[0]
|
|
376
|
-
if (!eventId || !opts.data)
|
|
490
|
+
if (!eventId || !opts.data)
|
|
491
|
+
throw new Error("usage: whiteboard edit <event id> --data <json> (merged into the event's data)")
|
|
377
492
|
const proj = needProject()
|
|
378
493
|
let data: Record<string, unknown>
|
|
379
|
-
try {
|
|
494
|
+
try {
|
|
495
|
+
data = JSON.parse(opts.data)
|
|
496
|
+
} catch {
|
|
497
|
+
throw new Error('--data must be a JSON object')
|
|
498
|
+
}
|
|
380
499
|
if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error('--data must be a JSON object')
|
|
381
500
|
console.log(JSON.stringify(editEvent(proj.eventsFile, eventId, data)))
|
|
382
501
|
break
|
|
@@ -390,15 +509,31 @@ switch (cmd) {
|
|
|
390
509
|
const id = pageId(proj, target)
|
|
391
510
|
const base = await ensureServer()
|
|
392
511
|
// what the page shows, to say now when nothing on it matches
|
|
393
|
-
const rendered =
|
|
394
|
-
|
|
395
|
-
|
|
512
|
+
const rendered = (await (
|
|
513
|
+
await fetch(`${base}/api/render?page=${encodeURIComponent(id)}`)
|
|
514
|
+
)
|
|
515
|
+
.json()
|
|
516
|
+
.catch(() => null)) as { html?: string; data?: unknown } | null
|
|
517
|
+
const text = (rendered?.html ?? '')
|
|
518
|
+
.replace(/<[^>]+>/g, ' ')
|
|
519
|
+
.replace(/&[a-z#0-9]+;/gi, ' ')
|
|
520
|
+
.replace(/\s+/g, ' ')
|
|
521
|
+
.toLowerCase()
|
|
522
|
+
const named = new RegExp(`<form[^>]*\\bname="${to.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"`).test(
|
|
523
|
+
rendered?.html ?? '',
|
|
524
|
+
)
|
|
396
525
|
if (rendered && !named && !text.includes(to.replace(/\s+/g, ' ').toLowerCase())) {
|
|
397
|
-
console.error(
|
|
526
|
+
console.error(
|
|
527
|
+
`whiteboard: nothing on ${id} matches ${JSON.stringify(to)} (a heading's text, a form's name, or text on the page)`,
|
|
528
|
+
)
|
|
398
529
|
process.exit(1)
|
|
399
530
|
}
|
|
400
|
-
const res = await fetch(`${base}/api/scroll`, {
|
|
401
|
-
|
|
531
|
+
const res = await fetch(`${base}/api/scroll`, {
|
|
532
|
+
method: 'POST',
|
|
533
|
+
headers: { 'content-type': 'application/json' },
|
|
534
|
+
body: JSON.stringify({ page: id, to }),
|
|
535
|
+
})
|
|
536
|
+
const { tabs } = (await res.json()) as { tabs: number }
|
|
402
537
|
if (!tabs) {
|
|
403
538
|
console.error(`whiteboard: no browser has ${id} open; \`whiteboard show ${target}\` prints its URL`)
|
|
404
539
|
process.exit(1)
|
|
@@ -415,21 +550,37 @@ switch (cmd) {
|
|
|
415
550
|
const names = (info.plugins ?? []).map((x: { name: string }) => x.name)
|
|
416
551
|
if (only && !names.includes(only)) throw new Error(`no plugin named "${only}" here: ${names.join(', ')}`)
|
|
417
552
|
const shown = (x: { name: string }) => !only || x.name === only
|
|
418
|
-
if (!only)
|
|
553
|
+
if (!only)
|
|
554
|
+
console.log(
|
|
555
|
+
info.pages ? `${info.pages.trim()}\n` : 'The config failed to load, so pages show as their raw text.\n',
|
|
556
|
+
)
|
|
419
557
|
for (const e of (info.plugins ?? []).filter(shown)) {
|
|
420
558
|
console.log(`<!-- plugin: ${e.name} -->\n# Plugin: ${e.name}\n`)
|
|
421
559
|
if (e.error) console.log(`It failed to load: ${e.error}\n`)
|
|
422
560
|
if (e.docs) console.log(`${e.docs.trim()}\n`)
|
|
423
561
|
if (e.events?.length) {
|
|
424
|
-
console.log(
|
|
425
|
-
|
|
562
|
+
console.log(
|
|
563
|
+
'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',
|
|
564
|
+
)
|
|
565
|
+
for (const v of e.events)
|
|
566
|
+
console.log(`- \`${v.type}\`${v.handOut ? ' (handed out)' : ''}${v.description ? `: ${v.description}` : ''}`)
|
|
426
567
|
console.log()
|
|
427
568
|
}
|
|
428
|
-
for (const c of e.commands)
|
|
429
|
-
|
|
569
|
+
for (const c of e.commands)
|
|
570
|
+
console.log(`- \`whiteboard ${[c.name, c.usage].filter(Boolean).join(' ')}\`: ${c.description}`)
|
|
571
|
+
if (e.routes.length)
|
|
572
|
+
console.log(`\nRoutes (\`auth\`: needs the project's token, header \`x-whiteboard-token\`):\n`)
|
|
430
573
|
for (const r of e.routes) {
|
|
431
|
-
const takes = [
|
|
432
|
-
|
|
574
|
+
const takes = [
|
|
575
|
+
r.body && `body ${JSON.stringify(r.body)}`,
|
|
576
|
+
r.query && `query ${JSON.stringify(r.query)}`,
|
|
577
|
+
r.stream && 'server-sent events',
|
|
578
|
+
]
|
|
579
|
+
.filter(Boolean)
|
|
580
|
+
.join(', ')
|
|
581
|
+
console.log(
|
|
582
|
+
`- \`${r.method} ${r.path}\`${r.auth ? ' (auth)' : ''}: ${r.description}${takes ? ` (${takes})` : ''}`,
|
|
583
|
+
)
|
|
433
584
|
}
|
|
434
585
|
console.log()
|
|
435
586
|
}
|
|
@@ -448,7 +599,10 @@ switch (cmd) {
|
|
|
448
599
|
}
|
|
449
600
|
if (opts.last) list = list.slice(-Number(opts.last))
|
|
450
601
|
for (const e of list) console.log(JSON.stringify(e))
|
|
451
|
-
if (opts.follow)
|
|
602
|
+
if (opts.follow)
|
|
603
|
+
follow(log, end, e => {
|
|
604
|
+
if (matches(e, f)) console.log(JSON.stringify(e))
|
|
605
|
+
})
|
|
452
606
|
break
|
|
453
607
|
}
|
|
454
608
|
case 'pick': {
|
|
@@ -459,7 +613,10 @@ switch (cmd) {
|
|
|
459
613
|
const events = readEvents(proj.eventsFile)
|
|
460
614
|
const event = events.find(e => e.id === id)
|
|
461
615
|
if (!event) throw new Error(`no event ${id} in ${proj.eventsFile}`)
|
|
462
|
-
if (pickedUp(events).has(id)) {
|
|
616
|
+
if (pickedUp(events).has(id)) {
|
|
617
|
+
console.error(`whiteboard: ${id} was picked up already`)
|
|
618
|
+
break
|
|
619
|
+
}
|
|
463
620
|
console.log(JSON.stringify(pick(proj.eventsFile, event)))
|
|
464
621
|
break
|
|
465
622
|
}
|
|
@@ -480,38 +637,67 @@ switch (cmd) {
|
|
|
480
637
|
// it holds each event briefly before deciding: by then that wait took it, or the copy knows.
|
|
481
638
|
const held = new Set<string>()
|
|
482
639
|
const skip = (e: WbEvent) => {
|
|
483
|
-
if (!held.has(e.id)) {
|
|
640
|
+
if (!held.has(e.id)) {
|
|
641
|
+
held.add(e.id)
|
|
642
|
+
setTimeout(() => recheck(), 500).unref()
|
|
643
|
+
return true
|
|
644
|
+
}
|
|
484
645
|
return takenByOther(others!(), e)
|
|
485
646
|
}
|
|
486
647
|
handOut(proj.eventsFile, f, e => console.log(JSON.stringify(e)), {
|
|
487
648
|
count: cmd === 'wait' ? Number(opts.count ?? 1) : Infinity,
|
|
488
649
|
forAgent: forAgent(handedOut),
|
|
489
|
-
...(others && {
|
|
650
|
+
...(others && {
|
|
651
|
+
skip,
|
|
652
|
+
recheck: again => {
|
|
653
|
+
recheck = again
|
|
654
|
+
},
|
|
655
|
+
}),
|
|
490
656
|
}).then(async () => {
|
|
491
657
|
// a wait on a page ends with an answer, and the agent acts on it; what is still unanswered
|
|
492
658
|
// there needs the next wait, which is easily forgotten: say so, right with the answer
|
|
493
659
|
if (cmd === 'wait' && !fallback) {
|
|
494
660
|
try {
|
|
495
|
-
const pages = await api(`/api/status?page=${encodeURIComponent(f.page!)}`) as PageStatus[]
|
|
496
|
-
for (const p of pages)
|
|
497
|
-
|
|
661
|
+
const pages = (await api(`/api/status?page=${encodeURIComponent(f.page!)}`)) as PageStatus[]
|
|
662
|
+
for (const p of pages)
|
|
663
|
+
if (p.open.length)
|
|
664
|
+
console.error(
|
|
665
|
+
`whiteboard: still unanswered on ${p.page}: ${p.open.join(', ')} → run \`whiteboard wait --page ${relFile(p.file)}\` again, in the background, once you've acted on this`,
|
|
666
|
+
)
|
|
667
|
+
} catch {
|
|
668
|
+
/* no status: the answer is what matters */
|
|
669
|
+
}
|
|
498
670
|
}
|
|
499
671
|
process.exit(0)
|
|
500
672
|
})
|
|
501
673
|
if (!fallback) listen(f, cmd)
|
|
502
|
-
if (opts.timeout && cmd === 'wait')
|
|
674
|
+
if (opts.timeout && cmd === 'wait')
|
|
675
|
+
setTimeout(
|
|
676
|
+
() => {
|
|
677
|
+
console.error('timeout')
|
|
678
|
+
process.exit(2)
|
|
679
|
+
},
|
|
680
|
+
Number(opts.timeout) * 1000,
|
|
681
|
+
)
|
|
503
682
|
break
|
|
504
683
|
}
|
|
505
684
|
case 'append': {
|
|
506
685
|
// the agent's side of the log: anything a page's plugins understand, such as a reply to a
|
|
507
686
|
// comment. Waits skip what is appended here, so an agent does not hear its own events.
|
|
508
687
|
const proj = needProject()
|
|
509
|
-
if (!opts.page || !opts.type)
|
|
688
|
+
if (!opts.page || !opts.type)
|
|
689
|
+
throw new Error("usage: whiteboard append --page <page|file> --type <type> [--data '<json>']")
|
|
510
690
|
let data = {}
|
|
511
691
|
if (opts.data) {
|
|
512
|
-
try {
|
|
692
|
+
try {
|
|
693
|
+
data = JSON.parse(opts.data)
|
|
694
|
+
} catch {
|
|
695
|
+
throw new Error('--data must be JSON, e.g. \'{"text":"…"}\'')
|
|
696
|
+
}
|
|
513
697
|
}
|
|
514
|
-
console.log(
|
|
698
|
+
console.log(
|
|
699
|
+
JSON.stringify(appendEvent(proj.eventsFile, { page: pageId(proj, opts.page), type: opts.type, data }, 'cli')),
|
|
700
|
+
)
|
|
515
701
|
break
|
|
516
702
|
}
|
|
517
703
|
case 'status':
|
|
@@ -520,10 +706,15 @@ switch (cmd) {
|
|
|
520
706
|
case 'skill': {
|
|
521
707
|
// the agent skill ships in this package; link it into whatever folder an agent reads skills from
|
|
522
708
|
const source = packagePath('skills', 'whiteboard')
|
|
523
|
-
if (!positionals[0]) {
|
|
709
|
+
if (!positionals[0]) {
|
|
710
|
+
console.log(source)
|
|
711
|
+
break
|
|
712
|
+
}
|
|
524
713
|
const target = path.join(path.resolve(positionals[0].replace(/^~(?=$|\/)/, os.homedir())), 'whiteboard')
|
|
525
714
|
let existing = null
|
|
526
|
-
try {
|
|
715
|
+
try {
|
|
716
|
+
existing = fs.lstatSync(target)
|
|
717
|
+
} catch {}
|
|
527
718
|
if (existing) {
|
|
528
719
|
if (existing.isSymbolicLink() && path.resolve(path.dirname(target), fs.readlinkSync(target)) === source) {
|
|
529
720
|
console.log(`already linked: ${target} -> ${source}`)
|
|
@@ -556,8 +747,13 @@ switch (cmd) {
|
|
|
556
747
|
server: ensureServer,
|
|
557
748
|
pagePath: page => `/${pageId(project, page ?? '')}`,
|
|
558
749
|
status: async page => api(`/api/status${page ? `?page=${encodeURIComponent(pageId(project, page))}` : ''}`),
|
|
559
|
-
handOut: async (filter, onEvent, options) =>
|
|
560
|
-
|
|
750
|
+
handOut: async (filter, onEvent, options) =>
|
|
751
|
+
handOut(project.eventsFile, { ...filter, page: filter.page ? pageId(project, filter.page) : null }, onEvent, {
|
|
752
|
+
...options,
|
|
753
|
+
forAgent: forAgent((await projectConfig(project)).handedOut),
|
|
754
|
+
}),
|
|
755
|
+
authLink: async to =>
|
|
756
|
+
`${await ensureServer()}/_/auth?token=${encodeURIComponent(projectToken(project.wb))}&to=${encodeURIComponent(to)}`,
|
|
561
757
|
})
|
|
562
758
|
break
|
|
563
759
|
}
|
|
@@ -567,7 +763,8 @@ switch (cmd) {
|
|
|
567
763
|
print(HELP)
|
|
568
764
|
if (commands.length) {
|
|
569
765
|
print(`\nfrom this project's plugins:\n`)
|
|
570
|
-
for (const c of commands)
|
|
766
|
+
for (const c of commands)
|
|
767
|
+
print(` whiteboard ${[c.name, usage(c.command)].filter(Boolean).join(' ')}\n ${c.command.description}`)
|
|
571
768
|
}
|
|
572
769
|
if (mine.length) console.error(`\nwhiteboard: ${cmd} has no command ${JSON.stringify(rest[0] ?? '')}`)
|
|
573
770
|
if (!asked) process.exit(1)
|