@bigmistqke/whiteboard 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +609 -2
  3. package/bin/whiteboard.ts +431 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  5. package/dist/bin/whiteboard.js +471 -0
  6. package/dist/client/runtime.js +391 -0
  7. package/dist/define.js +7 -0
  8. package/dist/plugins/callout/README.md +13 -0
  9. package/dist/plugins/callout/index.js +27 -0
  10. package/dist/plugins/callout/style.css +12 -0
  11. package/dist/plugins/chat/README.md +45 -0
  12. package/dist/plugins/chat/client.js +3 -0
  13. package/dist/plugins/chat/index.js +46 -0
  14. package/dist/plugins/chat/style.css +17 -0
  15. package/dist/plugins/claude-code/index.js +120 -0
  16. package/dist/plugins/code/README.md +42 -0
  17. package/dist/plugins/code/diff.js +64 -0
  18. package/dist/plugins/code/index.js +77 -0
  19. package/dist/plugins/code/style.css +29 -0
  20. package/dist/plugins/comments/client.js +10 -0
  21. package/dist/plugins/comments/index.js +51 -0
  22. package/dist/plugins/comments/style.css +32 -0
  23. package/dist/plugins/details/README.md +17 -0
  24. package/dist/plugins/details/index.js +17 -0
  25. package/dist/plugins/details/style.css +3 -0
  26. package/dist/plugins/form/README.md +111 -0
  27. package/dist/plugins/form/index.js +102 -0
  28. package/dist/plugins/form/style.css +41 -0
  29. package/dist/plugins/gallery/README.md +30 -0
  30. package/dist/plugins/gallery/client.js +1 -0
  31. package/dist/plugins/gallery/index.js +74 -0
  32. package/dist/plugins/gallery/style.css +27 -0
  33. package/dist/plugins/grid/README.md +33 -0
  34. package/dist/plugins/grid/index.js +33 -0
  35. package/dist/plugins/grid/style.css +25 -0
  36. package/dist/plugins/index.js +26 -0
  37. package/dist/plugins/mermaid/README.md +21 -0
  38. package/dist/plugins/mermaid/index.js +34 -0
  39. package/dist/plugins/mermaid/style.css +16 -0
  40. package/dist/plugins/quiet/README.md +26 -0
  41. package/dist/plugins/quiet/index.js +17 -0
  42. package/dist/plugins/terminal/client.js +41 -0
  43. package/dist/plugins/terminal/host.js +332 -0
  44. package/dist/plugins/terminal/index.js +171 -0
  45. package/dist/plugins/terminal/modes.js +32 -0
  46. package/dist/plugins/terminal/server.js +256 -0
  47. package/dist/plugins/terminal/sizes.js +57 -0
  48. package/dist/plugins/terminal/style.css +357 -0
  49. package/dist/solid/index.js +35 -0
  50. package/dist/src/auth.js +54 -0
  51. package/dist/src/client/page/base.css +274 -0
  52. package/dist/src/client/page/main.js +1004 -0
  53. package/dist/src/config.js +76 -0
  54. package/dist/src/events.js +162 -0
  55. package/dist/src/html.js +34 -0
  56. package/dist/src/load.js +77 -0
  57. package/dist/src/loader.js +120 -0
  58. package/dist/src/package.js +33 -0
  59. package/dist/src/pages/blocks.js +49 -0
  60. package/dist/src/pages/index.js +14 -0
  61. package/dist/src/pages/markdown.js +307 -0
  62. package/dist/src/pages/render.js +257 -0
  63. package/dist/src/plugins.js +238 -0
  64. package/dist/src/running.js +24 -0
  65. package/dist/src/serve.js +71 -0
  66. package/dist/src/server.js +700 -0
  67. package/dist/src/status.js +43 -0
  68. package/package.json +131 -4
  69. package/plugins/callout/README.md +13 -0
  70. package/plugins/callout/index.ts +25 -0
  71. package/plugins/callout/style.css +12 -0
  72. package/plugins/chat/README.md +45 -0
  73. package/plugins/chat/autosize.ts +101 -0
  74. package/plugins/chat/client.tsx +81 -0
  75. package/plugins/chat/index.ts +61 -0
  76. package/plugins/chat/style.css +17 -0
  77. package/plugins/claude-code/index.ts +136 -0
  78. package/plugins/code/README.md +42 -0
  79. package/plugins/code/diff.ts +58 -0
  80. package/plugins/code/index.ts +94 -0
  81. package/plugins/code/style.css +29 -0
  82. package/plugins/comments/client.ts +448 -0
  83. package/plugins/comments/index.ts +52 -0
  84. package/plugins/comments/style.css +32 -0
  85. package/plugins/details/README.md +17 -0
  86. package/plugins/details/index.ts +21 -0
  87. package/plugins/details/style.css +3 -0
  88. package/plugins/form/README.md +111 -0
  89. package/plugins/form/index.ts +104 -0
  90. package/plugins/form/style.css +41 -0
  91. package/plugins/gallery/README.md +30 -0
  92. package/plugins/gallery/client.tsx +64 -0
  93. package/plugins/gallery/index.ts +67 -0
  94. package/plugins/gallery/style.css +27 -0
  95. package/plugins/grid/README.md +33 -0
  96. package/plugins/grid/index.ts +36 -0
  97. package/plugins/grid/style.css +25 -0
  98. package/plugins/index.ts +19 -0
  99. package/plugins/mermaid/README.md +21 -0
  100. package/plugins/mermaid/index.ts +39 -0
  101. package/plugins/mermaid/style.css +16 -0
  102. package/plugins/quiet/README.md +26 -0
  103. package/plugins/quiet/index.ts +27 -0
  104. package/plugins/terminal/client.ts +317 -0
  105. package/plugins/terminal/host.ts +326 -0
  106. package/plugins/terminal/index.ts +138 -0
  107. package/plugins/terminal/modes.ts +30 -0
  108. package/plugins/terminal/server.ts +215 -0
  109. package/plugins/terminal/sizes.ts +60 -0
  110. package/plugins/terminal/style.css +71 -0
  111. package/skills/whiteboard/SKILL.md +292 -0
  112. package/solid/index.tsx +56 -0
  113. package/src/auth.ts +66 -0
  114. package/src/client/forms.ts +186 -0
  115. package/src/client/page/base.css +274 -0
  116. package/src/client/page/fresh.ts +205 -0
  117. package/src/client/page/main.ts +249 -0
  118. package/src/client/page/orphans.ts +46 -0
  119. package/src/client/page/theme.ts +32 -0
  120. package/src/client/page/view.ts +9 -0
  121. package/src/client/page/waiting.ts +109 -0
  122. package/src/client/runtime.ts +170 -0
  123. package/src/config.ts +96 -0
  124. package/src/define.ts +546 -0
  125. package/src/events.ts +194 -0
  126. package/src/html.ts +40 -0
  127. package/src/load.ts +89 -0
  128. package/src/loader.ts +109 -0
  129. package/src/package.ts +39 -0
  130. package/src/pages/blocks.ts +67 -0
  131. package/src/pages/index.ts +36 -0
  132. package/src/pages/markdown.ts +276 -0
  133. package/src/pages/render.ts +196 -0
  134. package/src/plugins.ts +197 -0
  135. package/src/running.ts +22 -0
  136. package/src/serve.ts +52 -0
  137. package/src/server.ts +644 -0
  138. package/src/status.ts +69 -0
@@ -0,0 +1,170 @@
1
+ // The core in the browser: the page's events, the live channel, and handing the page to the page
2
+ // module (./page: markdown live, the blocks' islands) and the plugins' modules. When the config failed
3
+ // to load, the page is its raw text: the server's HTML, replaced when it changes.
4
+ import type { Api, PluginMount, Listener, PageHandle, RenderedPage, StoredPage, WbEvent } from '../define'
5
+ import { html } from '../html.ts'
6
+ import { formsApi, trackForms } from './forms'
7
+
8
+ interface Handoff {
9
+ id: string; kind: StoredPage['kind']; data: unknown
10
+ /** the page module; null for raw text */
11
+ client: string | null
12
+ /** the plugins' browser modules */
13
+ plugins: { name: string; client: string }[]
14
+ }
15
+
16
+ const handoff: Handoff = JSON.parse(document.getElementById('wb-page')!.textContent!)
17
+ const root = document.getElementById('wb-root')!
18
+ const page = handoff.id
19
+ const isDir = handoff.kind === 'pages' || handoff.kind === 'folder'
20
+
21
+ // ---- events ------------------------------------------------------------------------------------
22
+
23
+ let events: WbEvent[] = []
24
+ const eventListeners = new Set<(events: WbEvent[]) => void>()
25
+ function setEvents(next: WbEvent[]) {
26
+ events = next
27
+ for (const fn of eventListeners) fn(events)
28
+ }
29
+
30
+ async function loadEvents() {
31
+ const res = await fetch(`/api/events?page=${encodeURIComponent(page)}`)
32
+ const all: WbEvent[] = await res.json()
33
+ setEvents(all.filter(e => e.page === page))
34
+ }
35
+
36
+ async function append(type: string, data: Record<string, unknown> = {}, action: string | null = null): Promise<WbEvent> {
37
+ const res = await fetch('/api/events', {
38
+ method: 'POST',
39
+ headers: { 'content-type': 'application/json' },
40
+ body: JSON.stringify({ page, type, action, data }),
41
+ })
42
+ if (!res.ok) throw new Error(await res.text())
43
+ const saved: WbEvent = await res.json()
44
+ if (!events.some(e => e.id === saved.id)) setEvents([...events, saved])
45
+ return saved
46
+ }
47
+
48
+ let listeners: Listener[] = []
49
+ const listenerListeners = new Set<(listeners: Listener[]) => void>()
50
+
51
+ async function loadListeners() {
52
+ listeners = await (await fetch(`/api/listeners?page=${encodeURIComponent(page)}`)).json()
53
+ for (const fn of listenerListeners) fn(listeners)
54
+ }
55
+
56
+ const renderedListeners = new Set<() => void>()
57
+
58
+ // ---- the corner and the title ------------------------------------------------------------------
59
+
60
+ // the server puts it in the page; a page from a server older than this runtime has none
61
+ const corner = document.getElementById('wb-dock') ?? document.body.appendChild(Object.assign(document.createElement('div'), { id: 'wb-dock' }))
62
+ let baseTitle = document.title
63
+ const badges = new Map<string, { text: string; order: number; since: number }>()
64
+ let badgeCount = 0
65
+ function writeTitle() {
66
+ const list = [...badges.values()].sort((a, b) => a.order - b.order || a.since - b.since)
67
+ document.title = `${list.map(b => `(${b.text}) `).join('')}${baseTitle}`
68
+ }
69
+
70
+ const api: Api = {
71
+ page,
72
+ events: () => events,
73
+ onEvents(fn) { eventListeners.add(fn); return () => eventListeners.delete(fn) },
74
+ last: type => events.findLast(e => e.type === type),
75
+ pickedUp: id => !!events.find(e => e.id === id)?.pickedUp,
76
+ append,
77
+ listeners: () => listeners,
78
+ onListeners(fn) { listenerListeners.add(fn); return () => listenerListeners.delete(fn) },
79
+ rendered() { for (const fn of renderedListeners) fn() },
80
+ onRendered(fn) { renderedListeners.add(fn); return () => renderedListeners.delete(fn) },
81
+ corner: () => corner,
82
+ badge(key, text, order = 0) {
83
+ if (text == null) badges.delete(key)
84
+ else badges.set(key, { text, order, since: badges.get(key)?.since ?? badgeCount++ })
85
+ writeTitle()
86
+ },
87
+ title(title) { baseTitle = title; writeTitle() },
88
+ ...formsApi,
89
+ }
90
+
91
+ // ---- the page ----------------------------------------------------------------------------------
92
+
93
+ const changeListeners = new Set<() => void>()
94
+
95
+ async function getJson<T>(url: string): Promise<T> {
96
+ const res = await fetch(url)
97
+ if (!res.ok) throw new Error(`${url}: ${res.status}`)
98
+ return res.json()
99
+ }
100
+
101
+ const handle: PageHandle = {
102
+ id: page,
103
+ kind: handoff.kind,
104
+ data: handoff.data,
105
+ onChange(fn) { changeListeners.add(fn); return () => changeListeners.delete(fn) },
106
+ render: () => getJson<RenderedPage>(`/api/render?page=${encodeURIComponent(page)}`),
107
+ stored: () => getJson<StoredPage>(`/api/page?page=${encodeURIComponent(page)}`),
108
+ }
109
+
110
+ // raw text: the server's HTML is the page, replaced when the page changes
111
+ if (!handoff.client) handle.onChange(async () => { root.innerHTML = (await handle.render()).html; api.rendered() })
112
+
113
+ // ---- live channel ------------------------------------------------------------------------------
114
+
115
+ function connect() {
116
+ const es = new EventSource(`/api/watch?page=${encodeURIComponent(page)}${isDir ? '&dir' : ''}`)
117
+ es.onopen = () => {
118
+ document.documentElement.dataset.wbLive = ''
119
+ // after a server restart: who is listening now, and what was answered meanwhile
120
+ loadListeners()
121
+ loadEvents()
122
+ }
123
+ es.onerror = () => { delete document.documentElement.dataset.wbLive }
124
+ es.addEventListener('change', () => { for (const fn of changeListeners) fn() })
125
+ es.addEventListener('events', () => loadEvents())
126
+ es.addEventListener('listeners', () => loadListeners())
127
+ // browser code changed; the page keeps what matters across a reload itself. Reload only once
128
+ // the server answers: reloading while it restarts would leave the browser's error page, which
129
+ // has nothing left in it to recover with.
130
+ es.addEventListener('reload', () => {
131
+ const go = async () => {
132
+ try { if ((await fetch('/api/health', { cache: 'no-store' })).ok) return location.reload() } catch { /* not up yet */ }
133
+ setTimeout(go, 500)
134
+ }
135
+ go()
136
+ })
137
+ es.addEventListener('style', () => {
138
+ for (const link of document.querySelectorAll<HTMLLinkElement>('link[rel=stylesheet]')) {
139
+ const url = new URL(link.href)
140
+ if (url.origin !== location.origin) continue
141
+ url.searchParams.set('t', String(Date.now()))
142
+ link.href = url.pathname + url.search
143
+ }
144
+ })
145
+ }
146
+
147
+ await Promise.all([loadEvents(), loadListeners()])
148
+ if (handoff.client) {
149
+ try {
150
+ const mount: PluginMount = (await import(handoff.client)).default
151
+ await mount(root, handle, api)
152
+ } catch (err) {
153
+ console.error('page:', err)
154
+ root.insertAdjacentHTML('afterbegin', String(html`<pre class="wb-error" role="alert">the page failed to start: ${(err as Error).message}</pre>`))
155
+ }
156
+ }
157
+ // named forms, once the page is on the page
158
+ trackForms(root, page, api)
159
+ // the plugins' modules after the page, so that what they work on is there; one failing leaves the others
160
+ await Promise.all((handoff.plugins ?? []).map(async ext => {
161
+ try {
162
+ const mount: PluginMount = (await import(ext.client)).default
163
+ if (typeof mount !== 'function') throw new Error('the client module must export a mount(root, page, api) function as default')
164
+ await mount(root, handle, api)
165
+ } catch (err) {
166
+ console.error(`plugin ${ext.name}:`, err)
167
+ root.insertAdjacentHTML('afterbegin', String(html`<pre class="wb-error" role="alert">plugin "${ext.name}" failed in the browser: ${(err as Error).message}</pre>`))
168
+ }
169
+ }))
170
+ connect()
package/src/config.ts ADDED
@@ -0,0 +1,96 @@
1
+ // The project, its files, and page ids.
2
+ //
3
+ // A project is a folder (usually a repository root) with a `.whiteboard/` folder in it.
4
+ // Everything whiteboard keeps lives in that folder:
5
+ // config.ts what the project changes from the defaults (optional)
6
+ // pages/ the pages, served at /<page>
7
+ // events.ndjson the events: form submits, chat messages, comments, and what agents picked up
8
+ // token the secret for what runs code (the terminal), made on first use
9
+ // server.json the port and process of this project's running server
10
+ // server.log that server's output
11
+ // Each project runs its own server; nothing is shared between projects.
12
+ import path from 'node:path'
13
+ import fs from 'node:fs'
14
+
15
+ /** A project: its folders and files. */
16
+ export interface Project {
17
+ /** the project folder */
18
+ dir: string
19
+ /** its .whiteboard folder */
20
+ wb: string
21
+ readonly configFile: string | null
22
+ pagesDir: string
23
+ eventsFile: string
24
+ serverFile: string
25
+ logFile: string
26
+ readonly name: string
27
+ }
28
+
29
+ export const CONFIG_NAMES = ['config.ts', 'config.mts', 'config.js', 'config.mjs']
30
+ /** the port a project's server tries first; it takes the next free one when that is taken */
31
+ export const DEFAULT_PORT = Number(process.env.WHITEBOARD_PORT || 4777)
32
+
33
+ /** The config file in a `.whiteboard` folder, or null. */
34
+ export function configFileIn(whiteboardDir: string): string | null {
35
+ for (const name of CONFIG_NAMES) {
36
+ const f = path.join(whiteboardDir, name)
37
+ if (fs.existsSync(f)) return f
38
+ }
39
+ return null
40
+ }
41
+
42
+ const isDir = (p: string) => { try { return fs.statSync(p).isDirectory() } catch { return false } }
43
+
44
+ /** The project folder a path is in: the nearest folder upward with a `.whiteboard/` folder. */
45
+ export function findProjectDir(from: string = process.cwd()): string | null {
46
+ let dir = path.resolve(from)
47
+ for (;;) {
48
+ if (isDir(path.join(dir, '.whiteboard'))) return dir
49
+ const up = path.dirname(dir)
50
+ if (up === dir) return null
51
+ dir = up
52
+ }
53
+ }
54
+
55
+ /** Everything about the project in `dir`, or null when `dir` is not one. */
56
+ export function project(dir: string | null | undefined): Project | null {
57
+ if (!dir) return null
58
+ const wb = path.join(dir, '.whiteboard')
59
+ if (!isDir(wb)) return null
60
+ return {
61
+ dir,
62
+ wb,
63
+ /** the config file, or null: looked up each time, since one can be added or removed while a server runs */
64
+ get configFile() { return configFileIn(wb) },
65
+ pagesDir: path.join(wb, 'pages'),
66
+ eventsFile: path.join(wb, 'events.ndjson'),
67
+ serverFile: path.join(wb, 'server.json'),
68
+ logFile: path.join(wb, 'server.log'),
69
+ /** shown in titles and the overview: `name` in the config if it is a plain literal, else the folder name */
70
+ get name() {
71
+ const file = configFileIn(wb)
72
+ return (file && (/\bname\s*:\s*['"]([^'"]+)['"]/.exec(fs.readFileSync(file, 'utf8')) ?? [])[1]) || path.basename(dir)
73
+ },
74
+ }
75
+ }
76
+
77
+ // ---- page ids ----------------------------------------------------------------------------------
78
+
79
+ const strip = (p: string) => p.replace(/\\/g, '/').replace(/\.md$/, '').replace(/^\/+|\/+$/g, '')
80
+
81
+ /**
82
+ * A file path or page name → the page id used in URLs and events.
83
+ * - a file inside the pages folder → "<path>" without .md
84
+ * - any other file inside the project folder → "-/<path>" (shown from the repository)
85
+ * - anything else → the name as given, without .md
86
+ */
87
+ export function pageId(proj: Project | null, input: unknown, cwd: string = process.cwd()): string {
88
+ if (!input) return ''
89
+ const s = String(input)
90
+ const looksLikeFile = path.isAbsolute(s) || s.startsWith('.') || /\.\w+$/.test(s)
91
+ if (!looksLikeFile || !proj) return strip(s)
92
+ const abs = path.resolve(cwd, s)
93
+ if (abs.startsWith(proj.pagesDir + path.sep)) return strip(path.relative(proj.pagesDir, abs))
94
+ if (abs === proj.dir || abs.startsWith(proj.dir + path.sep)) return `-/${path.relative(proj.dir, abs).replace(/\\/g, '/')}`.replace(/\/$/, '')
95
+ return strip(s)
96
+ }