@bigmistqke/workpad 0.0.0-stage → 0.18.1

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 (277) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +781 -2
  3. package/bin/workpad.ts +1526 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  5. package/dist/bin/workpad.js +1202 -0
  6. package/dist/client/runtime.js +1194 -0
  7. package/dist/define.js +27 -0
  8. package/dist/plugins/alert/README.md +18 -0
  9. package/dist/plugins/alert/index.js +40 -0
  10. package/dist/plugins/alert/public/style.css +40 -0
  11. package/dist/plugins/ansi/README.md +36 -0
  12. package/dist/plugins/ansi/index.js +63 -0
  13. package/dist/plugins/ansi/public/style.css +28 -0
  14. package/dist/plugins/badge/README.md +32 -0
  15. package/dist/plugins/badge/index.js +28 -0
  16. package/dist/plugins/badge/public/style.css +30 -0
  17. package/dist/plugins/chat/README.md +81 -0
  18. package/dist/plugins/chat/index.js +96 -0
  19. package/dist/plugins/chat/public/client.js +3 -0
  20. package/dist/plugins/chat/public/style.css +143 -0
  21. package/dist/plugins/claude/README.md +116 -0
  22. package/dist/plugins/claude/channel.js +130 -0
  23. package/dist/plugins/claude/doctor.js +316 -0
  24. package/dist/plugins/claude/index.js +199 -0
  25. package/dist/plugins/claude/session.js +68 -0
  26. package/dist/plugins/code/README.md +54 -0
  27. package/dist/plugins/code/diff.js +75 -0
  28. package/dist/plugins/code/index.js +105 -0
  29. package/dist/plugins/code/public/client.js +1 -0
  30. package/dist/plugins/code/public/style.css +99 -0
  31. package/dist/plugins/comments/README.md +30 -0
  32. package/dist/plugins/comments/index.js +39 -0
  33. package/dist/plugins/comments/public/client.js +24 -0
  34. package/dist/plugins/comments/public/style.css +224 -0
  35. package/dist/plugins/details/README.md +24 -0
  36. package/dist/plugins/details/index.js +28 -0
  37. package/dist/plugins/details/public/style.css +7 -0
  38. package/dist/plugins/draft/README.md +14 -0
  39. package/dist/plugins/draft/index.js +27 -0
  40. package/dist/plugins/draft/public/style.css +21 -0
  41. package/dist/plugins/forge/README.md +22 -0
  42. package/dist/plugins/forge/index.js +83 -0
  43. package/dist/plugins/form/README.md +126 -0
  44. package/dist/plugins/form/index.js +179 -0
  45. package/dist/plugins/form/public/style.css +172 -0
  46. package/dist/plugins/grid/README.md +45 -0
  47. package/dist/plugins/grid/index.js +35 -0
  48. package/dist/plugins/grid/public/style.css +98 -0
  49. package/dist/plugins/iframe/README.md +18 -0
  50. package/dist/plugins/iframe/index.js +49 -0
  51. package/dist/plugins/iframe/public/style.css +28 -0
  52. package/dist/plugins/index.js +44 -0
  53. package/dist/plugins/lightbox/README.md +24 -0
  54. package/dist/plugins/lightbox/index.js +20 -0
  55. package/dist/plugins/lightbox/public/client.js +4 -0
  56. package/dist/plugins/lightbox/public/style.css +66 -0
  57. package/dist/plugins/mermaid/README.md +21 -0
  58. package/dist/plugins/mermaid/index.js +42 -0
  59. package/dist/plugins/mermaid/public/style.css +21 -0
  60. package/dist/plugins/opencode/README.md +48 -0
  61. package/dist/plugins/opencode/channel.js +92 -0
  62. package/dist/plugins/opencode/index.js +64 -0
  63. package/dist/plugins/opencode/shim.js +150 -0
  64. package/dist/plugins/quiet/README.md +26 -0
  65. package/dist/plugins/quiet/index.js +23 -0
  66. package/dist/plugins/tabs/README.md +44 -0
  67. package/dist/plugins/tabs/index.js +75 -0
  68. package/dist/plugins/tabs/public/client.js +1 -0
  69. package/dist/plugins/tabs/public/style.css +98 -0
  70. package/dist/plugins/terminal/README.md +63 -0
  71. package/dist/plugins/terminal/host.js +338 -0
  72. package/dist/plugins/terminal/index.js +170 -0
  73. package/dist/plugins/terminal/modes.js +32 -0
  74. package/dist/plugins/terminal/public/client.js +41 -0
  75. package/dist/plugins/terminal/public/style.css +511 -0
  76. package/dist/plugins/terminal/server.js +256 -0
  77. package/dist/plugins/terminal/sizes.js +58 -0
  78. package/dist/plugins/timeline/README.md +48 -0
  79. package/dist/plugins/timeline/index.js +83 -0
  80. package/dist/plugins/timeline/public/style.css +138 -0
  81. package/dist/plugins/toc/README.md +40 -0
  82. package/dist/plugins/toc/index.js +29 -0
  83. package/dist/plugins/toc/public/client.js +1 -0
  84. package/dist/plugins/toc/public/style.css +117 -0
  85. package/dist/plugins/window/README.md +51 -0
  86. package/dist/plugins/window/index.js +91 -0
  87. package/dist/solid/index.js +41 -0
  88. package/dist/src/agents.js +81 -0
  89. package/dist/src/anchors.js +52 -0
  90. package/dist/src/api.js +282 -0
  91. package/dist/src/archive.js +49 -0
  92. package/dist/src/auth.js +67 -0
  93. package/dist/src/build.js +159 -0
  94. package/dist/src/client/page/base.css +1762 -0
  95. package/dist/src/client/page/main.js +2121 -0
  96. package/dist/src/config.js +76 -0
  97. package/dist/src/document.js +93 -0
  98. package/dist/src/entries.js +264 -0
  99. package/dist/src/events.js +322 -0
  100. package/dist/src/files.js +47 -0
  101. package/dist/src/formstate.js +65 -0
  102. package/dist/src/help.js +213 -0
  103. package/dist/src/html.js +34 -0
  104. package/dist/src/hub.js +348 -0
  105. package/dist/src/icons.js +6 -0
  106. package/dist/src/listen.js +132 -0
  107. package/dist/src/listeners.js +64 -0
  108. package/dist/src/load.js +81 -0
  109. package/dist/src/loader.js +179 -0
  110. package/dist/src/migrate.js +314 -0
  111. package/dist/src/package.js +52 -0
  112. package/dist/src/pages/blocks.js +67 -0
  113. package/dist/src/pages/board.js +59 -0
  114. package/dist/src/pages/index.js +27 -0
  115. package/dist/src/pages/markdown.js +863 -0
  116. package/dist/src/pages/render.js +761 -0
  117. package/dist/src/pages/window.js +29 -0
  118. package/dist/src/plugins.js +283 -0
  119. package/dist/src/public.js +24 -0
  120. package/dist/src/rename.js +130 -0
  121. package/dist/src/running.js +146 -0
  122. package/dist/src/serve.js +188 -0
  123. package/dist/src/server.js +1105 -0
  124. package/dist/src/site.js +179 -0
  125. package/dist/src/state.js +155 -0
  126. package/dist/src/status.js +323 -0
  127. package/dist/src/thread.js +68 -0
  128. package/dist/src/watchdog.js +38 -0
  129. package/dist/src/ws.js +128 -0
  130. package/package.json +154 -4
  131. package/plugins/README.md +304 -0
  132. package/plugins/alert/README.md +18 -0
  133. package/plugins/alert/index.ts +50 -0
  134. package/plugins/alert/public/style.css +40 -0
  135. package/plugins/ansi/README.md +36 -0
  136. package/plugins/ansi/index.ts +95 -0
  137. package/plugins/ansi/public/style.css +28 -0
  138. package/plugins/badge/README.md +32 -0
  139. package/plugins/badge/index.ts +36 -0
  140. package/plugins/badge/public/style.css +30 -0
  141. package/plugins/chat/README.md +81 -0
  142. package/plugins/chat/autosize.ts +139 -0
  143. package/plugins/chat/client.tsx +187 -0
  144. package/plugins/chat/index.ts +146 -0
  145. package/plugins/chat/public/style.css +143 -0
  146. package/plugins/claude/README.md +116 -0
  147. package/plugins/claude/channel.ts +136 -0
  148. package/plugins/claude/doctor.ts +274 -0
  149. package/plugins/claude/index.ts +248 -0
  150. package/plugins/claude/session.ts +70 -0
  151. package/plugins/code/README.md +54 -0
  152. package/plugins/code/client.ts +73 -0
  153. package/plugins/code/diff.ts +96 -0
  154. package/plugins/code/index.ts +136 -0
  155. package/plugins/code/public/style.css +99 -0
  156. package/plugins/comments/README.md +30 -0
  157. package/plugins/comments/client.ts +723 -0
  158. package/plugins/comments/index.ts +48 -0
  159. package/plugins/comments/public/style.css +224 -0
  160. package/plugins/details/README.md +24 -0
  161. package/plugins/details/index.ts +30 -0
  162. package/plugins/details/public/style.css +7 -0
  163. package/plugins/draft/README.md +14 -0
  164. package/plugins/draft/index.ts +23 -0
  165. package/plugins/draft/public/style.css +21 -0
  166. package/plugins/forge/README.md +22 -0
  167. package/plugins/forge/index.ts +84 -0
  168. package/plugins/form/README.md +126 -0
  169. package/plugins/form/index.ts +199 -0
  170. package/plugins/form/public/style.css +172 -0
  171. package/plugins/grid/README.md +45 -0
  172. package/plugins/grid/index.ts +41 -0
  173. package/plugins/grid/public/style.css +98 -0
  174. package/plugins/iframe/README.md +18 -0
  175. package/plugins/iframe/index.ts +53 -0
  176. package/plugins/iframe/public/style.css +28 -0
  177. package/plugins/index.ts +49 -0
  178. package/plugins/lightbox/README.md +24 -0
  179. package/plugins/lightbox/client.ts +109 -0
  180. package/plugins/lightbox/index.ts +25 -0
  181. package/plugins/lightbox/public/style.css +66 -0
  182. package/plugins/mermaid/README.md +21 -0
  183. package/plugins/mermaid/index.ts +46 -0
  184. package/plugins/mermaid/public/style.css +21 -0
  185. package/plugins/opencode/README.md +48 -0
  186. package/plugins/opencode/channel.ts +112 -0
  187. package/plugins/opencode/index.ts +91 -0
  188. package/plugins/opencode/shim.ts +166 -0
  189. package/plugins/quiet/README.md +26 -0
  190. package/plugins/quiet/index.ts +28 -0
  191. package/plugins/tabs/README.md +44 -0
  192. package/plugins/tabs/client.ts +96 -0
  193. package/plugins/tabs/index.ts +103 -0
  194. package/plugins/tabs/public/style.css +98 -0
  195. package/plugins/terminal/README.md +63 -0
  196. package/plugins/terminal/client.ts +391 -0
  197. package/plugins/terminal/host.ts +434 -0
  198. package/plugins/terminal/index.ts +199 -0
  199. package/plugins/terminal/modes.ts +30 -0
  200. package/plugins/terminal/public/style.css +225 -0
  201. package/plugins/terminal/server.ts +266 -0
  202. package/plugins/terminal/sizes.ts +72 -0
  203. package/plugins/timeline/README.md +48 -0
  204. package/plugins/timeline/index.ts +82 -0
  205. package/plugins/timeline/public/style.css +138 -0
  206. package/plugins/toc/README.md +40 -0
  207. package/plugins/toc/client.ts +236 -0
  208. package/plugins/toc/index.ts +55 -0
  209. package/plugins/toc/public/style.css +117 -0
  210. package/plugins/window/README.md +51 -0
  211. package/plugins/window/index.ts +92 -0
  212. package/skills/workpad/SKILL.md +592 -0
  213. package/solid/index.tsx +80 -0
  214. package/src/agents.ts +134 -0
  215. package/src/anchors.ts +85 -0
  216. package/src/api.ts +444 -0
  217. package/src/archive.ts +75 -0
  218. package/src/auth.ts +99 -0
  219. package/src/build.ts +213 -0
  220. package/src/client/base.ts +19 -0
  221. package/src/client/forms.ts +420 -0
  222. package/src/client/markdown.ts +147 -0
  223. package/src/client/page/base.css +1762 -0
  224. package/src/client/page/board.ts +166 -0
  225. package/src/client/page/elsewhere.ts +153 -0
  226. package/src/client/page/fresh.ts +343 -0
  227. package/src/client/page/heading-link.ts +32 -0
  228. package/src/client/page/live.ts +134 -0
  229. package/src/client/page/main.ts +398 -0
  230. package/src/client/page/orphans.ts +64 -0
  231. package/src/client/page/place.ts +260 -0
  232. package/src/client/page/sort.ts +49 -0
  233. package/src/client/page/sticky.ts +58 -0
  234. package/src/client/page/theme.ts +36 -0
  235. package/src/client/page/view.ts +9 -0
  236. package/src/client/page/waiting.ts +278 -0
  237. package/src/client/page/window.ts +40 -0
  238. package/src/client/reactive.ts +19 -0
  239. package/src/client/read.ts +111 -0
  240. package/src/client/runtime.ts +499 -0
  241. package/src/client/scroll.ts +90 -0
  242. package/src/client/toast.ts +99 -0
  243. package/src/config.ts +111 -0
  244. package/src/define.ts +997 -0
  245. package/src/document.ts +191 -0
  246. package/src/entries.ts +352 -0
  247. package/src/events.ts +463 -0
  248. package/src/files.ts +74 -0
  249. package/src/formstate.ts +72 -0
  250. package/src/help.ts +240 -0
  251. package/src/html.ts +44 -0
  252. package/src/hub.ts +435 -0
  253. package/src/icons.ts +18 -0
  254. package/src/listen.ts +171 -0
  255. package/src/listeners.ts +89 -0
  256. package/src/load.ts +113 -0
  257. package/src/loader.ts +227 -0
  258. package/src/migrate.ts +352 -0
  259. package/src/package.ts +62 -0
  260. package/src/pages/blocks.ts +106 -0
  261. package/src/pages/board.ts +89 -0
  262. package/src/pages/index.ts +79 -0
  263. package/src/pages/markdown.ts +976 -0
  264. package/src/pages/render.ts +845 -0
  265. package/src/pages/window.ts +69 -0
  266. package/src/plugins.ts +319 -0
  267. package/src/public.ts +39 -0
  268. package/src/rename.ts +147 -0
  269. package/src/running.ts +171 -0
  270. package/src/serve.ts +221 -0
  271. package/src/server.ts +1385 -0
  272. package/src/site.ts +170 -0
  273. package/src/state.ts +327 -0
  274. package/src/status.ts +466 -0
  275. package/src/thread.ts +98 -0
  276. package/src/watchdog.ts +53 -0
  277. package/src/ws.ts +155 -0
package/src/site.ts ADDED
@@ -0,0 +1,170 @@
1
+ // Where things are, for the pages: a page's id ↔ its file ↔ its address, its source with its
2
+ // frontmatter (parsed here, once), the folders of pages, and the address of a file a page links
3
+ // to. The server serves a project through one (servedSite); `workpad build` writes one out (see
4
+ // build.ts), with ids, files and addresses of its own.
5
+ import fs from 'node:fs'
6
+ import path from 'node:path'
7
+ import { parse as parseYaml } from 'yaml'
8
+ import type { Project } from './config.ts'
9
+ import { fileFilter, type FileFilter } from './files.ts'
10
+ import type { PageSource, PagesSource, StoredPage } from './define.ts'
11
+
12
+ /** A page's frontmatter: YAML between `---` lines, at the very start. */
13
+ export const FRONTMATTER = /^---\r?\n([\s\S]*?)\r?\n---\r?\n/
14
+
15
+ /** Is `file` the folder `dir` or inside it? */
16
+ export const within = (file: string, dir: string) => file === dir || file.startsWith(dir + path.sep)
17
+
18
+ /** Resolve a path inside a base folder, refusing anything that escapes it. */
19
+ export function inside(base: string, rel: string): string | null {
20
+ const p = path.resolve(base, '.' + path.posix.normalize('/' + rel))
21
+ return within(p, base) ? p : null
22
+ }
23
+
24
+ /** A page's frontmatter, parsed as YAML; empty, with the reason, when it isn't fields. */
25
+ export function parseFrontmatter(source: string): { data: Record<string, unknown>; error: string | null } {
26
+ const m = FRONTMATTER.exec(source)
27
+ if (!m) return { data: {}, error: null }
28
+ let data: unknown
29
+ try {
30
+ data = parseYaml(m[1]!)
31
+ } catch (err) {
32
+ return { data: {}, error: (err as Error).message }
33
+ }
34
+ if (data == null) return { data: {}, error: null }
35
+ if (typeof data !== 'object' || Array.isArray(data))
36
+ return { data: {}, error: 'it must be fields (`key: value` lines), not a single value or a list' }
37
+ return { data: data as Record<string, unknown>, error: null }
38
+ }
39
+
40
+ /** A markdown file as a page: its source, the body after its frontmatter, and the frontmatter parsed. */
41
+ export function pageSource(id: string, file: string): PageSource {
42
+ const source = fs.readFileSync(file, 'utf8')
43
+ const { data, error } = parseFrontmatter(source)
44
+ return {
45
+ kind: 'page',
46
+ id,
47
+ file,
48
+ mtime: fs.statSync(file).mtimeMs,
49
+ source,
50
+ body: source.replace(FRONTMATTER, ''),
51
+ frontmatter: data,
52
+ frontmatterError: error,
53
+ }
54
+ }
55
+
56
+ /** Text files show as files; anything with a NUL byte near the start is binary. */
57
+ function isText(file: string): boolean {
58
+ const fd = fs.openSync(file, 'r')
59
+ const buf = Buffer.alloc(8192)
60
+ const n = fs.readSync(fd, buf, 0, buf.length, 0)
61
+ fs.closeSync(fd)
62
+ return !buf.subarray(0, n).includes(0)
63
+ }
64
+
65
+ /** A repository file's id: `-/` and its path in the project (`-` for the project's folder). */
66
+ export const isRepoId = (id: string) => id === '-' || id.startsWith('-/')
67
+
68
+ /** A project as the server serves it: its pages under its base, its files under `-/` when it serves them. */
69
+ export interface ServedSite {
70
+ /**
71
+ * A page id → the stored page, or { raw: file } (serve the bytes), or null (nothing there). A
72
+ * folder of pages without an index lists its pages; a repository folder lists its entries.
73
+ */
74
+ page(id: string): StoredPage | { raw: string } | null
75
+ /** The pages in a folder of the pages folder, newest first (what waits on each is the status's). */
76
+ pagesIn(dir: string): PagesSource['pages']
77
+ /** The address that serves a file or folder on disk, or null when nothing here serves it. */
78
+ urlOf(file: string): string | null
79
+ /** For links: the target if it exists (also with .md added), as its address. */
80
+ urlFor(file: string): string | null
81
+ }
82
+
83
+ /**
84
+ * The project as its server serves it, below `base`. `files()` says whether it serves the project's
85
+ * own files (`-/…`), which the config can change while it runs.
86
+ */
87
+ export function servedSite(
88
+ project: Project,
89
+ { base, files, filter = () => fileFilter() }: { base: string; files: () => boolean; filter?: () => FileFilter },
90
+ ): ServedSite {
91
+ function pagesIn(dir: string): PagesSource['pages'] {
92
+ const out: PagesSource['pages'] = []
93
+ const walk = (d: string) => {
94
+ if (!fs.existsSync(d)) return
95
+ for (const ent of fs.readdirSync(d, { withFileTypes: true })) {
96
+ if (ent.name.startsWith('.') || ent.name === 'node_modules') continue
97
+ const full = path.join(d, ent.name)
98
+ if (ent.isDirectory()) walk(full)
99
+ else if (ent.name.endsWith('.md')) {
100
+ const id = path.relative(project.pagesDir, full).split(path.sep).join('/').replace(/\.md$/, '')
101
+ out.push({ id, file: full, mtime: fs.statSync(full).mtimeMs, lastEvent: null, awaiting: 0 })
102
+ }
103
+ }
104
+ }
105
+ walk(dir)
106
+ return out.sort((a, b) => b.mtime - a.mtime)
107
+ }
108
+
109
+ function page(id: string): StoredPage | { raw: string } | null {
110
+ if (!id) return { kind: 'pages', id: '', file: project.pagesDir, mtime: null, pages: pagesIn(project.pagesDir) }
111
+ if (isRepoId(id)) {
112
+ const target = inside(project.dir, id.slice(2))
113
+ if (!target || !fs.existsSync(target)) return null
114
+ // what the config's `files` leaves out isn't there (node_modules, .git always: src/files.ts)
115
+ const rel = path.relative(project.dir, target).split(path.sep).join('/')
116
+ const stat = fs.statSync(target)
117
+ if (rel && !(stat.isDirectory() ? filter().mayHold(rel) : filter().shows(rel))) return null
118
+ if (stat.isDirectory()) {
119
+ const entries = fs
120
+ .readdirSync(target, { withFileTypes: true })
121
+ .filter(e => {
122
+ if (e.name === '.DS_Store') return false
123
+ const child = rel ? `${rel}/${e.name}` : e.name
124
+ return e.isDirectory() ? filter().mayHold(child) : filter().shows(child)
125
+ })
126
+ .map(e => ({ name: e.name, dir: e.isDirectory() }))
127
+ .sort((a, b) => Number(b.dir) - Number(a.dir) || a.name.localeCompare(b.name))
128
+ return { kind: 'folder', id, file: target, mtime: stat.mtimeMs, entries }
129
+ }
130
+ if (/\.(md|markdown)$/i.test(target)) return pageSource(id, target)
131
+ if (stat.size > 1e6 || !isText(target)) return { raw: target }
132
+ return { kind: 'file', id, file: target, mtime: stat.mtimeMs, source: fs.readFileSync(target, 'utf8') }
133
+ }
134
+ const target = inside(project.pagesDir, id)
135
+ if (!target) return null
136
+ if (path.extname(id) && fs.existsSync(target) && fs.statSync(target).isFile()) return { raw: target }
137
+ const file = target + '.md'
138
+ if (fs.existsSync(target) && fs.statSync(target).isDirectory()) {
139
+ const index = path.join(target, 'index.md')
140
+ if (fs.existsSync(index)) return pageSource(id, index)
141
+ // a folder next to the page of its name (its screenshots, say) leaves the page its address;
142
+ // the files in it are still served at theirs
143
+ if (fs.existsSync(file)) return pageSource(id, file)
144
+ return { kind: 'pages', id, file: target, mtime: null, pages: pagesIn(target) }
145
+ }
146
+ return fs.existsSync(file) ? pageSource(id, file) : null
147
+ }
148
+
149
+ function urlOf(abs: string): string | null {
150
+ const rel = (dir: string) => path.relative(dir, abs).split(path.sep).map(encodeURIComponent).join('/')
151
+ if (within(abs, project.pagesDir)) return `${base}/${rel(project.pagesDir).replace(/\.md$/, '')}`
152
+ if (files() && within(abs, project.dir)) {
153
+ if (!filter().shows(path.relative(project.dir, abs).split(path.sep).join('/'))) return null
154
+ return `${base}/-/${rel(project.dir)}`
155
+ }
156
+ return null
157
+ }
158
+
159
+ function urlFor(abs: string): string | null {
160
+ for (const candidate of [abs, abs + '.md']) {
161
+ if (fs.existsSync(candidate)) {
162
+ const url = urlOf(candidate)
163
+ if (url) return url + (fs.statSync(candidate).isDirectory() && !url.endsWith('/') ? '/' : '')
164
+ }
165
+ }
166
+ return null
167
+ }
168
+
169
+ return { page, pagesIn, urlOf, urlFor }
170
+ }
package/src/state.ts ADDED
@@ -0,0 +1,327 @@
1
+ // Workpad's state as data: the shapes the server's API answers with (GET /api/forms, /api/pages,
2
+ // /api/chats, /api/agents, /api/subscriptions, /api/stream; see the README's API section), and how
3
+ // each is made from what the server knows: the pages' status, what their forms ask, the log and who
4
+ // listens. No disk and no HTTP in it: the server reads and hands over, the client library
5
+ // (`@bigmistqke/workpad/api`, src/api.ts) asks over HTTP and gets these.
6
+ //
7
+ // Everything a project's server answers carries `project`: the path the project is served under,
8
+ // '' for a server of its own, `/rg/develop` under the shared server. That is how the shared server's
9
+ // answers across projects say whose each is, and where to send what is asked of it next.
10
+ import type { FormState, Listener, PageStatus, WpEvent } from './define.ts'
11
+ import type { FormDetail, FormOption } from './pages/markdown.ts'
12
+ import { chatForms, formDetails } from './pages/markdown.ts'
13
+ import { CHAT_MESSAGE, fromReader, isQuestionName, namesOf, questionState, subscriptions } from './status.ts'
14
+ import { matches } from './events.ts'
15
+
16
+ export type { FormOption }
17
+
18
+ /** A project a server serves (GET /api/projects). */
19
+ export interface ProjectInfo {
20
+ /** where it is served, the path its URLs start with: '' for a server of its own, `/rg/develop` under the shared one */
21
+ base: string
22
+ /** what it is called (its config's name, else its folder's) */
23
+ name: string
24
+ /** how the reader knows it: `develop`, `worktrees › cmux` */
25
+ label: string
26
+ /** its folder */
27
+ dir: string
28
+ /** the shared server's entry it is in; null for a server of its own */
29
+ entry: string | null
30
+ }
31
+
32
+ /** A page (GET /api/pages). */
33
+ export interface PageInfo {
34
+ project: string
35
+ /** its id: `plan`, `task/plan` */
36
+ page: string
37
+ title: string
38
+ /** its address on the server, from the server's root: `/rg/develop/plan` */
39
+ url: string
40
+ /** its file */
41
+ file: string
42
+ /** when its file last changed, ISO; null when it has none */
43
+ modified: string | null
44
+ /** who subscribed to it */
45
+ subscribers: string[]
46
+ /** its questions, by name */
47
+ forms: string[]
48
+ /** how many things on it wait for the reader: open questions and unread messages */
49
+ awaiting: number
50
+ }
51
+
52
+ /** Why a form is held (`workpad hold`), since when, by whom. */
53
+ export interface HoldInfo {
54
+ reason: string
55
+ since: string
56
+ by: string | null
57
+ }
58
+
59
+ /** A form: a question on a page, or in an agent's chat message there (GET /api/forms). */
60
+ export interface FormInfo {
61
+ project: string
62
+ page: string
63
+ /** its name: the type of its answers; `<chat>/<name>` for one in a chat message */
64
+ name: string
65
+ /** what it asks: its label, else its first paragraph, as plain text; null when it says nothing (a hand-written form) */
66
+ question: string | null
67
+ /** its radios and checkboxes */
68
+ options: FormOption[]
69
+ /** its buttons' labels */
70
+ buttons: string[]
71
+ /** the `#…` that goes to it: `form:<name>`, or `message:<id>` for one in a chat message */
72
+ anchor: string
73
+ /** its address, from the server's root, with the anchor */
74
+ url: string
75
+ /**
76
+ * when it was asked: the time of the chat message it is in; for a form on a page, when the page was
77
+ * made (a form added later to an older page says the page's time)
78
+ */
79
+ askedAt: string | null
80
+ state: FormState
81
+ /** why it is held, when it is (state `held`) */
82
+ hold: HoldInfo | null
83
+ /** its latest answer, null without one */
84
+ answer: WpEvent | null
85
+ /** the chat it is talked about in: its own name, or the chat its message is in */
86
+ chat: string
87
+ }
88
+
89
+ /** A chat on a page: the messages that share a `chat` name (GET /api/chats). */
90
+ export interface ChatInfo {
91
+ project: string
92
+ page: string
93
+ chat: string
94
+ /** its messages in order, the reader's and the agents', those replaced by an edit left out */
95
+ messages: WpEvent[]
96
+ /** its latest message is the reader's: it waits for a reply */
97
+ awaitingReply: boolean
98
+ }
99
+
100
+ /** An agent subscribed to a page (GET /api/subscriptions). */
101
+ export interface SubscriptionInfo {
102
+ project: string
103
+ agent: string
104
+ /** the page, with the pages below it; '' for every page */
105
+ page: string
106
+ /** when it subscribed */
107
+ since: string
108
+ /** taken on through its channel (`workpad listen --channel`) */
109
+ channel: boolean
110
+ }
111
+
112
+ /** An agent the board knows (GET /api/agents): it named itself, subscribed, wrote something, or listens now. */
113
+ export interface AgentInfo {
114
+ project: string
115
+ /** its id: `--agent`, `WORKPAD_AGENT`, or the session a plugin names */
116
+ id: string
117
+ /** the name it gave itself (`workpad agent name`) */
118
+ name: string | null
119
+ /** the pages it is subscribed to ('' for every page) */
120
+ subscribed: string[]
121
+ /** what it listens on now: each a page ('' for every page) and a type (null: any) */
122
+ listening: { page: string; type: string | null }[]
123
+ /** when it was last seen: now when it listens, else its latest event in the log; null when neither */
124
+ lastSeen: string | null
125
+ }
126
+
127
+ /**
128
+ * What an event is, for a stream that wants only some (`kind=answer,chat`): a reader's `answer` to a
129
+ * question, a `chat` message (either side's), a `comment` (the comments plugin's), a `mark` the
130
+ * core writes (`workpad:received`, `:handled`, `:hold`…), or `other` (a plugin's own).
131
+ */
132
+ export type EventKind = 'answer' | 'chat' | 'comment' | 'mark' | 'other'
133
+
134
+ /** An event of a project's log, as the API gives it: with its project and kind (GET /api/stream, /api/events). */
135
+ export interface LiveEvent extends WpEvent {
136
+ project: string
137
+ kind: EventKind
138
+ }
139
+
140
+ export const kindOf = (e: WpEvent): EventKind =>
141
+ e.type === CHAT_MESSAGE
142
+ ? 'chat'
143
+ : e.type.startsWith('comments:')
144
+ ? 'comment'
145
+ : e.type.startsWith('workpad:')
146
+ ? 'mark'
147
+ : isQuestionName(e.type)
148
+ ? fromReader(e)
149
+ ? 'answer'
150
+ : 'other'
151
+ : 'other'
152
+
153
+ /** Which events (GET /api/events, /api/stream): each a comma-separated list where it can be several. */
154
+ export interface EventQuery {
155
+ page?: string | null
156
+ /** event types: a form's name, `chat:message`… */
157
+ type?: string | string[] | null
158
+ kind?: EventKind | EventKind[] | null
159
+ /** only those after this event (an id) or time (ISO) */
160
+ since?: string | null
161
+ }
162
+
163
+ const list = (v: unknown): string[] =>
164
+ v == null || v === '' ? [] : (Array.isArray(v) ? v : String(v).split(',')).map(String).filter(Boolean)
165
+
166
+ /** Does an event match a query? (`since` aside: it is a place in the log, see after.) */
167
+ export function matchesQuery(e: WpEvent, q: EventQuery): boolean {
168
+ const types = list(q.type)
169
+ const kinds = list(q.kind)
170
+ return (
171
+ matches(e, { page: q.page || null }) &&
172
+ (!types.length || types.includes(e.type)) &&
173
+ (!kinds.length || kinds.includes(kindOf(e)))
174
+ )
175
+ }
176
+
177
+ /** The events after `since`: an event's id (those after it in the log), or a time. */
178
+ export function after(events: WpEvent[], since: string | null | undefined): WpEvent[] {
179
+ if (!since) return events
180
+ const i = events.findIndex(e => e.id === since)
181
+ return i >= 0 ? events.slice(i + 1) : events.filter(e => e.ts > since)
182
+ }
183
+
184
+ export const live = (e: WpEvent, project: string): LiveEvent => ({ ...e, project, kind: kindOf(e) })
185
+
186
+ /** A page's address on the server, from its root. */
187
+ export const pageUrl = (base: string, page: string) => `${base}/${page.split('/').map(encodeURIComponent).join('/')}`
188
+
189
+ /** What the builders below are made from: what a project's server knows at one moment. */
190
+ export interface StateSource {
191
+ /** where the project is served ('' or `/rg/develop`) */
192
+ base: string
193
+ /** the status of each page */
194
+ statuses: PageStatus[]
195
+ /** the whole log */
196
+ events: WpEvent[]
197
+ /** what each `:::form` of a page asks, by the page's id */
198
+ details(page: string): FormDetail[]
199
+ /** when a page was made and last changed, in ms; null for a page without a file */
200
+ times(page: string): { born: number; modified: number } | null
201
+ /** who listens now */
202
+ listeners: Listener[]
203
+ }
204
+
205
+ const iso = (ms: number | null | undefined) => (ms ? new Date(ms).toISOString() : null)
206
+
207
+ export function pagesOf(s: StateSource): PageInfo[] {
208
+ return s.statuses.map(p => ({
209
+ project: s.base,
210
+ page: p.page,
211
+ title: p.title,
212
+ url: pageUrl(s.base, p.page),
213
+ file: p.file,
214
+ modified: iso(s.times(p.page)?.modified),
215
+ subscribers: p.subscribers,
216
+ forms: p.asks,
217
+ awaiting: p.open.length + p.unread.length,
218
+ }))
219
+ }
220
+
221
+ /** Every question of every page, with what it asks and where it stands. */
222
+ export function formsOf(s: StateSource): FormInfo[] {
223
+ const marks = { received: () => false, handled: () => false }
224
+ return s.statuses.flatMap(p => {
225
+ const own = s.events.filter(e => e.page === p.page)
226
+ const details = s.details(p.page)
227
+ const url = pageUrl(s.base, p.page)
228
+ return p.questions.map(({ name, state }): FormInfo => {
229
+ const hold = p.held.find(h => h.name === name)
230
+ // the latest answer: the reader's latest event of its name (questionState says which)
231
+ const answer = questionState(name, own, [], marks).last ?? null
232
+ const slash = name.indexOf('/')
233
+ if (slash >= 0) {
234
+ // a form in an agent's chat message: what it asks is in the message
235
+ const chat = name.slice(0, slash)
236
+ const found = chatForms(s.events, p.page, chat).find(f => f.name === name)
237
+ const detail = found ? formDetails(found.markdown)[0] : undefined
238
+ const anchor = found ? `message:${found.message.id}` : `chat:${chat}`
239
+ return {
240
+ project: s.base,
241
+ page: p.page,
242
+ name,
243
+ question: detail?.question ?? null,
244
+ options: detail?.options ?? [],
245
+ buttons: detail?.buttons ?? [],
246
+ anchor,
247
+ url: `${url}#${anchor}`,
248
+ askedAt: found?.message.ts ?? null,
249
+ state,
250
+ hold: hold ? { reason: hold.reason, since: hold.since, by: hold.by } : null,
251
+ answer,
252
+ chat,
253
+ }
254
+ }
255
+ const detail = details.find(d => d.name === name)
256
+ return {
257
+ project: s.base,
258
+ page: p.page,
259
+ name,
260
+ question: detail?.question ?? null,
261
+ options: detail?.options ?? [],
262
+ buttons: detail?.buttons ?? [],
263
+ anchor: `form:${name}`,
264
+ url: `${url}#form:${name}`,
265
+ askedAt: iso(s.times(p.page)?.born),
266
+ state,
267
+ hold: hold ? { reason: hold.reason, since: hold.since, by: hold.by } : null,
268
+ answer,
269
+ chat: name,
270
+ }
271
+ })
272
+ })
273
+ }
274
+
275
+ /** The chats on each page: every `chat` name its events carry, in order of their first message. */
276
+ export function chatsOf(s: StateSource): ChatInfo[] {
277
+ const pages = new Set(s.statuses.map(p => p.page))
278
+ const byChat = new Map<string, WpEvent[]>()
279
+ const replaced = new Set<string>()
280
+ for (const e of s.events) {
281
+ if (!pages.has(e.page) || e.type.startsWith('workpad:') || typeof e.data?.chat !== 'string') continue
282
+ if (e.data.replaces) replaced.add(String(e.data.replaces))
283
+ const key = `${e.page}\n${e.data.chat}`
284
+ byChat.set(key, [...(byChat.get(key) ?? []), e])
285
+ }
286
+ return [...byChat].map(([key, messages]) => {
287
+ const [page, chat] = key.split('\n') as [string, string]
288
+ const shown = messages.filter(m => !replaced.has(m.id))
289
+ return {
290
+ project: s.base,
291
+ page,
292
+ chat,
293
+ messages: shown,
294
+ awaitingReply: !!s.statuses.find(p => p.page === page)?.awaitingReply.includes(chat),
295
+ }
296
+ })
297
+ }
298
+
299
+ export function subscriptionsOf(s: Pick<StateSource, 'base' | 'events'>): SubscriptionInfo[] {
300
+ return subscriptions(s.events).map(e => ({
301
+ project: s.base,
302
+ agent: e.by!,
303
+ page: e.page,
304
+ since: e.ts,
305
+ channel: !!e.data?.channel,
306
+ }))
307
+ }
308
+
309
+ /** Everyone the board knows, in the order the log first names them, then those only listening. */
310
+ export function agentsOf(s: Pick<StateSource, 'base' | 'events' | 'listeners'>, now = new Date()): AgentInfo[] {
311
+ const names = namesOf(s.events)
312
+ const seen = new Map<string, string>()
313
+ for (const e of s.events) if (e.by) seen.set(e.by, e.ts)
314
+ const subs = subscriptionsOf(s)
315
+ const ids = [...new Set([...seen.keys(), ...s.listeners.flatMap(l => (l.agent ? [l.agent] : []))])]
316
+ return ids.map(id => {
317
+ const listening = s.listeners.filter(l => l.agent === id).map(l => ({ page: l.page, type: l.type }))
318
+ return {
319
+ project: s.base,
320
+ id,
321
+ name: names.get(id) ?? null,
322
+ subscribed: [...new Set(subs.filter(x => x.agent === id).map(x => x.page))],
323
+ listening,
324
+ lastSeen: listening.length ? now.toISOString() : (seen.get(id) ?? null),
325
+ }
326
+ })
327
+ }