@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.
Files changed (101) hide show
  1. package/README.md +23 -216
  2. package/bin/whiteboard.ts +82 -239
  3. package/dist/bin/whiteboard.js +70 -200
  4. package/dist/client/runtime.js +297 -61
  5. package/dist/plugins/badge/README.md +3 -1
  6. package/dist/plugins/badge/public/style.css +17 -0
  7. package/dist/plugins/chat/public/client.js +2 -2
  8. package/dist/plugins/chat/public/style.css +19 -6
  9. package/dist/plugins/code/README.md +10 -0
  10. package/dist/plugins/code/index.js +31 -22
  11. package/dist/plugins/code/public/client.js +1 -0
  12. package/dist/plugins/code/public/style.css +33 -2
  13. package/dist/plugins/comments/public/client.js +7 -2
  14. package/dist/plugins/comments/public/style.css +4 -0
  15. package/dist/plugins/form/public/style.css +2 -0
  16. package/dist/plugins/grid/public/style.css +0 -5
  17. package/dist/plugins/iframe/index.js +2 -2
  18. package/dist/plugins/index.js +5 -1
  19. package/dist/plugins/lightbox/public/client.js +1 -1
  20. package/dist/plugins/terminal/index.js +7 -9
  21. package/dist/plugins/terminal/public/client.js +1 -1
  22. package/dist/plugins/terminal/public/style.css +3 -3
  23. package/dist/plugins/timeline/README.md +40 -0
  24. package/dist/plugins/timeline/index.js +82 -0
  25. package/dist/plugins/timeline/public/style.css +118 -0
  26. package/dist/plugins/toc/README.md +13 -13
  27. package/dist/plugins/toc/index.js +6 -4
  28. package/dist/plugins/toc/public/client.js +1 -1
  29. package/dist/plugins/toc/public/style.css +19 -2
  30. package/dist/plugins/window/index.js +4 -4
  31. package/dist/solid/index.js +2 -1
  32. package/dist/src/auth.js +7 -3
  33. package/dist/src/build.js +159 -0
  34. package/dist/src/client/page/base.css +74 -35
  35. package/dist/src/client/page/main.js +16 -5
  36. package/dist/src/document.js +93 -0
  37. package/dist/src/events.js +7 -15
  38. package/dist/src/hub.js +90 -53
  39. package/dist/src/listen.js +100 -0
  40. package/dist/src/listeners.js +67 -0
  41. package/dist/src/pages/index.js +1 -1
  42. package/dist/src/pages/markdown.js +23 -11
  43. package/dist/src/pages/render.js +13 -8
  44. package/dist/src/serve.js +2 -0
  45. package/dist/src/server.js +204 -514
  46. package/dist/src/site.js +173 -0
  47. package/dist/src/status.js +63 -1
  48. package/dist/src/ws.js +128 -0
  49. package/package.json +1 -1
  50. package/plugins/README.md +258 -0
  51. package/plugins/badge/README.md +3 -1
  52. package/plugins/badge/public/style.css +17 -0
  53. package/plugins/chat/client.tsx +34 -6
  54. package/plugins/chat/public/style.css +19 -6
  55. package/plugins/code/README.md +10 -0
  56. package/plugins/code/client.ts +73 -0
  57. package/plugins/code/index.ts +38 -26
  58. package/plugins/code/public/style.css +33 -2
  59. package/plugins/comments/client.ts +56 -6
  60. package/plugins/comments/public/style.css +4 -0
  61. package/plugins/form/public/style.css +2 -0
  62. package/plugins/grid/public/style.css +0 -5
  63. package/plugins/iframe/index.ts +2 -2
  64. package/plugins/index.ts +6 -1
  65. package/plugins/lightbox/client.ts +13 -3
  66. package/plugins/terminal/client.ts +10 -2
  67. package/plugins/terminal/index.ts +8 -6
  68. package/plugins/terminal/public/style.css +3 -3
  69. package/plugins/timeline/README.md +40 -0
  70. package/plugins/timeline/index.ts +92 -0
  71. package/plugins/timeline/public/style.css +118 -0
  72. package/plugins/toc/README.md +13 -13
  73. package/plugins/toc/client.ts +58 -32
  74. package/plugins/toc/index.ts +13 -13
  75. package/plugins/toc/public/style.css +19 -2
  76. package/plugins/window/index.ts +4 -4
  77. package/skills/whiteboard/SKILL.md +3 -1
  78. package/solid/index.tsx +3 -0
  79. package/src/auth.ts +7 -2
  80. package/src/build.ts +213 -0
  81. package/src/client/forms.ts +102 -5
  82. package/src/client/page/base.css +74 -35
  83. package/src/client/page/elsewhere.ts +14 -4
  84. package/src/client/page/waiting.ts +10 -5
  85. package/src/client/read.ts +16 -3
  86. package/src/client/runtime.ts +219 -52
  87. package/src/client/toast.ts +36 -6
  88. package/src/define.ts +44 -18
  89. package/src/document.ts +191 -0
  90. package/src/events.ts +18 -17
  91. package/src/hub.ts +102 -56
  92. package/src/listen.ts +125 -0
  93. package/src/listeners.ts +95 -0
  94. package/src/pages/index.ts +1 -1
  95. package/src/pages/markdown.ts +29 -18
  96. package/src/pages/render.ts +13 -8
  97. package/src/serve.ts +2 -0
  98. package/src/server.ts +223 -611
  99. package/src/site.ts +165 -0
  100. package/src/status.ts +100 -0
  101. package/src/ws.ts +155 -0
@@ -0,0 +1,159 @@
1
+ import { RUNTIME, pageDocument, pluginParts } from "./document.js";
2
+ import { toPath } from "./loader.js";
3
+ import { PACKAGE_DIR } from "./package.js";
4
+ import { pageSource, within } from "./site.js";
5
+ import { stripTypeScriptTypes } from "node:module";
6
+ import fs from "node:fs";
7
+ import path from "node:path";
8
+ // src/build.ts
9
+ const ENGINE_DIR = PACKAGE_DIR;
10
+ const TYPESCRIPT = /\.m?ts$/i;
11
+ const typeScriptModule = (file) => stripTypeScriptTypes(fs.readFileSync(file, "utf8"));
12
+ /** What a build leaves out unless told otherwise: what is for writing a page, not for reading it. */
13
+ const BUILD_SKIP = [
14
+ "form",
15
+ "chat",
16
+ "comments",
17
+ "draft"
18
+ ];
19
+ /** In the folder a build writes, so that a next build knows it may replace it. */
20
+ const BUILD_MARK = ".whiteboard-build";
21
+ async function build({ project, config, hooks: setUp, files: served }, options = {}) {
22
+ const cfg = await config();
23
+ if (cfg.errors.length) throw new Error(`the config failed to load: ${cfg.errors[0].message}`);
24
+ if (!cfg.pages) throw new Error("the config has no pages renderer");
25
+ const pages = cfg.pages;
26
+ const defined = (...xs) => xs.find((x) => x !== void 0);
27
+ const patterns = defined(options.pages, cfg.build.pages) ?? [];
28
+ const skip = new Set(defined(options.skip, cfg.build.skip) ?? BUILD_SKIP);
29
+ const outDir = path.resolve(project.dir, defined(options.out, cfg.build.out) ?? "site");
30
+ if (!patterns.length) throw new Error("nothing to build: name the pages, as globs from the project's folder (whiteboard build 'docs/**/*.md'), or set build.pages in the config");
31
+ const warnings = [];
32
+ const include = patterns.filter((p) => !p.startsWith("!"));
33
+ const exclude = patterns.filter((p) => p.startsWith("!")).map((p) => p.slice(1));
34
+ const files = [...new Set(include.flatMap((p) => fs.globSync(p, { cwd: project.dir }).map((f) => path.resolve(project.dir, f))))].filter((f) => /\.(md|markdown)$/i.test(f) && fs.statSync(f).isFile()).filter((f) => !exclude.some((g) => path.matchesGlob(path.relative(project.dir, f), g))).sort();
35
+ if (!files.length) throw new Error(`no markdown files match ${patterns.join(" ")}`);
36
+ const base = files.map(path.dirname).reduce((a, b) => {
37
+ while (!within(b, a)) a = path.dirname(a);
38
+ return a;
39
+ });
40
+ const outOf = (file) => {
41
+ const rel = path.relative(base, file);
42
+ return path.join(outDir, /^(readme|index)\.(md|markdown)$/i.test(path.basename(rel)) ? path.join(path.dirname(rel), "index.html") : rel.replace(/\.(md|markdown)$/i, ".html"));
43
+ };
44
+ const built = new Map(files.map((f) => [f, outOf(f)]));
45
+ if (fs.existsSync(outDir)) {
46
+ if (fs.readdirSync(outDir).length && !fs.existsSync(path.join(outDir, BUILD_MARK))) throw new Error(`${outDir} has files a build didn't write: give another folder (--out), or empty it`);
47
+ fs.rmSync(outDir, { recursive: true });
48
+ }
49
+ fs.mkdirSync(outDir, { recursive: true });
50
+ fs.writeFileSync(path.join(outDir, BUILD_MARK), "written by whiteboard build; the next build replaces this folder\n");
51
+ const copied = /* @__PURE__ */ new Map();
52
+ function copyAsset(file) {
53
+ const known = copied.get(file);
54
+ if (known) return known;
55
+ const rel = within(file, ENGINE_DIR) ? path.join("_", "whiteboard", path.relative(ENGINE_DIR, file)) : within(file, project.dir) ? path.join("_", "project", path.relative(project.dir, file)) : path.join("_", "files", path.basename(path.dirname(file)), path.basename(file));
56
+ const dest = path.join(outDir, TYPESCRIPT.test(rel) ? rel.replace(/\.m?ts$/i, ".js") : rel);
57
+ copied.set(file, dest);
58
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
59
+ if (TYPESCRIPT.test(file)) fs.writeFileSync(dest, typeScriptModule(file));
60
+ else fs.copyFileSync(file, dest);
61
+ if (/\.css$/i.test(file)) for (const m of fs.readFileSync(file, "utf8").matchAll(/url\(\s*['"]?([^'")]+)['"]?\s*\)/g)) {
62
+ const ref = m[1];
63
+ if (/^(data:|[a-z][a-z0-9+.-]*:|\/|#)/i.test(ref)) continue;
64
+ const target = path.resolve(path.dirname(file), ref.replace(/[?#].*$/, ""));
65
+ if (fs.existsSync(target) && fs.statSync(target).isFile()) copyAsset(target);
66
+ }
67
+ return dest;
68
+ }
69
+ /** a file beside the pages (an image), copied to its place below the site */
70
+ function copyBeside(file) {
71
+ if (!within(file, base)) return null;
72
+ const dest = path.join(outDir, path.relative(base, file));
73
+ if (!fs.existsSync(dest)) {
74
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
75
+ fs.copyFileSync(file, dest);
76
+ }
77
+ return dest;
78
+ }
79
+ const hooks = await setUp();
80
+ const result = {
81
+ out: outDir,
82
+ pages: [],
83
+ warnings
84
+ };
85
+ for (const [file, dest] of built) {
86
+ const here = path.dirname(dest);
87
+ const relTo = (target) => path.relative(here, target).split(path.sep).map(encodeURIComponent).join("/") || ".";
88
+ const assetHere = (ref) => relTo(copyAsset(toPath(ref)));
89
+ const urlFor = (abs) => {
90
+ for (const candidate of [abs, abs + ".md"]) {
91
+ if (!fs.existsSync(candidate)) continue;
92
+ if (fs.statSync(candidate).isDirectory()) {
93
+ const index = [...built.keys()].find((f) => path.dirname(f) === candidate && /^(readme|index)\.(md|markdown)$/i.test(path.basename(f)));
94
+ if (index) return relTo(built.get(index));
95
+ continue;
96
+ }
97
+ if (built.has(candidate)) return relTo(built.get(candidate));
98
+ if (/\.(md|markdown)$/i.test(candidate)) {
99
+ warnings.push(`${path.relative(project.dir, file)} links to ${path.relative(project.dir, candidate)}, which is not in the build`);
100
+ return null;
101
+ }
102
+ const copy = copyBeside(candidate);
103
+ if (copy) return relTo(copy);
104
+ warnings.push(`${path.relative(project.dir, file)} links to ${path.relative(project.dir, candidate)}, outside the built pages' folder`);
105
+ return null;
106
+ }
107
+ return null;
108
+ };
109
+ const id = within(file, project.pagesDir) ? path.relative(project.pagesDir, file).split(path.sep).join("/").replace(/\.(md|markdown)$/i, "") : "-/" + path.relative(project.dir, file).split(path.sep).join("/");
110
+ const stored = pageSource(id, file);
111
+ if (stored.frontmatterError) warnings.push(`${path.relative(project.dir, file)}: frontmatter: ${stored.frontmatterError}`);
112
+ const context = {
113
+ project: {
114
+ name: project.name,
115
+ dir: project.dir,
116
+ files: served()
117
+ },
118
+ asset: assetHere,
119
+ urlFor,
120
+ live: false,
121
+ skip
122
+ };
123
+ const rendered = await pages.render(stored, context);
124
+ const named = /^(readme|index)\.(md|markdown)$/i.test(path.basename(file)) ? path.basename(path.dirname(file)) : path.basename(file).replace(/\.(md|markdown)$/i, "");
125
+ const title = !rendered.title || rendered.title === id ? named : rendered.title;
126
+ const out = {
127
+ ...rendered,
128
+ title,
129
+ html: String(rendered.html ?? ""),
130
+ head: String(rendered.head ?? "")
131
+ };
132
+ const parts = await pluginParts(hooks, null, {
133
+ asset: assetHere,
134
+ messages: [],
135
+ leaveOut: skip,
136
+ failed: (message) => warnings.push(`${path.relative(project.dir, file)}: ${message}`)
137
+ });
138
+ const doc = pageDocument(stored, {
139
+ page: true,
140
+ out,
141
+ notices: []
142
+ }, parts, {
143
+ live: false,
144
+ base: "",
145
+ asset: assetHere,
146
+ runtime: assetHere(RUNTIME),
147
+ title: project.name
148
+ });
149
+ fs.mkdirSync(here, { recursive: true });
150
+ fs.writeFileSync(dest, doc);
151
+ result.pages.push({
152
+ file,
153
+ to: dest
154
+ });
155
+ }
156
+ if (![...built.values()].includes(path.join(outDir, "index.html"))) warnings.push("no README.md or index.md in the top folder: the site has no index.html");
157
+ return result;
158
+ }
159
+ export { build };
@@ -19,6 +19,10 @@
19
19
  --warn: light-dark(#6b4fc0, #b4a0ff);
20
20
  --bad: light-dark(#c4352b, #ff6f66);
21
21
  --info: light-dark(#1d5bbf, #82aaff);
22
+ /* what the reader sent, shown at once but not yet confirmed by the server (an event with
23
+ `sending`): grey, in whatever carries it (a button, a bubble, a comment's text). Confirmed, it
24
+ takes its usual colour (a button flashes --ok first); never got there, --bad */
25
+ --wb-sending: light-dark(#9a9fa7, #6c717a);
22
26
  --shadow: none;
23
27
 
24
28
  /* Type. The article is set in --serif at --text-size; everything in it (its margins, its
@@ -302,7 +306,7 @@ hr::after {
302
306
  letter-spacing: 0.3em;
303
307
  }
304
308
 
305
- /* section numbers hanging in the margin: 1, 1.1, 1.1.1, in italic oldstyle figures. Only the
309
+ /* section numbers hanging in the margin: 1, 1.1, 1.1.1, down to h6, in italic oldstyle figures. Only the
306
310
  page's own headings (direct children of main), so headings inside cards stay unnumbered. The
307
311
  number is an inline-block pulled into the margin, so it shares the heading's baseline. */
308
312
  #wb-main {
@@ -318,8 +322,16 @@ hr::after {
318
322
  }
319
323
  #wb-main > h4 {
320
324
  counter-increment: h4;
325
+ counter-reset: h5;
321
326
  }
322
- #wb-main > :is(h2, h3, h4)::before {
327
+ #wb-main > h5 {
328
+ counter-increment: h5;
329
+ counter-reset: h6;
330
+ }
331
+ #wb-main > h6 {
332
+ counter-increment: h6;
333
+ }
334
+ #wb-main > :is(h2, h3, h4, h5, h6)::before {
323
335
  display: inline-block;
324
336
  box-sizing: content-box;
325
337
  width: 64px;
@@ -347,12 +359,18 @@ hr::after {
347
359
  content: counter(h2) '.' counter(h3) '.' counter(h4);
348
360
  font-size: 1.3em;
349
361
  }
362
+ #wb-main > h5::before {
363
+ content: counter(h2) '.' counter(h3) '.' counter(h4) '.' counter(h5);
364
+ }
365
+ #wb-main > h6::before {
366
+ content: counter(h2) '.' counter(h3) '.' counter(h4) '.' counter(h5) '.' counter(h6);
367
+ }
350
368
  /* no margin to hang them in: set the number in front of the heading, and indent less */
351
369
  @media (max-width: 860px) {
352
370
  main {
353
371
  --indent: 0px;
354
372
  }
355
- #wb-main > :is(h2, h3, h4)::before {
373
+ #wb-main > :is(h2, h3, h4, h5, h6)::before {
356
374
  width: auto;
357
375
  margin-left: 0;
358
376
  margin-right: 0.6em;
@@ -682,14 +700,32 @@ form.wb-form[data-wb-folded] {
682
700
  gap: 0;
683
701
  }
684
702
  /* named forms: the core marks where each stands (data-wb-open, -pending, -decided); this is how that looks */
685
- form[data-wb-state='sending'] {
686
- opacity: 0.7;
687
- }
688
- /* sending: the form's buttons can't send twice */
689
- form[data-wb-state='sending'] button {
690
- pointer-events: none;
703
+ /* the button a form was sent with says how that goes (forms.ts): the answer is the form's already
704
+ (optimistic), the button grey while the server hasn't confirmed it, green a moment once it has,
705
+ red ("Try again") when it never got there. The form stays usable: the same answer isn't sent twice. !important: a send's
706
+ state outranks however a plugin or the project styles its buttons (.wb-primary) */
707
+ form button[data-wb-send] {
708
+ transition:
709
+ background-color 0.2s,
710
+ border-color 0.2s,
711
+ color 0.2s;
712
+ }
713
+ form button[data-wb-send='sending'] {
714
+ background: var(--wb-sending) !important;
715
+ border-color: var(--wb-sending) !important;
716
+ color: var(--bg) !important;
691
717
  cursor: progress;
692
718
  }
719
+ form button[data-wb-send='sent'] {
720
+ background: var(--ok) !important;
721
+ border-color: var(--ok) !important;
722
+ color: var(--bg) !important;
723
+ }
724
+ form button[data-wb-send='failed'] {
725
+ background: var(--bad) !important;
726
+ border-color: var(--bad) !important;
727
+ color: var(--bg) !important;
728
+ }
693
729
  /* just sent: a short green flash, and a line saying what happened, for a few seconds */
694
730
  @keyframes wb-sent {
695
731
  from {
@@ -702,23 +738,16 @@ form[data-wb-state='sending'] button {
702
738
  form[data-wb-just-sent] {
703
739
  animation: wb-sent 1.4s ease-out;
704
740
  }
705
- form.wb-form[data-wb-state='failed']::before {
706
- content: "couldn't send it: try again";
707
- display: block;
708
- margin-bottom: var(--space-md);
709
- color: var(--bad);
710
- font: var(--weight-strong) var(--ui-tiny)/var(--ui-line-height) var(--sans);
711
- }
712
- form[data-wb-sent]:not(.wb-form)::after {
713
- content: '✓ sent ' attr(data-wb-sent);
714
- display: block;
715
- margin-top: var(--space-sm);
716
- color: var(--ok);
717
- font: var(--ui-small) var(--sans);
741
+ /* what a sent answer came to, beside the form's last send button (forms.ts): waiting for the agent
742
+ (muted), couldn't send (red) */
743
+ .wb-form-status {
744
+ margin-left: var(--space-md);
745
+ color: var(--muted);
746
+ font: var(--ui-tiny)/var(--ui-line-height) var(--sans);
718
747
  }
719
- form[data-wb-state='failed']:not(.wb-form)::after {
720
- content: 'failed to send';
748
+ .wb-form-status[data-kind='bad'] {
721
749
  color: var(--bad);
750
+ font-weight: var(--weight-strong);
722
751
  }
723
752
 
724
753
  /* a question the reader hasn't answered, or one the agent asks again: outlined, without a label of
@@ -730,21 +759,25 @@ form[data-wb-open] {
730
759
  }
731
760
  /* the outline sets it off: the form's rules above and below (its borders, which the outline would
732
761
  enclose) step aside meanwhile */
733
- form.wb-form[data-wb-open] {
762
+ form.wb-form:is([data-wb-open], [data-wb-pending], [data-wb-state='sending'], [data-wb-state='failed']) {
734
763
  border-top-color: transparent;
735
764
  border-bottom-color: transparent;
736
765
  }
737
- form[data-wb-pending]::before {
738
- content: 'sent · waiting for the agent to pick it up';
739
- display: block;
740
- margin-bottom: var(--space-md);
741
- color: var(--muted);
742
- font: var(--ui-tiny)/var(--ui-line-height) var(--sans);
766
+ /* the outline speaks the language of a send too: grey while the answer is on its way (it is the
767
+ form's already, so no longer outlined as unanswered) and while it waits for the agent to pick it
768
+ up, red when it never got there; gone once the agent took it (decided) */
769
+ form:is([data-wb-open], [data-wb-pending], [data-wb-state='sending'], [data-wb-state='failed']) {
770
+ transition: outline-color 0.2s;
743
771
  }
744
- form[data-wb-pending][data-wb-just-sent]::before {
745
- content: '✓ sent · waiting for the agent to pick it up';
746
- color: var(--ok);
747
- font-weight: var(--weight-strong);
772
+ form:is([data-wb-state='sending'], [data-wb-pending]) {
773
+ outline: 2px solid var(--wb-sending);
774
+ outline-offset: 10px;
775
+ border-radius: var(--radius-sm);
776
+ }
777
+ form[data-wb-state='failed'] {
778
+ outline: 2px solid var(--bad);
779
+ outline-offset: 10px;
780
+ border-radius: var(--radius-sm);
748
781
  }
749
782
 
750
783
  /* decided: the agent picked up the latest answer; the form folds into one line quoting it */
@@ -1236,3 +1269,9 @@ main:has(+ .wb-orphans:not([hidden])) {
1236
1269
  :root[data-wb-window-maximized] {
1237
1270
  overflow: hidden;
1238
1271
  }
1272
+ @media (prefers-reduced-motion: reduce) {
1273
+ form button[data-wb-send],
1274
+ form:is([data-wb-open], [data-wb-pending], [data-wb-state='sending'], [data-wb-state='failed']) {
1275
+ transition: none;
1276
+ }
1277
+ }
@@ -851,15 +851,22 @@ async function load() {
851
851
  list.replaceChildren(...rest.length && mine.length ? [heading("here")] : [], ...mine.map(item), ...rest.flatMap((g) => [heading(g.label), ...g.pages.map(item)]));
852
852
  }
853
853
  let timer = 0;
854
+ let stale = false;
854
855
  const reload = () => {
855
856
  clearTimeout(timer);
857
+ if (document.hidden) return void (stale = true);
856
858
  timer = window.setTimeout(load, 100);
857
859
  };
860
+ document.addEventListener("visibilitychange", () => {
861
+ if (document.hidden || !stale) return;
862
+ stale = false;
863
+ reload();
864
+ });
858
865
  function trackElsewhere(root, handle, core) {
859
866
  current$1 = handle.id;
860
867
  const nav = root.querySelector(".wb-nav");
861
868
  if (!nav) return;
862
- nav.querySelector(".wb-theme")?.before(box);
869
+ (nav.querySelector("[data-wb-slot=header]") ?? nav.querySelector(".wb-theme"))?.before(box);
863
870
  toggle.addEventListener("click", () => open(!!list.hidden));
864
871
  document.addEventListener("click", (e) => {
865
872
  if (!box.contains(e.target)) open(false);
@@ -874,7 +881,7 @@ function trackElsewhere(root, handle, core) {
874
881
  core.listeners.on(reload);
875
882
  handle.onChange(reload);
876
883
  load();
877
- if (base) setInterval(reload, 5e3);
884
+ if (base) document.addEventListener("wb-elsewhere", reload);
878
885
  }
879
886
  // src/client/page/waiting.ts
880
887
  const ORDER = STATES;
@@ -1074,10 +1081,14 @@ function trackWaiting(root, core) {
1074
1081
  if (form.dataset.wbState === "sending") return;
1075
1082
  sent.disconnect();
1076
1083
  if (form.dataset.wbState !== "sent") return;
1077
- if (fallback) api$1.toast("Sent. No agent waits on this page; the fallback agent picks it up.", { name: "sent" });
1078
- else api$1.toast("Sent, but no agent is listening right now. Your answer stays in the log until one checks this page.", {
1084
+ if (fallback) api$1.toast("Sent · to the fallback agent", {
1085
+ name: "sent",
1086
+ details: "No agent waits on this page, so the fallback agent picks it up."
1087
+ });
1088
+ else api$1.toast("Sent · no agent listening", {
1079
1089
  kind: "warn",
1080
- name: "sent"
1090
+ name: "sent",
1091
+ details: "Your answer stays in the log until an agent checks this page."
1081
1092
  });
1082
1093
  });
1083
1094
  sent.observe(form, {
@@ -0,0 +1,93 @@
1
+ import { html, raw } from "./html.js";
2
+ import { PACKAGE_DIR } from "./package.js";
3
+ import _taggedTemplateLiteral from "../_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js";
4
+ import path from "node:path";
5
+ // src/document.ts
6
+ var _templateObject;
7
+ /** The browser runtime, built by build.ts. */
8
+ const RUNTIME = path.join(PACKAGE_DIR, "dist", "client", "runtime.js");
9
+ /** The page in the browser (markdown live: morphing, blocks' islands, forms' marks), built by build.ts. */
10
+ const PAGE_CLIENT = path.join(PACKAGE_DIR, "dist", "src", "client", "page", "main.js");
11
+ /** The slots at the end of <body>, in the order they are there; `corner` is the page's dock. */
12
+ const BODY_SLOTS = [
13
+ "aside",
14
+ "bottom",
15
+ "overlay"
16
+ ];
17
+ /**
18
+ * What the plugins add to a page: each one's markup for the slots (`slots`, asked with the browser's request, or
19
+ * null for a page no browser asked for), and its browser module. One that failed to start or fails
20
+ * here is left out and told to `failed`; so are those in `leaveOut` (a build's skip), silently.
21
+ */
22
+ async function pluginParts(hooks, req, { asset, messages, leaveOut = /* @__PURE__ */ new Set(), failed }) {
23
+ const included = hooks.filter((h) => !leaveOut.has(h.name));
24
+ for (const h of included) if (h.error) failed(`plugin "${h.name}" failed to start: ${h.error.message}`);
25
+ const working = included.filter((h) => !h.error);
26
+ const parts = await Promise.all(working.map(async (h) => {
27
+ try {
28
+ return await h.slots?.(req) ?? null;
29
+ } catch (err) {
30
+ failed(`plugin "${h.name}" failed on this page: ${err.message}`);
31
+ return null;
32
+ }
33
+ }));
34
+ const slotted = (slot) => working.flatMap((h, i) => {
35
+ const markup = parts[i]?.[slot];
36
+ return markup == null ? [] : [{
37
+ name: h.name,
38
+ html: raw(String(markup))
39
+ }];
40
+ });
41
+ return {
42
+ head: parts.map((x) => raw(String(x?.head ?? ""))),
43
+ slots: {
44
+ aside: slotted("aside"),
45
+ bottom: slotted("bottom"),
46
+ overlay: slotted("overlay"),
47
+ corner: slotted("corner")
48
+ },
49
+ clients: working.filter((h) => h.client).map((h) => ({
50
+ name: h.name,
51
+ client: asset(h.client)
52
+ })),
53
+ messages
54
+ };
55
+ }
56
+ /** A stored page's frontmatter, for the browser (PageHandle.frontmatter): a markdown page's, else none. */
57
+ const frontmatterOf = (page) => page.kind === "page" ? page.frontmatter : {};
58
+ /** JSON made safe inside a <script>. */
59
+ const json = (s) => JSON.stringify(s).replace(/</g, "\\u003c");
60
+ const DOCK_STYLE = "#wb-dock{position:fixed;right:16px;bottom:calc(16px + var(--wb-inset-bottom,0px));z-index:30;display:flex;flex-direction:column;align-items:flex-end;gap:6px}#wb-dock:not(:has(> :not(:empty))){display:none}[data-wb-slot]:not(#wb-dock),[data-wb-slot]>[data-plugin]{display:contents}.wb-toasts{display:flex;flex-direction:column;align-items:flex-end;gap:6px}.wb-toasts:empty{display:none}.wb-toast{display:inline-flex;align-items:center;gap:8px;cursor:pointer;padding:5px 12px;border-radius:99px;font:600 .8rem/1.4 var(--sans,system-ui,sans-serif);background:var(--accent,#1d5bbf);color:var(--accent-text,#fff);border:1px solid transparent;box-shadow:0 2px 10px rgb(0 0 0/.18)}.wb-toast[hidden]{display:none}.wb-toast[data-kind=warn]{background:var(--warn,#6b4fc0)}.wb-toast[data-kind=alert]{background:var(--bad,#c4352b);color:#fff}@keyframes wb-flash{from{outline-color:var(--accent,#1d5bbf)}to{outline-color:transparent}}.wb-flash{outline:3px solid transparent;outline-offset:6px;border-radius:4px;animation:wb-flash 2s ease-out}.wb-toast-dismiss{margin-right:-4px;padding:0 4px;border-left:1px solid rgb(255 255 255/.35);opacity:.75}.wb-toast-dismiss:hover{opacity:1}.wb-toast{max-width:min(24rem,calc(100vw - 32px));text-align:left}.wb-toast-more{margin:0 -4px 0 -2px;padding:0 4px;opacity:.75}.wb-toast-more:hover{opacity:1}.wb-toast[data-open]{flex-wrap:wrap;border-radius:12px}.wb-toast-details{flex-basis:100%;padding:2px 0 3px;font-weight:400;cursor:text}.wb-toast-details[hidden]{display:none}";
61
+ /** Each plugin's own element in a slot, with what it rendered there. */
62
+ const slotted = (list) => list.map((x) => html`<div data-plugin="${x.name}">${x.html}</div>`);
63
+ /** A page as a document: the render, the plugins' parts, and the handoff for the browser runtime. */
64
+ function pageDocument(page, { page: rendered, out, notices }, parts, o) {
65
+ const handoff = {
66
+ id: page.id,
67
+ kind: page.kind,
68
+ live: o.live,
69
+ base: o.base,
70
+ client: rendered ? o.asset(PAGE_CLIENT) : null,
71
+ data: out.data ?? null,
72
+ frontmatter: frontmatterOf(page),
73
+ plugins: parts.clients,
74
+ messages: parts.messages,
75
+ ...o.stamps && { stamps: o.stamps }
76
+ };
77
+ return String(html(_templateObject || (_templateObject = _taggedTemplateLiteral([
78
+ "<!doctype html>\n <html lang=\"en\">\n <head>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\" />\n <title>",
79
+ "</title>\n <style>\n ",
80
+ "\n </style>\n ",
81
+ "",
82
+ "\n </head>\n <body data-page=\"",
83
+ "\" data-kind=\"",
84
+ "\">\n ",
85
+ "\n <div id=\"wb-root\">",
86
+ "</div>\n <div id=\"wb-dock\" data-wb-slot=\"corner\">",
87
+ "</div>\n <script type=\"application/json\" id=\"wb-page\">\n ",
88
+ "\n <\/script>\n <script type=\"module\" src=\"",
89
+ "\"><\/script>\n ",
90
+ "\n </body>\n </html>"
91
+ ])), out.title || page.id || o.title, DOCK_STYLE, raw(String(out.head ?? "")), parts.head, page.id, page.kind, notices.map((n) => html`<pre class="wb-error" role="alert">${n}</pre>`), raw(String(out.html)), slotted(parts.slots.corner), raw(json(handoff)), o.runtime, BODY_SLOTS.map((name) => html`<div data-wb-slot="${name}">${slotted(parts.slots[name])}</div>`)));
92
+ }
93
+ export { PAGE_CLIENT, RUNTIME, frontmatterOf, pageDocument, pluginParts };
@@ -1,12 +1,9 @@
1
- import { fromReader } from "./status.js";
1
+ import { CLAIM, PICKED, fromReader, pickedUp } from "./status.js";
2
2
  import fs from "node:fs";
3
3
  import crypto from "node:crypto";
4
4
  // src/events.ts
5
- /**
6
- * The type of the event `whiteboard new` appends when an agent session creates a page:
7
- * `{ session }`. The colon keeps it apart from form names, which are event types too.
8
- */
9
- const CLAIM = "whiteboard:claim";
5
+ /** An event id as the log has them: the time in base 36, a dash, random hex. */
6
+ const isEventId = (id) => typeof id === "string" && /^[0-9a-z]{6,12}-[0-9a-f]{6,32}$/.test(id);
10
7
  /**
11
8
  * Append one event; `action` is the button that submitted a form, null for anything else. `from`
12
9
  * is set only for events the command line writes ('cli'), so that waits skip them and pages can
@@ -14,7 +11,7 @@ const CLAIM = "whiteboard:claim";
14
11
  */
15
12
  function appendEvent(file, input, from = null) {
16
13
  const event = {
17
- id: `${Date.now().toString(36)}-${crypto.randomBytes(3).toString("hex")}`,
14
+ id: isEventId(input.id) ? input.id : `${Date.now().toString(36)}-${crypto.randomBytes(3).toString("hex")}`,
18
15
  ts: (/* @__PURE__ */ new Date()).toISOString(),
19
16
  page: String(input.page ?? ""),
20
17
  type: String(input.type ?? ""),
@@ -220,12 +217,6 @@ function matches(event, { page, type, exact } = {}) {
220
217
  if (type && event.type !== type) return false;
221
218
  return true;
222
219
  }
223
- /** The type of the event that says another event was handed to an agent: `{ event: <its id> }`. */
224
- const PICKED = "whiteboard:picked";
225
- /** The ids of the events that were picked up. Only the command line's picks count. */
226
- function pickedUp(events) {
227
- return new Set(events.filter((e) => e.type === "whiteboard:picked" && !fromReader(e)).map((e) => String(e.data?.event)));
228
- }
229
220
  /** Record that `event` was handed to an agent: from then on it counts as picked up. */
230
221
  function pick(file, event) {
231
222
  return appendEvent(file, {
@@ -251,7 +242,7 @@ const forAgent = (handedOut = /* @__PURE__ */ new Set()) => (type) => !type.incl
251
242
  * `follow`, or when `signal` aborts.
252
243
  */
253
244
  function handOut(file, filter, onEvent, options = {}) {
254
- const { count = Infinity, follow: following = true, interval, signal, forAgent: isForAgent = forAgent(), skip, recheck } = options;
245
+ const { count = Infinity, follow: following = true, interval, signal, forAgent: isForAgent = forAgent(), skip, recheck, unpicked = false } = options;
255
246
  return new Promise((resolve) => {
256
247
  let got = 0;
257
248
  let stop = () => {};
@@ -268,6 +259,7 @@ function handOut(file, filter, onEvent, options = {}) {
268
259
  };
269
260
  const left = /* @__PURE__ */ new Map();
270
261
  const offer = (e) => {
262
+ if (unpicked && pickedUp(read(file).events).has(e.id)) return true;
271
263
  if (skip?.(e)) {
272
264
  left.set(e.id, e);
273
265
  return true;
@@ -298,4 +290,4 @@ function handOut(file, filter, onEvent, options = {}) {
298
290
  }, { interval });
299
291
  });
300
292
  }
301
- export { CLAIM, PICKED, appendEvent, editEvent, follow, forAgent, fromReader, handOut, matches, parseLines, pick, pickedUp, prune, read, readEvents, rewrite };
293
+ export { CLAIM, PICKED, appendEvent, editEvent, follow, forAgent, fromReader, handOut, isEventId, matches, parseLines, pick, pickedUp, prune, read, readEvents, rewrite };