@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
@@ -0,0 +1,225 @@
1
+ /* The terminal drawer (see terminal.ts). Pages morph into each other on navigation, and the drawer is
2
+ named, so it keeps its place and size while the page around it changes. */
3
+ @view-transition {
4
+ navigation: auto;
5
+ }
6
+
7
+ /* How much of the window the drawer takes at the bottom, for the page to keep clear: its content
8
+ scrolls to above it, and its own fixed elements sit above it (the page's buttons at the bottom).
9
+ Registered as a length so that it computes to pixels a script can read. */
10
+ @property --wp-inset-bottom {
11
+ syntax: '<length>';
12
+ inherits: true;
13
+ initial-value: 0px;
14
+ }
15
+
16
+ :root {
17
+ --wp-term-h: 320px;
18
+ --wp-term-bar: 34px;
19
+ }
20
+ :root:has(#wp-terminal) {
21
+ --wp-inset-bottom: var(--wp-term-bar);
22
+ }
23
+ :root:has(#wp-terminal[data-state='open']) {
24
+ --wp-inset-bottom: calc(var(--wp-term-bar) + var(--wp-term-h));
25
+ }
26
+
27
+ #wp-terminal {
28
+ view-transition-name: wp-terminal;
29
+ position: fixed;
30
+ left: 0;
31
+ right: 0;
32
+ bottom: 0;
33
+ z-index: var(--layer-top);
34
+ display: flex;
35
+ flex-direction: column;
36
+ background: #0f1115;
37
+ color: #d7dae0;
38
+ border-top: var(--hairline) solid #2a2f3a;
39
+ box-shadow: 0 -6px 24px rgb(0 0 0 / 0.18);
40
+ font:
41
+ 500 12px/1 ui-sans-serif,
42
+ system-ui,
43
+ sans-serif;
44
+ }
45
+ body:has(#wp-terminal) {
46
+ padding-bottom: var(--wp-inset-bottom);
47
+ }
48
+
49
+ .wp-term-grip {
50
+ position: absolute;
51
+ left: 0;
52
+ right: 0;
53
+ top: -4px;
54
+ height: 8px;
55
+ cursor: ns-resize;
56
+ touch-action: none;
57
+ }
58
+ .wp-term-bar {
59
+ height: var(--wp-term-bar);
60
+ display: flex;
61
+ align-items: center;
62
+ gap: var(--space-xl);
63
+ padding: 0 var(--space-lg) 0 var(--space-sm);
64
+ flex: none;
65
+ }
66
+ .wp-term-bar button {
67
+ font: inherit;
68
+ color: inherit;
69
+ background: transparent;
70
+ border: var(--hairline) solid transparent;
71
+ border-radius: var(--radius-sm);
72
+ padding: var(--space-xs) var(--space-md);
73
+ cursor: pointer;
74
+ }
75
+ .wp-term-bar button:hover {
76
+ background: #1c2029;
77
+ border-color: #2a2f3a;
78
+ }
79
+ .wp-term-toggle {
80
+ display: inline-flex;
81
+ align-items: center;
82
+ gap: var(--space-sm);
83
+ letter-spacing: var(--caps-spacing);
84
+ text-transform: uppercase;
85
+ }
86
+ /* points up while closed (it opens upward), down while open */
87
+ .wp-term-chevron {
88
+ width: 7px;
89
+ height: 7px;
90
+ border: solid currentColor;
91
+ border-width: 0 1.5px 1.5px 0;
92
+ rotate: 45deg;
93
+ translate: 0 -2px;
94
+ transition:
95
+ rotate 0.2s,
96
+ translate 0.2s;
97
+ }
98
+ #wp-terminal[data-state='collapsed'] .wp-term-chevron {
99
+ rotate: -135deg;
100
+ translate: 0 2px;
101
+ }
102
+ .wp-term-status {
103
+ color: #8b93a3;
104
+ flex: 1;
105
+ min-width: 0;
106
+ overflow: hidden;
107
+ text-overflow: ellipsis;
108
+ white-space: nowrap;
109
+ }
110
+ .wp-term-actions {
111
+ display: flex;
112
+ gap: var(--space-xs);
113
+ }
114
+
115
+ /* the tabs: one per session, the current one lit */
116
+ /* the bar: the toggle, what the current tab says (status, actions), then the tabs and "+" at the right */
117
+ .wp-term-tabs {
118
+ display: flex;
119
+ align-items: center;
120
+ gap: var(--space-2xs);
121
+ min-width: 0;
122
+ flex: 0 1 auto;
123
+ overflow-x: auto;
124
+ scrollbar-width: none;
125
+ }
126
+ .wp-term-bar .wp-term-add {
127
+ flex: none;
128
+ }
129
+ .wp-term-tab {
130
+ display: inline-flex;
131
+ align-items: center;
132
+ flex: none;
133
+ border-radius: var(--radius-sm);
134
+ }
135
+ .wp-term-bar .wp-term-tab button {
136
+ padding: var(--space-xs) var(--space-xs) var(--space-xs) var(--space-md);
137
+ }
138
+ .wp-term-bar .wp-term-tab .wp-term-tab-close {
139
+ padding: var(--space-xs) var(--space-sm) var(--space-xs) var(--space-2xs);
140
+ opacity: 0.45;
141
+ }
142
+ .wp-term-bar .wp-term-tab .wp-term-tab-close:hover {
143
+ opacity: 1;
144
+ background: transparent;
145
+ border-color: transparent;
146
+ }
147
+ .wp-term-bar .wp-term-tab button:hover {
148
+ background: transparent;
149
+ border-color: transparent;
150
+ }
151
+ .wp-term-tab:hover {
152
+ background: #1c2029;
153
+ }
154
+ .wp-term-tab.wp-current {
155
+ background: #232836;
156
+ color: #fff;
157
+ }
158
+ .wp-term-tab[data-exited] {
159
+ color: #8b93a3;
160
+ }
161
+ .wp-term-bar .wp-term-add {
162
+ padding: var(--space-2xs) var(--space-md);
163
+ font-size: 15px;
164
+ line-height: 1;
165
+ color: #8b93a3;
166
+ }
167
+ .wp-term-bar .wp-term-add:hover {
168
+ color: #d7dae0;
169
+ }
170
+
171
+ /* content-box: the fit addon reads this height as the room for rows, so the padding must not be in it */
172
+ .wp-term-screen {
173
+ height: calc(var(--wp-term-h) - 10px);
174
+ padding: var(--space-xs) 0 var(--space-sm) var(--space-lg);
175
+ box-sizing: content-box;
176
+ overflow: hidden;
177
+ }
178
+ /* the screen as the server wrote it into the page, over the terminal until that has drawn it */
179
+ .wp-term-screen {
180
+ position: relative;
181
+ }
182
+ .wp-term-placeholder {
183
+ position: absolute;
184
+ inset: 4px 0 6px 10px;
185
+ z-index: 1;
186
+ overflow: hidden;
187
+ background: #0f1115;
188
+ font:
189
+ 13px/1.2 ui-monospace,
190
+ SFMono-Regular,
191
+ Menlo,
192
+ monospace;
193
+ white-space: pre;
194
+ color: #d7dae0;
195
+ }
196
+ .wp-term-placeholder > div {
197
+ height: 1.2em;
198
+ }
199
+ /* the server writes every tab's screen; the current tab's shows */
200
+ .wp-term-placeholder:not(.wp-current) {
201
+ display: none;
202
+ }
203
+ #wp-terminal[data-state='collapsed'] .wp-term-screen {
204
+ height: 0;
205
+ padding: 0;
206
+ visibility: hidden;
207
+ }
208
+
209
+ ::view-transition-group(wp-terminal) {
210
+ animation-duration: 0.22s;
211
+ animation-timing-function: cubic-bezier(0.2, 0.7, 0.2, 1);
212
+ }
213
+ /* opening and closing: the pictures keep their scale and stay anchored at the bar, so the text
214
+ doesn't stretch while the drawer grows */
215
+ ::view-transition-old(wp-terminal),
216
+ ::view-transition-new(wp-terminal) {
217
+ height: 100%;
218
+ object-fit: none;
219
+ object-position: top;
220
+ }
221
+ @media (prefers-reduced-motion: reduce) {
222
+ ::view-transition-group(*) {
223
+ animation: none !important;
224
+ }
225
+ }
@@ -0,0 +1,266 @@
1
+ // What the terminal's routes do, forwarded to its host: the sessions (the drawer's tabs), starting
2
+ // one, its keystrokes and size, following its output, closing it, stopping them all.
3
+ //
4
+ // The sessions run in a process of their own (host.js), which this starts the first time a session
5
+ // starts and reaches over a Unix socket. So they outlive the page server: a restarted server
6
+ // connects to the same host and finds them as they were. Each open drawer gets its own connection to
7
+ // the host, which sends it the whole screen of its tab first and then follows the output.
8
+ //
9
+ // A session is the user's shell. A tab started with a program runs it in the shell, and when it
10
+ // exits the shell takes over in the same tab (`$SHELL -c '"$@"; exec "$0" -l' $SHELL <program>`),
11
+ // so leaving the program leaves a prompt, not a dead tab.
12
+ //
13
+ // No workpad in it: give it the folder the session runs in and a folder for its socket and log.
14
+ // It doesn't check who asks; whoever mounts it must (it runs code).
15
+ import fs from 'node:fs'
16
+ import net from 'node:net'
17
+ import os from 'node:os'
18
+ import path from 'node:path'
19
+ import crypto from 'node:crypto'
20
+ import { spawn } from 'node:child_process'
21
+ import { createRequire } from 'node:module'
22
+ import { fileURLToPath } from 'node:url'
23
+ import type { IncomingMessage, ServerResponse } from 'node:http'
24
+
25
+ /** A session, as a tab. */
26
+ export interface Session {
27
+ id: string
28
+ label: string
29
+ running: boolean
30
+ exited: number | null
31
+ started: string
32
+ }
33
+
34
+ /** The tabs, and whether the terminal can run (node-pty is installed). */
35
+ export interface Sessions {
36
+ available: boolean
37
+ sessions: Session[]
38
+ }
39
+
40
+ export interface StartOptions {
41
+ /** the session to start in; a new tab without one */
42
+ id?: string
43
+ /** a program to run in the shell, with its arguments; the shell alone without one */
44
+ program?: string[]
45
+ /** start again in `id`, running what it ran before, even when it still runs */
46
+ restart?: boolean
47
+ }
48
+
49
+ /** A reply from the host: one JSON object, its fields by request (see host.ts). */
50
+ type Reply = Record<string, any>
51
+
52
+ const require = createRequire(import.meta.url)
53
+ const HOST = fileURLToPath(new URL(`./host${path.extname(fileURLToPath(import.meta.url))}`, import.meta.url))
54
+
55
+ const hasPty = () => {
56
+ try {
57
+ require.resolve('node-pty')
58
+ return true
59
+ } catch {
60
+ return false
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Where the host listens: `host.sock` in the terminal's folder, or, when that path is too long for
66
+ * a Unix socket (about 100 bytes), a name in /tmp made from it.
67
+ */
68
+ export function hostSocket(home: string): string {
69
+ const own = path.join(home, 'host.sock')
70
+ return Buffer.byteLength(own) < 100
71
+ ? own
72
+ : path.join('/tmp', `wp-term-${crypto.createHash('sha1').update(home).digest('hex').slice(0, 12)}.sock`)
73
+ }
74
+
75
+ /** `dir` the folder the sessions run in, `home` where the socket and log go */
76
+ export function createTerminal({ dir, home }: { dir: string; home: string }) {
77
+ const own = hostSocket(home)
78
+ const log = path.join(home, 'host.log')
79
+
80
+ // ---- the host -------------------------------------------------------------------------------
81
+
82
+ /** A connection to the host, or null when none runs. */
83
+ function connect(): Promise<net.Socket | null> {
84
+ return new Promise(resolve => {
85
+ const conn = net.connect(own)
86
+ conn.once('connect', () => resolve(conn))
87
+ conn.once('error', () => resolve(null))
88
+ })
89
+ }
90
+
91
+ /** Send one request; its one reply, or null when no host runs. */
92
+ async function ask(req: Record<string, unknown>, timeout = 2000): Promise<Reply | null> {
93
+ const conn = await connect()
94
+ if (!conn) return null
95
+ return new Promise(resolve => {
96
+ let buf = ''
97
+ const done = (value: Reply | null) => {
98
+ clearTimeout(timer)
99
+ conn.destroy()
100
+ resolve(value)
101
+ }
102
+ const timer = setTimeout(() => done(null), timeout)
103
+ conn.setEncoding('utf8')
104
+ conn.on('data', (c: string) => {
105
+ buf += c
106
+ const nl = buf.indexOf('\n')
107
+ if (nl >= 0) {
108
+ try {
109
+ done(JSON.parse(buf.slice(0, nl)))
110
+ } catch {
111
+ done(null)
112
+ }
113
+ }
114
+ })
115
+ conn.on('error', () => done(null))
116
+ conn.on('end', () => done(null))
117
+ conn.write(JSON.stringify(req) + '\n')
118
+ })
119
+ }
120
+
121
+ /** Start the host (detached: it outlives this server) and wait until it answers. */
122
+ async function startHost(): Promise<boolean> {
123
+ const out = fs.openSync(log, 'a')
124
+ const child = spawn(process.execPath, [HOST, dir, own], {
125
+ cwd: dir,
126
+ detached: true,
127
+ stdio: ['ignore', out, out],
128
+ env: process.env,
129
+ })
130
+ child.unref()
131
+ fs.closeSync(out)
132
+ for (let i = 0; i < 40; i++) {
133
+ await new Promise(r => setTimeout(r, 100))
134
+ if (await ask({ t: 'list' }, 500)) return true
135
+ }
136
+ return false
137
+ }
138
+
139
+ const shell = process.env.SHELL || '/bin/sh'
140
+
141
+ /** The sessions, as tabs: `{ id, label, running, exited, started }` each. */
142
+ async function sessions(): Promise<Sessions> {
143
+ const reply = await ask({ t: 'list' })
144
+ return { available: hasPty(), sessions: reply?.sessions ?? [] }
145
+ }
146
+
147
+ /** The screens as HTML rows by session id, for a page's first render. */
148
+ async function screens(list: Session[]): Promise<Record<string, string>> {
149
+ const out: Record<string, string> = {}
150
+ await Promise.all(
151
+ list.map(async s => {
152
+ out[s.id] = (await ask({ t: 'html', id: s.id }, 300))?.html ?? ''
153
+ }),
154
+ )
155
+ return out
156
+ }
157
+
158
+ // ---- what the routes do --------------------------------------------------------------------
159
+
160
+ const failure = (message: string, status = 500) => Object.assign(new Error(message), { status })
161
+
162
+ /**
163
+ * Start a tab: the shell, or `program` in it (a new tab without `id`). In a running session it
164
+ * reports it (`already`) unless `restart`; restarting without a program runs what it ran before.
165
+ */
166
+ async function start({ id, program = [], restart = false }: StartOptions = {}) {
167
+ if (!hasPty()) throw failure('the terminal needs node-pty: npm install node-pty')
168
+ const request = program.length
169
+ ? {
170
+ t: 'start',
171
+ id,
172
+ command: shell,
173
+ args: ['-c', '"$@"; exec "$0" -l', shell, ...program],
174
+ label: path.basename(program[0]!),
175
+ restart,
176
+ }
177
+ : restart && id != null
178
+ ? { t: 'start', id, restart }
179
+ : { t: 'start', id, command: shell, args: ['-l'], label: path.basename(shell), restart }
180
+ let reply = await ask(request)
181
+ if (!reply) {
182
+ if (!(await startHost())) throw failure(`the terminal's host did not start; see ${log}`)
183
+ reply = await ask(request)
184
+ }
185
+ if (!reply || reply.error) throw failure(reply?.error ?? 'no answer from the terminal host')
186
+ return { available: true, session: reply.session ?? '1', already: !!reply.already }
187
+ }
188
+
189
+ const input = async (id: string, data: string) => {
190
+ await ask({ t: 'input', id, d: data })
191
+ }
192
+ /** The size that fits drawer `viewer`; `active`: the reader is using that drawer now (see host.ts, Sizes). */
193
+ const resize = async (id: string, viewer: string, cols: number, rows: number, active = false) => {
194
+ await ask({ t: 'resize', id, viewer, cols, rows, active })
195
+ }
196
+
197
+ /** End one session and drop its tab. */
198
+ async function close(id: string) {
199
+ await ask({ t: 'close', id })
200
+ return { ok: true }
201
+ }
202
+
203
+ /**
204
+ * Follow a session as server-sent events on `res`: `sessions` (the tabs) and `screen` first, then
205
+ * `data`, `exit` and `sessions` as they come.
206
+ */
207
+ async function stream(req: IncomingMessage, res: ServerResponse, id: string | undefined, viewer?: string) {
208
+ res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' })
209
+ res.write(': terminal\n\n')
210
+ const ping = setInterval(() => res.write(': ping\n\n'), 25000)
211
+ const conn = await connect()
212
+ if (!conn) {
213
+ // no host runs yet: say so, and have the browser ask again shortly, so that a tab started
214
+ // meanwhile (from the command line, say) shows up
215
+ clearInterval(ping)
216
+ res.end('retry: 2000\nevent: none\ndata: {}\n\n')
217
+ return
218
+ }
219
+ // the host's messages, one per line, as server-sent events of the same name
220
+ let buf = ''
221
+ conn.setEncoding('utf8')
222
+ conn.on('data', (chunk: string) => {
223
+ buf += chunk
224
+ let nl
225
+ while ((nl = buf.indexOf('\n')) >= 0) {
226
+ const line = buf.slice(0, nl)
227
+ buf = buf.slice(nl + 1)
228
+ try {
229
+ const { t, ...data } = JSON.parse(line)
230
+ res.write(`event: ${t}\ndata: ${JSON.stringify(data)}\n\n`)
231
+ } catch {
232
+ /* not a message */
233
+ }
234
+ }
235
+ })
236
+ // the host went away (stopped, or its program ended and it left): the page shows it as stopped
237
+ conn.on('close', () => {
238
+ if (!res.writableEnded) res.write('event: exit\ndata: {"code":null}\n\n')
239
+ })
240
+ conn.on('error', () => {})
241
+ req.on('close', () => {
242
+ clearInterval(ping)
243
+ conn.destroy()
244
+ })
245
+ conn.write(JSON.stringify({ t: 'attach', id, viewer }) + '\n')
246
+ }
247
+
248
+ /** End the session and its host. */
249
+ async function stop() {
250
+ return { stopped: !!(await ask({ t: 'stop' })) }
251
+ }
252
+
253
+ /**
254
+ * Show the page at `path` (a path on this server) in the browser tab whose drawer the reader used
255
+ * last: for session `id`, or without one, of all sessions.
256
+ */
257
+ async function open(id: string | undefined, path: string) {
258
+ if (!path.startsWith('/') || path.startsWith('//'))
259
+ throw failure('open takes a path on this server, like /plan', 400)
260
+ const reply = await ask({ t: 'open', id, path })
261
+ if (!reply) throw failure('no terminal host runs', 404)
262
+ return { shown: Number(reply.shown ?? 0) }
263
+ }
264
+
265
+ return { sessions, screens, start, input, resize, close, open, stream, stop }
266
+ }
@@ -0,0 +1,72 @@
1
+ // Whose size a session has. A session has one size, and several drawers (browser windows) can show
2
+ // it at once. It takes the size of the drawer the reader used last, its active viewer: the one that
3
+ // last reported being used (opening it, focus, a keystroke), else the first to report a size. The
4
+ // others show it at that size. When the active drawer goes, the session passes to the drawer used
5
+ // most recently of those left: after a moment, since a page switch reconnects the same window under
6
+ // a new viewer, which claims it again.
7
+ //
8
+ // No terminal in here: host.ts applies what these return.
9
+
10
+ export interface Sizes {
11
+ cols: number
12
+ rows: number
13
+ /** the drawers showing the session, by viewer id: the size that fits each, and when each was last used */
14
+ viewers: Map<string, { cols: number; rows: number; used: number }>
15
+ /** the viewer whose size the session has */
16
+ active: string | null
17
+ }
18
+
19
+ /**
20
+ * A drawer reports the size that fits it; `used`: the reader uses it now. Returns the size the
21
+ * session should have: its own when the drawer is (now) the active one, else the session's as it is.
22
+ */
23
+ export function report(
24
+ s: Sizes,
25
+ viewer: string,
26
+ cols: number,
27
+ rows: number,
28
+ used: boolean,
29
+ now = Date.now(),
30
+ ): { cols: number; rows: number } {
31
+ const known = s.viewers.get(viewer)
32
+ s.viewers.set(viewer, { cols, rows, used: used ? now : (known?.used ?? 0) })
33
+ if (used || !s.active || !s.viewers.has(s.active)) s.active = viewer
34
+ return s.active === viewer ? { cols, rows } : { cols: s.cols, rows: s.rows }
35
+ }
36
+
37
+ /** A drawer stopped showing the session. True when it was the active one: call `handOver` after a moment. */
38
+ export function leave(s: Sizes, viewer: string): boolean {
39
+ s.viewers.delete(viewer)
40
+ return s.active === viewer
41
+ }
42
+
43
+ /**
44
+ * After the active drawer `viewer` left: the drawer used most recently of those left becomes the
45
+ * active one. Returns the size the session should take, or null to leave it as it is (another drawer
46
+ * claimed the session in the meantime, or none is left).
47
+ */
48
+ export function handOver(s: Sizes, viewer: string): { cols: number; rows: number } | null {
49
+ if (s.active !== viewer || s.viewers.has(viewer)) return null
50
+ // the one used last: the first of them, as a stable sort by `used` would give
51
+ let next: [string, { cols: number; rows: number; used: number }] | undefined
52
+ for (const viewer of s.viewers) if (!next || viewer[1].used > next[1].used) next = viewer
53
+ s.active = next?.[0] ?? null
54
+ return next ? { cols: next[1].cols, rows: next[1].rows } : null
55
+ }
56
+
57
+ /**
58
+ * Of several sessions, the one whose active drawer the reader used last: where "the browser the
59
+ * reader is in" is, for a program that doesn't know its own tab. Undefined when no drawer is active.
60
+ */
61
+ export function lastUsed<S extends Sizes>(sessions: S[]): S | undefined {
62
+ let best: S | undefined
63
+ let at = -1
64
+ for (const s of sessions) {
65
+ const viewer = s.active ? s.viewers.get(s.active) : undefined
66
+ if (viewer && viewer.used > at) {
67
+ best = s
68
+ at = viewer.used
69
+ }
70
+ }
71
+ return best
72
+ }
@@ -0,0 +1,48 @@
1
+ Steps in order down a line, a dot per step: what was done and what it showed.
2
+ Each step has room for full sentences, unlike a diagram's boxes.
3
+
4
+ Each step is an `:::item[Title]` container, titled by its label; without a
5
+ label it is untitled. The timeline takes one colon more than its steps:
6
+ `::::timeline` around `:::item`. Headings in a step are the step's own, at
7
+ any level, and stay headings in its body ("What it does", "Result"). A
8
+ step's body is any markdown, blocks too: a takeaway that should stand out
9
+ can be an alert, `> [!TIP] The takeaway`. A badge ending a step's label sits
10
+ at its right. A `:when[2026-11-21 10:43]` in a step's label is when it
11
+ happened: shown above the title, as written. It works in any heading too,
12
+ on the page or in a step's body: wherever it is written in the heading, it
13
+ shows above it, and it is not in the heading's anchor. A `:::form` or a `:::details` directly in the timeline is a
14
+ step as it is, its own label its title.
15
+
16
+ **Sub steps.** A timeline inside a step is that step's sub steps: drawn a level
17
+ in, on a thinner line, and a timeline inside one of those again a level
18
+ further, to any depth. Each container takes one colon more than what it
19
+ holds: the step around a timeline five, the timeline around that step six.
20
+ That is also how steps are grouped: a step for the group, its steps in a
21
+ timeline inside it.
22
+
23
+ ````markdown
24
+ ::::::timeline[Steps so far]
25
+ :::item[:when[2026-11-21 10:43] Made a minimal repro :badge[done]]
26
+ A chain of 6,400 closures overflows the stack: the crash is the chain, not
27
+ Solid.
28
+ :::
29
+
30
+ :::::item[Counted the visits]
31
+ 5,150 visits for 100 links: each link walks the whole chain again.
32
+
33
+ ::::timeline
34
+ :::item[With the probe off]
35
+ #### What it does
36
+ Runs the same chain with the probe switched off.
37
+
38
+ #### Result
39
+ The same 5,150: the probe isn't what walks it.
40
+ :::
41
+
42
+ :::item[With the cache on]
43
+ 100 visits: one per link.
44
+ :::
45
+ ::::
46
+ :::::
47
+ ::::::
48
+ ````
@@ -0,0 +1,82 @@
1
+ // Timeline: steps in order down a line, a dot per step, each with room for full sentences: what was
2
+ // done and what it showed. Its items are its steps (`:::item[Title]`); headings in a step are its
3
+ // own. A `:when[…]` in a step's label is when it happened, and in any heading too. A timeline inside
4
+ // a step is that step's sub steps, drawn a level in (style.css). Rendered on the server; no script.
5
+ import fs from 'node:fs'
6
+ import { definePlugin, html, raw, type DirectiveContext, type Item, type Plugin } from '@bigmistqke/workpad'
7
+ import { builtinPublic } from '../../src/package.ts'
8
+
9
+ type Node = {
10
+ type: string
11
+ name?: string
12
+ value?: string
13
+ children?: Node[]
14
+ }
15
+
16
+ const text = (node: Node): string => node.value ?? (node.children ?? []).map(text).join('')
17
+
18
+ /** A when's text. Its `:59` in `19:59` parses as a directive named `59`: put it back as written. */
19
+ const whenText = (node: Node): string =>
20
+ (node.children ?? [])
21
+ .map(n => (n.type === 'textDirective' ? `:${n.name}${n.children?.length ? `[${text(n)}]` : ''}` : text(n)))
22
+ .join('')
23
+
24
+ /** Takes a `:when[…]` out of a heading: its text, and the heading without it. */
25
+ export function when(title: Node[]): { when: string | null; title: Node[] } {
26
+ const found = title.find(n => n.type === 'textDirective' && n.name === 'when')
27
+ if (!found) return { when: null, title }
28
+ const rest = title.filter(n => n !== found)
29
+ // the space that separated it from the title
30
+ const last = rest[rest.length - 1]
31
+ if (last?.type === 'text' && last.value) last.value = last.value.trimEnd()
32
+ const first = rest[0]
33
+ if (first?.type === 'text' && first.value) first.value = first.value.trimStart()
34
+ return { when: whenText(found).trim(), title: rest }
35
+ }
36
+
37
+ /**
38
+ * A step (an item, DirectiveContext.items): when, title and body. Its label, as written, is its title,
39
+ * a `:when` in it taken out. A form or a details that is an item is the step's body as it is, its
40
+ * label its own title, never shown twice. A timeline in the body (its sub steps) renders as its own
41
+ * block.
42
+ */
43
+ async function renderStep(ctx: DirectiveContext, item: Item) {
44
+ const label = item.block ? null : (item.label as Node[] | null)
45
+ const split = label ? when(label) : { when: null, title: null }
46
+ const title = split.title?.length ? await ctx.toHtml(split.title) : null
47
+ return html`<li class="wp-tl-step">
48
+ ${split.when && html`<time class="wp-tl-when">${split.when}</time>`}
49
+ ${title?.trim() && html`<div class="wp-tl-title" data-wp-title>${raw(title)}</div>`}
50
+ ${item.html.trim() && html`<div class="wp-tl-body">${raw(item.html)}</div>`}
51
+ </li>`
52
+ }
53
+
54
+ /** `:when[…]` anywhere else, in any heading: shown above it (style.css), as in a step. */
55
+ export function renderWhen(ctx: DirectiveContext) {
56
+ // its label as written: the node, since a `:59` in it is a directive of its own (whenText)
57
+ const at = whenText((ctx as DirectiveContext & { node: Node }).node).trim()
58
+ return at ? html`<time class="wp-when">${at}</time>` : html``
59
+ }
60
+
61
+ async function render(ctx: DirectiveContext) {
62
+ const steps = await Promise.all((await ctx.items()).map(item => renderStep(ctx, item)))
63
+ return html`<div class="wp-timeline">
64
+ ${ctx.label && html`<div class="wp-tl-caption">${raw(ctx.label)}</div>`}
65
+ <ol class="wp-tl-steps">
66
+ ${steps}
67
+ </ol>
68
+ </div>`
69
+ }
70
+
71
+ export default function timeline(): Plugin {
72
+ return definePlugin({
73
+ name: 'timeline',
74
+ public: builtinPublic('timeline'),
75
+ style: 'style.css',
76
+ blocks: {
77
+ timeline: { attrs: [], html: render, menu: { group: 'arrange', summary: 'in order, down a line: what happened when' } },
78
+ when: { html: renderWhen, menu: { group: 'mark', summary: 'a date or time, on a heading' } },
79
+ },
80
+ docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
81
+ })
82
+ }