@bigmistqke/whiteboard 0.6.1 → 0.8.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 +23 -216
- package/bin/whiteboard.ts +82 -239
- package/dist/bin/whiteboard.js +70 -200
- package/dist/client/runtime.js +297 -61
- package/dist/plugins/badge/README.md +3 -1
- package/dist/plugins/badge/public/style.css +17 -0
- package/dist/plugins/chat/public/client.js +2 -2
- package/dist/plugins/chat/public/style.css +19 -6
- package/dist/plugins/code/README.md +10 -0
- package/dist/plugins/code/index.js +31 -22
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +33 -2
- package/dist/plugins/comments/public/client.js +7 -2
- package/dist/plugins/comments/public/style.css +4 -0
- package/dist/plugins/form/public/style.css +2 -0
- package/dist/plugins/grid/public/style.css +0 -5
- package/dist/plugins/iframe/index.js +2 -2
- package/dist/plugins/index.js +5 -1
- package/dist/plugins/lightbox/public/client.js +1 -1
- package/dist/plugins/terminal/index.js +7 -9
- package/dist/plugins/terminal/public/client.js +1 -1
- package/dist/plugins/terminal/public/style.css +3 -3
- package/dist/plugins/timeline/README.md +40 -0
- package/dist/plugins/timeline/index.js +82 -0
- package/dist/plugins/timeline/public/style.css +118 -0
- package/dist/plugins/toc/README.md +13 -13
- package/dist/plugins/toc/index.js +6 -4
- package/dist/plugins/toc/public/client.js +1 -1
- package/dist/plugins/toc/public/style.css +19 -2
- package/dist/plugins/window/index.js +4 -4
- package/dist/solid/index.js +2 -1
- package/dist/src/auth.js +7 -3
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +74 -35
- package/dist/src/client/page/main.js +16 -5
- package/dist/src/document.js +93 -0
- package/dist/src/events.js +7 -15
- package/dist/src/hub.js +90 -53
- package/dist/src/listen.js +100 -0
- package/dist/src/listeners.js +67 -0
- package/dist/src/pages/index.js +1 -1
- package/dist/src/pages/markdown.js +23 -11
- package/dist/src/pages/render.js +13 -8
- package/dist/src/serve.js +2 -0
- package/dist/src/server.js +204 -514
- package/dist/src/site.js +173 -0
- package/dist/src/status.js +63 -1
- package/dist/src/ws.js +128 -0
- package/package.json +1 -1
- package/plugins/README.md +258 -0
- package/plugins/badge/README.md +3 -1
- package/plugins/badge/public/style.css +17 -0
- package/plugins/chat/client.tsx +34 -6
- package/plugins/chat/public/style.css +19 -6
- package/plugins/code/README.md +10 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/index.ts +38 -26
- package/plugins/code/public/style.css +33 -2
- package/plugins/comments/client.ts +56 -6
- package/plugins/comments/public/style.css +4 -0
- package/plugins/form/public/style.css +2 -0
- package/plugins/grid/public/style.css +0 -5
- package/plugins/iframe/index.ts +2 -2
- package/plugins/index.ts +6 -1
- package/plugins/lightbox/client.ts +13 -3
- package/plugins/terminal/client.ts +10 -2
- package/plugins/terminal/index.ts +8 -6
- package/plugins/terminal/public/style.css +3 -3
- package/plugins/timeline/README.md +40 -0
- package/plugins/timeline/index.ts +92 -0
- package/plugins/timeline/public/style.css +118 -0
- package/plugins/toc/README.md +13 -13
- package/plugins/toc/client.ts +58 -32
- package/plugins/toc/index.ts +13 -13
- package/plugins/toc/public/style.css +19 -2
- package/plugins/window/index.ts +4 -4
- package/skills/whiteboard/SKILL.md +3 -1
- package/solid/index.tsx +3 -0
- package/src/auth.ts +7 -2
- package/src/build.ts +213 -0
- package/src/client/forms.ts +102 -5
- package/src/client/page/base.css +74 -35
- package/src/client/page/elsewhere.ts +14 -4
- package/src/client/page/waiting.ts +10 -5
- package/src/client/read.ts +16 -3
- package/src/client/runtime.ts +219 -52
- package/src/client/toast.ts +36 -6
- package/src/define.ts +44 -18
- package/src/document.ts +191 -0
- package/src/events.ts +18 -17
- package/src/hub.ts +102 -56
- package/src/listen.ts +125 -0
- package/src/listeners.ts +95 -0
- package/src/pages/index.ts +1 -1
- package/src/pages/markdown.ts +29 -18
- package/src/pages/render.ts +13 -8
- package/src/serve.ts +2 -0
- package/src/server.ts +223 -611
- package/src/site.ts +165 -0
- package/src/status.ts +100 -0
- package/src/ws.ts +155 -0
package/README.md
CHANGED
|
@@ -211,7 +211,13 @@ A form with a `name` and no `action` attribute is recorded when submitted:
|
|
|
211
211
|
events of other types too, through `api.append(type, data)`. Those types
|
|
212
212
|
are named after their plugin, `<plugin>:<what>` (`chat:message`,
|
|
213
213
|
`comments:reply`, whiteboard's own `whiteboard:picked`), so a form's name
|
|
214
|
-
can't contain a colon and its answers never mix with them.
|
|
214
|
+
can't contain a colon and its answers never mix with them. Appending is
|
|
215
|
+
optimistic: the event is in `api.events()` at once, with `sending: true`
|
|
216
|
+
until the server has it, so show it then as on its way, not as done: in
|
|
217
|
+
`--wb-sending` (grey), as the core does for a form's button and the chat
|
|
218
|
+
for a message. A send that heard nothing back is tried again, without being
|
|
219
|
+
appended twice; only when none gets through does the event leave the
|
|
220
|
+
events again and the promise `api.append` returned reject. The built-in
|
|
215
221
|
`form` plugin writes forms as markdown with inputs inline
|
|
216
222
|
(`:::form{name=ship}` … `:radio[this week]{name=when}` … `:::`); a
|
|
217
223
|
hand-written `<form name="…">` works without it.
|
|
@@ -424,12 +430,12 @@ Every colour is a `light-dark()` pair, so one line sets it for both themes:
|
|
|
424
430
|
```
|
|
425
431
|
|
|
426
432
|
The built-in plugins are ansi, badge, callout, chat, code, comments, details, draft,
|
|
427
|
-
forge, form, grid, iframe, lightbox, mermaid, quiet, steps and window: every
|
|
428
|
-
`whiteboard docs` shows how to write each. A config's `plugins` add to
|
|
429
|
-
and one with a built-in's name replaces it. The
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
+
forge, form, grid, iframe, lightbox, mermaid, quiet, steps, timeline, toc and window: every
|
|
434
|
+
project has them, and `whiteboard docs` shows how to write each. A config's `plugins` add to
|
|
435
|
+
them, and one with a built-in's name replaces it. The table of contents (`toc`) shows on a page
|
|
436
|
+
that asks for it in its frontmatter (`plugins: { toc: true }`), or on every page with
|
|
437
|
+
`toc({ always: true })` in the config. The terminal and the Claude Code plugin are opt-in, since
|
|
438
|
+
they run code or are about one agent.
|
|
433
439
|
|
|
434
440
|
Named forms need nothing in the config: the core sends a submit of any
|
|
435
441
|
`<form name>` as an event, on any page.
|
|
@@ -466,9 +472,10 @@ whiteboard build 'docs/guide/*.md' --out preview # or what is given, over the
|
|
|
466
472
|
(so no answers, chats or comments), new-content marks and toasts, and the
|
|
467
473
|
plugins and blocks in `skip`: their blocks render as nothing, their
|
|
468
474
|
stylesheets, browser modules and what they add to the page aren't included.
|
|
469
|
-
A plugin's `
|
|
475
|
+
A plugin's `slots` is asked with no request, since no browser asked for the
|
|
470
476
|
page: the terminal's drawer, which is only for the project's own browser,
|
|
471
|
-
isn't there.
|
|
477
|
+
isn't there. Files a page names (its images, an iframe's page) are copied
|
|
478
|
+
beside it.
|
|
472
479
|
- **What is for reading stays:** steps, the lightbox, ⤢ on
|
|
473
480
|
windows, the theme toggle, the contents, details. Code and diagrams were
|
|
474
481
|
rendered on the server, so the site loads no highlighter.
|
|
@@ -488,214 +495,14 @@ way, but steps, the lightbox and the theme toggle need the server.
|
|
|
488
495
|
|
|
489
496
|
## Writing a plugin
|
|
490
497
|
|
|
491
|
-
Everything that extends whiteboard is a plugin: syntax for pages,
|
|
492
|
-
behaviour on every page, commands, events
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
`.whiteboard/name/`. Local plugins go in `.whiteboard/plugins/`.
|
|
498
|
+
Everything that extends whiteboard is a plugin: syntax for pages (blocks),
|
|
499
|
+
behaviour on every page, commands, routes, events, and what goes in the
|
|
500
|
+
page's slots (its header bar, a panel beside the text, a drawer, the corner).
|
|
501
|
+
A plugin is an object with a name, and everything it adds lives under that
|
|
502
|
+
name. Local plugins go in `.whiteboard/plugins/`.
|
|
497
503
|
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
A block is syntax: `:::name` around content, `::name` on a line, `:name`
|
|
501
|
-
inside a line, or a code fence of that name. It renders on the server, and
|
|
502
|
-
with a `client` it is mounted in the browser too.
|
|
503
|
-
|
|
504
|
-
```ts
|
|
505
|
-
// .whiteboard/plugins/checklist/index.ts
|
|
506
|
-
import { definePlugin, html, type DirectiveContext } from '@bigmistqke/whiteboard'
|
|
507
|
-
|
|
508
|
-
const toText = (node: any): string => node.value ?? (node.children ?? []).map(toText).join('')
|
|
509
|
-
|
|
510
|
-
export default function checklist() {
|
|
511
|
-
return definePlugin({
|
|
512
|
-
name: 'checklist',
|
|
513
|
-
blocks: {
|
|
514
|
-
checklist: {
|
|
515
|
-
// what the browser part gets: the block's list items as text
|
|
516
|
-
props: ctx => {
|
|
517
|
-
const c = ctx as DirectiveContext // a block (`:::checklist`), not a code fence
|
|
518
|
-
return {
|
|
519
|
-
name: c.attrs.name ?? 'checklist',
|
|
520
|
-
items: (c.children as any[]).filter(n => n.type === 'list')
|
|
521
|
-
.flatMap(list => list.children.map((item: any) => toText(item))),
|
|
522
|
-
}
|
|
523
|
-
},
|
|
524
|
-
// shown until the client mounts, then replaced
|
|
525
|
-
html: () => html`<ul class="checklist"></ul>`,
|
|
526
|
-
client: 'client.js', // in its public/ folder
|
|
527
|
-
},
|
|
528
|
-
},
|
|
529
|
-
// its events, and whether a `wait` on the page hands them out (a tick is information: no)
|
|
530
|
-
events: { 'checklist:tick': { description: '`{ checklist, item, checked }`' } },
|
|
531
|
-
// for `whiteboard docs`, under a `# Plugin: checklist` heading the core adds
|
|
532
|
-
docs: () => 'A list the reader ticks:\n\n :::checklist{name=launch}\n - Write the changelog\n :::\n',
|
|
533
|
-
})
|
|
534
|
-
}
|
|
535
|
-
```
|
|
536
|
-
|
|
537
|
-
```js
|
|
538
|
-
// .whiteboard/plugins/checklist/public/client.js: its public/ folder is served, nothing else of it
|
|
539
|
-
export default function mount(el, { name, items }, api) {
|
|
540
|
-
const ticked = () => {
|
|
541
|
-
const state = new Map()
|
|
542
|
-
for (const e of api.events()) if (e.type === 'checklist:tick' && e.data.checklist === name) state.set(e.data.item, e.data.checked)
|
|
543
|
-
return state
|
|
544
|
-
}
|
|
545
|
-
const draw = () => {
|
|
546
|
-
const state = ticked()
|
|
547
|
-
el.replaceChildren(...items.map(item => {
|
|
548
|
-
const box = Object.assign(document.createElement('input'), { type: 'checkbox', checked: !!state.get(item) })
|
|
549
|
-
box.addEventListener('change', () => api.append('checklist:tick', { checklist: name, item, checked: box.checked }))
|
|
550
|
-
const label = document.createElement('label')
|
|
551
|
-
label.append(box, ' ', item)
|
|
552
|
-
return label
|
|
553
|
-
}))
|
|
554
|
-
}
|
|
555
|
-
draw()
|
|
556
|
-
// the page's events, also the ones it appends itself, come back here
|
|
557
|
-
return api.events.on(draw)
|
|
558
|
-
}
|
|
559
|
-
```
|
|
560
|
-
|
|
561
|
-
```ts
|
|
562
|
-
// .whiteboard/config.ts
|
|
563
|
-
import { defineConfig } from '@bigmistqke/whiteboard'
|
|
564
|
-
import checklist from './plugins/checklist/index.ts'
|
|
565
|
-
|
|
566
|
-
export default defineConfig({ plugins: [checklist()] })
|
|
567
|
-
```
|
|
568
|
-
|
|
569
|
-
An agent can wait for a tick by asking for it: `whiteboard wait --page
|
|
570
|
-
<file> --type checklist:tick`.
|
|
571
|
-
|
|
572
|
-
### Behaviour on every page: votes
|
|
573
|
-
|
|
574
|
-
A plugin can also put something on every page, and add commands:
|
|
575
|
-
|
|
576
|
-
```ts
|
|
577
|
-
// .whiteboard/plugins/votes/index.ts
|
|
578
|
-
import { definePlugin } from '@bigmistqke/whiteboard'
|
|
579
|
-
import { read } from '@bigmistqke/whiteboard/log'
|
|
580
|
-
|
|
581
|
-
export default function votes() {
|
|
582
|
-
return definePlugin({
|
|
583
|
-
name: 'votes',
|
|
584
|
-
events: { 'votes:up': { description: 'the reader voted the page up' } },
|
|
585
|
-
commands: {
|
|
586
|
-
'': {
|
|
587
|
-
description: 'print the votes per page',
|
|
588
|
-
run(ctx) {
|
|
589
|
-
const counts = new Map<string, number>()
|
|
590
|
-
for (const e of read(ctx.eventsFile).events) if (e.type === 'votes:up') counts.set(e.page, (counts.get(e.page) ?? 0) + 1)
|
|
591
|
-
for (const [page, n] of counts) console.log(`${page}: ${n}`)
|
|
592
|
-
},
|
|
593
|
-
},
|
|
594
|
-
},
|
|
595
|
-
docs: () => 'The reader can vote a page up: a `votes:up` event. `whiteboard votes` counts them.',
|
|
596
|
-
// from setup: what the running server adds (routes, markup, the browser module)
|
|
597
|
-
setup: () => ({ client: 'client.js' }),
|
|
598
|
-
})
|
|
599
|
-
}
|
|
600
|
-
```
|
|
601
|
-
|
|
602
|
-
```js
|
|
603
|
-
// .whiteboard/plugins/votes/public/client.js
|
|
604
|
-
export default function mount(root, page, api) {
|
|
605
|
-
const button = document.createElement('button')
|
|
606
|
-
const draw = () => { button.textContent = `👍 ${api.events().filter(e => e.type === 'votes:up').length}` }
|
|
607
|
-
button.addEventListener('click', () => api.append('votes:up'))
|
|
608
|
-
api.corner().append(button) // the page's corner, for floating buttons
|
|
609
|
-
api.events.on(draw)
|
|
610
|
-
draw()
|
|
611
|
-
}
|
|
612
|
-
```
|
|
613
|
-
|
|
614
|
-
### What a plugin can have
|
|
615
|
-
|
|
616
|
-
- **`blocks`**, by name. A block has `html(ctx)` (the server's HTML; `ctx`
|
|
617
|
-
has the block's attributes, label, content as mdast nodes, `toHtml`,
|
|
618
|
-
`items()` and more), `props(ctx)` (JSON for its client), `client` (a
|
|
619
|
-
browser module, mounted per block), `style` (linked on pages that use it),
|
|
620
|
-
`fence: true` to also take the code fences of its name (```` ```mermaid ````),
|
|
621
|
-
`fence: 'any'` for the block that takes every code fence nobody else
|
|
622
|
-
claims, and `open(ctx)`: what it shows on its own, for a window around it.
|
|
623
|
-
Block names are unique across a project's plugins.
|
|
624
|
-
- **`compose(ctx)`**: itself around any block that names the plugin in its
|
|
625
|
-
`compose` attribute, that block's HTML as `ctx.children` (see below).
|
|
626
|
-
- **`events`**, by type (`<plugin>:<what>`): a description each, and
|
|
627
|
-
`handOut: true` for those an agent should get unasked, like a chat
|
|
628
|
-
message. `whiteboard docs` lists them.
|
|
629
|
-
- **`style`**: a stylesheet on every page.
|
|
630
|
-
- **`docs`**: markdown for agents: how to write its blocks, what its events
|
|
631
|
-
mean, how to answer.
|
|
632
|
-
- **`commands`**: `whiteboard <name>` and `whiteboard <name> <sub>`, with
|
|
633
|
-
declared options that the core parses and checks. A command gets the
|
|
634
|
-
project, its event log (`ctx.eventsFile`, for
|
|
635
|
-
`@bigmistqke/whiteboard/log`), its token and server, what is open on each
|
|
636
|
-
page (`ctx.status`), and handing out (`ctx.handOut`, what `wait` does).
|
|
637
|
-
- **`session()`**: the agent session a command runs in, for a plugin that
|
|
638
|
-
knows how its agent says so.
|
|
639
|
-
- **`setup(ctx)`**, which runs once per loaded config on the server and
|
|
640
|
-
returns:
|
|
641
|
-
- `routes`, by method and path (`'POST /start'` is mounted at
|
|
642
|
-
`/api/<name>/start`), each with what it does, whether it needs the
|
|
643
|
-
project's token (`auth`), and the types of its body and query; the core
|
|
644
|
-
checks all of that before the route's code runs;
|
|
645
|
-
- `shell`, markup at the end of every page's `<head>` and `<body>`;
|
|
646
|
-
- `client`, a module on every page, run once per page load.
|
|
647
|
-
|
|
648
|
-
**A plugin can compose itself around other blocks.** With `compose`, a
|
|
649
|
-
block whose `compose` attribute names the plugin is put in what it returns,
|
|
650
|
-
the block's HTML as `children`, as a component gets what it wraps. The
|
|
651
|
-
window plugin is one: `::image{src compose=window}`, ```` ```mermaid {compose=window} ````
|
|
652
|
-
and `:::grid{compose=window}` all go in a window, and none of those blocks
|
|
653
|
-
knows of windows. A block says what it shows on its own with `open` (an
|
|
654
|
-
image's file), which a window's ↗ opens. Several named compose in the order
|
|
655
|
-
written, the first innermost: `compose=lightbox,window`. A name no plugin
|
|
656
|
-
composes is said in place.
|
|
657
|
-
|
|
658
|
-
```ts
|
|
659
|
-
definePlugin({
|
|
660
|
-
name: 'window',
|
|
661
|
-
compose: ({ children, label, open }) => windowFrame({ label, open, body: children }),
|
|
662
|
-
})
|
|
663
|
-
```
|
|
664
|
-
|
|
665
|
-
**A plugin's browser files live in its `public/` folder**, and only that
|
|
666
|
-
folder is served: its client modules, styles, images and fonts, never its
|
|
667
|
-
server code or the project's files beside it. `client` and `style` (a
|
|
668
|
-
plugin's and a block's) name files in it: `'client.js'`. `definePlugin` puts
|
|
669
|
-
it next to the module that calls it (`plugins/checklist/public/` for
|
|
670
|
-
`plugins/checklist/index.ts`); say another with `public: new URL('./assets/',
|
|
671
|
-
import.meta.url)`; a plain object plugin, or one written in the config, has
|
|
672
|
-
`.whiteboard/public/`. A file named outside it is a config error. Browser
|
|
673
|
-
code is JavaScript or TypeScript: a `.ts` file is served with its types
|
|
674
|
-
stripped (Node's type stripping, as for server code: types only, so no
|
|
675
|
-
`enum` or `namespace`, and imports name their `.ts` files). JSX still needs
|
|
676
|
-
building first. A block's `client` gets
|
|
677
|
-
`mount(element, props, api)` and may return a cleanup, run when the block
|
|
678
|
-
is replaced on a live update; it replaces the server's HTML when it mounts.
|
|
679
|
-
A page module (`setup`'s `client`) gets `mount(root, page, api)`. Both get
|
|
680
|
-
the page's `api`: its `events` and `append` (which comes back through
|
|
681
|
-
`events.on`), the `listeners`, `forms` and `unread` messages, `onRendered`
|
|
682
|
-
(after a live update, to put back what it adds), the page's `frontmatter`
|
|
683
|
-
on the page handle (YAML, as parsed) and the plugin's own `settings` in it
|
|
684
|
-
(under `plugins:` by its name: `plugins: { toc: true }` is `true` for toc), `corner()` for floating buttons, `toast()` for a label in
|
|
685
|
-
the corner that says what is going on and goes there (one that hides after a
|
|
686
|
-
few seconds, or one that stays with a count), `badge()` for counts in the
|
|
687
|
-
tab title, the `theme`, and `url(path)`: the address of one of the
|
|
688
|
-
project's server routes as this page requests it (`api.url('/api/votes/count')`).
|
|
689
|
-
Ask for the plugin's routes through it rather than writing `/api/…`: under a
|
|
690
|
-
shared server a project is served below a path (`/rg/develop`). Every value
|
|
691
|
-
that changes is called for what it
|
|
692
|
-
is now and has `.on(fn)` for each change: `api.events()`,
|
|
693
|
-
`api.events.on(draw)`, `page.frontmatter.on(…)`. Files beside a browser module that are browser
|
|
694
|
-
assets (scripts, styles, images, fonts) are served too.
|
|
695
|
-
|
|
696
|
-
Something that covers the bottom of the window says how much in
|
|
697
|
-
`--wb-inset-bottom`, and the page keeps clear of it. A plugin can't be
|
|
698
|
-
named like a built-in command or a folder of the core.
|
|
504
|
+
How to write one, with examples, is in [plugins/README.md](plugins/README.md);
|
|
505
|
+
the plugins in that folder are the built-in ones.
|
|
699
506
|
|
|
700
507
|
## Pages
|
|
701
508
|
|
package/bin/whiteboard.ts
CHANGED
|
@@ -36,8 +36,9 @@ import {
|
|
|
36
36
|
stateDir,
|
|
37
37
|
} from '../src/entries.ts'
|
|
38
38
|
import { compareVersions, VERSION } from '../src/package.ts'
|
|
39
|
-
import { statusLines,
|
|
40
|
-
import
|
|
39
|
+
import { statusLines, whose } from '../src/status.ts'
|
|
40
|
+
import { fallback as fallbackLoop, listen } from '../src/listen.ts'
|
|
41
|
+
import type { Listener, PageStatus } from '../src/define.ts'
|
|
41
42
|
|
|
42
43
|
// a failing command prints its message, not a stack trace
|
|
43
44
|
const fail = (err: unknown) => {
|
|
@@ -305,112 +306,6 @@ async function askInProcess(route: string): Promise<Response> {
|
|
|
305
306
|
}
|
|
306
307
|
}
|
|
307
308
|
|
|
308
|
-
/**
|
|
309
|
-
* Tell the project's server that an agent is waiting, for as long as this command runs: pages show
|
|
310
|
-
* what is waited for. Holds one connection open, the listener; reconnects when the server restarts.
|
|
311
|
-
* Reading the log does not depend on it, so answers are never missed when the server is away.
|
|
312
|
-
*/
|
|
313
|
-
/**
|
|
314
|
-
* Tell the server who is listening, for as long as the command runs (again after a restart). The
|
|
315
|
-
* fallback is refused when another one runs: it exits, saying so. Resolves once the server took it.
|
|
316
|
-
*/
|
|
317
|
-
function listen(f: EventFilter, command: 'wait' | 'watch', fallback = false): Promise<void> {
|
|
318
|
-
return new Promise(registered => {
|
|
319
|
-
const connect = async () => {
|
|
320
|
-
// a wait never starts a server: without one, it waits for one to appear (the log is read
|
|
321
|
-
// without it all the same)
|
|
322
|
-
let base
|
|
323
|
-
try {
|
|
324
|
-
base = await needServer()
|
|
325
|
-
} catch {
|
|
326
|
-
return setTimeout(connect, 2000).unref()
|
|
327
|
-
}
|
|
328
|
-
const q = new URLSearchParams({
|
|
329
|
-
command,
|
|
330
|
-
...(f.page ? { page: f.page } : {}),
|
|
331
|
-
...(f.type ? { type: f.type } : {}),
|
|
332
|
-
...(fallback ? { fallback: '' } : {}),
|
|
333
|
-
})
|
|
334
|
-
let again = false
|
|
335
|
-
const retry = (ms: number) => {
|
|
336
|
-
if (!again) {
|
|
337
|
-
again = true
|
|
338
|
-
setTimeout(connect, ms).unref()
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
const req = http.get(`${base}/api/listen?${q}`, res => {
|
|
342
|
-
if (res.statusCode === 409) {
|
|
343
|
-
console.error(
|
|
344
|
-
'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.',
|
|
345
|
-
)
|
|
346
|
-
process.exit(1)
|
|
347
|
-
}
|
|
348
|
-
res.resume()
|
|
349
|
-
registered()
|
|
350
|
-
res.on('close', () => retry(1000))
|
|
351
|
-
})
|
|
352
|
-
req.on('error', () => retry(2000))
|
|
353
|
-
// the connection alone does not keep the command running
|
|
354
|
-
req.on('socket', socket => socket.unref())
|
|
355
|
-
}
|
|
356
|
-
connect()
|
|
357
|
-
})
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* Who else listens, kept current: the fallback leaves to them what they would take. Calls
|
|
362
|
-
* `changed` after each change (the server's live channel says when).
|
|
363
|
-
*/
|
|
364
|
-
async function otherListeners(
|
|
365
|
-
changed: () => void,
|
|
366
|
-
server: () => Promise<string> = needServer,
|
|
367
|
-
): Promise<() => Listener[]> {
|
|
368
|
-
let list: Listener[] = []
|
|
369
|
-
const load = async () => {
|
|
370
|
-
try {
|
|
371
|
-
list = (await (await fetch(`${await server()}/api/listeners`)).json()) as Listener[]
|
|
372
|
-
} catch {
|
|
373
|
-
/* the server restarts */
|
|
374
|
-
}
|
|
375
|
-
changed()
|
|
376
|
-
}
|
|
377
|
-
const follow = async () => {
|
|
378
|
-
let again = false
|
|
379
|
-
const retry = () => {
|
|
380
|
-
if (!again) {
|
|
381
|
-
again = true
|
|
382
|
-
setTimeout(follow, 1000).unref()
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
let base
|
|
386
|
-
try {
|
|
387
|
-
base = await server()
|
|
388
|
-
} catch {
|
|
389
|
-
return retry()
|
|
390
|
-
}
|
|
391
|
-
const req = http.get(`${base}/api/watch?dir`, res => {
|
|
392
|
-
let buf = ''
|
|
393
|
-
res.setEncoding('utf8')
|
|
394
|
-
res.on('data', (chunk: string) => {
|
|
395
|
-
buf += chunk
|
|
396
|
-
if (buf.includes('event: listeners')) {
|
|
397
|
-
buf = ''
|
|
398
|
-
load()
|
|
399
|
-
}
|
|
400
|
-
if (buf.length > 4096) buf = buf.slice(-64)
|
|
401
|
-
})
|
|
402
|
-
res.on('close', retry)
|
|
403
|
-
// reconnected after a restart: who listens now
|
|
404
|
-
load()
|
|
405
|
-
})
|
|
406
|
-
req.on('error', retry)
|
|
407
|
-
req.on('socket', socket => socket.unref())
|
|
408
|
-
}
|
|
409
|
-
await load()
|
|
410
|
-
follow()
|
|
411
|
-
return () => list
|
|
412
|
-
}
|
|
413
|
-
|
|
414
309
|
function init(name: string | undefined) {
|
|
415
310
|
const dir = process.cwd()
|
|
416
311
|
const wb = path.join(dir, '.whiteboard')
|
|
@@ -708,7 +603,11 @@ switch (cmd) {
|
|
|
708
603
|
})
|
|
709
604
|
const { tabs } = (await res.json()) as { tabs: number }
|
|
710
605
|
if (!tabs) {
|
|
711
|
-
|
|
606
|
+
// a tab may be open but not connected: a background tab connects again when it is shown, and
|
|
607
|
+
// then scrolls there (the scroll is in the log)
|
|
608
|
+
console.error(
|
|
609
|
+
`whiteboard: no tab of ${id} is connected now; one that is open scrolls there once it is back (a background tab connects again when it is shown). \`whiteboard show ${target}\` prints its URL`,
|
|
610
|
+
)
|
|
712
611
|
process.exit(1)
|
|
713
612
|
}
|
|
714
613
|
console.log(`scrolled ${tabs} tab${tabs > 1 ? 's' : ''} of ${id} to ${JSON.stringify(to)}`)
|
|
@@ -807,49 +706,19 @@ switch (cmd) {
|
|
|
807
706
|
break
|
|
808
707
|
}
|
|
809
708
|
const { handedOut } = await projectConfig(proj)
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
const others = fallback ? (await listen(f, cmd, true), await otherListeners(() => recheck())) : null
|
|
815
|
-
// The fallback's copy of who else listens can lag a moment behind a wait that just started, so
|
|
816
|
-
// it holds each event briefly before deciding: by then that wait took it, or the copy knows.
|
|
817
|
-
const held = new Set<string>()
|
|
818
|
-
const skip = (e: WbEvent) => {
|
|
819
|
-
if (!held.has(e.id)) {
|
|
820
|
-
held.add(e.id)
|
|
821
|
-
setTimeout(() => recheck(), 500).unref()
|
|
822
|
-
return true
|
|
823
|
-
}
|
|
824
|
-
return takenByOther(others!(), e)
|
|
709
|
+
const query = {
|
|
710
|
+
command: cmd,
|
|
711
|
+
...(f.page ? { page: f.page } : {}),
|
|
712
|
+
...(f.type ? { type: f.type } : {}),
|
|
825
713
|
}
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
}),
|
|
835
|
-
}).then(async () => {
|
|
836
|
-
// a wait on a page ends with an answer, and the agent acts on it; what is still unanswered
|
|
837
|
-
// there needs the next wait, which is easily forgotten: say so, right with the answer
|
|
838
|
-
if (cmd === 'wait' && !fallback) {
|
|
839
|
-
try {
|
|
840
|
-
const pages = (await api(`/api/status?page=${encodeURIComponent(f.page!)}`)) as PageStatus[]
|
|
841
|
-
for (const p of pages)
|
|
842
|
-
if (p.open.length)
|
|
843
|
-
console.error(
|
|
844
|
-
`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`,
|
|
845
|
-
)
|
|
846
|
-
} catch {
|
|
847
|
-
/* no status: the answer is what matters */
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
process.exit(0)
|
|
851
|
-
})
|
|
852
|
-
if (!fallback) listen(f, cmd)
|
|
714
|
+
const refused = (reason: string) => {
|
|
715
|
+
console.error(
|
|
716
|
+
`whiteboard: ${reason}: 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.`,
|
|
717
|
+
)
|
|
718
|
+
process.exit(1)
|
|
719
|
+
}
|
|
720
|
+
const at = () => needServer().catch(() => null)
|
|
721
|
+
const options = { count: cmd === 'wait' ? Number(opts.count ?? 1) : Infinity, forAgent: forAgent(handedOut) }
|
|
853
722
|
if (opts.timeout && cmd === 'wait')
|
|
854
723
|
setTimeout(
|
|
855
724
|
() => {
|
|
@@ -858,6 +727,37 @@ switch (cmd) {
|
|
|
858
727
|
},
|
|
859
728
|
Number(opts.timeout) * 1000,
|
|
860
729
|
)
|
|
730
|
+
// without --page, the fallback: only what no other listener would take, on any page, as it comes
|
|
731
|
+
// and once they stop listening (an answer left in the log, too)
|
|
732
|
+
const fallback = !f.page
|
|
733
|
+
if (fallback) {
|
|
734
|
+
const update = fallbackLoop((_, handing) => {
|
|
735
|
+
void handOut(proj.eventsFile, f, e => console.log(JSON.stringify(e)), { ...options, ...handing }).then(() =>
|
|
736
|
+
process.exit(0),
|
|
737
|
+
)
|
|
738
|
+
})
|
|
739
|
+
await listen({ at, query: { ...query, fallback: '' }, onSnapshot: update, onRefused: refused })
|
|
740
|
+
} else {
|
|
741
|
+
// registered before it reads the log, so a fallback leaves its answers to it from the first one
|
|
742
|
+
await listen({ at, query, onRefused: refused }, { orNone: true })
|
|
743
|
+
void handOut(proj.eventsFile, f, e => console.log(JSON.stringify(e)), options).then(async () => {
|
|
744
|
+
// a wait on a page ends with an answer, and the agent acts on it; what is still unanswered
|
|
745
|
+
// there needs the next wait, which is easily forgotten: say so, right with the answer
|
|
746
|
+
if (cmd === 'wait') {
|
|
747
|
+
try {
|
|
748
|
+
const pages = (await api(`/api/status?page=${encodeURIComponent(f.page!)}`)) as PageStatus[]
|
|
749
|
+
for (const p of pages)
|
|
750
|
+
if (p.open.length)
|
|
751
|
+
console.error(
|
|
752
|
+
`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`,
|
|
753
|
+
)
|
|
754
|
+
} catch {
|
|
755
|
+
/* no status: the answer is what matters */
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
process.exit(0)
|
|
759
|
+
})
|
|
760
|
+
}
|
|
861
761
|
break
|
|
862
762
|
}
|
|
863
763
|
case 'append': {
|
|
@@ -992,98 +892,41 @@ async function entryFallback(entry: string, f: EventFilter, command: 'wait' | 'w
|
|
|
992
892
|
},
|
|
993
893
|
Number(opts.timeout) * 1000,
|
|
994
894
|
)
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
const q = new URLSearchParams({ entry, command, ...(f.type ? { type: f.type } : {}) })
|
|
1001
|
-
let again = false
|
|
1002
|
-
const retry = (ms: number) => {
|
|
1003
|
-
if (!again) {
|
|
1004
|
-
again = true
|
|
1005
|
-
setTimeout(connect, ms).unref()
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
const req = http.get(`${shared.origin}/api/listen?${q}`, res => {
|
|
1009
|
-
if (res.statusCode === 409) {
|
|
1010
|
-
let body = ''
|
|
1011
|
-
res.on('data', d => (body += d))
|
|
1012
|
-
res.on('end', () => {
|
|
1013
|
-
console.error(
|
|
1014
|
-
`whiteboard: ${JSON.parse(body || '{}').error ?? 'another agent is the fallback'}. Add --page to wait on a page of your own.`,
|
|
1015
|
-
)
|
|
1016
|
-
process.exit(1)
|
|
1017
|
-
})
|
|
1018
|
-
return
|
|
1019
|
-
}
|
|
1020
|
-
res.resume()
|
|
1021
|
-
registered()
|
|
1022
|
-
res.on('close', () => retry(1000))
|
|
1023
|
-
})
|
|
1024
|
-
req.on('error', () => retry(2000))
|
|
1025
|
-
req.on('socket', socket => socket.unref())
|
|
1026
|
-
}
|
|
1027
|
-
connect()
|
|
1028
|
-
})
|
|
1029
|
-
// a hand-out per project of the entry, as the shared server knows them
|
|
1030
|
-
const following = new Map<string, AbortController>()
|
|
1031
|
-
async function start(p: { dir: string; base: string }, origin: string) {
|
|
895
|
+
let origin = ''
|
|
896
|
+
const page = (id: string) => id.split('/').map(encodeURIComponent).join('/')
|
|
897
|
+
// a hand-out per project of the entry, as the shared server names them, each event with its
|
|
898
|
+
// project and its page's address
|
|
899
|
+
const update = fallbackLoop((p, handing) => {
|
|
1032
900
|
const proj = projectAt(p.dir)
|
|
1033
901
|
if (!proj) return
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
if (!shared || !now) throw new Error('not served')
|
|
1042
|
-
return `${shared.origin}${now.base}`
|
|
1043
|
-
}
|
|
1044
|
-
const others = await otherListeners(() => recheck(), at)
|
|
1045
|
-
// as for a project's fallback: hold each event a moment, for a wait that just started
|
|
1046
|
-
const held = new Set<string>()
|
|
1047
|
-
const skip = (e: WbEvent) => {
|
|
1048
|
-
if (!held.has(e.id)) {
|
|
1049
|
-
held.add(e.id)
|
|
1050
|
-
setTimeout(() => recheck(), 500).unref()
|
|
1051
|
-
return true
|
|
1052
|
-
}
|
|
1053
|
-
return takenByOther(others(), e)
|
|
1054
|
-
}
|
|
1055
|
-
const page = (id: string) => id.split('/').map(encodeURIComponent).join('/')
|
|
1056
|
-
await handOut(
|
|
1057
|
-
proj.eventsFile,
|
|
1058
|
-
{ type: f.type },
|
|
1059
|
-
e => {
|
|
1060
|
-
console.log(JSON.stringify({ ...e, project: p.dir, url: `${origin}${p.base}/${page(e.page)}` }))
|
|
1061
|
-
if (++total >= count) done.abort()
|
|
1062
|
-
},
|
|
1063
|
-
{
|
|
1064
|
-
forAgent: forAgent(handedOut),
|
|
1065
|
-
signal: AbortSignal.any([stop.signal, done.signal]),
|
|
1066
|
-
skip,
|
|
1067
|
-
recheck: again => {
|
|
1068
|
-
recheck = again
|
|
902
|
+
void projectConfig(proj).then(({ handedOut }) =>
|
|
903
|
+
handOut(
|
|
904
|
+
proj.eventsFile,
|
|
905
|
+
{ type: f.type },
|
|
906
|
+
e => {
|
|
907
|
+
console.log(JSON.stringify({ ...e, project: p.dir, url: `${origin}${p.base}/${page(e.page)}` }))
|
|
908
|
+
if (++total >= count) done.abort()
|
|
1069
909
|
},
|
|
1070
|
-
|
|
910
|
+
{ forAgent: forAgent(handedOut), ...handing, signal: AbortSignal.any([handing.signal, done.signal]) },
|
|
911
|
+
),
|
|
1071
912
|
)
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
913
|
+
})
|
|
914
|
+
// held at the shared server for as long as this runs (again after a restart); without one running
|
|
915
|
+
// it waits for one, as any wait does: who else listens is the server's to say
|
|
916
|
+
await listen({
|
|
917
|
+
at: async () => {
|
|
918
|
+
origin = (await sharedServer(file))?.origin ?? ''
|
|
919
|
+
return origin || null
|
|
920
|
+
},
|
|
921
|
+
query: { entry, command, ...(f.type ? { type: f.type } : {}) },
|
|
922
|
+
onSnapshot: update,
|
|
923
|
+
onRefused: reason => {
|
|
924
|
+
console.error(`whiteboard: ${reason}. Add --page to wait on a page of your own.`)
|
|
925
|
+
process.exit(1)
|
|
926
|
+
},
|
|
927
|
+
})
|
|
928
|
+
// keeps the command running: the connection does not
|
|
929
|
+
setInterval(() => {}, 1 << 30)
|
|
1087
930
|
}
|
|
1088
931
|
|
|
1089
932
|
/** The commands the project's plugins add: `whiteboard <plugin> [sub]`. */
|