@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,278 @@
1
+ // How the page shows what waits for the reader: its open questions and the agent's messages they
2
+ // haven't read (status.ts). The core marks each named form with where it stands (open, pending, received,
3
+ // handled; see `api.forms.on`); the page styles those marks (base.css), folds a handled form into
4
+ // one line quoting its answer, counts them in the tab title, and puts two toasts in the page's corner, "→ unanswered (n)" for the questions and "→ unread
5
+ // (n)" for the messages, each going from one to the next; the second has a ✕ that puts it away
6
+ // until another message comes. A message is found on the page by its
7
+ // element (`data-wp-message`), or, when it isn't shown, by the element that holds it
8
+ // (`data-wp-holds`: a comment's badge), which is sent `wp-reveal` to show it. Arriving by a link that
9
+ // ends in `#wp-waiting` (the header's menu, the overview), the page goes to the first. An answer no
10
+ // agent is listening for says so once it is sent, in a toast: the form's own mark may be out of view.
11
+ import type { Api, FormState, FormStatus, Toast, WpEvent } from '../../define.ts'
12
+ import { answerParts } from '../forms'
13
+ import { viewBottom } from './view'
14
+ import { WAITING_HASH } from './elsewhere'
15
+
16
+ let main: HTMLElement
17
+ let api: Api
18
+ let forms: FormStatus[] = []
19
+
20
+ // forms the reader opened again to change their answer, by name, with the answer they opened:
21
+ // they stay open until a newer answer is handled
22
+ const reopened = new Map<string, string>()
23
+
24
+ const inState = (state: FormState) => forms.filter(f => f.state === state && f.form.isConnected).map(f => f.form)
25
+
26
+ /** Where an unread message is on the page: its own element, else the one holding it. */
27
+ function messageTarget(id: string): HTMLElement | null {
28
+ const own = document.querySelector<HTMLElement>(`[data-wp-message="${CSS.escape(id)}"]`)
29
+ if (own && own.getClientRects().length) return own
30
+ return (
31
+ [...document.querySelectorAll<HTMLElement>('[data-wp-holds]')].find(el =>
32
+ el.dataset.wpHolds!.split(' ').includes(id),
33
+ ) ?? null
34
+ )
35
+ }
36
+
37
+ /** What waits for the reader, top to bottom: open forms and unread messages. */
38
+ function targets(kind: 'all' | 'messages' = 'all'): HTMLElement[] {
39
+ const list = [
40
+ ...new Set([
41
+ ...(kind === 'all' ? inState('open') : []),
42
+ ...api
43
+ .unread()
44
+ .map(e => messageTarget(e.id))
45
+ .filter((el): el is HTMLElement => !!el),
46
+ ]),
47
+ ]
48
+ const top = (el: HTMLElement) => el.getBoundingClientRect().top
49
+ return list.sort((a, b) => top(a) - top(b))
50
+ }
51
+
52
+ // the corner's toasts: the questions waiting, and the agent's messages unread; each goes to the next
53
+ let waitingToast: Toast
54
+ let unreadToast: Toast
55
+ let toastTimer = 0
56
+ let unreadShown = false
57
+ // closed by its ✕: the messages unread then, by id. The toast stays away until one more comes; they
58
+ // stay unread (the tab title still counts them)
59
+ let dismissed: Set<string> | null = null
60
+
61
+ // A form an agent received folds into a card, since it is no longer the reader's to do: what was
62
+ // chosen, what was written under it (a few lines), and "received · change" while the agent is on it,
63
+ // "decided · change" once it handled it (a click on the card opens the form again). Its chat folds too, into
64
+ // "💬 n messages", opened by a click; a message from the agent the reader hasn't read opens it, and
65
+ // it stays open once read, until the reader folds it, so a reply never hides in the fold. The card is the page's, put back in the
66
+ // form after each update.
67
+ const chatsOpen = new Set<string>()
68
+
69
+ function card(form: HTMLFormElement, name: string, last: WpEvent, state: FormStatus['state']) {
70
+ let el = form.querySelector<HTMLElement>(':scope > .wp-folded')
71
+ if (!el) {
72
+ el = document.createElement('div')
73
+ el.className = 'wp-folded'
74
+ el.innerHTML =
75
+ '<div class="wp-folded-head"><span class="wp-folded-choice"></span><span class="wp-folded-change"></span></div><ul class="wp-folded-choices"></ul><div class="wp-folded-text"></div><button type="button" class="wp-folded-chat"></button>'
76
+ form.prepend(el)
77
+ }
78
+ const { action, chosen, written } = answerParts(last.data, last.action)
79
+ // One answer and no button: it is the title. Else the button it was sent with is the title (or
80
+ // "answered"), and each answer a line under it. Nothing chosen, only written: the text is the
81
+ // answer, said once.
82
+ const list = action || chosen.length > 1 ? chosen : []
83
+ const head = action ?? (chosen.length > 1 ? 'answered' : (chosen[0] ?? written.shift() ?? 'answered'))
84
+ el.querySelector('.wp-folded-choice')!.textContent = `✓ ${head}`
85
+ const choices = el.querySelector<HTMLElement>('.wp-folded-choices')!
86
+ choices.replaceChildren(...list.map(c => Object.assign(document.createElement('li'), { textContent: c })))
87
+ choices.hidden = !list.length
88
+ el.querySelector('.wp-folded-change')!.textContent = state === 'received' ? 'received · change' : 'decided · change'
89
+ const text = el.querySelector<HTMLElement>('.wp-folded-text')!
90
+ text.textContent = written.join('\n\n')
91
+ text.hidden = !written.length
92
+ // the chat: how many messages, and open or folded
93
+ const messages = api.events().filter(e => e.type === 'chat:message' && e.data.chat === name).length
94
+ const thread = form.querySelector<HTMLElement>(':scope > [data-block=chat-thread]')
95
+ const unreadHere =
96
+ !!thread &&
97
+ api
98
+ .unread()
99
+ .some(
100
+ m =>
101
+ thread.querySelector(`[data-wp-message="${CSS.escape(m.id)}"]`) ||
102
+ (m.type === 'chat:message' && m.data.chat === name),
103
+ )
104
+ // a message to read opens it, and it stays open once read, until the reader folds it; and it
105
+ // doesn't fold from under a message being written: its box has the focus, or text not sent yet
106
+ const box = thread?.querySelector<HTMLTextAreaElement>('textarea')
107
+ const writing = !!box && (document.activeElement === box || box.value.trim() !== '')
108
+ if (unreadHere || writing) chatsOpen.add(name)
109
+ const open = chatsOpen.has(name)
110
+ form.toggleAttribute('data-wp-chat-open', open)
111
+ const chat = el.querySelector<HTMLButtonElement>('.wp-folded-chat')!
112
+ chat.hidden = !messages
113
+ chat.textContent = `💬 ${messages} message${messages === 1 ? '' : 's'} ${open ? '▾' : '▸'}`
114
+ chat.setAttribute('aria-expanded', String(open))
115
+ }
116
+
117
+ function show(list: FormStatus[] = forms) {
118
+ forms = list
119
+ for (const { form, name, state, last } of forms) {
120
+ const done = state === 'received' || state === 'handled'
121
+ if (last && reopened.has(name) && reopened.get(name) !== last.id && done) reopened.delete(name)
122
+ const folded = done && !reopened.has(name)
123
+ form.toggleAttribute('data-wp-folded', folded)
124
+ if (folded && last) card(form, name, last, state)
125
+ else {
126
+ form.querySelector(':scope > .wp-folded')?.remove()
127
+ form.removeAttribute('data-wp-chat-open')
128
+ }
129
+ }
130
+ const unread = api.unread().length
131
+ const n = inState('open').length + unread
132
+ const questions = inState('open').length
133
+ if (questions) {
134
+ waitingToast.update(`→ unanswered (${questions})`)
135
+ waitingToast.show()
136
+ } else waitingToast.hide()
137
+ api.badge('waiting', n ? `!${n}` : null, -1)
138
+ // the agent's messages: a toast while there are any. It comes a moment late: a message that
139
+ // arrives in view (an open thread) is read before it would show.
140
+ clearTimeout(toastTimer)
141
+ const ids = api.unread().map(e => e.id)
142
+ if (dismissed && (!unread || ids.some(id => !dismissed!.has(id)))) dismissed = null
143
+ if (dismissed) unreadToast.hide()
144
+ else if (!unread) unreadToast.hide()
145
+ else if (unreadShown) unreadToast.update(`→ unread (${unread})`)
146
+ else
147
+ toastTimer = window.setTimeout(() => {
148
+ const left = api.unread().length
149
+ if (!left) return
150
+ unreadToast.update(`→ unread (${left})`)
151
+ unreadToast.show()
152
+ unreadShown = true
153
+ }, 1000)
154
+ if (!unread) unreadShown = false
155
+ // arrived to answer: the first thing waiting, once the page knows (the log loads after the page)
156
+ if (arriving && n) {
157
+ arriving = false
158
+ history.replaceState(history.state, '', location.pathname + location.search)
159
+ requestAnimationFrame(() => {
160
+ const first = targets()[0]
161
+ if (first) jumpTo(first)
162
+ })
163
+ }
164
+ }
165
+
166
+ let arriving = location.hash === WAITING_HASH
167
+ // a page whose questions were answered meanwhile has none left to go to
168
+ setTimeout(() => {
169
+ arriving = false
170
+ }, 5000)
171
+
172
+ // What a jump went to last, and where the page was scrolled then: jumping again goes to the one
173
+ // after it, round to the first. Once the reader scrolled themselves, the next one is the first
174
+ // below where they are.
175
+ let current: HTMLElement | null = null
176
+ let scrolledTo = 0
177
+
178
+ function next(list: HTMLElement[]) {
179
+ const i = current ? list.indexOf(current) : -1
180
+ if (i >= 0 && Math.abs(scrollY - scrolledTo) < 40) return list[(i + 1) % list.length]
181
+ return list.find(f => f.getBoundingClientRect().top > viewBottom() / 2) ?? list[0]
182
+ }
183
+
184
+ function jump(what: 'open' | 'messages') {
185
+ const list = what === 'messages' ? targets('messages') : inState('open')
186
+ const target = next(list)
187
+ if (target) jumpTo(target)
188
+ }
189
+
190
+ function jumpTo(target: HTMLElement) {
191
+ current = target
192
+ // centred in the part of the window the page is seen in (above the terminal drawer), as far as
193
+ // the page allows
194
+ const r = target.getBoundingClientRect()
195
+ scrolledTo = Math.max(
196
+ 0,
197
+ Math.min(scrollY + r.top + r.height / 2 - viewBottom() / 2, document.documentElement.scrollHeight - innerHeight),
198
+ )
199
+ scrollTo({ top: scrolledTo, behavior: 'smooth' })
200
+ if (target instanceof HTMLFormElement) {
201
+ if (target.hasAttribute('data-wp-open'))
202
+ target
203
+ .querySelector<HTMLElement>('input:not([type=hidden]), textarea, select, button')
204
+ ?.focus({ preventScroll: true })
205
+ } else target.dispatchEvent(new CustomEvent('wp-reveal', { bubbles: true }))
206
+ }
207
+
208
+ export function trackWaiting(root: HTMLElement, core: Api) {
209
+ main = root
210
+ api = core
211
+ waitingToast = api.toast('', { ms: 0, name: 'waiting', order: -2, onClick: () => jump('open') })
212
+ unreadToast = api.toast('', {
213
+ ms: 0,
214
+ name: 'unread',
215
+ order: -1,
216
+ onClick: () => jump('messages'),
217
+ onDismiss: () => {
218
+ dismissed = new Set(api.unread().map(e => e.id))
219
+ unreadShown = false
220
+ },
221
+ })
222
+ waitingToast.hide()
223
+ unreadToast.hide()
224
+ // a folded form opens again on a click, to change the answer; not on one in its thread, which
225
+ // stays open under it
226
+ main.addEventListener('click', e => {
227
+ const target = e.target as HTMLElement
228
+ const form = target.closest<HTMLFormElement>('form[data-wp-folded]')
229
+ if (!form || target.closest('[data-block=chat-thread]')) return
230
+ e.preventDefault()
231
+ // its chat, folded or open: not the form
232
+ if (target.closest('.wp-folded-chat')) {
233
+ const name = form.getAttribute('name')!
234
+ if (chatsOpen.has(name) || form.hasAttribute('data-wp-chat-open')) {
235
+ chatsOpen.delete(name)
236
+ form.removeAttribute('data-wp-chat-open')
237
+ } else chatsOpen.add(name)
238
+ show(forms)
239
+ return
240
+ }
241
+ const name = form.getAttribute('name')!
242
+ const last = api.last(name)
243
+ if (last) reopened.set(name, last.id)
244
+ show(forms)
245
+ form.querySelector<HTMLElement>('input:not([type=hidden]), textarea, select')?.focus({ preventScroll: true })
246
+ })
247
+ // a link to a folded form's chat, or to a message in it (#chat:…, #message:…): the chat opens
248
+ main.addEventListener('wp-reveal', e => {
249
+ const target = e.target as HTMLElement
250
+ const form = target.closest<HTMLFormElement>('form[data-wp-folded]')
251
+ if (!form || !target.closest('[data-block=chat-thread]')) return
252
+ chatsOpen.add(form.getAttribute('name')!)
253
+ show(forms)
254
+ })
255
+ api.forms.on(show)
256
+ // a message read, or a new one
257
+ api.events.on(() => show())
258
+ show(api.forms())
259
+ document.addEventListener('submit', e => {
260
+ const form = e.target as HTMLFormElement
261
+ const name = form.getAttribute('name')
262
+ if (!name || !main.contains(form) || !forms.some(f => f.form === form)) return
263
+ // who listens as it is sent: one that takes it ends its listen right after
264
+ if (api.listeners().some(l => l.type === name || l.type === null)) return
265
+ const sent = new MutationObserver(() => {
266
+ if (form.dataset.wpState === 'sending') return
267
+ sent.disconnect()
268
+ if (form.dataset.wpState !== 'sent') return
269
+ api.toast('Sent · nobody listening', {
270
+ kind: 'warn',
271
+ name: 'sent',
272
+ details: 'Your answer stays in the log until someone listens on this page.',
273
+ })
274
+ })
275
+ sent.observe(form, { attributes: true, attributeFilter: ['data-wp-state'] })
276
+ // going down, as the core's own (forms.ts): a form in a form doesn't bubble up to here
277
+ }, { capture: true })
278
+ }
@@ -0,0 +1,40 @@
1
+ // A window (src/pages/window.ts: the iframe block, `:::window`, any block that draws one) filling the
2
+ // browser window, and back: its header's button, or Esc. Which one is kept across the page's
3
+ // updates, which can write the block anew: by its place among the page's windows.
4
+ import type { Api } from '../../define.ts'
5
+
6
+ const windows = () => [...document.querySelectorAll<HTMLElement>('#wp-main .wp-window')]
7
+ let maximized: number | null = null
8
+
9
+ function apply() {
10
+ const all = windows()
11
+ if (maximized != null && !all[maximized]) maximized = null
12
+ all.forEach((win, i) => {
13
+ const on = i === maximized
14
+ win.toggleAttribute('data-wp-maximized', on)
15
+ const button = win.querySelector<HTMLButtonElement>('[data-wp-maximize]')
16
+ if (button) {
17
+ button.title = on ? 'Back into the page (Esc)' : 'Fill the window'
18
+ button.setAttribute('aria-label', button.title)
19
+ button.setAttribute('aria-pressed', String(on))
20
+ }
21
+ })
22
+ document.documentElement.toggleAttribute('data-wp-window-maximized', maximized != null)
23
+ }
24
+
25
+ export function trackWindows(api: Api) {
26
+ document.addEventListener('click', e => {
27
+ const win = (e.target as Element).closest?.('[data-wp-maximize]')?.closest<HTMLElement>('.wp-window')
28
+ if (!win) return
29
+ const i = windows().indexOf(win)
30
+ maximized = maximized === i ? null : i
31
+ apply()
32
+ })
33
+ document.addEventListener('keydown', e => {
34
+ if (e.key === 'Escape' && maximized != null) {
35
+ maximized = null
36
+ apply()
37
+ }
38
+ })
39
+ api.onRendered(apply)
40
+ }
@@ -0,0 +1,19 @@
1
+ // The api's changing values (Reactive in define.ts): a function for what it is now, `.on` to hear
2
+ // each change. Whoever owns the value calls `notify()` after it changed.
3
+ import type { Reactive } from '../define.ts'
4
+
5
+ export function reactive<T>(get: () => T): Reactive<T> & { notify(): void } {
6
+ const listeners = new Set<(value: T) => void>()
7
+ const value = (() => get()) as Reactive<T> & { notify(): void }
8
+ value.on = fn => {
9
+ listeners.add(fn)
10
+ return () => {
11
+ listeners.delete(fn)
12
+ }
13
+ }
14
+ value.notify = () => {
15
+ const now = get()
16
+ for (const fn of [...listeners]) fn(now)
17
+ }
18
+ return value
19
+ }
@@ -0,0 +1,111 @@
1
+ // The agent's messages the reader hasn't read (status.ts: unreadMessages), and reading them: an
2
+ // element marked `data-wp-message="<event id>"` (a reply in an open thread, a chat bubble) reads
3
+ // its message once it has been mostly in view (most of it on screen, or filling most of the window)
4
+ // while the tab is visible. Reads are sent together, a
5
+ // moment later, as one `workpad:read` event. A message in a chat no longer on the page (folded
6
+ // after it, see page/orphans.ts) is not unread: it is kept there as a record.
7
+ import type { WpEvent } from '../define.ts'
8
+ import { READ, unreadMessages } from '../status.ts'
9
+
10
+ let isMessage: (type: string) => boolean = () => false
11
+ let getEvents: () => WpEvent[] = () => []
12
+ let send: (ids: string[]) => void = () => {}
13
+ let chats: () => ReadonlySet<string> | null = () => null
14
+ // read here but not in the log yet: they don't count as unread meanwhile
15
+ const sending = new Set<string>()
16
+
17
+ export const unread = (): WpEvent[] => unreadMessages(getEvents(), isMessage, chats()).filter(e => !sending.has(e.id))
18
+
19
+ /**
20
+ * The chats the page's content shows, by name: the one under every form and each chat block; null
21
+ * while there is no page content (a folder, raw text), when every chat's messages count.
22
+ */
23
+ export function chatsShown(): Set<string> | null {
24
+ const main = document.getElementById('wp-main')
25
+ if (!main) return null
26
+ return new Set(
27
+ [
28
+ ...[...main.querySelectorAll('form[name]')].map(f => f.getAttribute('name')),
29
+ ...[...main.querySelectorAll('[data-chat]')].map(c => c.getAttribute('data-chat')),
30
+ ].filter((name): name is string => !!name),
31
+ )
32
+ }
33
+
34
+ // read, to send in the next batch
35
+ const queued = new Set<string>()
36
+ let timer = 0
37
+ export function markRead(ids: string[]) {
38
+ const open = new Set(unread().map(e => e.id))
39
+ const fresh = ids.filter(id => open.has(id))
40
+ if (!fresh.length) return
41
+ for (const id of fresh) {
42
+ sending.add(id)
43
+ queued.add(id)
44
+ }
45
+ clearTimeout(timer)
46
+ timer = window.setTimeout(() => {
47
+ const batch = [...queued]
48
+ queued.clear()
49
+ if (batch.length) send(batch)
50
+ }, 300)
51
+ }
52
+
53
+ /** Once the log has the reads, they need no holding. */
54
+ export function readsArrived(events: WpEvent[]) {
55
+ for (const e of events)
56
+ if (e.type === READ) for (const id of (e.data?.ids as unknown[] | undefined) ?? []) sending.delete(String(id))
57
+ }
58
+
59
+ // Mostly in view: most of the message is on screen, or, for one taller than the window, it fills
60
+ // most of the window.
61
+ const MOSTLY = 0.6
62
+ function mostlyInView(entry: IntersectionObserverEntry) {
63
+ if (!entry.isIntersecting) return false
64
+ if (entry.intersectionRatio >= MOSTLY) return true
65
+ const viewport = entry.rootBounds?.height ?? window.innerHeight
66
+ return entry.intersectionRect.height >= MOSTLY * viewport
67
+ }
68
+
69
+ // what is in view now, read once the tab is visible too
70
+ const inView = new Set<string>()
71
+ const seen = new IntersectionObserver(
72
+ entries => {
73
+ for (const entry of entries) {
74
+ const id = (entry.target as HTMLElement).dataset.wpMessage!
75
+ if (mostlyInView(entry)) inView.add(id)
76
+ else inView.delete(id)
77
+ }
78
+ readVisible()
79
+ },
80
+ // steps: a message taller than the window never reaches 0.6 of itself in view, so its share is
81
+ // reported as it scrolls by
82
+ { threshold: Array.from({ length: 21 }, (_, i) => i / 20) },
83
+ )
84
+
85
+ function readVisible() {
86
+ if (document.visibilityState === 'visible' && inView.size) markRead([...inView])
87
+ }
88
+
89
+ const observed = new WeakSet<Element>()
90
+ function observeMessages() {
91
+ const open = new Set(unread().map(e => e.id))
92
+ for (const el of document.querySelectorAll<HTMLElement>('[data-wp-message]')) {
93
+ if (observed.has(el) || !open.has(el.dataset.wpMessage!)) continue
94
+ observed.add(el)
95
+ seen.observe(el)
96
+ }
97
+ }
98
+
99
+ export function trackReading(options: { messages: string[]; events: () => WpEvent[]; send: (ids: string[]) => void }) {
100
+ const types = new Set(options.messages)
101
+ isMessage = type => types.has(type)
102
+ getEvents = options.events
103
+ send = options.send
104
+ chats = chatsShown
105
+ new MutationObserver(observeMessages).observe(document.body, { childList: true, subtree: true })
106
+ document.addEventListener('visibilitychange', readVisible)
107
+ observeMessages()
108
+ }
109
+
110
+ /** After the events changed: messages that came since may already be on screen. */
111
+ export const messagesChanged = () => observeMessages()