@bigmistqke/workpad 0.19.0 → 0.20.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 (83) hide show
  1. package/README.md +18 -9
  2. package/bin/workpad.ts +187 -37
  3. package/dist/bin/workpad.js +156 -32
  4. package/dist/client/runtime.js +141 -18
  5. package/dist/plugins/chat/README.md +7 -3
  6. package/dist/plugins/chat/index.js +1 -0
  7. package/dist/plugins/chat/public/client.js +2 -2
  8. package/dist/plugins/chat/public/style.css +10 -0
  9. package/dist/plugins/claude/channel.js +22 -4
  10. package/dist/plugins/comments/README.md +2 -1
  11. package/dist/plugins/comments/index.js +2 -0
  12. package/dist/plugins/comments/public/client.js +6 -5
  13. package/dist/plugins/comments/public/style.css +9 -0
  14. package/dist/plugins/form/README.md +1 -1
  15. package/dist/plugins/index.js +3 -1
  16. package/dist/plugins/opencode/channel.js +1 -0
  17. package/dist/plugins/progress/README.md +41 -0
  18. package/dist/plugins/progress/index.js +117 -0
  19. package/dist/plugins/progress/public/client.js +1 -0
  20. package/dist/plugins/progress/public/style.css +85 -0
  21. package/dist/plugins/progress/state.js +210 -0
  22. package/dist/solid/index.js +3 -1
  23. package/dist/src/agents.js +7 -3
  24. package/dist/src/anchors.js +25 -1
  25. package/dist/src/build.js +1 -1
  26. package/dist/src/client/page/base.css +48 -2
  27. package/dist/src/client/page/main.js +58 -30
  28. package/dist/src/document.js +6 -4
  29. package/dist/src/events.js +19 -9
  30. package/dist/src/help.js +43 -8
  31. package/dist/src/hub.js +14 -1
  32. package/dist/src/loader.js +7 -2
  33. package/dist/src/migrate.js +1 -1
  34. package/dist/src/pages/board.js +1 -1
  35. package/dist/src/pages/markdown.js +3 -3
  36. package/dist/src/pages/render.js +16 -0
  37. package/dist/src/server.js +25 -12
  38. package/dist/src/set.js +99 -0
  39. package/dist/src/status.js +47 -8
  40. package/package.json +6 -1
  41. package/plugins/README.md +3 -1
  42. package/plugins/chat/README.md +7 -3
  43. package/plugins/chat/client.tsx +20 -3
  44. package/plugins/chat/index.ts +1 -0
  45. package/plugins/chat/public/style.css +10 -0
  46. package/plugins/claude/channel.ts +19 -5
  47. package/plugins/comments/README.md +2 -1
  48. package/plugins/comments/client.ts +19 -0
  49. package/plugins/comments/index.ts +7 -1
  50. package/plugins/comments/public/style.css +9 -0
  51. package/plugins/form/README.md +1 -1
  52. package/plugins/index.ts +2 -0
  53. package/plugins/opencode/channel.ts +2 -1
  54. package/plugins/progress/README.md +41 -0
  55. package/plugins/progress/client.ts +172 -0
  56. package/plugins/progress/index.ts +166 -0
  57. package/plugins/progress/public/style.css +85 -0
  58. package/plugins/progress/state.ts +278 -0
  59. package/skills/workpad/SKILL.md +126 -15
  60. package/solid/index.tsx +6 -2
  61. package/src/agents.ts +17 -2
  62. package/src/anchors.ts +32 -0
  63. package/src/build.ts +1 -1
  64. package/src/client/delivery.ts +49 -0
  65. package/src/client/forms.ts +18 -11
  66. package/src/client/page/base.css +48 -2
  67. package/src/client/page/waiting.ts +45 -25
  68. package/src/client/read.ts +9 -5
  69. package/src/client/runtime.ts +7 -3
  70. package/src/client/toast.ts +27 -3
  71. package/src/define.ts +92 -8
  72. package/src/document.ts +19 -5
  73. package/src/events.ts +26 -6
  74. package/src/help.ts +42 -7
  75. package/src/hub.ts +9 -0
  76. package/src/loader.ts +14 -4
  77. package/src/migrate.ts +1 -1
  78. package/src/pages/board.ts +3 -1
  79. package/src/pages/markdown.ts +4 -4
  80. package/src/pages/render.ts +19 -1
  81. package/src/server.ts +43 -13
  82. package/src/set.ts +118 -0
  83. package/src/status.ts +100 -13
@@ -0,0 +1,49 @@
1
+ // Where each of the reader's events for an agent went (Api.delivery): a form's answer, a chat
2
+ // message, a comment. One rule for all of them (deliveryOf in status.ts), from the log's marks and
3
+ // who listens on the page, kept up to date as those change and as an answer waits long enough to be
4
+ // late. What the page draws of it (a line beside what was sent, a count in the corner) is the page's
5
+ // and the plugins'.
6
+ import type { Delivery, Listener, Reactive, WpEvent } from '../define'
7
+ import { NOT_RECEIVED_AFTER, deliveryOf, fromReader, isQuestionName } from '../status.ts'
8
+ import { reactive } from './reactive'
9
+
10
+ const same = (a: ReadonlyMap<string, Delivery>, b: ReadonlyMap<string, Delivery>) =>
11
+ a.size === b.size && [...a].every(([id, d]) => b.get(id)?.text === d.text)
12
+
13
+ /**
14
+ * The delivery of each of the reader's events for an agent among `events`: a form's answer (a type
15
+ * without a colon) and the plugins' types in `handedOut`.
16
+ */
17
+ export function trackDelivery(
18
+ events: Reactive<WpEvent[]>,
19
+ listeners: Reactive<Listener[]>,
20
+ handedOut: ReadonlySet<string>,
21
+ ): Reactive<ReadonlyMap<string, Delivery>> {
22
+ const forAgent = (e: WpEvent) => fromReader(e) && (isQuestionName(e.type) || handedOut.has(e.type))
23
+ let current: ReadonlyMap<string, Delivery> = new Map()
24
+ let timer: ReturnType<typeof setTimeout> | undefined
25
+ const value = reactive(() => current)
26
+ const update = () => {
27
+ const now = Date.now()
28
+ const listening = listeners()
29
+ const next = new Map<string, Delivery>()
30
+ // when the next answer waiting turns late, to look again then
31
+ let soonest = Infinity
32
+ for (const e of events()) {
33
+ if (!forAgent(e)) continue
34
+ const d = deliveryOf(e, listening, now)
35
+ next.set(e.id, d)
36
+ if (d.state === 'waiting' || d.state === 'unfollowed')
37
+ soonest = Math.min(soonest, Date.parse(e.ts) + NOT_RECEIVED_AFTER - now)
38
+ }
39
+ clearTimeout(timer)
40
+ if (soonest < Infinity) timer = setTimeout(update, Math.max(0, soonest) + 50)
41
+ if (same(current, next)) return
42
+ current = next
43
+ value.notify()
44
+ }
45
+ events.on(update)
46
+ listeners.on(update)
47
+ update()
48
+ return value
49
+ }
@@ -254,33 +254,36 @@ async function onSubmit(e: SubmitEvent) {
254
254
 
255
255
  // What a sent answer came to, said beside the form's last send button, where the eye is when it is
256
256
  // pressed (a line over the form was out of view below a long list of options): `.wp-form-status`,
257
- // "waiting for an agent to receive it", or `data-kind="bad"` "couldn't send it". Never what the
258
- // button says already (sendState): nothing while sending, no "sent".
257
+ // where its answer went (api.delivery: waiting for an agent to receive it, no one follows the page,
258
+ // not received after 10 min, received by whom), with the delivery's state in `data-wp-delivery`; or
259
+ // `data-kind="bad"` "couldn't send it". Never what the button says already (sendState): nothing while
260
+ // sending, no "sent". A late answer's line is in the warning colour (`data-kind="warn"`).
259
261
  const sendButtons = (form: HTMLFormElement) =>
260
262
  [...form.querySelectorAll<HTMLButtonElement | HTMLInputElement>('button, input[type=submit]')].filter(
261
263
  b => (b instanceof HTMLInputElement || (b.type !== 'button' && b.type !== 'reset')) && b.form === form,
262
264
  )
263
265
 
264
266
  function showStatus(form: HTMLFormElement) {
267
+ const last = api.last(form.getAttribute('name')!)
268
+ const delivery = last ? api.delivery().get(last.id) : undefined
269
+ const answered = form.hasAttribute('data-wp-pending') || form.hasAttribute('data-wp-received')
265
270
  const text =
266
271
  form.dataset.wpState === 'failed'
267
272
  ? (["couldn't send it", 'bad'] as const)
268
- : form.dataset.wpState === 'sending'
273
+ : form.dataset.wpState === 'sending' || !answered || !delivery || delivery.state === 'sending'
269
274
  ? null
270
- : form.hasAttribute('data-wp-pending')
271
- ? (['waiting for an agent to receive it', null] as const)
272
- : form.hasAttribute('data-wp-received')
273
- ? (['the agent has it', null] as const)
274
- : null
275
+ : ([delivery.text, delivery.state === 'late' ? 'warn' : null] as const)
275
276
  let el = form.querySelector<HTMLElement>('.wp-form-status')
276
- const last = sendButtons(form).at(-1)
277
- if (!text || !last) return void el?.remove()
277
+ const button = sendButtons(form).at(-1)
278
+ if (!text || !button) return void el?.remove()
278
279
  if (!el) el = Object.assign(document.createElement('span'), { className: 'wp-form-status' })
279
280
  el.setAttribute('role', 'status')
280
- if (el.previousElementSibling !== last) last.after(el)
281
+ if (el.previousElementSibling !== button) button.after(el)
281
282
  el.textContent = text[0]
282
283
  if (text[1]) el.dataset.kind = text[1]
283
284
  else delete el.dataset.kind
285
+ if (text[1] === 'bad') delete el.dataset.wpDelivery
286
+ else el.dataset.wpDelivery = delivery!.state
284
287
  }
285
288
 
286
289
  const ATTR: Record<FormState, string> = {
@@ -388,6 +391,10 @@ export function trackForms(el: HTMLElement, pageId: string, core: Api) {
388
391
  document.addEventListener('submit', onSubmit, { capture: true })
389
392
  api.onRendered(refresh)
390
393
  api.listeners.on(mark)
394
+ // an answer waiting turned late, or who received it is known
395
+ api.delivery.on(() => {
396
+ for (const form of forms()) showStatus(form)
397
+ })
391
398
  api.events.on(() => {
392
399
  restoreLast(false)
393
400
  mark()
@@ -949,8 +949,8 @@ form button[data-wp-send='failed'] {
949
949
  form[data-wp-just-sent] {
950
950
  animation: wp-sent 1.4s ease-out;
951
951
  }
952
- /* what a sent answer came to, beside the form's last send button (forms.ts): waiting for the agent
953
- (muted), couldn't send (red) */
952
+ /* what a sent answer came to, beside the form's last send button (forms.ts): where it went (muted),
953
+ not received for long (the warning colour), couldn't send (red) */
954
954
  .wp-form-status {
955
955
  margin-left: var(--space-md);
956
956
  color: var(--muted);
@@ -960,6 +960,10 @@ form[data-wp-just-sent] {
960
960
  color: var(--bad);
961
961
  font-weight: var(--weight-strong);
962
962
  }
963
+ .wp-form-status[data-kind='warn'] {
964
+ color: var(--warn);
965
+ font-weight: var(--weight-strong);
966
+ }
963
967
 
964
968
  /* a question the reader hasn't answered, or one the agent asks again: ringed, without a label of
965
969
  its own (one laid into the form landed on its content in some layouts; the corner counts them).
@@ -1329,6 +1333,48 @@ input.wp-board-search::placeholder {
1329
1333
  font: var(--ui-small)/var(--ui-line-height) var(--sans);
1330
1334
  color: color-mix(in srgb, var(--text) 72%, var(--muted));
1331
1335
  }
1336
+ /* a running progress on the page (plugins/progress): a small bar and its line */
1337
+ .wp-board-card > .wp-board-progress {
1338
+ flex: 1 1 100%;
1339
+ display: flex;
1340
+ align-items: center;
1341
+ gap: var(--space-sm);
1342
+ min-width: 0;
1343
+ font: var(--ui-small)/var(--ui-line-height) var(--sans);
1344
+ }
1345
+ .wp-board-progress[data-stale] {
1346
+ opacity: 0.55;
1347
+ }
1348
+ .wp-board-progress-text {
1349
+ min-width: 0;
1350
+ overflow: hidden;
1351
+ text-overflow: ellipsis;
1352
+ white-space: nowrap;
1353
+ }
1354
+ .wp-board-progress-bar {
1355
+ position: relative;
1356
+ flex: none;
1357
+ width: 48px;
1358
+ height: 4px;
1359
+ border-radius: 2px;
1360
+ overflow: hidden;
1361
+ background: color-mix(in srgb, var(--muted) 28%, transparent);
1362
+ }
1363
+ .wp-board-progress-bar > span {
1364
+ position: absolute;
1365
+ inset: 0 auto 0 0;
1366
+ background: var(--accent);
1367
+ }
1368
+ .wp-board-progress[data-stale] .wp-board-progress-bar > span {
1369
+ background: var(--muted);
1370
+ }
1371
+ .wp-board-progress-dot {
1372
+ flex: none;
1373
+ width: 7px;
1374
+ height: 7px;
1375
+ border-radius: 50%;
1376
+ background: var(--accent);
1377
+ }
1332
1378
  .wp-board-card > .wp-board-when {
1333
1379
  color: color-mix(in srgb, var(--muted) 72%, var(--surface));
1334
1380
  margin-top: calc(var(--space-xs) * -1);
@@ -6,9 +6,12 @@
6
6
  // until another message comes. A message is found on the page by its
7
7
  // element (`data-wp-message`), or, when it isn't shown, by the element that holds it
8
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.
9
+ // ends in `#wp-waiting` (the header's menu, the overview), the page goes to the first. Where an
10
+ // answer went is said beside it (api.delivery: forms.ts, the chat, the comments); one no agent
11
+ // received for long (late) is counted in a third toast, "→ not received (n)", in the warning colour,
12
+ // which goes to each: an element marked `data-wp-delivery="late"`.
11
13
  import type { Api, FormState, FormStatus, Toast, WpEvent } from '../../define.ts'
14
+ import { isQuestionName } from '../../status.ts'
12
15
  import { answerParts } from '../forms'
13
16
  import { viewBottom } from './view'
14
17
  import { WAITING_HASH } from './elsewhere'
@@ -49,9 +52,11 @@ function targets(kind: 'all' | 'messages' = 'all'): HTMLElement[] {
49
52
  return list.sort((a, b) => top(a) - top(b))
50
53
  }
51
54
 
52
- // the corner's toasts: the questions waiting, and the agent's messages unread; each goes to the next
55
+ // the corner's toasts: the questions waiting, the agent's messages unread, and the reader's answers
56
+ // no agent received for long; each goes to the next
53
57
  let waitingToast: Toast
54
58
  let unreadToast: Toast
59
+ let lateToast: Toast
55
60
  let toastTimer = 0
56
61
  let unreadShown = false
57
62
  // closed by its ✕: the messages unread then, by id. The toast stays away until one more comes; they
@@ -85,7 +90,9 @@ function card(form: HTMLFormElement, name: string, last: WpEvent, state: FormSta
85
90
  const choices = el.querySelector<HTMLElement>('.wp-folded-choices')!
86
91
  choices.replaceChildren(...list.map(c => Object.assign(document.createElement('li'), { textContent: c })))
87
92
  choices.hidden = !list.length
88
- el.querySelector('.wp-folded-change')!.textContent = state === 'received' ? 'received · change' : 'decided · change'
93
+ // who has it, when it said ("received by …")
94
+ const received = api.delivery().get(last.id)?.text ?? 'received'
95
+ el.querySelector('.wp-folded-change')!.textContent = `${state === 'received' ? received : 'decided'} · change`
89
96
  const text = el.querySelector<HTMLElement>('.wp-folded-text')!
90
97
  text.textContent = written.join('\n\n')
91
98
  text.hidden = !written.length
@@ -135,6 +142,11 @@ function show(list: FormStatus[] = forms) {
135
142
  waitingToast.show()
136
143
  } else waitingToast.hide()
137
144
  api.badge('waiting', n ? `!${n}` : null, -1)
145
+ const late = lateAnswers().length
146
+ if (late) {
147
+ lateToast.update(`→ not received (${late})`)
148
+ lateToast.show()
149
+ } else lateToast.hide()
138
150
  // the agent's messages: a toast while there are any. It comes a moment late: a message that
139
151
  // arrives in view (an open thread) is read before it would show.
140
152
  clearTimeout(toastTimer)
@@ -163,6 +175,29 @@ function show(list: FormStatus[] = forms) {
163
175
  }
164
176
  }
165
177
 
178
+ /**
179
+ * The reader's answers no agent received for long (late): each of a form only its latest (an older
180
+ * answer was overtaken by it), every chat message and comment.
181
+ */
182
+ function lateAnswers(): WpEvent[] {
183
+ const delivery = api.delivery()
184
+ return api
185
+ .events()
186
+ .filter(e => delivery.get(e.id)?.state === 'late' && (!isQuestionName(e.type) || api.last(e.type)?.id === e.id))
187
+ }
188
+
189
+ /** Where the late answers are on the page, top to bottom: a form's line goes to its form. */
190
+ function lateTargets(): HTMLElement[] {
191
+ const top = (el: HTMLElement) => el.getBoundingClientRect().top
192
+ return [
193
+ ...new Set(
194
+ [...document.querySelectorAll<HTMLElement>('[data-wp-delivery=late]')]
195
+ .filter(el => el.getClientRects().length)
196
+ .map(el => el.closest<HTMLElement>('form') ?? el),
197
+ ),
198
+ ].sort((a, b) => top(a) - top(b))
199
+ }
200
+
166
201
  let arriving = location.hash === WAITING_HASH
167
202
  // a page whose questions were answered meanwhile has none left to go to
168
203
  setTimeout(() => {
@@ -181,8 +216,8 @@ function next(list: HTMLElement[]) {
181
216
  return list.find(f => f.getBoundingClientRect().top > viewBottom() / 2) ?? list[0]
182
217
  }
183
218
 
184
- function jump(what: 'open' | 'messages') {
185
- const list = what === 'messages' ? targets('messages') : inState('open')
219
+ function jump(what: 'open' | 'messages' | 'late') {
220
+ const list = what === 'messages' ? targets('messages') : what === 'late' ? lateTargets() : inState('open')
186
221
  const target = next(list)
187
222
  if (target) jumpTo(target)
188
223
  }
@@ -219,8 +254,10 @@ export function trackWaiting(root: HTMLElement, core: Api) {
219
254
  unreadShown = false
220
255
  },
221
256
  })
257
+ lateToast = api.toast('', { ms: 0, kind: 'warn', name: 'not-received', order: -1, onClick: () => jump('late') })
222
258
  waitingToast.hide()
223
259
  unreadToast.hide()
260
+ lateToast.hide()
224
261
  // a folded form opens again on a click, to change the answer; not on one in its thread, which
225
262
  // stays open under it
226
263
  main.addEventListener('click', e => {
@@ -255,24 +292,7 @@ export function trackWaiting(root: HTMLElement, core: Api) {
255
292
  api.forms.on(show)
256
293
  // a message read, or a new one
257
294
  api.events.on(() => show())
295
+ // an answer turned late, or who received it is known
296
+ api.delivery.on(() => show())
258
297
  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
298
  }
@@ -7,14 +7,15 @@
7
7
  import type { WpEvent } from '../define.ts'
8
8
  import { READ, unreadMessages } from '../status.ts'
9
9
 
10
- let isMessage: (type: string) => boolean = () => false
10
+ // the event types for the reader, each with the `data` fields it requires
11
+ let messages: ReadonlyMap<string, readonly string[]> = new Map()
11
12
  let getEvents: () => WpEvent[] = () => []
12
13
  let send: (ids: string[]) => void = () => {}
13
14
  let chats: () => ReadonlySet<string> | null = () => null
14
15
  // read here but not in the log yet: they don't count as unread meanwhile
15
16
  const sending = new Set<string>()
16
17
 
17
- export const unread = (): WpEvent[] => unreadMessages(getEvents(), isMessage, chats()).filter(e => !sending.has(e.id))
18
+ export const unread = (): WpEvent[] => unreadMessages(getEvents(), messages, chats()).filter(e => !sending.has(e.id))
18
19
 
19
20
  /**
20
21
  * The chats the page's content shows, by name: the one under every form and each chat block; null
@@ -96,9 +97,12 @@ function observeMessages() {
96
97
  }
97
98
  }
98
99
 
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)
100
+ export function trackReading(options: {
101
+ messages: Record<string, string[]>
102
+ events: () => WpEvent[]
103
+ send: (ids: string[]) => void
104
+ }) {
105
+ messages = new Map(Object.entries(options.messages))
102
106
  getEvents = options.events
103
107
  send = options.send
104
108
  chats = chatsShown
@@ -4,6 +4,7 @@
4
4
  import type { Api, PluginMount, Listener, PageHandle, RenderedPage, SlotName, StoredPage, WpEvent } from '../define'
5
5
  import { html } from '../html.ts'
6
6
  import { formsApi, trackForms } from './forms'
7
+ import { trackDelivery } from './delivery'
7
8
  import { createToast } from './toast'
8
9
  import { markRead, messagesChanged, readsArrived, trackReading, unread as unreadNow } from './read'
9
10
  import { READ, SCROLL, fromReader } from '../status.ts'
@@ -20,8 +21,10 @@ interface Handoff {
20
21
  client: string | null
21
22
  /** the plugins' browser modules */
22
23
  plugins: { name: string; client: string }[]
23
- /** the event types that, from the agent, are messages for the reader */
24
- messages: string[]
24
+ /** the event types that, from the agent, are messages for the reader, each with the `data` fields it requires */
25
+ messages: Record<string, string[]>
26
+ /** the plugins' event types that, from the reader, go to an agent */
27
+ handedOut?: string[]
25
28
  /** the page's frontmatter (PageHandle.frontmatter) */
26
29
  frontmatter: Record<string, unknown>
27
30
  /** false on a built page (`workpad build`): a file, no server behind it */
@@ -259,6 +262,7 @@ const api: Api = {
259
262
  },
260
263
  toast: createToast(cornerOfPage),
261
264
  ...formsApi,
265
+ delivery: trackDelivery(eventsValue, listenersValue, new Set(handoff.handedOut ?? [])),
262
266
  }
263
267
 
264
268
  // ---- the page ----------------------------------------------------------------------------------
@@ -445,7 +449,7 @@ if (handoff.client) {
445
449
  if (live) {
446
450
  trackForms(root, page, api)
447
451
  trackReading({
448
- messages: handoff.messages ?? [],
452
+ messages: handoff.messages ?? {},
449
453
  events: () => events,
450
454
  send: ids => {
451
455
  append(READ, { ids }).catch(err => console.error('marking read:', err))
@@ -6,7 +6,7 @@
6
6
  // adds a ✕ that runs it and closes the toast. A toast stays short: what more there is to say goes in
7
7
  // `details`, which a ▸ unfolds under it; unfolded, it doesn't go by itself until folded again. Their look is the core's (server.ts), in the page's
8
8
  // theme colours.
9
- import type { Toast, ToastOptions } from '../define.ts'
9
+ import type { Toast, ToastBar, ToastOptions } from '../define.ts'
10
10
 
11
11
  let box: HTMLElement | null = null
12
12
  function place(corner: HTMLElement) {
@@ -22,7 +22,7 @@ function place(corner: HTMLElement) {
22
22
  export function createToast(corner: () => HTMLElement) {
23
23
  return function toast(
24
24
  text: string,
25
- { kind = 'info', ms = 7000, onClick, onDismiss, name, order = 0, details }: ToastOptions = {},
25
+ { kind = 'info', ms = 7000, onClick, onDismiss, name, order = 0, details, meta, bar }: ToastOptions = {},
26
26
  ): Toast {
27
27
  const el = document.createElement('button')
28
28
  let timer: ReturnType<typeof setTimeout> | undefined
@@ -39,6 +39,28 @@ export function createToast(corner: () => HTMLElement) {
39
39
  label.className = 'wp-toast-text'
40
40
  label.textContent = text
41
41
  el.append(label)
42
+ // a quiet toast's time at the right, and its bar under the line (a progress)
43
+ let metaEl: HTMLElement | null = null
44
+ let barEl: HTMLElement | null = null
45
+ const setMeta = (text: string | null | undefined) => {
46
+ if (!text) return void metaEl?.remove()
47
+ metaEl ??= Object.assign(document.createElement('span'), { className: 'wp-toast-meta' })
48
+ metaEl.textContent = text
49
+ if (!metaEl.isConnected) label.after(metaEl)
50
+ }
51
+ const setBar = (to: ToastBar | null | undefined) => {
52
+ if (!to) return void barEl?.remove()
53
+ if (!barEl) {
54
+ barEl = Object.assign(document.createElement('span'), { className: 'wp-toast-bar' })
55
+ barEl.append(document.createElement('span'))
56
+ }
57
+ barEl.dataset.tone = to.tone ?? 'accent'
58
+ barEl.toggleAttribute('data-indeterminate', to.value == null)
59
+ barEl.style.setProperty('--wp-toast-bar', to.value == null ? '' : `${Math.max(0, Math.min(1, to.value)) * 100}%`)
60
+ if (!barEl.isConnected) el.append(barEl)
61
+ }
62
+ setMeta(meta)
63
+ setBar(bar)
42
64
  let more: HTMLElement | null = null
43
65
  let detail: HTMLElement | null = null
44
66
  if (details) {
@@ -84,8 +106,10 @@ export function createToast(corner: () => HTMLElement) {
84
106
  place(corner()).append(el)
85
107
  return {
86
108
  close,
87
- update: next => {
109
+ update: (next, more) => {
88
110
  label.textContent = next
111
+ if (more && 'meta' in more) setMeta(more.meta)
112
+ if (more && 'bar' in more) setBar(more.bar)
89
113
  },
90
114
  hide: () => {
91
115
  el.hidden = true
package/src/define.ts CHANGED
@@ -195,6 +195,12 @@ export interface EventSpec {
195
195
  * event's id) has been in view; default false
196
196
  */
197
197
  forReader?: boolean
198
+ /**
199
+ * the fields of its `data` it can't be without, each a non-empty string (`['chat', 'text']`):
200
+ * `workpad append` and the API refuse one that lacks any, and one in the log that lacks any is
201
+ * no message for the reader, since nothing on the page can show it
202
+ */
203
+ requires?: string[]
198
204
  }
199
205
 
200
206
  export interface Project {
@@ -329,6 +335,11 @@ export interface CommandContext {
329
335
  running(): Promise<{ base: string; version: string } | null>
330
336
  /** the version of workpad this command runs */
331
337
  version: string
338
+ /**
339
+ * who runs it: `--agent`, else `WORKPAD_AGENT`, else the agent session a plugin names; null when
340
+ * none says. The `by` of what the command appends to the log itself.
341
+ */
342
+ agent: string | null
332
343
  /**
333
344
  * The project's state as data, and what an agent changes of it: the client of
334
345
  * `@bigmistqke/workpad/api`, connected to the project's server (started at its first call when
@@ -346,9 +357,10 @@ export interface CommandContext {
346
357
  status(page?: string): Promise<PageStatus[]>
347
358
  /**
348
359
  * 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.
360
+ * listen` does, for agent `agent`: the reader's and other agents', never its own (nor those of its
361
+ * `aliases`); also those from before, each counted as received (by `agent`) from then on; a reader's
362
+ * answer it received before and not handled again (`again`), another agent's message never twice.
363
+ * With `follow: false` only those there are now. Resolves with how many.
352
364
  * For the log's other primitives, import `@bigmistqke/workpad/log`.
353
365
  */
354
366
  handOut(
@@ -360,6 +372,8 @@ export interface CommandContext {
360
372
  signal?: AbortSignal
361
373
  mark?: boolean
362
374
  agent?: string | null
375
+ /** the other ids the agent goes by (its sessions before, the session beside WORKPAD_AGENT), read as events come */
376
+ aliases?: ReadonlySet<string>
363
377
  /** an event id: of the backlog before it, what someone received is skipped, the reader's aside (channelSince) */
364
378
  skipReceivedBefore?: string
365
379
  },
@@ -367,9 +381,9 @@ export interface CommandContext {
367
381
  /**
368
382
  * Listen on `page` at the project's server as `agent`, as `workpad listen` does, for as
369
383
  * 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.
384
+ * a page; resolves once the server holds it. `signal` lets go of it (the session moved on).
371
385
  */
372
- listen(page: string, options?: { agent?: string | null }): Promise<void>
386
+ listen(page: string, options?: { agent?: string | null; signal?: AbortSignal }): Promise<void>
373
387
  /**
374
388
  * Take the pages no one follows (`workpad listen --unfollowed`), as `agent`, until `signal`
375
389
  * aborts: listen on each, and hand the reader's answers there that no other agent received to
@@ -506,6 +520,21 @@ export interface IndexPage {
506
520
  unread: number
507
521
  /** the reader's answers no agent received */
508
522
  unreceived: number
523
+ /** the progress running on it that moved last (plugins/progress), or null: its line on the card */
524
+ progress?: IndexProgress | null
525
+ }
526
+
527
+ /** A running progress, as a card on the overview shows it. */
528
+ export interface IndexProgress {
529
+ id: string
530
+ note: string
531
+ /** how far, out of `total`; null without a count */
532
+ n: number | null
533
+ total: number | null
534
+ /** when it last moved */
535
+ ts: string
536
+ /** no update for long, or past its ETA */
537
+ stale: boolean
509
538
  }
510
539
 
511
540
  /** A project, as the shared server's index gives it (/api/index, the overview). */
@@ -627,8 +656,19 @@ export interface WpEvent {
627
656
  * session a plugin names. An agent's events reach every listener but this identity.
628
657
  */
629
658
  by?: string
659
+ /**
660
+ * the agent session it was written in, when the command line knew one and it isn't `by` (the
661
+ * writer was named by `--agent` or `WORKPAD_AGENT`): a listener known by that session doesn't
662
+ * get it either, it is its own
663
+ */
664
+ session?: string
630
665
  /** in the browser: a wait or watch handed this event to an agent */
631
666
  received?: true
667
+ /**
668
+ * in the browser: who received it first, as the page names them: the name the agent gave itself
669
+ * (`workpad agent name`), else its id (a long one cut short); absent when it gave neither
670
+ */
671
+ receivedBy?: string
632
672
  /** in the browser: the agent handled this event (acknowledged it, or replied to it) */
633
673
  handled?: true
634
674
  /**
@@ -660,8 +700,12 @@ export interface Listener {
660
700
 
661
701
  /** How a toast looks and acts (Api.toast). */
662
702
  export interface ToastOptions {
663
- /** its colour: `info` (the accent, the default), `warn`, or `alert` (as for what is new) */
664
- kind?: 'info' | 'warn' | 'alert'
703
+ /**
704
+ * its colour: `info` (the accent, the default), `warn`, or `alert` (as for what is new); or
705
+ * `quiet`, a card in the page's own surface and text colours, for what stays a while without
706
+ * asking anything (a progress), with `meta` and `bar`
707
+ */
708
+ kind?: 'info' | 'warn' | 'alert' | 'quiet'
665
709
  /** milliseconds before it goes, 7000 by default; 0: it stays until closed through its `Toast` */
666
710
  ms?: number
667
711
  /** what a click on it does (go to what it is about); a toast that goes by itself also closes */
@@ -677,12 +721,26 @@ export interface ToastOptions {
677
721
  * this under it (and keeps it open while it is read). Keep `text` a few words, and put the why here.
678
722
  */
679
723
  details?: string
724
+ /** a few muted words at the right of its line: a time (`~20 min left`, `just now`) */
725
+ meta?: string
726
+ /**
727
+ * a thin bar under its line: how far (0 to 1; null: no count, a stripe that moves), in the
728
+ * accent, or `good` (green) or `bad` (red)
729
+ */
730
+ bar?: ToastBar
731
+ }
732
+
733
+ /** A toast's bar (ToastOptions.bar). */
734
+ export interface ToastBar {
735
+ value: number | null
736
+ tone?: 'accent' | 'good' | 'bad'
680
737
  }
681
738
 
682
739
  /** A toast on the screen: change what it says (a count), hide it and show it again, or close it. */
683
740
  export interface Toast {
684
741
  close(): void
685
- update(text: string): void
742
+ /** its text, and its `meta` and `bar` when given (null takes one away) */
743
+ update(text: string, more?: { meta?: string | null; bar?: ToastBar | null }): void
686
744
  hide(): void
687
745
  show(): void
688
746
  }
@@ -780,6 +838,32 @@ export interface Api {
780
838
  * count). It changes when the forms are marked again: the log, the listeners or the page changed.
781
839
  */
782
840
  forms: Reactive<FormStatus[]>
841
+ /**
842
+ * Where each of the reader's events for an agent went (a form's answer, a chat message, a
843
+ * comment), by its id: on its way, waiting for an agent (or for anyone to follow the page at
844
+ * all), late, received by whom, handled. For a line beside what was sent. It changes with the
845
+ * log, with who listens, and as an answer waits long enough to be late (NOT_RECEIVED_AFTER).
846
+ */
847
+ delivery: Reactive<ReadonlyMap<string, Delivery>>
848
+ }
849
+
850
+ /**
851
+ * Where one of the reader's events for an agent went (Api.delivery):
852
+ * - sending: on its way to the server (retried while it doesn't answer)
853
+ * - waiting: in the log, and someone listens on the page: an agent will receive it
854
+ * - unfollowed: in the log, and no one listens on the page: it waits until an agent does
855
+ * - late: no agent received it for NOT_RECEIVED_AFTER (`nobody`: and no one listens now)
856
+ * - received: an agent has it (`by` says who, when it said)
857
+ * - handled: an agent is done with it: it said so, or replied
858
+ */
859
+ export interface Delivery {
860
+ state: 'sending' | 'waiting' | 'unfollowed' | 'late' | 'received' | 'handled'
861
+ /** a few words saying so, for beside what was sent */
862
+ text: string
863
+ /** received: who received it (WpEvent.receivedBy) */
864
+ by?: string
865
+ /** late: and no one listens on the page now */
866
+ nobody?: boolean
783
867
  }
784
868
 
785
869
  /**