@bigmistqke/workpad 0.0.0-stage → 0.18.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 (277) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +781 -2
  3. package/bin/workpad.ts +1509 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  5. package/dist/bin/workpad.js +1190 -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 +2098 -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 +211 -0
  103. package/dist/src/html.js +34 -0
  104. package/dist/src/hub.js +341 -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 +665 -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 +588 -0
  213. package/solid/index.tsx +80 -0
  214. package/src/agents.ts +133 -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 +137 -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 +238 -0
  251. package/src/html.ts +44 -0
  252. package/src/hub.ts +426 -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 +748 -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/define.ts ADDED
@@ -0,0 +1,997 @@
1
+ // The contract between the core, a project's config and its plugins.
2
+ //
3
+ // import { defineConfig, definePlugin, html } from '@bigmistqke/workpad'
4
+ //
5
+ // The core stores pages as markdown, renders them, keeps the event log and runs the live channel
6
+ // between them and a browser. Everything else is a plugin: the syntax pages are written in (blocks),
7
+ // behaviour on every page, commands, routes. defineConfig and definePlugin only return their
8
+ // argument; they exist for types.
9
+ import type { IncomingMessage, ServerResponse } from 'node:http'
10
+ import type { Html } from './html.ts'
11
+ import type { Workpad } from './api.ts'
12
+
13
+ // HTML from a template literal, escaped by default: for a block, a plugin's `slots`
14
+ export { html, raw, escape, Html } from './html.ts'
15
+ export { windowFrame, type WindowOptions } from './pages/window.ts'
16
+ export { anchorTaken, slugText } from './anchors.ts'
17
+ export type { FilesConfig } from './files.ts'
18
+ import type { FilesConfig } from './files.ts'
19
+
20
+ // ---- config ------------------------------------------------------------------------------------
21
+
22
+ export interface Config {
23
+ /** shown in page titles and the overview (default: the folder name) */
24
+ name?: string
25
+ /**
26
+ * Serve the project's other files at /-/<path>, list them, and watch them for changes (default:
27
+ * true). Set it to false for a project whose folder is not a repository, such as your home
28
+ * folder: then only `.workpad/` is served and watched. Or say which: `{ include, exclude }`,
29
+ * globs relative to the project folder (`{ include: ['docs/**', '*.md'], exclude: ['dist'] }`).
30
+ * node_modules and .git are always left out.
31
+ */
32
+ files?: boolean | FilesConfig
33
+ /**
34
+ * Plugins, added to the built-in ones (form, chat, code, comments and the rest); one with a
35
+ * built-in's name replaces it (`code({ themes })`). Such as the terminal drawer
36
+ * (`@bigmistqke/workpad/terminal`).
37
+ */
38
+ plugins?: Plugin[]
39
+ /** a stylesheet linked on every page, after the built-in ones: the project's own look */
40
+ style?: URL | string
41
+ /** what `workpad build` makes: a static site of some pages, without what is only for writing them */
42
+ build?: BuildConfig
43
+ /**
44
+ * Commands of the project's own, by name, without a plugin around them: `commands: { mr: { description,
45
+ * run: ctx => … } }` is `workpad mr`. Each gets the same context as a plugin's command (its
46
+ * folder is `.workpad/<name>/`, and `ctx.wp` reaches the project's state). A name is letters,
47
+ * digits, - and _, and can't be a built-in command's, a folder of the core's, or a plugin's: the
48
+ * config is refused with one that is.
49
+ */
50
+ commands?: Record<string, Command>
51
+ }
52
+
53
+ /**
54
+ * `workpad build`: pages rendered once, as plain HTML files with their images and styles, links
55
+ * between them relative. What is for writing them is left out: the live channel, the event log,
56
+ * and the plugins and blocks in `skip`. What is for reading stays (tabs, ⤢, the theme, contents).
57
+ */
58
+ export interface BuildConfig {
59
+ /**
60
+ * Which pages, as globs from the project's folder; `!` leaves out: `['docs/**\/*.md', '!docs/notes/**']`.
61
+ * Each lands in `out` at its place below the folder they share (`docs/api/forms.md` →
62
+ * `api/forms.html`); a README.md or index.md is its folder's index.html.
63
+ */
64
+ pages?: string[]
65
+ /** plugins or blocks left out of the site (default: form, chat, comments, draft) */
66
+ skip?: string[]
67
+ /** the folder the site goes to, from the project's folder (default: site) */
68
+ out?: string
69
+ }
70
+
71
+ export const defineConfig = (config: Config): Config => config
72
+
73
+ // ---- plugins --------------------------------------------------------------------------------
74
+
75
+ /**
76
+ * What extends workpad: syntax (blocks), behaviour on every page, and commands. `setup` runs
77
+ * once per loaded config, on the server, and returns what the plugin adds to it. Everything is
78
+ * optional but the name.
79
+ *
80
+ * What a plugin adds lives under its name, so two plugins can't collide:
81
+ * - its routes are mounted at `/api/<name>/…`,
82
+ * - its commands are `workpad <name>` and `workpad <name> <sub>`,
83
+ * - its files go in `.workpad/<name>/`, which ignores itself in git and reloads nothing.
84
+ * The core checks a route's token and arguments before its code runs, and lists routes and commands
85
+ * in `workpad help` and `workpad docs`.
86
+ */
87
+ export interface Plugin {
88
+ /** letters, digits, - and _; not a folder of the core (`pages`, and `plugins` for local plugins) nor a built-in command */
89
+ name: string
90
+ /**
91
+ * Syntax: the blocks it adds to pages, by name: `:::name`, `::name`, `:name` and ```` ```name ````
92
+ * go to the block of that name. Block names are unique across a project's plugins.
93
+ */
94
+ blocks?: Record<string, Block<any>>
95
+ /**
96
+ * The folder its browser files are served from, and nothing else of it: its client modules,
97
+ * stylesheets, images, fonts (TypeScript there is served with its types stripped). `client` and
98
+ * `style` here and in its blocks name files in it. definePlugin defaults it to `public/` next to
99
+ * the module that calls it; a plain object plugin has the project's `.workpad/public/`.
100
+ */
101
+ public?: URL | string
102
+ /** a stylesheet linked on every page, in its public folder (a block's `style` links only where it is used) */
103
+ style?: URL | string
104
+ /**
105
+ * Text that links somewhere, as written in plain text (`#12` → an issue): the core turns each
106
+ * match into a link when it renders a page, but not inside code, links or inline directives.
107
+ * Called once per project folder (`dir`), so it can read the project (its git remote, say).
108
+ */
109
+ links?(ctx: { dir: string }): TextLink[]
110
+ /**
111
+ * The event types it writes to the log, named `<plugin name>:<what>`, and who they're for.
112
+ * `workpad docs` lists them. Only those with `handOut: true` reach an agent unasked: a `listen`
113
+ * on a page hands them out, `workpad status` counts them, and the channels deliver them.
114
+ * Others stay in the log, for `workpad events` or a `wait --type` that asks for them by name.
115
+ */
116
+ events?: Record<string, EventSpec>
117
+ /**
118
+ * Composes itself around other blocks: a block whose `compose` attribute names this plugin
119
+ * (`::image{src compose=window}`, ```` ```mermaid {compose=window} ````) is put in what this
120
+ * returns, the block's own HTML as `children`, as a component gets what it wraps. The block needs
121
+ * to know nothing of it. Several named (`compose=lightbox,window`) compose in the order written, the
122
+ * first innermost. Not inline blocks, nor a block of this plugin's name (`:::window` is one already).
123
+ * What it reads of the block's attributes (the window's `title`) its docs say.
124
+ */
125
+ compose?(ctx: ComposeContext): string | Html | Promise<string | Html>
126
+ setup?(ctx: PluginContext): PluginHooks | Promise<PluginHooks>
127
+ /** by subcommand: `''` is `workpad <name>` itself, `stop` is `workpad <name> stop` */
128
+ commands?: Record<string, Command>
129
+ /**
130
+ * Markdown for whoever writes pages here, printed by `workpad docs` after how pages are written,
131
+ * under a heading the core adds (`# Plugin: <name>`), so start below it (`##`):
132
+ * what the plugin sends to the page's log, and how to answer it (comments say how to reply)
133
+ */
134
+ docs?(): string | Promise<string>
135
+ /**
136
+ * How its events on a page read once the page is archived (`workpad archive`), which keeps the
137
+ * page as a markdown file with what was answered on it: given all of the page's events, in the
138
+ * order they came, markdown for its own (a section from `##`), or nothing when none are its own.
139
+ */
140
+ archive?(events: WpEvent[]): string | undefined | Promise<string | undefined>
141
+ /**
142
+ * Markdown at the end of a page, as if it were written there (the chat at the bottom of every
143
+ * page): it renders with the page, and what is in it counts as the page's (its forms are
144
+ * questions, its chats are on the page). Given the page as written; null adds nothing.
145
+ */
146
+ end?(page: PageSource): string | null
147
+ /**
148
+ * The agent session the command line runs in, for a plugin that knows how its agent says so
149
+ * (Claude Code's reads `CLAUDE_CODE_SESSION_ID`); null when it doesn't run in one. `workpad
150
+ * new` subscribes the session to the page it makes, and `workpad status` marks the session's
151
+ * pages. The first plugin that names a session wins; `--agent` and `WORKPAD_AGENT` come before any.
152
+ */
153
+ session?(): string | null | undefined | Promise<string | null | undefined>
154
+ }
155
+
156
+ /**
157
+ * A plugin, with its public folder defaulted to `public/` next to the module that calls this
158
+ * (found from the call stack), when it doesn't say one.
159
+ */
160
+ export const definePlugin = (plugin: Plugin): Plugin =>
161
+ plugin.public != null ? plugin : { ...plugin, public: callerPublic() }
162
+
163
+ /** `public/` next to the module that called definePlugin: the first frame of the stack in another file. */
164
+ function callerPublic(): URL | undefined {
165
+ const own = import.meta.url
166
+ for (const line of (new Error().stack ?? '').split('\n').slice(1)) {
167
+ const m = /(file:\/\/[^\s)]+?|\/[^\s():]+?):\d+:\d+/.exec(line)
168
+ if (!m) continue
169
+ const url = m[1].startsWith('file:') ? m[1] : `file://${m[1]}`
170
+ if (url === own) continue
171
+ return new URL('./public/', url)
172
+ }
173
+ return undefined
174
+ }
175
+
176
+ /** A pattern in a page's text that links somewhere (Plugin.links). */
177
+ export interface TextLink {
178
+ /** what to find; matched against each run of plain text */
179
+ pattern: RegExp
180
+ /** where a match links to; null leaves it as text */
181
+ href(match: RegExpExecArray): string | null
182
+ /** what the link says on hover */
183
+ title?(match: RegExpExecArray): string
184
+ }
185
+
186
+ /** One of a plugin's event types. */
187
+ export interface EventSpec {
188
+ /** what it is, and its `data`: for `workpad docs` */
189
+ description?: string
190
+ /** hand it to an agent unasked, like a form's answer (a message for the agent); default false */
191
+ handOut?: boolean
192
+ /**
193
+ * one from the agent is a message for the reader (a reply in a thread, a chat message): it waits
194
+ * for them until read, which the page records once its element (marked `data-wp-message` with the
195
+ * event's id) has been in view; default false
196
+ */
197
+ forReader?: boolean
198
+ }
199
+
200
+ export interface Project {
201
+ name: string
202
+ /** the project folder */
203
+ dir: string
204
+ /** its .workpad folder */
205
+ wp: string
206
+ }
207
+
208
+ export interface PluginContext {
209
+ project: Project
210
+ /** the plugin's own folder, `.workpad/<name>/` (made, and ignored in git) */
211
+ dataDir: string
212
+ /**
213
+ * whether a request carries the project's token: for a check of its own, beyond a route's `auth`.
214
+ * No request (`slots` for a page being built) has none.
215
+ */
216
+ authorized(req: IncomingMessage | null): boolean
217
+ }
218
+
219
+ /**
220
+ * The type of a body or query field, or a command option: `string`, `number`, `boolean` or
221
+ * `string[]`, with `?` when it may be left out.
222
+ */
223
+ export type FieldType = 'string' | 'number' | 'boolean' | 'string[]' | 'string?' | 'number?' | 'boolean?' | 'string[]?'
224
+
225
+ /**
226
+ * The places on a page the core owns, for what a plugin puts there (each plugin its own element in
227
+ * each, in plugin order; see Api.slot): `header`, in the page's header bar before the theme button;
228
+ * `aside`, `bottom` and `overlay`, at the end of <body>, laid out as their content says (a panel
229
+ * beside the text, a drawer, something over the page); `corner`, the column of floating labels at
230
+ * the bottom right.
231
+ */
232
+ export type SlotName = 'header' | 'aside' | 'bottom' | 'overlay' | 'corner'
233
+
234
+ /**
235
+ * What a plugin's `slots` gives a page on the server: `head` at the end of <head>, and markup for
236
+ * its element in each slot but `header` (the header bar is the page's own render; a plugin adds to
237
+ * it in the browser).
238
+ */
239
+ export type SlotMarkup = { [K in 'head' | Exclude<SlotName, 'header'>]?: string | Html }
240
+
241
+ export interface PluginHooks {
242
+ /** by method and path, `'POST /start'`: mounted at `/api/<name>/start` */
243
+ routes?: Record<string, Route>
244
+ /**
245
+ * Markup for the page's slots (SlotMarkup), on the server: there on the first render, so an
246
+ * element in it can take part in the view transition between two pages. The browser module then
247
+ * mounts on its element in each slot (Api.slot). Called for every page; null adds nothing.
248
+ *
249
+ * Something that covers an edge of the window (a drawer at the bottom) says how much of it, as a
250
+ * length in `--wp-inset-bottom` on :root, registered with `@property` so that it computes to
251
+ * pixels. The page keeps its own fixed elements clear of it and counts what is behind it as out
252
+ * of view.
253
+ *
254
+ * `req` is the browser's request for the page; null for a page `workpad build` writes, which no
255
+ * browser asked for: what is only for the project's own browser (the terminal's drawer, behind
256
+ * `authorized`) has nothing to add there.
257
+ */
258
+ slots?(req: IncomingMessage | null): SlotMarkup | null | Promise<SlotMarkup | null>
259
+ /**
260
+ * A browser module whose default export is an `PluginMount`: on every page, the core imports
261
+ * it once the page has mounted, and hands it the page and its api. For
262
+ * behaviour on the page's own content (its forms, say); markup of the plugin's own can come
263
+ * from `slots` instead. A file in the plugin's public folder: `'client.js'`.
264
+ */
265
+ client?: URL | string
266
+ }
267
+
268
+ export interface Route {
269
+ description: string
270
+ /** needs the project's token: for anything that runs code. The core refuses a request without it */
271
+ auth?: boolean
272
+ /** the JSON body's fields; any other field is refused */
273
+ body?: Record<string, FieldType>
274
+ /** the query's fields */
275
+ query?: Record<string, FieldType>
276
+ /** writes to `res` itself, for as long as it likes (server-sent events): `run` gets `res`, its result is ignored */
277
+ stream?: boolean
278
+ /**
279
+ * What the route does. Its result is sent as JSON (`{ ok: true }` for none); an error it throws is
280
+ * sent as `{ error }` with the error's `status` (default 500).
281
+ */
282
+ run(request: RouteRequest): unknown | Promise<unknown>
283
+ }
284
+
285
+ export interface RouteRequest {
286
+ body: Record<string, unknown>
287
+ query: Record<string, unknown>
288
+ req: IncomingMessage
289
+ /** only for a stream route */
290
+ res?: ServerResponse
291
+ }
292
+
293
+ /** `workpad <plugin> [sub] …` */
294
+ export interface Command {
295
+ description: string
296
+ /** the positional arguments, for the help: `[page]` */
297
+ args?: string
298
+ /** `--name`: its type, what it means, and for the help a word for its value (`value: 'id'` shows as `--name <id>`) */
299
+ options?: Record<string, { type: FieldType; description?: string; value?: string; short?: string }>
300
+ /** takes arguments after `--`, passed on untouched (`ctx.rest`); for the help, what they are */
301
+ rest?: string
302
+ /**
303
+ * Reserved, for a feature to come: a page's own inline scripts may call a command marked live.
304
+ * Accepted and checked (a boolean), and does nothing yet.
305
+ */
306
+ live?: boolean
307
+ run(ctx: CommandContext): void | Promise<void>
308
+ }
309
+
310
+ export interface CommandContext {
311
+ /** the options, parsed and checked by their declaration */
312
+ options: Record<string, unknown>
313
+ positionals: string[]
314
+ /** what followed `--`, untouched (only for a command that declares `rest`) */
315
+ rest: string[]
316
+ project: Project
317
+ /** the project's event log, for `@bigmistqke/workpad/log` (`read(ctx.eventsFile)`, …) */
318
+ eventsFile: string
319
+ /** the plugin's own folder, `.workpad/<name>/` (a config's command: `.workpad/<command>/`) */
320
+ dataDir: string
321
+ /** the project's token, for the requests the command sends (header `x-workpad-token`) */
322
+ token: string
323
+ /** the base URL of the project's server, started when it isn't running */
324
+ server(): Promise<string>
325
+ /**
326
+ * the project's server if it runs, never started: its base URL and the version it runs; null when
327
+ * it doesn't. One that accepts connections but doesn't answer (stuck) is an error that says so.
328
+ */
329
+ running(): Promise<{ base: string; version: string } | null>
330
+ /** the version of workpad this command runs */
331
+ version: string
332
+ /**
333
+ * The project's state as data, and what an agent changes of it: the client of
334
+ * `@bigmistqke/workpad/api`, connected to the project's server (started at its first call when
335
+ * it isn't running), writing as whoever runs the command (`--agent`, `WORKPAD_AGENT`, the
336
+ * session a plugin names). `await ctx.wp.forms({ state: 'open' })`, `ctx.wp.hold(form, reason)`.
337
+ */
338
+ wp: Workpad
339
+ /** the id of a page given as its name or its file (`plan`, `.workpad/pages/plan.md`): `plan` */
340
+ page(page: string): string
341
+ /** the path of a page or file of the project, as the server serves it: `/plan` */
342
+ pagePath(page?: string): string
343
+ /** a link that gives a browser the project's token, then goes to `path` */
344
+ authLink(path: string): Promise<string>
345
+ /** what is open on each page (or on `page` and the pages below it), as `workpad status` reports it */
346
+ status(page?: string): Promise<PageStatus[]>
347
+ /**
348
+ * Hand the events on `page` (and below it, unless `exact`) or of `type` to `onEvent`, as `workpad
349
+ * listen` does, for agent `agent`: the reader's and other agents', never its own; also those from
350
+ * before, each counted as received (by `agent`) from then on; one it received before and not handled
351
+ * again (`again`). With `follow: false` only those there are now. Resolves with how many.
352
+ * For the log's other primitives, import `@bigmistqke/workpad/log`.
353
+ */
354
+ handOut(
355
+ filter: { page?: string | null; type?: string | null; exact?: boolean },
356
+ onEvent: (event: WpEvent, again: boolean) => void,
357
+ options?: {
358
+ count?: number
359
+ follow?: boolean
360
+ signal?: AbortSignal
361
+ mark?: boolean
362
+ agent?: string | null
363
+ /** an event id: of the backlog before it, what someone received is skipped, the reader's aside (channelSince) */
364
+ skipReceivedBefore?: string
365
+ },
366
+ ): Promise<number>
367
+ /**
368
+ * Listen on `page` at the project's server as `agent`, as `workpad listen` does, for as
369
+ * long as the command runs: the page and `workpad status` show who listens. Any number listen on
370
+ * a page; resolves once the server holds it.
371
+ */
372
+ listen(page: string, options?: { agent?: string | null }): Promise<void>
373
+ /**
374
+ * Be the channel of agent `session`, a plugin's command that pushes answers into it, for as long
375
+ * as the command runs. Whether its messages reach the agent only the agent can say: `workpad
376
+ * ping` in the session calls `onPing` with a code, which the channel pushes into the session as a
377
+ * pong (`pong()` of src/agents.ts says it); the agent confirms with `workpad agent ping <code>`, and
378
+ * `onConfirmed` is called. Until then a channel listens on nothing (`listen`), so the pages say
379
+ * nobody listens and a wait can take them; once confirmed, its messages count as delivered.
380
+ * `move`: the session changed (a resume).
381
+ */
382
+ channel(session: string, handlers: { onPing(code: string): void; onConfirmed(): void }): { move(session: string): void }
383
+ }
384
+
385
+ /** What is open on one page. */
386
+ export interface PageStatus {
387
+ page: string
388
+ /** what the reader knows it by: its first heading, else its name */
389
+ title: string
390
+ /** the page's file */
391
+ file: string
392
+ /** who subscribed to the page (`workpad new`, `workpad listen --page`), in the order they did */
393
+ subscribers: string[]
394
+ /** the event types the page waits on the reader for: its named forms */
395
+ asks: string[]
396
+ /** where each of them stands, as the page shows it: open, pending, received, handled, held or settled */
397
+ questions: { name: string; state: FormState }[]
398
+ /** those waiting for the reader: no answer yet, or a running wait asks for a new one */
399
+ open: string[]
400
+ /**
401
+ * those an agent put on hold (`workpad hold`), with why, when and by whom: they wait on
402
+ * something else, not on the reader, so they are not among `open`
403
+ */
404
+ held: { name: string; reason: string; since: string; by: string | null }[]
405
+ /** the open ones no running wait or watch (but the fallback) takes */
406
+ unheard: string[]
407
+ /** the agent's messages on the page the reader hasn't read (see EventSpec.forReader) */
408
+ unread: WpEvent[]
409
+ /** the reader's events on the page that no wait, watch or hook received */
410
+ unreceived: WpEvent[]
411
+ /** the reader's events an agent received that the agent hasn't handled (marked handled, or replied to) */
412
+ unhandled: WpEvent[]
413
+ /** the chats on the page whose latest message is the reader's: waiting for the agent's reply */
414
+ awaitingReply: string[]
415
+ /** the running waits and watches that cover the page */
416
+ listeners: Listener[]
417
+ }
418
+
419
+ // ---- pages ---------------------------------------------------------------------------------------
420
+
421
+ export interface Rendered {
422
+ /** the page's HTML: a string, or built with `html` (the core turns it into a string once) */
423
+ html: string | Html
424
+ title?: string
425
+ /** extra markup for <head>: stylesheets, meta */
426
+ head?: string | Html
427
+ /** JSON handed to the client with the HTML, e.g. what to mount where */
428
+ data?: unknown
429
+ /**
430
+ * What is wrong on the page, for whoever wrote it, one sentence each: a block nobody brings, a
431
+ * form's input outside any form. Shown above the page, and listed by `workpad check`.
432
+ */
433
+ problems?: string[]
434
+ }
435
+
436
+ /** A render as the browser gets it: the core has turned its HTML into strings. */
437
+ export interface RenderedPage extends Rendered {
438
+ html: string
439
+ head?: string
440
+ /** the page's frontmatter (see PageHandle.frontmatter) */
441
+ frontmatter: Record<string, unknown>
442
+ }
443
+
444
+ /** A page as the core stores it: what is in the file, read as is. */
445
+ export type StoredPage = PageSource | FileSource | FolderSource | PagesSource
446
+
447
+ interface StoredBase {
448
+ /** "plan", "task/plan", "-/docs/guide.md" for a repository file, "" for the overview */
449
+ id: string
450
+ /** absolute path on disk */
451
+ file: string
452
+ /** last modified, ms */
453
+ mtime: number | null
454
+ }
455
+
456
+ /** A page: a markdown file in .workpad/pages/, or one in the repository. */
457
+ export interface PageSource extends StoredBase {
458
+ kind: 'page'
459
+ source: string
460
+ /** `source` without its frontmatter: the page's content */
461
+ body: string
462
+ /** the frontmatter's fields, when the file starts with one (`source` keeps it): YAML, as parsed */
463
+ frontmatter: Record<string, unknown>
464
+ /** why the frontmatter couldn't be read (it is then empty), shown on the page */
465
+ frontmatterError?: string | null
466
+ }
467
+
468
+ /** Any other text file in the repository. */
469
+ export interface FileSource extends StoredBase {
470
+ kind: 'file'
471
+ source: string
472
+ }
473
+
474
+ /** A folder in the repository. */
475
+ export interface FolderSource extends StoredBase {
476
+ kind: 'folder'
477
+ entries: { name: string; dir: boolean }[]
478
+ }
479
+
480
+ /** A page, as the shared server's index gives it (/api/index, the overview). */
481
+ export interface IndexPage {
482
+ page: string
483
+ title: string
484
+ url: string
485
+ /** when its file last changed, in ms */
486
+ mtime: number
487
+ /** its questions waiting for the reader */
488
+ open: string[]
489
+ /** the agent's messages on it the reader hasn't read */
490
+ unread: number
491
+ /** the reader's answers no agent received */
492
+ unreceived: number
493
+ }
494
+
495
+ /** A project, as the shared server's index gives it (/api/index, the overview). */
496
+ export interface IndexProject {
497
+ entry: string
498
+ group: string
499
+ name: string
500
+ /** how the reader knows it: `develop`, `worktrees › cmux` */
501
+ label: string
502
+ dir: string
503
+ base: string
504
+ /** its pages, the latest changed first */
505
+ pages: IndexPage[]
506
+ /** when one of its pages last changed; 0 without pages */
507
+ active: number
508
+ error?: string
509
+ }
510
+
511
+ /** The overview, or a folder of pages without an index.md. */
512
+ export interface PagesSource extends StoredBase {
513
+ kind: 'pages'
514
+ /** `awaiting`: how many things on the page wait for the reader: open questions, unread messages */
515
+ pages: { id: string; file: string; mtime: number; lastEvent: string | null; awaiting: number }[]
516
+ /**
517
+ * The shared server's overview at `/` (see Entries in DESIGN.md): every project of its entries,
518
+ * with its pages, laid out as columns (src/pages/board.ts); `note` is said under them (the
519
+ * server's version).
520
+ */
521
+ projects?: (IndexProject & { files: boolean })[]
522
+ note?: string
523
+ }
524
+
525
+ /** What the core offers the pages on the server: how files are served and linked. */
526
+ export interface RenderContext {
527
+ /**
528
+ * `files`: whether its own files are served; `shows`: which of them (a path in the project
529
+ * folder, with `/`), as its `files` setting says
530
+ */
531
+ project: { name: string; dir: string; files: boolean; shows?: (rel: string) => boolean }
532
+ /**
533
+ * The URL the browser loads a file from: a client module, a stylesheet, an image next to a
534
+ * block. Files are served only once asked for, and are watched from then on:
535
+ * a stylesheet reloads in place, a browser module reloads the page.
536
+ */
537
+ asset(file: string | URL): string
538
+ /**
539
+ * The URL that shows a file or folder on disk (also `file.md` for `file`), or null when it does
540
+ * not exist or nothing serves it. For relative links: resolve them from the page's folder first.
541
+ */
542
+ urlFor(file: string): string | null
543
+ /** false in a build (`workpad build`): the page is a file, with no updates and no events */
544
+ live?: boolean
545
+ /** in a build, the plugins and blocks left out: their blocks render as nothing */
546
+ skip?: ReadonlySet<string>
547
+ /**
548
+ * The path the project is served under: '' for a server of its own, `/rg/develop` under a shared
549
+ * server (see Entries). Every URL the server makes starts with it; `asset` and `urlFor` add it.
550
+ */
551
+ base?: string
552
+ /** under a shared server: the entry the project is in, and how the project is known in it (`worktrees › cmux`) */
553
+ entry?: { name: string; label?: string }
554
+ }
555
+
556
+ /**
557
+ * The default export of a plugin's browser module (from `setup`): the page's root, the page, and its
558
+ * api. It runs once per page load, and is never unmounted: going to another page loads a new one.
559
+ */
560
+ export type PluginMount = (root: HTMLElement, page: PageHandle, api: Api) => void | Promise<void>
561
+
562
+ /** The page, in the browser. */
563
+ export interface PageHandle {
564
+ id: string
565
+ kind: StoredPage['kind']
566
+ /**
567
+ * false on a built page (`workpad build`): a file, with no server behind it, so no events, no
568
+ * listeners and no updates; only what is for reading runs.
569
+ */
570
+ live: boolean
571
+ /** what the server render returned with the HTML now in the root; it changes with each live update */
572
+ data: Reactive<unknown>
573
+ /**
574
+ * The page's frontmatter: YAML between `---` lines at its start, as parsed; empty for a page
575
+ * without one, one that doesn't parse, and for the overview, folders and code. It changes with a
576
+ * live update that changed it, once the new page is in place (`onChange` comes before, with the
577
+ * old one still here). The top level is the core's and other tools' (`title`, `tags`); a plugin's
578
+ * own is under `plugins:`, by its name, and is its `settings`.
579
+ */
580
+ frontmatter: Reactive<Record<string, unknown>>
581
+ /**
582
+ * For a plugin's browser module, its own entry in the page's frontmatter, under `plugins:` by its
583
+ * name, as parsed: `plugins: { toc: true }` is `true` for toc, `plugins: { toc: { maxWidth: 240px } }`
584
+ * an object; undefined when the page says nothing for it (and for the page module). It changes
585
+ * when that entry does.
586
+ */
587
+ settings: Reactive<unknown>
588
+ /** called when the stored page changed; returns an unsubscribe */
589
+ onChange(fn: () => void): () => void
590
+ /** the server render, afresh */
591
+ render(): Promise<RenderedPage>
592
+ /** the page as stored, afresh */
593
+ stored(): Promise<StoredPage>
594
+ }
595
+
596
+ // ---- events ------------------------------------------------------------------------------------
597
+
598
+ export interface WpEvent {
599
+ id: string
600
+ ts: string
601
+ page: string
602
+ /** what kind of event: for a submitted form, the form's name */
603
+ type: string
604
+ /** the button that submitted a form; null for other events */
605
+ action: string | null
606
+ data: Record<string, unknown>
607
+ /** 'cli' for events the command line appended (`workpad append`), such as an agent's reply */
608
+ from?: 'cli'
609
+ /**
610
+ * who appended it, when the command line knows: `--agent`, else `WORKPAD_AGENT`, else the agent
611
+ * session a plugin names. An agent's events reach every listener but this identity.
612
+ */
613
+ by?: string
614
+ /** in the browser: a wait or watch handed this event to an agent */
615
+ received?: true
616
+ /** in the browser: the agent handled this event (acknowledged it, or replied to it) */
617
+ handled?: true
618
+ /**
619
+ * in the browser: appended here and shown at once, but the server hasn't confirmed it yet. Show
620
+ * it as on its way ("sending…"), not as done. Should it fail, it leaves the events again.
621
+ */
622
+ sending?: true
623
+ }
624
+
625
+ /**
626
+ * Someone listening for answers right now: a running `workpad listen`, or a plugin's channel.
627
+ * Any number listen on a page, and each hears everything there.
628
+ */
629
+ export interface Listener {
630
+ /** the page it listens on, with the pages below it; '' for every page */
631
+ page: string
632
+ /** only this event type (a form's name), or null for all */
633
+ type: string | null
634
+ /** who listens: the agent's id (`--agent`, `WORKPAD_AGENT`, a plugin's session); null for an unnamed one */
635
+ agent: string | null
636
+ /** the name the agent gave itself (`workpad agent name`), when it did: as the server lists it */
637
+ name?: string
638
+ }
639
+
640
+ /** How a toast looks and acts (Api.toast). */
641
+ export interface ToastOptions {
642
+ /** its colour: `info` (the accent, the default), `warn`, or `alert` (as for what is new) */
643
+ kind?: 'info' | 'warn' | 'alert'
644
+ /** milliseconds before it goes, 7000 by default; 0: it stays until closed through its `Toast` */
645
+ ms?: number
646
+ /** what a click on it does (go to what it is about); a toast that goes by itself also closes */
647
+ onClick?: () => void
648
+ /** gives it a ✕, which runs this and closes it */
649
+ onDismiss?: () => void
650
+ /** a name to find it by (`data-toast`), for its styles and for tests */
651
+ name?: string
652
+ /** where it is among the corner's toasts: lower first (default 0) */
653
+ order?: number
654
+ /**
655
+ * more about it, for whoever wants to know: the toast stays a short line, with a ▸ that unfolds
656
+ * this under it (and keeps it open while it is read). Keep `text` a few words, and put the why here.
657
+ */
658
+ details?: string
659
+ }
660
+
661
+ /** A toast on the screen: change what it says (a count), hide it and show it again, or close it. */
662
+ export interface Toast {
663
+ close(): void
664
+ update(text: string): void
665
+ hide(): void
666
+ show(): void
667
+ }
668
+
669
+ /**
670
+ * A value that changes: call it for what it is now, and `.on(fn)` to hear each change, with the
671
+ * new value (it returns an unsubscribe). Every changing value of the api is one, so a framework
672
+ * can turn any of them into its own reactive value with one helper (the Solid one does).
673
+ */
674
+ export interface Reactive<T> {
675
+ (): T
676
+ on(fn: (value: T) => void): () => void
677
+ }
678
+
679
+ /** What the core gives the page and every plugin in the browser. Framework-free: its changing values are Reactive. */
680
+ export interface Api {
681
+ /** this page's id */
682
+ page: string
683
+ /**
684
+ * A path of the project's server as this page requests it: `url('/api/<plugin>/x')`. Under a shared
685
+ * server the project is served below a path (`/rg/develop`), so a plugin's client asks through this
686
+ * rather than writing `/api/…` itself.
687
+ */
688
+ url(path: string): string
689
+ /** every event recorded for this page, oldest first */
690
+ events: Reactive<WpEvent[]>
691
+ /** the latest event of a type, e.g. the last submit of a form */
692
+ last(type: string): WpEvent | undefined
693
+ /** record an event for this page: it lands in the log, where the agent reads it */
694
+ append(type: string, data?: Record<string, unknown>, action?: string | null): Promise<WpEvent>
695
+ /** what became of the reader's event: received by an agent, handled by the agent */
696
+ marks: Marks
697
+ /**
698
+ * The agent's messages on this page the reader hasn't read, oldest first (see
699
+ * EventSpec.forReader). An element marked `data-wp-message="<event id>"` reads its message once
700
+ * it has been in view; `markRead` does so for messages read otherwise (a thread closed).
701
+ */
702
+ unread: Reactive<WpEvent[]>
703
+ markRead(ids: string[]): void
704
+ /**
705
+ * A message's markdown (a comment, a chat message) as HTML, rendered by the server like a page:
706
+ * code fences highlighted, text links (`#12`), but raw HTML as text and no directives. Cached.
707
+ */
708
+ markdown(text: string): Promise<string>
709
+ /**
710
+ * An agent's chat message as HTML: a small page, rendered by the server with every block, as on
711
+ * the page (its forms named in its chat, `<chat>/<name>`). Its blocks' scripts mount once it is in
712
+ * the page. Cached by its id and text, so an edited one renders again.
713
+ */
714
+ message(event: WpEvent): Promise<string>
715
+ /**
716
+ * Markdown with every block, as HTML, rendered by the server as if it were written on this page:
717
+ * for what a plugin's client draws itself, with workpad's blocks in it (a chat under each of
718
+ * its own notes: `::chat{name=…}`). Its blocks' scripts mount once the HTML is in the page's
719
+ * content. Cached by text.
720
+ */
721
+ blocks(markdown: string): Promise<string>
722
+ /** the agents waiting for answers on this page right now */
723
+ listeners: Reactive<Listener[]>
724
+ /**
725
+ * Say that the page's content changed in place: the page module calls it after each patch on an update
726
+ * (it morphs it) calls this after each patch, so that plugins working on the content
727
+ * can put back what they add to it.
728
+ */
729
+ rendered(): void
730
+ /** called after each `rendered()`; returns an unsubscribe */
731
+ onRendered(fn: () => void): () => void
732
+ /**
733
+ * Mount on this plugin's own element in a slot (SlotName): `mount` is called with it, holding
734
+ * what the plugin's `slots` rendered there on the server, or empty, and what it returns is called
735
+ * when the element is taken away. The plugin never makes or looks up the element. Returns what
736
+ * takes it away. A page without the slot (raw text has no header bar) mounts nothing.
737
+ */
738
+ slot(name: SlotName, mount: (el: HTMLElement) => void | (() => void)): () => void
739
+ /**
740
+ * A count in the tab title, under `key`: `(text)` in front of the page's title; null removes it.
741
+ * Lower `order` comes first (default 0), so what waits for the reader can lead.
742
+ */
743
+ badge(key: string, text: string | null, order?: number): void
744
+ /** the page's own title, behind the badges */
745
+ title(title: string): void
746
+ /**
747
+ * A short label in the page's corner that says what is going on and takes the reader there:
748
+ * what waits for them, what is new, an answer nobody listens for. It goes after a few seconds,
749
+ * or on a click, which runs `onClick` (go to what it is about); with `ms: 0` it stays, and what
750
+ * this returns changes its text, hides and shows it, or closes it.
751
+ */
752
+ toast(text: string, options?: ToastOptions): Toast
753
+ /**
754
+ * The page's named forms and where each stands. The core looks after every `<form name>` without
755
+ * an `action`: it sends a submit as an event, keeps drafts, puts the last answer back, and marks
756
+ * each form with its state (`data-wp-open`, `-pending`, `-received`, `-handled`, `-held`, `-settled`;
757
+ * a held form shows why in `data-wp-held`). How that
758
+ * looks is the page's styles'; this is for drawing more than the attributes allow (a summary, a
759
+ * count). It changes when the forms are marked again: the log, the listeners or the page changed.
760
+ */
761
+ forms: Reactive<FormStatus[]>
762
+ }
763
+
764
+ /**
765
+ * Where a named form stands:
766
+ * - open: no answer yet (or an agent asks again): it waits for the reader
767
+ * - pending: answered, but nobody has picked the answer up yet
768
+ * - received: someone has its latest answer
769
+ * - handled: someone is done with its latest answer
770
+ * - held: an agent put it on hold, with a reason (`workpad hold`, "waits for !2145"): it waits on
771
+ * something else, not on the reader; released (`workpad release`), it is open again
772
+ * - settled: answered elsewhere, not by the form: an agent settled it (`workpad settle`), or it is
773
+ * a form in a message in the chat under a form that is decided (handled); it asks nothing any more
774
+ */
775
+ export type FormState = 'open' | 'pending' | 'received' | 'handled' | 'held' | 'settled'
776
+
777
+ /** What became of an answer: received (a wait or watch printed it, or the agent said so), handled (the agent acted on it). */
778
+ export interface Marks {
779
+ received(id: string): boolean
780
+ handled(id: string): boolean
781
+ }
782
+
783
+ export interface FormStatus {
784
+ form: HTMLFormElement
785
+ /** the form's name: the type of its answers */
786
+ name: string
787
+ state: FormState
788
+ /** its latest answer */
789
+ last: WpEvent | undefined
790
+ /** why it is held, when it is (state `held`) */
791
+ hold: { reason: string; since: string; by: string | null } | null
792
+ }
793
+
794
+ // ---- blocks ------------------------------------------------------------------------------------
795
+
796
+ /**
797
+ * A block handles the directives with its name (its key in a plugin's `blocks`): `:::name`,
798
+ * `::name` and `:name`; and, when it says so (`fence`), the code fences of that language,
799
+ * ```` ```name ````. Server functions run while the page renders; `client` runs in the browser.
800
+ *
801
+ * Its files are in its plugin's public folder, named within it, so they work wherever it is
802
+ * installed: `client: 'chart.js'`, `style: 'chart.css'`.
803
+ */
804
+ /**
805
+ * What an agent writing a page is trying to do, the menu `workpad docs` groups blocks by: ask the
806
+ * reader something, show them something, arrange several things (or one out of the way), mark how
807
+ * something stands or how much it counts.
808
+ */
809
+ export type BlockGroup = 'ask' | 'show' | 'arrange' | 'mark'
810
+
811
+ export interface Block<Props = unknown> {
812
+ /**
813
+ * Its line on the menu `workpad docs` prints: the group it is under, and what it is for in a
814
+ * few words (`'side by side'`). Without it, it is only in its plugin's docs: a form's inputs, a
815
+ * block the core writes itself.
816
+ */
817
+ menu?: { group: BlockGroup; summary: string }
818
+ /**
819
+ * Which code fences it renders. `true`: those of its name (```` ```mermaid ````). `'any'`: those
820
+ * too, and every fence no block claims by name, and the code view of repository files: the block
821
+ * for code in general, such as a syntax highlighter (when several set it, the last plugin's wins).
822
+ * Without it, a fence of its name is code like any other: ```` ```text ```` is no text field.
823
+ */
824
+ fence?: boolean | 'any'
825
+ /**
826
+ * It renders the alerts markdown writes as a quote that starts with its type, as GitHub, Obsidian
827
+ * and most markdown tools read them: `> [!WARNING] A title` and the quote's lines below. Such a
828
+ * block is no directive: `:::name` stays unknown. Its context has the type in `attrs.type`
829
+ * (lowercase), the title after it as `label`, and the rest of the quote as `children`. When several
830
+ * set it, the last plugin's wins; without one, an alert is a quote like any other.
831
+ */
832
+ alert?: boolean
833
+ /**
834
+ * The attributes it reads (`{cols=2}`, ```` ```ansi {wrap} ````). When it says, any other is
835
+ * reported as doing nothing there (`workpad check`, and above the page), besides those every
836
+ * block takes: `compose` and the composers' names, `title`, `id`, `class`. Without it, none is.
837
+ */
838
+ attrs?: string[]
839
+ /** props for the client; JSON-serialisable. Without it a client gets the defaults (see DirectiveProps / FenceProps). */
840
+ props?(ctx: ServerContext): Props | Promise<Props>
841
+ /**
842
+ * HTML for the block, as a string or built with `html`. Without `client` this is the whole
843
+ * output; with it, what shows until it mounts.
844
+ */
845
+ html?(ctx: ServerContext): string | Html | null | Promise<string | Html | null>
846
+ /**
847
+ * What the block shows, on its own: an image's file, a page's address: what a plugin composed
848
+ * around it gets as `open` (Plugin.compose), the window's ↗. Without it, the window has no ↗.
849
+ */
850
+ open?(ctx: ServerContext): string | null
851
+ /** a browser module whose default export is a `Mount`, in its plugin's public folder: `'chart.js'` */
852
+ client?: URL | string
853
+ /** a stylesheet linked on every page that uses the block, in its plugin's public folder: `'chart.css'` */
854
+ style?: URL | string
855
+ }
856
+
857
+ /** A block with typed props, for one written apart from its plugin. */
858
+ export const defineBlock = <Props = unknown>(block: Block<Props>): Block<Props> => block
859
+
860
+ /**
861
+ * Default export of a block's `client` module: mounted once per block, again with new props when
862
+ * the block changes. Return a cleanup, or nothing: it runs when the block is replaced or removed.
863
+ */
864
+ export type Mount<Props = unknown> = (
865
+ element: HTMLElement,
866
+ props: Props,
867
+ api: ClientApi,
868
+ ) => void | (() => void) | Promise<void | (() => void)>
869
+
870
+ // ---- server context ----------------------------------------------------------------------------
871
+
872
+ /**
873
+ * One of a block's items (DirectiveContext.items): what a grid makes a card of, tabs a tab. Each is
874
+ * a container directly in the block, `:::item[Title]` … `:::`, the block one colon more than its
875
+ * items (`::::grid`). A `:::form` or a `:::details` directly in the block is an item as it is: the
876
+ * block itself its content, its label the title. What is outside any item shows as an untitled item
877
+ * of its own, and is a problem: a heading or a `---` starts none.
878
+ */
879
+ export interface Item {
880
+ /** the label as HTML; null for an item without one */
881
+ title: string | null
882
+ /** the label's own words as plain text, without the inline blocks in it (a badge, a `:when`); null without one */
883
+ text: string | null
884
+ /** the label as mdast nodes, as written (before it rendered), for a block that takes it apart (a `:when` in it) */
885
+ label: unknown[] | null
886
+ /** the item's content as HTML: for a form or a details, the whole block, its label too */
887
+ html: string
888
+ /** the same as mdast nodes, for a block that takes it apart (its first picture, say) */
889
+ nodes: unknown[]
890
+ /** the block the item is as it is (`form`, `details`); null for an `:::item` */
891
+ block: string | null
892
+ /** stays the same while its title does (its slug), else its place: to remember an item across updates */
893
+ key: string
894
+ }
895
+
896
+ interface BaseContext {
897
+ /** page id, e.g. "plan", or "-/docs/guide.md" for a repository file */
898
+ page: string
899
+ /** absolute path of the markdown file, and its folder (resolve files next to the page with it) */
900
+ file: string
901
+ dir: string
902
+ /**
903
+ * The address of a file named relative to the page (an image's `src`): served, the server's; in a
904
+ * build, of a copy beside the built page. A URL, an address from the root, or a file that isn't
905
+ * there comes back as written.
906
+ */
907
+ fileUrl(src: string): string
908
+ /**
909
+ * This block's id, for links to it: `<type>:<name>` (`form:logger`, `tab:intro`), so `#form:logger`
910
+ * goes to it. A heading's id is its slug, which never holds a colon, so the two never meet; ids
911
+ * starting `wp-` are the page's own. Null when the page has that id already: a name the writer
912
+ * chose (a form's) goes to the first, and the block shows `anchorTaken` in its place; a name derived
913
+ * from text (`numbered`: a step's title) gets `-1`, `-2` and on instead, as a heading does.
914
+ */
915
+ anchor(type: string, name: string, options?: { numbered?: boolean }): string | null
916
+ /**
917
+ * In an agent's chat message, which renders as a small page: the chat it is in, and the number an
918
+ * unnamed form there takes next (`next()`: 1, 2, … in the chat). A form in it is named
919
+ * `<chat>/<name>`, so it never meets one of the page's. Null on the page itself.
920
+ */
921
+ chat: { name: string; next(): number } | null
922
+ name: string
923
+ attrs: Record<string, string>
924
+ parseAttrs(s: string): Record<string, string>
925
+ /** render mdast nodes to HTML; nested directives and fences go through their blocks */
926
+ toHtml(nodes: unknown[]): Promise<string>
927
+ toString(node: unknown): string
928
+ /** render a markdown string to HTML (blocks included) */
929
+ markdown(src: string): Promise<string>
930
+ /** like markdown(), without the wrapping <p> for a single paragraph */
931
+ inline(src: string): Promise<string>
932
+ /** what a client gets when the block has no props() */
933
+ defaults(): Promise<Record<string, unknown>>
934
+ }
935
+
936
+ /** `:::name[label]{attrs}` … `:::`, `::name[label]{attrs}`, `:name[label]{attrs}` */
937
+ export interface DirectiveContext extends BaseContext {
938
+ kind: 'containerDirective' | 'leafDirective' | 'textDirective'
939
+ /** label as HTML / plain text, null without one */
940
+ label: string | null
941
+ labelText: string | null
942
+ /** the block's content as mdast nodes */
943
+ children: unknown[]
944
+ /** the content as items: its `:::item[Title]` containers, and a form or a details directly in it (Item) */
945
+ items(): Promise<Item[]>
946
+ }
947
+
948
+ /** ```name attrs */
949
+ export interface FenceContext extends BaseContext {
950
+ kind: 'fence'
951
+ text: string
952
+ /** the fence's language (its first word); for a fence claimed by name it equals `name`, null when there is none */
953
+ lang: string | null
954
+ }
955
+
956
+ export type ServerContext = DirectiveContext | FenceContext
957
+
958
+ /** What a plugin's `compose` gets: the block it goes around, and that block's HTML as `children`. */
959
+ export interface ComposeContext {
960
+ /** the block's HTML: what is composed in */
961
+ children: Html
962
+ /** the block's name: `image`, `mermaid` */
963
+ block: string
964
+ /** the block's attributes */
965
+ attrs: Record<string, string>
966
+ /** the block's label as HTML, null without one (fences have none) */
967
+ label: string | null
968
+ /** what the block shows on its own (Block.open): an image's file, a page's address; null without */
969
+ open: string | null
970
+ }
971
+
972
+ /** Default props of a directive without props(). */
973
+ export interface DirectiveProps {
974
+ name: string
975
+ attrs: Record<string, string>
976
+ label: string | null
977
+ html: string
978
+ /** its items (Item), without their mdast nodes */
979
+ items: Omit<Item, 'nodes' | 'label'>[]
980
+ }
981
+
982
+ /** Default props of a fence without props(). */
983
+ export interface FenceProps {
984
+ name: string
985
+ attrs: Record<string, string>
986
+ text: string
987
+ }
988
+
989
+ // ---- client api --------------------------------------------------------------------------------
990
+
991
+ export type Theme = 'light' | 'dark'
992
+
993
+ /** Passed to every Mount: the core's api, and the page's theme. */
994
+ export interface ClientApi extends Api {
995
+ /** the theme in effect (the reader's choice, else the system's) */
996
+ theme: Reactive<Theme>
997
+ }